diff --git a/build.py b/build.py index 24b688360..ebbbf503e 100755 --- a/build.py +++ b/build.py @@ -106,6 +106,9 @@ def execv(cmd, redirect=None): def system(cmd, redirect=None): return subprocess.run(cmd, shell=True, stdout=redirect if redirect != None else STDOUT) +def xz(data): + return lzma.compress(data, preset=9, check=lzma.CHECK_NONE) + def sign_zip(unsigned, output, release): signer_name = 'zipsigner-3.0.jar' zipsigner = os.path.join('utils', 'build', 'libs', signer_name) @@ -151,7 +154,7 @@ def sign_apk(source, target): def binary_dump(src, out, var_name): out.write('const static unsigned char {}[] = {{'.format(var_name)) - for i, c in enumerate(src.read()): + for i, c in enumerate(xz(src.read())): if i % 16 == 0: out.write('\n') out.write('0x{:02X},'.format(c)) @@ -173,11 +176,11 @@ def gen_update_binary(): binary = os.path.join('native', 'out', 'armeabi-v7a', 'busybox') with open(binary, 'rb') as busybox: update_bin.append('\'\nBB_ARM=') - update_bin.append(base64.b64encode(lzma.compress(busybox.read(), preset=9, check=lzma.CHECK_NONE)).decode('ascii')) + update_bin.append(base64.b64encode(xz(busybox.read())).decode('ascii')) binary = os.path.join('native', 'out', 'x86', 'busybox') with open(binary, 'rb') as busybox: update_bin.append('\nBB_X86=') - update_bin.append(base64.b64encode(lzma.compress(busybox.read(), preset=9, check=lzma.CHECK_NONE)).decode('ascii')) + update_bin.append(base64.b64encode(xz(busybox.read())).decode('ascii')) update_bin.append('\n') with open(os.path.join('scripts', 'update_binary.sh'), 'r') as script: update_bin.append(script.read()) @@ -213,7 +216,7 @@ def build_binary(args): bin_file = os.path.join('native', 'out', arch, 'magisk') with open(os.path.join('native', 'out', arch, 'binaries_arch.h'), 'w') as out: with open(bin_file, 'rb') as src: - binary_dump(src, out, 'magisk_bin') + binary_dump(src, out, 'magisk_xz') old_plat = False flags = base_flags @@ -280,7 +283,7 @@ def build_apk(args): mkdir(os.path.join('native', 'out')) with open(os.path.join('native', 'out', 'binaries.h'), 'w') as out: with open(release, 'rb') as src: - binary_dump(src, out, 'manager_bin'); + binary_dump(src, out, 'manager_xz'); else: proc = execv([gradlew, 'app:assembleDebug']) if proc.returncode != 0: diff --git a/native/jni/Android.mk b/native/jni/Android.mk index cbbaa80c0..16ae1b970 100644 --- a/native/jni/Android.mk +++ b/native/jni/Android.mk @@ -67,10 +67,11 @@ ifdef B_INIT # magiskinit include $(CLEAR_VARS) LOCAL_MODULE := magiskinit -LOCAL_STATIC_LIBRARIES := libsepol +LOCAL_STATIC_LIBRARIES := libsepol libxz LOCAL_C_INCLUDES := \ jni/include \ jni/magiskpolicy \ + $(EXT_PATH)/include \ out \ out/$(TARGET_ARCH_ABI) \ $(LIBSEPOL) diff --git a/native/jni/b64xz.c b/native/jni/b64xz.c index 2517986a9..bc0ee3838 100644 --- a/native/jni/b64xz.c +++ b/native/jni/b64xz.c @@ -22,7 +22,7 @@ static void decodeblock(uint8_t* in, uint8_t* out) { out[2] = (uint8_t)(((in[2] << 6) & 0xc0) | in[3]); } -static int unxz(struct xz_dec *dec, void *buf, unsigned size) { +static int unxz(struct xz_dec *dec, const void *buf, unsigned size) { uint8_t out[8192]; struct xz_buf b = { .in = buf, diff --git a/native/jni/core/magiskinit.c b/native/jni/core/magiskinit.c index 8e3ec3c99..083b4df88 100644 --- a/native/jni/core/magiskinit.c +++ b/native/jni/core/magiskinit.c @@ -36,6 +36,8 @@ #include #include +#include + #include "binaries.h" #include "binaries_arch.h" @@ -243,16 +245,38 @@ static int patch_sepolicy() { return 0; } +static int unxz(int fd, const void *buf, size_t size) { + uint8_t out[8192]; + struct xz_dec *dec = xz_dec_init(XZ_DYNALLOC, 1 << 26); + struct xz_buf b = { + .in = buf, + .in_pos = 0, + .in_size = size, + .out = out, + .out_pos = 0, + .out_size = sizeof(out) + }; + enum xz_ret ret; + do { + ret = xz_dec_run(dec, &b); + if (ret != XZ_OK && ret != XZ_STREAM_END) + return 1; + write(fd, out, b.out_pos); + b.out_pos = 0; + } while (b.in_pos != size); + return 0; +} + static int dump_magisk(const char *path, mode_t mode) { int fd = creat(path, mode); - xwrite(fd, magisk_bin, sizeof(magisk_bin)); + unxz(fd, magisk_xz, sizeof(magisk_xz)); close(fd); return 0; } static int dump_manager(const char *path, mode_t mode) { int fd = creat(path, mode); - xwrite(fd, manager_bin, sizeof(manager_bin)); + unxz(fd, manager_xz, sizeof(manager_xz)); close(fd); return 0; }