diff --git a/jni/magiskboot/Android.mk b/jni/magiskboot/Android.mk index 7e9e44037..5568d391e 100644 --- a/jni/magiskboot/Android.mk +++ b/jni/magiskboot/Android.mk @@ -2,10 +2,11 @@ LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := magiskboot -LOCAL_STATIC_LIBRARIES := liblzma liblz4 +LOCAL_STATIC_LIBRARIES := liblzma liblz4 libbz2 LOCAL_C_INCLUDES := \ jni/ndk-compression/xz/src/liblzma/api/ \ - jni/ndk-compression/lz4/lib/ + jni/ndk-compression/lz4/lib/ \ + jni/ndk-compression/bzip2 LOCAL_SRC_FILES := main.c unpack.c repack.c hexpatch.c parseimg.c compress.c LOCAL_LDLIBS += -lz @@ -13,3 +14,4 @@ include $(BUILD_EXECUTABLE) include jni/ndk-compression/xz/src/liblzma/Android.mk include jni/ndk-compression/lz4/lib/Android.mk +include jni/ndk-compression/bzip2/Android.mk diff --git a/jni/magiskboot/compress.c b/jni/magiskboot/compress.c index 3a8df422d..e779ec42a 100644 --- a/jni/magiskboot/compress.c +++ b/jni/magiskboot/compress.c @@ -1,6 +1,7 @@ #include #include #include +#include #include "magiskboot.h" @@ -11,7 +12,7 @@ static int open_new(const char *filename) { return fd; } -static void write_file(const int fd, const unsigned char *buf, const size_t size, const char *filename) { +static void write_file(const int fd, const void *buf, const size_t size, const char *filename) { if (write(fd, buf, size) != size) error(1, "Error in writing %s", filename); } @@ -272,3 +273,71 @@ void lz4(int mode, const char* filename, unsigned char* buf, size_t size) { free(out); } + +// Mode: 0 = decode; 1 = encode +void bzip2(int mode, const char* filename, unsigned char* buf, size_t size) { + size_t ret = 0, action, have, pos = 0; + bz_stream strm; + char out[CHUNK]; + + report(mode, filename); + int fd = open_new(filename); + + strm.bzalloc = NULL; + strm.bzfree = NULL; + strm.opaque = NULL; + + switch(mode) { + case 0: + ret = BZ2_bzDecompressInit(&strm, 0, 0); + break; + case 1: + ret = BZ2_bzCompressInit(&strm, 9, 0, 0); + break; + default: + error(1, "Unsupported bzip2 mode!"); + } + + if (ret != BZ_OK) + error(1, "Unable to init bzlib stream"); + + do { + strm.next_in = (char *) buf + pos; + if (pos + CHUNK >= size) { + strm.avail_in = size - pos; + action = BZ_FINISH; + } else { + strm.avail_in = CHUNK; + action = BZ_RUN; + } + pos += strm.avail_in; + + do { + strm.avail_out = CHUNK; + strm.next_out = out; + switch(mode) { + case 0: + ret = BZ2_bzDecompress(&strm); + break; + case 1: + ret = BZ2_bzCompress(&strm, action); + break; + } + + have = CHUNK - strm.avail_out; + write_file(fd, out, have, filename); + + } while (strm.avail_out == 0); + + } while(pos < size); + + switch(mode) { + case 0: + BZ2_bzDecompressEnd(&strm); + break; + case 1: + BZ2_bzCompressEnd(&strm); + break; + } + close(fd); +} diff --git a/jni/magiskboot/magiskboot.h b/jni/magiskboot/magiskboot.h index 01fa5ff56..fc62b38a3 100644 --- a/jni/magiskboot/magiskboot.h +++ b/jni/magiskboot/magiskboot.h @@ -60,5 +60,6 @@ void parse_img(unsigned char *orig, size_t size); void gzip(int mode, const char* filename, unsigned char* buf, size_t size); void lzma(int mode, const char* filename, unsigned char* buf, size_t size); void lz4(int mode, const char* filename, unsigned char* buf, size_t size); +void bzip2(int mode, const char* filename, unsigned char* buf, size_t size); #endif diff --git a/jni/magiskboot/repack.c b/jni/magiskboot/repack.c index 5dfda473b..2407edea2 100644 --- a/jni/magiskboot/repack.c +++ b/jni/magiskboot/repack.c @@ -110,7 +110,7 @@ void repack(const char* image) { break; case BZIP2: sprintf(name, "%s.%s", RAMDISK_FILE, "bz2"); - error(1, "Unsupported format! Please compress manually!"); + bzip2(1, name, cpio, cpio_size); break; case LZ4: sprintf(name, "%s.%s", RAMDISK_FILE, "lz4"); diff --git a/jni/magiskboot/unpack.c b/jni/magiskboot/unpack.c index 0f304a4ea..4e3621959 100644 --- a/jni/magiskboot/unpack.c +++ b/jni/magiskboot/unpack.c @@ -49,6 +49,8 @@ void unpack(const char* image) { case LZOP: sprintf(name, "%s.%s", RAMDISK_FILE, "lzo"); printf("Unsupported format! Please decompress manually!\n"); + // Dump the compressed ramdisk + dump(ramdisk, hdr.ramdisk_size, name); break; case XZ: sprintf(name, "%s.%s", RAMDISK_FILE, "xz"); @@ -60,7 +62,7 @@ void unpack(const char* image) { break; case BZIP2: sprintf(name, "%s.%s", RAMDISK_FILE, "bz2"); - printf("Unsupported format! Please decompress manually!\n"); + bzip2(0, RAMDISK_FILE, ramdisk, hdr.ramdisk_size); break; case LZ4: sprintf(name, "%s.%s", RAMDISK_FILE, "lz4"); @@ -70,8 +72,6 @@ void unpack(const char* image) { // Never happens break; } - // Dump the compressed ramdisk, just in case - dump(ramdisk, hdr.ramdisk_size, name); if (hdr.second_size) { // Dump second diff --git a/jni/ndk-compression b/jni/ndk-compression index 602750457..ca5279dea 160000 --- a/jni/ndk-compression +++ b/jni/ndk-compression @@ -1 +1 @@ -Subproject commit 602750457f2383f9a726eb12d2907f4ed282be91 +Subproject commit ca5279deaa9de6ac695af1c710353d616428e229