Magisk/native/jni/magiskboot/compress.cpp

518 lines
11 KiB
C++
Raw Normal View History

2017-09-14 15:11:56 +00:00
#include <unistd.h>
2017-11-09 17:51:41 +00:00
#include <fcntl.h>
2018-09-27 07:11:10 +00:00
#include <stdlib.h>
#include <string.h>
2017-09-14 15:11:56 +00:00
#include <sys/mman.h>
2017-02-28 13:56:13 +00:00
#include <zlib.h>
#include <lzma.h>
2017-03-28 20:09:59 +00:00
#include <lz4.h>
2017-03-01 20:12:47 +00:00
#include <lz4frame.h>
2017-10-12 16:18:40 +00:00
#include <lz4hc.h>
2017-03-01 21:23:31 +00:00
#include <bzlib.h>
2017-02-28 13:56:13 +00:00
2017-02-27 21:37:47 +00:00
#include "magiskboot.h"
2017-09-14 15:11:56 +00:00
#include "logging.h"
#include "utils.h"
2017-02-27 21:37:47 +00:00
2017-03-28 20:09:59 +00:00
#define CHUNK 0x40000
2017-03-01 20:12:47 +00:00
// Mode: 0 = decode; 1 = encode
size_t gzip(int mode, int fd, const void *buf, size_t size) {
2017-11-11 20:08:52 +00:00
size_t ret = 0, have, total = 0;
2017-02-27 21:37:47 +00:00
z_stream strm;
unsigned char out[CHUNK];
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
2017-02-28 16:46:11 +00:00
switch(mode) {
case 0:
2017-11-11 20:08:52 +00:00
ret = inflateInit2(&strm, 15 | 16);
2017-02-28 16:46:11 +00:00
break;
case 1:
2017-11-11 20:08:52 +00:00
ret = deflateInit2(&strm, 9, Z_DEFLATED, 15 | 16, 8, Z_DEFAULT_STRATEGY);
2017-02-28 16:46:11 +00:00
break;
2017-02-27 21:37:47 +00:00
}
if (ret != Z_OK)
2017-09-14 15:11:56 +00:00
LOGE("Unable to init zlib stream\n");
2017-02-27 21:37:47 +00:00
2018-10-25 01:08:06 +00:00
strm.next_in = (Bytef *) buf;
2017-11-11 20:08:52 +00:00
strm.avail_in = size;
2017-02-27 21:37:47 +00:00
do {
2017-11-11 20:08:52 +00:00
strm.avail_out = CHUNK;
strm.next_out = out;
switch(mode) {
case 0:
ret = inflate(&strm, Z_FINISH);
break;
case 1:
ret = deflate(&strm, Z_FINISH);
break;
2017-02-27 21:37:47 +00:00
}
2017-11-11 20:08:52 +00:00
if (ret == Z_STREAM_ERROR)
LOGE("Error when running gzip\n");
have = CHUNK - strm.avail_out;
total += xwrite(fd, out, have);
} while (strm.avail_out == 0);
2017-02-27 21:37:47 +00:00
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
}
return total;
2017-02-27 21:37:47 +00:00
}
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
size_t lzma(int mode, int fd, const void *buf, size_t size) {
2017-11-11 20:08:52 +00:00
size_t have, total = 0;
2018-10-25 01:08:06 +00:00
lzma_ret ret = LZMA_OK;
2017-02-28 13:56:13 +00:00
lzma_stream strm = LZMA_STREAM_INIT;
lzma_options_lzma opt;
2017-11-11 20:08:52 +00:00
unsigned char out[CHUNK];
2017-02-28 13:56:13 +00:00
2017-02-28 16:46:11 +00:00
// Initialize preset
2017-10-12 16:18:40 +00:00
lzma_lzma_preset(&opt, 9);
2017-02-28 16:46:11 +00:00
lzma_filter filters[] = {
{ .id = LZMA_FILTER_LZMA2, .options = &opt },
2018-10-25 01:08:06 +00:00
{ .id = LZMA_VLI_UNKNOWN, .options = nullptr },
2017-02-28 16:46:11 +00:00
};
switch(mode) {
case 0:
ret = lzma_auto_decoder(&strm, UINT64_MAX, 0);
break;
case 1:
ret = lzma_stream_encoder(&strm, filters, LZMA_CHECK_CRC32);
2017-02-28 16:46:11 +00:00
break;
case 2:
ret = lzma_alone_encoder(&strm, &opt);
break;
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-09-14 15:11:56 +00:00
LOGE("Unable to init lzma stream\n");
2017-02-28 13:56:13 +00:00
2018-10-25 01:08:06 +00:00
strm.next_in = static_cast<const uint8_t *>(buf);
2017-11-11 20:08:52 +00:00
strm.avail_in = size;
2017-02-28 13:56:13 +00:00
2017-11-11 20:08:52 +00:00
do {
strm.avail_out = CHUNK;
strm.next_out = out;
ret = lzma_code(&strm, LZMA_FINISH);
2017-02-28 13:56:13 +00:00
if (ret != LZMA_OK && ret != LZMA_STREAM_END)
2017-09-14 15:11:56 +00:00
LOGE("LZMA error %d!\n", ret);
2017-11-11 20:08:52 +00:00
have = CHUNK - strm.avail_out;
total += xwrite(fd, out, have);
} while (strm.avail_out == 0);
2017-02-28 13:56:13 +00:00
lzma_end(&strm);
return total;
2017-02-28 13:56:13 +00:00
}
2017-03-01 20:12:47 +00:00
// Mode: 0 = decode; 1 = encode
2018-10-25 01:08:06 +00:00
size_t lz4(int mode, int fd, const uint8_t *buf, size_t size) {
2017-03-01 20:12:47 +00:00
LZ4F_decompressionContext_t dctx;
LZ4F_compressionContext_t cctx;
LZ4F_frameInfo_t info;
2017-09-12 17:33:22 +00:00
size_t blockSize, outCapacity, avail_in, ret = 0, pos = 0, total = 0;
2017-03-01 20:12:47 +00:00
size_t have, read;
2018-10-25 01:08:06 +00:00
uint8_t *out = nullptr;
2017-03-01 20:12:47 +00:00
// Initialize context
switch(mode) {
case 0:
ret = LZ4F_createDecompressionContext(&dctx, LZ4F_VERSION);
break;
case 1:
ret = LZ4F_createCompressionContext(&cctx, LZ4F_VERSION);
break;
}
2017-09-12 07:27:28 +00:00
2017-03-01 20:12:47 +00:00
if (LZ4F_isError(ret))
2017-09-14 15:11:56 +00:00
LOGE("Context creation error: %s\n", LZ4F_getErrorName(ret));
2017-03-01 20:12:47 +00:00
// Allocate out buffer
2017-09-12 17:33:22 +00:00
blockSize = 1 << 22;
2017-03-01 20:12:47 +00:00
switch(mode) {
case 0:
// Read header
2017-09-12 17:33:22 +00:00
read = blockSize;
2017-03-01 20:12:47 +00:00
ret = LZ4F_getFrameInfo(dctx, &info, buf, &read);
if (LZ4F_isError(ret))
2017-09-14 15:11:56 +00:00
LOGE("LZ4F_getFrameInfo error: %s\n", LZ4F_getErrorName(ret));
2017-03-01 20:12:47 +00:00
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:
2017-09-14 15:11:56 +00:00
LOGE("Impossible unless more block sizes are allowed\n");
2017-03-01 20:12:47 +00:00
}
pos += read;
break;
case 1:
2018-10-25 01:08:06 +00:00
outCapacity = LZ4F_compressFrameBound(blockSize, nullptr);
2017-03-01 20:12:47 +00:00
break;
}
2018-10-25 01:08:06 +00:00
out = new uint8_t[outCapacity];
2017-03-01 20:12:47 +00:00
// Write header
if (mode == 1) {
2018-10-25 01:08:06 +00:00
LZ4F_preferences_t prefs = LZ4F_preferences_t();
2017-09-12 17:33:22 +00:00
prefs.autoFlush = 1;
prefs.compressionLevel = 9;
2018-10-25 01:08:06 +00:00
prefs.frameInfo.blockMode = LZ4F_blockIndependent;
prefs.frameInfo.blockSizeID = LZ4F_max4MB;
prefs.frameInfo.blockChecksumFlag = LZ4F_noBlockChecksum;
prefs.frameInfo.contentChecksumFlag = LZ4F_contentChecksumEnabled;
2017-09-12 17:33:22 +00:00
have = ret = LZ4F_compressBegin(cctx, out, size, &prefs);
2017-03-01 20:12:47 +00:00
if (LZ4F_isError(ret))
2017-09-14 15:11:56 +00:00
LOGE("Failed to start compression: error %s\n", LZ4F_getErrorName(ret));
total += xwrite(fd, out, have);
2017-03-01 20:12:47 +00:00
}
do {
2017-09-12 17:33:22 +00:00
if (pos + blockSize >= size) {
2017-03-01 20:12:47 +00:00
avail_in = size - pos;
} else {
2017-09-12 17:33:22 +00:00
avail_in = blockSize;
2017-03-01 20:12:47 +00:00
}
do {
switch(mode) {
case 0:
2017-09-12 17:33:22 +00:00
have = outCapacity;
read = avail_in;
2018-10-25 01:08:06 +00:00
ret = LZ4F_decompress(dctx, out, &have, buf + pos, &read, nullptr);
2017-03-01 20:12:47 +00:00
break;
case 1:
read = avail_in;
2018-10-25 01:08:06 +00:00
have = ret = LZ4F_compressUpdate(cctx, out, outCapacity, buf + pos, avail_in, nullptr);
2017-03-01 20:12:47 +00:00
break;
}
if (LZ4F_isError(ret))
2017-09-14 15:11:56 +00:00
LOGE("LZ4 coding error: %s\n", LZ4F_getErrorName(ret));
2017-03-01 20:12:47 +00:00
total += xwrite(fd, out, have);
2017-03-01 20:12:47 +00:00
// 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:
2018-10-25 01:08:06 +00:00
have = ret = LZ4F_compressEnd(cctx, out, outCapacity, nullptr);
2017-03-01 20:12:47 +00:00
if (LZ4F_isError(ret))
2017-09-14 15:11:56 +00:00
LOGE("Failed to end compression: error %s\n", LZ4F_getErrorName(ret));
2017-03-01 20:12:47 +00:00
total += xwrite(fd, out, have);
2017-03-01 20:12:47 +00:00
LZ4F_freeCompressionContext(cctx);
break;
}
2018-10-25 01:08:06 +00:00
delete[] out;
return total;
2017-03-01 20:12:47 +00:00
}
2017-03-01 21:23:31 +00:00
// Mode: 0 = decode; 1 = encode
size_t bzip2(int mode, int fd, const void* buf, size_t size) {
2017-11-11 20:08:52 +00:00
size_t ret = 0, have, total = 0;
2017-03-01 21:23:31 +00:00
bz_stream strm;
char out[CHUNK];
2018-10-25 01:08:06 +00:00
strm.bzalloc = nullptr;
strm.bzfree = nullptr;
strm.opaque = nullptr;
2017-03-01 21:23:31 +00:00
switch(mode) {
case 0:
ret = BZ2_bzDecompressInit(&strm, 0, 0);
break;
case 1:
ret = BZ2_bzCompressInit(&strm, 9, 0, 0);
break;
}
if (ret != BZ_OK)
2017-09-14 15:11:56 +00:00
LOGE("Unable to init bzlib stream\n");
2017-03-01 21:23:31 +00:00
2018-10-25 01:08:06 +00:00
strm.next_in = (char *) buf;
2017-11-11 20:08:52 +00:00
strm.avail_in = size;
2017-03-01 21:23:31 +00:00
do {
2017-11-11 20:08:52 +00:00
strm.avail_out = CHUNK;
strm.next_out = out;
switch(mode) {
case 0:
ret = BZ2_bzDecompress(&strm);
break;
case 1:
ret = BZ2_bzCompress(&strm, BZ_FINISH);
break;
2017-03-01 21:23:31 +00:00
}
2017-11-11 20:08:52 +00:00
have = CHUNK - strm.avail_out;
total += xwrite(fd, out, have);
} while (strm.avail_out == 0);
2017-03-01 21:23:31 +00:00
switch(mode) {
case 0:
BZ2_bzDecompressEnd(&strm);
break;
case 1:
BZ2_bzCompressEnd(&strm);
break;
}
return total;
2017-03-01 21:23:31 +00:00
}
2017-03-02 13:59:37 +00:00
2017-11-11 20:08:52 +00:00
#define LZ4_LEGACY_BLOCKSIZE 0x800000
2017-03-28 20:09:59 +00:00
// Mode: 0 = decode; 1 = encode
2018-10-25 01:08:06 +00:00
size_t lz4_legacy(int mode, int fd, const uint8_t *buf, size_t size) {
2017-10-12 16:18:40 +00:00
size_t pos = 0;
2017-03-28 20:09:59 +00:00
int have;
char *out;
2017-10-12 16:18:40 +00:00
unsigned block_size, insize, total = 0;
2017-03-28 20:09:59 +00:00
switch(mode) {
case 0:
2018-10-25 01:08:06 +00:00
out = new char[LZ4_LEGACY_BLOCKSIZE];
2017-03-28 20:09:59 +00:00
// Skip magic
pos += 4;
break;
case 1:
2018-10-25 01:08:06 +00:00
out = new char[LZ4_COMPRESSBOUND(LZ4_LEGACY_BLOCKSIZE)];
2017-03-28 20:09:59 +00:00
// Write magic
total += xwrite(fd, "\x02\x21\x4c\x18", 4);
2017-03-28 20:09:59 +00:00
break;
}
do {
switch(mode) {
case 0:
2017-10-12 16:18:40 +00:00
// Read block size
block_size = *(unsigned *)(buf + pos);
2017-03-28 20:09:59 +00:00
pos += 4;
if (block_size > LZ4_COMPRESSBOUND(LZ4_LEGACY_BLOCKSIZE))
2017-10-12 16:18:40 +00:00
goto done;
2018-10-25 01:08:06 +00:00
have = LZ4_decompress_safe((const char *) buf + pos, out, block_size, LZ4_LEGACY_BLOCKSIZE);
2017-03-28 20:09:59 +00:00
if (have < 0)
2017-09-14 15:11:56 +00:00
LOGE("Cannot decode lz4_legacy block\n");
2017-03-28 20:09:59 +00:00
pos += block_size;
break;
case 1:
if (pos + LZ4_LEGACY_BLOCKSIZE >= size)
insize = size - pos;
else
insize = LZ4_LEGACY_BLOCKSIZE;
2018-10-25 01:08:06 +00:00
have = LZ4_compress_HC((const char *) buf + pos, out, insize, LZ4_COMPRESSBOUND(LZ4_LEGACY_BLOCKSIZE), 9);
2017-03-28 20:09:59 +00:00
if (have == 0)
2017-09-14 15:11:56 +00:00
LOGE("lz4_legacy compression error\n");
2017-03-28 20:09:59 +00:00
pos += insize;
2017-10-12 16:18:40 +00:00
// Write block size
total += xwrite(fd, &have, sizeof(have));
2017-03-28 20:09:59 +00:00
break;
}
// Write main data
total += xwrite(fd, out, have);
2017-03-28 20:09:59 +00:00
} while(pos < size);
2017-10-12 16:18:40 +00:00
done:
if (mode == 1) {
// Append original size to output
unsigned uncomp = size;
xwrite(fd, &uncomp, sizeof(uncomp));
}
2018-10-25 01:08:06 +00:00
delete[] out;
return total;
2017-03-28 20:09:59 +00:00
}
2018-10-25 01:08:06 +00:00
long long decompress(format_t type, int fd, const void *from, size_t size) {
const uint8_t *buf = (uint8_t *) from;
2017-03-02 13:59:37 +00:00
switch (type) {
case GZIP:
2018-10-25 01:08:06 +00:00
return gzip(0, fd, buf, size);
2017-03-02 13:59:37 +00:00
case XZ:
2018-10-25 01:08:06 +00:00
return lzma(0, fd, buf, size);
2017-03-02 13:59:37 +00:00
case LZMA:
2018-10-25 01:08:06 +00:00
return lzma(0, fd, buf, size);
2017-03-02 13:59:37 +00:00
case BZIP2:
2018-10-25 01:08:06 +00:00
return bzip2(0, fd, buf, size);
2017-03-02 13:59:37 +00:00
case LZ4:
2018-10-25 01:08:06 +00:00
return lz4(0, fd, buf, size);
2017-03-28 20:09:59 +00:00
case LZ4_LEGACY:
2018-10-25 01:08:06 +00:00
return lz4_legacy(0, fd, buf, size);
2017-03-02 13:59:37 +00:00
default:
// Unsupported
return -1;
2017-03-02 13:59:37 +00:00
}
}
2018-10-25 01:08:06 +00:00
long long compress(format_t type, int fd, const void *from, size_t size) {
const uint8_t *buf = (uint8_t *) from;
2017-03-02 13:59:37 +00:00
switch (type) {
case GZIP:
2018-10-25 01:08:06 +00:00
return gzip(1, fd, buf, size);
2017-03-02 13:59:37 +00:00
case XZ:
2018-10-25 01:08:06 +00:00
return lzma(1, fd, buf, size);
2017-03-02 13:59:37 +00:00
case LZMA:
2018-10-25 01:08:06 +00:00
return lzma(2, fd, buf, size);
2017-03-02 13:59:37 +00:00
case BZIP2:
2018-10-25 01:08:06 +00:00
return bzip2(1, fd, buf, size);
2017-03-02 13:59:37 +00:00
case LZ4:
2018-10-25 01:08:06 +00:00
return lz4(1, fd, buf, size);
2017-03-28 20:09:59 +00:00
case LZ4_LEGACY:
2018-10-25 01:08:06 +00:00
return lz4_legacy(1, fd, buf, size);
2017-03-02 13:59:37 +00:00
default:
// Unsupported
return -1;
2017-03-02 13:59:37 +00:00
}
}
/*
* Below are utility functions for commandline
*/
2018-10-25 01:08:06 +00:00
void decompress(char *from, const char *to) {
2017-11-10 12:25:41 +00:00
int strip = 1;
2017-09-12 07:27:28 +00:00
void *file;
2017-11-10 12:25:41 +00:00
size_t size = 0;
if (strcmp(from, "-") == 0)
2017-12-06 17:30:48 +00:00
stream_full_read(STDIN_FILENO, &file, &size);
2017-11-10 12:25:41 +00:00
else
mmap_ro(from, &file, &size);
2018-02-09 19:34:13 +00:00
format_t type = check_fmt(file, size);
char *ext;
ext = strrchr(from, '.');
2018-10-25 01:08:06 +00:00
if (to == nullptr)
2017-11-10 12:25:41 +00:00
to = from;
2018-10-25 01:08:06 +00:00
if (ext != nullptr) {
2017-11-10 12:25:41 +00:00
// Strip out a matched file extension
switch (type) {
case GZIP:
if (strcmp(ext, ".gz") != 0)
2017-11-10 12:25:41 +00:00
strip = 0;
break;
case XZ:
if (strcmp(ext, ".xz") != 0)
2017-11-10 12:25:41 +00:00
strip = 0;
break;
case LZMA:
if (strcmp(ext, ".lzma") != 0)
2017-11-10 12:25:41 +00:00
strip = 0;
break;
case BZIP2:
if (strcmp(ext, ".bz2") != 0)
2017-11-10 12:25:41 +00:00
strip = 0;
break;
2017-03-28 20:09:59 +00:00
case LZ4_LEGACY:
case LZ4:
if (strcmp(ext, ".lz4") != 0)
2017-11-10 12:25:41 +00:00
strip = 0;
break;
default:
2017-09-14 15:11:56 +00:00
LOGE("Provided file \'%s\' is not a supported archive format\n", from);
}
2017-11-10 12:25:41 +00:00
if (strip)
*ext = '\0';
}
int fd;
if (strcmp(to, "-") == 0) {
fd = STDOUT_FILENO;
} else {
2017-11-10 12:25:41 +00:00
fd = creat(to, 0644);
2017-12-20 19:36:18 +00:00
fprintf(stderr, "Decompressing to [%s]\n", to);
}
2017-11-10 12:25:41 +00:00
2018-10-25 01:08:06 +00:00
decompress(type, fd, file, size);
2017-11-10 12:25:41 +00:00
close(fd);
2018-10-25 01:08:06 +00:00
if (to == from && ext != nullptr) {
2017-11-10 12:25:41 +00:00
*ext = '.';
unlink(from);
}
if (strcmp(from, "-") == 0)
free(file);
else
munmap(file, size);
}
2018-10-25 01:08:06 +00:00
void compress(const char *method, const char *from, const char *to) {
2018-01-28 19:12:35 +00:00
format_t type;
2018-10-25 01:08:06 +00:00
const char *ext;
char dest[PATH_MAX];
if (strcmp(method, "gzip") == 0) {
type = GZIP;
ext = "gz";
} else if (strcmp(method, "xz") == 0) {
type = XZ;
ext = "xz";
} else if (strcmp(method, "lzma") == 0) {
type = LZMA;
ext = "lzma";
} else if (strcmp(method, "lz4") == 0) {
type = LZ4;
ext = "lz4";
2017-03-28 20:09:59 +00:00
} else if (strcmp(method, "lz4_legacy") == 0) {
type = LZ4_LEGACY;
ext = "lz4";
} else if (strcmp(method, "bzip2") == 0) {
type = BZIP2;
ext = "bz2";
} else {
2017-04-28 13:48:38 +00:00
fprintf(stderr, "Only support following methods: ");
for (int i = 0; SUP_LIST[i]; ++i)
fprintf(stderr, "%s ", SUP_LIST[i]);
fprintf(stderr, "\n");
exit(1);
}
2017-09-12 07:27:28 +00:00
void *file;
size_t size;
2017-11-10 12:25:41 +00:00
if (strcmp(from, "-") == 0)
2017-12-06 17:30:48 +00:00
stream_full_read(STDIN_FILENO, &file, &size);
2017-09-12 17:33:22 +00:00
else
2017-11-10 12:25:41 +00:00
mmap_ro(from, &file, &size);
2018-10-25 01:08:06 +00:00
if (to == nullptr) {
2017-11-10 12:25:41 +00:00
if (strcmp(from, "-") == 0)
strcpy(dest, "-");
else
snprintf(dest, sizeof(dest), "%s.%s", from, ext);
} else
2017-09-12 17:33:22 +00:00
strcpy(dest, to);
2017-11-10 12:25:41 +00:00
int fd;
if (strcmp(dest, "-") == 0) {
fd = STDOUT_FILENO;
} else {
fd = creat(dest, 0644);
2017-12-20 19:36:18 +00:00
fprintf(stderr, "Compressing to [%s]\n", dest);
2017-11-10 12:25:41 +00:00
}
2018-10-25 01:08:06 +00:00
compress(type, fd, file, size);
close(fd);
2017-11-10 12:25:41 +00:00
if (strcmp(from, "-") == 0)
free(file);
else
munmap(file, size);
2018-10-25 01:08:06 +00:00
if (to == nullptr)
unlink(from);
}