Use vectors instead arrays

This commit is contained in:
chiteroman 2023-12-29 00:33:37 +01:00
parent d1a74c846e
commit 0b21f031de

View File

@ -100,14 +100,15 @@ public:
if (strcmp(name, "com.google.android.gms.unstable") == 0) { if (strcmp(name, "com.google.android.gms.unstable") == 0) {
long dexSize = 0, jsonSize = 0;
int fd = api->connectCompanion(); int fd = api->connectCompanion();
read(fd, &dexSize, sizeof(long)); read(fd, &dexSize, sizeof(long));
if (dexSize > 0) { if (dexSize > 0) {
dexBuffer = new unsigned char[dexSize]; dexVector.resize(dexSize);
read(fd, dexBuffer, dexSize); read(fd, dexVector.data(), dexSize);
} else { } else {
@ -120,13 +121,13 @@ public:
if (jsonSize > 0) { if (jsonSize > 0) {
jsonBuffer = new char[jsonSize]; jsonVector.resize(jsonSize);
read(fd, jsonBuffer, jsonSize); read(fd, jsonVector.data(), jsonSize);
} else { } else {
LOGD("Couldn't load pif.json file in memory!"); LOGD("Couldn't load pif.json file in memory!");
delete[] dexBuffer; dexVector.clear();
api->setOption(zygisk::DLCLOSE_MODULE_LIBRARY); api->setOption(zygisk::DLCLOSE_MODULE_LIBRARY);
goto end; goto end;
} }
@ -144,9 +145,9 @@ public:
} }
void postAppSpecialize(const zygisk::AppSpecializeArgs *args) override { void postAppSpecialize(const zygisk::AppSpecializeArgs *args) override {
if (dexSize < 1 || jsonSize < 1 || dexBuffer == nullptr || jsonBuffer == nullptr) return; if (dexVector.empty() || jsonVector.empty()) return;
std::string_view jsonStr(jsonBuffer, jsonSize); std::string jsonStr(jsonVector.cbegin(), jsonVector.cend());
nlohmann::json json = nlohmann::json::parse(jsonStr, nullptr, false, true); nlohmann::json json = nlohmann::json::parse(jsonStr, nullptr, false, true);
if (json.contains("FIRST_API_LEVEL")) { if (json.contains("FIRST_API_LEVEL")) {
@ -191,7 +192,7 @@ public:
auto dexClClass = env->FindClass("dalvik/system/InMemoryDexClassLoader"); auto dexClClass = env->FindClass("dalvik/system/InMemoryDexClassLoader");
auto dexClInit = env->GetMethodID(dexClClass, "<init>", auto dexClInit = env->GetMethodID(dexClClass, "<init>",
"(Ljava/nio/ByteBuffer;Ljava/lang/ClassLoader;)V"); "(Ljava/nio/ByteBuffer;Ljava/lang/ClassLoader;)V");
auto buffer = env->NewDirectByteBuffer(dexBuffer, dexSize); auto buffer = env->NewDirectByteBuffer(dexVector.data(), dexVector.size());
auto dexCl = env->NewObject(dexClClass, dexClInit, buffer, systemClassLoader); auto dexCl = env->NewObject(dexClClass, dexClInit, buffer, systemClassLoader);
LOGD("load class"); LOGD("load class");
@ -207,16 +208,8 @@ public:
auto str = env->NewStringUTF(json.dump().c_str()); auto str = env->NewStringUTF(json.dump().c_str());
env->CallStaticVoidMethod(entryClass, entryInit, str); env->CallStaticVoidMethod(entryClass, entryInit, str);
env->DeleteLocalRef(clClass); dexVector.clear();
env->DeleteLocalRef(dexClClass); jsonVector.clear();
env->DeleteLocalRef(buffer);
env->DeleteLocalRef(dexCl);
env->DeleteLocalRef(entryClassName);
env->DeleteLocalRef(entryClassObj);
env->DeleteLocalRef(str);
delete[] dexBuffer;
delete[] jsonBuffer;
} }
void preServerSpecialize(zygisk::ServerSpecializeArgs *args) override { void preServerSpecialize(zygisk::ServerSpecializeArgs *args) override {
@ -226,15 +219,12 @@ public:
private: private:
zygisk::Api *api = nullptr; zygisk::Api *api = nullptr;
JNIEnv *env = nullptr; JNIEnv *env = nullptr;
long dexSize = 0, jsonSize = 0; std::vector<char> dexVector, jsonVector;
unsigned char *dexBuffer = nullptr;
char *jsonBuffer = nullptr;
}; };
static void companion(int fd) { static void companion(int fd) {
long dexSize = 0, jsonSize = 0; long dexSize = 0, jsonSize = 0;
unsigned char *dexBuffer = nullptr; std::vector<char> dexVector, jsonVector;
char *jsonBuffer = nullptr;
FILE *dexFile = fopen(CLASSES_DEX, "rb"); FILE *dexFile = fopen(CLASSES_DEX, "rb");
@ -244,16 +234,14 @@ static void companion(int fd) {
dexSize = ftell(dexFile); dexSize = ftell(dexFile);
fseek(dexFile, 0, SEEK_SET); fseek(dexFile, 0, SEEK_SET);
dexBuffer = new unsigned char[dexSize]; dexVector.resize(dexSize);
fread(dexBuffer, 1, dexSize, dexFile); fread(dexVector.data(), 1, dexSize, dexFile);
fclose(dexFile); fclose(dexFile);
} }
write(fd, &dexSize, sizeof(long)); write(fd, &dexSize, sizeof(long));
write(fd, dexBuffer, dexSize); write(fd, dexVector.data(), dexSize);
delete[] dexBuffer;
FILE *jsonFile = fopen(PIF_JSON, "r"); FILE *jsonFile = fopen(PIF_JSON, "r");
@ -268,16 +256,14 @@ static void companion(int fd) {
jsonSize = ftell(jsonFile); jsonSize = ftell(jsonFile);
fseek(jsonFile, 0, SEEK_SET); fseek(jsonFile, 0, SEEK_SET);
jsonBuffer = new char[jsonSize]; jsonVector.resize(jsonSize);
fread(jsonBuffer, 1, jsonSize, jsonFile); fread(jsonVector.data(), 1, jsonSize, jsonFile);
fclose(jsonFile); fclose(jsonFile);
} }
write(fd, &jsonSize, sizeof(long)); write(fd, &jsonSize, sizeof(long));
write(fd, jsonBuffer, jsonSize); write(fd, jsonVector.data(), jsonSize);
delete[] jsonBuffer;
} }
REGISTER_ZYGISK_MODULE(PlayIntegrityFix) REGISTER_ZYGISK_MODULE(PlayIntegrityFix)