Debian Codegraph @

internal/magic/archive.go

raw symbols SCIP
1 package magic
2
3 import (
4 "bytes"
5 "encoding/binary"
6 )
7
8 // SevenZ matches a 7z archive.
9 func SevenZ(raw []byte, _ uint32) bool {
10 return bytes.HasPrefix(raw, []byte{0x37, 0x7A, 0xBC, 0xAF, 0x27, 0x1C})
11 }
12
13 // Gzip matches gzip files based on http://www.zlib.org/rfc-gzip.html#header-trailer.
14 func Gzip(raw []byte, _ uint32) bool {
15 return bytes.HasPrefix(raw, []byte{0x1f, 0x8b})
16 }
17
18 // Fits matches an Flexible Image Transport System file.
19 func Fits(raw []byte, _ uint32) bool {
20 return bytes.HasPrefix(raw, []byte{
21 0x53, 0x49, 0x4D, 0x50, 0x4C, 0x45, 0x20, 0x20, 0x3D, 0x20,
22 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
23 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x54,
24 })
25 }
26
27 // Xar matches an eXtensible ARchive format file.
28 func Xar(raw []byte, _ uint32) bool {
29 return bytes.HasPrefix(raw, []byte{0x78, 0x61, 0x72, 0x21})
30 }
31
32 // Bz2 matches a bzip2 file.
33 func Bz2(raw []byte, _ uint32) bool {
34 return bytes.HasPrefix(raw, []byte{0x42, 0x5A, 0x68})
35 }
36
37 // Ar matches an ar (Unix) archive file.
38 func Ar(raw []byte, _ uint32) bool {
39 return bytes.HasPrefix(raw, []byte{0x21, 0x3C, 0x61, 0x72, 0x63, 0x68, 0x3E})
40 }
41
42 // Deb matches a Debian package file.
43 func Deb(raw []byte, _ uint32) bool {
44 return offset(raw, []byte{
45 0x64, 0x65, 0x62, 0x69, 0x61, 0x6E, 0x2D,
46 0x62, 0x69, 0x6E, 0x61, 0x72, 0x79,
47 }, 8)
48 }
49
50 // Warc matches a Web ARChive file.
51 func Warc(raw []byte, _ uint32) bool {
52 return bytes.HasPrefix(raw, []byte("WARC/1.0")) ||
53 bytes.HasPrefix(raw, []byte("WARC/1.1"))
54 }
55
56 // Cab matches a Microsoft Cabinet archive file.
57 func Cab(raw []byte, _ uint32) bool {
58 return bytes.HasPrefix(raw, []byte("MSCF\x00\x00\x00\x00"))
59 }
60
61 // Xz matches an xz compressed stream based on https://tukaani.org/xz/xz-file-format.txt.
62 func Xz(raw []byte, _ uint32) bool {
63 return bytes.HasPrefix(raw, []byte{0xFD, 0x37, 0x7A, 0x58, 0x5A, 0x00})
64 }
65
66 // Lzip matches an Lzip compressed file.
67 func Lzip(raw []byte, _ uint32) bool {
68 return bytes.HasPrefix(raw, []byte{0x4c, 0x5a, 0x49, 0x50})
69 }
70
71 // RPM matches an RPM or Delta RPM package file.
72 func RPM(raw []byte, _ uint32) bool {
73 return bytes.HasPrefix(raw, []byte{0xed, 0xab, 0xee, 0xdb}) ||
74 bytes.HasPrefix(raw, []byte("drpm"))
75 }
76
77 // RAR matches a RAR archive file.
78 func RAR(raw []byte, _ uint32) bool {
79 return bytes.HasPrefix(raw, []byte("Rar!\x1A\x07\x00")) ||
80 bytes.HasPrefix(raw, []byte("Rar!\x1A\x07\x01\x00"))
81 }
82
83 // InstallShieldCab matches an InstallShield Cabinet archive file.
84 func InstallShieldCab(raw []byte, _ uint32) bool {
85 return len(raw) > 7 &&
86 bytes.Equal(raw[0:4], []byte("ISc(")) &&
87 raw[6] == 0 &&
88 (raw[7] == 1 || raw[7] == 2 || raw[7] == 4)
89 }
90
91 // Zstd matches a Zstandard archive file.
92 // https://github.com/facebook/zstd/blob/dev/doc/zstd_compression_format.md
93 func Zstd(raw []byte, limit uint32) bool {
94 if len(raw) < 4 {
95 return false
96 }
97 sig := binary.LittleEndian.Uint32(raw)
98 // Check for Zstandard frames and skippable frames.
99 return (sig >= 0xFD2FB522 && sig <= 0xFD2FB528) ||
100 (sig >= 0x184D2A50 && sig <= 0x184D2A5F)
101 }
102
103 // CRX matches a Chrome extension file: a zip archive prepended by a package header.
104 func CRX(raw []byte, limit uint32) bool {
105 const minHeaderLen = 16
106 if len(raw) < minHeaderLen || !bytes.HasPrefix(raw, []byte("Cr24")) {
107 return false
108 }
109 pubkeyLen := int64(binary.LittleEndian.Uint32(raw[8:12]))
110 sigLen := int64(binary.LittleEndian.Uint32(raw[12:16]))
111 zipOffset := minHeaderLen + pubkeyLen + sigLen
112 if zipOffset < 0 || int64(len(raw)) < zipOffset {
113 return false
114 }
115 return Zip(raw[zipOffset:], limit)
116 }
117
118 // Cpio matches a cpio archive file.
119 func Cpio(raw []byte, _ uint32) bool {
120 if len(raw) < 6 {
121 return false
122 }
123 return binary.LittleEndian.Uint16(raw) == 070707 || // binary cpio
124 bytes.HasPrefix(raw, []byte("070707")) || // portable ASCII cpios
125 bytes.HasPrefix(raw, []byte("070701")) ||
126 bytes.HasPrefix(raw, []byte("070702"))
127 }
128
129 // Tar matches a (t)ape (ar)chive file.
130 // Tar files are divided into 512 bytes records. First record contains a 257
131 // bytes header padded with NUL.
132 func Tar(raw []byte, _ uint32) bool {
133 const sizeRecord = 512
134
135 // The structure of a tar header:
136 // type TarHeader struct {
137 // Name [100]byte
138 // Mode [8]byte
139 // Uid [8]byte
140 // Gid [8]byte
141 // Size [12]byte
142 // Mtime [12]byte
143 // Chksum [8]byte
144 // Linkflag byte
145 // Linkname [100]byte
146 // Magic [8]byte
147 // Uname [32]byte
148 // Gname [32]byte
149 // Devmajor [8]byte
150 // Devminor [8]byte
151 // }
152
153 if len(raw) < sizeRecord {
154 return false
155 }
156 raw = raw[:sizeRecord]
157
158 // First 100 bytes of the header represent the file name.
159 // Check if file looks like Gentoo GLEP binary package.
160 if bytes.Contains(raw[:100], []byte("/gpkg-1\x00")) {
161 return false
162 }
163
164 // Get the checksum recorded into the file.
165 recsum := tarParseOctal(raw[148:156])
166 if recsum == -1 {
167 return false
168 }
169 sum1, sum2 := tarChksum(raw)
170 return recsum == sum1 || recsum == sum2
171 }
172
173 // tarParseOctal converts octal string to decimal int.
174 func tarParseOctal(b []byte) int64 {
175 // Because unused fields are filled with NULs, we need to skip leading NULs.
176 // Fields may also be padded with spaces or NULs.
177 // So we remove leading and trailing NULs and spaces to be sure.
178 b = bytes.Trim(b, " \x00")
179
180 if len(b) == 0 {
181 return -1
182 }
183 ret := int64(0)
184 for _, b := range b {
185 if b == 0 {
186 break
187 }
188 if b < '0' || b > '7' {
189 return -1
190 }
191 ret = (ret << 3) | int64(b-'0')
192 }
193 return ret
194 }
195
196 // tarChksum computes the checksum for the header block b.
197 // The actual checksum is written to same b block after it has been calculated.
198 // Before calculation the bytes from b reserved for checksum have placeholder
199 // value of ASCII space 0x20.
200 // POSIX specifies a sum of the unsigned byte values, but the Sun tar used
201 // signed byte values. We compute and return both.
202 func tarChksum(b []byte) (unsigned, signed int64) {
203 for i, c := range b {
204 if 148 <= i && i < 156 {
205 c = ' ' // Treat the checksum field itself as all spaces.
206 }
207 unsigned += int64(c)
208 signed += int64(int8(c))
209 }
210 return unsigned, signed
211 }
212
213 // Zlib matches zlib compressed files.
214 func Zlib(raw []byte, _ uint32) bool {
215 // https://www.ietf.org/rfc/rfc6713.txt
216 // This check has one fault: ASCII code can satisfy it; for ex: []byte("x ")
217 zlib := len(raw) > 1 &&
218 raw[0] == 'x' && binary.BigEndian.Uint16(raw)%31 == 0
219 // Check that the file is not a regular text to avoid false positives.
220 return zlib && !Text(raw, 0)
221 }