1 .\" $File: magic.man,v 1.92 2017/11/04 01:11:32 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.
41 This offset can be a negative number if it is:
44 The first direct offset of the magic entry (at continuation level 0),
45 in which case it is interpreted an offset from end end of the file
47 This works only when a file descriptor to the file is a available and it
50 A continuation offset relative to the end of the last up-level field
54 The type of the data to be tested.
55 The possible values are:
56 .Bl -tag -width ".Dv lestring16"
60 A two-byte value in this machine's native byte order.
62 A four-byte value in this machine's native byte order.
64 An eight-byte value in this machine's native byte order.
66 A 32-bit single precision IEEE floating point number in this machine's native byte order.
68 A 64-bit double precision IEEE floating point number in this machine's native byte order.
71 The string type specification can be optionally followed
75 flag compacts whitespace in the target, which must
76 contain at least one whitespace character.
79 consecutive blanks, the target needs at least
81 consecutive blanks to match.
84 flag treats every blank in the magic as an optional blank.
87 flag specifies case insensitive matching: lower case
88 characters in the magic match both lower and upper case characters in the
89 target, whereas upper case characters in the magic only match upper case
90 characters in the target.
93 flag specifies case insensitive matching: upper case
94 characters in the magic match both lower and upper case characters in the
95 target, whereas lower case characters in the magic only match upper case
96 characters in the target.
97 To do a complete case insensitive match, specify both
103 flag forces the test to be done for text files, while the
105 flag forces the test to be done for binary files.
108 flag causes the string to be trimmed, i.e. leading and trailing whitespace
109 is deleted before the string is printed.
111 A Pascal-style string where the first byte/short/int is interpreted as the
113 The length defaults to byte and can be specified as a modifier.
114 The following modifiers are supported:
115 .Bl -tag -compact -width B
117 A byte length (default).
119 A 4 byte big endian length.
121 A 2 byte big endian length.
123 A 4 byte little endian length.
125 A 2 byte little endian length.
127 The length includes itself in its count.
129 The string is not NUL terminated.
131 is used rather than the more
134 because this type of length is a feature of the JPEG
137 A four-byte value interpreted as a UNIX date.
139 A eight-byte value interpreted as a UNIX date.
141 A four-byte value interpreted as a UNIX-style date, but interpreted as
142 local time rather than UTC.
144 An eight-byte value interpreted as a UNIX-style date, but interpreted as
145 local time rather than UTC.
147 An eight-byte value interpreted as a Windows-style date.
149 A 32-bit ID3 length in big-endian byte order.
151 A two-byte value in big-endian byte order.
153 A four-byte value in big-endian byte order.
155 An eight-byte value in big-endian byte order.
157 A 32-bit single precision IEEE floating point number in big-endian byte order.
159 A 64-bit double precision IEEE floating point number in big-endian byte order.
161 A four-byte value in big-endian byte order,
162 interpreted as a Unix date.
164 An eight-byte value in big-endian byte order,
165 interpreted as a Unix date.
167 A four-byte value in big-endian byte order,
168 interpreted as a UNIX-style date, but interpreted as local time rather
171 An eight-byte value in big-endian byte order,
172 interpreted as a UNIX-style date, but interpreted as local time rather
175 An eight-byte value in big-endian byte order,
176 interpreted as a Windows-style date.
178 A two-byte unicode (UCS16) string in big-endian byte order.
180 A 32-bit ID3 length in little-endian byte order.
182 A two-byte value in little-endian byte order.
184 A four-byte value in little-endian byte order.
186 An eight-byte value in little-endian byte order.
188 A 32-bit single precision IEEE floating point number in little-endian byte order.
190 A 64-bit double precision IEEE floating point number in little-endian byte order.
192 A four-byte value in little-endian byte order,
193 interpreted as a UNIX date.
195 An eight-byte value in little-endian byte order,
196 interpreted as a UNIX date.
198 A four-byte value in little-endian byte order,
199 interpreted as a UNIX-style date, but interpreted as local time rather
202 An eight-byte value in little-endian byte order,
203 interpreted as a UNIX-style date, but interpreted as local time rather
206 An eight-byte value in little-endian byte order,
207 interpreted as a Windows-style date.
209 A two-byte unicode (UCS16) string in little-endian byte order.
211 A four-byte value in middle-endian (PDP-11) byte order.
213 A four-byte value in middle-endian (PDP-11) byte order,
214 interpreted as a UNIX date.
216 A four-byte value in middle-endian (PDP-11) byte order,
217 interpreted as a UNIX-style date, but interpreted as local time rather
220 Starting at the given offset, consult the magic database again.
223 magic is by default absolute in the file, but one can specify
225 to indicate that the offset is relative from the beginning of the entry.
229 magic instance that can be called from another
231 magic entry, like a subroutine call.
232 Named instance direct magic offsets are relative to the offset of the
233 previous matched entry, but indirect offsets are relative to the beginning
234 of the file as usual.
235 Named magic entries always match.
237 Recursively call the named magic starting from the current offset.
238 If the name of the referenced begins with a
240 then the endianness of the magic is switched; if the magic mentioned
246 This is useful to avoid duplicating the rules for different endianness.
248 A regular expression match in extended POSIX regular expression syntax
250 Regular expressions can take exponential time to process, and their
251 performance is hard to predict, so their use is discouraged.
252 When used in production environments, their performance
253 should be carefully checked.
254 The size of the string to search should also be limited by specifying
256 to avoid performance issues scanning long files.
257 The type specification can also be optionally followed by
261 flag makes the match case insensitive, while the
263 flag update the offset to the start offset of the match, rather than the end.
266 modifier, changes the limit of length to mean number of lines instead of a
268 Lines are delimited by the platforms native line delimiter.
269 When a line count is specified, an implicit byte count also computed assuming
270 each line is 80 characters long.
271 If neither a byte or line count is specified, the search is limited automatically
276 match the beginning and end of individual lines, respectively,
277 not beginning and end of file.
279 A literal string search starting at the given offset.
280 The same modifier flags can be used as for string patterns.
281 The search expression must contain the range in the form
283 that is the number of positions at which the match will be
284 attempted, starting from the start offset.
286 searching larger binary expressions with variable offsets, using
288 escapes for special characters.
289 The order of modifier and number is not relevant.
291 This is intended to be used with the test
293 (which is always true) and it has no type.
294 It matches when no other test at that continuation level has matched before.
295 Clearing that matched tests for a continuation level, can be done using the
299 This test is always true and clears the match flag for that continuation level.
300 It is intended to be used with the
305 For compatibility with the Single
307 Standard, the type specifiers
353 and the type specifier
357 In addition, the type specifier
361 and the type specifier
366 Each top-level magic pattern (see below for an explanation of levels)
367 is classified as text or binary according to the types used.
372 are classified as text tests, unless non-printable characters are used
374 All other tests are classified as binary.
376 pattern is considered to be a test text when all its patterns are text
377 patterns; otherwise, it is considered to be a binary pattern.
379 matching a file, binary patterns are tried first; if no match is
380 found, and the file looks like text, then its encoding is determined
381 and the text patterns are tried.
383 The numeric types may optionally be followed by
386 to specify that the value is to be AND'ed with the
387 numeric value before any comparisons are done.
390 to the type indicates that ordered comparisons should be unsigned.
392 The value to be compared with the value from the file.
395 is specified in C form; if it is a string, it is specified as a C string
396 with the usual escapes permitted (e.g. \en for new-line).
399 may be preceded by a character indicating the operation to be performed.
402 to specify that the value from the file must equal the specified value,
404 to specify that the value from the file must be less than the specified
407 to specify that the value from the file must be greater than the specified
410 to specify that the value from the file must have set all of the bits
411 that are set in the specified value,
413 to specify that the value from the file must have clear any of the bits
414 that are set in the specified value, or
416 the value specified after is negated before tested.
418 to specify that any value will match.
419 If the character is omitted, it is assumed to be
426 don't work with floats and doubles.
429 specifies that the line matches if the test does
433 Numeric values are specified in C form; e.g.
441 Numeric operations are not performed on date types, instead the numeric
442 value is interpreted as an offset.
444 For string values, the string from the
445 file must match the specified string.
453 can be applied to strings.
454 The length used for matching is that of the string argument
456 This means that a line can match any non-empty string (usually used to
457 then print the string), with
459 (because all non-empty strings are greater than the empty string).
461 Dates are treated as numerical values in the respective internal
466 always evaluates to true.
468 The message to be printed if the comparison succeeds.
469 If the string contains a
471 format specification, the value from the file (with any specified masking
472 performed) is printed using the message as the format string.
473 If the string begins with
475 the message printed is the remainder of the string with no whitespace
476 added before it: multiple matches are normally separated by a single
480 An APPLE 4+4 character APPLE creator and type can be specified as:
481 .Bd -literal -offset indent
485 A MIME type is given on a separate line, which must be the next
486 non-blank or comment line after the magic line that identifies the
487 file type, and has the following format:
488 .Bd -literal -offset indent
492 i.e. the literal string
494 followed by the MIME type.
496 An optional strength can be supplied on a separate line which refers to
497 the current magic description using the following format:
498 .Bd -literal -offset indent
512 is a constant between 0 and 255.
513 This constant is applied using the specified operand
514 to the currently computed default magic strength.
516 Some file formats contain additional information which is to be printed
517 along with the file type or need additional tests to determine the true
519 These additional tests are introduced by one or more
521 characters preceding the offset.
524 on the line indicates the level of the test; a line with no
526 at the beginning is considered to be at level 0.
527 Tests are arranged in a tree-like hierarchy:
528 if the test on a line at level
530 succeeds, all following tests at level
532 are performed, and the messages printed if the tests succeed, until a line
536 For more complex files, one can use empty messages to get just the
537 "if/then" effect, in the following way:
538 .Bd -literal -offset indent
540 \*[Gt]0x18 leshort \*[Lt]0x40 MS-DOS executable
541 \*[Gt]0x18 leshort \*[Gt]0x3f extended PC executable (e.g., MS Windows)
544 Offsets do not need to be constant, but can also be read from the file
546 If the first character following the last
550 then the string after the parenthesis is interpreted as an indirect offset.
551 That means that the number after the parenthesis is used as an offset in
553 The value at that offset is read, and is used again as an offset
555 Indirect offsets are of the form:
556 .Em (( x [[.,][bislBISL]][+\-][ y ]) .
559 is used as an offset in the file.
560 A byte, id3 length, short or long is read at that offset depending on the
563 The value is treated as signed if
565 is specified or unsigned if
568 The capitalized types interpret the number as a big endian
569 value, whereas the small letter versions interpret the number as a little
573 type interprets the number as a middle endian (PDP-11) value.
574 To that number the value of
576 is added and the result is used as an offset in the file.
577 The default type if one is not specified is long.
579 That way variable length structures can be examined:
580 .Bd -literal -offset indent
581 # MS Windows executables are also valid MS-DOS executables
583 \*[Gt]0x18 leshort \*[Lt]0x40 MZ executable (MS-DOS)
584 # skip the whole block below if it is not an extended executable
585 \*[Gt]0x18 leshort \*[Gt]0x3f
586 \*[Gt]\*[Gt](0x3c.l) string PE\e0\e0 PE executable (MS-Windows)
587 \*[Gt]\*[Gt](0x3c.l) string LX\e0\e0 LX executable (OS/2)
590 This strategy of examining has a drawback: you must make sure that you
591 eventually print something, or users may get empty output (such as when
592 there is neither PE\e0\e0 nor LE\e0\e0 in the above example).
594 If this indirect offset cannot be used directly, simple calculations are
596 .Em [+-*/%\*[Am]|^]number
597 inside parentheses allows one to modify
598 the value read from the file before it is used as an offset:
599 .Bd -literal -offset indent
600 # MS Windows executables are also valid MS-DOS executables
602 # sometimes, the value at 0x18 is less that 0x40 but there's still an
603 # extended executable, simply appended to the file
604 \*[Gt]0x18 leshort \*[Lt]0x40
605 \*[Gt]\*[Gt](4.s*512) leshort 0x014c COFF executable (MS-DOS, DJGPP)
606 \*[Gt]\*[Gt](4.s*512) leshort !0x014c MZ executable (MS-DOS)
609 Sometimes you do not know the exact offset as this depends on the length or
610 position (when indirection was used before) of preceding fields.
611 You can specify an offset relative to the end of the last up-level
614 as a prefix to the offset:
615 .Bd -literal -offset indent
617 \*[Gt]0x18 leshort \*[Gt]0x3f
618 \*[Gt]\*[Gt](0x3c.l) string PE\e0\e0 PE executable (MS-Windows)
619 # immediately following the PE signature is the CPU type
620 \*[Gt]\*[Gt]\*[Gt]\*[Am]0 leshort 0x14c for Intel 80386
621 \*[Gt]\*[Gt]\*[Gt]\*[Am]0 leshort 0x184 for DEC Alpha
624 Indirect and relative offsets can be combined:
625 .Bd -literal -offset indent
627 \*[Gt]0x18 leshort \*[Lt]0x40
628 \*[Gt]\*[Gt](4.s*512) leshort !0x014c MZ executable (MS-DOS)
629 # if it's not COFF, go back 512 bytes and add the offset taken
630 # from byte 2/3, which is yet another way of finding the start
631 # of the extended executable
632 \*[Gt]\*[Gt]\*[Gt]\*[Am](2.s-514) string LE LE executable (MS Windows VxD driver)
635 Or the other way around:
636 .Bd -literal -offset indent
638 \*[Gt]0x18 leshort \*[Gt]0x3f
639 \*[Gt]\*[Gt](0x3c.l) string LE\e0\e0 LE executable (MS-Windows)
640 # at offset 0x80 (-4, since relative offsets start at the end
641 # of the up-level match) inside the LE header, we find the absolute
642 # offset to the code area, where we look for a specific signature
643 \*[Gt]\*[Gt]\*[Gt](\*[Am]0x7c.l+0x26) string UPX \eb, UPX compressed
647 .Bd -literal -offset indent
649 \*[Gt]0x18 leshort \*[Gt]0x3f
650 \*[Gt]\*[Gt](0x3c.l) string LE\e0\e0 LE executable (MS-Windows)
651 # at offset 0x58 inside the LE header, we find the relative offset
652 # to a data area where we look for a specific signature
653 \*[Gt]\*[Gt]\*[Gt]\*[Am](\*[Am]0x54.l-3) string UNACE \eb, ACE self-extracting archive
656 If you have to deal with offset/length pairs in your file, even the
657 second value in a parenthesized expression can be taken from the file itself,
658 using another set of parentheses.
659 Note that this additional indirect offset is always relative to the
660 start of the main indirect offset.
661 .Bd -literal -offset indent
663 \*[Gt]0x18 leshort \*[Gt]0x3f
664 \*[Gt]\*[Gt](0x3c.l) string PE\e0\e0 PE executable (MS-Windows)
665 # search for the PE section called ".idata"...
666 \*[Gt]\*[Gt]\*[Gt]\*[Am]0xf4 search/0x140 .idata
667 # ...and go to the end of it, calculated from start+length;
668 # these are located 14 and 10 bytes after the section name
669 \*[Gt]\*[Gt]\*[Gt]\*[Gt](\*[Am]0xe.l+(-4)) string PK\e3\e4 \eb, ZIP self-extracting archive
672 If you have a list of known values at a particular continuation level,
673 and you want to provide a switch-like default case:
674 .Bd -literal -offset indent
675 # clear that continuation level match
677 \*[Gt]18 lelong 1 one
678 \*[Gt]18 lelong 2 two
680 # print default match
681 \*[Gt]\*[Gt]18 lelong x unmatched 0x%x
684 .Xr file __CSECTION__
685 \- the command that reads this file.
696 do not depend on the length of the C data types
700 on the platform, even though the Single
702 Specification implies that they do. However, as OS X Mountain Lion has
705 Specification validation suite, and supplies a version of
706 .Xr file __CSECTION__
707 in which they do not depend on the sizes of the C data types and that is
708 built for a 64-bit environment in which
710 is 8 bytes rather than 4 bytes, presumably the validation suite does not
711 test whether, for example
713 refers to an item with the same size as the C data type
715 There should probably be
727 and specified-byte-order variants of them,
728 to make it clearer that those types have specified widths.
730 .\" From: guy@sun.uucp (Guy Harris)
731 .\" Newsgroups: net.bugs.usg
732 .\" Subject: /etc/magic's format isn't well documented
733 .\" Message-ID: <2752@sun.uucp>
734 .\" Date: 3 Sep 85 08:19:07 GMT
735 .\" Organization: Sun Microsystems, Inc.
738 .\" Here's a manual page for the format accepted by the "file" made by adding
739 .\" the changes I posted to the S5R2 version.
741 .\" Modified for Ian Darwin's version of the file command.