1 .\" $NetBSD: magic.5,v 1.3 2009/05/08 20:19:39 wiz Exp $
3 .\" $File: magic.man,v 1.59 2008/11/06 23:22:53 christos Exp $
7 .\" install as magic.4 on USG, magic.5 on V7, Berkeley and Linux systems.
10 .Nd file command's magic pattern file
12 This manual page documents the format of the magic file as
15 command, version 5.03.
18 command identifies the type of a file using,
20 a test for whether the file contains certain
21 .Dq "magic patterns" .
23 .Pa /usr/share/misc/magic
24 specifies what patterns are to be tested for, what message or
25 MIME type to print if a particular pattern is found,
26 and additional information to extract from the file.
28 Each line of the file specifies a test to be performed.
29 A test compares the data starting at a particular offset
30 in the file with a byte value, a string or a numeric value.
31 If the test succeeds, a message is printed.
32 The line consists of the following fields:
33 .Bl -tag -width ".Dv message"
35 A number specifying the offset, in bytes, into the file of the data
36 which is to be tested.
38 The type of the data to be tested.
39 The possible values are:
40 .Bl -tag -width ".Dv lestring16"
44 A two-byte value in this machine's native byte order.
46 A four-byte value in this machine's native byte order.
48 An eight-byte value in this machine's native byte order.
50 A 32-bit single precision IEEE floating point number in this machine's native byte order.
52 A 64-bit double precision IEEE floating point number in this machine's native byte order.
55 The string type specification can be optionally followed
59 flag compacts whitespace in the target, which must
60 contain at least one whitespace character.
63 consecutive blanks, the target needs at least
65 consecutive blanks to match.
68 flag treats every blank in the target as an optional blank.
71 flag, specifies case insensitive matching: lowercase
72 characters in the magic match both lower and upper case characters in the
73 target, whereas upper case characters in the magic only match uppercase
74 characters in the target.
76 A Pascal-style string where the first byte is interpreted as the an
78 The string is not NUL terminated.
80 A four-byte value interpreted as a UNIX date.
82 A eight-byte value interpreted as a UNIX date.
84 A four-byte value interpreted as a UNIX-style date, but interpreted as
85 local time rather than UTC.
87 An eight-byte value interpreted as a UNIX-style date, but interpreted as
88 local time rather than UTC.
90 A 32-bit ID3 length in big-endian byte order.
92 A two-byte value in big-endian byte order.
94 A four-byte value in big-endian byte order.
96 An eight-byte value in big-endian byte order.
98 A 32-bit single precision IEEE floating point number in big-endian byte order.
100 A 64-bit double precision IEEE floating point number in big-endian byte order.
102 A four-byte value in big-endian byte order,
103 interpreted as a Unix date.
105 An eight-byte value in big-endian byte order,
106 interpreted as a Unix date.
108 A four-byte value in big-endian byte order,
109 interpreted as a UNIX-style date, but interpreted as local time rather
112 An eight-byte value in big-endian byte order,
113 interpreted as a UNIX-style date, but interpreted as local time rather
116 A two-byte unicode (UCS16) string in big-endian byte order.
118 A 32-bit ID3 length in little-endian byte order.
120 A two-byte value in little-endian byte order.
122 A four-byte value in little-endian byte order.
124 An eight-byte value in little-endian byte order.
126 A 32-bit single precision IEEE floating point number in little-endian byte order.
128 A 64-bit double precision IEEE floating point number in little-endian byte order.
130 A four-byte value in little-endian byte order,
131 interpreted as a UNIX date.
133 An eight-byte value in little-endian byte order,
134 interpreted as a UNIX date.
136 A four-byte value in little-endian byte order,
137 interpreted as a UNIX-style date, but interpreted as local time rather
140 An eight-byte value in little-endian byte order,
141 interpreted as a UNIX-style date, but interpreted as local time rather
144 A two-byte unicode (UCS16) string in little-endian byte order.
146 A four-byte value in middle-endian (PDP-11) byte order.
148 A four-byte value in middle-endian (PDP-11) byte order,
149 interpreted as a UNIX date.
151 A four-byte value in middle-endian (PDP-11) byte order,
152 interpreted as a UNIX-style date, but interpreted as local time rather
155 Starting at the given offset, consult the magic database again.
157 A regular expression match in extended POSIX regular expression syntax
159 Regular expressions can take exponential time to process, and their
160 performance is hard to predict, so their use is discouraged.
161 When used in production environments, their performance
162 should be carefully checked.
163 The type specification can be optionally followed by
167 flag makes the match case insensitive, while the
169 flag update the offset to the start offset of the match, rather than the end.
170 The regular expression is tested against line
175 Line endings are assumed to be in the machine's native format.
179 match the beginning and end of individual lines, respectively,
180 not beginning and end of file.
182 A literal string search starting at the given offset.
183 The same modifier flags can be used as for string patterns.
184 The modifier flags (if any) must be followed by
186 the range, that is, the number of positions at which the match will be
187 attempted, starting from the start offset.
189 searching larger binary expressions with variable offsets, using
191 escapes for special characters.
192 The offset works as for regex.
194 This is intended to be used with the test
196 (which is always true) and a message that is to be used if there are
200 Each top-level magic pattern (see below for an explanation of levels)
201 is classified as text or binary according to the types used.
206 are classified as text tests, unless non-printable characters are used
208 All other tests are classified as binary.
210 pattern is considered to be a test text when all its patterns are text
211 patterns; otherwise, it is considered to be a binary pattern.
213 matching a file, binary patterns are tried first; if no match is
214 found, and the file looks like text, then its encoding is determined
215 and the text patterns are tried.
217 The numeric types may optionally be followed by
220 to specify that the value is to be AND'ed with the
221 numeric value before any comparisons are done.
224 to the type indicates that ordered comparisons should be unsigned.
226 The value to be compared with the value from the file.
229 is specified in C form; if it is a string, it is specified as a C string
230 with the usual escapes permitted (e.g. \en for new-line).
233 may be preceded by a character indicating the operation to be performed.
236 to specify that the value from the file must equal the specified value,
238 to specify that the value from the file must be less than the specified
241 to specify that the value from the file must be greater than the specified
244 to specify that the value from the file must have set all of the bits
245 that are set in the specified value,
247 to specify that the value from the file must have clear any of the bits
248 that are set in the specified value, or
250 the value specified after is negated before tested.
252 to specify that any value will match.
253 If the character is omitted, it is assumed to be
260 don't work with floats and doubles.
263 specifies that the line matches if the test does
267 Numeric values are specified in C form; e.g.
275 For string values, the string from the
276 file must match the specified string.
284 can be applied to strings.
285 The length used for matching is that of the string argument
287 This means that a line can match any non-empty string (usually used to
288 then print the string), with
290 (because all non-empty strings are greater than the empty string).
294 always evaluates to true.
296 The message to be printed if the comparison succeeds.
297 If the string contains a
299 format specification, the value from the file (with any specified masking
300 performed) is printed using the message as the format string.
301 If the string begins with
303 the message printed is the remainder of the string with no whitespace
304 added before it: multiple matches are normally separated by a single
308 An APPLE 4+4 character APPLE creator and type can be specified as:
309 .Bd -literal -offset indent
313 A MIME type is given on a separate line, which must be the next
314 non-blank or comment line after the magic line that identifies the
315 file type, and has the following format:
316 .Bd -literal -offset indent
320 i.e. the literal string
322 followed by the MIME type.
324 An optional strength can be supplied on a separate line which refers to
325 the current magic description using the following format:
326 .Bd -literal -offset indent
340 is a constant between 0 and 255.
341 This constant is applied using the specified operand
342 to the currently computed default magic strength.
344 Some file formats contain additional information which is to be printed
345 along with the file type or need additional tests to determine the true
347 These additional tests are introduced by one or more
349 characters preceding the offset.
352 on the line indicates the level of the test; a line with no
354 at the beginning is considered to be at level 0.
355 Tests are arranged in a tree-like hierarchy:
356 If a the test on a line at level
358 succeeds, all following tests at level
360 are performed, and the messages printed if the tests succeed, untile a line
364 For more complex files, one can use empty messages to get just the
365 "if/then" effect, in the following way:
366 .Bd -literal -offset indent
368 \*[Gt]0x18 leshort \*[Lt]0x40 MS-DOS executable
369 \*[Gt]0x18 leshort \*[Gt]0x3f extended PC executable (e.g., MS Windows)
372 Offsets do not need to be constant, but can also be read from the file
374 If the first character following the last
378 then the string after the parenthesis is interpreted as an indirect offset.
379 That means that the number after the parenthesis is used as an offset in
381 The value at that offset is read, and is used again as an offset
383 Indirect offsets are of the form:
384 .Em (( x [.[bislBISL]][+\-][ y ]) .
387 is used as an offset in the file.
388 A byte, id3 length, short or long is read at that offset depending on the
391 The capitalized types interpret the number as a big endian
392 value, whereas the small letter versions interpret the number as a little
396 type interprets the number as a middle endian (PDP-11) value.
397 To that number the value of
399 is added and the result is used as an offset in the file.
400 The default type if one is not specified is long.
402 That way variable length structures can be examined:
403 .Bd -literal -offset indent
404 # MS Windows executables are also valid MS-DOS executables
406 \*[Gt]0x18 leshort \*[Lt]0x40 MZ executable (MS-DOS)
407 # skip the whole block below if it is not an extended executable
408 \*[Gt]0x18 leshort \*[Gt]0x3f
409 \*[Gt]\*[Gt](0x3c.l) string PE\e0\e0 PE executable (MS-Windows)
410 \*[Gt]\*[Gt](0x3c.l) string LX\e0\e0 LX executable (OS/2)
413 This strategy of examining has a drawback: You must make sure that
414 you eventually print something, or users may get empty output (like, when
415 there is neither PE\e0\e0 nor LE\e0\e0 in the above example)
417 If this indirect offset cannot be used directly, simple calculations are
419 .Em [+-*/%\*[Am]|^]number
420 inside parentheses allows one to modify
421 the value read from the file before it is used as an offset:
422 .Bd -literal -offset indent
423 # MS Windows executables are also valid MS-DOS executables
425 # sometimes, the value at 0x18 is less that 0x40 but there's still an
426 # extended executable, simply appended to the file
427 \*[Gt]0x18 leshort \*[Lt]0x40
428 \*[Gt]\*[Gt](4.s*512) leshort 0x014c COFF executable (MS-DOS, DJGPP)
429 \*[Gt]\*[Gt](4.s*512) leshort !0x014c MZ executable (MS-DOS)
432 Sometimes you do not know the exact offset as this depends on the length or
433 position (when indirection was used before) of preceding fields.
434 You can specify an offset relative to the end of the last up-level
437 as a prefix to the offset:
438 .Bd -literal -offset indent
440 \*[Gt]0x18 leshort \*[Gt]0x3f
441 \*[Gt]\*[Gt](0x3c.l) string PE\e0\e0 PE executable (MS-Windows)
442 # immediately following the PE signature is the CPU type
443 \*[Gt]\*[Gt]\*[Gt]\*[Am]0 leshort 0x14c for Intel 80386
444 \*[Gt]\*[Gt]\*[Gt]\*[Am]0 leshort 0x184 for DEC Alpha
447 Indirect and relative offsets can be combined:
448 .Bd -literal -offset indent
450 \*[Gt]0x18 leshort \*[Lt]0x40
451 \*[Gt]\*[Gt](4.s*512) leshort !0x014c MZ executable (MS-DOS)
452 # if it's not COFF, go back 512 bytes and add the offset taken
453 # from byte 2/3, which is yet another way of finding the start
454 # of the extended executable
455 \*[Gt]\*[Gt]\*[Gt]\*[Am](2.s-514) string LE LE executable (MS Windows VxD driver)
458 Or the other way around:
459 .Bd -literal -offset indent
461 \*[Gt]0x18 leshort \*[Gt]0x3f
462 \*[Gt]\*[Gt](0x3c.l) string LE\e0\e0 LE executable (MS-Windows)
463 # at offset 0x80 (-4, since relative offsets start at the end
464 # of the up-level match) inside the LE header, we find the absolute
465 # offset to the code area, where we look for a specific signature
466 \*[Gt]\*[Gt]\*[Gt](\*[Am]0x7c.l+0x26) string UPX \eb, UPX compressed
470 .Bd -literal -offset indent
472 \*[Gt]0x18 leshort \*[Gt]0x3f
473 \*[Gt]\*[Gt](0x3c.l) string LE\e0\e0 LE executable (MS-Windows)
474 # at offset 0x58 inside the LE header, we find the relative offset
475 # to a data area where we look for a specific signature
476 \*[Gt]\*[Gt]\*[Gt]\*[Am](\*[Am]0x54.l-3) string UNACE \eb, ACE self-extracting archive
479 Finally, if you have to deal with offset/length pairs in your file, even the
480 second value in a parenthesized expression can be taken from the file itself,
481 using another set of parentheses.
482 Note that this additional indirect offset is always relative to the
483 start of the main indirect offset.
484 .Bd -literal -offset indent
486 \*[Gt]0x18 leshort \*[Gt]0x3f
487 \*[Gt]\*[Gt](0x3c.l) string PE\e0\e0 PE executable (MS-Windows)
488 # search for the PE section called ".idata"...
489 \*[Gt]\*[Gt]\*[Gt]\*[Am]0xf4 search/0x140 .idata
490 # ...and go to the end of it, calculated from start+length;
491 # these are located 14 and 10 bytes after the section name
492 \*[Gt]\*[Gt]\*[Gt]\*[Gt](\*[Am]0xe.l+(-4)) string PK\e3\e4 \eb, ZIP self-extracting archive
496 \- the command that reads this file.
514 are system-dependent; perhaps they should be specified as a number
515 of bytes (2B, 4B, etc),
516 since the files being recognized typically come from
517 a system on which the lengths are invariant.
519 .\" From: guy@sun.uucp (Guy Harris)
520 .\" Newsgroups: net.bugs.usg
521 .\" Subject: /etc/magic's format isn't well documented
522 .\" Message-ID: <2752@sun.uucp>
523 .\" Date: 3 Sep 85 08:19:07 GMT
524 .\" Organization: Sun Microsystems, Inc.
527 .\" Here's a manual page for the format accepted by the "file" made by adding
528 .\" the changes I posted to the S5R2 version.
530 .\" Modified for Ian Darwin's version of the file command.