1 .\" $File: magic.man,v 1.85 2015/01/01 17:07:34 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 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.
90 flag causes the string to be trimmed, i.e. leading and trailing whitespace
91 is deleted before the string is printed.
93 A Pascal-style string where the first byte/short/int is interpreted as the
95 The length defaults to byte and can be specified as a modifier.
96 The following modifiers are supported:
97 .Bl -tag -compact -width B
99 A byte length (default).
101 A 2 byte big endian length.
103 A 2 byte big little length.
105 A 4 byte big endian length.
107 A 4 byte big little length.
109 The length includes itself in its count.
111 The string is not NUL terminated.
113 is used rather than the more
116 because this type of length is a feature of the JPEG
119 A four-byte value interpreted as a UNIX date.
121 A eight-byte value interpreted as a UNIX date.
123 A four-byte value interpreted as a UNIX-style date, but interpreted as
124 local time rather than UTC.
126 An eight-byte value interpreted as a UNIX-style date, but interpreted as
127 local time rather than UTC.
129 An eight-byte value interpreted as a Windows-style date.
131 A 32-bit ID3 length in big-endian byte order.
133 A two-byte value in big-endian byte order.
135 A four-byte value in big-endian byte order.
137 An eight-byte value in big-endian byte order.
139 A 32-bit single precision IEEE floating point number in big-endian byte order.
141 A 64-bit double precision IEEE floating point number in big-endian byte order.
143 A four-byte value in big-endian byte order,
144 interpreted as a Unix date.
146 An eight-byte value in big-endian byte order,
147 interpreted as a Unix date.
149 A four-byte value in big-endian byte order,
150 interpreted as a UNIX-style date, but interpreted as local time rather
153 An eight-byte value in big-endian byte order,
154 interpreted as a UNIX-style date, but interpreted as local time rather
157 An eight-byte value in big-endian byte order,
158 interpreted as a Windows-style date.
160 A two-byte unicode (UCS16) string in big-endian byte order.
162 A 32-bit ID3 length in little-endian byte order.
164 A two-byte value in little-endian byte order.
166 A four-byte value in little-endian byte order.
168 An eight-byte value in little-endian byte order.
170 A 32-bit single precision IEEE floating point number in little-endian byte order.
172 A 64-bit double precision IEEE floating point number in little-endian byte order.
174 A four-byte value in little-endian byte order,
175 interpreted as a UNIX date.
177 An eight-byte value in little-endian byte order,
178 interpreted as a UNIX date.
180 A four-byte value in little-endian byte order,
181 interpreted as a UNIX-style date, but interpreted as local time rather
184 An eight-byte value in little-endian byte order,
185 interpreted as a UNIX-style date, but interpreted as local time rather
188 An eight-byte value in little-endian byte order,
189 interpreted as a Windows-style date.
191 A two-byte unicode (UCS16) string in little-endian byte order.
193 A four-byte value in middle-endian (PDP-11) byte order.
195 A four-byte value in middle-endian (PDP-11) byte order,
196 interpreted as a UNIX date.
198 A four-byte value in middle-endian (PDP-11) byte order,
199 interpreted as a UNIX-style date, but interpreted as local time rather
202 Starting at the given offset, consult the magic database again.
205 magic is by default absolute in the file, but one can specify
207 to indicate that the offset is relative from the beginning of the entry.
211 magic instance that can be called from another
213 magic entry, like a subroutine call.
214 Named instance direct magic offsets are relative to the offset of the
215 previous matched entry, but indirect offsets are relative to the beginning
216 of the file as usual.
217 Named magic entries always match.
219 Recursively call the named magic starting from the current offset.
220 If the name of the referenced begins with a
222 then the endianness of the magic is switched; if the magic mentioned
228 This is useful to avoid duplicating the rules for different endianness.
230 A regular expression match in extended POSIX regular expression syntax
232 Regular expressions can take exponential time to process, and their
233 performance is hard to predict, so their use is discouraged.
234 When used in production environments, their performance
235 should be carefully checked.
236 The size of the string to search should also be limited by specifying
238 to avoid performance issues scanning long files.
239 The type specification can also be optionally followed by
243 flag makes the match case insensitive, while the
245 flag update the offset to the start offset of the match, rather than the end.
248 modifier, changes the limit of length to mean number of lines instead of a
250 Lines are delimited by the platforms native line delimiter.
251 When a line count is specified, an implicit byte count also computed assuming
252 each line is 80 characters long.
253 If neither a byte or line count is specified, the search is limited automatically
258 match the beginning and end of individual lines, respectively,
259 not beginning and end of file.
261 A literal string search starting at the given offset.
262 The same modifier flags can be used as for string patterns.
263 The search expression must contain the range in the form
265 that is the number of positions at which the match will be
266 attempted, starting from the start offset.
268 searching larger binary expressions with variable offsets, using
270 escapes for special characters.
271 The order of modifier and number is not relevant.
273 This is intended to be used with the test
275 (which is always true) and it has no type.
276 It matches when no other test at that continuation level has matched before.
277 Clearing that matched tests for a continuation level, can be done using the
281 This test is always true and clears the match flag for that continuation level.
282 It is intended to be used with the
287 For compatibility with the Single
289 Standard, the type specifiers
335 and the type specifier
339 In addition, the type specifier
343 and the type specifier
348 Each top-level magic pattern (see below for an explanation of levels)
349 is classified as text or binary according to the types used.
354 are classified as text tests, unless non-printable characters are used
356 All other tests are classified as binary.
358 pattern is considered to be a test text when all its patterns are text
359 patterns; otherwise, it is considered to be a binary pattern.
361 matching a file, binary patterns are tried first; if no match is
362 found, and the file looks like text, then its encoding is determined
363 and the text patterns are tried.
365 The numeric types may optionally be followed by
368 to specify that the value is to be AND'ed with the
369 numeric value before any comparisons are done.
372 to the type indicates that ordered comparisons should be unsigned.
374 The value to be compared with the value from the file.
377 is specified in C form; if it is a string, it is specified as a C string
378 with the usual escapes permitted (e.g. \en for new-line).
381 may be preceded by a character indicating the operation to be performed.
384 to specify that the value from the file must equal the specified value,
386 to specify that the value from the file must be less than the specified
389 to specify that the value from the file must be greater than the specified
392 to specify that the value from the file must have set all of the bits
393 that are set in the specified value,
395 to specify that the value from the file must have clear any of the bits
396 that are set in the specified value, or
398 the value specified after is negated before tested.
400 to specify that any value will match.
401 If the character is omitted, it is assumed to be
408 don't work with floats and doubles.
411 specifies that the line matches if the test does
415 Numeric values are specified in C form; e.g.
423 Numeric operations are not performed on date types, instead the numeric
424 value is interpreted as an offset.
426 For string values, the string from the
427 file must match the specified string.
435 can be applied to strings.
436 The length used for matching is that of the string argument
438 This means that a line can match any non-empty string (usually used to
439 then print the string), with
441 (because all non-empty strings are greater than the empty string).
443 Dates are treated as numerical values in the respective internal
448 always evaluates to true.
450 The message to be printed if the comparison succeeds.
451 If the string contains a
453 format specification, the value from the file (with any specified masking
454 performed) is printed using the message as the format string.
455 If the string begins with
457 the message printed is the remainder of the string with no whitespace
458 added before it: multiple matches are normally separated by a single
462 An APPLE 4+4 character APPLE creator and type can be specified as:
463 .Bd -literal -offset indent
467 A MIME type is given on a separate line, which must be the next
468 non-blank or comment line after the magic line that identifies the
469 file type, and has the following format:
470 .Bd -literal -offset indent
474 i.e. the literal string
476 followed by the MIME type.
478 An optional strength can be supplied on a separate line which refers to
479 the current magic description using the following format:
480 .Bd -literal -offset indent
494 is a constant between 0 and 255.
495 This constant is applied using the specified operand
496 to the currently computed default magic strength.
498 Some file formats contain additional information which is to be printed
499 along with the file type or need additional tests to determine the true
501 These additional tests are introduced by one or more
503 characters preceding the offset.
506 on the line indicates the level of the test; a line with no
508 at the beginning is considered to be at level 0.
509 Tests are arranged in a tree-like hierarchy:
510 if the test on a line at level
512 succeeds, all following tests at level
514 are performed, and the messages printed if the tests succeed, until a line
518 For more complex files, one can use empty messages to get just the
519 "if/then" effect, in the following way:
520 .Bd -literal -offset indent
522 \*[Gt]0x18 leshort \*[Lt]0x40 MS-DOS executable
523 \*[Gt]0x18 leshort \*[Gt]0x3f extended PC executable (e.g., MS Windows)
526 Offsets do not need to be constant, but can also be read from the file
528 If the first character following the last
532 then the string after the parenthesis is interpreted as an indirect offset.
533 That means that the number after the parenthesis is used as an offset in
535 The value at that offset is read, and is used again as an offset
537 Indirect offsets are of the form:
538 .Em (( x [.[bislBISL]][+\-][ y ]) .
541 is used as an offset in the file.
542 A byte, id3 length, short or long is read at that offset depending on the
545 The capitalized types interpret the number as a big endian
546 value, whereas the small letter versions interpret the number as a little
550 type interprets the number as a middle endian (PDP-11) value.
551 To that number the value of
553 is added and the result is used as an offset in the file.
554 The default type if one is not specified is long.
556 That way variable length structures can be examined:
557 .Bd -literal -offset indent
558 # MS Windows executables are also valid MS-DOS executables
560 \*[Gt]0x18 leshort \*[Lt]0x40 MZ executable (MS-DOS)
561 # skip the whole block below if it is not an extended executable
562 \*[Gt]0x18 leshort \*[Gt]0x3f
563 \*[Gt]\*[Gt](0x3c.l) string PE\e0\e0 PE executable (MS-Windows)
564 \*[Gt]\*[Gt](0x3c.l) string LX\e0\e0 LX executable (OS/2)
567 This strategy of examining has a drawback: You must make sure that
568 you eventually print something, or users may get empty output (like, when
569 there is neither PE\e0\e0 nor LE\e0\e0 in the above example)
571 If this indirect offset cannot be used directly, simple calculations are
573 .Em [+-*/%\*[Am]|^]number
574 inside parentheses allows one to modify
575 the value read from the file before it is used as an offset:
576 .Bd -literal -offset indent
577 # MS Windows executables are also valid MS-DOS executables
579 # sometimes, the value at 0x18 is less that 0x40 but there's still an
580 # extended executable, simply appended to the file
581 \*[Gt]0x18 leshort \*[Lt]0x40
582 \*[Gt]\*[Gt](4.s*512) leshort 0x014c COFF executable (MS-DOS, DJGPP)
583 \*[Gt]\*[Gt](4.s*512) leshort !0x014c MZ executable (MS-DOS)
586 Sometimes you do not know the exact offset as this depends on the length or
587 position (when indirection was used before) of preceding fields.
588 You can specify an offset relative to the end of the last up-level
591 as a prefix to the offset:
592 .Bd -literal -offset indent
594 \*[Gt]0x18 leshort \*[Gt]0x3f
595 \*[Gt]\*[Gt](0x3c.l) string PE\e0\e0 PE executable (MS-Windows)
596 # immediately following the PE signature is the CPU type
597 \*[Gt]\*[Gt]\*[Gt]\*[Am]0 leshort 0x14c for Intel 80386
598 \*[Gt]\*[Gt]\*[Gt]\*[Am]0 leshort 0x184 for DEC Alpha
601 Indirect and relative offsets can be combined:
602 .Bd -literal -offset indent
604 \*[Gt]0x18 leshort \*[Lt]0x40
605 \*[Gt]\*[Gt](4.s*512) leshort !0x014c MZ executable (MS-DOS)
606 # if it's not COFF, go back 512 bytes and add the offset taken
607 # from byte 2/3, which is yet another way of finding the start
608 # of the extended executable
609 \*[Gt]\*[Gt]\*[Gt]\*[Am](2.s-514) string LE LE executable (MS Windows VxD driver)
612 Or the other way around:
613 .Bd -literal -offset indent
615 \*[Gt]0x18 leshort \*[Gt]0x3f
616 \*[Gt]\*[Gt](0x3c.l) string LE\e0\e0 LE executable (MS-Windows)
617 # at offset 0x80 (-4, since relative offsets start at the end
618 # of the up-level match) inside the LE header, we find the absolute
619 # offset to the code area, where we look for a specific signature
620 \*[Gt]\*[Gt]\*[Gt](\*[Am]0x7c.l+0x26) string UPX \eb, UPX compressed
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 0x58 inside the LE header, we find the relative offset
629 # to a data area where we look for a specific signature
630 \*[Gt]\*[Gt]\*[Gt]\*[Am](\*[Am]0x54.l-3) string UNACE \eb, ACE self-extracting archive
633 If you have to deal with offset/length pairs in your file, even the
634 second value in a parenthesized expression can be taken from the file itself,
635 using another set of parentheses.
636 Note that this additional indirect offset is always relative to the
637 start of the main indirect offset.
638 .Bd -literal -offset indent
640 \*[Gt]0x18 leshort \*[Gt]0x3f
641 \*[Gt]\*[Gt](0x3c.l) string PE\e0\e0 PE executable (MS-Windows)
642 # search for the PE section called ".idata"...
643 \*[Gt]\*[Gt]\*[Gt]\*[Am]0xf4 search/0x140 .idata
644 # ...and go to the end of it, calculated from start+length;
645 # these are located 14 and 10 bytes after the section name
646 \*[Gt]\*[Gt]\*[Gt]\*[Gt](\*[Am]0xe.l+(-4)) string PK\e3\e4 \eb, ZIP self-extracting archive
649 If you have a list of known avalues at a particular continuation level,
650 and you want to provide a switch-like default case:
651 .Bd -literal -offset indent
652 # clear that continuation level match
654 \*[Gt]18 lelong 1 one
655 \*[Gt]18 lelong 2 two
657 # print default match
658 \*[Gt]\*[Gt]18 lelong x unmatched 0x%x
661 .Xr file __CSECTION__
662 \- the command that reads this file.
673 do not depend on the length of the C data types
677 on the platform, even though the Single
679 Specification implies that they do. However, as OS X Mountain Lion has
682 Specification validation suite, and supplies a version of
683 .Xr file __CSECTION__
684 in which they do not depend on the sizes of the C data types and that is
685 built for a 64-bit environment in which
687 is 8 bytes rather than 4 bytes, presumably the validation suite does not
688 test whether, for example
690 refers to an item with the same size as the C data type
692 There should probably be
704 and specified-byte-order variants of them,
705 to make it clearer that those types have specified widths.
707 .\" From: guy@sun.uucp (Guy Harris)
708 .\" Newsgroups: net.bugs.usg
709 .\" Subject: /etc/magic's format isn't well documented
710 .\" Message-ID: <2752@sun.uucp>
711 .\" Date: 3 Sep 85 08:19:07 GMT
712 .\" Organization: Sun Microsystems, Inc.
715 .\" Here's a manual page for the format accepted by the "file" made by adding
716 .\" the changes I posted to the S5R2 version.
718 .\" Modified for Ian Darwin's version of the file command.