1 .\" $File: magic.man,v 1.71 2011/12/07 11:58:24 rrt Exp $
5 .\" install as magic.4 on USG, magic.5 on V7, Berkeley and Linux systems.
8 .Nd file command's magic pattern file
10 This manual page documents the format of the magic file as
13 command, version __VERSION__.
16 command identifies the type of a file using,
18 a test for whether the file contains certain
19 .Dq "magic patterns" .
22 specifies what patterns are to be tested for, what message or
23 MIME type to print if a particular pattern is found,
24 and additional information to extract from the file.
26 Each line of the file specifies a test to be performed.
27 A test compares the data starting at a particular offset
28 in the file with a byte value, a string or a numeric value.
29 If the test succeeds, a message is printed.
30 The line consists of the following fields:
31 .Bl -tag -width ".Dv message"
33 A number specifying the offset, in bytes, into the file of the data
34 which is to be tested.
36 The type of the data to be tested.
37 The possible values are:
38 .Bl -tag -width ".Dv lestring16"
42 A two-byte value in this machine's native byte order.
44 A four-byte value in this machine's native byte order.
46 An eight-byte value in this machine's native byte order.
48 A 32-bit single precision IEEE floating point number in this machine's native byte order.
50 A 64-bit double precision IEEE floating point number in this machine's native byte order.
53 The string type specification can be optionally followed
57 flag compacts whitespace in the target, which must
58 contain at least one whitespace character.
61 consecutive blanks, the target needs at least
63 consecutive blanks to match.
66 flag treats every blank in the magic as an optional blank.
69 flag specifies case insensitive matching: lower case
70 characters in the magic match both lower and upper case characters in the
71 target, whereas upper case characters in the magic only match upper case
72 characters in the target.
75 flag specifies case insensitive matching: upper case
76 characters in the magic match both lower and upper case characters in the
77 target, whereas lower case characters in the magic only match upper case
78 characters in the target.
79 To do a complete case insensitive match, specify both
85 flag forces the test to be done for text files, while the
87 flag forces the test to be done for binary files.
89 A Pascal-style string where the first byte/short/int is interpreted as the an
91 The length defaults to byte and can be specified as a modifier.
92 The following modifiers are supported:
93 .Bl -tag -compact -width B
95 A byte length (default).
97 A 2 byte big endian length.
99 A 2 byte big little length.
101 A 4 byte big endian length.
103 A 4 byte big little length.
105 The length includes itself in its count.
107 The string is not NUL terminated.
109 is used rather than the more
112 because this type of length is a feature of the JPEG
115 A four-byte value interpreted as a UNIX date.
117 A eight-byte value interpreted as a UNIX date.
119 A four-byte value interpreted as a UNIX-style date, but interpreted as
120 local time rather than UTC.
122 An eight-byte value interpreted as a UNIX-style date, but interpreted as
123 local time rather than UTC.
125 A 32-bit ID3 length in big-endian byte order.
127 A two-byte value in big-endian byte order.
129 A four-byte value in big-endian byte order.
131 An eight-byte value in big-endian byte order.
133 A 32-bit single precision IEEE floating point number in big-endian byte order.
135 A 64-bit double precision IEEE floating point number in big-endian byte order.
137 A four-byte value in big-endian byte order,
138 interpreted as a Unix date.
140 An eight-byte value in big-endian byte order,
141 interpreted as a Unix date.
143 A four-byte value in big-endian byte order,
144 interpreted as a UNIX-style date, but interpreted as local time rather
147 An eight-byte value in big-endian byte order,
148 interpreted as a UNIX-style date, but interpreted as local time rather
151 A two-byte unicode (UCS16) string in big-endian byte order.
153 A 32-bit ID3 length in little-endian byte order.
155 A two-byte value in little-endian byte order.
157 A four-byte value in little-endian byte order.
159 An eight-byte value in little-endian byte order.
161 A 32-bit single precision IEEE floating point number in little-endian byte order.
163 A 64-bit double precision IEEE floating point number in little-endian byte order.
165 A four-byte value in little-endian byte order,
166 interpreted as a UNIX date.
168 An eight-byte value in little-endian byte order,
169 interpreted as a UNIX date.
171 A four-byte value in little-endian byte order,
172 interpreted as a UNIX-style date, but interpreted as local time rather
175 An eight-byte value in little-endian byte order,
176 interpreted as a UNIX-style date, but interpreted as local time rather
179 A two-byte unicode (UCS16) string in little-endian byte order.
181 A four-byte value in middle-endian (PDP-11) byte order.
183 A four-byte value in middle-endian (PDP-11) byte order,
184 interpreted as a UNIX date.
186 A four-byte value in middle-endian (PDP-11) byte order,
187 interpreted as a UNIX-style date, but interpreted as local time rather
190 Starting at the given offset, consult the magic database again.
192 A regular expression match in extended POSIX regular expression syntax
194 Regular expressions can take exponential time to process, and their
195 performance is hard to predict, so their use is discouraged.
196 When used in production environments, their performance
197 should be carefully checked.
198 The type specification can be optionally followed by
202 flag makes the match case insensitive, while the
204 flag update the offset to the start offset of the match, rather than the end.
205 The regular expression is tested against line
210 Line endings are assumed to be in the machine's native format.
214 match the beginning and end of individual lines, respectively,
215 not beginning and end of file.
217 A literal string search starting at the given offset.
218 The same modifier flags can be used as for string patterns.
219 The modifier flags (if any) must be followed by
221 the range, that is, the number of positions at which the match will be
222 attempted, starting from the start offset.
224 searching larger binary expressions with variable offsets, using
226 escapes for special characters.
227 The offset works as for regex.
229 This is intended to be used with the test
231 (which is always true) and a message that is to be used if there are
235 Each top-level magic pattern (see below for an explanation of levels)
236 is classified as text or binary according to the types used.
241 are classified as text tests, unless non-printable characters are used
243 All other tests are classified as binary.
245 pattern is considered to be a test text when all its patterns are text
246 patterns; otherwise, it is considered to be a binary pattern.
248 matching a file, binary patterns are tried first; if no match is
249 found, and the file looks like text, then its encoding is determined
250 and the text patterns are tried.
252 The numeric types may optionally be followed by
255 to specify that the value is to be AND'ed with the
256 numeric value before any comparisons are done.
259 to the type indicates that ordered comparisons should be unsigned.
261 The value to be compared with the value from the file.
264 is specified in C form; if it is a string, it is specified as a C string
265 with the usual escapes permitted (e.g. \en for new-line).
268 may be preceded by a character indicating the operation to be performed.
271 to specify that the value from the file must equal the specified value,
273 to specify that the value from the file must be less than the specified
276 to specify that the value from the file must be greater than the specified
279 to specify that the value from the file must have set all of the bits
280 that are set in the specified value,
282 to specify that the value from the file must have clear any of the bits
283 that are set in the specified value, or
285 the value specified after is negated before tested.
287 to specify that any value will match.
288 If the character is omitted, it is assumed to be
295 don't work with floats and doubles.
298 specifies that the line matches if the test does
302 Numeric values are specified in C form; e.g.
310 For string values, the string from the
311 file must match the specified string.
319 can be applied to strings.
320 The length used for matching is that of the string argument
322 This means that a line can match any non-empty string (usually used to
323 then print the string), with
325 (because all non-empty strings are greater than the empty string).
329 always evaluates to true.
331 The message to be printed if the comparison succeeds.
332 If the string contains a
334 format specification, the value from the file (with any specified masking
335 performed) is printed using the message as the format string.
336 If the string begins with
338 the message printed is the remainder of the string with no whitespace
339 added before it: multiple matches are normally separated by a single
343 An APPLE 4+4 character APPLE creator and type can be specified as:
344 .Bd -literal -offset indent
348 A MIME type is given on a separate line, which must be the next
349 non-blank or comment line after the magic line that identifies the
350 file type, and has the following format:
351 .Bd -literal -offset indent
355 i.e. the literal string
357 followed by the MIME type.
359 An optional strength can be supplied on a separate line which refers to
360 the current magic description using the following format:
361 .Bd -literal -offset indent
375 is a constant between 0 and 255.
376 This constant is applied using the specified operand
377 to the currently computed default magic strength.
379 Some file formats contain additional information which is to be printed
380 along with the file type or need additional tests to determine the true
382 These additional tests are introduced by one or more
384 characters preceding the offset.
387 on the line indicates the level of the test; a line with no
389 at the beginning is considered to be at level 0.
390 Tests are arranged in a tree-like hierarchy:
391 if the test on a line at level
393 succeeds, all following tests at level
395 are performed, and the messages printed if the tests succeed, until a line
399 For more complex files, one can use empty messages to get just the
400 "if/then" effect, in the following way:
401 .Bd -literal -offset indent
403 \*[Gt]0x18 leshort \*[Lt]0x40 MS-DOS executable
404 \*[Gt]0x18 leshort \*[Gt]0x3f extended PC executable (e.g., MS Windows)
407 Offsets do not need to be constant, but can also be read from the file
409 If the first character following the last
413 then the string after the parenthesis is interpreted as an indirect offset.
414 That means that the number after the parenthesis is used as an offset in
416 The value at that offset is read, and is used again as an offset
418 Indirect offsets are of the form:
419 .Em (( x [.[bislBISL]][+\-][ y ]) .
422 is used as an offset in the file.
423 A byte, id3 length, short or long is read at that offset depending on the
426 The capitalized types interpret the number as a big endian
427 value, whereas the small letter versions interpret the number as a little
431 type interprets the number as a middle endian (PDP-11) value.
432 To that number the value of
434 is added and the result is used as an offset in the file.
435 The default type if one is not specified is long.
437 That way variable length structures can be examined:
438 .Bd -literal -offset indent
439 # MS Windows executables are also valid MS-DOS executables
441 \*[Gt]0x18 leshort \*[Lt]0x40 MZ executable (MS-DOS)
442 # skip the whole block below if it is not an extended executable
443 \*[Gt]0x18 leshort \*[Gt]0x3f
444 \*[Gt]\*[Gt](0x3c.l) string PE\e0\e0 PE executable (MS-Windows)
445 \*[Gt]\*[Gt](0x3c.l) string LX\e0\e0 LX executable (OS/2)
448 This strategy of examining has a drawback: You must make sure that
449 you eventually print something, or users may get empty output (like, when
450 there is neither PE\e0\e0 nor LE\e0\e0 in the above example)
452 If this indirect offset cannot be used directly, simple calculations are
454 .Em [+-*/%\*[Am]|^]number
455 inside parentheses allows one to modify
456 the value read from the file before it is used as an offset:
457 .Bd -literal -offset indent
458 # MS Windows executables are also valid MS-DOS executables
460 # sometimes, the value at 0x18 is less that 0x40 but there's still an
461 # extended executable, simply appended to the file
462 \*[Gt]0x18 leshort \*[Lt]0x40
463 \*[Gt]\*[Gt](4.s*512) leshort 0x014c COFF executable (MS-DOS, DJGPP)
464 \*[Gt]\*[Gt](4.s*512) leshort !0x014c MZ executable (MS-DOS)
467 Sometimes you do not know the exact offset as this depends on the length or
468 position (when indirection was used before) of preceding fields.
469 You can specify an offset relative to the end of the last up-level
472 as a prefix to the offset:
473 .Bd -literal -offset indent
475 \*[Gt]0x18 leshort \*[Gt]0x3f
476 \*[Gt]\*[Gt](0x3c.l) string PE\e0\e0 PE executable (MS-Windows)
477 # immediately following the PE signature is the CPU type
478 \*[Gt]\*[Gt]\*[Gt]\*[Am]0 leshort 0x14c for Intel 80386
479 \*[Gt]\*[Gt]\*[Gt]\*[Am]0 leshort 0x184 for DEC Alpha
482 Indirect and relative offsets can be combined:
483 .Bd -literal -offset indent
485 \*[Gt]0x18 leshort \*[Lt]0x40
486 \*[Gt]\*[Gt](4.s*512) leshort !0x014c MZ executable (MS-DOS)
487 # if it's not COFF, go back 512 bytes and add the offset taken
488 # from byte 2/3, which is yet another way of finding the start
489 # of the extended executable
490 \*[Gt]\*[Gt]\*[Gt]\*[Am](2.s-514) string LE LE executable (MS Windows VxD driver)
493 Or the other way around:
494 .Bd -literal -offset indent
496 \*[Gt]0x18 leshort \*[Gt]0x3f
497 \*[Gt]\*[Gt](0x3c.l) string LE\e0\e0 LE executable (MS-Windows)
498 # at offset 0x80 (-4, since relative offsets start at the end
499 # of the up-level match) inside the LE header, we find the absolute
500 # offset to the code area, where we look for a specific signature
501 \*[Gt]\*[Gt]\*[Gt](\*[Am]0x7c.l+0x26) string UPX \eb, UPX compressed
505 .Bd -literal -offset indent
507 \*[Gt]0x18 leshort \*[Gt]0x3f
508 \*[Gt]\*[Gt](0x3c.l) string LE\e0\e0 LE executable (MS-Windows)
509 # at offset 0x58 inside the LE header, we find the relative offset
510 # to a data area where we look for a specific signature
511 \*[Gt]\*[Gt]\*[Gt]\*[Am](\*[Am]0x54.l-3) string UNACE \eb, ACE self-extracting archive
514 Finally, if you have to deal with offset/length pairs in your file, even the
515 second value in a parenthesized expression can be taken from the file itself,
516 using another set of parentheses.
517 Note that this additional indirect offset is always relative to the
518 start of the main indirect offset.
519 .Bd -literal -offset indent
521 \*[Gt]0x18 leshort \*[Gt]0x3f
522 \*[Gt]\*[Gt](0x3c.l) string PE\e0\e0 PE executable (MS-Windows)
523 # search for the PE section called ".idata"...
524 \*[Gt]\*[Gt]\*[Gt]\*[Am]0xf4 search/0x140 .idata
525 # ...and go to the end of it, calculated from start+length;
526 # these are located 14 and 10 bytes after the section name
527 \*[Gt]\*[Gt]\*[Gt]\*[Gt](\*[Am]0xe.l+(-4)) string PK\e3\e4 \eb, ZIP self-extracting archive
530 .Xr file __CSECTION__
531 \- the command that reads this file.
549 are system-dependent; perhaps they should be specified as a number
550 of bytes (2B, 4B, etc),
551 since the files being recognized typically come from
552 a system on which the lengths are invariant.
554 .\" From: guy@sun.uucp (Guy Harris)
555 .\" Newsgroups: net.bugs.usg
556 .\" Subject: /etc/magic's format isn't well documented
557 .\" Message-ID: <2752@sun.uucp>
558 .\" Date: 3 Sep 85 08:19:07 GMT
559 .\" Organization: Sun Microsystems, Inc.
562 .\" Here's a manual page for the format accepted by the "file" made by adding
563 .\" the changes I posted to the S5R2 version.
565 .\" Modified for Ian Darwin's version of the file command.