xref: /dflybsd-src/contrib/file/doc/magic.man (revision 6fca56fb90a257291c53bba3b861b751027c3e3d)
1*6fca56fbSSascha Wildner.\" $File: magic.man,v 1.97 2019/11/15 21:03:14 christos Exp $
2*6fca56fbSSascha Wildner.Dd January 21, 2019
3327e51cbSPeter Avalos.Dt MAGIC __FSECTION__
4327e51cbSPeter Avalos.Os
579343712SPeter Avalos.\" install as magic.4 on USG, magic.5 on V7, Berkeley and Linux systems.
6327e51cbSPeter Avalos.Sh NAME
7327e51cbSPeter Avalos.Nm magic
879343712SPeter Avalos.Nd file command's magic pattern file
9327e51cbSPeter Avalos.Sh DESCRIPTION
10c30bd091SSascha WildnerThis manual page documents the format of magic files as
11327e51cbSPeter Avalosused by the
12327e51cbSPeter Avalos.Xr file __CSECTION__
13327e51cbSPeter Avaloscommand, version __VERSION__.
14327e51cbSPeter AvalosThe
15327e51cbSPeter Avalos.Xr file __CSECTION__
16327e51cbSPeter Avaloscommand identifies the type of a file using,
17327e51cbSPeter Avalosamong other tests,
1879343712SPeter Avalosa test for whether the file contains certain
1979343712SPeter Avalos.Dq "magic patterns" .
20c30bd091SSascha WildnerThe database of these
21c30bd091SSascha Wildner.Dq "magic patterns"
22c30bd091SSascha Wildneris usually located in a binary file in
23c30bd091SSascha Wildner.Pa __MAGIC__.mgc
24c30bd091SSascha Wildneror a directory of source text magic pattern fragment files in
25c30bd091SSascha Wildner.Pa __MAGIC__ .
26c30bd091SSascha WildnerThe database specifies what patterns are to be tested for, what message or
2779343712SPeter AvalosMIME type to print if a particular pattern is found,
28327e51cbSPeter Avalosand additional information to extract from the file.
29327e51cbSPeter Avalos.Pp
30c30bd091SSascha WildnerThe format of the source fragment files that are used to build this database
31c30bd091SSascha Wildneris as follows:
32c30bd091SSascha WildnerEach line of a fragment file specifies a test to be performed.
33327e51cbSPeter AvalosA test compares the data starting at a particular offset
3479343712SPeter Avalosin the file with a byte value, a string or a numeric value.
35327e51cbSPeter AvalosIf the test succeeds, a message is printed.
36327e51cbSPeter AvalosThe line consists of the following fields:
37327e51cbSPeter Avalos.Bl -tag -width ".Dv message"
38327e51cbSPeter Avalos.It Dv offset
39*6fca56fbSSascha WildnerA number specifying the offset (in bytes) into the file of the data
40327e51cbSPeter Avaloswhich is to be tested.
41*6fca56fbSSascha WildnerThis offset can be a negative number if it is:
42*6fca56fbSSascha Wildner.Bl -bullet  -compact
43*6fca56fbSSascha Wildner.It
44*6fca56fbSSascha WildnerThe first direct offset of the magic entry (at continuation level 0),
45*6fca56fbSSascha Wildnerin which case it is interpreted an offset from end end of the file
46*6fca56fbSSascha Wildnergoing backwards.
47*6fca56fbSSascha WildnerThis works only when a file descriptor to the file is available and it
48*6fca56fbSSascha Wildneris a regular file.
49*6fca56fbSSascha Wildner.It
50*6fca56fbSSascha WildnerA continuation offset relative to the end of the last up-level field
51*6fca56fbSSascha Wildner.Dv ( \*[Am] ) .
52*6fca56fbSSascha Wildner.El
53327e51cbSPeter Avalos.It Dv type
54327e51cbSPeter AvalosThe type of the data to be tested.
55327e51cbSPeter AvalosThe possible values are:
56327e51cbSPeter Avalos.Bl -tag -width ".Dv lestring16"
57327e51cbSPeter Avalos.It Dv byte
58327e51cbSPeter AvalosA one-byte value.
59327e51cbSPeter Avalos.It Dv short
6079343712SPeter AvalosA two-byte value in this machine's native byte order.
61327e51cbSPeter Avalos.It Dv long
6279343712SPeter AvalosA four-byte value in this machine's native byte order.
63327e51cbSPeter Avalos.It Dv quad
6479343712SPeter AvalosAn eight-byte value in this machine's native byte order.
65327e51cbSPeter Avalos.It Dv float
6679343712SPeter AvalosA 32-bit single precision IEEE floating point number in this machine's native byte order.
67327e51cbSPeter Avalos.It Dv double
6879343712SPeter AvalosA 64-bit double precision IEEE floating point number in this machine's native byte order.
69327e51cbSPeter Avalos.It Dv string
70327e51cbSPeter AvalosA string of bytes.
71327e51cbSPeter AvalosThe string type specification can be optionally followed
72e8af9738SPeter Avalosby /[WwcCtbT]*.
73327e51cbSPeter AvalosThe
74884044a5SPeter Avalos.Dq W
75327e51cbSPeter Avalosflag compacts whitespace in the target, which must
76327e51cbSPeter Avaloscontain at least one whitespace character.
77327e51cbSPeter AvalosIf the magic has
78327e51cbSPeter Avalos.Dv n
79327e51cbSPeter Avalosconsecutive blanks, the target needs at least
80327e51cbSPeter Avalos.Dv n
81327e51cbSPeter Avalosconsecutive blanks to match.
82327e51cbSPeter AvalosThe
83884044a5SPeter Avalos.Dq w
849f86ab30SPeter Avalosflag treats every blank in the magic as an optional blank.
85884044a5SPeter AvalosThe
86327e51cbSPeter Avalos.Dq c
879f86ab30SPeter Avalosflag specifies case insensitive matching: lower case
88327e51cbSPeter Avaloscharacters in the magic match both lower and upper case characters in the
8979343712SPeter Avalostarget, whereas upper case characters in the magic only match upper case
90327e51cbSPeter Avaloscharacters in the target.
91884044a5SPeter AvalosThe
92884044a5SPeter Avalos.Dq C
939f86ab30SPeter Avalosflag specifies case insensitive matching: upper case
94884044a5SPeter Avaloscharacters in the magic match both lower and upper case characters in the
95884044a5SPeter Avalostarget, whereas lower case characters in the magic only match upper case
96884044a5SPeter Avaloscharacters in the target.
97884044a5SPeter AvalosTo do a complete case insensitive match, specify both
98884044a5SPeter Avalos.Dq c
99884044a5SPeter Avalosand
100884044a5SPeter Avalos.Dq C .
101884044a5SPeter AvalosThe
102884044a5SPeter Avalos.Dq t
1039f86ab30SPeter Avalosflag forces the test to be done for text files, while the
104884044a5SPeter Avalos.Dq b
1059f86ab30SPeter Avalosflag forces the test to be done for binary files.
106e8af9738SPeter AvalosThe
107e8af9738SPeter Avalos.Dq T
108e8af9738SPeter Avalosflag causes the string to be trimmed, i.e. leading and trailing whitespace
109e8af9738SPeter Avalosis deleted before the string is printed.
110327e51cbSPeter Avalos.It Dv pstring
111e8af9738SPeter AvalosA Pascal-style string where the first byte/short/int is interpreted as the
112327e51cbSPeter Avalosunsigned length.
113e4d4ce0cSPeter AvalosThe length defaults to byte and can be specified as a modifier.
114e4d4ce0cSPeter AvalosThe following modifiers are supported:
115e4d4ce0cSPeter Avalos.Bl -tag -compact -width B
116e4d4ce0cSPeter Avalos.It B
117e4d4ce0cSPeter AvalosA byte length (default).
118e4d4ce0cSPeter Avalos.It H
119c30bd091SSascha WildnerA 2 byte big endian length.
120*6fca56fbSSascha Wildner.It h
121c30bd091SSascha WildnerA 2 byte little endian length.
122*6fca56fbSSascha Wildner.It L
123*6fca56fbSSascha WildnerA 4 byte big endian length.
124*6fca56fbSSascha Wildner.It l
125*6fca56fbSSascha WildnerA 4 byte little endian length.
126e4d4ce0cSPeter Avalos.It J
127e4d4ce0cSPeter AvalosThe length includes itself in its count.
128e4d4ce0cSPeter Avalos.El
129327e51cbSPeter AvalosThe string is not NUL terminated.
130e4d4ce0cSPeter Avalos.Dq J
131e4d4ce0cSPeter Avalosis used rather than the more
132e4d4ce0cSPeter Avalosvaluable
133e4d4ce0cSPeter Avalos.Dq I
134e4d4ce0cSPeter Avalosbecause this type of length is a feature of the JPEG
135e4d4ce0cSPeter Avalosformat.
136327e51cbSPeter Avalos.It Dv date
137327e51cbSPeter AvalosA four-byte value interpreted as a UNIX date.
138327e51cbSPeter Avalos.It Dv qdate
139*6fca56fbSSascha WildnerAn eight-byte value interpreted as a UNIX date.
140327e51cbSPeter Avalos.It Dv ldate
141327e51cbSPeter AvalosA four-byte value interpreted as a UNIX-style date, but interpreted as
142327e51cbSPeter Avaloslocal time rather than UTC.
143327e51cbSPeter Avalos.It Dv qldate
144327e51cbSPeter AvalosAn eight-byte value interpreted as a UNIX-style date, but interpreted as
145327e51cbSPeter Avaloslocal time rather than UTC.
146e8af9738SPeter Avalos.It Dv qwdate
147e8af9738SPeter AvalosAn eight-byte value interpreted as a Windows-style date.
14879343712SPeter Avalos.It Dv beid3
14979343712SPeter AvalosA 32-bit ID3 length in big-endian byte order.
150327e51cbSPeter Avalos.It Dv beshort
15179343712SPeter AvalosA two-byte value in big-endian byte order.
152327e51cbSPeter Avalos.It Dv belong
15379343712SPeter AvalosA four-byte value in big-endian byte order.
154327e51cbSPeter Avalos.It Dv bequad
15579343712SPeter AvalosAn eight-byte value in big-endian byte order.
156327e51cbSPeter Avalos.It Dv befloat
15779343712SPeter AvalosA 32-bit single precision IEEE floating point number in big-endian byte order.
158327e51cbSPeter Avalos.It Dv bedouble
15979343712SPeter AvalosA 64-bit double precision IEEE floating point number in big-endian byte order.
160327e51cbSPeter Avalos.It Dv bedate
16179343712SPeter AvalosA four-byte value in big-endian byte order,
162327e51cbSPeter Avalosinterpreted as a Unix date.
163327e51cbSPeter Avalos.It Dv beqdate
16479343712SPeter AvalosAn eight-byte value in big-endian byte order,
165327e51cbSPeter Avalosinterpreted as a Unix date.
166327e51cbSPeter Avalos.It Dv beldate
16779343712SPeter AvalosA four-byte value in big-endian byte order,
168327e51cbSPeter Avalosinterpreted as a UNIX-style date, but interpreted as local time rather
169327e51cbSPeter Avalosthan UTC.
170327e51cbSPeter Avalos.It Dv beqldate
17179343712SPeter AvalosAn eight-byte value in big-endian byte order,
172327e51cbSPeter Avalosinterpreted as a UNIX-style date, but interpreted as local time rather
173327e51cbSPeter Avalosthan UTC.
174e8af9738SPeter Avalos.It Dv beqwdate
175e8af9738SPeter AvalosAn eight-byte value in big-endian byte order,
176e8af9738SPeter Avalosinterpreted as a Windows-style date.
177327e51cbSPeter Avalos.It Dv bestring16
178327e51cbSPeter AvalosA two-byte unicode (UCS16) string in big-endian byte order.
17979343712SPeter Avalos.It Dv leid3
18079343712SPeter AvalosA 32-bit ID3 length in little-endian byte order.
181327e51cbSPeter Avalos.It Dv leshort
18279343712SPeter AvalosA two-byte value in little-endian byte order.
183327e51cbSPeter Avalos.It Dv lelong
18479343712SPeter AvalosA four-byte value in little-endian byte order.
185327e51cbSPeter Avalos.It Dv lequad
18679343712SPeter AvalosAn eight-byte value in little-endian byte order.
187327e51cbSPeter Avalos.It Dv lefloat
18879343712SPeter AvalosA 32-bit single precision IEEE floating point number in little-endian byte order.
189327e51cbSPeter Avalos.It Dv ledouble
19079343712SPeter AvalosA 64-bit double precision IEEE floating point number in little-endian byte order.
191327e51cbSPeter Avalos.It Dv ledate
19279343712SPeter AvalosA four-byte value in little-endian byte order,
193327e51cbSPeter Avalosinterpreted as a UNIX date.
194327e51cbSPeter Avalos.It Dv leqdate
19579343712SPeter AvalosAn eight-byte value in little-endian byte order,
196327e51cbSPeter Avalosinterpreted as a UNIX date.
197327e51cbSPeter Avalos.It Dv leldate
19879343712SPeter AvalosA four-byte value in little-endian byte order,
199327e51cbSPeter Avalosinterpreted as a UNIX-style date, but interpreted as local time rather
200327e51cbSPeter Avalosthan UTC.
201327e51cbSPeter Avalos.It Dv leqldate
20279343712SPeter AvalosAn eight-byte value in little-endian byte order,
203327e51cbSPeter Avalosinterpreted as a UNIX-style date, but interpreted as local time rather
204327e51cbSPeter Avalosthan UTC.
205e8af9738SPeter Avalos.It Dv leqwdate
206e8af9738SPeter AvalosAn eight-byte value in little-endian byte order,
207e8af9738SPeter Avalosinterpreted as a Windows-style date.
208327e51cbSPeter Avalos.It Dv lestring16
209327e51cbSPeter AvalosA two-byte unicode (UCS16) string in little-endian byte order.
210327e51cbSPeter Avalos.It Dv melong
21179343712SPeter AvalosA four-byte value in middle-endian (PDP-11) byte order.
212327e51cbSPeter Avalos.It Dv medate
21379343712SPeter AvalosA four-byte value in middle-endian (PDP-11) byte order,
214327e51cbSPeter Avalosinterpreted as a UNIX date.
215327e51cbSPeter Avalos.It Dv meldate
21679343712SPeter AvalosA four-byte value in middle-endian (PDP-11) byte order,
217327e51cbSPeter Avalosinterpreted as a UNIX-style date, but interpreted as local time rather
218327e51cbSPeter Avalosthan UTC.
21979343712SPeter Avalos.It Dv indirect
22079343712SPeter AvalosStarting at the given offset, consult the magic database again.
221c30bd091SSascha WildnerThe offset of the
22282c5fa3eSPeter Avalos.Dv indirect
22382c5fa3eSPeter Avalosmagic is by default absolute in the file, but one can specify
22482c5fa3eSPeter Avalos.Dv /r
22582c5fa3eSPeter Avalosto indicate that the offset is relative from the beginning of the entry.
226e8af9738SPeter Avalos.It Dv name
227e8af9738SPeter AvalosDefine a
228e8af9738SPeter Avalos.Dq named
229e8af9738SPeter Avalosmagic instance that can be called from another
230e8af9738SPeter Avalos.Dv use
231e8af9738SPeter Avalosmagic entry, like a subroutine call.
232e8af9738SPeter AvalosNamed instance direct magic offsets are relative to the offset of the
233e8af9738SPeter Avalosprevious matched entry, but indirect offsets are relative to the beginning
234e8af9738SPeter Avalosof the file as usual.
235e8af9738SPeter AvalosNamed magic entries always match.
236e8af9738SPeter Avalos.It Dv use
237e8af9738SPeter AvalosRecursively call the named magic starting from the current offset.
238e8af9738SPeter AvalosIf the name of the referenced begins with a
239e8af9738SPeter Avalos.Dv ^
240e8af9738SPeter Avalosthen the endianness of the magic is switched; if the magic mentioned
241e8af9738SPeter Avalos.Dv leshort
242e8af9738SPeter Avalosfor example,
243e8af9738SPeter Avalosit is treated as
244e8af9738SPeter Avalos.Dv beshort
245e8af9738SPeter Avalosand vice versa.
246e8af9738SPeter AvalosThis is useful to avoid duplicating the rules for different endianness.
247327e51cbSPeter Avalos.It Dv regex
248327e51cbSPeter AvalosA regular expression match in extended POSIX regular expression syntax
249a96e001bSPeter Avalos(like egrep).
250a96e001bSPeter AvalosRegular expressions can take exponential time to process, and their
251a96e001bSPeter Avalosperformance is hard to predict, so their use is discouraged.
252a96e001bSPeter AvalosWhen used in production environments, their performance
253a96e001bSPeter Avalosshould be carefully checked.
25482c5fa3eSPeter AvalosThe size of the string to search should also be limited by specifying
25582c5fa3eSPeter Avalos.Dv /<length> ,
25682c5fa3eSPeter Avalosto avoid performance issues scanning long files.
25782c5fa3eSPeter AvalosThe type specification can also be optionally followed by
25882c5fa3eSPeter Avalos.Dv /[c][s][l] .
259327e51cbSPeter AvalosThe
260327e51cbSPeter Avalos.Dq c
261327e51cbSPeter Avalosflag makes the match case insensitive, while the
262327e51cbSPeter Avalos.Dq s
26379343712SPeter Avalosflag update the offset to the start offset of the match, rather than the end.
26482c5fa3eSPeter AvalosThe
26582c5fa3eSPeter Avalos.Dq l
26682c5fa3eSPeter Avalosmodifier, changes the limit of length to mean number of lines instead of a
26782c5fa3eSPeter Avalosbyte count.
26882c5fa3eSPeter AvalosLines are delimited by the platforms native line delimiter.
26982c5fa3eSPeter AvalosWhen a line count is specified, an implicit byte count also computed assuming
27082c5fa3eSPeter Avaloseach line is 80 characters long.
27182c5fa3eSPeter AvalosIf neither a byte or line count is specified, the search is limited automatically
27282c5fa3eSPeter Avalosto 8KiB.
273327e51cbSPeter Avalos.Dv ^
274327e51cbSPeter Avalosand
275327e51cbSPeter Avalos.Dv $
27679343712SPeter Avalosmatch the beginning and end of individual lines, respectively,
277327e51cbSPeter Avalosnot beginning and end of file.
278327e51cbSPeter Avalos.It Dv search
279a96e001bSPeter AvalosA literal string search starting at the given offset.
280a96e001bSPeter AvalosThe same modifier flags can be used as for string patterns.
281e8af9738SPeter AvalosThe search expression must contain the range in the form
282e8af9738SPeter Avalos.Dv /number,
283e8af9738SPeter Avalosthat is the number of positions at which the match will be
284a96e001bSPeter Avalosattempted, starting from the start offset.
285a96e001bSPeter AvalosThis is suitable for
28679343712SPeter Avalossearching larger binary expressions with variable offsets, using
287327e51cbSPeter Avalos.Dv \e
288a96e001bSPeter Avalosescapes for special characters.
289e8af9738SPeter AvalosThe order of modifier and number is not relevant.
290327e51cbSPeter Avalos.It Dv default
29179343712SPeter AvalosThis is intended to be used with the test
29279343712SPeter Avalos.Em x
293e8af9738SPeter Avalos(which is always true) and it has no type.
294e8af9738SPeter AvalosIt matches when no other test at that continuation level has matched before.
295e8af9738SPeter AvalosClearing that matched tests for a continuation level, can be done using the
296e8af9738SPeter Avalos.Dv clear
297e8af9738SPeter Avalostest.
298e8af9738SPeter Avalos.It Dv clear
299e8af9738SPeter AvalosThis test is always true and clears the match flag for that continuation level.
300e8af9738SPeter AvalosIt is intended to be used with the
301e8af9738SPeter Avalos.Dv default
302e8af9738SPeter Avalostest.
303327e51cbSPeter Avalos.El
30479343712SPeter Avalos.Pp
305e8af9738SPeter AvalosFor compatibility with the Single
306e8af9738SPeter Avalos.Ux
307e8af9738SPeter AvalosStandard, the type specifiers
308e8af9738SPeter Avalos.Dv dC
309e8af9738SPeter Avalosand
310e8af9738SPeter Avalos.Dv d1
311e8af9738SPeter Avalosare equivalent to
312e8af9738SPeter Avalos.Dv byte ,
313e8af9738SPeter Avalosthe type specifiers
314e8af9738SPeter Avalos.Dv uC
315e8af9738SPeter Avalosand
316e8af9738SPeter Avalos.Dv u1
317e8af9738SPeter Avalosare equivalent to
318e8af9738SPeter Avalos.Dv ubyte ,
319e8af9738SPeter Avalosthe type specifiers
320e8af9738SPeter Avalos.Dv dS
321e8af9738SPeter Avalosand
322e8af9738SPeter Avalos.Dv d2
323e8af9738SPeter Avalosare equivalent to
324e8af9738SPeter Avalos.Dv short ,
325e8af9738SPeter Avalosthe type specifiers
326e8af9738SPeter Avalos.Dv uS
327e8af9738SPeter Avalosand
328e8af9738SPeter Avalos.Dv u2
329e8af9738SPeter Avalosare equivalent to
330e8af9738SPeter Avalos.Dv ushort ,
331e8af9738SPeter Avalosthe type specifiers
332e8af9738SPeter Avalos.Dv dI ,
333e8af9738SPeter Avalos.Dv dL ,
334e8af9738SPeter Avalosand
335e8af9738SPeter Avalos.Dv d4
336e8af9738SPeter Avalosare equivalent to
337e8af9738SPeter Avalos.Dv long ,
338e8af9738SPeter Avalosthe type specifiers
339e8af9738SPeter Avalos.Dv uI ,
340e8af9738SPeter Avalos.Dv uL ,
341e8af9738SPeter Avalosand
342e8af9738SPeter Avalos.Dv u4
343e8af9738SPeter Avalosare equivalent to
344e8af9738SPeter Avalos.Dv ulong ,
345e8af9738SPeter Avalosthe type specifier
346e8af9738SPeter Avalos.Dv d8
347e8af9738SPeter Avalosis equivalent to
348e8af9738SPeter Avalos.Dv quad ,
349e8af9738SPeter Avalosthe type specifier
350e8af9738SPeter Avalos.Dv u8
351e8af9738SPeter Avalosis equivalent to
352e8af9738SPeter Avalos.Dv uquad ,
353e8af9738SPeter Avalosand the type specifier
354e8af9738SPeter Avalos.Dv s
355e8af9738SPeter Avalosis equivalent to
356e8af9738SPeter Avalos.Dv string .
357e8af9738SPeter AvalosIn addition, the type specifier
358e8af9738SPeter Avalos.Dv dQ
359e8af9738SPeter Avalosis equivalent to
360e8af9738SPeter Avalos.Dv quad
361e8af9738SPeter Avalosand the type specifier
362e8af9738SPeter Avalos.Dv uQ
363e8af9738SPeter Avalosis equivalent to
364e8af9738SPeter Avalos.Dv uquad .
365e8af9738SPeter Avalos.Pp
36679343712SPeter AvalosEach top-level magic pattern (see below for an explanation of levels)
367a96e001bSPeter Avalosis classified as text or binary according to the types used.
368a96e001bSPeter AvalosTypes
36979343712SPeter Avalos.Dq regex
37079343712SPeter Avalosand
37179343712SPeter Avalos.Dq search
37279343712SPeter Avalosare classified as text tests, unless non-printable characters are used
373a96e001bSPeter Avalosin the pattern.
374a96e001bSPeter AvalosAll other tests are classified as binary.
375a96e001bSPeter AvalosA top-level
37679343712SPeter Avalospattern is considered to be a test text when all its patterns are text
377a96e001bSPeter Avalospatterns; otherwise, it is considered to be a binary pattern.
378a96e001bSPeter AvalosWhen
37979343712SPeter Avalosmatching a file, binary patterns are tried first; if no match is
38079343712SPeter Avalosfound, and the file looks like text, then its encoding is determined
38179343712SPeter Avalosand the text patterns are tried.
382327e51cbSPeter Avalos.Pp
383327e51cbSPeter AvalosThe numeric types may optionally be followed by
384327e51cbSPeter Avalos.Dv \*[Am]
385327e51cbSPeter Avalosand a numeric value,
386327e51cbSPeter Avalosto specify that the value is to be AND'ed with the
387327e51cbSPeter Avalosnumeric value before any comparisons are done.
388327e51cbSPeter AvalosPrepending a
389327e51cbSPeter Avalos.Dv u
390327e51cbSPeter Avalosto the type indicates that ordered comparisons should be unsigned.
391327e51cbSPeter Avalos.It Dv test
392327e51cbSPeter AvalosThe value to be compared with the value from the file.
393327e51cbSPeter AvalosIf the type is
394327e51cbSPeter Avalosnumeric, this value
395327e51cbSPeter Avalosis specified in C form; if it is a string, it is specified as a C string
396327e51cbSPeter Avaloswith the usual escapes permitted (e.g. \en for new-line).
397327e51cbSPeter Avalos.Pp
398327e51cbSPeter AvalosNumeric values
399327e51cbSPeter Avalosmay be preceded by a character indicating the operation to be performed.
400327e51cbSPeter AvalosIt may be
401327e51cbSPeter Avalos.Dv = ,
402327e51cbSPeter Avalosto specify that the value from the file must equal the specified value,
403327e51cbSPeter Avalos.Dv \*[Lt] ,
404327e51cbSPeter Avalosto specify that the value from the file must be less than the specified
405327e51cbSPeter Avalosvalue,
406327e51cbSPeter Avalos.Dv \*[Gt] ,
407327e51cbSPeter Avalosto specify that the value from the file must be greater than the specified
408327e51cbSPeter Avalosvalue,
409327e51cbSPeter Avalos.Dv \*[Am] ,
410327e51cbSPeter Avalosto specify that the value from the file must have set all of the bits
411327e51cbSPeter Avalosthat are set in the specified value,
412327e51cbSPeter Avalos.Dv ^ ,
413327e51cbSPeter Avalosto specify that the value from the file must have clear any of the bits
414327e51cbSPeter Avalosthat are set in the specified value, or
415327e51cbSPeter Avalos.Dv ~ ,
416327e51cbSPeter Avalosthe value specified after is negated before tested.
417327e51cbSPeter Avalos.Dv x ,
418327e51cbSPeter Avalosto specify that any value will match.
419327e51cbSPeter AvalosIf the character is omitted, it is assumed to be
420327e51cbSPeter Avalos.Dv = .
421327e51cbSPeter AvalosOperators
422327e51cbSPeter Avalos.Dv \*[Am] ,
423327e51cbSPeter Avalos.Dv ^ ,
424327e51cbSPeter Avalosand
425327e51cbSPeter Avalos.Dv ~
426327e51cbSPeter Avalosdon't work with floats and doubles.
42779343712SPeter AvalosThe operator
42879343712SPeter Avalos.Dv !\&
429327e51cbSPeter Avalosspecifies that the line matches if the test does
430327e51cbSPeter Avalos.Em not
431327e51cbSPeter Avalossucceed.
432327e51cbSPeter Avalos.Pp
433327e51cbSPeter AvalosNumeric values are specified in C form; e.g.
434327e51cbSPeter Avalos.Dv 13
435327e51cbSPeter Avalosis decimal,
436327e51cbSPeter Avalos.Dv 013
437327e51cbSPeter Avalosis octal, and
438327e51cbSPeter Avalos.Dv 0x13
439327e51cbSPeter Avalosis hexadecimal.
440327e51cbSPeter Avalos.Pp
44182c5fa3eSPeter AvalosNumeric operations are not performed on date types, instead the numeric
44282c5fa3eSPeter Avalosvalue is interpreted as an offset.
44382c5fa3eSPeter Avalos.Pp
44479343712SPeter AvalosFor string values, the string from the
44579343712SPeter Avalosfile must match the specified string.
446327e51cbSPeter AvalosThe operators
447327e51cbSPeter Avalos.Dv = ,
448327e51cbSPeter Avalos.Dv \*[Lt]
449327e51cbSPeter Avalosand
450327e51cbSPeter Avalos.Dv \*[Gt]
451327e51cbSPeter Avalos(but not
452327e51cbSPeter Avalos.Dv \*[Am] )
453327e51cbSPeter Avaloscan be applied to strings.
454327e51cbSPeter AvalosThe length used for matching is that of the string argument
455327e51cbSPeter Avalosin the magic file.
45679343712SPeter AvalosThis means that a line can match any non-empty string (usually used to
45779343712SPeter Avalosthen print the string), with
458327e51cbSPeter Avalos.Em \*[Gt]\e0
45979343712SPeter Avalos(because all non-empty strings are greater than the empty string).
460327e51cbSPeter Avalos.Pp
461e8af9738SPeter AvalosDates are treated as numerical values in the respective internal
462e8af9738SPeter Avalosrepresentation.
463e8af9738SPeter Avalos.Pp
464327e51cbSPeter AvalosThe special test
465327e51cbSPeter Avalos.Em x
466327e51cbSPeter Avalosalways evaluates to true.
467e4d4ce0cSPeter Avalos.It Dv message
468327e51cbSPeter AvalosThe message to be printed if the comparison succeeds.
469327e51cbSPeter AvalosIf the string contains a
470327e51cbSPeter Avalos.Xr printf 3
471327e51cbSPeter Avalosformat specification, the value from the file (with any specified masking
472327e51cbSPeter Avalosperformed) is printed using the message as the format string.
47379343712SPeter AvalosIf the string begins with
47479343712SPeter Avalos.Dq \eb ,
47579343712SPeter Avalosthe message printed is the remainder of the string with no whitespace
47679343712SPeter Avalosadded before it: multiple matches are normally separated by a single
47779343712SPeter Avalosspace.
478327e51cbSPeter Avalos.El
479327e51cbSPeter Avalos.Pp
48079343712SPeter AvalosAn APPLE 4+4 character APPLE creator and type can be specified as:
48179343712SPeter Avalos.Bd -literal -offset indent
48279343712SPeter Avalos!:apple	CREATYPE
48379343712SPeter Avalos.Ed
48479343712SPeter Avalos.Pp
48579343712SPeter AvalosA MIME type is given on a separate line, which must be the next
48679343712SPeter Avalosnon-blank or comment line after the magic line that identifies the
48779343712SPeter Avalosfile type, and has the following format:
48879343712SPeter Avalos.Bd -literal -offset indent
48979343712SPeter Avalos!:mime	MIMETYPE
49079343712SPeter Avalos.Ed
49179343712SPeter Avalos.Pp
49279343712SPeter Avalosi.e. the literal string
49379343712SPeter Avalos.Dq !:mime
49479343712SPeter Avalosfollowed by the MIME type.
49579343712SPeter Avalos.Pp
49679343712SPeter AvalosAn optional strength can be supplied on a separate line which refers to
49779343712SPeter Avalosthe current magic description using the following format:
49879343712SPeter Avalos.Bd -literal -offset indent
49979343712SPeter Avalos!:strength OP VALUE
50079343712SPeter Avalos.Ed
50179343712SPeter Avalos.Pp
50279343712SPeter AvalosThe operand
50379343712SPeter Avalos.Dv OP
50479343712SPeter Avaloscan be:
50579343712SPeter Avalos.Dv + ,
50679343712SPeter Avalos.Dv - ,
50779343712SPeter Avalos.Dv * ,
50879343712SPeter Avalosor
50979343712SPeter Avalos.Dv /
51079343712SPeter Avalosand
51179343712SPeter Avalos.Dv VALUE
51279343712SPeter Avalosis a constant between 0 and 255.
51379343712SPeter AvalosThis constant is applied using the specified operand
51479343712SPeter Avalosto the currently computed default magic strength.
51579343712SPeter Avalos.Pp
516327e51cbSPeter AvalosSome file formats contain additional information which is to be printed
517327e51cbSPeter Avalosalong with the file type or need additional tests to determine the true
518327e51cbSPeter Avalosfile type.
519327e51cbSPeter AvalosThese additional tests are introduced by one or more
520327e51cbSPeter Avalos.Em \*[Gt]
521327e51cbSPeter Avaloscharacters preceding the offset.
522327e51cbSPeter AvalosThe number of
523327e51cbSPeter Avalos.Em \*[Gt]
524327e51cbSPeter Avaloson the line indicates the level of the test; a line with no
525327e51cbSPeter Avalos.Em \*[Gt]
526327e51cbSPeter Avalosat the beginning is considered to be at level 0.
527327e51cbSPeter AvalosTests are arranged in a tree-like hierarchy:
528e4d4ce0cSPeter Avalosif the test on a line at level
529327e51cbSPeter Avalos.Em n
530327e51cbSPeter Avalossucceeds, all following tests at level
531327e51cbSPeter Avalos.Em n+1
532e4d4ce0cSPeter Avalosare performed, and the messages printed if the tests succeed, until a line
533327e51cbSPeter Avaloswith level
534327e51cbSPeter Avalos.Em n
535327e51cbSPeter Avalos(or less) appears.
536327e51cbSPeter AvalosFor more complex files, one can use empty messages to get just the
537327e51cbSPeter Avalos"if/then" effect, in the following way:
538327e51cbSPeter Avalos.Bd -literal -offset indent
539327e51cbSPeter Avalos0      string   MZ
540327e51cbSPeter Avalos\*[Gt]0x18  leshort  \*[Lt]0x40   MS-DOS executable
541327e51cbSPeter Avalos\*[Gt]0x18  leshort  \*[Gt]0x3f   extended PC executable (e.g., MS Windows)
542327e51cbSPeter Avalos.Ed
543327e51cbSPeter Avalos.Pp
544327e51cbSPeter AvalosOffsets do not need to be constant, but can also be read from the file
545327e51cbSPeter Avalosbeing examined.
546327e51cbSPeter AvalosIf the first character following the last
547327e51cbSPeter Avalos.Em \*[Gt]
548327e51cbSPeter Avalosis a
549e4d4ce0cSPeter Avalos.Em \&(
550327e51cbSPeter Avalosthen the string after the parenthesis is interpreted as an indirect offset.
551327e51cbSPeter AvalosThat means that the number after the parenthesis is used as an offset in
552327e51cbSPeter Avalosthe file.
553327e51cbSPeter AvalosThe value at that offset is read, and is used again as an offset
554327e51cbSPeter Avalosin the file.
555327e51cbSPeter AvalosIndirect offsets are of the form:
556*6fca56fbSSascha Wildner.Em (( x [[.,][bBcCeEfFgGhHiIlmsSqQ]][+\-][ y ]) .
557327e51cbSPeter AvalosThe value of
558327e51cbSPeter Avalos.Em x
559327e51cbSPeter Avalosis used as an offset in the file.
56079343712SPeter AvalosA byte, id3 length, short or long is read at that offset depending on the
561*6fca56fbSSascha Wildner.Em [bBcCeEfFgGhHiIlmsSqQ]
562327e51cbSPeter Avalostype specifier.
563c30bd091SSascha WildnerThe value is treated as signed if
564c30bd091SSascha Wildner.Dq ,
565c30bd091SSascha Wildneris specified or unsigned if
566c30bd091SSascha Wildner.Dq .
567c30bd091SSascha Wildneris specified.
568327e51cbSPeter AvalosThe capitalized types interpret the number as a big endian
569327e51cbSPeter Avalosvalue, whereas the small letter versions interpret the number as a little
570327e51cbSPeter Avalosendian value;
571327e51cbSPeter Avalosthe
572327e51cbSPeter Avalos.Em m
573327e51cbSPeter Avalostype interprets the number as a middle endian (PDP-11) value.
574327e51cbSPeter AvalosTo that number the value of
575327e51cbSPeter Avalos.Em y
576327e51cbSPeter Avalosis added and the result is used as an offset in the file.
577327e51cbSPeter AvalosThe default type if one is not specified is long.
578*6fca56fbSSascha WildnerThe following types are recognized:
579*6fca56fbSSascha Wildner.Bl -column -offset indent "Type" "Half/Short" "Little" "Size"
580*6fca56fbSSascha Wildner.It Sy Type	Sy Mnemonic	Sy Endian	Sy Size
581*6fca56fbSSascha Wildner.It bcBc	Byte/Char	N/A	1
582*6fca56fbSSascha Wildner.It efg	Double	Little	8
583*6fca56fbSSascha Wildner.It EFG	Double	Big	8
584*6fca56fbSSascha Wildner.It hs	Half/Short	Little	2
585*6fca56fbSSascha Wildner.It HS	Half/Short	Big	2
586*6fca56fbSSascha Wildner.It i	ID3	Little	4
587*6fca56fbSSascha Wildner.It I	ID3	Big	4
588*6fca56fbSSascha Wildner.It m	Middle	Middle	4
589*6fca56fbSSascha Wildner.It q	Quad	Little	8
590*6fca56fbSSascha Wildner.It Q	Quad	Big	8
591*6fca56fbSSascha Wildner.El
592327e51cbSPeter Avalos.Pp
593327e51cbSPeter AvalosThat way variable length structures can be examined:
594327e51cbSPeter Avalos.Bd -literal -offset indent
595327e51cbSPeter Avalos# MS Windows executables are also valid MS-DOS executables
596327e51cbSPeter Avalos0           string  MZ
597327e51cbSPeter Avalos\*[Gt]0x18       leshort \*[Lt]0x40   MZ executable (MS-DOS)
598327e51cbSPeter Avalos# skip the whole block below if it is not an extended executable
599327e51cbSPeter Avalos\*[Gt]0x18       leshort \*[Gt]0x3f
600327e51cbSPeter Avalos\*[Gt]\*[Gt](0x3c.l)  string  PE\e0\e0  PE executable (MS-Windows)
601327e51cbSPeter Avalos\*[Gt]\*[Gt](0x3c.l)  string  LX\e0\e0  LX executable (OS/2)
602327e51cbSPeter Avalos.Ed
603327e51cbSPeter Avalos.Pp
604c30bd091SSascha WildnerThis strategy of examining has a drawback: you must make sure that you
605c30bd091SSascha Wildnereventually print something, or users may get empty output (such as when
606c30bd091SSascha Wildnerthere is neither PE\e0\e0 nor LE\e0\e0 in the above example).
607327e51cbSPeter Avalos.Pp
60879343712SPeter AvalosIf this indirect offset cannot be used directly, simple calculations are
609327e51cbSPeter Avalospossible: appending
61079343712SPeter Avalos.Em [+-*/%\*[Am]|^]number
611327e51cbSPeter Avalosinside parentheses allows one to modify
612327e51cbSPeter Avalosthe value read from the file before it is used as an offset:
613327e51cbSPeter Avalos.Bd -literal -offset indent
614327e51cbSPeter Avalos# MS Windows executables are also valid MS-DOS executables
615327e51cbSPeter Avalos0           string  MZ
616327e51cbSPeter Avalos# sometimes, the value at 0x18 is less that 0x40 but there's still an
617327e51cbSPeter Avalos# extended executable, simply appended to the file
618327e51cbSPeter Avalos\*[Gt]0x18       leshort \*[Lt]0x40
619327e51cbSPeter Avalos\*[Gt]\*[Gt](4.s*512) leshort 0x014c  COFF executable (MS-DOS, DJGPP)
620327e51cbSPeter Avalos\*[Gt]\*[Gt](4.s*512) leshort !0x014c MZ executable (MS-DOS)
621327e51cbSPeter Avalos.Ed
622327e51cbSPeter Avalos.Pp
623327e51cbSPeter AvalosSometimes you do not know the exact offset as this depends on the length or
624327e51cbSPeter Avalosposition (when indirection was used before) of preceding fields.
625327e51cbSPeter AvalosYou can specify an offset relative to the end of the last up-level
626327e51cbSPeter Avalosfield using
627327e51cbSPeter Avalos.Sq \*[Am]
628327e51cbSPeter Avalosas a prefix to the offset:
629327e51cbSPeter Avalos.Bd -literal -offset indent
630327e51cbSPeter Avalos0           string  MZ
631327e51cbSPeter Avalos\*[Gt]0x18       leshort \*[Gt]0x3f
632327e51cbSPeter Avalos\*[Gt]\*[Gt](0x3c.l)  string  PE\e0\e0    PE executable (MS-Windows)
633327e51cbSPeter Avalos# immediately following the PE signature is the CPU type
634327e51cbSPeter Avalos\*[Gt]\*[Gt]\*[Gt]\*[Am]0       leshort 0x14c     for Intel 80386
635327e51cbSPeter Avalos\*[Gt]\*[Gt]\*[Gt]\*[Am]0       leshort 0x184     for DEC Alpha
636327e51cbSPeter Avalos.Ed
637327e51cbSPeter Avalos.Pp
638327e51cbSPeter AvalosIndirect and relative offsets can be combined:
639327e51cbSPeter Avalos.Bd -literal -offset indent
640327e51cbSPeter Avalos0             string  MZ
641327e51cbSPeter Avalos\*[Gt]0x18         leshort \*[Lt]0x40
642327e51cbSPeter Avalos\*[Gt]\*[Gt](4.s*512)   leshort !0x014c MZ executable (MS-DOS)
643327e51cbSPeter Avalos# if it's not COFF, go back 512 bytes and add the offset taken
644327e51cbSPeter Avalos# from byte 2/3, which is yet another way of finding the start
645327e51cbSPeter Avalos# of the extended executable
646327e51cbSPeter Avalos\*[Gt]\*[Gt]\*[Gt]\*[Am](2.s-514) string  LE      LE executable (MS Windows VxD driver)
647327e51cbSPeter Avalos.Ed
648327e51cbSPeter Avalos.Pp
649327e51cbSPeter AvalosOr the other way around:
650327e51cbSPeter Avalos.Bd -literal -offset indent
651327e51cbSPeter Avalos0                 string  MZ
652327e51cbSPeter Avalos\*[Gt]0x18             leshort \*[Gt]0x3f
653327e51cbSPeter Avalos\*[Gt]\*[Gt](0x3c.l)        string  LE\e0\e0  LE executable (MS-Windows)
654327e51cbSPeter Avalos# at offset 0x80 (-4, since relative offsets start at the end
655327e51cbSPeter Avalos# of the up-level match) inside the LE header, we find the absolute
656327e51cbSPeter Avalos# offset to the code area, where we look for a specific signature
657327e51cbSPeter Avalos\*[Gt]\*[Gt]\*[Gt](\*[Am]0x7c.l+0x26) string  UPX     \eb, UPX compressed
658327e51cbSPeter Avalos.Ed
659327e51cbSPeter Avalos.Pp
660327e51cbSPeter AvalosOr even both!
661327e51cbSPeter Avalos.Bd -literal -offset indent
662327e51cbSPeter Avalos0                string  MZ
663327e51cbSPeter Avalos\*[Gt]0x18            leshort \*[Gt]0x3f
664327e51cbSPeter Avalos\*[Gt]\*[Gt](0x3c.l)       string  LE\e0\e0 LE executable (MS-Windows)
665327e51cbSPeter Avalos# at offset 0x58 inside the LE header, we find the relative offset
666327e51cbSPeter Avalos# to a data area where we look for a specific signature
667327e51cbSPeter Avalos\*[Gt]\*[Gt]\*[Gt]\*[Am](\*[Am]0x54.l-3)  string  UNACE  \eb, ACE self-extracting archive
668327e51cbSPeter Avalos.Ed
669327e51cbSPeter Avalos.Pp
670e8af9738SPeter AvalosIf you have to deal with offset/length pairs in your file, even the
671327e51cbSPeter Avalossecond value in a parenthesized expression can be taken from the file itself,
672327e51cbSPeter Avalosusing another set of parentheses.
673327e51cbSPeter AvalosNote that this additional indirect offset is always relative to the
674327e51cbSPeter Avalosstart of the main indirect offset.
675327e51cbSPeter Avalos.Bd -literal -offset indent
676327e51cbSPeter Avalos0                 string       MZ
677327e51cbSPeter Avalos\*[Gt]0x18             leshort      \*[Gt]0x3f
678327e51cbSPeter Avalos\*[Gt]\*[Gt](0x3c.l)        string       PE\e0\e0 PE executable (MS-Windows)
679327e51cbSPeter Avalos# search for the PE section called ".idata"...
680327e51cbSPeter Avalos\*[Gt]\*[Gt]\*[Gt]\*[Am]0xf4          search/0x140 .idata
681327e51cbSPeter Avalos# ...and go to the end of it, calculated from start+length;
682327e51cbSPeter Avalos# these are located 14 and 10 bytes after the section name
683327e51cbSPeter Avalos\*[Gt]\*[Gt]\*[Gt]\*[Gt](\*[Am]0xe.l+(-4)) string       PK\e3\e4 \eb, ZIP self-extracting archive
684327e51cbSPeter Avalos.Ed
685e8af9738SPeter Avalos.Pp
686c30bd091SSascha WildnerIf you have a list of known values at a particular continuation level,
687e8af9738SPeter Avalosand you want to provide a switch-like default case:
688e8af9738SPeter Avalos.Bd -literal -offset indent
689e8af9738SPeter Avalos# clear that continuation level match
690e8af9738SPeter Avalos\*[Gt]18	clear
691e8af9738SPeter Avalos\*[Gt]18	lelong	1	one
692e8af9738SPeter Avalos\*[Gt]18	lelong	2	two
693e8af9738SPeter Avalos\*[Gt]18	default	x
694e8af9738SPeter Avalos# print default match
695e8af9738SPeter Avalos\*[Gt]\*[Gt]18	lelong	x	unmatched 0x%x
696e8af9738SPeter Avalos.Ed
697327e51cbSPeter Avalos.Sh SEE ALSO
698327e51cbSPeter Avalos.Xr file __CSECTION__
699327e51cbSPeter Avalos\- the command that reads this file.
700327e51cbSPeter Avalos.Sh BUGS
701327e51cbSPeter AvalosThe formats
702327e51cbSPeter Avalos.Dv long ,
703327e51cbSPeter Avalos.Dv belong ,
704327e51cbSPeter Avalos.Dv lelong ,
705327e51cbSPeter Avalos.Dv melong ,
706327e51cbSPeter Avalos.Dv short ,
707327e51cbSPeter Avalos.Dv beshort ,
708327e51cbSPeter Avalosand
709e8af9738SPeter Avalos.Dv leshort
710e8af9738SPeter Avalosdo not depend on the length of the C data types
711e8af9738SPeter Avalos.Dv short
712e8af9738SPeter Avalosand
713e8af9738SPeter Avalos.Dv long
714e8af9738SPeter Avaloson the platform, even though the Single
715e8af9738SPeter Avalos.Ux
716e8af9738SPeter AvalosSpecification implies that they do.  However, as OS X Mountain Lion has
717e8af9738SPeter Avalospassed the Single
718e8af9738SPeter Avalos.Ux
719e8af9738SPeter AvalosSpecification validation suite, and supplies a version of
720e8af9738SPeter Avalos.Xr file __CSECTION__
721e8af9738SPeter Avalosin which they do not depend on the sizes of the C data types and that is
722e8af9738SPeter Avalosbuilt for a 64-bit environment in which
723e8af9738SPeter Avalos.Dv long
724e8af9738SPeter Avalosis 8 bytes rather than 4 bytes, presumably the validation suite does not
725e8af9738SPeter Avalostest whether, for example
726e8af9738SPeter Avalos.Dv long
727e8af9738SPeter Avalosrefers to an item with the same size as the C data type
728e8af9738SPeter Avalos.Dv long .
729e8af9738SPeter AvalosThere should probably be
730e8af9738SPeter Avalos.Dv type
731e8af9738SPeter Avalosnames
732e8af9738SPeter Avalos.Dv int8 ,
733e8af9738SPeter Avalos.Dv uint8 ,
734e8af9738SPeter Avalos.Dv int16 ,
735e8af9738SPeter Avalos.Dv uint16 ,
736e8af9738SPeter Avalos.Dv int32 ,
737e8af9738SPeter Avalos.Dv uint32 ,
738e8af9738SPeter Avalos.Dv int64 ,
739e8af9738SPeter Avalosand
740e8af9738SPeter Avalos.Dv uint64 ,
741e8af9738SPeter Avalosand specified-byte-order variants of them,
742e8af9738SPeter Avalosto make it clearer that those types have specified widths.
743327e51cbSPeter Avalos.\"
744327e51cbSPeter Avalos.\" From: guy@sun.uucp (Guy Harris)
745327e51cbSPeter Avalos.\" Newsgroups: net.bugs.usg
746327e51cbSPeter Avalos.\" Subject: /etc/magic's format isn't well documented
747327e51cbSPeter Avalos.\" Message-ID: <2752@sun.uucp>
748327e51cbSPeter Avalos.\" Date: 3 Sep 85 08:19:07 GMT
749327e51cbSPeter Avalos.\" Organization: Sun Microsystems, Inc.
750327e51cbSPeter Avalos.\" Lines: 136
751327e51cbSPeter Avalos.\"
752327e51cbSPeter Avalos.\" Here's a manual page for the format accepted by the "file" made by adding
753327e51cbSPeter Avalos.\" the changes I posted to the S5R2 version.
754327e51cbSPeter Avalos.\"
755327e51cbSPeter Avalos.\" Modified for Ian Darwin's version of the file command.
756