Add decompression command

This commit is contained in:
topjohnwu 2019-02-23 16:53:51 -05:00
parent 1d3ce9fef1
commit 340bac7e42
2 changed files with 29 additions and 2 deletions

View File

@ -29,6 +29,7 @@ public:
void restore(); void restore();
void backup(const char *orig); void backup(const char *orig);
void compress(); void compress();
void decompress();
}; };
void magisk_cpio::patch(bool keepverity, bool keepforceencrypt) { void magisk_cpio::patch(bool keepverity, bool keepforceencrypt) {
@ -206,6 +207,8 @@ void magisk_cpio::backup(const char *orig) {
} }
void magisk_cpio::compress() { void magisk_cpio::compress() {
if (exists(ramdisk_xz))
return;
fprintf(stderr, "Compressing cpio -> [%s]\n", ramdisk_xz); fprintf(stderr, "Compressing cpio -> [%s]\n", ramdisk_xz);
auto init = entries.extract("init"); auto init = entries.extract("init");
XZEncoder encoder; XZEncoder encoder;
@ -219,6 +222,22 @@ void magisk_cpio::compress() {
insert(xz); insert(xz);
} }
void magisk_cpio::decompress() {
auto it = entries.find(ramdisk_xz);
if (it == entries.end())
return;
fprintf(stderr, "Decompressing cpio [%s]\n", ramdisk_xz);
LZMADecoder decoder;
decoder.set_out(make_unique<BufOutStream>());
decoder.write(it->second->data, it->second->filesize);
decoder.finalize();
entries.erase(it);
char *buf;
size_t sz;
static_cast<BufOutStream *>(decoder.get_out())->getbuf(buf, sz);
load_cpio(buf, sz);
}
int cpio_commands(int argc, char *argv[]) { int cpio_commands(int argc, char *argv[]) {
char *incpio = argv[0]; char *incpio = argv[0];
++argv; ++argv;
@ -251,6 +270,8 @@ int cpio_commands(int argc, char *argv[]) {
return 0; return 0;
} else if (strcmp(cmdv[0], "compress") == 0){ } else if (strcmp(cmdv[0], "compress") == 0){
cpio.compress(); cpio.compress();
} else if (strcmp(cmdv[0], "decompress") == 0){
cpio.decompress();
} else if (cmdc == 2 && strcmp(cmdv[0], "exists") == 0) { } else if (cmdc == 2 && strcmp(cmdv[0], "exists") == 0) {
exit(!cpio.exists(cmdv[1])); exit(!cpio.exists(cmdv[1]));
} else if (cmdc == 2 && strcmp(cmdv[0], "backup") == 0) { } else if (cmdc == 2 && strcmp(cmdv[0], "backup") == 0) {

View File

@ -64,14 +64,20 @@ public:
return true; return true;
} }
template <typename T> template <typename bytes, typename length>
void release(void *&b, T &len) { void release(bytes *&b, length &len) {
b = buf; b = buf;
len = off; len = off;
buf = nullptr; buf = nullptr;
off = cap = 0; off = cap = 0;
} }
template <typename bytes, typename length>
void getbuf(bytes *&b, length &len) const {
b = buf;
len = off;
}
~BufOutStream() override { ~BufOutStream() override {
free(buf); free(buf);
} }