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

SEE ALSO

365     file(1) - the command that reads this file.
366

BUGS

368     The formats long, belong, lelong, melong, short, beshort, leshort, date,
369     bedate, medate, ledate, beldate, leldate, and meldate are system-depen‐
370     dent; perhaps they should be specified as a number of bytes (2B, 4B,
371     etc), since the files being recognized typically come from a system on
372     which the lengths are invariant.
373
374BSD                             August 30, 2008                            BSD
Impressum