From 8faba8426a21dde999828efc4ebce95efc9bb2ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Mon, 15 Jan 2024 11:46:41 +0100 Subject: [PATCH] AdrenoTools: Fix picking zip files from the Downloads folder. Add more validation. --- Common/Data/Format/JSONReader.h | 6 +- Common/System/Request.h | 1 + Qt/QtMain.cpp | 3 + UI/GameSettingsScreen.cpp | 94 +++++++++++-------- UWP/PPSSPP_UWPMain.cpp | 3 + Windows/main.cpp | 3 + android/jni/app-android.cpp | 8 +- .../src/org/ppsspp/ppsspp/NativeActivity.java | 4 +- 8 files changed, 77 insertions(+), 45 deletions(-) diff --git a/Common/Data/Format/JSONReader.h b/Common/Data/Format/JSONReader.h index 0f5804ecaa43..44b33d25eed2 100644 --- a/Common/Data/Format/JSONReader.h +++ b/Common/Data/Format/JSONReader.h @@ -10,8 +10,7 @@ namespace json { struct JsonGet { - JsonGet(const JsonValue &value) : value_(value) { - } + JsonGet(const JsonValue &value) : value_(value) {} int numChildren() const; const JsonNode *get(const char *child_name) const; @@ -47,7 +46,8 @@ struct JsonGet { class JsonReader { public: JsonReader(const std::string &filename); - JsonReader(const void *data, size_t size) { + // Makes a copy, after this returns you can free the input buffer. + JsonReader(const char *data, size_t size) { buffer_ = (char *)malloc(size + 1); if (buffer_) { memcpy(buffer_, data, size); diff --git a/Common/System/Request.h b/Common/System/Request.h index a53e90463731..314dd7f88a03 100644 --- a/Common/System/Request.h +++ b/Common/System/Request.h @@ -91,6 +91,7 @@ enum class BrowseFileType { INI, DB, SOUND_EFFECT, + ZIP, ANY, }; diff --git a/Qt/QtMain.cpp b/Qt/QtMain.cpp index b406305a6b54..b66fd389d487 100644 --- a/Qt/QtMain.cpp +++ b/Qt/QtMain.cpp @@ -318,6 +318,9 @@ bool MainUI::HandleCustomEvent(QEvent *e) { case BrowseFileType::SOUND_EFFECT: filter = "WAVE files (*.wav)"; break; + case BrowseFileType::ZIP: + filter = "ZIP files (*.zip)"; + break; case BrowseFileType::ANY: break; } diff --git a/UI/GameSettingsScreen.cpp b/UI/GameSettingsScreen.cpp index daefeb9c195e..d6e2a27bf0bc 100644 --- a/UI/GameSettingsScreen.cpp +++ b/UI/GameSettingsScreen.cpp @@ -1268,59 +1268,75 @@ UI::EventReturn DeveloperToolsScreen::OnCustomDriverChange(UI::EventParams &e) { UI::EventReturn DeveloperToolsScreen::OnCustomDriverInstall(UI::EventParams &e) { auto gr = GetI18NCategory(I18NCat::GRAPHICS); - System_BrowseForFile(gr->T("Install Custom Driver..."), BrowseFileType::ANY, [this](const std::string &value, int) { - const Path driverPath = g_Config.internalDataDirectory / "drivers"; - - if (!value.empty()) { - Path zipPath = Path(value); - - bool success = false; - - if (zipPath.GetFileExtension() == ".zip") { - ZipFileReader *zipFileReader = ZipFileReader::Create(zipPath, ""); - - size_t metaDataSize; - uint8_t *metaData = zipFileReader->ReadFile("meta.json", &metaDataSize); + System_BrowseForFile(gr->T("Install Custom Driver..."), BrowseFileType::ZIP, [this](const std::string &value, int) { + if (value.empty()) { + return; + } - Path tempMeta = Path(g_Config.internalDataDirectory / "meta.json"); + auto gr = GetI18NCategory(I18NCat::GRAPHICS); - File::CreateEmptyFile(tempMeta); - File::WriteDataToFile(false, metaData, metaDataSize, tempMeta); + Path zipPath = Path(value); - delete[] metaData; + // Don't bother checking the file extension. Can't always do that with files from Download (they have paths like content://com.android.providers.downloads.documents/document/msf%3A1000001095). + // Though, it may be possible to get it in other ways. - json::JsonReader meta = json::JsonReader((g_Config.internalDataDirectory / "meta.json").c_str()); - if (meta.ok()) { - std::string driverName = meta.root().get("name")->value.toString(); + std::unique_ptr zipFileReader = std::unique_ptr(ZipFileReader::Create(zipPath, "", true)); + if (!zipFileReader) { + g_OSD.Show(OSDType::MESSAGE_ERROR, gr->T("The chosen file is not a valid ZIP file.")); + ERROR_LOG(SYSTEM, "Failed to open file '%s' as zip", zipPath.c_str()); + return; + } - Path newCustomDriver = driverPath / driverName; - File::CreateFullPath(newCustomDriver); + size_t metaDataSize; + uint8_t *metaData = zipFileReader->ReadFile("meta.json", &metaDataSize); + if (!metaData) { + g_OSD.Show(OSDType::MESSAGE_ERROR, gr->T("The chosen ZIP file doesn't contain a valid driver"), "meta.json missing"); + return; + } - std::vector zipListing; - zipFileReader->GetFileListing("", &zipListing, nullptr); + // Validate the json file. TODO: Be a bit more detailed. + json::JsonReader meta = json::JsonReader((const char *)metaData, metaDataSize); + delete[] metaData; + if (!meta.ok()) { + g_OSD.Show(OSDType::MESSAGE_ERROR, gr->T("The chosen ZIP file doesn't contain a valid driver"), "meta.json not valid json"); + return; + } - for (auto file : zipListing) { - File::CreateEmptyFile(newCustomDriver / file.name); + const JsonNode *nameNode = meta.root().get("name"); + if (!nameNode) { + g_OSD.Show(OSDType::MESSAGE_ERROR, gr->T("The chosen ZIP file doesn't contain a valid driver"), "missing driver name in json"); + return; + } - size_t size; - uint8_t *data = zipFileReader->ReadFile(file.name.c_str(), &size); - File::WriteDataToFile(false, data, size, newCustomDriver / file.name); + std::string driverName = nameNode->value.toString(); + if (driverName.empty()) { + g_OSD.Show(OSDType::MESSAGE_ERROR, gr->T("The chosen ZIP file doesn't contain a valid driver"), "driver name empty"); + return; + } - delete[] data; - } + const Path newCustomDriver = g_Config.internalDataDirectory / "drivers" / driverName; + NOTICE_LOG(G3D, "Installing driver into '%s'", newCustomDriver.c_str()); + File::CreateFullPath(newCustomDriver); - File::Delete(tempMeta); + std::vector zipListing; + zipFileReader->GetFileListing("", &zipListing, nullptr); - success = true; + for (auto file : zipListing) { + File::CreateEmptyFile(newCustomDriver / file.name); - RecreateViews(); - } - } - if (!success) { - auto gr = GetI18NCategory(I18NCat::GRAPHICS); - g_OSD.Show(OSDType::MESSAGE_ERROR, gr->T("The file is not a ZIP file containing a compatible driver.")); + size_t size; + uint8_t *data = zipFileReader->ReadFile(file.name.c_str(), &size); + if (!data) { + g_OSD.Show(OSDType::MESSAGE_ERROR, gr->T("The chosen ZIP file doesn't contain a valid driver"), file.name.c_str()); + return; } + File::WriteDataToFile(false, data, size, newCustomDriver / file.name); + delete[] data; } + + auto iz = GetI18NCategory(I18NCat::INSTALLZIP); + g_OSD.Show(OSDType::MESSAGE_SUCCESS, iz->T("Installed!")); + RecreateViews(); }); return UI::EVENT_DONE; } diff --git a/UWP/PPSSPP_UWPMain.cpp b/UWP/PPSSPP_UWPMain.cpp index 00321de052cb..5f7f9d6e9f81 100644 --- a/UWP/PPSSPP_UWPMain.cpp +++ b/UWP/PPSSPP_UWPMain.cpp @@ -507,6 +507,9 @@ bool System_MakeRequest(SystemRequestType type, int requestId, const std::string case BrowseFileType::INI: supportedExtensions = { ".ini" }; break; + case BrowseFileType::ZIP: + supportedExtensions = { ".zip" }; + break; case BrowseFileType::DB: supportedExtensions = { ".db" }; break; diff --git a/Windows/main.cpp b/Windows/main.cpp index 05dd0b6018cc..22cc681f9014 100644 --- a/Windows/main.cpp +++ b/Windows/main.cpp @@ -569,6 +569,9 @@ bool System_MakeRequest(SystemRequestType type, int requestId, const std::string case BrowseFileType::INI: filter = MakeFilter(L"Ini files (*.ini)|*.ini|All files (*.*)|*.*||"); break; + case BrowseFileType::ZIP: + filter = MakeFilter(L"ZIP files (*.zip)|*.zip|All files (*.*)|*.*||"); + break; case BrowseFileType::DB: filter = MakeFilter(L"Cheat db files (*.db)|*.db|All files (*.*)|*.*||"); break; diff --git a/android/jni/app-android.cpp b/android/jni/app-android.cpp index 0cd5ec1b4802..08858ca83d39 100644 --- a/android/jni/app-android.cpp +++ b/android/jni/app-android.cpp @@ -1108,12 +1108,16 @@ bool System_MakeRequest(SystemRequestType type, int requestId, const std::string case SystemRequestType::BROWSE_FOR_FILE: { BrowseFileType fileType = (BrowseFileType)param3; + std::string params = StringFromFormat("%d", requestId); switch (fileType) { case BrowseFileType::SOUND_EFFECT: - PushCommand("browse_file_audio", StringFromFormat("%d", requestId)); + PushCommand("browse_file_audio", params); + break; + case BrowseFileType::ZIP: + PushCommand("browse_file_zip", params); break; default: - PushCommand("browse_file", StringFromFormat("%d", requestId)); + PushCommand("browse_file", params); break; } return true; diff --git a/android/src/org/ppsspp/ppsspp/NativeActivity.java b/android/src/org/ppsspp/ppsspp/NativeActivity.java index 17e7cac416a3..2d50642cc3f6 100644 --- a/android/src/org/ppsspp/ppsspp/NativeActivity.java +++ b/android/src/org/ppsspp/ppsspp/NativeActivity.java @@ -1409,7 +1409,7 @@ public boolean processCommand(String command, String params) { Log.e(TAG, e.toString()); return false; } - } else if (command.equals("browse_file") || command.equals("browse_file_audio")) { + } else if (command.equals("browse_file") || command.equals("browse_file_audio") || command.equals("browse_file_zip")) { try { int requestId = Integer.parseInt(params); int packedResultCode = packResultCode(RESULT_OPEN_DOCUMENT, requestId); @@ -1418,6 +1418,8 @@ public boolean processCommand(String command, String params) { intent.addCategory(Intent.CATEGORY_OPENABLE); if (command.equals("browse_file_audio")) { intent.setType("audio/x-wav"); + } else if (command.equals("browse_file_zip")) { + intent.setType("application/zip"); } else { intent.setType("*/*"); }