From 340bac7e427a6cb0ed934040db5b9ba3c7385ed0 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sat, 23 Feb 2019 16:53:51 -0500 Subject: [PATCH] Add decompression command --- native/jni/magiskboot/ramdisk.cpp | 21 +++++++++++++++++++++ native/jni/utils/include/OutStream.h | 10 ++++++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/native/jni/magiskboot/ramdisk.cpp b/native/jni/magiskboot/ramdisk.cpp index 86484e426..75594c665 100644 --- a/native/jni/magiskboot/ramdisk.cpp +++ b/native/jni/magiskboot/ramdisk.cpp @@ -29,6 +29,7 @@ public: void restore(); void backup(const char *orig); void compress(); + void decompress(); }; void magisk_cpio::patch(bool keepverity, bool keepforceencrypt) { @@ -206,6 +207,8 @@ void magisk_cpio::backup(const char *orig) { } void magisk_cpio::compress() { + if (exists(ramdisk_xz)) + return; fprintf(stderr, "Compressing cpio -> [%s]\n", ramdisk_xz); auto init = entries.extract("init"); XZEncoder encoder; @@ -219,6 +222,22 @@ void magisk_cpio::compress() { 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()); + decoder.write(it->second->data, it->second->filesize); + decoder.finalize(); + entries.erase(it); + char *buf; + size_t sz; + static_cast(decoder.get_out())->getbuf(buf, sz); + load_cpio(buf, sz); +} + int cpio_commands(int argc, char *argv[]) { char *incpio = argv[0]; ++argv; @@ -251,6 +270,8 @@ int cpio_commands(int argc, char *argv[]) { return 0; } else if (strcmp(cmdv[0], "compress") == 0){ cpio.compress(); + } else if (strcmp(cmdv[0], "decompress") == 0){ + cpio.decompress(); } else if (cmdc == 2 && strcmp(cmdv[0], "exists") == 0) { exit(!cpio.exists(cmdv[1])); } else if (cmdc == 2 && strcmp(cmdv[0], "backup") == 0) { diff --git a/native/jni/utils/include/OutStream.h b/native/jni/utils/include/OutStream.h index 116bab307..88715e982 100644 --- a/native/jni/utils/include/OutStream.h +++ b/native/jni/utils/include/OutStream.h @@ -64,14 +64,20 @@ public: return true; } - template - void release(void *&b, T &len) { + template + void release(bytes *&b, length &len) { b = buf; len = off; buf = nullptr; off = cap = 0; } + template + void getbuf(bytes *&b, length &len) const { + b = buf; + len = off; + } + ~BufOutStream() override { free(buf); }