1MAGIC(5)                    BSD File Formats Manual                   MAGIC(5)
2

NAME

4     magic — file command's magic pattern file
5

DESCRIPTION

7     This manual page documents the format of the magic file as used by the
8     file(1) command, version 5.11.  The file(1) command identifies the type
9     of a file using, among other tests, a test for whether the file contains
10     certain “magic patterns”.  The file /usr/share/misc/magic specifies what
11     patterns are to be tested for, what message or MIME type to print if a
12     particular pattern is found, and additional information to extract from
13     the file.
14
15     Each line of the file specifies a test to be performed.  A test compares
16     the data starting at a particular offset in the file with a byte value, a
17     string or a numeric value.  If the test succeeds, a message is printed.
18     The line consists of the following fields:
19
20     offset   A number specifying the offset, in bytes, into the file of the
21              data which is to be tested.
22
23     type     The type of the data to be tested.  The possible values are:
24
25              byte        A one-byte value.
26
27              short       A two-byte value in this machine's native byte
28                          order.
29
30              long        A four-byte value in this machine's native byte
31                          order.
32
33              quad        An eight-byte value in this machine's native byte
34                          order.
35
36              float       A 32-bit single precision IEEE floating point number
37                          in this machine's native byte order.
38
39              double      A 64-bit double precision IEEE floating point number
40                          in this machine's native byte order.
41
42              string      A string of bytes.  The string type specification
43                          can be optionally followed by /[WwcCtb]*.  The “W”
44                          flag compacts whitespace in the target, which must
45                          contain at least one whitespace character.  If the
46                          magic has n consecutive blanks, the target needs at
47                          least n consecutive blanks to match.  The “w” flag
48                          treats every blank in the magic as an optional
49                          blank.  The “c” flag specifies case insensitive
50                          matching: lower case characters in the magic match
51                          both lower and upper case characters in the target,
52                          whereas upper case characters in the magic only
53                          match upper case characters in the target.  The “C”
54                          flag specifies case insensitive matching: upper case
55                          characters in the magic match both lower and upper
56                          case characters in the target, whereas lower case
57                          characters in the magic only match upper case char‐
58                          acters in the target.  To do a complete case insen‐
59                          sitive match, specify both “c” and “C”.  The “t”
60                          flag forces the test to be done for text files,
61                          while the “b” flag forces the test to be done for
62                          binary files.
63
64              pstring     A Pascal-style string where the first byte/short/int
65                          is interpreted as the an unsigned length.  The
66                          length defaults to byte and can be specified as a
67                          modifier.  The following modifiers are supported:
68                          B  A byte length (default).
69                          H  A 2 byte big endian length.
70                          h  A 2 byte big little length.
71                          L  A 4 byte big endian length.
72                          l  A 4 byte big little length.
73                          J  The length includes itself in its count.
74                          The string is not NUL terminated.  “J” is used
75                          rather than the more valuable “I” because this type
76                          of length is a feature of the JPEG format.
77
78              date        A four-byte value interpreted as a UNIX date.
79
80              qdate       A eight-byte value interpreted as a UNIX date.
81
82              ldate       A four-byte value interpreted as a UNIX-style date,
83                          but interpreted as local time rather than UTC.
84
85              qldate      An eight-byte value interpreted as a UNIX-style
86                          date, but interpreted as local time rather than UTC.
87
88              beid3       A 32-bit ID3 length in big-endian byte order.
89
90              beshort     A two-byte value in big-endian byte order.
91
92              belong      A four-byte value in big-endian byte order.
93
94              bequad      An eight-byte value in big-endian byte order.
95
96              befloat     A 32-bit single precision IEEE floating point number
97                          in big-endian byte order.
98
99              bedouble    A 64-bit double precision IEEE floating point number
100                          in big-endian byte order.
101
102              bedate      A four-byte value in big-endian byte order, inter‐
103                          preted as a Unix date.
104
105              beqdate     An eight-byte value in big-endian byte order, inter‐
106                          preted as a Unix date.
107
108              beldate     A four-byte value in big-endian byte order, inter‐
109                          preted as a UNIX-style date, but interpreted as
110                          local time rather than UTC.
111
112              beqldate    An eight-byte value in big-endian byte order, inter‐
113                          preted as a UNIX-style date, but interpreted as
114                          local time rather than UTC.
115
116              bestring16  A two-byte unicode (UCS16) string in big-endian byte
117                          order.
118
119              leid3       A 32-bit ID3 length in little-endian byte order.
120
121              leshort     A two-byte value in little-endian byte order.
122
123              lelong      A four-byte value in little-endian byte order.
124
125              lequad      An eight-byte value in little-endian byte order.
126
127              lefloat     A 32-bit single precision IEEE floating point number
128                          in little-endian byte order.
129
130              ledouble    A 64-bit double precision IEEE floating point number
131                          in little-endian byte order.
132
133              ledate      A four-byte value in little-endian byte order,
134                          interpreted as a UNIX date.
135
136              leqdate     An eight-byte value in little-endian byte order,
137                          interpreted as a UNIX date.
138
139              leldate     A four-byte value in little-endian byte order,
140                          interpreted as a UNIX-style date, but interpreted as
141                          local time rather than UTC.
142
143              leqldate    An eight-byte value in little-endian byte order,
144                          interpreted as a UNIX-style date, but interpreted as
145                          local time rather than UTC.
146
147              lestring16  A two-byte unicode (UCS16) string in little-endian
148                          byte order.
149
150              melong      A four-byte value in middle-endian (PDP-11) byte
151                          order.
152
153              medate      A four-byte value in middle-endian (PDP-11) byte
154                          order, interpreted as a UNIX date.
155
156              meldate     A four-byte value in middle-endian (PDP-11) byte
157                          order, interpreted as a UNIX-style date, but inter‐
158                          preted as local time rather than UTC.
159
160              indirect    Starting at the given offset, consult the magic
161                          database again.
162
163              regex       A regular expression match in extended POSIX regular
164                          expression syntax (like egrep).  Regular expressions
165                          can take exponential time to process, and their per‐
166                          formance is hard to predict, so their use is dis‐
167                          couraged.  When used in production environments,
168                          their performance should be carefully checked.  The
169                          type specification can be optionally followed by
170                          /[c][s].  The “c” flag makes the match case insensi‐
171                          tive, while the “s” flag update the offset to the
172                          start offset of the match, rather than the end.  The
173                          regular expression is tested against line N + 1
174                          onwards, where N is the given offset.  Line endings
175                          are assumed to be in the machine's native format.  ^
176                          and $ match the beginning and end of individual
177                          lines, respectively, not beginning and end of file.
178
179              search      A literal string search starting at the given off‐
180                          set.  The same modifier flags can be used as for
181                          string patterns.  The modifier flags (if any) must
182                          be followed by /number the range, that is, the num‐
183                          ber of positions at which the match will be
184                          attempted, starting from the start offset.  This is
185                          suitable for searching larger binary expressions
186                          with variable offsets, using \ escapes for special
187                          characters.  The offset works as for regex.
188
189              default     This is intended to be used with the test x (which
190                          is always true) and a message that is to be used if
191                          there are no other matches.
192
193              Each top-level magic pattern (see below for an explanation of
194              levels) is classified as text or binary according to the types
195              used.  Types “regex” and “search” are classified as text tests,
196              unless non-printable characters are used in the pattern.  All
197              other tests are classified as binary.  A top-level pattern is
198              considered to be a test text when all its patterns are text pat‐
199              terns; otherwise, it is considered to be a binary pattern.  When
200              matching a file, binary patterns are tried first; if no match is
201              found, and the file looks like text, then its encoding is deter‐
202              mined and the text patterns are tried.
203
204              The numeric types may optionally be followed by & and a numeric
205              value, to specify that the value is to be AND'ed with the
206              numeric value before any comparisons are done.  Prepending a u
207              to the type indicates that ordered comparisons should be
208              unsigned.
209
210     test     The value to be compared with the value from the file.  If the
211              type is numeric, this value is specified in C form; if it is a
212              string, it is specified as a C string with the usual escapes
213              permitted (e.g. \n for new-line).
214
215              Numeric values may be preceded by a character indicating the
216              operation to be performed.  It may be =, to specify that the
217              value from the file must equal the specified value, <, to spec‐
218              ify that the value from the file must be less than the specified
219              value, >, to specify that the value from the file must be
220              greater than the specified value, &, to specify that the value
221              from the file must have set all of the bits that are set in the
222              specified value, ^, to specify that the value from the file must
223              have clear any of the bits that are set in the specified value,
224              or ~, the value specified after is negated before tested.  x, to
225              specify that any value will match.  If the character is omitted,
226              it is assumed to be =.  Operators &, ^, and ~ don't work with
227              floats and doubles.  The operator ! specifies that the line
228              matches if the test does not succeed.
229
230              Numeric values are specified in C form; e.g.  13 is decimal, 013
231              is octal, and 0x13 is hexadecimal.
232
233              For string values, the string from the file must match the spec‐
234              ified string.  The operators =, < and > (but not &) can be
235              applied to strings.  The length used for matching is that of the
236              string argument in the magic file.  This means that a line can
237              match any non-empty string (usually used to then print the
238              string), with >\0 (because all non-empty strings are greater
239              than the empty string).
240
241              The special test x always evaluates to true.
242
243     message  The message to be printed if the comparison succeeds.  If the
244              string contains a printf(3) format specification, the value from
245              the file (with any specified masking performed) is printed using
246              the message as the format string.  If the string begins with
247              “\b”, the message printed is the remainder of the string with no
248              whitespace added before it: multiple matches are normally sepa‐
249              rated by a single space.
250
251     An APPLE 4+4 character APPLE creator and type can be specified as:
252
253           !:apple CREATYPE
254
255     A MIME type is given on a separate line, which must be the next non-blank
256     or comment line after the magic line that identifies the file type, and
257     has the following format:
258
259           !:mime  MIMETYPE
260
261     i.e. the literal string “!:mime” followed by the MIME type.
262
263     An optional strength can be supplied on a separate line which refers to
264     the current magic description using the following format:
265
266           !:strength OP VALUE
267
268     The operand OP can be: +, -, *, or / and VALUE is a constant between 0
269     and 255.  This constant is applied using the specified operand to the
270     currently computed default magic strength.
271
272     Some file formats contain additional information which is to be printed
273     along with the file type or need additional tests to determine the true
274     file type.  These additional tests are introduced by one or more > char‐
275     acters preceding the offset.  The number of > on the line indicates the
276     level of the test; a line with no > at the beginning is considered to be
277     at level 0.  Tests are arranged in a tree-like hierarchy: if the test on
278     a line at level n succeeds, all following tests at level n+1 are per‐
279     formed, and the messages printed if the tests succeed, until a line with
280     level n (or less) appears.  For more complex files, one can use empty
281     messages to get just the "if/then" effect, in the following way:
282
283           0      string   MZ
284           >0x18  leshort  <0x40   MS-DOS executable
285           >0x18  leshort  >0x3f   extended PC executable (e.g., MS Windows)
286
287     Offsets do not need to be constant, but can also be read from the file
288     being examined.  If the first character following the last > is a ( then
289     the string after the parenthesis is interpreted as an indirect offset.
290     That means that the number after the parenthesis is used as an offset in
291     the file.  The value at that offset is read, and is used again as an off‐
292     set in the file.  Indirect offsets are of the form: (( x
293     [.[bislBISL]][+-][ y ]).  The value of x is used as an offset in the
294     file.  A byte, id3 length, short or long is read at that offset depending
295     on the [bislBISLm] type specifier.  The capitalized types interpret the
296     number as a big endian value, whereas the small letter versions interpret
297     the number as a little endian value; the m type interprets the number as
298     a middle endian (PDP-11) value.  To that number the value of y is added
299     and the result is used as an offset in the file.  The default type if one
300     is not specified is long.
301
302     That way variable length structures can be examined:
303
304           # MS Windows executables are also valid MS-DOS executables
305           0           string  MZ
306           >0x18       leshort <0x40   MZ executable (MS-DOS)
307           # skip the whole block below if it is not an extended executable
308           >0x18       leshort >0x3f
309           >>(0x3c.l)  string  PE\0\0  PE executable (MS-Windows)
310           >>(0x3c.l)  string  LX\0\0  LX executable (OS/2)
311
312     This strategy of examining has a drawback: You must make sure that you
313     eventually print something, or users may get empty output (like, when
314     there is neither PE\0\0 nor LE\0\0 in the above example)
315
316     If this indirect offset cannot be used directly, simple calculations are
317     possible: appending [+-*/%&|^]number inside parentheses allows one to
318     modify the value read from the file before it is used as an offset:
319
320           # MS Windows executables are also valid MS-DOS executables
321           0           string  MZ
322           # sometimes, the value at 0x18 is less that 0x40 but there's still an
323           # extended executable, simply appended to the file
324           >0x18       leshort <0x40
325           >>(4.s*512) leshort 0x014c  COFF executable (MS-DOS, DJGPP)
326           >>(4.s*512) leshort !0x014c MZ executable (MS-DOS)
327
328     Sometimes you do not know the exact offset as this depends on the length
329     or position (when indirection was used before) of preceding fields.  You
330     can specify an offset relative to the end of the last up-level field
331     using ‘&’ as a prefix to the offset:
332
333           0           string  MZ
334           >0x18       leshort >0x3f
335           >>(0x3c.l)  string  PE\0\0    PE executable (MS-Windows)
336           # immediately following the PE signature is the CPU type
337           >>>&0       leshort 0x14c     for Intel 80386
338           >>>&0       leshort 0x184     for DEC Alpha
339
340     Indirect and relative offsets can be combined:
341
342           0             string  MZ
343           >0x18         leshort <0x40
344           >>(4.s*512)   leshort !0x014c MZ executable (MS-DOS)
345           # if it's not COFF, go back 512 bytes and add the offset taken
346           # from byte 2/3, which is yet another way of finding the start
347           # of the extended executable
348           >>>&(2.s-514) string  LE      LE executable (MS Windows VxD driver)
349
350     Or the other way around:
351
352           0                 string  MZ
353           >0x18             leshort >0x3f
354           >>(0x3c.l)        string  LE\0\0  LE executable (MS-Windows)
355           # at offset 0x80 (-4, since relative offsets start at the end
356           # of the up-level match) inside the LE header, we find the absolute
357           # offset to the code area, where we look for a specific signature
358           >>>(&0x7c.l+0x26) string  UPX     \b, UPX compressed
359
360     Or even both!
361
362           0                string  MZ
363           >0x18            leshort >0x3f
364           >>(0x3c.l)       string  LE\0\0 LE executable (MS-Windows)
365           # at offset 0x58 inside the LE header, we find the relative offset
366           # to a data area where we look for a specific signature
367           >>>&(&0x54.l-3)  string  UNACE  \b, ACE self-extracting archive
368
369     Finally, if you have to deal with offset/length pairs in your file, even
370     the second value in a parenthesized expression can be taken from the file
371     itself, using another set of parentheses.  Note that this additional
372     indirect offset is always relative to the start of the main indirect off‐
373     set.
374
375           0                 string       MZ
376           >0x18             leshort      >0x3f
377           >>(0x3c.l)        string       PE\0\0 PE executable (MS-Windows)
378           # search for the PE section called ".idata"...
379           >>>&0xf4          search/0x140 .idata
380           # ...and go to the end of it, calculated from start+length;
381           # these are located 14 and 10 bytes after the section name
382           >>>>(&0xe.l+(-4)) string       PK\3\4 \b, ZIP self-extracting archive
383

SEE ALSO

385     file(1) - the command that reads this file.
386

BUGS

388     The formats long, belong, lelong, melong, short, beshort, leshort, date,
389     bedate, medate, ledate, beldate, leldate, and meldate are system-depen‐
390     dent; perhaps they should be specified as a number of bytes (2B, 4B,
391     etc), since the files being recognized typically come from a system on
392     which the lengths are invariant.
393
394BSD                             April 20, 2011                             BSD
Impressum