1 .\" $File: magic.man,v 1.91 2017/02/12 15:30:08 christos 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 magic files 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 is usually located in a binary file in
24 or a directory of source text magic pattern fragment files in
26 The database specifies what patterns are to be tested for, what message or
27 MIME type to print if a particular pattern is found,
28 and additional information to extract from the file.
30 The format of the source fragment files that are used to build this database
32 Each line of a fragment file specifies a test to be performed.
33 A test compares the data starting at a particular offset
34 in the file with a byte value, a string or a numeric value.
35 If the test succeeds, a message is printed.
36 The line consists of the following fields:
37 .Bl -tag -width ".Dv message"
39 A number specifying the offset, in bytes, into the file of the data
40 which is to be tested.
42 The type of the data to be tested.
43 The possible values are:
44 .Bl -tag -width ".Dv lestring16"
48 A two-byte value in this machine's native byte order.
50 A four-byte value in this machine's native byte order.
52 An eight-byte value in this machine's native byte order.
54 A 32-bit single precision IEEE floating point number in this machine's native byte order.
56 A 64-bit double precision IEEE floating point number in this machine's native byte order.
59 The string type specification can be optionally followed
63 flag compacts whitespace in the target, which must
64 contain at least one whitespace character.
67 consecutive blanks, the target needs at least
69 consecutive blanks to match.
72 flag treats every blank in the magic as an optional blank.
75 flag specifies case insensitive matching: lower case
76 characters in the magic match both lower and upper case characters in the
77 target, whereas upper case characters in the magic only match upper case
78 characters in the target.
81 flag specifies case insensitive matching: upper case
82 characters in the magic match both lower and upper case characters in the
83 target, whereas lower case characters in the magic only match upper case
84 characters in the target.
85 To do a complete case insensitive match, specify both
91 flag forces the test to be done for text files, while the
93 flag forces the test to be done for binary files.
96 flag causes the string to be trimmed, i.e. leading and trailing whitespace
97 is deleted before the string is printed.
99 A Pascal-style string where the first byte/short/int is interpreted as the
101 The length defaults to byte and can be specified as a modifier.
102 The following modifiers are supported:
103 .Bl -tag -compact -width B
105 A byte length (default).
107 A 4 byte big endian length.
109 A 2 byte big endian length.
111 A 4 byte little endian length.
113 A 2 byte little endian length.
115 The length includes itself in its count.
117 The string is not NUL terminated.
119 is used rather than the more
122 because this type of length is a feature of the JPEG
125 A four-byte value interpreted as a UNIX date.
127 A eight-byte value interpreted as a UNIX date.
129 A four-byte value interpreted as a UNIX-style date, but interpreted as
130 local time rather than UTC.
132 An eight-byte value interpreted as a UNIX-style date, but interpreted as
133 local time rather than UTC.
135 An eight-byte value interpreted as a Windows-style date.
137 A 32-bit ID3 length in big-endian byte order.
139 A two-byte value in big-endian byte order.
141 A four-byte value in big-endian byte order.
143 An eight-byte value in big-endian byte order.
145 A 32-bit single precision IEEE floating point number in big-endian byte order.
147 A 64-bit double precision IEEE floating point number in big-endian byte order.
149 A four-byte value in big-endian byte order,
150 interpreted as a Unix date.
152 An eight-byte value in big-endian byte order,
153 interpreted as a Unix date.
155 A four-byte value in big-endian byte order,
156 interpreted as a UNIX-style date, but interpreted as local time rather
159 An eight-byte value in big-endian byte order,
160 interpreted as a UNIX-style date, but interpreted as local time rather
163 An eight-byte value in big-endian byte order,
164 interpreted as a Windows-style date.
166 A two-byte unicode (UCS16) string in big-endian byte order.
168 A 32-bit ID3 length in little-endian byte order.
170 A two-byte value in little-endian byte order.
172 A four-byte value in little-endian byte order.
174 An eight-byte value in little-endian byte order.
176 A 32-bit single precision IEEE floating point number in little-endian byte order.
178 A 64-bit double precision IEEE floating point number in little-endian byte order.
180 A four-byte value in little-endian byte order,
181 interpreted as a UNIX date.
183 An eight-byte value in little-endian byte order,
184 interpreted as a UNIX date.
186 A four-byte value in little-endian byte order,
187 interpreted as a UNIX-style date, but interpreted as local time rather
190 An eight-byte value in little-endian byte order,
191 interpreted as a UNIX-style date, but interpreted as local time rather
194 An eight-byte value in little-endian byte order,
195 interpreted as a Windows-style date.
197 A two-byte unicode (UCS16) string in little-endian byte order.
199 A four-byte value in middle-endian (PDP-11) byte order.
201 A four-byte value in middle-endian (PDP-11) byte order,
202 interpreted as a UNIX date.
204 A four-byte value in middle-endian (PDP-11) byte order,
205 interpreted as a UNIX-style date, but interpreted as local time rather
208 Starting at the given offset, consult the magic database again.
211 magic is by default absolute in the file, but one can specify
213 to indicate that the offset is relative from the beginning of the entry.
217 magic instance that can be called from another
219 magic entry, like a subroutine call.
220 Named instance direct magic offsets are relative to the offset of the
221 previous matched entry, but indirect offsets are relative to the beginning
222 of the file as usual.
223 Named magic entries always match.
225 Recursively call the named magic starting from the current offset.
226 If the name of the referenced begins with a
228 then the endianness of the magic is switched; if the magic mentioned
234 This is useful to avoid duplicating the rules for different endianness.
236 A regular expression match in extended POSIX regular expression syntax
238 Regular expressions can take exponential time to process, and their
239 performance is hard to predict, so their use is discouraged.
240 When used in production environments, their performance
241 should be carefully checked.
242 The size of the string to search should also be limited by specifying
244 to avoid performance issues scanning long files.
245 The type specification can also be optionally followed by
249 flag makes the match case insensitive, while the
251 flag update the offset to the start offset of the match, rather than the end.
254 modifier, changes the limit of length to mean number of lines instead of a
256 Lines are delimited by the platforms native line delimiter.
257 When a line count is specified, an implicit byte count also computed assuming
258 each line is 80 characters long.
259 If neither a byte or line count is specified, the search is limited automatically
264 match the beginning and end of individual lines, respectively,
265 not beginning and end of file.
267 A literal string search starting at the given offset.
268 The same modifier flags can be used as for string patterns.
269 The search expression must contain the range in the form
271 that is the number of positions at which the match will be
272 attempted, starting from the start offset.
274 searching larger binary expressions with variable offsets, using
276 escapes for special characters.
277 The order of modifier and number is not relevant.
279 This is intended to be used with the test
281 (which is always true) and it has no type.
282 It matches when no other test at that continuation level has matched before.
283 Clearing that matched tests for a continuation level, can be done using the
287 This test is always true and clears the match flag for that continuation level.
288 It is intended to be used with the
293 For compatibility with the Single
295 Standard, the type specifiers
341 and the type specifier
345 In addition, the type specifier
349 and the type specifier
354 Each top-level magic pattern (see below for an explanation of levels)
355 is classified as text or binary according to the types used.
360 are classified as text tests, unless non-printable characters are used
362 All other tests are classified as binary.
364 pattern is considered to be a test text when all its patterns are text
365 patterns; otherwise, it is considered to be a binary pattern.
367 matching a file, binary patterns are tried first; if no match is
368 found, and the file looks like text, then its encoding is determined
369 and the text patterns are tried.
371 The numeric types may optionally be followed by
374 to specify that the value is to be AND'ed with the
375 numeric value before any comparisons are done.
378 to the type indicates that ordered comparisons should be unsigned.
380 The value to be compared with the value from the file.
383 is specified in C form; if it is a string, it is specified as a C string
384 with the usual escapes permitted (e.g. \en for new-line).
387 may be preceded by a character indicating the operation to be performed.
390 to specify that the value from the file must equal the specified value,
392 to specify that the value from the file must be less than the specified
395 to specify that the value from the file must be greater than the specified
398 to specify that the value from the file must have set all of the bits
399 that are set in the specified value,
401 to specify that the value from the file must have clear any of the bits
402 that are set in the specified value, or
404 the value specified after is negated before tested.
406 to specify that any value will match.
407 If the character is omitted, it is assumed to be
414 don't work with floats and doubles.
417 specifies that the line matches if the test does
421 Numeric values are specified in C form; e.g.
429 Numeric operations are not performed on date types, instead the numeric
430 value is interpreted as an offset.
432 For string values, the string from the
433 file must match the specified string.
441 can be applied to strings.
442 The length used for matching is that of the string argument
444 This means that a line can match any non-empty string (usually used to
445 then print the string), with
447 (because all non-empty strings are greater than the empty string).
449 Dates are treated as numerical values in the respective internal
454 always evaluates to true.
456 The message to be printed if the comparison succeeds.
457 If the string contains a
459 format specification, the value from the file (with any specified masking
460 performed) is printed using the message as the format string.
461 If the string begins with
463 the message printed is the remainder of the string with no whitespace
464 added before it: multiple matches are normally separated by a single
468 An APPLE 4+4 character APPLE creator and type can be specified as:
469 .Bd -literal -offset indent
473 A MIME type is given on a separate line, which must be the next
474 non-blank or comment line after the magic line that identifies the
475 file type, and has the following format:
476 .Bd -literal -offset indent
480 i.e. the literal string
482 followed by the MIME type.
484 An optional strength can be supplied on a separate line which refers to
485 the current magic description using the following format:
486 .Bd -literal -offset indent
500 is a constant between 0 and 255.
501 This constant is applied using the specified operand
502 to the currently computed default magic strength.
504 Some file formats contain additional information which is to be printed
505 along with the file type or need additional tests to determine the true
507 These additional tests are introduced by one or more
509 characters preceding the offset.
512 on the line indicates the level of the test; a line with no
514 at the beginning is considered to be at level 0.
515 Tests are arranged in a tree-like hierarchy:
516 if the test on a line at level
518 succeeds, all following tests at level
520 are performed, and the messages printed if the tests succeed, until a line
524 For more complex files, one can use empty messages to get just the
525 "if/then" effect, in the following way:
526 .Bd -literal -offset indent
528 \*[Gt]0x18 leshort \*[Lt]0x40 MS-DOS executable
529 \*[Gt]0x18 leshort \*[Gt]0x3f extended PC executable (e.g., MS Windows)
532 Offsets do not need to be constant, but can also be read from the file
534 If the first character following the last
538 then the string after the parenthesis is interpreted as an indirect offset.
539 That means that the number after the parenthesis is used as an offset in
541 The value at that offset is read, and is used again as an offset
543 Indirect offsets are of the form:
544 .Em (( x [[.,][bislBISL]][+\-][ y ]) .
547 is used as an offset in the file.
548 A byte, id3 length, short or long is read at that offset depending on the
551 The value is treated as signed if
553 is specified or unsigned if
556 The capitalized types interpret the number as a big endian
557 value, whereas the small letter versions interpret the number as a little
561 type interprets the number as a middle endian (PDP-11) value.
562 To that number the value of
564 is added and the result is used as an offset in the file.
565 The default type if one is not specified is long.
567 That way variable length structures can be examined:
568 .Bd -literal -offset indent
569 # MS Windows executables are also valid MS-DOS executables
571 \*[Gt]0x18 leshort \*[Lt]0x40 MZ executable (MS-DOS)
572 # skip the whole block below if it is not an extended executable
573 \*[Gt]0x18 leshort \*[Gt]0x3f
574 \*[Gt]\*[Gt](0x3c.l) string PE\e0\e0 PE executable (MS-Windows)
575 \*[Gt]\*[Gt](0x3c.l) string LX\e0\e0 LX executable (OS/2)
578 This strategy of examining has a drawback: you must make sure that you
579 eventually print something, or users may get empty output (such as when
580 there is neither PE\e0\e0 nor LE\e0\e0 in the above example).
582 If this indirect offset cannot be used directly, simple calculations are
584 .Em [+-*/%\*[Am]|^]number
585 inside parentheses allows one to modify
586 the value read from the file before it is used as an offset:
587 .Bd -literal -offset indent
588 # MS Windows executables are also valid MS-DOS executables
590 # sometimes, the value at 0x18 is less that 0x40 but there's still an
591 # extended executable, simply appended to the file
592 \*[Gt]0x18 leshort \*[Lt]0x40
593 \*[Gt]\*[Gt](4.s*512) leshort 0x014c COFF executable (MS-DOS, DJGPP)
594 \*[Gt]\*[Gt](4.s*512) leshort !0x014c MZ executable (MS-DOS)
597 Sometimes you do not know the exact offset as this depends on the length or
598 position (when indirection was used before) of preceding fields.
599 You can specify an offset relative to the end of the last up-level
602 as a prefix to the offset:
603 .Bd -literal -offset indent
605 \*[Gt]0x18 leshort \*[Gt]0x3f
606 \*[Gt]\*[Gt](0x3c.l) string PE\e0\e0 PE executable (MS-Windows)
607 # immediately following the PE signature is the CPU type
608 \*[Gt]\*[Gt]\*[Gt]\*[Am]0 leshort 0x14c for Intel 80386
609 \*[Gt]\*[Gt]\*[Gt]\*[Am]0 leshort 0x184 for DEC Alpha
612 Indirect and relative offsets can be combined:
613 .Bd -literal -offset indent
615 \*[Gt]0x18 leshort \*[Lt]0x40
616 \*[Gt]\*[Gt](4.s*512) leshort !0x014c MZ executable (MS-DOS)
617 # if it's not COFF, go back 512 bytes and add the offset taken
618 # from byte 2/3, which is yet another way of finding the start
619 # of the extended executable
620 \*[Gt]\*[Gt]\*[Gt]\*[Am](2.s-514) string LE LE executable (MS Windows VxD driver)
623 Or the other way around:
624 .Bd -literal -offset indent
626 \*[Gt]0x18 leshort \*[Gt]0x3f
627 \*[Gt]\*[Gt](0x3c.l) string LE\e0\e0 LE executable (MS-Windows)
628 # at offset 0x80 (-4, since relative offsets start at the end
629 # of the up-level match) inside the LE header, we find the absolute
630 # offset to the code area, where we look for a specific signature
631 \*[Gt]\*[Gt]\*[Gt](\*[Am]0x7c.l+0x26) string UPX \eb, UPX compressed
635 .Bd -literal -offset indent
637 \*[Gt]0x18 leshort \*[Gt]0x3f
638 \*[Gt]\*[Gt](0x3c.l) string LE\e0\e0 LE executable (MS-Windows)
639 # at offset 0x58 inside the LE header, we find the relative offset
640 # to a data area where we look for a specific signature
641 \*[Gt]\*[Gt]\*[Gt]\*[Am](\*[Am]0x54.l-3) string UNACE \eb, ACE self-extracting archive
644 If you have to deal with offset/length pairs in your file, even the
645 second value in a parenthesized expression can be taken from the file itself,
646 using another set of parentheses.
647 Note that this additional indirect offset is always relative to the
648 start of the main indirect offset.
649 .Bd -literal -offset indent
651 \*[Gt]0x18 leshort \*[Gt]0x3f
652 \*[Gt]\*[Gt](0x3c.l) string PE\e0\e0 PE executable (MS-Windows)
653 # search for the PE section called ".idata"...
654 \*[Gt]\*[Gt]\*[Gt]\*[Am]0xf4 search/0x140 .idata
655 # ...and go to the end of it, calculated from start+length;
656 # these are located 14 and 10 bytes after the section name
657 \*[Gt]\*[Gt]\*[Gt]\*[Gt](\*[Am]0xe.l+(-4)) string PK\e3\e4 \eb, ZIP self-extracting archive
660 If you have a list of known values at a particular continuation level,
661 and you want to provide a switch-like default case:
662 .Bd -literal -offset indent
663 # clear that continuation level match
665 \*[Gt]18 lelong 1 one
666 \*[Gt]18 lelong 2 two
668 # print default match
669 \*[Gt]\*[Gt]18 lelong x unmatched 0x%x
672 .Xr file __CSECTION__
673 \- the command that reads this file.
684 do not depend on the length of the C data types
688 on the platform, even though the Single
690 Specification implies that they do. However, as OS X Mountain Lion has
693 Specification validation suite, and supplies a version of
694 .Xr file __CSECTION__
695 in which they do not depend on the sizes of the C data types and that is
696 built for a 64-bit environment in which
698 is 8 bytes rather than 4 bytes, presumably the validation suite does not
699 test whether, for example
701 refers to an item with the same size as the C data type
703 There should probably be
715 and specified-byte-order variants of them,
716 to make it clearer that those types have specified widths.
718 .\" From: guy@sun.uucp (Guy Harris)
719 .\" Newsgroups: net.bugs.usg
720 .\" Subject: /etc/magic's format isn't well documented
721 .\" Message-ID: <2752@sun.uucp>
722 .\" Date: 3 Sep 85 08:19:07 GMT
723 .\" Organization: Sun Microsystems, Inc.
726 .\" Here's a manual page for the format accepted by the "file" made by adding
727 .\" the changes I posted to the S5R2 version.
729 .\" Modified for Ian Darwin's version of the file command.