From 599c27e833ac7f1c7822c6bea20bff6144a0fffb Mon Sep 17 00:00:00 2001 From: chiteroman <98092901+chiteroman@users.noreply.github.com> Date: Sun, 26 Nov 2023 22:22:56 +0100 Subject: [PATCH] Fix JSON parsing and reformat code --- app/src/main/cpp/main.cpp | 42 ++++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/app/src/main/cpp/main.cpp b/app/src/main/cpp/main.cpp index 63ef3a0..7136169 100644 --- a/app/src/main/cpp/main.cpp +++ b/app/src/main/cpp/main.cpp @@ -1,7 +1,6 @@ #include #include #include -#include #include "zygisk.hpp" #include "shadowhook.h" @@ -168,13 +167,31 @@ private: std::string data(propVector.cbegin(), propVector.cend()); nlohmann::json json = nlohmann::json::parse(data, nullptr, false, true); - auto getStringFromJson = [&json](const std::string &key) { - return json.contains(key) && !json[key].is_null() ? json[key].get() - : "NULL"; - }; + if (json.contains("SECURITY_PATCH")) { + if (json["SECURITY_PATCH"].is_null()) { + SECURITY_PATCH = "NULL"; + } else if (json["SECURITY_PATCH"].is_string()) { + SECURITY_PATCH = json["SECURITY_PATCH"].get(); + } else { + LOGD("Error parsing SECURITY_PATCH!"); + } + } else { + LOGD("Key SECURITY_PATCH doesn't exist in JSON file!"); + } - SECURITY_PATCH = getStringFromJson("SECURITY_PATCH"); - FIRST_API_LEVEL = getStringFromJson("FIRST_API_LEVEL"); + if (json.contains("FIRST_API_LEVEL")) { + if (json["FIRST_API_LEVEL"].is_null()) { + FIRST_API_LEVEL = "NULL"; + } else if (json["FIRST_API_LEVEL"].is_string()) { + FIRST_API_LEVEL = json["FIRST_API_LEVEL"].get(); + } else { + LOGD("Error parsing FIRST_API_LEVEL!"); + } + } else { + LOGD("Key FIRST_API_LEVEL doesn't exist in JSON file!"); + } + + json.clear(); } void inject() { @@ -214,18 +231,20 @@ private: }; static void companion(int fd) { - FILE *dex = fopen(DEX_FILE_PATH, "rb"); long dexSize = 0; char *dexBuffer = nullptr; + long jsonSize = 0; char *jsonBuffer = nullptr; + FILE *dex = fopen(DEX_FILE_PATH, "rb"); + if (dex) { fseek(dex, 0, SEEK_END); dexSize = ftell(dex); fseek(dex, 0, SEEK_SET); - dexBuffer = (char*)calloc(1, dexSize); + dexBuffer = static_cast(calloc(1, dexSize)); fread(dexBuffer, 1, dexSize, dex); fclose(dex); @@ -238,7 +257,7 @@ static void companion(int fd) { jsonSize = ftell(json); fseek(json, 0, SEEK_SET); - jsonBuffer = (char*)calloc(1, jsonSize); + jsonBuffer = static_cast(calloc(1, jsonSize)); fread(jsonBuffer, 1, jsonSize, json); fclose(json); @@ -254,7 +273,6 @@ static void companion(int fd) { free(jsonBuffer); } - REGISTER_ZYGISK_MODULE(PlayIntegrityFix) -REGISTER_ZYGISK_COMPANION(companion) +REGISTER_ZYGISK_COMPANION(companion) \ No newline at end of file