Debian Codegraph @

matchers/archive.go

raw symbols SCIP
1 package matchers
2
3 import "encoding/binary"
4
5 const (
6 ZstdMagicSkippableStart = 0x184D2A50
7 ZstdMagicSkippableMask = 0xFFFFFFF0
8 )
9
10 var (
11 TypeEpub = newType("epub", "application/epub+zip")
12 TypeZip = newType("zip", "application/zip")
13 TypeTar = newType("tar", "application/x-tar")
14 TypeRar = newType("rar", "application/vnd.rar")
15 TypeGz = newType("gz", "application/gzip")
16 TypeBz2 = newType("bz2", "application/x-bzip2")
17 Type7z = newType("7z", "application/x-7z-compressed")
18 TypeXz = newType("xz", "application/x-xz")
19 TypeZstd = newType("zst", "application/zstd")
20 TypePdf = newType("pdf", "application/pdf")
21 TypeExe = newType("exe", "application/vnd.microsoft.portable-executable")
22 TypeSwf = newType("swf", "application/x-shockwave-flash")
23 TypeRtf = newType("rtf", "application/rtf")
24 TypeEot = newType("eot", "application/octet-stream")
25 TypePs = newType("ps", "application/postscript")
26 TypeSqlite = newType("sqlite", "application/vnd.sqlite3")
27 TypeNes = newType("nes", "application/x-nintendo-nes-rom")
28 TypeCrx = newType("crx", "application/x-google-chrome-extension")
29 TypeCab = newType("cab", "application/vnd.ms-cab-compressed")
30 TypeDeb = newType("deb", "application/vnd.debian.binary-package")
31 TypeAr = newType("ar", "application/x-unix-archive")
32 TypeZ = newType("Z", "application/x-compress")
33 TypeLz = newType("lz", "application/x-lzip")
34 TypeRpm = newType("rpm", "application/x-rpm")
35 TypeElf = newType("elf", "application/x-executable")
36 TypeDcm = newType("dcm", "application/dicom")
37 TypeIso = newType("iso", "application/x-iso9660-image")
38 TypeMachO = newType("macho", "application/x-mach-binary") // Mach-O binaries have no common extension.
39 )
40
41 var Archive = Map{
42 TypeEpub: bytePrefixMatcher(epubMagic),
43 TypeZip: Zip,
44 TypeTar: Tar,
45 TypeRar: Rar,
46 TypeGz: bytePrefixMatcher(gzMagic),
47 TypeBz2: bytePrefixMatcher(bz2Magic),
48 Type7z: bytePrefixMatcher(sevenzMagic),
49 TypeXz: bytePrefixMatcher(xzMagic),
50 TypeZstd: Zst,
51 TypePdf: bytePrefixMatcher(pdfMagic),
52 TypeExe: bytePrefixMatcher(exeMagic),
53 TypeSwf: Swf,
54 TypeRtf: bytePrefixMatcher(rtfMagic),
55 TypeEot: Eot,
56 TypePs: bytePrefixMatcher(psMagic),
57 TypeSqlite: bytePrefixMatcher(sqliteMagic),
58 TypeNes: bytePrefixMatcher(nesMagic),
59 TypeCrx: bytePrefixMatcher(crxMagic),
60 TypeCab: Cab,
61 TypeDeb: bytePrefixMatcher(debMagic),
62 TypeAr: bytePrefixMatcher(arMagic),
63 TypeZ: Z,
64 TypeLz: bytePrefixMatcher(lzMagic),
65 TypeRpm: Rpm,
66 TypeElf: Elf,
67 TypeDcm: Dcm,
68 TypeIso: Iso,
69 TypeMachO: MachO,
70 }
71
72 var (
73 epubMagic = []byte{
74 0x50, 0x4B, 0x03, 0x04, 0x6D, 0x69, 0x6D, 0x65,
75 0x74, 0x79, 0x70, 0x65, 0x61, 0x70, 0x70, 0x6C,
76 0x69, 0x63, 0x61, 0x74, 0x69, 0x6F, 0x6E, 0x2F,
77 0x65, 0x70, 0x75, 0x62, 0x2B, 0x7A, 0x69, 0x70,
78 }
79 gzMagic = []byte{0x1F, 0x8B, 0x08}
80 bz2Magic = []byte{0x42, 0x5A, 0x68}
81 sevenzMagic = []byte{0x37, 0x7A, 0xBC, 0xAF, 0x27, 0x1C}
82 pdfMagic = []byte{0x25, 0x50, 0x44, 0x46}
83 exeMagic = []byte{0x4D, 0x5A}
84 rtfMagic = []byte{0x7B, 0x5C, 0x72, 0x74, 0x66}
85 nesMagic = []byte{0x4E, 0x45, 0x53, 0x1A}
86 crxMagic = []byte{0x43, 0x72, 0x32, 0x34}
87 psMagic = []byte{0x25, 0x21}
88 xzMagic = []byte{0xFD, 0x37, 0x7A, 0x58, 0x5A, 0x00}
89 sqliteMagic = []byte{0x53, 0x51, 0x4C, 0x69}
90 debMagic = []byte{
91 0x21, 0x3C, 0x61, 0x72, 0x63, 0x68, 0x3E, 0x0A,
92 0x64, 0x65, 0x62, 0x69, 0x61, 0x6E, 0x2D, 0x62,
93 0x69, 0x6E, 0x61, 0x72, 0x79,
94 }
95 arMagic = []byte{0x21, 0x3C, 0x61, 0x72, 0x63, 0x68, 0x3E}
96 zstdMagic = []byte{0x28, 0xB5, 0x2F, 0xFD}
97 lzMagic = []byte{0x4C, 0x5A, 0x49, 0x50}
98 )
99
100 func bytePrefixMatcher(magicPattern []byte) Matcher {
101 return func(data []byte) bool {
102 return compareBytes(data, magicPattern, 0)
103 }
104 }
105
106 func Zip(buf []byte) bool {
107 return len(buf) > 3 &&
108 buf[0] == 0x50 && buf[1] == 0x4B &&
109 (buf[2] == 0x3 || buf[2] == 0x5 || buf[2] == 0x7) &&
110 (buf[3] == 0x4 || buf[3] == 0x6 || buf[3] == 0x8)
111 }
112
113 func Tar(buf []byte) bool {
114 return len(buf) > 261 &&
115 buf[257] == 0x75 && buf[258] == 0x73 &&
116 buf[259] == 0x74 && buf[260] == 0x61 &&
117 buf[261] == 0x72
118 }
119
120 func Rar(buf []byte) bool {
121 return len(buf) > 6 &&
122 buf[0] == 0x52 && buf[1] == 0x61 && buf[2] == 0x72 &&
123 buf[3] == 0x21 && buf[4] == 0x1A && buf[5] == 0x7 &&
124 (buf[6] == 0x0 || buf[6] == 0x1)
125 }
126
127 func Swf(buf []byte) bool {
128 return len(buf) > 2 &&
129 (buf[0] == 0x43 || buf[0] == 0x46) &&
130 buf[1] == 0x57 && buf[2] == 0x53
131 }
132
133 func Cab(buf []byte) bool {
134 return len(buf) > 3 &&
135 ((buf[0] == 0x4D && buf[1] == 0x53 && buf[2] == 0x43 && buf[3] == 0x46) ||
136 (buf[0] == 0x49 && buf[1] == 0x53 && buf[2] == 0x63 && buf[3] == 0x28))
137 }
138
139 func Eot(buf []byte) bool {
140 return len(buf) > 35 &&
141 buf[34] == 0x4C && buf[35] == 0x50 &&
142 ((buf[8] == 0x02 && buf[9] == 0x00 &&
143 buf[10] == 0x01) || (buf[8] == 0x01 &&
144 buf[9] == 0x00 && buf[10] == 0x00) ||
145 (buf[8] == 0x02 && buf[9] == 0x00 &&
146 buf[10] == 0x02))
147 }
148
149 func Z(buf []byte) bool {
150 return len(buf) > 1 &&
151 ((buf[0] == 0x1F && buf[1] == 0xA0) ||
152 (buf[0] == 0x1F && buf[1] == 0x9D))
153 }
154
155 func Rpm(buf []byte) bool {
156 return len(buf) > 96 &&
157 buf[0] == 0xED && buf[1] == 0xAB &&
158 buf[2] == 0xEE && buf[3] == 0xDB
159 }
160
161 func Elf(buf []byte) bool {
162 return len(buf) > 52 &&
163 buf[0] == 0x7F && buf[1] == 0x45 &&
164 buf[2] == 0x4C && buf[3] == 0x46
165 }
166
167 func Dcm(buf []byte) bool {
168 return len(buf) > 131 &&
169 buf[128] == 0x44 && buf[129] == 0x49 &&
170 buf[130] == 0x43 && buf[131] == 0x4D
171 }
172
173 func Iso(buf []byte) bool {
174 return len(buf) > 32773 &&
175 buf[32769] == 0x43 && buf[32770] == 0x44 &&
176 buf[32771] == 0x30 && buf[32772] == 0x30 &&
177 buf[32773] == 0x31
178 }
179
180 func MachO(buf []byte) bool {
181 return len(buf) > 3 && ((buf[0] == 0xFE && buf[1] == 0xED && buf[2] == 0xFA && buf[3] == 0xCF) ||
182 (buf[0] == 0xFE && buf[1] == 0xED && buf[2] == 0xFA && buf[3] == 0xCE) ||
183 (buf[0] == 0xBE && buf[1] == 0xBA && buf[2] == 0xFE && buf[3] == 0xCA) ||
184 // Big endian versions below here...
185 (buf[0] == 0xCF && buf[1] == 0xFA && buf[2] == 0xED && buf[3] == 0xFE) ||
186 (buf[0] == 0xCE && buf[1] == 0xFA && buf[2] == 0xED && buf[3] == 0xFE) ||
187 (buf[0] == 0xCA && buf[1] == 0xFE && buf[2] == 0xBA && buf[3] == 0xBE))
188 }
189
190 // Zstandard compressed data is made of one or more frames.
191 // There are two frame formats defined by Zstandard: Zstandard frames and Skippable frames.
192 // See more details from https://tools.ietf.org/id/draft-kucherawy-dispatch-zstd-00.html#rfc.section.2
193 func Zst(buf []byte) bool {
194 if compareBytes(buf, zstdMagic, 0) {
195 return true
196 } else {
197 // skippable frames
198 if len(buf) < 8 {
199 return false
200 }
201 if binary.LittleEndian.Uint32(buf[:4]) & ZstdMagicSkippableMask == ZstdMagicSkippableStart {
202 userDataLength := binary.LittleEndian.Uint32(buf[4:8])
203 if len(buf) < 8 + int(userDataLength) {
204 return false
205 }
206 nextFrame := buf[8+userDataLength:]
207 return Zst(nextFrame)
208 }
209 return false
210 }
211 }