110 lines
2.1 KiB
C++
Raw Normal View History

2017-09-14 23:11:56 +08:00
#include <string.h>
2020-03-09 01:50:30 -07:00
#include "format.hpp"
2017-09-14 23:11:56 +08:00
2019-02-20 20:49:26 -05:00
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;
2020-10-14 23:45:06 -07:00
name2fmt["lz4_lg"] = LZ4_LG;
2019-02-20 20:49:26 -05:00
}
};
static FormatInit init;
2018-02-10 03:34:13 +08:00
#define MATCH(s) (len >= (sizeof(s) - 1) && memcmp(buf, s, sizeof(s) - 1) == 0)
format_t check_fmt(const void *buf, size_t len) {
if (MATCH(CHROMEOS_MAGIC)) {
2017-09-14 23:11:56 +08:00
return CHROMEOS;
2018-02-10 03:34:13 +08:00
} else if (MATCH(BOOT_MAGIC)) {
2017-09-14 23:11:56 +08:00
return AOSP;
2019-03-08 00:47:15 -05:00
} else if (MATCH(GZIP1_MAGIC) || MATCH(GZIP2_MAGIC)) {
2017-09-14 23:11:56 +08:00
return GZIP;
2018-02-10 03:34:13 +08:00
} else if (MATCH(LZOP_MAGIC)) {
2017-09-14 23:11:56 +08:00
return LZOP;
2018-02-10 03:34:13 +08:00
} else if (MATCH(XZ_MAGIC)) {
2017-09-14 23:11:56 +08:00
return XZ;
2018-02-10 03:34:13 +08:00
} else if (len >= 13 && memcmp(buf, "\x5d\x00\x00", 3) == 0
2017-09-14 23:11:56 +08:00
&& (((char *)buf)[12] == '\xff' || ((char *)buf)[12] == '\x00')) {
return LZMA;
2018-02-10 03:34:13 +08:00
} else if (MATCH(BZIP_MAGIC)) {
2017-09-14 23:11:56 +08:00
return BZIP2;
2019-03-08 00:47:15 -05:00
} else if (MATCH(LZ41_MAGIC) || MATCH(LZ42_MAGIC)) {
2017-09-14 23:11:56 +08:00
return LZ4;
2018-02-10 03:34:13 +08:00
} else if (MATCH(LZ4_LEG_MAGIC)) {
2017-09-14 23:11:56 +08:00
return LZ4_LEGACY;
2018-02-10 03:34:13 +08:00
} else if (MATCH(MTK_MAGIC)) {
2017-09-14 23:11:56 +08:00
return MTK;
2020-10-12 01:55:33 -07:00
} else if (MATCH(DTB_MAGIC)) {
return DTB;
2018-02-10 03:34:13 +08:00
} else if (MATCH(DHTB_MAGIC)) {
2018-01-29 22:16:02 +08:00
return DHTB;
2018-02-10 03:34:13 +08:00
} else if (MATCH(TEGRABLOB_MAGIC)) {
2018-01-30 05:20:18 +08:00
return BLOB;
2017-09-14 23:11:56 +08:00
} else {
return UNKNOWN;
}
}
2019-02-20 20:49:26 -05:00
const char *Fmt2Name::operator[](format_t fmt) {
2018-01-29 03:12:35 +08:00
switch (fmt) {
2017-09-14 23:11:56 +08:00
case CHROMEOS:
2019-02-20 20:49:26 -05:00
return "chromeos";
2017-09-14 23:11:56 +08:00
case AOSP:
2019-02-20 20:49:26 -05:00
return "aosp";
2017-09-14 23:11:56 +08:00
case GZIP:
2019-02-20 20:49:26 -05:00
return "gzip";
2017-09-14 23:11:56 +08:00
case LZOP:
2019-02-20 20:49:26 -05:00
return "lzop";
2017-09-14 23:11:56 +08:00
case XZ:
2019-02-20 20:49:26 -05:00
return "xz";
2017-09-14 23:11:56 +08:00
case LZMA:
2019-02-20 20:49:26 -05:00
return "lzma";
2017-09-14 23:11:56 +08:00
case BZIP2:
2019-02-20 20:49:26 -05:00
return "bzip2";
2017-09-14 23:11:56 +08:00
case LZ4:
2019-02-20 20:49:26 -05:00
return "lz4";
2017-09-14 23:11:56 +08:00
case LZ4_LEGACY:
2019-02-20 20:49:26 -05:00
return "lz4_legacy";
2020-10-14 23:45:06 -07:00
case LZ4_LG:
return "lz4_lg";
2017-09-14 23:11:56 +08:00
case MTK:
2019-02-20 20:49:26 -05:00
return "mtk";
2020-10-12 01:55:33 -07:00
case DTB:
return "dtb";
2019-02-20 20:49:26 -05:00
default:
return "raw";
}
}
const char *Fmt2Ext::operator[](format_t fmt) {
switch (fmt) {
case GZIP:
return ".gz";
case LZOP:
return ".lzo";
2019-02-20 20:49:26 -05:00
case XZ:
return ".xz";
case LZMA:
return ".lzma";
case BZIP2:
return ".bz2";
case LZ4:
case LZ4_LEGACY:
2020-10-14 23:45:06 -07:00
case LZ4_LG:
2019-02-20 20:49:26 -05:00
return ".lz4";
2017-09-14 23:11:56 +08:00
default:
2019-02-20 20:49:26 -05:00
return "";
2017-09-14 23:11:56 +08:00
}
}