-
Notifications
You must be signed in to change notification settings - Fork 18
/
format.cpp
110 lines (103 loc) · 2.84 KB
/
format.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include "format.hpp"
Name2Fmt name2fmt;
Fmt2Name fmt2name;
Fmt2Ext fmt2ext;
#define CHECKED_MATCH(s) (len >= (sizeof(s) - 1) && BUFFER_MATCH(buf, s))
format_t check_fmt(const void *buf, size_t len) {
if (CHECKED_MATCH(CHROMEOS_MAGIC)) {
return CHROMEOS;
} else if (CHECKED_MATCH(BOOT_MAGIC)) {
return AOSP;
} else if (CHECKED_MATCH(VENDOR_BOOT_MAGIC)) {
return AOSP_VENDOR;
} else if (CHECKED_MATCH(GZIP1_MAGIC) || CHECKED_MATCH(GZIP2_MAGIC)) {
return GZIP;
} else if (CHECKED_MATCH(LZOP_MAGIC)) {
return LZOP;
} else if (CHECKED_MATCH(XZ_MAGIC)) {
return XZ;
} else if (len >= 13 && memcmp(buf, "\x5d\x00\x00", 3) == 0
&& (((char *)buf)[12] == '\xff' || ((char *)buf)[12] == '\x00')) {
return LZMA;
} else if (CHECKED_MATCH(BZIP_MAGIC)) {
return BZIP2;
} else if (CHECKED_MATCH(LZ41_MAGIC) || CHECKED_MATCH(LZ42_MAGIC)) {
return LZ4;
} else if (CHECKED_MATCH(LZ4_LEG_MAGIC)) {
return LZ4_LEGACY;
} else if (CHECKED_MATCH(MTK_MAGIC)) {
return MTK;
} else if (CHECKED_MATCH(DTB_MAGIC)) {
return DTB;
} else if (CHECKED_MATCH(DHTB_MAGIC)) {
return DHTB;
} else if (CHECKED_MATCH(TEGRABLOB_MAGIC)) {
return BLOB_FMT;
} else if (len >= 0x28 && memcmp(&((char *)buf)[0x24], ZIMAGE_MAGIC, 4) == 0) {
return ZIMAGE;
} else {
return UNKNOWN;
}
}
const char *Fmt2Name::operator[](format_t fmt) {
switch (fmt) {
case GZIP:
return "gzip";
case ZOPFLI:
return "zopfli";
case LZOP:
return "lzop";
case XZ:
return "xz";
case LZMA:
return "lzma";
case BZIP2:
return "bzip2";
case LZ4:
return "lz4";
case LZ4_LEGACY:
return "lz4_legacy";
case LZ4_LG:
return "lz4_lg";
case DTB:
return "dtb";
case ZIMAGE:
return "zimage";
default:
return "raw";
}
}
const char *Fmt2Ext::operator[](format_t fmt) {
switch (fmt) {
case GZIP:
case ZOPFLI:
return ".gz";
case LZOP:
return ".lzo";
case XZ:
return ".xz";
case LZMA:
return ".lzma";
case BZIP2:
return ".bz2";
case LZ4:
case LZ4_LEGACY:
case LZ4_LG:
return ".lz4";
default:
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("zopfli", ZOPFLI)
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;
}