diff --git a/native/jni/magiskboot/compress.cpp b/native/jni/magiskboot/compress.cpp index f8eef1fd6..c43e05a41 100644 --- a/native/jni/magiskboot/compress.cpp +++ b/native/jni/magiskboot/compress.cpp @@ -607,8 +607,8 @@ void decompress(char *infile, const char *outfile) { } void compress(const char *method, const char *infile, const char *outfile) { - auto it = name2fmt.find(method); - if (it == name2fmt.end()) + format_t fmt = name2fmt[method]; + if (fmt == UNKNOWN) LOGE("Unknown compression method: [%s]\n", method); bool in_std = infile == "-"sv; @@ -624,7 +624,7 @@ void compress(const char *method, const char *infile, const char *outfile) { /* If user does not provide outfile and infile is not * STDIN, output to .[ext] */ string tmp(infile); - tmp += fmt2ext[it->second]; + tmp += fmt2ext[fmt]; out_fp = xfopen(tmp.data(), "we"); fprintf(stderr, "Compressing to [%s]\n", tmp.data()); rm_in = true; @@ -633,7 +633,7 @@ void compress(const char *method, const char *infile, const char *outfile) { out_fp = outfile == "-"sv ? stdout : xfopen(outfile, "we"); } - auto strm = get_encoder(it->second, make_unique(out_fp)); + auto strm = get_encoder(fmt, make_unique(out_fp)); char buf[4096]; size_t len; diff --git a/native/jni/magiskboot/format.cpp b/native/jni/magiskboot/format.cpp index 0fc9d2e13..b6f437914 100644 --- a/native/jni/magiskboot/format.cpp +++ b/native/jni/magiskboot/format.cpp @@ -1,26 +1,9 @@ -#include - #include "format.hpp" -std::map name2fmt; +Name2Fmt 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; - name2fmt["lz4_lg"] = LZ4_LG; - } -}; - -static FormatInit init; - #define CHECKED_MATCH(s) (len >= (sizeof(s) - 1) && BUFFER_MATCH(buf, s)) format_t check_fmt(const void *buf, size_t len) { @@ -103,3 +86,17 @@ const char *Fmt2Ext::operator[](format_t fmt) { return ""; } } + +#define CHECK(s, f) else if (name == s) return f; + +format_t Name2Fmt::operator[](std::string_view name) { + if (0) {} + CHECK("gzip", GZIP) + CHECK("xz", XZ) + CHECK("lzma", LZMA) + CHECK("bzip2", BZIP2) + CHECK("lz4", LZ4) + CHECK("lz4_legacy", LZ4_LEGACY) + CHECK("lz4_lg", LZ4_LG) + else return UNKNOWN; +} diff --git a/native/jni/magiskboot/format.hpp b/native/jni/magiskboot/format.hpp index da0f3cbb6..d4ac997a2 100644 --- a/native/jni/magiskboot/format.hpp +++ b/native/jni/magiskboot/format.hpp @@ -1,6 +1,5 @@ #pragma once -#include #include typedef enum { @@ -69,8 +68,13 @@ public: const char *operator[](format_t fmt); }; +class Name2Fmt { +public: + format_t operator[](std::string_view name); +}; + format_t check_fmt(const void *buf, size_t len); -extern std::map name2fmt; +extern Name2Fmt name2fmt; extern Fmt2Name fmt2name; extern Fmt2Ext fmt2ext; diff --git a/native/jni/magiskboot/main.cpp b/native/jni/magiskboot/main.cpp index f353ac515..00a129a25 100644 --- a/native/jni/magiskboot/main.cpp +++ b/native/jni/magiskboot/main.cpp @@ -12,6 +12,12 @@ using namespace std; +static void print_methods() { + for (int fmt = GZIP; fmt < LZOP; ++fmt) { + fprintf(stderr, "%s ", fmt2name[(format_t) fmt]); + } +} + static void usage(char *arg0) { fprintf(stderr, R"EOF(MagiskBoot - Boot Image Modification Tool @@ -96,8 +102,7 @@ Supported actions: /[outfile] can be '-' to be STDIN/STDOUT Supported methods: )EOF", arg0); - for (auto &it : name2fmt) - fprintf(stderr, "%s ", it.first.data()); + print_methods(); fprintf(stderr, R"EOF( @@ -106,8 +111,7 @@ Supported actions: /[outfile] can be '-' to be STDIN/STDOUT Supported methods: )EOF"); - for (auto &it : name2fmt) - fprintf(stderr, "%s ", it.first.data()); + print_methods(); fprintf(stderr, "\n\n"); exit(1);