2 .\" $File: magic.man,v 1.93 2018/06/22 20:39:49 christos Exp $
6 .\" install as magic.4 on USG, magic.5 on V7, Berkeley and Linux systems.
9 .Nd file command's magic pattern file
11 This manual page documents the format of magic files as
14 command, version __VERSION__.
17 command identifies the type of a file using,
19 a test for whether the file contains certain
20 .Dq "magic patterns" .
23 is usually located in a binary file in
25 or a directory of source text magic pattern fragment files in
27 The database specifies what patterns are to be tested for, what message or
28 MIME type to print if a particular pattern is found,
29 and additional information to extract from the file.
31 The format of the source fragment files that are used to build this database
33 Each line of a fragment file specifies a test to be performed.
34 A test compares the data starting at a particular offset
35 in the file with a byte value, a string or a numeric value.
36 If the test succeeds, a message is printed.
37 The line consists of the following fields:
38 .Bl -tag -width ".Dv message"
40 A number specifying the offset (in bytes) into the file of the data
41 which is to be tested.
42 This offset can be a negative number if it is:
45 The first direct offset of the magic entry (at continuation level 0),
46 in which case it is interpreted an offset from end end of the file
48 This works only when a file descriptor to the file is a available and it
51 A continuation offset relative to the end of the last up-level field
55 The type of the data to be tested.
56 The possible values are:
57 .Bl -tag -width ".Dv lestring16"
61 A two-byte value in this machine's native byte order.
63 A four-byte value in this machine's native byte order.
65 An eight-byte value in this machine's native byte order.
67 A 32-bit single precision IEEE floating point number in this machine's native byte order.
69 A 64-bit double precision IEEE floating point number in this machine's native byte order.
72 The string type specification can be optionally followed
76 flag compacts whitespace in the target, which must
77 contain at least one whitespace character.
80 consecutive blanks, the target needs at least
82 consecutive blanks to match.
85 flag treats every blank in the magic as an optional blank.
88 flag specifies case insensitive matching: lower case
89 characters in the magic match both lower and upper case characters in the
90 target, whereas upper case characters in the magic only match upper case
91 characters in the target.
94 flag specifies case insensitive matching: upper case
95 characters in the magic match both lower and upper case characters in the
96 target, whereas lower case characters in the magic only match upper case
97 characters in the target.
98 To do a complete case insensitive match, specify both
104 flag forces the test to be done for text files, while the
106 flag forces the test to be done for binary files.
109 flag causes the string to be trimmed, i.e. leading and trailing whitespace
110 is deleted before the string is printed.
112 A Pascal-style string where the first byte/short/int is interpreted as the
114 The length defaults to byte and can be specified as a modifier.
115 The following modifiers are supported:
116 .Bl -tag -compact -width B
118 A byte length (default).
120 A 4 byte big endian length.
122 A 2 byte big endian length.
124 A 4 byte little endian length.
126 A 2 byte little endian length.
128 The length includes itself in its count.
130 The string is not NUL terminated.
132 is used rather than the more
135 because this type of length is a feature of the JPEG
138 A four-byte value interpreted as a UNIX date.
140 A eight-byte value interpreted as a UNIX date.
142 A four-byte value interpreted as a UNIX-style date, but interpreted as
143 local time rather than UTC.
145 An eight-byte value interpreted as a UNIX-style date, but interpreted as
146 local time rather than UTC.
148 An eight-byte value interpreted as a Windows-style date.
150 A 32-bit ID3 length in big-endian byte order.
152 A two-byte value in big-endian byte order.
154 A four-byte value in big-endian byte order.
156 An eight-byte value in big-endian byte order.
158 A 32-bit single precision IEEE floating point number in big-endian byte order.
160 A 64-bit double precision IEEE floating point number in big-endian byte order.
162 A four-byte value in big-endian byte order,
163 interpreted as a Unix date.
165 An eight-byte value in big-endian byte order,
166 interpreted as a Unix date.
168 A four-byte value in big-endian byte order,
169 interpreted as a UNIX-style date, but interpreted as local time rather
172 An eight-byte value in big-endian byte order,
173 interpreted as a UNIX-style date, but interpreted as local time rather
176 An eight-byte value in big-endian byte order,
177 interpreted as a Windows-style date.
179 A two-byte unicode (UCS16) string in big-endian byte order.
181 A 32-bit ID3 length in little-endian byte order.
183 A two-byte value in little-endian byte order.
185 A four-byte value in little-endian byte order.
187 An eight-byte value in little-endian byte order.
189 A 32-bit single precision IEEE floating point number in little-endian byte order.
191 A 64-bit double precision IEEE floating point number in little-endian byte order.
193 A four-byte value in little-endian byte order,
194 interpreted as a UNIX date.
196 An eight-byte value in little-endian byte order,
197 interpreted as a UNIX date.
199 A four-byte value in little-endian byte order,
200 interpreted as a UNIX-style date, but interpreted as local time rather
203 An eight-byte value in little-endian byte order,
204 interpreted as a UNIX-style date, but interpreted as local time rather
207 An eight-byte value in little-endian byte order,
208 interpreted as a Windows-style date.
210 A two-byte unicode (UCS16) string in little-endian byte order.
212 A four-byte value in middle-endian (PDP-11) byte order.
214 A four-byte value in middle-endian (PDP-11) byte order,
215 interpreted as a UNIX date.
217 A four-byte value in middle-endian (PDP-11) byte order,
218 interpreted as a UNIX-style date, but interpreted as local time rather
221 Starting at the given offset, consult the magic database again.
224 magic is by default absolute in the file, but one can specify
226 to indicate that the offset is relative from the beginning of the entry.
230 magic instance that can be called from another
232 magic entry, like a subroutine call.
233 Named instance direct magic offsets are relative to the offset of the
234 previous matched entry, but indirect offsets are relative to the beginning
235 of the file as usual.
236 Named magic entries always match.
238 Recursively call the named magic starting from the current offset.
239 If the name of the referenced begins with a
241 then the endianness of the magic is switched; if the magic mentioned
247 This is useful to avoid duplicating the rules for different endianness.
249 A regular expression match in extended POSIX regular expression syntax
251 Regular expressions can take exponential time to process, and their
252 performance is hard to predict, so their use is discouraged.
253 When used in production environments, their performance
254 should be carefully checked.
255 The size of the string to search should also be limited by specifying
257 to avoid performance issues scanning long files.
258 The type specification can also be optionally followed by
262 flag makes the match case insensitive, while the
264 flag update the offset to the start offset of the match, rather than the end.
267 modifier, changes the limit of length to mean number of lines instead of a
269 Lines are delimited by the platforms native line delimiter.
270 When a line count is specified, an implicit byte count also computed assuming
271 each line is 80 characters long.
272 If neither a byte or line count is specified, the search is limited automatically
277 match the beginning and end of individual lines, respectively,
278 not beginning and end of file.
280 A literal string search starting at the given offset.
281 The same modifier flags can be used as for string patterns.
282 The search expression must contain the range in the form
284 that is the number of positions at which the match will be
285 attempted, starting from the start offset.
287 searching larger binary expressions with variable offsets, using
289 escapes for special characters.
290 The order of modifier and number is not relevant.
292 This is intended to be used with the test
294 (which is always true) and it has no type.
295 It matches when no other test at that continuation level has matched before.
296 Clearing that matched tests for a continuation level, can be done using the
300 This test is always true and clears the match flag for that continuation level.
301 It is intended to be used with the
306 For compatibility with the Single
308 Standard, the type specifiers
354 and the type specifier
358 In addition, the type specifier
362 and the type specifier
367 Each top-level magic pattern (see below for an explanation of levels)
368 is classified as text or binary according to the types used.
373 are classified as text tests, unless non-printable characters are used
375 All other tests are classified as binary.
377 pattern is considered to be a test text when all its patterns are text
378 patterns; otherwise, it is considered to be a binary pattern.
380 matching a file, binary patterns are tried first; if no match is
381 found, and the file looks like text, then its encoding is determined
382 and the text patterns are tried.
384 The numeric types may optionally be followed by
387 to specify that the value is to be AND'ed with the
388 numeric value before any comparisons are done.
391 to the type indicates that ordered comparisons should be unsigned.
393 The value to be compared with the value from the file.
396 is specified in C form; if it is a string, it is specified as a C string
397 with the usual escapes permitted (e.g. \en for new-line).
400 may be preceded by a character indicating the operation to be performed.
403 to specify that the value from the file must equal the specified value,
405 to specify that the value from the file must be less than the specified
408 to specify that the value from the file must be greater than the specified
411 to specify that the value from the file must have set all of the bits
412 that are set in the specified value,
414 to specify that the value from the file must have clear any of the bits
415 that are set in the specified value, or
417 the value specified after is negated before tested.
419 to specify that any value will match.
420 If the character is omitted, it is assumed to be
427 don't work with floats and doubles.
430 specifies that the line matches if the test does
434 Numeric values are specified in C form; e.g.
442 Numeric operations are not performed on date types, instead the numeric
443 value is interpreted as an offset.
445 For string values, the string from the
446 file must match the specified string.
454 can be applied to strings.
455 The length used for matching is that of the string argument
457 This means that a line can match any non-empty string (usually used to
458 then print the string), with
460 (because all non-empty strings are greater than the empty string).
462 Dates are treated as numerical values in the respective internal
467 always evaluates to true.
469 The message to be printed if the comparison succeeds.
470 If the string contains a
472 format specification, the value from the file (with any specified masking
473 performed) is printed using the message as the format string.
474 If the string begins with
476 the message printed is the remainder of the string with no whitespace
477 added before it: multiple matches are normally separated by a single
481 An APPLE 4+4 character APPLE creator and type can be specified as:
482 .Bd -literal -offset indent
486 A MIME type is given on a separate line, which must be the next
487 non-blank or comment line after the magic line that identifies the
488 file type, and has the following format:
489 .Bd -literal -offset indent
493 i.e. the literal string
495 followed by the MIME type.
497 An optional strength can be supplied on a separate line which refers to
498 the current magic description using the following format:
499 .Bd -literal -offset indent
513 is a constant between 0 and 255.
514 This constant is applied using the specified operand
515 to the currently computed default magic strength.
517 Some file formats contain additional information which is to be printed
518 along with the file type or need additional tests to determine the true
520 These additional tests are introduced by one or more
522 characters preceding the offset.
525 on the line indicates the level of the test; a line with no
527 at the beginning is considered to be at level 0.
528 Tests are arranged in a tree-like hierarchy:
529 if the test on a line at level
531 succeeds, all following tests at level
533 are performed, and the messages printed if the tests succeed, until a line
537 For more complex files, one can use empty messages to get just the
538 "if/then" effect, in the following way:
539 .Bd -literal -offset indent
541 \*[Gt]0x18 leshort \*[Lt]0x40 MS-DOS executable
542 \*[Gt]0x18 leshort \*[Gt]0x3f extended PC executable (e.g., MS Windows)
545 Offsets do not need to be constant, but can also be read from the file
547 If the first character following the last
551 then the string after the parenthesis is interpreted as an indirect offset.
552 That means that the number after the parenthesis is used as an offset in
554 The value at that offset is read, and is used again as an offset
556 Indirect offsets are of the form:
557 .Em (( x [[.,][bBcCeEfFgGhHiIlmsSqQ]][+\-][ y ]) .
560 is used as an offset in the file.
561 A byte, id3 length, short or long is read at that offset depending on the
562 .Em [bBcCeEfFgGhHiIlmsSqQ]
564 The value is treated as signed if
566 is specified or unsigned if
569 The capitalized types interpret the number as a big endian
570 value, whereas the small letter versions interpret the number as a little
574 type interprets the number as a middle endian (PDP-11) value.
575 To that number the value of
577 is added and the result is used as an offset in the file.
578 The default type if one is not specified is long.
579 The following types are recognized:
580 .Bl -column -offset indent "Type" "Half/Short" "Little" "Size"
581 .It Sy Type Sy Mnemonic Sy Endian Sy Size
582 .It bcBc Byte/Char N/A 1
583 .It efg Double Little 8
585 .It hs Half/Short Little 2
586 .It HS Half/Short Big 2
589 .It m Middle Middle 4
594 That way variable length structures can be examined:
595 .Bd -literal -offset indent
596 # MS Windows executables are also valid MS-DOS executables
598 \*[Gt]0x18 leshort \*[Lt]0x40 MZ executable (MS-DOS)
599 # skip the whole block below if it is not an extended executable
600 \*[Gt]0x18 leshort \*[Gt]0x3f
601 \*[Gt]\*[Gt](0x3c.l) string PE\e0\e0 PE executable (MS-Windows)
602 \*[Gt]\*[Gt](0x3c.l) string LX\e0\e0 LX executable (OS/2)
605 This strategy of examining has a drawback: you must make sure that you
606 eventually print something, or users may get empty output (such as when
607 there is neither PE\e0\e0 nor LE\e0\e0 in the above example).
609 If this indirect offset cannot be used directly, simple calculations are
611 .Em [+-*/%\*[Am]|^]number
612 inside parentheses allows one to modify
613 the value read from the file before it is used as an offset:
614 .Bd -literal -offset indent
615 # MS Windows executables are also valid MS-DOS executables
617 # sometimes, the value at 0x18 is less that 0x40 but there's still an
618 # extended executable, simply appended to the file
619 \*[Gt]0x18 leshort \*[Lt]0x40
620 \*[Gt]\*[Gt](4.s*512) leshort 0x014c COFF executable (MS-DOS, DJGPP)
621 \*[Gt]\*[Gt](4.s*512) leshort !0x014c MZ executable (MS-DOS)
624 Sometimes you do not know the exact offset as this depends on the length or
625 position (when indirection was used before) of preceding fields.
626 You can specify an offset relative to the end of the last up-level
629 as a prefix to the offset:
630 .Bd -literal -offset indent
632 \*[Gt]0x18 leshort \*[Gt]0x3f
633 \*[Gt]\*[Gt](0x3c.l) string PE\e0\e0 PE executable (MS-Windows)
634 # immediately following the PE signature is the CPU type
635 \*[Gt]\*[Gt]\*[Gt]\*[Am]0 leshort 0x14c for Intel 80386
636 \*[Gt]\*[Gt]\*[Gt]\*[Am]0 leshort 0x184 for DEC Alpha
639 Indirect and relative offsets can be combined:
640 .Bd -literal -offset indent
642 \*[Gt]0x18 leshort \*[Lt]0x40
643 \*[Gt]\*[Gt](4.s*512) leshort !0x014c MZ executable (MS-DOS)
644 # if it's not COFF, go back 512 bytes and add the offset taken
645 # from byte 2/3, which is yet another way of finding the start
646 # of the extended executable
647 \*[Gt]\*[Gt]\*[Gt]\*[Am](2.s-514) string LE LE executable (MS Windows VxD driver)
650 Or the other way around:
651 .Bd -literal -offset indent
653 \*[Gt]0x18 leshort \*[Gt]0x3f
654 \*[Gt]\*[Gt](0x3c.l) string LE\e0\e0 LE executable (MS-Windows)
655 # at offset 0x80 (-4, since relative offsets start at the end
656 # of the up-level match) inside the LE header, we find the absolute
657 # offset to the code area, where we look for a specific signature
658 \*[Gt]\*[Gt]\*[Gt](\*[Am]0x7c.l+0x26) string UPX \eb, UPX compressed
662 .Bd -literal -offset indent
664 \*[Gt]0x18 leshort \*[Gt]0x3f
665 \*[Gt]\*[Gt](0x3c.l) string LE\e0\e0 LE executable (MS-Windows)
666 # at offset 0x58 inside the LE header, we find the relative offset
667 # to a data area where we look for a specific signature
668 \*[Gt]\*[Gt]\*[Gt]\*[Am](\*[Am]0x54.l-3) string UNACE \eb, ACE self-extracting archive
671 If you have to deal with offset/length pairs in your file, even the
672 second value in a parenthesized expression can be taken from the file itself,
673 using another set of parentheses.
674 Note that this additional indirect offset is always relative to the
675 start of the main indirect offset.
676 .Bd -literal -offset indent
678 \*[Gt]0x18 leshort \*[Gt]0x3f
679 \*[Gt]\*[Gt](0x3c.l) string PE\e0\e0 PE executable (MS-Windows)
680 # search for the PE section called ".idata"...
681 \*[Gt]\*[Gt]\*[Gt]\*[Am]0xf4 search/0x140 .idata
682 # ...and go to the end of it, calculated from start+length;
683 # these are located 14 and 10 bytes after the section name
684 \*[Gt]\*[Gt]\*[Gt]\*[Gt](\*[Am]0xe.l+(-4)) string PK\e3\e4 \eb, ZIP self-extracting archive
687 If you have a list of known values at a particular continuation level,
688 and you want to provide a switch-like default case:
689 .Bd -literal -offset indent
690 # clear that continuation level match
692 \*[Gt]18 lelong 1 one
693 \*[Gt]18 lelong 2 two
695 # print default match
696 \*[Gt]\*[Gt]18 lelong x unmatched 0x%x
699 .Xr file __CSECTION__
700 \- the command that reads this file.
711 do not depend on the length of the C data types
715 on the platform, even though the Single
717 Specification implies that they do. However, as OS X Mountain Lion has
720 Specification validation suite, and supplies a version of
721 .Xr file __CSECTION__
722 in which they do not depend on the sizes of the C data types and that is
723 built for a 64-bit environment in which
725 is 8 bytes rather than 4 bytes, presumably the validation suite does not
726 test whether, for example
728 refers to an item with the same size as the C data type
730 There should probably be
742 and specified-byte-order variants of them,
743 to make it clearer that those types have specified widths.
745 .\" From: guy@sun.uucp (Guy Harris)
746 .\" Newsgroups: net.bugs.usg
747 .\" Subject: /etc/magic's format isn't well documented
748 .\" Message-ID: <2752@sun.uucp>
749 .\" Date: 3 Sep 85 08:19:07 GMT
750 .\" Organization: Sun Microsystems, Inc.
753 .\" Here's a manual page for the format accepted by the "file" made by adding
754 .\" the changes I posted to the S5R2 version.
756 .\" Modified for Ian Darwin's version of the file command.