xref: /dragonfly/contrib/file/doc/magic.man (revision 52f9f0d9)
1.\" $File: magic.man,v 1.71 2011/12/07 11:58:24 rrt Exp $
2.Dd April 20, 2011
3.Dt MAGIC __FSECTION__
4.Os
5.\" install as magic.4 on USG, magic.5 on V7, Berkeley and Linux systems.
6.Sh NAME
7.Nm magic
8.Nd file command's magic pattern file
9.Sh DESCRIPTION
10This manual page documents the format of the magic file as
11used by the
12.Xr file __CSECTION__
13command, version __VERSION__.
14The
15.Xr file __CSECTION__
16command identifies the type of a file using,
17among other tests,
18a test for whether the file contains certain
19.Dq "magic patterns" .
20The file
21.Pa __MAGIC__
22specifies what patterns are to be tested for, what message or
23MIME type to print if a particular pattern is found,
24and additional information to extract from the file.
25.Pp
26Each line of the file specifies a test to be performed.
27A test compares the data starting at a particular offset
28in the file with a byte value, a string or a numeric value.
29If the test succeeds, a message is printed.
30The line consists of the following fields:
31.Bl -tag -width ".Dv message"
32.It Dv offset
33A number specifying the offset, in bytes, into the file of the data
34which is to be tested.
35.It Dv type
36The type of the data to be tested.
37The possible values are:
38.Bl -tag -width ".Dv lestring16"
39.It Dv byte
40A one-byte value.
41.It Dv short
42A two-byte value in this machine's native byte order.
43.It Dv long
44A four-byte value in this machine's native byte order.
45.It Dv quad
46An eight-byte value in this machine's native byte order.
47.It Dv float
48A 32-bit single precision IEEE floating point number in this machine's native byte order.
49.It Dv double
50A 64-bit double precision IEEE floating point number in this machine's native byte order.
51.It Dv string
52A string of bytes.
53The string type specification can be optionally followed
54by /[WwcCtb]*.
55The
56.Dq W
57flag compacts whitespace in the target, which must
58contain at least one whitespace character.
59If the magic has
60.Dv n
61consecutive blanks, the target needs at least
62.Dv n
63consecutive blanks to match.
64The
65.Dq w
66flag treats every blank in the magic as an optional blank.
67The
68.Dq c
69flag specifies case insensitive matching: lower case
70characters in the magic match both lower and upper case characters in the
71target, whereas upper case characters in the magic only match upper case
72characters in the target.
73The
74.Dq C
75flag specifies case insensitive matching: upper case
76characters in the magic match both lower and upper case characters in the
77target, whereas lower case characters in the magic only match upper case
78characters in the target.
79To do a complete case insensitive match, specify both
80.Dq c
81and
82.Dq C .
83The
84.Dq t
85flag forces the test to be done for text files, while the
86.Dq b
87flag forces the test to be done for binary files.
88.It Dv pstring
89A Pascal-style string where the first byte/short/int is interpreted as the an
90unsigned length.
91The length defaults to byte and can be specified as a modifier.
92The following modifiers are supported:
93.Bl -tag -compact -width B
94.It B
95A byte length (default).
96.It H
97A 2 byte big endian length.
98.It h
99A 2 byte big little length.
100.It L
101A 4 byte big endian length.
102.It l
103A 4 byte big little length.
104.It J
105The length includes itself in its count.
106.El
107The string is not NUL terminated.
108.Dq J
109is used rather than the more
110valuable
111.Dq I
112because this type of length is a feature of the JPEG
113format.
114.It Dv date
115A four-byte value interpreted as a UNIX date.
116.It Dv qdate
117A eight-byte value interpreted as a UNIX date.
118.It Dv ldate
119A four-byte value interpreted as a UNIX-style date, but interpreted as
120local time rather than UTC.
121.It Dv qldate
122An eight-byte value interpreted as a UNIX-style date, but interpreted as
123local time rather than UTC.
124.It Dv beid3
125A 32-bit ID3 length in big-endian byte order.
126.It Dv beshort
127A two-byte value in big-endian byte order.
128.It Dv belong
129A four-byte value in big-endian byte order.
130.It Dv bequad
131An eight-byte value in big-endian byte order.
132.It Dv befloat
133A 32-bit single precision IEEE floating point number in big-endian byte order.
134.It Dv bedouble
135A 64-bit double precision IEEE floating point number in big-endian byte order.
136.It Dv bedate
137A four-byte value in big-endian byte order,
138interpreted as a Unix date.
139.It Dv beqdate
140An eight-byte value in big-endian byte order,
141interpreted as a Unix date.
142.It Dv beldate
143A four-byte value in big-endian byte order,
144interpreted as a UNIX-style date, but interpreted as local time rather
145than UTC.
146.It Dv beqldate
147An eight-byte value in big-endian byte order,
148interpreted as a UNIX-style date, but interpreted as local time rather
149than UTC.
150.It Dv bestring16
151A two-byte unicode (UCS16) string in big-endian byte order.
152.It Dv leid3
153A 32-bit ID3 length in little-endian byte order.
154.It Dv leshort
155A two-byte value in little-endian byte order.
156.It Dv lelong
157A four-byte value in little-endian byte order.
158.It Dv lequad
159An eight-byte value in little-endian byte order.
160.It Dv lefloat
161A 32-bit single precision IEEE floating point number in little-endian byte order.
162.It Dv ledouble
163A 64-bit double precision IEEE floating point number in little-endian byte order.
164.It Dv ledate
165A four-byte value in little-endian byte order,
166interpreted as a UNIX date.
167.It Dv leqdate
168An eight-byte value in little-endian byte order,
169interpreted as a UNIX date.
170.It Dv leldate
171A four-byte value in little-endian byte order,
172interpreted as a UNIX-style date, but interpreted as local time rather
173than UTC.
174.It Dv leqldate
175An eight-byte value in little-endian byte order,
176interpreted as a UNIX-style date, but interpreted as local time rather
177than UTC.
178.It Dv lestring16
179A two-byte unicode (UCS16) string in little-endian byte order.
180.It Dv melong
181A four-byte value in middle-endian (PDP-11) byte order.
182.It Dv medate
183A four-byte value in middle-endian (PDP-11) byte order,
184interpreted as a UNIX date.
185.It Dv meldate
186A four-byte value in middle-endian (PDP-11) byte order,
187interpreted as a UNIX-style date, but interpreted as local time rather
188than UTC.
189.It Dv indirect
190Starting at the given offset, consult the magic database again.
191.It Dv regex
192A regular expression match in extended POSIX regular expression syntax
193(like egrep).
194Regular expressions can take exponential time to process, and their
195performance is hard to predict, so their use is discouraged.
196When used in production environments, their performance
197should be carefully checked.
198The type specification can be optionally followed by
199.Dv /[c][s] .
200The
201.Dq c
202flag makes the match case insensitive, while the
203.Dq s
204flag update the offset to the start offset of the match, rather than the end.
205The regular expression is tested against line
206.Dv N + 1
207onwards, where
208.Dv N
209is the given offset.
210Line endings are assumed to be in the machine's native format.
211.Dv ^
212and
213.Dv $
214match the beginning and end of individual lines, respectively,
215not beginning and end of file.
216.It Dv search
217A literal string search starting at the given offset.
218The same modifier flags can be used as for string patterns.
219The modifier flags (if any) must be followed by
220.Dv /number
221the range, that is, the number of positions at which the match will be
222attempted, starting from the start offset.
223This is suitable for
224searching larger binary expressions with variable offsets, using
225.Dv \e
226escapes for special characters.
227The offset works as for regex.
228.It Dv default
229This is intended to be used with the test
230.Em x
231(which is always true) and a message that is to be used if there are
232no other matches.
233.El
234.Pp
235Each top-level magic pattern (see below for an explanation of levels)
236is classified as text or binary according to the types used.
237Types
238.Dq regex
239and
240.Dq search
241are classified as text tests, unless non-printable characters are used
242in the pattern.
243All other tests are classified as binary.
244A top-level
245pattern is considered to be a test text when all its patterns are text
246patterns; otherwise, it is considered to be a binary pattern.
247When
248matching a file, binary patterns are tried first; if no match is
249found, and the file looks like text, then its encoding is determined
250and the text patterns are tried.
251.Pp
252The numeric types may optionally be followed by
253.Dv \*[Am]
254and a numeric value,
255to specify that the value is to be AND'ed with the
256numeric value before any comparisons are done.
257Prepending a
258.Dv u
259to the type indicates that ordered comparisons should be unsigned.
260.It Dv test
261The value to be compared with the value from the file.
262If the type is
263numeric, this value
264is specified in C form; if it is a string, it is specified as a C string
265with the usual escapes permitted (e.g. \en for new-line).
266.Pp
267Numeric values
268may be preceded by a character indicating the operation to be performed.
269It may be
270.Dv = ,
271to specify that the value from the file must equal the specified value,
272.Dv \*[Lt] ,
273to specify that the value from the file must be less than the specified
274value,
275.Dv \*[Gt] ,
276to specify that the value from the file must be greater than the specified
277value,
278.Dv \*[Am] ,
279to specify that the value from the file must have set all of the bits
280that are set in the specified value,
281.Dv ^ ,
282to specify that the value from the file must have clear any of the bits
283that are set in the specified value, or
284.Dv ~ ,
285the value specified after is negated before tested.
286.Dv x ,
287to specify that any value will match.
288If the character is omitted, it is assumed to be
289.Dv = .
290Operators
291.Dv \*[Am] ,
292.Dv ^ ,
293and
294.Dv ~
295don't work with floats and doubles.
296The operator
297.Dv !\&
298specifies that the line matches if the test does
299.Em not
300succeed.
301.Pp
302Numeric values are specified in C form; e.g.
303.Dv 13
304is decimal,
305.Dv 013
306is octal, and
307.Dv 0x13
308is hexadecimal.
309.Pp
310For string values, the string from the
311file must match the specified string.
312The operators
313.Dv = ,
314.Dv \*[Lt]
315and
316.Dv \*[Gt]
317(but not
318.Dv \*[Am] )
319can be applied to strings.
320The length used for matching is that of the string argument
321in the magic file.
322This means that a line can match any non-empty string (usually used to
323then print the string), with
324.Em \*[Gt]\e0
325(because all non-empty strings are greater than the empty string).
326.Pp
327The special test
328.Em x
329always evaluates to true.
330.It Dv message
331The message to be printed if the comparison succeeds.
332If the string contains a
333.Xr printf 3
334format specification, the value from the file (with any specified masking
335performed) is printed using the message as the format string.
336If the string begins with
337.Dq \eb ,
338the message printed is the remainder of the string with no whitespace
339added before it: multiple matches are normally separated by a single
340space.
341.El
342.Pp
343An APPLE 4+4 character APPLE creator and type can be specified as:
344.Bd -literal -offset indent
345!:apple	CREATYPE
346.Ed
347.Pp
348A MIME type is given on a separate line, which must be the next
349non-blank or comment line after the magic line that identifies the
350file type, and has the following format:
351.Bd -literal -offset indent
352!:mime	MIMETYPE
353.Ed
354.Pp
355i.e. the literal string
356.Dq !:mime
357followed by the MIME type.
358.Pp
359An optional strength can be supplied on a separate line which refers to
360the current magic description using the following format:
361.Bd -literal -offset indent
362!:strength OP VALUE
363.Ed
364.Pp
365The operand
366.Dv OP
367can be:
368.Dv + ,
369.Dv - ,
370.Dv * ,
371or
372.Dv /
373and
374.Dv VALUE
375is a constant between 0 and 255.
376This constant is applied using the specified operand
377to the currently computed default magic strength.
378.Pp
379Some file formats contain additional information which is to be printed
380along with the file type or need additional tests to determine the true
381file type.
382These additional tests are introduced by one or more
383.Em \*[Gt]
384characters preceding the offset.
385The number of
386.Em \*[Gt]
387on the line indicates the level of the test; a line with no
388.Em \*[Gt]
389at the beginning is considered to be at level 0.
390Tests are arranged in a tree-like hierarchy:
391if the test on a line at level
392.Em n
393succeeds, all following tests at level
394.Em n+1
395are performed, and the messages printed if the tests succeed, until a line
396with level
397.Em n
398(or less) appears.
399For more complex files, one can use empty messages to get just the
400"if/then" effect, in the following way:
401.Bd -literal -offset indent
4020      string   MZ
403\*[Gt]0x18  leshort  \*[Lt]0x40   MS-DOS executable
404\*[Gt]0x18  leshort  \*[Gt]0x3f   extended PC executable (e.g., MS Windows)
405.Ed
406.Pp
407Offsets do not need to be constant, but can also be read from the file
408being examined.
409If the first character following the last
410.Em \*[Gt]
411is a
412.Em \&(
413then the string after the parenthesis is interpreted as an indirect offset.
414That means that the number after the parenthesis is used as an offset in
415the file.
416The value at that offset is read, and is used again as an offset
417in the file.
418Indirect offsets are of the form:
419.Em (( x [.[bislBISL]][+\-][ y ]) .
420The value of
421.Em x
422is used as an offset in the file.
423A byte, id3 length, short or long is read at that offset depending on the
424.Em [bislBISLm]
425type specifier.
426The capitalized types interpret the number as a big endian
427value, whereas the small letter versions interpret the number as a little
428endian value;
429the
430.Em m
431type interprets the number as a middle endian (PDP-11) value.
432To that number the value of
433.Em y
434is added and the result is used as an offset in the file.
435The default type if one is not specified is long.
436.Pp
437That way variable length structures can be examined:
438.Bd -literal -offset indent
439# MS Windows executables are also valid MS-DOS executables
4400           string  MZ
441\*[Gt]0x18       leshort \*[Lt]0x40   MZ executable (MS-DOS)
442# skip the whole block below if it is not an extended executable
443\*[Gt]0x18       leshort \*[Gt]0x3f
444\*[Gt]\*[Gt](0x3c.l)  string  PE\e0\e0  PE executable (MS-Windows)
445\*[Gt]\*[Gt](0x3c.l)  string  LX\e0\e0  LX executable (OS/2)
446.Ed
447.Pp
448This strategy of examining has a drawback: You must make sure that
449you eventually print something, or users may get empty output (like, when
450there is neither PE\e0\e0 nor LE\e0\e0 in the above example)
451.Pp
452If this indirect offset cannot be used directly, simple calculations are
453possible: appending
454.Em [+-*/%\*[Am]|^]number
455inside parentheses allows one to modify
456the value read from the file before it is used as an offset:
457.Bd -literal -offset indent
458# MS Windows executables are also valid MS-DOS executables
4590           string  MZ
460# sometimes, the value at 0x18 is less that 0x40 but there's still an
461# extended executable, simply appended to the file
462\*[Gt]0x18       leshort \*[Lt]0x40
463\*[Gt]\*[Gt](4.s*512) leshort 0x014c  COFF executable (MS-DOS, DJGPP)
464\*[Gt]\*[Gt](4.s*512) leshort !0x014c MZ executable (MS-DOS)
465.Ed
466.Pp
467Sometimes you do not know the exact offset as this depends on the length or
468position (when indirection was used before) of preceding fields.
469You can specify an offset relative to the end of the last up-level
470field using
471.Sq \*[Am]
472as a prefix to the offset:
473.Bd -literal -offset indent
4740           string  MZ
475\*[Gt]0x18       leshort \*[Gt]0x3f
476\*[Gt]\*[Gt](0x3c.l)  string  PE\e0\e0    PE executable (MS-Windows)
477# immediately following the PE signature is the CPU type
478\*[Gt]\*[Gt]\*[Gt]\*[Am]0       leshort 0x14c     for Intel 80386
479\*[Gt]\*[Gt]\*[Gt]\*[Am]0       leshort 0x184     for DEC Alpha
480.Ed
481.Pp
482Indirect and relative offsets can be combined:
483.Bd -literal -offset indent
4840             string  MZ
485\*[Gt]0x18         leshort \*[Lt]0x40
486\*[Gt]\*[Gt](4.s*512)   leshort !0x014c MZ executable (MS-DOS)
487# if it's not COFF, go back 512 bytes and add the offset taken
488# from byte 2/3, which is yet another way of finding the start
489# of the extended executable
490\*[Gt]\*[Gt]\*[Gt]\*[Am](2.s-514) string  LE      LE executable (MS Windows VxD driver)
491.Ed
492.Pp
493Or the other way around:
494.Bd -literal -offset indent
4950                 string  MZ
496\*[Gt]0x18             leshort \*[Gt]0x3f
497\*[Gt]\*[Gt](0x3c.l)        string  LE\e0\e0  LE executable (MS-Windows)
498# at offset 0x80 (-4, since relative offsets start at the end
499# of the up-level match) inside the LE header, we find the absolute
500# offset to the code area, where we look for a specific signature
501\*[Gt]\*[Gt]\*[Gt](\*[Am]0x7c.l+0x26) string  UPX     \eb, UPX compressed
502.Ed
503.Pp
504Or even both!
505.Bd -literal -offset indent
5060                string  MZ
507\*[Gt]0x18            leshort \*[Gt]0x3f
508\*[Gt]\*[Gt](0x3c.l)       string  LE\e0\e0 LE executable (MS-Windows)
509# at offset 0x58 inside the LE header, we find the relative offset
510# to a data area where we look for a specific signature
511\*[Gt]\*[Gt]\*[Gt]\*[Am](\*[Am]0x54.l-3)  string  UNACE  \eb, ACE self-extracting archive
512.Ed
513.Pp
514Finally, if you have to deal with offset/length pairs in your file, even the
515second value in a parenthesized expression can be taken from the file itself,
516using another set of parentheses.
517Note that this additional indirect offset is always relative to the
518start of the main indirect offset.
519.Bd -literal -offset indent
5200                 string       MZ
521\*[Gt]0x18             leshort      \*[Gt]0x3f
522\*[Gt]\*[Gt](0x3c.l)        string       PE\e0\e0 PE executable (MS-Windows)
523# search for the PE section called ".idata"...
524\*[Gt]\*[Gt]\*[Gt]\*[Am]0xf4          search/0x140 .idata
525# ...and go to the end of it, calculated from start+length;
526# these are located 14 and 10 bytes after the section name
527\*[Gt]\*[Gt]\*[Gt]\*[Gt](\*[Am]0xe.l+(-4)) string       PK\e3\e4 \eb, ZIP self-extracting archive
528.Ed
529.Sh SEE ALSO
530.Xr file __CSECTION__
531\- the command that reads this file.
532.Sh BUGS
533The formats
534.Dv long ,
535.Dv belong ,
536.Dv lelong ,
537.Dv melong ,
538.Dv short ,
539.Dv beshort ,
540.Dv leshort ,
541.Dv date ,
542.Dv bedate ,
543.Dv medate ,
544.Dv ledate ,
545.Dv beldate ,
546.Dv leldate ,
547and
548.Dv meldate
549are system-dependent; perhaps they should be specified as a number
550of bytes (2B, 4B, etc),
551since the files being recognized typically come from
552a system on which the lengths are invariant.
553.\"
554.\" From: guy@sun.uucp (Guy Harris)
555.\" Newsgroups: net.bugs.usg
556.\" Subject: /etc/magic's format isn't well documented
557.\" Message-ID: <2752@sun.uucp>
558.\" Date: 3 Sep 85 08:19:07 GMT
559.\" Organization: Sun Microsystems, Inc.
560.\" Lines: 136
561.\"
562.\" Here's a manual page for the format accepted by the "file" made by adding
563.\" the changes I posted to the S5R2 version.
564.\"
565.\" Modified for Ian Darwin's version of the file command.
566