Compress binaries and use xz-embedded in magiskinit

This commit is contained in:
topjohnwu
2018-08-10 05:03:54 +08:00
parent 97588408a2
commit d4568aa0a7
4 changed files with 37 additions and 9 deletions

View File

@ -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)

View File

@ -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,

View File

@ -36,6 +36,8 @@
#include <sys/sendfile.h>
#include <sys/sysmacros.h>
#include <xz.h>
#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;
}