Commit fd4aaab1 authored by topjohnwu's avatar topjohnwu

Rewrite zip signing

parent 42d14d5c
...@@ -14,7 +14,6 @@ import com.topjohnwu.magisk.utils.ZipUtils; ...@@ -14,7 +14,6 @@ import com.topjohnwu.magisk.utils.ZipUtils;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream; import java.io.OutputStream;
public class ProcessRepoZip extends ParallelTask<Void, Void, Boolean> { public class ProcessRepoZip extends ParallelTask<Void, Void, Boolean> {
...@@ -38,51 +37,33 @@ public class ProcessRepoZip extends ParallelTask<Void, Void, Boolean> { ...@@ -38,51 +37,33 @@ public class ProcessRepoZip extends ParallelTask<Void, Void, Boolean> {
@Override @Override
protected Boolean doInBackground(Void... params) { protected Boolean doInBackground(Void... params) {
FileInputStream in;
FileOutputStream out;
try { try {
// Create temp file // Create temp file
File temp1 = new File(magiskManager.getCacheDir(), "1.zip"); File temp1 = new File(magiskManager.getCacheDir(), "1.zip");
File temp2 = new File(magiskManager.getCacheDir(), "2.zip"); File temp2 = new File(magiskManager.getCacheDir(), "2.zip");
if (magiskManager.getCacheDir().mkdirs()) { magiskManager.getCacheDir().mkdirs();
temp1.createNewFile(); temp1.createNewFile();
temp2.createNewFile(); temp2.createNewFile();
}
out = new FileOutputStream(temp1);
// First remove top folder in Github source zip, Uri -> temp1 // First remove top folder in Github source zip, Uri -> temp1
ZipUtils.removeTopFolder(activity.getContentResolver().openInputStream(mUri), out); ZipUtils.removeTopFolder(activity.getContentResolver().openInputStream(mUri), temp1);
out.flush();
out.close();
out = new FileOutputStream(temp2);
// Then sign the zip for the first time, temp1 -> temp2 // Then sign the zip for the first time, temp1 -> temp2
ZipUtils.signZip(activity, temp1, out, false); ZipUtils.signZip(activity, temp1, temp2, false);
out.flush();
out.close();
// Adjust the zip to prevent unzip issues, temp2 -> temp2
ZipUtils.adjustZip(temp2);
out = new FileOutputStream(temp1);
// Finally, sign the whole zip file again, temp2 -> temp1 // Adjust the zip to prevent unzip issues, temp2 -> temp1
ZipUtils.signZip(activity, temp2, out, true); ZipUtils.zipAdjust(temp2.getPath(), temp1.getPath());
out.flush();
out.close();
in = new FileInputStream(temp1); // Finally, sign the whole zip file again, temp1 -> temp2
ZipUtils.signZip(activity, temp1, temp2, true);
// Write it back to the downloaded zip, temp1 -> Uri // Write it back to the downloaded zip, temp2 -> Uri
FileInputStream in = new FileInputStream(temp2);
try (OutputStream target = activity.getContentResolver().openOutputStream(mUri)) { try (OutputStream target = activity.getContentResolver().openOutputStream(mUri)) {
byte[] buffer = new byte[4096]; byte[] buffer = new byte[4096];
int length; int length;
if (target == null) throw new FileNotFoundException(); if (target == null) throw new FileNotFoundException();
while ((length = in.read(buffer)) > 0) while ((length = in.read(buffer)) > 0)
target.write(buffer, 0, length); target.write(buffer, 0, length);
} }
......
...@@ -7,7 +7,6 @@ import android.net.Uri; ...@@ -7,7 +7,6 @@ import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.support.v4.content.FileProvider; import android.support.v4.content.FileProvider;
import com.topjohnwu.magisk.MagiskManager;
import com.topjohnwu.magisk.utils.Utils; import com.topjohnwu.magisk.utils.Utils;
import java.io.File; import java.io.File;
......
...@@ -3,58 +3,17 @@ ...@@ -3,58 +3,17 @@
// //
#include <jni.h> #include <jni.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include "zipadjust.h" #include "zipadjust.h"
JNIEXPORT jbyteArray JNICALL
Java_com_topjohnwu_magisk_utils_ZipUtils_zipAdjust___3BI(JNIEnv *env, jclass type,
jbyteArray jbytes, jint size) {
fin = (*env)->GetPrimitiveArrayCritical(env, jbytes, NULL);
insize = (size_t) size;
zipadjust(0);
(*env)->ReleasePrimitiveArrayCritical(env, jbytes, fin, 0);
jbyteArray ret = (*env)->NewByteArray(env, outsize);
(*env)->SetByteArrayRegion(env, ret, 0, outsize, (const jbyte*) fout);
free(fout);
return ret;
}
JNIEXPORT void JNICALL JNIEXPORT void JNICALL
Java_com_topjohnwu_magisk_utils_ZipUtils_zipAdjust__Ljava_lang_String_2(JNIEnv *env, jclass type, jstring name) { Java_com_topjohnwu_magisk_utils_ZipUtils_zipAdjust(JNIEnv *env, jclass type, jstring filenameIn_,
const char *filename = (*env)->GetStringUTFChars(env, name, NULL); jstring filenameOut_) {
int fd = open(filename, O_RDONLY); const char *filenameIn = (*env)->GetStringUTFChars(env, filenameIn_, 0);
if (fd < 0) const char *filenameOut = (*env)->GetStringUTFChars(env, filenameOut_, 0);
return;
// Load the file to memory
insize = lseek(fd, 0, SEEK_END);
lseek(fd, 0, SEEK_SET);
fin = malloc(insize);
read(fd, fin, insize);
zipadjust(0);
close(fd);
// Open file for output
fd = open(filename, O_WRONLY | O_TRUNC);
if (fd < 0)
return;
(*env)->ReleaseStringUTFChars(env, name, filename);
// Write back to file
lseek(fd, 0, SEEK_SET);
write(fd, fout, outsize);
close(fd); // TODO
free(fin); zipadjust(filenameIn, filenameOut, 0);
free(fout);
} (*env)->ReleaseStringUTFChars(env, filenameIn_, filenameIn);
(*env)->ReleaseStringUTFChars(env, filenameOut_, filenameOut);
}
\ No newline at end of file
This diff is collapsed.
...@@ -3,10 +3,7 @@ ...@@ -3,10 +3,7 @@
#include <android/log.h> #include <android/log.h>
int zipadjust(int decompress); int zipadjust(const char* filenameIn, const char* filenameOut, int decompress);
extern size_t insize, outsize, alloc;
extern unsigned char *fin, *fout;
#define LOG_TAG "zipadjust" #define LOG_TAG "zipadjust"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment