Modernize compress and decompress

This commit is contained in:
topjohnwu 2019-02-20 20:49:26 -05:00
parent 86d8026301
commit e59c5c8780
7 changed files with 192 additions and 180 deletions

View File

@ -11,6 +11,7 @@
#include "bootimg.h" #include "bootimg.h"
#include "magiskboot.h" #include "magiskboot.h"
#include "compress.h"
static void dump(void *buf, size_t size, const char *filename) { static void dump(void *buf, size_t size, const char *filename) {
if (size == 0) if (size == 0)
@ -163,11 +164,8 @@ int boot_img::parse_image(const char * image) {
r_fmt = check_fmt(ramdisk, hdr->ramdisk_size); r_fmt = check_fmt(ramdisk, hdr->ramdisk_size);
} }
char fmt[16]; fprintf(stderr, "KERNEL_FMT [%s]\n", fmt2name[k_fmt]);
get_fmt_name(k_fmt, fmt); fprintf(stderr, "RAMDISK_FMT [%s]\n", fmt2name[r_fmt]);
fprintf(stderr, "KERNEL_FMT [%s]\n", fmt);
get_fmt_name(r_fmt, fmt);
fprintf(stderr, "RAMDISK_FMT [%s]\n", fmt);
return flags & CHROMEOS_FLAG ? CHROMEOS_RET : 0; return flags & CHROMEOS_FLAG ? CHROMEOS_RET : 0;
default: default:

View File

@ -3,6 +3,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <memory>
#include <logging.h> #include <logging.h>
#include <utils.h> #include <utils.h>
@ -10,142 +11,121 @@
#include "magiskboot.h" #include "magiskboot.h"
#include "compress.h" #include "compress.h"
using namespace std;
int64_t decompress(format_t type, int fd, const void *from, size_t size) { int64_t decompress(format_t type, int fd, const void *from, size_t size) {
auto cmp = get_decoder(type); unique_ptr<Compression> cmp(get_decoder(type));
int64_t ret = cmp->one_step(fd, from, size); return cmp->one_step(fd, from, size);
delete cmp;
return ret;
} }
int64_t compress(format_t type, int fd, const void *from, size_t size) { int64_t compress(format_t type, int fd, const void *from, size_t size) {
auto cmp = get_encoder(type); unique_ptr<Compression> cmp(get_encoder(type));
int64_t ret = cmp->one_step(fd, from, size); return cmp->one_step(fd, from, size);
delete cmp;
return ret;
} }
void decompress(char *from, const char *to) { static bool read_file(FILE *fp, const function<void (void *, size_t)> &fn) {
int strip = 1; char buf[4096];
void *file; size_t len;
size_t size = 0; while ((len = fread(buf, 1, sizeof(buf), fp)))
if (strcmp(from, "-") == 0) fn(buf, len);
stream_full_read(STDIN_FILENO, &file, &size); return true;
else }
mmap_ro(from, &file, &size);
format_t type = check_fmt(file, size); void decompress(char *infile, const char *outfile) {
char *ext; bool in_std = strcmp(infile, "-") == 0;
ext = strrchr(from, '.'); bool rm_in = false;
if (to == nullptr)
to = from; FILE *in_file = in_std ? stdin : xfopen(infile, "re");
if (ext != nullptr) { int out_fd = -1;
// Strip out a matched file extension unique_ptr<Compression> cmp;
switch (type) {
case GZIP: read_file(in_file, [&](void *buf, size_t len) -> void {
if (strcmp(ext, ".gz") != 0) if (out_fd < 0) {
strip = 0; format_t type = check_fmt(buf, len);
break; if (!COMPRESSED(type))
case XZ: LOGE("Input file is not a compressed type!\n");
if (strcmp(ext, ".xz") != 0)
strip = 0; cmp = std::move(unique_ptr<Compression>(get_decoder(type)));
break; fprintf(stderr, "Detected format: [%s]\n", fmt2name[type]);
case LZMA:
if (strcmp(ext, ".lzma") != 0) /* If user does not provide outfile, infile has to be either
strip = 0; * <path>.[ext], or '-'. Outfile will be either <path> or '-'.
break; * If the input does not have proper format, abort */
case BZIP2:
if (strcmp(ext, ".bz2") != 0) char *ext = nullptr;
strip = 0; if (outfile == nullptr) {
break; outfile = infile;
case LZ4_LEGACY: if (!in_std) {
case LZ4: ext = strrchr(infile, '.');
if (strcmp(ext, ".lz4") != 0) if (ext == nullptr || strcmp(ext, fmt2ext[type]) != 0)
strip = 0; LOGE("Input file is not a supported type!\n");
break;
default: // Strip out extension and remove input
LOGE("Provided file \'%s\' is not a supported archive format\n", from); *ext = '\0';
rm_in = true;
fprintf(stderr, "Decompressing to [%s]\n", outfile);
}
}
out_fd = strcmp(outfile, "-") == 0 ? STDOUT_FILENO : creat(outfile, 0644);
cmp->set_outfd(out_fd);
if (ext) *ext = '.';
} }
if (strip) if (!cmp->update(buf, len))
*ext = '\0'; LOGE("Decompression error!\n");
} });
int fd; cmp->finalize();
fclose(in_file);
close(out_fd);
if (strcmp(to, "-") == 0) { if (rm_in)
fd = STDOUT_FILENO; unlink(infile);
} else {
fd = creat(to, 0644);
fprintf(stderr, "Decompressing to [%s]\n", to);
}
decompress(type, fd, file, size);
close(fd);
if (to == from && ext != nullptr) {
*ext = '.';
unlink(from);
}
if (strcmp(from, "-") == 0)
free(file);
else
munmap(file, size);
} }
void compress(const char *method, const char *from, const char *to) { void compress(const char *method, const char *infile, const char *outfile) {
format_t type; auto it = name2fmt.find(method);
const char *ext; if (it == name2fmt.end())
char dest[PATH_MAX]; LOGE("Unsupported compression method: [%s]\n", method);
if (strcmp(method, "gzip") == 0) {
type = GZIP; unique_ptr<Compression> cmp(get_encoder(it->second));
ext = "gz";
} else if (strcmp(method, "xz") == 0) { bool in_std = strcmp(infile, "-") == 0;
type = XZ; bool rm_in = false;
ext = "xz";
} else if (strcmp(method, "lzma") == 0) { FILE *in_file = in_std ? stdin : xfopen(infile, "re");
type = LZMA; int out_fd;
ext = "lzma";
} else if (strcmp(method, "lz4") == 0) { if (outfile == nullptr) {
type = LZ4; if (in_std) {
ext = "lz4"; out_fd = STDOUT_FILENO;
} else if (strcmp(method, "lz4_legacy") == 0) { } else {
type = LZ4_LEGACY; /* If user does not provide outfile and infile is not
ext = "lz4"; * STDIN, output to <infile>.[ext] */
} else if (strcmp(method, "bzip2") == 0) { char *tmp = new char[strlen(infile) + 5];
type = BZIP2; sprintf(tmp, "%s%s", infile, fmt2ext[it->second]);
ext = "bz2"; out_fd = creat(tmp, 0644);
fprintf(stderr, "Compressing to [%s]\n", tmp);
delete[] tmp;
rm_in = true;
}
} else { } else {
fprintf(stderr, "Only support following methods: "); out_fd = strcmp(infile, "-") == 0 ? STDOUT_FILENO : creat(infile, 0644);
for (int i = 0; SUP_LIST[i]; ++i)
fprintf(stderr, "%s ", SUP_LIST[i]);
fprintf(stderr, "\n");
exit(1);
} }
void *file;
size_t size; cmp->set_outfd(out_fd);
if (strcmp(from, "-") == 0)
stream_full_read(STDIN_FILENO, &file, &size); read_file(in_file, [&](void *buf, size_t len) -> void {
else if (!cmp->update(buf, len))
mmap_ro(from, &file, &size); LOGE("Compression error!\n");
if (to == nullptr) { });
if (strcmp(from, "-") == 0)
strcpy(dest, "-"); cmp->finalize();
else fclose(in_file);
snprintf(dest, sizeof(dest), "%s.%s", from, ext); close(out_fd);
} else
strcpy(dest, to); if (rm_in)
int fd; unlink(infile);
if (strcmp(dest, "-") == 0) {
fd = STDOUT_FILENO;
} else {
fd = creat(dest, 0644);
fprintf(stderr, "Compressing to [%s]\n", dest);
}
compress(type, fd, file, size);
close(fd);
if (strcmp(from, "-") == 0)
free(file);
else
munmap(file, size);
if (to == nullptr)
unlink(from);
} }

View File

@ -185,3 +185,7 @@ private:
Compression *get_encoder(format_t type); Compression *get_encoder(format_t type);
Compression *get_decoder(format_t type); Compression *get_decoder(format_t type);
int64_t compress(format_t type, int fd, const void *from, size_t size);
int64_t decompress(format_t type, int fd, const void *from, size_t size);
void compress(const char *method, const char *infile, const char *outfile);
void decompress(char *infile, const char *outfile);

View File

@ -2,6 +2,24 @@
#include "format.h" #include "format.h"
std::map<std::string_view, format_t> name2fmt;
Fmt2Name fmt2name;
Fmt2Ext fmt2ext;
class FormatInit {
public:
FormatInit() {
name2fmt["gzip"] = GZIP;
name2fmt["xz"] = XZ;
name2fmt["lzma"] = LZMA;
name2fmt["bzip2"] = BZIP2;
name2fmt["lz4"] = LZ4;
name2fmt["lz4_legacy"] = LZ4_LEGACY;
}
};
static FormatInit init;
#define MATCH(s) (len >= (sizeof(s) - 1) && memcmp(buf, s, sizeof(s) - 1) == 0) #define MATCH(s) (len >= (sizeof(s) - 1) && memcmp(buf, s, sizeof(s) - 1) == 0)
format_t check_fmt(const void *buf, size_t len) { format_t check_fmt(const void *buf, size_t len) {
@ -41,44 +59,51 @@ format_t check_fmt(const void *buf, size_t len) {
} }
} }
void get_fmt_name(format_t fmt, char *name) { const char *Fmt2Name::operator[](format_t fmt) {
const char *s;
switch (fmt) { switch (fmt) {
case CHROMEOS: case CHROMEOS:
s = "chromeos"; return "chromeos";
break;
case AOSP: case AOSP:
s = "aosp"; return "aosp";
break;
case GZIP: case GZIP:
s = "gzip"; return "gzip";
break;
case LZOP: case LZOP:
s = "lzop"; return "lzop";
break;
case XZ: case XZ:
s = "xz"; return "xz";
break;
case LZMA: case LZMA:
s = "lzma"; return "lzma";
break;
case BZIP2: case BZIP2:
s = "bzip2"; return "bzip2";
break;
case LZ4: case LZ4:
s = "lz4"; return "lz4";
break;
case LZ4_LEGACY: case LZ4_LEGACY:
s = "lz4_legacy"; return "lz4_legacy";
break;
case MTK: case MTK:
s = "mtk"; return "mtk";
break;
case DTB: case DTB:
s = "dtb"; return "dtb";
break;
default: default:
s = "raw"; return "raw";
}
}
const char *Fmt2Ext::operator[](format_t fmt) {
switch (fmt) {
case GZIP:
return ".gz";
case LZOP:
return ".lzop";
case XZ:
return ".xz";
case LZMA:
return ".lzma";
case BZIP2:
return ".bz2";
case LZ4:
case LZ4_LEGACY:
return ".lz4";
default:
return "";
} }
strcpy(name, s);
} }

View File

@ -1,26 +1,32 @@
#ifndef _FORMAT_H_ #ifndef _FORMAT_H_
#define _FORMAT_H_ #define _FORMAT_H_
#include <map>
#include <string_view>
typedef enum { typedef enum {
UNKNOWN, UNKNOWN,
/* Boot formats */
CHROMEOS, CHROMEOS,
AOSP, AOSP,
ELF32, ELF32,
ELF64, ELF64,
DHTB,
BLOB,
/* Compression formats */
GZIP, GZIP,
LZOP,
XZ, XZ,
LZMA, LZMA,
BZIP2, BZIP2,
LZ4, LZ4,
LZ4_LEGACY, LZ4_LEGACY,
/* Misc */
LZOP,
MTK, MTK,
DTB, DTB,
DHTB,
BLOB
} format_t; } format_t;
#define COMPRESSED(fmt) (fmt >= GZIP && fmt <= LZ4_LEGACY) #define COMPRESSED(fmt) ((fmt) >= GZIP && (fmt) <= LZ4_LEGACY)
#define BOOT_MAGIC "ANDROID!" #define BOOT_MAGIC "ANDROID!"
#define CHROMEOS_MAGIC "CHROMEOS" #define CHROMEOS_MAGIC "CHROMEOS"
@ -47,7 +53,20 @@ typedef enum {
#define SUP_LIST ((const char *[]) { "gzip", "xz", "lzma", "bzip2", "lz4", "lz4_legacy", NULL }) #define SUP_LIST ((const char *[]) { "gzip", "xz", "lzma", "bzip2", "lz4", "lz4_legacy", NULL })
#define SUP_EXT_LIST ((const char *[]) { "gz", "xz", "lzma", "bz2", "lz4", "lz4", NULL }) #define SUP_EXT_LIST ((const char *[]) { "gz", "xz", "lzma", "bz2", "lz4", "lz4", NULL })
class Fmt2Name {
public:
const char *operator[](format_t fmt);
};
class Fmt2Ext {
public:
const char *operator[](format_t fmt);
};
format_t check_fmt(const void *buf, size_t len); format_t check_fmt(const void *buf, size_t len);
void get_fmt_name(format_t fmt, char *name);
extern std::map<std::string_view, format_t> name2fmt;
extern Fmt2Name fmt2name;
extern Fmt2Ext fmt2ext;
#endif #endif

View File

@ -18,14 +18,8 @@ int unpack(const char *image);
void repack(const char* orig_image, const char* out_image); void repack(const char* orig_image, const char* out_image);
void hexpatch(const char *image, const char *from, const char *to); void hexpatch(const char *image, const char *from, const char *to);
int cpio_commands(int argc, char *argv[]); int cpio_commands(int argc, char *argv[]);
void compress(const char *method, const char *from, const char *to);
void decompress(char *from, const char *to);
int dtb_commands(const char *cmd, int argc, char *argv[]); int dtb_commands(const char *cmd, int argc, char *argv[]);
// Compressions
int64_t compress(format_t type, int fd, const void *from, size_t size);
int64_t decompress(format_t type, int fd, const void *from, size_t size);
// Pattern // Pattern
int patch_verity(void **buf, uint32_t *size, int patch); int patch_verity(void **buf, uint32_t *size, int patch);
void patch_encryption(void **buf, uint32_t *size); void patch_encryption(void **buf, uint32_t *size);

View File

@ -10,10 +10,7 @@
#include <flags.h> #include <flags.h>
#include "magiskboot.h" #include "magiskboot.h"
#include "compress.h"
/********************
Patch Boot Image
*********************/
static void usage(char *arg0) { static void usage(char *arg0) {
fprintf(stderr, fprintf(stderr,
@ -137,24 +134,19 @@ int main(int argc, char *argv[]) {
} else if (argc > 2 && strcmp(argv[1], "--unpack") == 0) { } else if (argc > 2 && strcmp(argv[1], "--unpack") == 0) {
return unpack(argv[2]); return unpack(argv[2]);
} else if (argc > 2 && strcmp(argv[1], "--repack") == 0) { } else if (argc > 2 && strcmp(argv[1], "--repack") == 0) {
repack(argv[2], argc > 3 ? argv[3] : NEW_BOOT); repack(argv[2], argv[3] ? argv[3] : NEW_BOOT);
} else if (argc > 2 && strcmp(argv[1], "--decompress") == 0) { } else if (argc > 2 && strcmp(argv[1], "--decompress") == 0) {
decompress(argv[2], argc > 3 ? argv[3] : nullptr); decompress(argv[2], argv[3]);
} else if (argc > 2 && strncmp(argv[1], "--compress", 10) == 0) { } else if (argc > 2 && strncmp(argv[1], "--compress", 10) == 0) {
const char *method; compress(argv[1][10] == '=' ? &argv[1][11] : "gzip", argv[2], argv[3]);
method = strchr(argv[1], '=');
if (method == nullptr) method = "gzip";
else method++;
compress(method, argv[2], argc > 3 ? argv[3] : nullptr);
} else if (argc > 4 && strcmp(argv[1], "--hexpatch") == 0) { } else if (argc > 4 && strcmp(argv[1], "--hexpatch") == 0) {
hexpatch(argv[2], argv[3], argv[4]); hexpatch(argv[2], argv[3], argv[4]);
} else if (argc > 2 && strcmp(argv[1], "--cpio") == 0) { } else if (argc > 2 && strcmp(argv[1], "--cpio") == 0) {
if (cpio_commands(argc - 2, argv + 2)) usage(argv[0]); if (cpio_commands(argc - 2, argv + 2)) usage(argv[0]);
} else if (argc > 2 && strncmp(argv[1], "--dtb", 5) == 0) { } else if (argc > 2 && strncmp(argv[1], "--dtb", 5) == 0) {
char *cmd = argv[1] + 5; if (argv[1][5] != '-')
if (*cmd == '\0') usage(argv[0]); usage(argv[0]);
else ++cmd; if (dtb_commands(&argv[1][6], argc - 2, argv + 2))
if (dtb_commands(cmd, argc - 2, argv + 2))
usage(argv[0]); usage(argv[0]);
} else { } else {
usage(argv[0]); usage(argv[0]);