diff --git a/Android/MMKV/build.gradle b/Android/MMKV/build.gradle index 7589b64e..49ea817e 100644 --- a/Android/MMKV/build.gradle +++ b/Android/MMKV/build.gradle @@ -8,7 +8,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:3.3.0' + classpath 'com.android.tools.build:gradle:3.3.2' classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.1' classpath 'digital.wup:android-maven-publish:3.6.2' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" diff --git a/Android/MMKV/gradle.properties b/Android/MMKV/gradle.properties index 6fcf8e89..6357fba3 100644 --- a/Android/MMKV/gradle.properties +++ b/Android/MMKV/gradle.properties @@ -12,6 +12,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.0.17 +VERSION_NAME_PREFIX=1.0.18 #VERSION_NAME_SUFFIX=-SNAPSHOT VERSION_NAME_SUFFIX= \ No newline at end of file diff --git a/Android/MMKV/mmkv/src/main/cpp/MMKV.cpp b/Android/MMKV/mmkv/src/main/cpp/MMKV.cpp index 62280ede..92a9d56b 100644 --- a/Android/MMKV/mmkv/src/main/cpp/MMKV.cpp +++ b/Android/MMKV/mmkv/src/main/cpp/MMKV.cpp @@ -76,7 +76,7 @@ MMKV::MMKV( , m_fileLock(m_metaFile.getFd()) , m_sharedProcessLock(&m_fileLock, SharedLockType) , m_exclusiveProcessLock(&m_fileLock, ExclusiveLockType) - , m_isInterProcess((mode & MMKV_MULTI_PROCESS) != 0) + , m_isInterProcess((mode & MMKV_MULTI_PROCESS) != 0 || (mode & CONTEXT_MODE_MULTI_PROCESS) != 0) , m_isAshmem((mode & MMKV_ASHMEM) != 0) { m_fd = -1; m_ptr = nullptr; @@ -198,8 +198,10 @@ void MMKV::initializeMMKV(const std::string &rootDir) { g_rootDir = rootDir; char *path = strdup(g_rootDir.c_str()); - mkPath(path); - free(path); + if (path) { + mkPath(path); + free(path); + } MMKVInfo("root dir: %s", g_rootDir.c_str()); } @@ -341,6 +343,7 @@ void MMKV::loadFromFile() { } else { auto strategic = onMMKVFileLengthError(m_mmapID); if (strategic == OnErrorRecover) { + writeAcutalSize(m_size - Fixed32Size); loadFromFile = true; needFullWriteback = true; } @@ -677,8 +680,8 @@ bool MMKV::ensureMemorySize(size_t newSize) { size_t lenNeeded = data.length() + offset + newSize; if (m_isAshmem) { if (lenNeeded > m_size) { - MMKVWarning("ashmem %s reach size limit:%zu, consider configure with larger size", - m_mmapID.c_str(), m_size); + MMKVError("ashmem %s reach size limit:%zu, consider configure with larger size", + m_mmapID.c_str(), m_size); return false; } } else { @@ -1211,15 +1214,45 @@ bool MMKV::getVectorForKey(const std::string &key, std::vector &res return false; } -size_t MMKV::getValueSizeForKey(const std::string &key) { +size_t MMKV::getValueSizeForKey(const std::string &key, bool actualSize) { if (key.empty()) { return 0; } SCOPEDLOCK(m_lock); auto &data = getDataForKey(key); + if (actualSize) { + CodedInputData input(data.getPtr(), data.length()); + auto length = input.readInt32(); + if (pbRawVarint32Size(length) + length == data.length()) { + return static_cast(length); + } + } return data.length(); } +int32_t MMKV::writeValueToBuffer(const std::string &key, void *ptr, int32_t size) { + if (key.empty()) { + return -1; + } + SCOPEDLOCK(m_lock); + auto &data = getDataForKey(key); + CodedInputData input(data.getPtr(), data.length()); + auto length = input.readInt32(); + auto offset = pbRawVarint32Size(length); + if (offset + length == data.length()) { + if (length <= size) { + memcpy(ptr, (uint8_t *) data.getPtr() + offset, length); + return length; + } + } else { + if (data.length() <= size) { + memcpy(ptr, data.getPtr(), data.length()); + return static_cast(data.length()); + } + } + return -1; +} + #pragma mark - enumerate bool MMKV::containsKey(const std::string &key) { @@ -1307,28 +1340,32 @@ bool MMKV::isFileValid(const std::string &mmapID) { uint32_t crcFile = 0; MMBuffer *data = readWholeFile(crcPath.c_str()); - if (data && data->getPtr()) { - MMKVMetaInfo metaInfo; - metaInfo.read(data->getPtr()); - crcFile = metaInfo.m_crcDigest; + if (data) { + if (data->getPtr()) { + MMKVMetaInfo metaInfo; + metaInfo.read(data->getPtr()); + crcFile = metaInfo.m_crcDigest; + } delete data; } else { return false; } const int offset = pbFixed32Size(0); - size_t actualSize = 0; + uint32_t crcDigest = 0; MMBuffer *fileData = readWholeFile(kvPath.c_str()); if (fileData) { - actualSize = CodedInputData(fileData->getPtr(), fileData->length()).readFixed32(); - if (actualSize > fileData->length() - offset) { - delete fileData; - return false; - } - - uint32_t crcDigest = (uint32_t) crc32(0, (const uint8_t *) fileData->getPtr() + offset, - (uint32_t) actualSize); + if (fileData->getPtr()) { + size_t actualSize = + CodedInputData(fileData->getPtr(), fileData->length()).readFixed32(); + if (actualSize > fileData->length() - offset) { + delete fileData; + return false; + } + crcDigest = (uint32_t) crc32(0, (const uint8_t *) fileData->getPtr() + offset, + (uint32_t) actualSize); + } delete fileData; return crcFile == crcDigest; } else { @@ -1338,8 +1375,10 @@ bool MMKV::isFileValid(const std::string &mmapID) { static void mkSpecialCharacterFileDirectory() { char *path = strdup((g_rootDir + "/" + SPECIAL_CHARACTER_DIRECTORY_NAME).c_str()); - mkPath(path); - free(path); + if (path) { + mkPath(path); + free(path); + } } static string md5(const string &value) { diff --git a/Android/MMKV/mmkv/src/main/cpp/MMKV.h b/Android/MMKV/mmkv/src/main/cpp/MMKV.h index 517641db..fb25a9a3 100644 --- a/Android/MMKV/mmkv/src/main/cpp/MMKV.h +++ b/Android/MMKV/mmkv/src/main/cpp/MMKV.h @@ -38,7 +38,8 @@ class AESCrypt; enum MMKVMode : uint32_t { MMKV_SINGLE_PROCESS = 0x1, MMKV_MULTI_PROCESS = 0x2, - MMKV_ASHMEM = 0x4, + CONTEXT_MODE_MULTI_PROCESS = 0x4, // in case someone mistakenly pass Context.MODE_MULTI_PROCESS + MMKV_ASHMEM = 0x8, }; class MMKV { @@ -191,7 +192,9 @@ class MMKV { bool getVectorForKey(const std::string &key, std::vector &result); - size_t getValueSizeForKey(const std::string &key); + size_t getValueSizeForKey(const std::string &key, bool acutalSize); + + int32_t writeValueToBuffer(const std::string &key, void *ptr, int32_t size); bool containsKey(const std::string &key); diff --git a/Android/MMKV/mmkv/src/main/cpp/MmapedFile.cpp b/Android/MMKV/mmkv/src/main/cpp/MmapedFile.cpp index 569134f5..279de1db 100644 --- a/Android/MMKV/mmkv/src/main/cpp/MmapedFile.cpp +++ b/Android/MMKV/mmkv/src/main/cpp/MmapedFile.cpp @@ -185,6 +185,9 @@ bool createFile(const std::string &filePath) { } else { // create parent dir char *path = strdup(filePath.c_str()); + if (!path) { + return false; + } auto ptr = strrchr(path, '/'); if (ptr) { *ptr = '\0'; diff --git a/Android/MMKV/mmkv/src/main/cpp/native-bridge.cpp b/Android/MMKV/mmkv/src/main/cpp/native-bridge.cpp index e63af5ec..44a8c067 100644 --- a/Android/MMKV/mmkv/src/main/cpp/native-bridge.cpp +++ b/Android/MMKV/mmkv/src/main/cpp/native-bridge.cpp @@ -160,7 +160,7 @@ static JNIEnv *getCurrentEnv() { MMKVRecoverStrategic onMMKVCRCCheckFail(const std::string &mmapID) { auto currentEnv = getCurrentEnv(); - if (g_currentJVM && g_callbackOnCRCFailID) { + if (currentEnv && g_callbackOnCRCFailID) { jstring str = string2jstring(currentEnv, mmapID); auto strategic = currentEnv->CallStaticIntMethod(g_cls, g_callbackOnCRCFailID, str); return static_cast(strategic); @@ -701,14 +701,12 @@ extern "C" JNIEXPORT void JNICALL Java_com_tencent_mmkv_MMKV_close(JNIEnv *env, } } -extern "C" JNIEXPORT jint JNICALL Java_com_tencent_mmkv_MMKV_valueSize(JNIEnv *env, - jobject instance, - jlong handle, - jstring oKey) { +extern "C" JNIEXPORT jint JNICALL Java_com_tencent_mmkv_MMKV_valueSize( + JNIEnv *env, jobject instance, jlong handle, jstring oKey, jboolean actualSize) { MMKV *kv = reinterpret_cast(handle); if (kv && oKey) { string key = jstring2string(env, oKey); - return kv->getValueSizeForKey(key); + return static_cast(kv->getValueSizeForKey(key, (bool) actualSize)); } return 0; } @@ -724,3 +722,31 @@ extern "C" JNIEXPORT void JNICALL Java_com_tencent_mmkv_MMKV_setLogReDirecting(J jboolean enable) { g_isLogRedirecting = (enable == JNI_TRUE); } + +extern "C" JNIEXPORT jlong JNICALL Java_com_tencent_mmkv_MMKV_createNB(JNIEnv *env, + jobject instance, + jint size) { + auto ptr = malloc(static_cast(size)); + if (!ptr) { + MMKVError("fail to create NativeBuffer:%s", strerror(errno)); + return 0; + } + return reinterpret_cast(ptr); +} + +extern "C" JNIEXPORT void JNICALL Java_com_tencent_mmkv_MMKV_destroyNB(JNIEnv *env, + jobject instance, + jlong pointer, + jint size) { + free(reinterpret_cast(pointer)); +} + +extern "C" JNIEXPORT jint JNICALL Java_com_tencent_mmkv_MMKV_writeValueToNB( + JNIEnv *env, jobject instance, jlong handle, jstring oKey, jlong pointer, jint size) { + MMKV *kv = reinterpret_cast(handle); + if (kv && oKey) { + string key = jstring2string(env, oKey); + return kv->writeValueToBuffer(key, reinterpret_cast(pointer), size); + } + return -1; +} diff --git a/Android/MMKV/mmkv/src/main/java/com/tencent/mmkv/MMKV.java b/Android/MMKV/mmkv/src/main/java/com/tencent/mmkv/MMKV.java index 144b4ee0..4f1e842b 100644 --- a/Android/MMKV/mmkv/src/main/java/com/tencent/mmkv/MMKV.java +++ b/Android/MMKV/mmkv/src/main/java/com/tencent/mmkv/MMKV.java @@ -126,7 +126,10 @@ public static void setLogLevel(MMKVLogLevel level) { static public final int MULTI_PROCESS_MODE = 0x2; - static private final int ASHMEM_MODE = 0x4; + // in case someone mistakenly pass Context.MODE_MULTI_PROCESS + static private final int CONTEXT_MODE_MULTI_PROCESS = 0x4; + + static private final int ASHMEM_MODE = 0x8; public static MMKV mmkvWithID(String mmapID) { if (rootDir == null) { @@ -436,7 +439,13 @@ public T decodeParcelable(String key, Class tClass, T // return the actual size consumption of the key's value // Note: might be a little bigger than value's length public int getValueSize(String key) { - return valueSize(nativeHandle, key); + return valueSize(nativeHandle, key, false); + } + + // return the actual size of the key's value + // String's length or byte[]'s length, etc + public int getValueActualSize(String key) { + return valueSize(nativeHandle, key, true); } public boolean containsKey(String key) { @@ -647,6 +656,24 @@ public static MMKV mmkvWithAshmemFD(String mmapID, int fd, int metaFD, String cr public native int ashmemMetaFD(); + // native buffer + public static NativeBuffer createNativeBuffer(int size) { + long pointer = createNB(size); + if (pointer <= 0) { + return null; + } + return new NativeBuffer(pointer, size); + } + + public static void destroyNativeBuffer(NativeBuffer buffer) { + destroyNB(buffer.pointer, buffer.size); + } + + // return size written, -1 on error + public int writeValueToNativeBuffer(String key, NativeBuffer buffer) { + return writeValueToNB(nativeHandle, key, buffer.pointer, buffer.size); + } + // callback handler private static MMKVHandler gCallbackHandler; private static boolean gWantLogReDirecting = false; @@ -778,9 +805,15 @@ private MMKV(long handle) { private native void removeValueForKey(long handle, String key); - private native int valueSize(long handle, String key); + private native int valueSize(long handle, String key, boolean actualSize); private static native void setLogLevel(int level); private static native void setLogReDirecting(boolean enable); + + private static native long createNB(int size); + + private static native void destroyNB(long pointer, int size); + + private native int writeValueToNB(long handle, String key, long pointer, int size); } diff --git a/Android/MMKV/mmkv/src/main/java/com/tencent/mmkv/MMKVContentProvider.java b/Android/MMKV/mmkv/src/main/java/com/tencent/mmkv/MMKVContentProvider.java index 1ec10b44..6f46bdd0 100644 --- a/Android/MMKV/mmkv/src/main/java/com/tencent/mmkv/MMKVContentProvider.java +++ b/Android/MMKV/mmkv/src/main/java/com/tencent/mmkv/MMKVContentProvider.java @@ -80,7 +80,9 @@ private static String queryAuthority(Context context) { PackageManager mgr = context.getPackageManager(); if (mgr != null) { ProviderInfo providerInfo = mgr.getProviderInfo(componentName, 0); - return providerInfo.authority; + if (providerInfo != null) { + return providerInfo.authority; + } } } catch (Exception e) { e.printStackTrace(); diff --git a/Android/MMKV/mmkv/src/main/java/com/tencent/mmkv/NativeBuffer.java b/Android/MMKV/mmkv/src/main/java/com/tencent/mmkv/NativeBuffer.java new file mode 100644 index 00000000..69525ff4 --- /dev/null +++ b/Android/MMKV/mmkv/src/main/java/com/tencent/mmkv/NativeBuffer.java @@ -0,0 +1,11 @@ +package com.tencent.mmkv; + +public final class NativeBuffer { + public long pointer; + public int size; + + public NativeBuffer(long ptr, int length) { + pointer = ptr; + size = length; + } +} diff --git a/Android/MMKV/mmkvdemo/build.gradle b/Android/MMKV/mmkvdemo/build.gradle index eb32af8a..51ee0a2c 100644 --- a/Android/MMKV/mmkvdemo/build.gradle +++ b/Android/MMKV/mmkvdemo/build.gradle @@ -57,14 +57,14 @@ repositories { // } jcenter() mavenCentral() - mavenLocal() +// mavenLocal() } dependencies { implementation fileTree(include: ['*.jar'], dir: 'libs') // implementation project(':mmkv') -// implementation 'com.tencent:mmkv:1.0.17' - implementation 'com.tencent:mmkv-static:1.0.17' + implementation 'com.tencent:mmkv:1.0.18' +// implementation 'com.tencent:mmkv-static:1.0.18' implementation 'com.android.support:appcompat-v7:28.0.0' implementation 'com.android.support.constraint:constraint-layout:1.1.3' testImplementation 'junit:junit:4.12' diff --git a/Android/MMKV/mmkvdemo/src/main/java/com/tencent/mmkvdemo/MainActivity.java b/Android/MMKV/mmkvdemo/src/main/java/com/tencent/mmkvdemo/MainActivity.java index 061440aa..d5af58e8 100644 --- a/Android/MMKV/mmkvdemo/src/main/java/com/tencent/mmkvdemo/MainActivity.java +++ b/Android/MMKV/mmkvdemo/src/main/java/com/tencent/mmkvdemo/MainActivity.java @@ -38,6 +38,7 @@ import com.tencent.mmkv.MMKVRecoverStrategic; import com.tencent.mmkv.MMKVLogLevel; import com.getkeepsafe.relinker.ReLinker; +import com.tencent.mmkv.NativeBuffer; import org.jetbrains.annotations.Nullable; @@ -200,7 +201,16 @@ private MMKV testMMKV(String mmapID, String cryptKey, boolean decodeOnly, String byte[] bytes = kv.decodeBytes("bytes"); Log.i("MMKV", "bytes: " + new String(bytes)); Log.i("MMKV", "bytes length = " + bytes.length - + ", value size consumption = " + kv.getValueSize("bytes")); + + ", value size consumption = " + kv.getValueSize("bytes") + + ", value size = " + kv.getValueActualSize("bytes")); + + int sizeNeeded = kv.getValueActualSize("bytes"); + NativeBuffer nativeBuffer = MMKV.createNativeBuffer(sizeNeeded); + if (nativeBuffer != null) { + int size = kv.writeValueToNativeBuffer("bytes", nativeBuffer); + Log.i("MMKV", "size Needed = " + sizeNeeded + " written size = " + size); + MMKV.destroyNativeBuffer(nativeBuffer); + } if (!decodeOnly) { TestParcelable testParcelable = new TestParcelable(1024, "Hi Parcelable"); diff --git a/CHANGELOG.md b/CHANGELOG.md index 0b309bbe..73645823 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,23 @@ # MMKV Change Log +## v1.0.18 / 2019-03-14 +### iOS / macOS +What's new + +* Fix a bug that defaultValue was not returned while decoding a `NSCoding` value. +* Fix a compile error on static linking MMKV while openssl is static linked too. + +### Android +What's new + +* Introducing **Native Buffer**. Checkout [wiki](https://github.com/Tencent/MMKV/wiki/android_advance#native-buffer) for details. +* Fix a potential crash when trying to recover data from file length error. +* Protect from mistakenly passing `Context.MODE_MULTI_PROCESS` to init MMKV. + + +### Win32 +* Fix a potential crash when trying to recover data from file length error. + ## v1.0.17 / 2019-01-25 ### iOS / macOS What's new diff --git a/MMKV.podspec b/MMKV.podspec index 5072ac67..0771ea4e 100644 --- a/MMKV.podspec +++ b/MMKV.podspec @@ -1,7 +1,7 @@ Pod::Spec.new do |s| s.name = "MMKV" - s.version = "1.0.17" + s.version = "1.0.18" s.summary = "MMKV is a cross-platform key-value storage framework developed by WeChat." s.description = <<-DESC diff --git a/README.md b/README.md index 2a9f4190..4a9d41d7 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ [![license](https://img.shields.io/badge/license-BSD_3-brightgreen.svg?style=flat)](https://github.com/Tencent/MMKV/blob/master/LICENSE.TXT) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/Tencent/MMKV/pulls) -[![Release Version](https://img.shields.io/badge/release-1.0.17-brightgreen.svg)](https://github.com/Tencent/MMKV/releases) +[![Release Version](https://img.shields.io/badge/release-1.0.18-brightgreen.svg)](https://github.com/Tencent/MMKV/releases) [![Platform](https://img.shields.io/badge/Platform-%20iOS%20%7C%20Android-brightgreen.svg)](https://github.com/Tencent/MMKV/wiki/home) 中文版本请参看[这里](./readme_cn.md) @@ -75,8 +75,8 @@ Add the following lines to `build.gradle` on your app module: ```gradle dependencies { - implementation 'com.tencent:mmkv:1.0.17' - // replace "1.0.17" with any available version + implementation 'com.tencent:mmkv:1.0.18' + // replace "1.0.18" with any available version } ``` diff --git a/Win32/MMKV/MMKV.cpp b/Win32/MMKV/MMKV.cpp index 446c1c7f..8c7e8c6e 100644 --- a/Win32/MMKV/MMKV.cpp +++ b/Win32/MMKV/MMKV.cpp @@ -238,6 +238,7 @@ void MMKV::loadFromFile() { if (strategic == OnErrorRecover) { loadFromFile = true; needFullWriteback = true; + writeAcutalSize(m_size - Fixed32Size); } } } @@ -1290,4 +1291,4 @@ static MMKVRecoverStrategic onMMKVFileLengthError(const std::string &mmapID) { return g_errorHandler(mmapID, MMKVErrorType::MMKVFileLength); } return OnErrorDiscard; -} \ No newline at end of file +} diff --git a/iOS/MMKV/MMKV.xcodeproj/project.pbxproj b/iOS/MMKV/MMKV.xcodeproj/project.pbxproj index 86bbbd68..0e3699ed 100644 --- a/iOS/MMKV/MMKV.xcodeproj/project.pbxproj +++ b/iOS/MMKV/MMKV.xcodeproj/project.pbxproj @@ -16,12 +16,12 @@ CB1FD4CD2046ACBC00931B5F /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CB1FD4CC2046ACBC00931B5F /* Foundation.framework */; }; CB6DEFA9212E6F010024F0E8 /* AESCrypt.h in Headers */ = {isa = PBXBuildFile; fileRef = CB6DEF9F212E6F010024F0E8 /* AESCrypt.h */; }; CB6DEFAA212E6F010024F0E8 /* opensslconf.h in Headers */ = {isa = PBXBuildFile; fileRef = CB6DEFA1212E6F010024F0E8 /* opensslconf.h */; }; - CB6DEFAB212E6F010024F0E8 /* cfb128.c in Sources */ = {isa = PBXBuildFile; fileRef = CB6DEFA2212E6F010024F0E8 /* cfb128.c */; }; - CB6DEFAC212E6F010024F0E8 /* cfb128.c in Sources */ = {isa = PBXBuildFile; fileRef = CB6DEFA2212E6F010024F0E8 /* cfb128.c */; }; - CB6DEFAD212E6F010024F0E8 /* aes_core.c in Sources */ = {isa = PBXBuildFile; fileRef = CB6DEFA3212E6F010024F0E8 /* aes_core.c */; }; - CB6DEFAE212E6F010024F0E8 /* aes_core.c in Sources */ = {isa = PBXBuildFile; fileRef = CB6DEFA3212E6F010024F0E8 /* aes_core.c */; }; - CB6DEFAF212E6F010024F0E8 /* aes_cfb.c in Sources */ = {isa = PBXBuildFile; fileRef = CB6DEFA4212E6F010024F0E8 /* aes_cfb.c */; }; - CB6DEFB0212E6F010024F0E8 /* aes_cfb.c in Sources */ = {isa = PBXBuildFile; fileRef = CB6DEFA4212E6F010024F0E8 /* aes_cfb.c */; }; + CB6DEFAB212E6F010024F0E8 /* cfb128.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CB6DEFA2212E6F010024F0E8 /* cfb128.cpp */; }; + CB6DEFAC212E6F010024F0E8 /* cfb128.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CB6DEFA2212E6F010024F0E8 /* cfb128.cpp */; }; + CB6DEFAD212E6F010024F0E8 /* aes_core.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CB6DEFA3212E6F010024F0E8 /* aes_core.cpp */; }; + CB6DEFAE212E6F010024F0E8 /* aes_core.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CB6DEFA3212E6F010024F0E8 /* aes_core.cpp */; }; + CB6DEFAF212E6F010024F0E8 /* aes_cfb.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CB6DEFA4212E6F010024F0E8 /* aes_cfb.cpp */; }; + CB6DEFB0212E6F010024F0E8 /* aes_cfb.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CB6DEFA4212E6F010024F0E8 /* aes_cfb.cpp */; }; CB6DEFB1212E6F010024F0E8 /* aes.h in Headers */ = {isa = PBXBuildFile; fileRef = CB6DEFA5212E6F010024F0E8 /* aes.h */; }; CB6DEFB2212E6F010024F0E8 /* modes.h in Headers */ = {isa = PBXBuildFile; fileRef = CB6DEFA6212E6F010024F0E8 /* modes.h */; }; CB6DEFB3212E6F010024F0E8 /* aes_locl.h in Headers */ = {isa = PBXBuildFile; fileRef = CB6DEFA7212E6F010024F0E8 /* aes_locl.h */; }; @@ -45,10 +45,10 @@ CBDF32912192C0340028DB4D /* MMKVHandler.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = CBDF328F2192C0000028DB4D /* MMKVHandler.h */; }; CBF00476217DD7D0001BB3ED /* md5.h in Headers */ = {isa = PBXBuildFile; fileRef = CBF00471217DD7D0001BB3ED /* md5.h */; }; CBF00477217DD7D0001BB3ED /* md32_common.h in Headers */ = {isa = PBXBuildFile; fileRef = CBF00472217DD7D0001BB3ED /* md32_common.h */; }; - CBF00478217DD7D0001BB3ED /* md5_dgst.c in Sources */ = {isa = PBXBuildFile; fileRef = CBF00473217DD7D0001BB3ED /* md5_dgst.c */; }; - CBF00479217DD7D0001BB3ED /* md5_dgst.c in Sources */ = {isa = PBXBuildFile; fileRef = CBF00473217DD7D0001BB3ED /* md5_dgst.c */; }; - CBF0047A217DD7D0001BB3ED /* md5_one.c in Sources */ = {isa = PBXBuildFile; fileRef = CBF00474217DD7D0001BB3ED /* md5_one.c */; }; - CBF0047B217DD7D0001BB3ED /* md5_one.c in Sources */ = {isa = PBXBuildFile; fileRef = CBF00474217DD7D0001BB3ED /* md5_one.c */; }; + CBF00478217DD7D0001BB3ED /* md5_dgst.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CBF00473217DD7D0001BB3ED /* md5_dgst.cpp */; }; + CBF00479217DD7D0001BB3ED /* md5_dgst.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CBF00473217DD7D0001BB3ED /* md5_dgst.cpp */; }; + CBF0047A217DD7D0001BB3ED /* md5_one.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CBF00474217DD7D0001BB3ED /* md5_one.cpp */; }; + CBF0047B217DD7D0001BB3ED /* md5_one.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CBF00474217DD7D0001BB3ED /* md5_one.cpp */; }; CBF0047C217DD7D0001BB3ED /* md5_locl.h in Headers */ = {isa = PBXBuildFile; fileRef = CBF00475217DD7D0001BB3ED /* md5_locl.h */; }; /* End PBXBuildFile section */ @@ -84,9 +84,9 @@ CB1FD5152049382F00931B5F /* ScopedLock.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = ScopedLock.hpp; sourceTree = ""; }; CB6DEF9F212E6F010024F0E8 /* AESCrypt.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AESCrypt.h; sourceTree = ""; }; CB6DEFA1212E6F010024F0E8 /* opensslconf.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = opensslconf.h; sourceTree = ""; }; - CB6DEFA2212E6F010024F0E8 /* cfb128.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = cfb128.c; sourceTree = ""; }; - CB6DEFA3212E6F010024F0E8 /* aes_core.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = aes_core.c; sourceTree = ""; }; - CB6DEFA4212E6F010024F0E8 /* aes_cfb.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = aes_cfb.c; sourceTree = ""; }; + CB6DEFA2212E6F010024F0E8 /* cfb128.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = cfb128.cpp; sourceTree = ""; }; + CB6DEFA3212E6F010024F0E8 /* aes_core.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = aes_core.cpp; sourceTree = ""; }; + CB6DEFA4212E6F010024F0E8 /* aes_cfb.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = aes_cfb.cpp; sourceTree = ""; }; CB6DEFA5212E6F010024F0E8 /* aes.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = aes.h; sourceTree = ""; }; CB6DEFA6212E6F010024F0E8 /* modes.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = modes.h; sourceTree = ""; }; CB6DEFA7212E6F010024F0E8 /* aes_locl.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = aes_locl.h; sourceTree = ""; }; @@ -102,8 +102,8 @@ CBDF328F2192C0000028DB4D /* MMKVHandler.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MMKVHandler.h; sourceTree = ""; }; CBF00471217DD7D0001BB3ED /* md5.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = md5.h; sourceTree = ""; }; CBF00472217DD7D0001BB3ED /* md32_common.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = md32_common.h; sourceTree = ""; }; - CBF00473217DD7D0001BB3ED /* md5_dgst.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = md5_dgst.c; sourceTree = ""; }; - CBF00474217DD7D0001BB3ED /* md5_one.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = md5_one.c; sourceTree = ""; }; + CBF00473217DD7D0001BB3ED /* md5_dgst.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = md5_dgst.cpp; sourceTree = ""; }; + CBF00474217DD7D0001BB3ED /* md5_one.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = md5_one.cpp; sourceTree = ""; }; CBF00475217DD7D0001BB3ED /* md5_locl.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = md5_locl.h; sourceTree = ""; }; /* End PBXFileReference section */ @@ -196,14 +196,14 @@ CB6DEFA0212E6F010024F0E8 /* openssl */ = { isa = PBXGroup; children = ( - CB6DEFA4212E6F010024F0E8 /* aes_cfb.c */, - CB6DEFA3212E6F010024F0E8 /* aes_core.c */, + CB6DEFA4212E6F010024F0E8 /* aes_cfb.cpp */, + CB6DEFA3212E6F010024F0E8 /* aes_core.cpp */, CB6DEFA7212E6F010024F0E8 /* aes_locl.h */, CB6DEFA5212E6F010024F0E8 /* aes.h */, - CB6DEFA2212E6F010024F0E8 /* cfb128.c */, - CBF00473217DD7D0001BB3ED /* md5_dgst.c */, + CB6DEFA2212E6F010024F0E8 /* cfb128.cpp */, + CBF00473217DD7D0001BB3ED /* md5_dgst.cpp */, CBF00475217DD7D0001BB3ED /* md5_locl.h */, - CBF00474217DD7D0001BB3ED /* md5_one.c */, + CBF00474217DD7D0001BB3ED /* md5_one.cpp */, CBF00471217DD7D0001BB3ED /* md5.h */, CBF00472217DD7D0001BB3ED /* md32_common.h */, CB6DEFA6212E6F010024F0E8 /* modes.h */, @@ -334,15 +334,15 @@ files = ( CB1FD4C320469B8800931B5F /* MiniCodedOutputData.mm in Sources */, CB8FD91C21F1B8C10005EE8F /* MMKVLog.mm in Sources */, - CB6DEFAB212E6F010024F0E8 /* cfb128.c in Sources */, - CB6DEFAF212E6F010024F0E8 /* aes_cfb.c in Sources */, + CB6DEFAB212E6F010024F0E8 /* cfb128.cpp in Sources */, + CB6DEFAF212E6F010024F0E8 /* aes_cfb.cpp in Sources */, CB1FD4962046984F00931B5F /* MMKV.mm in Sources */, CB1FD4C520469B8800931B5F /* MiniPBUtility.mm in Sources */, - CB6DEFAD212E6F010024F0E8 /* aes_core.c in Sources */, + CB6DEFAD212E6F010024F0E8 /* aes_core.cpp in Sources */, CB1FD4C420469B8800931B5F /* MiniCodedInputData.mm in Sources */, CB865F5620FF33F600427B9D /* MemoryFile.mm in Sources */, - CBF00478217DD7D0001BB3ED /* md5_dgst.c in Sources */, - CBF0047A217DD7D0001BB3ED /* md5_one.c in Sources */, + CBF00478217DD7D0001BB3ED /* md5_dgst.cpp in Sources */, + CBF0047A217DD7D0001BB3ED /* md5_one.cpp in Sources */, CB6DEFB4212E6F010024F0E8 /* AESCrypt.mm in Sources */, CB1FD4C620469B8800931B5F /* MiniPBCoder.mm in Sources */, ); @@ -354,15 +354,15 @@ files = ( CBC1A8FE20DA948A00AD5087 /* MiniCodedInputData.mm in Sources */, CB8FD91D21F1B8C10005EE8F /* MMKVLog.mm in Sources */, - CB6DEFAC212E6F010024F0E8 /* cfb128.c in Sources */, - CB6DEFB0212E6F010024F0E8 /* aes_cfb.c in Sources */, + CB6DEFAC212E6F010024F0E8 /* cfb128.cpp in Sources */, + CB6DEFB0212E6F010024F0E8 /* aes_cfb.cpp in Sources */, CBC1A8FF20DA948A00AD5087 /* MiniCodedOutputData.mm in Sources */, CBC1A90020DA948A00AD5087 /* MiniPBCoder.mm in Sources */, - CB6DEFAE212E6F010024F0E8 /* aes_core.c in Sources */, + CB6DEFAE212E6F010024F0E8 /* aes_core.cpp in Sources */, CBC1A90120DA948A00AD5087 /* MiniPBUtility.mm in Sources */, CB865F5720FF33F600427B9D /* MemoryFile.mm in Sources */, - CBF00479217DD7D0001BB3ED /* md5_dgst.c in Sources */, - CBF0047B217DD7D0001BB3ED /* md5_one.c in Sources */, + CBF00479217DD7D0001BB3ED /* md5_dgst.cpp in Sources */, + CBF0047B217DD7D0001BB3ED /* md5_one.cpp in Sources */, CB6DEFB5212E6F010024F0E8 /* AESCrypt.mm in Sources */, CBC1A90220DA948A00AD5087 /* MMKV.mm in Sources */, ); @@ -547,6 +547,7 @@ DYLIB_COMPATIBILITY_VERSION = 1; DYLIB_CURRENT_VERSION = 1; DYLIB_INSTALL_NAME_BASE = "@rpath"; + GENERATE_MASTER_OBJECT_FILE = NO; INFOPLIST_FILE = "$(SRCROOT)/MMKV/Resources/Info.plist"; INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; IPHONEOS_DEPLOYMENT_TARGET = 8.0; @@ -585,6 +586,7 @@ DYLIB_COMPATIBILITY_VERSION = 1; DYLIB_CURRENT_VERSION = 1; DYLIB_INSTALL_NAME_BASE = "@rpath"; + GENERATE_MASTER_OBJECT_FILE = NO; INFOPLIST_FILE = "$(SRCROOT)/MMKV/Resources/Info.plist"; INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; IPHONEOS_DEPLOYMENT_TARGET = 8.0; diff --git a/iOS/MMKV/MMKV/MMKV.mm b/iOS/MMKV/MMKV/MMKV.mm index 7be53e9f..20de7b92 100644 --- a/iOS/MMKV/MMKV/MMKV.mm +++ b/iOS/MMKV/MMKV/MMKV.mm @@ -1181,7 +1181,11 @@ - (nullable NSString *)getStringForKey:(NSString *)key defaultValue:(nullable NS if (key.length <= 0) { return defaultValue; } - return [self getObjectOfClass:NSString.class forKey:key]; + NSString *valueString = [self getObjectOfClass:NSString.class forKey:key]; + if (!valueString) { + valueString = defaultValue; + } + return valueString; } - (nullable NSDate *)getDateForKey:(NSString *)key { @@ -1191,7 +1195,11 @@ - (nullable NSDate *)getDateForKey:(NSString *)key defaultValue:(nullable NSDate if (key.length <= 0) { return defaultValue; } - return [self getObjectOfClass:NSDate.class forKey:key]; + NSDate *valueDate = [self getObjectOfClass:NSDate.class forKey:key]; + if (!valueDate) { + valueDate = defaultValue; + } + return valueDate; } - (nullable NSData *)getDataForKey:(NSString *)key { @@ -1201,7 +1209,11 @@ - (nullable NSData *)getDataForKey:(NSString *)key defaultValue:(nullable NSData if (key.length <= 0) { return defaultValue; } - return [self getObjectOfClass:NSData.class forKey:key]; + NSData *valueData = [self getObjectOfClass:NSData.class forKey:key]; + if (!valueData) { + valueData = defaultValue; + } + return valueData; } - (size_t)getValueSizeForKey:(NSString *)key NS_SWIFT_NAME(valueSize(forKey:)) { @@ -1483,7 +1495,7 @@ + (BOOL)tranlateData:(id)obj key:(NSString *)key kv:(MMKV *)kv { static NSString *md5(NSString *value) { unsigned char md[MD5_DIGEST_LENGTH] = {0}; char tmp[3] = {0}, buf[33] = {0}; - MD5((unsigned char *) value.UTF8String, [value lengthOfBytesUsingEncoding:NSUTF8StringEncoding], md); + openssl::MD5((unsigned char *) value.UTF8String, [value lengthOfBytesUsingEncoding:NSUTF8StringEncoding], md); for (int i = 0; i < MD5_DIGEST_LENGTH; i++) { sprintf(tmp, "%2.2x", md[i]); strcat(buf, tmp); diff --git a/iOS/MMKV/MMKV/Resources/Info.plist b/iOS/MMKV/MMKV/Resources/Info.plist index b1edd161..cd9d0cce 100644 --- a/iOS/MMKV/MMKV/Resources/Info.plist +++ b/iOS/MMKV/MMKV/Resources/Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType FMWK CFBundleShortVersionString - 1.0.17 + 1.0.18 CFBundleVersion $(CURRENT_PROJECT_VERSION) NSPrincipalClass diff --git a/iOS/MMKV/MMKV/aes/AESCrypt.h b/iOS/MMKV/MMKV/aes/AESCrypt.h index 0196bd9e..ea481f6c 100644 --- a/iOS/MMKV/MMKV/aes/AESCrypt.h +++ b/iOS/MMKV/MMKV/aes/AESCrypt.h @@ -31,7 +31,7 @@ constexpr size_t AES_KEY_BITSET_LEN = 128; class AESCrypt { unsigned char m_vector[AES_KEY_LEN] = {0}; unsigned char m_key[AES_KEY_LEN] = {0}; - AES_KEY m_aesKey = {0}; + openssl::AES_KEY m_aesKey = {0}; int m_number = 0; public: diff --git a/iOS/MMKV/MMKV/aes/AESCrypt.mm b/iOS/MMKV/MMKV/aes/AESCrypt.mm index 48481e89..5e857fee 100644 --- a/iOS/MMKV/MMKV/aes/AESCrypt.mm +++ b/iOS/MMKV/MMKV/aes/AESCrypt.mm @@ -29,7 +29,7 @@ memcpy(m_key, key, (keyLength > AES_KEY_LEN) ? AES_KEY_LEN : keyLength); memcpy(m_vector, m_key, AES_KEY_LEN); - int ret = AES_set_encrypt_key(m_key, AES_KEY_BITSET_LEN, &m_aesKey); + int ret = openssl::AES_set_encrypt_key(m_key, AES_KEY_BITSET_LEN, &m_aesKey); assert(ret == 0); } } @@ -49,14 +49,14 @@ if (!input || !output || length == 0) { return; } - AES_cfb128_encrypt(input, output, length, &m_aesKey, m_vector, &m_number, AES_ENCRYPT); + openssl::AES_cfb128_encrypt(input, output, length, &m_aesKey, m_vector, &m_number, AES_ENCRYPT); } void AESCrypt::decrypt(const unsigned char *input, unsigned char *output, size_t length) { if (!input || !output || length == 0) { return; } - AES_cfb128_encrypt(input, output, length, &m_aesKey, m_vector, &m_number, AES_DECRYPT); + openssl::AES_cfb128_encrypt(input, output, length, &m_aesKey, m_vector, &m_number, AES_DECRYPT); } #ifndef NDEBUG diff --git a/iOS/MMKV/MMKV/aes/openssl/aes.h b/iOS/MMKV/MMKV/aes/openssl/aes.h index 33da448c..b671f614 100644 --- a/iOS/MMKV/MMKV/aes/openssl/aes.h +++ b/iOS/MMKV/MMKV/aes/openssl/aes.h @@ -13,9 +13,8 @@ # include "opensslconf.h" # include -# ifdef __cplusplus -extern "C" { -# endif + +namespace openssl { # define AES_ENCRYPT 1 # define AES_DECRYPT 0 @@ -48,8 +47,6 @@ void AES_cfb128_encrypt(const unsigned char *in, unsigned char *out, size_t length, const AES_KEY *key, unsigned char *ivec, int *num, const int enc); -# ifdef __cplusplus -} -# endif +} // namespace openssl #endif diff --git a/iOS/MMKV/MMKV/aes/openssl/aes_cfb.c b/iOS/MMKV/MMKV/aes/openssl/aes_cfb.cpp similarity index 95% rename from iOS/MMKV/MMKV/aes/openssl/aes_cfb.c rename to iOS/MMKV/MMKV/aes/openssl/aes_cfb.cpp index ffe6438b..23fe8019 100644 --- a/iOS/MMKV/MMKV/aes/openssl/aes_cfb.c +++ b/iOS/MMKV/MMKV/aes/openssl/aes_cfb.cpp @@ -15,6 +15,7 @@ * The extra state information to record how much of the 128bit block we have * used is contained in *num; */ +namespace openssl { void AES_cfb128_encrypt(const unsigned char *in, unsigned char *out, size_t length, const AES_KEY *key, @@ -24,3 +25,5 @@ void AES_cfb128_encrypt(const unsigned char *in, unsigned char *out, CRYPTO_cfb128_encrypt(in, out, length, key, ivec, num, enc, (block128_f) AES_encrypt); } + +} // namespace openssl diff --git a/iOS/MMKV/MMKV/aes/openssl/aes_core.c b/iOS/MMKV/MMKV/aes/openssl/aes_core.cpp similarity index 99% rename from iOS/MMKV/MMKV/aes/openssl/aes_core.c rename to iOS/MMKV/MMKV/aes/openssl/aes_core.cpp index 2f8c7d45..4505aada 100644 --- a/iOS/MMKV/MMKV/aes/openssl/aes_core.c +++ b/iOS/MMKV/MMKV/aes/openssl/aes_core.cpp @@ -42,6 +42,8 @@ #include "aes.h" #include "aes_locl.h" +namespace openssl { + /*- Te0[x] = S [x].[02, 01, 01, 03]; Te1[x] = S [x].[03, 02, 01, 01]; @@ -627,6 +629,7 @@ static const u32 rcon[] = { 0x1B000000, 0x36000000, /* for 128-bit blocks, Rijndael never uses more than 10 rcon values */ }; + /** * Expand the cipher key into the encryption key schedule. */ @@ -919,3 +922,5 @@ void AES_encrypt(const unsigned char *in, unsigned char *out, rk[3]; PUTU32(out + 12, s3); } + +} // namespace openssl diff --git a/iOS/MMKV/MMKV/aes/openssl/cfb128.c b/iOS/MMKV/MMKV/aes/openssl/cfb128.cpp similarity index 98% rename from iOS/MMKV/MMKV/aes/openssl/cfb128.c rename to iOS/MMKV/MMKV/aes/openssl/cfb128.cpp index 3f5a809b..019e28bf 100644 --- a/iOS/MMKV/MMKV/aes/openssl/cfb128.c +++ b/iOS/MMKV/MMKV/aes/openssl/cfb128.cpp @@ -11,6 +11,8 @@ #include "modes.h" #include +namespace openssl { + /* * The input and output encrypted as though 128bit cfb mode is being used. * The extra state information to record how much of the 128bit block we have @@ -129,3 +131,5 @@ void CRYPTO_cfb128_encrypt(const unsigned char *in, unsigned char *out, *num = n; } } + +} // namespace openssl diff --git a/iOS/MMKV/MMKV/aes/openssl/md32_common.h b/iOS/MMKV/MMKV/aes/openssl/md32_common.h index dbdddc65..08af38c0 100755 --- a/iOS/MMKV/MMKV/aes/openssl/md32_common.h +++ b/iOS/MMKV/MMKV/aes/openssl/md32_common.h @@ -81,9 +81,9 @@ #ifndef HASH_UPDATE # error "HASH_UPDATE must be defined!" #endif -#ifndef HASH_TRANSFORM -# error "HASH_TRANSFORM must be defined!" -#endif +//#ifndef HASH_TRANSFORM +//# error "HASH_TRANSFORM must be defined!" +//#endif #ifndef HASH_FINAL # error "HASH_FINAL must be defined!" #endif @@ -126,7 +126,7 @@ int HASH_UPDATE(HASH_CTX *c, const void *data_, size_t len) { - const unsigned char *data = data_; + auto data = (const unsigned char *) data_; unsigned char *p; HASH_LONG l; size_t n; @@ -182,10 +182,10 @@ int HASH_UPDATE(HASH_CTX *c, const void *data_, size_t len) return 1; } -void HASH_TRANSFORM(HASH_CTX *c, const unsigned char *data) -{ - HASH_BLOCK_DATA_ORDER(c, data, 1); -} +//void HASH_TRANSFORM(HASH_CTX *c, const unsigned char *data) +//{ +// HASH_BLOCK_DATA_ORDER(c, data, 1); +//} int HASH_FINAL(unsigned char *md, HASH_CTX *c) { diff --git a/iOS/MMKV/MMKV/aes/openssl/md5.h b/iOS/MMKV/MMKV/aes/openssl/md5.h index 18fac685..a94893f6 100755 --- a/iOS/MMKV/MMKV/aes/openssl/md5.h +++ b/iOS/MMKV/MMKV/aes/openssl/md5.h @@ -14,9 +14,8 @@ # ifndef OPENSSL_NO_MD5 # include -# ifdef __cplusplus -extern "C" { -# endif + +namespace openssl { /* * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @@ -40,10 +39,9 @@ int MD5_Init(MD5_CTX *c); int MD5_Update(MD5_CTX *c, const void *data, size_t len); int MD5_Final(unsigned char *md, MD5_CTX *c); unsigned char *MD5(const unsigned char *d, size_t n, unsigned char *md); -void MD5_Transform(MD5_CTX *c, const unsigned char *b); -# ifdef __cplusplus -} -# endif +//void MD5_Transform(MD5_CTX *c, const unsigned char *b); + +} // namespace openssl # endif #endif diff --git a/iOS/MMKV/MMKV/aes/openssl/md5_dgst.c b/iOS/MMKV/MMKV/aes/openssl/md5_dgst.cpp similarity index 97% rename from iOS/MMKV/MMKV/aes/openssl/md5_dgst.c rename to iOS/MMKV/MMKV/aes/openssl/md5_dgst.cpp index d3b89ff0..c76d41a1 100755 --- a/iOS/MMKV/MMKV/aes/openssl/md5_dgst.c +++ b/iOS/MMKV/MMKV/aes/openssl/md5_dgst.cpp @@ -10,6 +10,7 @@ #include #include "md5_locl.h" +namespace openssl { /* * Implemented from RFC1321 The MD5 Message-Digest Algorithm */ @@ -35,8 +36,8 @@ int MD5_Init(MD5_CTX *c) # endif void md5_block_data_order(MD5_CTX *c, const void *data_, size_t num) { - const unsigned char *data = data_; - register unsigned MD32_REG_T A, B, C, D, l; + auto data = (const unsigned char *) data_; + unsigned MD32_REG_T A, B, C, D, l; # ifndef MD32_XARRAY /* See comment in crypto/sha/sha_locl.h for details. */ unsigned MD32_REG_T XX0, XX1, XX2, XX3, XX4, XX5, XX6, XX7, @@ -161,3 +162,5 @@ void md5_block_data_order(MD5_CTX *c, const void *data_, size_t num) } } #endif + +} // namespace openssl diff --git a/iOS/MMKV/MMKV/aes/openssl/md5_locl.h b/iOS/MMKV/MMKV/aes/openssl/md5_locl.h index 03e68d44..d20eaed7 100755 --- a/iOS/MMKV/MMKV/aes/openssl/md5_locl.h +++ b/iOS/MMKV/MMKV/aes/openssl/md5_locl.h @@ -11,6 +11,8 @@ #include #include "md5.h" +namespace openssl { + void md5_block_data_order(MD5_CTX *c, const void *p, size_t num); #define DATA_ORDER_IS_LITTLE_ENDIAN @@ -19,7 +21,7 @@ void md5_block_data_order(MD5_CTX *c, const void *p, size_t num); #define HASH_CTX MD5_CTX #define HASH_CBLOCK MD5_CBLOCK #define HASH_UPDATE MD5_Update -#define HASH_TRANSFORM MD5_Transform +//#define HASH_TRANSFORM MD5_Transform #define HASH_FINAL MD5_Final #define HASH_MAKE_STRING(c,s) do { \ unsigned long ll; \ @@ -66,3 +68,5 @@ void md5_block_data_order(MD5_CTX *c, const void *p, size_t num); a+=((k)+(t)+I((b),(c),(d))); \ a=ROTATE(a,s); \ a+=b; }; + +} // namespace openssl diff --git a/iOS/MMKV/MMKV/aes/openssl/md5_one.c b/iOS/MMKV/MMKV/aes/openssl/md5_one.cpp similarity index 93% rename from iOS/MMKV/MMKV/aes/openssl/md5_one.c rename to iOS/MMKV/MMKV/aes/openssl/md5_one.cpp index 30d56c0d..12278ef5 100755 --- a/iOS/MMKV/MMKV/aes/openssl/md5_one.c +++ b/iOS/MMKV/MMKV/aes/openssl/md5_one.cpp @@ -11,6 +11,8 @@ #include #include "md5.h" +namespace openssl { + unsigned char *MD5(const unsigned char *d, size_t n, unsigned char *md) { MD5_CTX c; @@ -24,3 +26,5 @@ unsigned char *MD5(const unsigned char *d, size_t n, unsigned char *md) MD5_Final(md, &c); return md; } + +} // namespace openssl diff --git a/iOS/MMKV/MMKV/aes/openssl/modes.h b/iOS/MMKV/MMKV/aes/openssl/modes.h index 6fab3d6f..e8e3f536 100644 --- a/iOS/MMKV/MMKV/aes/openssl/modes.h +++ b/iOS/MMKV/MMKV/aes/openssl/modes.h @@ -9,9 +9,8 @@ #include -#ifdef __cplusplus -extern "C" { -#endif +namespace openssl { + typedef void (*block128_f) (const unsigned char in[16], unsigned char out[16], const void *key); @@ -19,9 +18,6 @@ void CRYPTO_cfb128_encrypt(const unsigned char *in, unsigned char *out, size_t len, const void *key, unsigned char ivec[16], int *num, int enc, block128_f block); -#ifdef __cplusplus -} +} // namespace openssl #define STRICT_ALIGNMENT 1 - -#endif diff --git a/readme_cn.md b/readme_cn.md index 3b7236e6..e727ef49 100644 --- a/readme_cn.md +++ b/readme_cn.md @@ -59,8 +59,8 @@ NSString *str = [mmkv getStringForKey:@"string"]; ```gradle dependencies { - implementation 'com.tencent:mmkv:1.0.17' - // replace "1.0.17" with any available version + implementation 'com.tencent:mmkv:1.0.18' + // replace "1.0.18" with any available version } ```