Add xz support

This commit is contained in:
topjohnwu 2017-02-28 21:56:13 +08:00
parent 463cbceb07
commit 0d51997e46
4 changed files with 62 additions and 2 deletions

View File

@ -3,6 +3,7 @@ LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS) include $(CLEAR_VARS)
LOCAL_MODULE := magiskboot LOCAL_MODULE := magiskboot
LOCAL_STATIC_LIBRARIES := liblzma LOCAL_STATIC_LIBRARIES := liblzma
LOCAL_C_INCLUDES := jni/ndk-compression/xz/src/liblzma/api/
LOCAL_SRC_FILES := main.c unpack.c repack.c hexpatch.c parseimg.c compress.c LOCAL_SRC_FILES := main.c unpack.c repack.c hexpatch.c parseimg.c compress.c
LOCAL_LDLIBS += -lz LOCAL_LDLIBS += -lz
include $(BUILD_EXECUTABLE) include $(BUILD_EXECUTABLE)

View File

@ -1,3 +1,6 @@
#include <zlib.h>
#include <lzma.h>
#include "magiskboot.h" #include "magiskboot.h"
void gzip(int dec, const char* filename, unsigned char* buf, size_t size) { void gzip(int dec, const char* filename, unsigned char* buf, size_t size) {
@ -25,7 +28,7 @@ void gzip(int dec, const char* filename, unsigned char* buf, size_t size) {
do { do {
strm.next_in = buf + pos; strm.next_in = buf + pos;
if (pos + CHUNK > size) { if (pos + CHUNK >= size) {
strm.avail_in = size - pos; strm.avail_in = size - pos;
pos = size; pos = size;
flush = Z_FINISH; flush = Z_FINISH;
@ -57,3 +60,58 @@ void gzip(int dec, const char* filename, unsigned char* buf, size_t size) {
} }
close(fd); close(fd);
} }
void lzma(int dec, const char* filename, unsigned char* buf, size_t size) {
int have, pos = 0;
lzma_ret ret;
lzma_stream strm = LZMA_STREAM_INIT;
lzma_options_lzma opt;
lzma_action action = LZMA_RUN;
unsigned char out[BUFSIZ];
int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd < 0)
error(1, "Unable to create %s", filename);
if (dec) {
ret = lzma_auto_decoder(&strm, UINT64_MAX, 0);
} else {
lzma_lzma_preset(&opt, LZMA_PRESET_DEFAULT);
lzma_filter filters[] = {
{ .id = LZMA_FILTER_LZMA2, .options = &opt },
{ .id = LZMA_VLI_UNKNOWN, .options = NULL },
};
ret = lzma_stream_encoder(&strm, filters, LZMA_CHECK_CRC64);
}
if (ret != LZMA_OK)
error(1, "Unable to init lzma");
do {
strm.next_in = buf + pos;
if (pos + BUFSIZ >= size) {
strm.avail_in = size - pos;
pos = size;
action = LZMA_FINISH;
} else {
strm.avail_in = BUFSIZ;
pos += BUFSIZ;
}
do {
strm.avail_out = BUFSIZ;
strm.next_out = out;
ret = lzma_code(&strm, action);
have = BUFSIZ - strm.avail_out;
if (write(fd, out, have) != have)
error(1, "Error in writing %s", filename);
} while (strm.avail_out == 0 && ret == LZMA_OK);
if (ret != LZMA_OK && ret != LZMA_STREAM_END)
error(1, "LZMA error %d!", ret);
} while (pos < size);
lzma_end(&strm);
}

View File

@ -10,7 +10,6 @@
#include <sys/mman.h> #include <sys/mman.h>
#include <fcntl.h> #include <fcntl.h>
#include <string.h> #include <string.h>
#include <zlib.h>
#include "bootimg.h" #include "bootimg.h"
@ -58,5 +57,6 @@ void parse_img(unsigned char *orig, size_t size);
// Compressions // Compressions
void gzip(int dec, const char* filename, unsigned char* buf, size_t size); void gzip(int dec, const char* filename, unsigned char* buf, size_t size);
void lzma(int dec, const char* filename, unsigned char* buf, size_t size);
#endif #endif

View File

@ -51,6 +51,7 @@ void unpack(const char* image) {
break; break;
case XZ: case XZ:
sprintf(name, "%s.%s", RAMDISK_FILE, "xz"); sprintf(name, "%s.%s", RAMDISK_FILE, "xz");
lzma(1, RAMDISK_FILE, ramdisk, hdr.ramdisk_size);
break; break;
case LZMA: case LZMA:
sprintf(name, "%s.%s", RAMDISK_FILE, "lzma"); sprintf(name, "%s.%s", RAMDISK_FILE, "lzma");