diff --git a/Android/dexkit/src/main/cpp/native-bridge.cpp b/Android/dexkit/src/main/cpp/native-bridge.cpp index a1e02b9f..b9bf8aee 100644 --- a/Android/dexkit/src/main/cpp/native-bridge.cpp +++ b/Android/dexkit/src/main/cpp/native-bridge.cpp @@ -1,6 +1,6 @@ #include #include -#include +#include "dex_kit.h" #include "dex_kit_jni_helper.h" #define TAG "DexKit" @@ -30,27 +30,18 @@ extern "C" JNIEXPORT void JNICALL Java_io_luckypray_dexkit_DexKitBridge_nativeSetThreadNum(JNIEnv *env, jclass clazz, jlong native_ptr, jint thread_num) { - if (!native_ptr) { - return; - } SetThreadNum(env, native_ptr, thread_num); } DEXKIT_JNI jint Java_io_luckypray_dexkit_DexKitBridge_nativeGetDexNum(JNIEnv *env, jclass clazz, jlong native_ptr) { - if (!native_ptr) { - return 0; - } return GetDexNum(env, native_ptr); } DEXKIT_JNI void Java_io_luckypray_dexkit_DexKitBridge_nativeRelease(JNIEnv *env, jclass clazz, jlong native_ptr) { - if (!native_ptr) { - return; - } ReleaseDexKitInstance(env, native_ptr); } @@ -61,9 +52,6 @@ Java_io_luckypray_dexkit_DexKitBridge_nativeBatchFindClassesUsingStrings(JNIEnv jobject map, jboolean advanced_match, jintArray dex_priority) { - if (!native_ptr) { - return CMap2JMap(env, std::map>()); - } return BatchFindClassesUsingStrings(env, native_ptr, map, advanced_match, dex_priority); } @@ -74,9 +62,6 @@ Java_io_luckypray_dexkit_DexKitBridge_nativeBatchFindMethodsUsingStrings(JNIEnv jobject map, jboolean advanced_match, jintArray dex_priority) { - if (!native_ptr) { - return CMap2JMap(env, std::map>()); - } return BatchFindMethodsUsingStrings(env, native_ptr, map, advanced_match, dex_priority); } @@ -93,9 +78,6 @@ Java_io_luckypray_dexkit_DexKitBridge_nativeFindMethodCaller(JNIEnv *env, jclass jstring caller_method_return_type, jobjectArray caller_method_param_types, jintArray dex_priority) { - if (!native_ptr) { - return StrVec2JStrArr(env, std::vector()); - } return FindMethodCaller(env, native_ptr, method_descriptor, method_declare_class, method_declare_name, method_return_type, method_param_types, caller_method_declare_class, caller_method_declare_name, @@ -115,9 +97,6 @@ Java_io_luckypray_dexkit_DexKitBridge_nativeFindMethodInvoking(JNIEnv *env, jcla jstring be_called_method_return_type, jobjectArray be_called_method_param_types, jintArray dex_priority) { - if (!native_ptr) { - return CMap2JMap(env, std::map>()); - } return FindMethodInvoking(env, native_ptr, method_descriptor, method_declare_class, method_declare_name, method_return_type, method_param_types, be_called_method_declare_class, be_called_method_declare_name, @@ -138,9 +117,6 @@ Java_io_luckypray_dexkit_DexKitBridge_nativeFindMethodUsingField(JNIEnv *env, jc jstring caller_method_return_type, jobjectArray caller_method_param_types, jintArray dex_priority) { - if (!native_ptr) { - return StrVec2JStrArr(env, std::vector()); - } return FindMethodUsingField(env, native_ptr, field_descriptor, field_declare_class, field_name, field_type, used_flags, caller_method_declare_class, caller_method_name, caller_method_return_type, @@ -157,9 +133,6 @@ Java_io_luckypray_dexkit_DexKitBridge_nativeFindMethodUsingString(JNIEnv *env, j jstring method_return_type, jobjectArray method_param_types, jintArray dex_priority) { - if (!native_ptr) { - return StrVec2JStrArr(env, std::vector()); - } return FindMethodUsingString(env, native_ptr, used_string, advanced_match, method_declare_class, method_name, method_return_type, method_param_types, dex_priority); } @@ -172,9 +145,6 @@ Java_io_luckypray_dexkit_DexKitBridge_nativeFindMethod(JNIEnv *env, jclass clazz jstring method_return_type, jobjectArray method_param_types, jintArray dex_priority) { - if (!native_ptr) { - return StrVec2JStrArr(env, std::vector()); - } return FindMethod(env, native_ptr, method_declare_class, method_name, method_return_type, method_param_types, dex_priority); } @@ -184,9 +154,6 @@ Java_io_luckypray_dexkit_DexKitBridge_nativeFindSubClasses(JNIEnv *env, jclass c jlong native_ptr, jstring parent_class, jintArray dex_priority) { - if (!native_ptr) { - return StrVec2JStrArr(env, std::vector()); - } return FindSubClasses(env, native_ptr, parent_class, dex_priority); } @@ -199,9 +166,6 @@ Java_io_luckypray_dexkit_DexKitBridge_nativeFindMethodOpPrefixSeq(JNIEnv *env, j jstring method_return_type, jobjectArray method_param_types, jintArray dex_priority) { - if (!native_ptr) { - return StrVec2JStrArr(env, std::vector()); - } return FindMethodOpPrefixSeq(env, native_ptr, op_prefix_seq, method_declare_class, method_name, method_return_type, method_param_types, dex_priority); } @@ -216,9 +180,6 @@ Java_io_luckypray_dexkit_DexKitBridge_nativeFindMethodUsingOpCodeSeq(JNIEnv *env jstring method_return_type, jobjectArray method_param_types, jintArray dex_priority) { - if (!native_ptr) { - return StrVec2JStrArr(env, std::vector()); - } return FindMethodUsingOpCodeSeq(env, native_ptr, op_seq, method_declare_class, method_name, method_return_type, method_param_types, dex_priority); } @@ -233,9 +194,6 @@ Java_io_luckypray_dexkit_DexKitBridge_nativeGetMethodOpCodeSeq(JNIEnv *env, jcla jstring method_return_type, jobjectArray method_param_types, jintArray dex_priority) { - if (!native_ptr) { - return EmptyJMap(env); - } return GetMethodOpCodeSeq(env, native_ptr, method_descriptor, method_declare_class, method_name, method_return_type, method_param_types, dex_priority); } \ No newline at end of file diff --git a/Core/include/dex_kit_jni_helper.h b/Core/include/dex_kit_jni_helper.h index cca6bc5d..38bad732 100644 --- a/Core/include/dex_kit_jni_helper.h +++ b/Core/include/dex_kit_jni_helper.h @@ -15,7 +15,7 @@ std::vector JIntArr2u8Vec(JNIEnv *env, const jintArray jIntArr); jintArray U8Vec2JIntArr(JNIEnv *env, const std::vector &vector); -jobject EmptyJmap(JNIEnv *env); +jobject EmptyJMap(JNIEnv *env); jlong CreateDexKitInstance(JNIEnv *env, jstring apkPath) { auto path = env->GetStringUTFChars(apkPath, nullptr); @@ -24,51 +24,66 @@ jlong CreateDexKitInstance(JNIEnv *env, jstring apkPath) { } void SetThreadNum(JNIEnv *env, jlong dexKitPtr, jint threadNum) { + if (!dexKitPtr) { + return; + } auto dexKit = reinterpret_cast(dexKitPtr); dexKit->SetThreadNum(threadNum); } jint GetDexNum(JNIEnv *env, jlong dexKitPtr) { + if (!dexKitPtr) { + return 0; + } auto dexKit = reinterpret_cast(dexKitPtr); return dexKit->GetDexNum(); } void ReleaseDexKitInstance(JNIEnv *env, jlong dexKit) { + if (!dexKit) { + return; + } delete reinterpret_cast(dexKit); } jobject BatchFindClassesUsingStrings(JNIEnv *env, - jlong dexKit, + jlong dexKitPtr, jobject jMap, jboolean advanced_match, jintArray dex_priority) { - auto dexKitPtr = reinterpret_cast(dexKit); + if (!dexKitPtr) { + return CMap2JMap(env, std::map>()); + } + auto dexKit = reinterpret_cast(dexKitPtr); auto map = JMap2CMap(env, jMap); std::vector dexPriority; if (dex_priority != NULL) { dexPriority = JIntArr2IntVec(env, dex_priority); } - auto res = dexKitPtr->BatchFindClassesUsingStrings(map, advanced_match, dexPriority); + auto res = dexKit->BatchFindClassesUsingStrings(map, advanced_match, dexPriority); return CMap2JMap(env, res); } jobject BatchFindMethodsUsingStrings(JNIEnv *env, - jlong dexKit, + jlong dexKitPtr, jobject jMap, jboolean advanced_match, jintArray dex_priority) { - auto dexKitPtr = reinterpret_cast(dexKit); + if (!dexKitPtr) { + return CMap2JMap(env, std::map>()); + } + auto dexKit = reinterpret_cast(dexKitPtr); auto map = JMap2CMap(env, jMap); std::vector dexPriority; if (dex_priority != NULL) { dexPriority = JIntArr2IntVec(env, dex_priority); } - auto res = dexKitPtr->BatchFindMethodsUsingStrings(map, advanced_match, dexPriority); + auto res = dexKit->BatchFindMethodsUsingStrings(map, advanced_match, dexPriority); return CMap2JMap(env, res); } jobjectArray FindMethodCaller(JNIEnv *env, - jlong dexKit, + jlong dexKitPtr, jstring method_descriptor, jstring method_declare_class, jstring method_name, @@ -79,7 +94,10 @@ jobjectArray FindMethodCaller(JNIEnv *env, jstring caller_method_return_type, jobjectArray caller_method_param_types, jintArray dex_priority) { - auto dexKitPtr = reinterpret_cast(dexKit); + if (!dexKitPtr) { + return StrVec2JStrArr(env, std::vector()); + } + auto dexKit = reinterpret_cast(dexKitPtr); auto methodDescriptor = env->GetStringUTFChars(method_descriptor, nullptr); auto methodDeclareClass = env->GetStringUTFChars(method_declare_class, nullptr); auto methodName = env->GetStringUTFChars(method_name, nullptr); @@ -99,16 +117,16 @@ jobjectArray FindMethodCaller(JNIEnv *env, if (dex_priority != NULL) { dexPriority = JIntArr2IntVec(env, dex_priority); } - auto res = dexKitPtr->FindMethodCaller(methodDescriptor, - methodDeclareClass, - methodName, - methodReturnType, - ParamTypes, - callerMethodClass, - callerMethodName, - callerMethodReturnType, - callerParamTypes, - dexPriority); + auto res = dexKit->FindMethodCaller(methodDescriptor, + methodDeclareClass, + methodName, + methodReturnType, + ParamTypes, + callerMethodClass, + callerMethodName, + callerMethodReturnType, + callerParamTypes, + dexPriority); env->ReleaseStringUTFChars(method_descriptor, methodDescriptor); env->ReleaseStringUTFChars(method_declare_class, methodDeclareClass); env->ReleaseStringUTFChars(method_name, methodName); @@ -120,7 +138,7 @@ jobjectArray FindMethodCaller(JNIEnv *env, } jobject FindMethodInvoking(JNIEnv *env, - jlong dexKit, + jlong dexKitPtr, jstring method_descriptor, jstring method_declare_class, jstring method_name, @@ -131,7 +149,10 @@ jobject FindMethodInvoking(JNIEnv *env, jstring be_called_method_return_type, jobjectArray be_called_method_param_types, jintArray dex_priority) { - auto dexKitPtr = reinterpret_cast(dexKit); + if (!dexKitPtr) { + return CMap2JMap(env, std::map>()); + } + auto dexKit = reinterpret_cast(dexKitPtr); auto methodDescriptor = env->GetStringUTFChars(method_descriptor, nullptr); auto methodDeclareClass = env->GetStringUTFChars(method_declare_class, nullptr); auto methodName = env->GetStringUTFChars(method_name, nullptr); @@ -152,16 +173,16 @@ jobject FindMethodInvoking(JNIEnv *env, if (dex_priority != NULL) { dexPriority = JIntArr2IntVec(env, dex_priority); } - auto res = dexKitPtr->FindMethodInvoking(methodDescriptor, - methodDeclareClass, - methodName, - methodReturnType, - ParamTypes, - beCalledMethodDeclareClass, - beCalledMethodDeclareName, - beCalledMethodReturnType, - beCalledMethodParamTypes, - dexPriority); + auto res = dexKit->FindMethodInvoking(methodDescriptor, + methodDeclareClass, + methodName, + methodReturnType, + ParamTypes, + beCalledMethodDeclareClass, + beCalledMethodDeclareName, + beCalledMethodReturnType, + beCalledMethodParamTypes, + dexPriority); env->ReleaseStringUTFChars(method_descriptor, methodDescriptor); env->ReleaseStringUTFChars(method_declare_class, methodDeclareClass); env->ReleaseStringUTFChars(method_name, methodName); @@ -173,7 +194,7 @@ jobject FindMethodInvoking(JNIEnv *env, } jobject FindMethodUsingField(JNIEnv *env, - jlong dexKit, + jlong dexKitPtr, jstring field_descriptor, jstring field_declare_class, jstring field_name, @@ -184,7 +205,10 @@ jobject FindMethodUsingField(JNIEnv *env, jstring caller_method_return_type, jobjectArray caller_method_param_types, jintArray dex_priority) { - auto dexKitPtr = reinterpret_cast(dexKit); + if (!dexKitPtr) { + return StrVec2JStrArr(env, std::vector()); + } + auto dexKit = reinterpret_cast(dexKitPtr); auto fieldDescriptor = env->GetStringUTFChars(field_descriptor, nullptr); auto fieldDeclareClass = env->GetStringUTFChars(field_declare_class, nullptr); auto fieldName = env->GetStringUTFChars(field_name, nullptr); @@ -203,16 +227,16 @@ jobject FindMethodUsingField(JNIEnv *env, if (dex_priority != NULL) { dexPriority = JIntArr2IntVec(env, dex_priority); } - auto res = dexKitPtr->FindMethodUsingField(fieldDescriptor, - fieldDeclareClass, - fieldName, - fieldType, - used_flags, - callerMethodClass, - callerMethodName, - callerMethodReturnType, - callerParamTypes, - dexPriority); + auto res = dexKit->FindMethodUsingField(fieldDescriptor, + fieldDeclareClass, + fieldName, + fieldType, + used_flags, + callerMethodClass, + callerMethodName, + callerMethodReturnType, + callerParamTypes, + dexPriority); env->ReleaseStringUTFChars(field_descriptor, fieldDescriptor); env->ReleaseStringUTFChars(field_declare_class, fieldDeclareClass); env->ReleaseStringUTFChars(field_name, fieldName); @@ -224,7 +248,7 @@ jobject FindMethodUsingField(JNIEnv *env, } jobjectArray FindMethodUsingString(JNIEnv *env, - jlong dexKit, + jlong dexKitPtr, jstring used_utf8_string, jboolean advanced_match, jstring method_declare_class, @@ -232,7 +256,10 @@ jobjectArray FindMethodUsingString(JNIEnv *env, jstring method_return_type, jobjectArray method_param_types, jintArray dex_priority) { - auto dexKitPtr = reinterpret_cast(dexKit); + if (!dexKitPtr) { + return StrVec2JStrArr(env, std::vector()); + } + auto dexKit = reinterpret_cast(dexKitPtr); auto usedUtf8String = env->GetStringUTFChars(used_utf8_string, nullptr); auto methodDeclareClass = env->GetStringUTFChars(method_declare_class, nullptr); auto methodName = env->GetStringUTFChars(method_name, nullptr); @@ -245,13 +272,13 @@ jobjectArray FindMethodUsingString(JNIEnv *env, if (dex_priority != NULL) { dexPriority = JIntArr2IntVec(env, dex_priority); } - auto res = dexKitPtr->FindMethodUsingString(usedUtf8String, - advanced_match, - methodDeclareClass, - methodName, - methodReturnType, - ParamTypes, - dexPriority); + auto res = dexKit->FindMethodUsingString(usedUtf8String, + advanced_match, + methodDeclareClass, + methodName, + methodReturnType, + ParamTypes, + dexPriority); env->ReleaseStringUTFChars(used_utf8_string, usedUtf8String); env->ReleaseStringUTFChars(method_declare_class, methodDeclareClass); env->ReleaseStringUTFChars(method_name, methodName); @@ -260,13 +287,16 @@ jobjectArray FindMethodUsingString(JNIEnv *env, } jobjectArray FindMethod(JNIEnv *env, - jlong dexKit, + jlong dexKitPtr, jstring method_declare_class, jstring method_name, jstring method_return_type, jobjectArray method_param_types, jintArray dex_priority) { - auto dexKitPtr = reinterpret_cast(dexKit); + if (!dexKitPtr) { + return StrVec2JStrArr(env, std::vector()); + } + auto dexKit = reinterpret_cast(dexKitPtr); auto methodDeclareClass = env->GetStringUTFChars(method_declare_class, nullptr); auto methodName = env->GetStringUTFChars(method_name, nullptr); auto methodReturnType = env->GetStringUTFChars(method_return_type, nullptr); @@ -278,11 +308,11 @@ jobjectArray FindMethod(JNIEnv *env, if (dex_priority != NULL) { dexPriority = JIntArr2IntVec(env, dex_priority); } - auto res = dexKitPtr->FindMethod(methodDeclareClass, - methodName, - methodReturnType, - ParamTypes, - dexPriority); + auto res = dexKit->FindMethod(methodDeclareClass, + methodName, + methodReturnType, + ParamTypes, + dexPriority); env->ReleaseStringUTFChars(method_declare_class, methodDeclareClass); env->ReleaseStringUTFChars(method_name, methodName); env->ReleaseStringUTFChars(method_return_type, methodReturnType); @@ -290,29 +320,35 @@ jobjectArray FindMethod(JNIEnv *env, } jobjectArray FindSubClasses(JNIEnv *env, - jlong dexKit, + jlong dexKitPtr, jstring parent_class, jintArray dex_priority) { - auto dexKitPtr = reinterpret_cast(dexKit); + if (!dexKitPtr) { + return StrVec2JStrArr(env, std::vector()); + } + auto dexKit = reinterpret_cast(dexKitPtr); auto parentClass = env->GetStringUTFChars(parent_class, nullptr); std::vector dexPriority; if (dex_priority != NULL) { dexPriority = JIntArr2IntVec(env, dex_priority); } - auto res = dexKitPtr->FindSubClasses(parentClass, dexPriority); + auto res = dexKit->FindSubClasses(parentClass, dexPriority); env->ReleaseStringUTFChars(parent_class, parentClass); return StrVec2JStrArr(env, res); } jobjectArray FindMethodOpPrefixSeq(JNIEnv *env, - jlong dexKit, + jlong dexKitPtr, jintArray op_prefix_seq, jstring method_declare_class, jstring method_name, jstring method_return_type, jobjectArray method_param_types, jintArray dex_priority) { - auto dexKitPtr = reinterpret_cast(dexKit); + if (!dexKitPtr) { + return StrVec2JStrArr(env, std::vector()); + } + auto dexKit = reinterpret_cast(dexKitPtr); auto opPrefixSeq = JIntArr2u8Vec(env, op_prefix_seq); auto methodDeclareClass = env->GetStringUTFChars(method_declare_class, nullptr); auto methodName = env->GetStringUTFChars(method_name, nullptr); @@ -325,12 +361,12 @@ jobjectArray FindMethodOpPrefixSeq(JNIEnv *env, if (dex_priority != NULL) { dexPriority = JIntArr2IntVec(env, dex_priority); } - auto res = dexKitPtr->FindMethodOpPrefixSeq(opPrefixSeq, - methodDeclareClass, - methodName, - methodReturnType, - ParamTypes, - dexPriority); + auto res = dexKit->FindMethodOpPrefixSeq(opPrefixSeq, + methodDeclareClass, + methodName, + methodReturnType, + ParamTypes, + dexPriority); env->ReleaseStringUTFChars(method_declare_class, methodDeclareClass); env->ReleaseStringUTFChars(method_name, methodName); env->ReleaseStringUTFChars(method_return_type, methodReturnType); @@ -338,14 +374,17 @@ jobjectArray FindMethodOpPrefixSeq(JNIEnv *env, } jobjectArray FindMethodUsingOpCodeSeq(JNIEnv *env, - jlong dexKit, + jlong dexKitPtr, jintArray op_code_seq, jstring method_declare_class, jstring method_name, jstring method_return_type, jobjectArray method_param_types, jintArray dex_priority) { - auto dexKitPtr = reinterpret_cast(dexKit); + if (!dexKitPtr) { + return StrVec2JStrArr(env, std::vector()); + } + auto dexKit = reinterpret_cast(dexKitPtr); auto opCodeSeq = JIntArr2u8Vec(env, op_code_seq); auto methodDeclareClass = env->GetStringUTFChars(method_declare_class, nullptr); auto methodName = env->GetStringUTFChars(method_name, nullptr); @@ -358,12 +397,12 @@ jobjectArray FindMethodUsingOpCodeSeq(JNIEnv *env, if (dex_priority != NULL) { dexPriority = JIntArr2IntVec(env, dex_priority); } - auto res = dexKitPtr->FindMethodUsingOpCodeSeq(opCodeSeq, - methodDeclareClass, - methodName, - methodReturnType, - paramTypes, - dexPriority); + auto res = dexKit->FindMethodUsingOpCodeSeq(opCodeSeq, + methodDeclareClass, + methodName, + methodReturnType, + paramTypes, + dexPriority); env->ReleaseStringUTFChars(method_declare_class, methodDeclareClass); env->ReleaseStringUTFChars(method_name, methodName); env->ReleaseStringUTFChars(method_return_type, methodReturnType); @@ -371,14 +410,17 @@ jobjectArray FindMethodUsingOpCodeSeq(JNIEnv *env, } jobject GetMethodOpCodeSeq(JNIEnv *env, - jlong dexKit, + jlong dexKitPtr, jstring method_descriptor, jstring method_declare_class, jstring method_name, jstring method_return_type, jobjectArray method_param_types, jintArray dex_priority) { - auto dexKitPtr = reinterpret_cast(dexKit); + if (!dexKitPtr) { + return EmptyJMap(env); + } + auto dexKit = reinterpret_cast(dexKitPtr); auto methodDescriptor = env->GetStringUTFChars(method_descriptor, nullptr); auto methodDeclareClass = env->GetStringUTFChars(method_declare_class, nullptr); auto methodName = env->GetStringUTFChars(method_name, nullptr); @@ -391,12 +433,12 @@ jobject GetMethodOpCodeSeq(JNIEnv *env, if (dex_priority != NULL) { dexPriority = JIntArr2IntVec(env, dex_priority); } - auto res = dexKitPtr->GetMethodOpCodeSeq(methodDescriptor, - methodDeclareClass, - methodName, - methodReturnType, - paramTypes, - dexPriority); + auto res = dexKit->GetMethodOpCodeSeq(methodDescriptor, + methodDeclareClass, + methodName, + methodReturnType, + paramTypes, + dexPriority); env->ReleaseStringUTFChars(method_descriptor, methodDescriptor); env->ReleaseStringUTFChars(method_declare_class, methodDeclareClass); env->ReleaseStringUTFChars(method_name, methodName); diff --git a/Core/main.cpp b/Core/main.cpp index 30370985..299b1eeb 100644 --- a/Core/main.cpp +++ b/Core/main.cpp @@ -1,12 +1,8 @@ #include -#include #include -#include #include #include #include "dex_kit.h" -#include "code_format.h" -#include "kmp.h" int main() { std::map> obfuscate = { diff --git a/Core/qq-example.cpp b/Core/qq-example.cpp index 06c484de..8ce8146d 100644 --- a/Core/qq-example.cpp +++ b/Core/qq-example.cpp @@ -1,7 +1,5 @@ #include -#include #include -#include #include #include #include "dex_kit.h"