Debian Codegraph @

internal/magic/binary.go

raw symbols SCIP
1 package magic
2
3 import (
4 "bytes"
5 "debug/macho"
6 "encoding/binary"
7 )
8
9 // Lnk matches Microsoft lnk binary format.
10 func Lnk(raw []byte, _ uint32) bool {
11 return bytes.HasPrefix(raw, []byte{0x4C, 0x00, 0x00, 0x00, 0x01, 0x14, 0x02, 0x00})
12 }
13
14 // Wasm matches a web assembly File Format file.
15 func Wasm(raw []byte, _ uint32) bool {
16 return bytes.HasPrefix(raw, []byte{0x00, 0x61, 0x73, 0x6D})
17 }
18
19 // Exe matches a Windows/DOS executable file.
20 func Exe(raw []byte, _ uint32) bool {
21 return len(raw) > 1 && raw[0] == 0x4D && raw[1] == 0x5A
22 }
23
24 // Elf matches an Executable and Linkable Format file.
25 func Elf(raw []byte, _ uint32) bool {
26 return bytes.HasPrefix(raw, []byte{0x7F, 0x45, 0x4C, 0x46})
27 }
28
29 // Nes matches a Nintendo Entertainment system ROM file.
30 func Nes(raw []byte, _ uint32) bool {
31 return bytes.HasPrefix(raw, []byte{0x4E, 0x45, 0x53, 0x1A})
32 }
33
34 // SWF matches an Adobe Flash swf file.
35 func SWF(raw []byte, _ uint32) bool {
36 return bytes.HasPrefix(raw, []byte("CWS")) ||
37 bytes.HasPrefix(raw, []byte("FWS")) ||
38 bytes.HasPrefix(raw, []byte("ZWS"))
39 }
40
41 // Torrent has bencoded text in the beginning.
42 func Torrent(raw []byte, _ uint32) bool {
43 return bytes.HasPrefix(raw, []byte("d8:announce"))
44 }
45
46 // PAR1 matches a parquet file.
47 func Par1(raw []byte, _ uint32) bool {
48 return bytes.HasPrefix(raw, []byte{0x50, 0x41, 0x52, 0x31})
49 }
50
51 // CBOR matches a Concise Binary Object Representation https://cbor.io/
52 func CBOR(raw []byte, _ uint32) bool {
53 return bytes.HasPrefix(raw, []byte{0xD9, 0xD9, 0xF7})
54 }
55
56 // Java bytecode and Mach-O binaries share the same magic number.
57 // More info here https://github.com/threatstack/libmagic/blob/master/magic/Magdir/cafebabe
58 func classOrMachOFat(in []byte) bool {
59 // There should be at least 8 bytes for both of them because the only way to
60 // quickly distinguish them is by comparing byte at position 7
61 if len(in) < 8 {
62 return false
63 }
64
65 return binary.BigEndian.Uint32(in) == macho.MagicFat
66 }
67
68 // Class matches a java class file.
69 func Class(raw []byte, limit uint32) bool {
70 return classOrMachOFat(raw) && raw[7] > 30
71 }
72
73 // MachO matches Mach-O binaries format.
74 func MachO(raw []byte, limit uint32) bool {
75 if classOrMachOFat(raw) && raw[7] < 0x14 {
76 return true
77 }
78
79 if len(raw) < 4 {
80 return false
81 }
82
83 be := binary.BigEndian.Uint32(raw)
84 le := binary.LittleEndian.Uint32(raw)
85
86 return be == macho.Magic32 ||
87 le == macho.Magic32 ||
88 be == macho.Magic64 ||
89 le == macho.Magic64
90 }
91
92 // Dbf matches a dBase file.
93 // https://www.dbase.com/Knowledgebase/INT/db7_file_fmt.htm
94 func Dbf(raw []byte, limit uint32) bool {
95 if len(raw) < 68 {
96 return false
97 }
98
99 // 3rd and 4th bytes contain the last update month and day of month.
100 if raw[2] == 0 || raw[2] > 12 || raw[3] == 0 || raw[3] > 31 {
101 return false
102 }
103
104 // 12, 13, 30, 31 are reserved bytes and always filled with 0x00.
105 if raw[12] != 0x00 || raw[13] != 0x00 || raw[30] != 0x00 || raw[31] != 0x00 {
106 return false
107 }
108 // Production MDX flag;
109 // 0x01 if a production .MDX file exists for this table;
110 // 0x00 if no .MDX file exists.
111 if raw[28] > 0x01 {
112 return false
113 }
114
115 // dbf type is dictated by the first byte.
116 dbfTypes := []byte{
117 0x02, 0x03, 0x04, 0x05, 0x30, 0x31, 0x32, 0x42, 0x62, 0x7B, 0x82,
118 0x83, 0x87, 0x8A, 0x8B, 0x8E, 0xB3, 0xCB, 0xE5, 0xF5, 0xF4, 0xFB,
119 }
120 for _, b := range dbfTypes {
121 if raw[0] == b {
122 return true
123 }
124 }
125
126 return false
127 }
128
129 // ElfObj matches an object file.
130 func ElfObj(raw []byte, limit uint32) bool {
131 return len(raw) > 17 && ((raw[16] == 0x01 && raw[17] == 0x00) ||
132 (raw[16] == 0x00 && raw[17] == 0x01))
133 }
134
135 // ElfExe matches an executable file.
136 func ElfExe(raw []byte, limit uint32) bool {
137 return len(raw) > 17 && ((raw[16] == 0x02 && raw[17] == 0x00) ||
138 (raw[16] == 0x00 && raw[17] == 0x02))
139 }
140
141 // ElfLib matches a shared library file.
142 func ElfLib(raw []byte, limit uint32) bool {
143 return len(raw) > 17 && ((raw[16] == 0x03 && raw[17] == 0x00) ||
144 (raw[16] == 0x00 && raw[17] == 0x03))
145 }
146
147 // ElfDump matches a core dump file.
148 func ElfDump(raw []byte, limit uint32) bool {
149 return len(raw) > 17 && ((raw[16] == 0x04 && raw[17] == 0x00) ||
150 (raw[16] == 0x00 && raw[17] == 0x04))
151 }
152
153 // Dcm matches a DICOM medical format file.
154 func Dcm(raw []byte, limit uint32) bool {
155 return len(raw) > 131 &&
156 bytes.Equal(raw[128:132], []byte{0x44, 0x49, 0x43, 0x4D})
157 }
158
159 // Marc matches a MARC21 (MAchine-Readable Cataloging) file.
160 func Marc(raw []byte, limit uint32) bool {
161 // File is at least 24 bytes ("leader" field size).
162 if len(raw) < 24 {
163 return false
164 }
165
166 // Fixed bytes at offset 20.
167 if !bytes.Equal(raw[20:24], []byte("4500")) {
168 return false
169 }
170
171 // First 5 bytes are ASCII digits.
172 for i := 0; i < 5; i++ {
173 if raw[i] < '0' || raw[i] > '9' {
174 return false
175 }
176 }
177
178 // Field terminator is present in first 2048 bytes.
179 return bytes.Contains(raw[:min(2048, len(raw))], []byte{0x1E})
180 }
181
182 // GLB matches a glTF model format file.
183 // GLB is the binary file format representation of 3D models saved in
184 // the GL transmission Format (glTF).
185 // GLB uses little endian and its header structure is as follows:
186 //
187 // <-- 12-byte header -->
188 // | magic | version | length |
189 // | (uint32) | (uint32) | (uint32) |
190 // | \x67\x6C\x54\x46 | \x01\x00\x00\x00 | ... |
191 // | g l T F | 1 | ... |
192 //
193 // Visit [glTF specification] and [IANA glTF entry] for more details.
194 //
195 // [glTF specification]: https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html
196 // [IANA glTF entry]: https://www.iana.org/assignments/media-types/model/gltf-binary
197 func GLB(raw []byte, _ uint32) bool {
198 return bytes.HasPrefix(raw, []byte("\x67\x6C\x54\x46\x02\x00\x00\x00")) ||
199 bytes.HasPrefix(raw, []byte("\x67\x6C\x54\x46\x01\x00\x00\x00"))
200 }
201
202 // TzIf matches a Time Zone Information Format (TZif) file.
203 // See more: https://tools.ietf.org/id/draft-murchison-tzdist-tzif-00.html#rfc.section.3
204 // Its header structure is shown below:
205 //
206 // +---------------+---+
207 // | magic (4) | <-+-- version (1)
208 // +---------------+---+---------------------------------------+
209 // | [unused - reserved for future use] (15) |
210 // +---------------+---------------+---------------+-----------+
211 // | isutccnt (4) | isstdcnt (4) | leapcnt (4) |
212 // +---------------+---------------+---------------+
213 // | timecnt (4) | typecnt (4) | charcnt (4) |
214 func TzIf(raw []byte, limit uint32) bool {
215 // File is at least 44 bytes (header size).
216 if len(raw) < 44 {
217 return false
218 }
219
220 if !bytes.HasPrefix(raw, []byte("TZif")) {
221 return false
222 }
223
224 // Field "typecnt" MUST not be zero.
225 if binary.BigEndian.Uint32(raw[36:40]) == 0 {
226 return false
227 }
228
229 // Version has to be NUL (0x00), '2' (0x32) or '3' (0x33).
230 return raw[4] == 0x00 || raw[4] == 0x32 || raw[4] == 0x33
231 }