2017-02-28 13:56:13 +00:00
|
|
|
#include <zlib.h>
|
|
|
|
#include <lzma.h>
|
2017-03-01 20:12:47 +00:00
|
|
|
#include <lz4frame.h>
|
2017-02-28 13:56:13 +00:00
|
|
|
|
2017-02-27 21:37:47 +00:00
|
|
|
#include "magiskboot.h"
|
|
|
|
|
2017-03-01 20:12:47 +00:00
|
|
|
static int open_new(const char *filename) {
|
|
|
|
int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
|
|
|
|
if (fd < 0)
|
|
|
|
error(1, "Unable to create %s", filename);
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void write_file(const int fd, const unsigned char *buf, const size_t size, const char *filename) {
|
|
|
|
if (write(fd, buf, size) != size)
|
|
|
|
error(1, "Error in writing %s", filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void report(const int mode, const char* filename) {
|
|
|
|
switch(mode) {
|
|
|
|
case 0:
|
|
|
|
printf("Decompressing to %s\n", filename);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
printf("Compressing to %s\n", filename);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mode: 0 = decode; 1 = encode
|
2017-02-28 16:46:11 +00:00
|
|
|
void gzip(int mode, const char* filename, unsigned char* buf, size_t size) {
|
2017-03-01 20:12:47 +00:00
|
|
|
size_t ret = 0, flush, have, pos = 0;
|
2017-02-27 21:37:47 +00:00
|
|
|
z_stream strm;
|
|
|
|
unsigned char out[CHUNK];
|
|
|
|
|
2017-03-01 20:12:47 +00:00
|
|
|
report(mode, filename);
|
|
|
|
int fd = open_new(filename);
|
2017-02-27 21:37:47 +00:00
|
|
|
|
|
|
|
strm.zalloc = Z_NULL;
|
|
|
|
strm.zfree = Z_NULL;
|
|
|
|
strm.opaque = Z_NULL;
|
|
|
|
|
2017-02-28 16:46:11 +00:00
|
|
|
switch(mode) {
|
|
|
|
case 0:
|
|
|
|
ret = inflateInit2(&strm, windowBits | ZLIB_GZIP);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
ret = deflateInit2(&strm, 9, Z_DEFLATED, windowBits | ZLIB_GZIP, memLevel, Z_DEFAULT_STRATEGY);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error(1, "Unsupported gzip mode!");
|
2017-02-27 21:37:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ret != Z_OK)
|
2017-03-01 20:12:47 +00:00
|
|
|
error(1, "Unable to init zlib stream");
|
2017-02-27 21:37:47 +00:00
|
|
|
|
|
|
|
do {
|
|
|
|
strm.next_in = buf + pos;
|
2017-02-28 13:56:13 +00:00
|
|
|
if (pos + CHUNK >= size) {
|
2017-02-27 21:37:47 +00:00
|
|
|
strm.avail_in = size - pos;
|
|
|
|
flush = Z_FINISH;
|
|
|
|
} else {
|
|
|
|
strm.avail_in = CHUNK;
|
|
|
|
flush = Z_NO_FLUSH;
|
|
|
|
}
|
2017-03-01 20:12:47 +00:00
|
|
|
pos += strm.avail_in;
|
2017-02-27 21:37:47 +00:00
|
|
|
|
|
|
|
do {
|
|
|
|
strm.avail_out = CHUNK;
|
|
|
|
strm.next_out = out;
|
2017-02-28 16:46:11 +00:00
|
|
|
switch(mode) {
|
|
|
|
case 0:
|
|
|
|
ret = inflate(&strm, flush);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
ret = deflate(&strm, flush);
|
|
|
|
break;
|
2017-02-27 21:37:47 +00:00
|
|
|
}
|
2017-02-28 16:46:11 +00:00
|
|
|
if (ret == Z_STREAM_ERROR)
|
|
|
|
error(1, "Error when running gzip");
|
|
|
|
|
2017-02-27 21:37:47 +00:00
|
|
|
have = CHUNK - strm.avail_out;
|
2017-03-01 20:12:47 +00:00
|
|
|
write_file(fd, out, have, filename);
|
2017-02-28 16:46:11 +00:00
|
|
|
|
2017-02-27 21:37:47 +00:00
|
|
|
} while (strm.avail_out == 0);
|
|
|
|
|
|
|
|
} while(pos < size);
|
|
|
|
|
2017-02-28 16:46:11 +00:00
|
|
|
switch(mode) {
|
|
|
|
case 0:
|
|
|
|
inflateEnd(&strm);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
deflateEnd(&strm);
|
|
|
|
break;
|
2017-02-27 21:37:47 +00:00
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
}
|
2017-02-28 13:56:13 +00:00
|
|
|
|
2017-02-28 16:46:11 +00:00
|
|
|
|
|
|
|
// Mode: 0 = decode xz/lzma; 1 = encode xz; 2 = encode lzma
|
|
|
|
void lzma(int mode, const char* filename, unsigned char* buf, size_t size) {
|
|
|
|
size_t have, pos = 0;
|
2017-03-01 20:12:47 +00:00
|
|
|
lzma_ret ret = 0;
|
2017-02-28 13:56:13 +00:00
|
|
|
lzma_stream strm = LZMA_STREAM_INIT;
|
|
|
|
lzma_options_lzma opt;
|
2017-03-01 20:12:47 +00:00
|
|
|
lzma_action action;
|
2017-02-28 13:56:13 +00:00
|
|
|
unsigned char out[BUFSIZ];
|
|
|
|
|
2017-03-01 20:12:47 +00:00
|
|
|
report(mode, filename);
|
|
|
|
int fd = open_new(filename);
|
2017-02-28 13:56:13 +00:00
|
|
|
|
2017-02-28 16:46:11 +00:00
|
|
|
// Initialize preset
|
|
|
|
lzma_lzma_preset(&opt, LZMA_PRESET_DEFAULT);
|
|
|
|
lzma_filter filters[] = {
|
|
|
|
{ .id = LZMA_FILTER_LZMA2, .options = &opt },
|
|
|
|
{ .id = LZMA_VLI_UNKNOWN, .options = NULL },
|
|
|
|
};
|
|
|
|
|
|
|
|
switch(mode) {
|
|
|
|
case 0:
|
|
|
|
ret = lzma_auto_decoder(&strm, UINT64_MAX, 0);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
ret = lzma_stream_encoder(&strm, filters, LZMA_CHECK_CRC64);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
ret = lzma_alone_encoder(&strm, &opt);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error(1, "Unsupported lzma mode!");
|
2017-02-28 13:56:13 +00:00
|
|
|
}
|
|
|
|
|
2017-02-28 16:46:11 +00:00
|
|
|
|
2017-02-28 13:56:13 +00:00
|
|
|
if (ret != LZMA_OK)
|
2017-03-01 20:12:47 +00:00
|
|
|
error(1, "Unable to init lzma stream");
|
2017-02-28 13:56:13 +00:00
|
|
|
|
|
|
|
do {
|
|
|
|
strm.next_in = buf + pos;
|
|
|
|
if (pos + BUFSIZ >= size) {
|
|
|
|
strm.avail_in = size - pos;
|
|
|
|
action = LZMA_FINISH;
|
|
|
|
} else {
|
|
|
|
strm.avail_in = BUFSIZ;
|
2017-03-01 20:12:47 +00:00
|
|
|
action = LZMA_RUN;
|
2017-02-28 13:56:13 +00:00
|
|
|
}
|
2017-03-01 20:12:47 +00:00
|
|
|
pos += strm.avail_in;
|
2017-02-28 13:56:13 +00:00
|
|
|
|
|
|
|
do {
|
|
|
|
strm.avail_out = BUFSIZ;
|
|
|
|
strm.next_out = out;
|
|
|
|
ret = lzma_code(&strm, action);
|
|
|
|
have = BUFSIZ - strm.avail_out;
|
2017-03-01 20:12:47 +00:00
|
|
|
write_file(fd, out, have, filename);
|
2017-02-28 13:56:13 +00:00
|
|
|
} 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);
|
|
|
|
}
|
2017-03-01 20:12:47 +00:00
|
|
|
|
|
|
|
// Mode: 0 = decode; 1 = encode
|
|
|
|
void lz4(int mode, const char* filename, unsigned char* buf, size_t size) {
|
|
|
|
LZ4F_decompressionContext_t dctx;
|
|
|
|
LZ4F_compressionContext_t cctx;
|
|
|
|
LZ4F_frameInfo_t info;
|
|
|
|
|
|
|
|
size_t outCapacity, avail_in, ret = 0, pos = 0;
|
|
|
|
size_t have, read;
|
|
|
|
unsigned char *out = NULL;
|
|
|
|
|
|
|
|
report(mode, filename);
|
|
|
|
int fd = open_new(filename);
|
|
|
|
|
|
|
|
// Initialize context
|
|
|
|
switch(mode) {
|
|
|
|
case 0:
|
|
|
|
ret = LZ4F_createDecompressionContext(&dctx, LZ4F_VERSION);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
ret = LZ4F_createCompressionContext(&cctx, LZ4F_VERSION);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error(1, "Unsupported lz4 mode!");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (LZ4F_isError(ret))
|
|
|
|
error(1, "Context creation error: %s\n", LZ4F_getErrorName(ret));
|
|
|
|
|
|
|
|
// Allocate out buffer
|
|
|
|
switch(mode) {
|
|
|
|
case 0:
|
|
|
|
// Read header
|
|
|
|
read = CHUNK;
|
|
|
|
ret = LZ4F_getFrameInfo(dctx, &info, buf, &read);
|
|
|
|
if (LZ4F_isError(ret))
|
|
|
|
error(1, "LZ4F_getFrameInfo error: %s\n", LZ4F_getErrorName(ret));
|
|
|
|
switch (info.blockSizeID) {
|
|
|
|
case LZ4F_default:
|
|
|
|
case LZ4F_max64KB: outCapacity = 1 << 16; break;
|
|
|
|
case LZ4F_max256KB: outCapacity = 1 << 18; break;
|
|
|
|
case LZ4F_max1MB: outCapacity = 1 << 20; break;
|
|
|
|
case LZ4F_max4MB: outCapacity = 1 << 22; break;
|
|
|
|
default:
|
|
|
|
error(1, "Impossible unless more block sizes are allowed\n");
|
|
|
|
}
|
|
|
|
pos += read;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
outCapacity = LZ4F_compressBound(CHUNK, NULL) + LZ4_HEADER_SIZE + LZ4_FOOTER_SIZE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
out = malloc(outCapacity);
|
|
|
|
if (!out)
|
|
|
|
error(1, "LZ4 malloc error!");
|
|
|
|
|
|
|
|
// Write header
|
|
|
|
if (mode == 1) {
|
|
|
|
have = ret = LZ4F_compressBegin(cctx, out, size, NULL);
|
|
|
|
if (LZ4F_isError(ret))
|
|
|
|
error(1, "Failed to start compression: error %s\n", LZ4F_getErrorName(ret));
|
|
|
|
write_file(fd, out, have, filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (pos + CHUNK >= size) {
|
|
|
|
avail_in = size - pos;
|
|
|
|
} else {
|
|
|
|
avail_in = CHUNK;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
switch(mode) {
|
|
|
|
case 0:
|
|
|
|
have = outCapacity, read = avail_in;
|
|
|
|
ret = LZ4F_decompress(dctx, out, &have, buf + pos, &read, NULL);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
read = avail_in;
|
|
|
|
have = ret = LZ4F_compressUpdate(cctx, out, outCapacity, buf + pos, avail_in, NULL);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (LZ4F_isError(ret))
|
|
|
|
error(1, "LZ4 coding error: %s\n", LZ4F_getErrorName(ret));
|
|
|
|
|
|
|
|
write_file(fd, out, have, filename);
|
|
|
|
// Update status
|
|
|
|
pos += read;
|
|
|
|
avail_in -= read;
|
|
|
|
} while(avail_in != 0 && ret != 0);
|
|
|
|
|
|
|
|
} while(pos < size && ret != 0);
|
|
|
|
|
|
|
|
switch(mode) {
|
|
|
|
case 0:
|
|
|
|
LZ4F_freeDecompressionContext(dctx);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
have = ret = LZ4F_compressEnd(cctx, out, outCapacity, NULL);
|
|
|
|
if (LZ4F_isError(ret))
|
|
|
|
error(1, "Failed to end compression: error %s\n", LZ4F_getErrorName(ret));
|
|
|
|
|
|
|
|
write_file(fd, out, have, filename);
|
|
|
|
|
|
|
|
LZ4F_freeCompressionContext(cctx);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(out);
|
|
|
|
}
|