1
mirror of https://github.com/topjohnwu/Magisk synced 2024-11-14 22:28:37 +01:00

Add bzip2 support

This commit is contained in:
topjohnwu 2017-03-02 05:23:31 +08:00
parent e08d46aa76
commit 2b3b087c29
6 changed files with 80 additions and 8 deletions

View File

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

View File

@ -1,6 +1,7 @@
#include <zlib.h>
#include <lzma.h>
#include <lz4frame.h>
#include <bzlib.h>
#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);
}

View File

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

View File

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

View File

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

@ -1 +1 @@
Subproject commit 602750457f2383f9a726eb12d2907f4ed282be91
Subproject commit ca5279deaa9de6ac695af1c710353d616428e229