Skip to content

Commit

Permalink
功能[自定义Turnip驱动]: 检测导入的文件是否为可执行文件
Browse files Browse the repository at this point in the history
  • Loading branch information
Vera-Firefly committed Nov 2, 2024
1 parent 8dada86 commit c80bd1c
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 10 deletions.
45 changes: 38 additions & 7 deletions app_pojavlauncher/src/main/java/com/firefly/utils/TurnipUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,32 @@ public String getTurnipDriver(String version) {
return Tools.TURNIP_DIR + "/" + version;
}

/** 首先我们有考虑过在启动器内下载
* 但启动器内下载的不一定满足所有用户
* 经过慎重考虑,还是决定让用户自己选择,并非在启动器内下载
* 也许后面会出一个在启动器内下载的方法
* 但直觉告诉我,这将止步于此,启动器内下载要重新考虑方案
* 而且满足不了所有用户
*/
public boolean saveTurnipDriver(Context context, Uri fileUri, String folderName) {
try {
try (InputStream inputStream = context.getContentResolver().openInputStream(fileUri)) {
if (inputStream == null || !isELFFile(inputStream)) {
return false;
}

inputStream.close(); // Close an open validation file stream
InputStream newInputStream = context.getContentResolver().openInputStream(fileUri);

File targetDir = new File(turnipDir, folderName);
if (!targetDir.exists() && !targetDir.mkdirs()) {
return false;
}

File targetFile = new File(targetDir, "libvulkan_freedreno.so");

try (InputStream inputStream = context.getContentResolver().openInputStream(fileUri);
OutputStream outputStream = new FileOutputStream(targetFile)) {
if (inputStream == null) return false;

try (OutputStream outputStream = new FileOutputStream(targetFile)) {
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
while ((bytesRead = newInputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
}
Expand All @@ -69,6 +79,27 @@ public boolean saveTurnipDriver(Context context, Uri fileUri, String folderName)
}
}

/** Why is there a verification here?
* MovTery: 没有,哪怕是不是被小白用的,你都得检查用户导入了什么东西,检查它能不能用才是关键
* Vera-Firefly: 总不能真有唐完的人不会用硬用它吧?
* 最终我检查了一下代码…所以…这个是遗漏的验证…
*/
private boolean isELFFile(InputStream inputStream) {
try {
byte[] elfMagic = new byte[4];
int bytesRead = inputStream.read(elfMagic);

return bytesRead == 4 &&
elfMagic[0] == 0x7F &&
elfMagic[1] == 'E' &&
elfMagic[2] == 'L' &&
elfMagic[3] == 'F';
} catch (Exception e) {
e.printStackTrace();
return false;
}
}

public boolean deleteTurnipDriver(String version) {
File libDir = new File(turnipDir, version);
if (libDir.exists()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@
<string name="pgw_settings_custom_turnip_creat">添加</string>
<string name="pgw_settings_ctu_version_name">输入版本名称</string>
<string name="pgw_settings_ctu_saved">驱动已成功保存</string>
<string name="pgw_settings_ctu_save_fail">驱动保存失败</string>
<string name="pgw_settings_ctu_save_fail">驱动保存失败, 选择的文件损坏或不是可执行文件</string>
<string name="pgw_settings_ctu_delete_title">删除 Turnip 驱动</string>
<string name="pgw_settings_ctu_delete_message">确定要删除 %s 吗?</string>
<string name="pgw_settings_ctu">自定义 Turnip 驱动</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@
<string name="pgw_settings_custom_turnip_creat">新增</string>
<string name="pgw_settings_ctu_version_name">輸入版本名稱</string>
<string name="pgw_settings_ctu_saved">驅動已成功儲存</string>
<string name="pgw_settings_ctu_save_fail">驅動儲存失敗</string>
<string name="pgw_settings_ctu_save_fail">驅動儲存失敗, 選擇的文檔破損或不是可執行的文檔</string>
<string name="pgw_settings_ctu_delete_title">刪除 Turnip 驅動</string>
<string name="pgw_settings_ctu_delete_message">確定要刪除 %s 嗎?</string>
<string name="pgw_settings_ctu">自訂 Turnip 驅動</string>
Expand Down
2 changes: 1 addition & 1 deletion app_pojavlauncher/src/main/res/values/pgw_strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@
<string name="pgw_settings_custom_turnip_creat">Add</string>
<string name="pgw_settings_ctu_version_name">Enter Version Name</string>
<string name="pgw_settings_ctu_saved">Driver saved successfully</string>
<string name="pgw_settings_ctu_save_fail">Failed to save driver</string>
<string name="pgw_settings_ctu_save_fail">Failed to save driver, file is corrupt or not executable</string>
<string name="pgw_settings_ctu_delete_title">Delete Turnip Driver</string>
<string name="pgw_settings_ctu_delete_message">Are you sure you want to delete %s ?</string>
<string name="pgw_settings_ctu">Custom Turnip Driver</string>
Expand Down

0 comments on commit c80bd1c

Please sign in to comment.