xref: /dflybsd-src/contrib/file/doc/magic.man (revision a96e001b75d2a5877ad6fcec5487cf480f9d827a)
1*a96e001bSPeter Avalos.\" $File: magic.man,v 1.69 2011/05/13 22:11:44 christos Exp $
2884044a5SPeter Avalos.Dd April 20, 2011
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
10327e51cbSPeter AvalosThis manual page documents the format of the magic file 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" .
20327e51cbSPeter AvalosThe file
21327e51cbSPeter Avalos.Pa __MAGIC__
2279343712SPeter Avalosspecifies what patterns are to be tested for, what message or
2379343712SPeter AvalosMIME type to print if a particular pattern is found,
24327e51cbSPeter Avalosand additional information to extract from the file.
25327e51cbSPeter Avalos.Pp
26327e51cbSPeter AvalosEach line of the file specifies a test to be performed.
27327e51cbSPeter AvalosA test compares the data starting at a particular offset
2879343712SPeter Avalosin the file with a byte value, a string or a numeric value.
29327e51cbSPeter AvalosIf the test succeeds, a message is printed.
30327e51cbSPeter AvalosThe line consists of the following fields:
31327e51cbSPeter Avalos.Bl -tag -width ".Dv message"
32327e51cbSPeter Avalos.It Dv offset
33327e51cbSPeter AvalosA number specifying the offset, in bytes, into the file of the data
34327e51cbSPeter Avaloswhich is to be tested.
35327e51cbSPeter Avalos.It Dv type
36327e51cbSPeter AvalosThe type of the data to be tested.
37327e51cbSPeter AvalosThe possible values are:
38327e51cbSPeter Avalos.Bl -tag -width ".Dv lestring16"
39327e51cbSPeter Avalos.It Dv byte
40327e51cbSPeter AvalosA one-byte value.
41327e51cbSPeter Avalos.It Dv short
4279343712SPeter AvalosA two-byte value in this machine's native byte order.
43327e51cbSPeter Avalos.It Dv long
4479343712SPeter AvalosA four-byte value in this machine's native byte order.
45327e51cbSPeter Avalos.It Dv quad
4679343712SPeter AvalosAn eight-byte value in this machine's native byte order.
47327e51cbSPeter Avalos.It Dv float
4879343712SPeter AvalosA 32-bit single precision IEEE floating point number in this machine's native byte order.
49327e51cbSPeter Avalos.It Dv double
5079343712SPeter AvalosA 64-bit double precision IEEE floating point number in this machine's native byte order.
51327e51cbSPeter Avalos.It Dv string
52327e51cbSPeter AvalosA string of bytes.
53327e51cbSPeter AvalosThe string type specification can be optionally followed
54884044a5SPeter Avalosby /[WwcCtb]*.
55327e51cbSPeter AvalosThe
56884044a5SPeter Avalos.Dq W
57327e51cbSPeter Avalosflag compacts whitespace in the target, which must
58327e51cbSPeter Avaloscontain at least one whitespace character.
59327e51cbSPeter AvalosIf the magic has
60327e51cbSPeter Avalos.Dv n
61327e51cbSPeter Avalosconsecutive blanks, the target needs at least
62327e51cbSPeter Avalos.Dv n
63327e51cbSPeter Avalosconsecutive blanks to match.
64327e51cbSPeter AvalosThe
65884044a5SPeter Avalos.Dq w
66327e51cbSPeter Avalosflag treats every blank in the target as an optional blank.
67884044a5SPeter AvalosThe
68327e51cbSPeter Avalos.Dq c
69327e51cbSPeter Avalosflag, specifies case insensitive matching: lower case
70327e51cbSPeter Avaloscharacters in the magic match both lower and upper case characters in the
7179343712SPeter Avalostarget, whereas upper case characters in the magic only match upper case
72327e51cbSPeter Avaloscharacters in the target.
73884044a5SPeter AvalosThe
74884044a5SPeter Avalos.Dq C
75884044a5SPeter Avalosflag, specifies case insensitive matching: upper case
76884044a5SPeter Avaloscharacters in the magic match both lower and upper case characters in the
77884044a5SPeter Avalostarget, whereas lower case characters in the magic only match upper case
78884044a5SPeter Avaloscharacters in the target.
79884044a5SPeter AvalosTo do a complete case insensitive match, specify both
80884044a5SPeter Avalos.Dq c
81884044a5SPeter Avalosand
82884044a5SPeter Avalos.Dq C .
83884044a5SPeter AvalosThe
84884044a5SPeter Avalos.Dq t
85884044a5SPeter Avalosflag, forces the test to be done for text files, while the
86884044a5SPeter Avalos.Dq b
87884044a5SPeter Avalosflag, forces the test to be done for binary files.
88327e51cbSPeter Avalos.It Dv pstring
89e4d4ce0cSPeter AvalosA Pascal-style string where the first byte/short/int is interpreted as the an
90327e51cbSPeter Avalosunsigned length.
91e4d4ce0cSPeter AvalosThe length defaults to byte and can be specified as a modifier.
92e4d4ce0cSPeter AvalosThe following modifiers are supported:
93e4d4ce0cSPeter Avalos.Bl -tag -compact -width B
94e4d4ce0cSPeter Avalos.It B
95e4d4ce0cSPeter AvalosA byte length (default).
96e4d4ce0cSPeter Avalos.It H
97e4d4ce0cSPeter AvalosA 2 byte big endian length.
98e4d4ce0cSPeter Avalos.It h
99e4d4ce0cSPeter AvalosA 2 byte big little length.
100e4d4ce0cSPeter Avalos.It L
101e4d4ce0cSPeter AvalosA 4 byte big endian length.
102e4d4ce0cSPeter Avalos.It l
103e4d4ce0cSPeter AvalosA 4 byte big little length.
104e4d4ce0cSPeter Avalos.It J
105e4d4ce0cSPeter AvalosThe length includes itself in its count.
106e4d4ce0cSPeter Avalos.El
107327e51cbSPeter AvalosThe string is not NUL terminated.
108e4d4ce0cSPeter Avalos.Dq J
109e4d4ce0cSPeter Avalosis used rather than the more
110e4d4ce0cSPeter Avalosvaluable
111e4d4ce0cSPeter Avalos.Dq I
112e4d4ce0cSPeter Avalosbecause this type of length is a feature of the JPEG
113e4d4ce0cSPeter Avalosformat.
114327e51cbSPeter Avalos.It Dv date
115327e51cbSPeter AvalosA four-byte value interpreted as a UNIX date.
116327e51cbSPeter Avalos.It Dv qdate
117327e51cbSPeter AvalosA eight-byte value interpreted as a UNIX date.
118327e51cbSPeter Avalos.It Dv ldate
119327e51cbSPeter AvalosA four-byte value interpreted as a UNIX-style date, but interpreted as
120327e51cbSPeter Avaloslocal time rather than UTC.
121327e51cbSPeter Avalos.It Dv qldate
122327e51cbSPeter AvalosAn eight-byte value interpreted as a UNIX-style date, but interpreted as
123327e51cbSPeter Avaloslocal time rather than UTC.
12479343712SPeter Avalos.It Dv beid3
12579343712SPeter AvalosA 32-bit ID3 length in big-endian byte order.
126327e51cbSPeter Avalos.It Dv beshort
12779343712SPeter AvalosA two-byte value in big-endian byte order.
128327e51cbSPeter Avalos.It Dv belong
12979343712SPeter AvalosA four-byte value in big-endian byte order.
130327e51cbSPeter Avalos.It Dv bequad
13179343712SPeter AvalosAn eight-byte value in big-endian byte order.
132327e51cbSPeter Avalos.It Dv befloat
13379343712SPeter AvalosA 32-bit single precision IEEE floating point number in big-endian byte order.
134327e51cbSPeter Avalos.It Dv bedouble
13579343712SPeter AvalosA 64-bit double precision IEEE floating point number in big-endian byte order.
136327e51cbSPeter Avalos.It Dv bedate
13779343712SPeter AvalosA four-byte value in big-endian byte order,
138327e51cbSPeter Avalosinterpreted as a Unix date.
139327e51cbSPeter Avalos.It Dv beqdate
14079343712SPeter AvalosAn eight-byte value in big-endian byte order,
141327e51cbSPeter Avalosinterpreted as a Unix date.
142327e51cbSPeter Avalos.It Dv beldate
14379343712SPeter AvalosA four-byte value in big-endian byte order,
144327e51cbSPeter Avalosinterpreted as a UNIX-style date, but interpreted as local time rather
145327e51cbSPeter Avalosthan UTC.
146327e51cbSPeter Avalos.It Dv beqldate
14779343712SPeter AvalosAn eight-byte value in big-endian byte order,
148327e51cbSPeter Avalosinterpreted as a UNIX-style date, but interpreted as local time rather
149327e51cbSPeter Avalosthan UTC.
150327e51cbSPeter Avalos.It Dv bestring16
151327e51cbSPeter AvalosA two-byte unicode (UCS16) string in big-endian byte order.
15279343712SPeter Avalos.It Dv leid3
15379343712SPeter AvalosA 32-bit ID3 length in little-endian byte order.
154327e51cbSPeter Avalos.It Dv leshort
15579343712SPeter AvalosA two-byte value in little-endian byte order.
156327e51cbSPeter Avalos.It Dv lelong
15779343712SPeter AvalosA four-byte value in little-endian byte order.
158327e51cbSPeter Avalos.It Dv lequad
15979343712SPeter AvalosAn eight-byte value in little-endian byte order.
160327e51cbSPeter Avalos.It Dv lefloat
16179343712SPeter AvalosA 32-bit single precision IEEE floating point number in little-endian byte order.
162327e51cbSPeter Avalos.It Dv ledouble
16379343712SPeter AvalosA 64-bit double precision IEEE floating point number in little-endian byte order.
164327e51cbSPeter Avalos.It Dv ledate
16579343712SPeter AvalosA four-byte value in little-endian byte order,
166327e51cbSPeter Avalosinterpreted as a UNIX date.
167327e51cbSPeter Avalos.It Dv leqdate
16879343712SPeter AvalosAn eight-byte value in little-endian byte order,
169327e51cbSPeter Avalosinterpreted as a UNIX date.
170327e51cbSPeter Avalos.It Dv leldate
17179343712SPeter AvalosA four-byte value in little-endian byte order,
172327e51cbSPeter Avalosinterpreted as a UNIX-style date, but interpreted as local time rather
173327e51cbSPeter Avalosthan UTC.
174327e51cbSPeter Avalos.It Dv leqldate
17579343712SPeter AvalosAn eight-byte value in little-endian byte order,
176327e51cbSPeter Avalosinterpreted as a UNIX-style date, but interpreted as local time rather
177327e51cbSPeter Avalosthan UTC.
178327e51cbSPeter Avalos.It Dv lestring16
179327e51cbSPeter AvalosA two-byte unicode (UCS16) string in little-endian byte order.
180327e51cbSPeter Avalos.It Dv melong
18179343712SPeter AvalosA four-byte value in middle-endian (PDP-11) byte order.
182327e51cbSPeter Avalos.It Dv medate
18379343712SPeter AvalosA four-byte value in middle-endian (PDP-11) byte order,
184327e51cbSPeter Avalosinterpreted as a UNIX date.
185327e51cbSPeter Avalos.It Dv meldate
18679343712SPeter AvalosA four-byte value in middle-endian (PDP-11) byte order,
187327e51cbSPeter Avalosinterpreted as a UNIX-style date, but interpreted as local time rather
188327e51cbSPeter Avalosthan UTC.
18979343712SPeter Avalos.It Dv indirect
19079343712SPeter AvalosStarting at the given offset, consult the magic database again.
191327e51cbSPeter Avalos.It Dv regex
192327e51cbSPeter AvalosA regular expression match in extended POSIX regular expression syntax
193*a96e001bSPeter Avalos(like egrep).
194*a96e001bSPeter AvalosRegular expressions can take exponential time to process, and their
195*a96e001bSPeter Avalosperformance is hard to predict, so their use is discouraged.
196*a96e001bSPeter AvalosWhen used in production environments, their performance
197*a96e001bSPeter Avalosshould be carefully checked.
198*a96e001bSPeter AvalosThe type specification can be optionally followed by
19979343712SPeter Avalos.Dv /[c][s] .
200327e51cbSPeter AvalosThe
201327e51cbSPeter Avalos.Dq c
202327e51cbSPeter Avalosflag makes the match case insensitive, while the
203327e51cbSPeter Avalos.Dq s
20479343712SPeter Avalosflag update the offset to the start offset of the match, rather than the end.
20579343712SPeter AvalosThe regular expression is tested against line
20679343712SPeter Avalos.Dv N + 1
20779343712SPeter Avalosonwards, where
208327e51cbSPeter Avalos.Dv N
20979343712SPeter Avalosis the given offset.
21079343712SPeter AvalosLine endings are assumed to be in the machine's native format.
211327e51cbSPeter Avalos.Dv ^
212327e51cbSPeter Avalosand
213327e51cbSPeter Avalos.Dv $
21479343712SPeter Avalosmatch the beginning and end of individual lines, respectively,
215327e51cbSPeter Avalosnot beginning and end of file.
216327e51cbSPeter Avalos.It Dv search
217*a96e001bSPeter AvalosA literal string search starting at the given offset.
218*a96e001bSPeter AvalosThe same modifier flags can be used as for string patterns.
219*a96e001bSPeter AvalosThe modifier flags (if any) must be followed by
22079343712SPeter Avalos.Dv /number
22179343712SPeter Avalosthe range, that is, the number of positions at which the match will be
222*a96e001bSPeter Avalosattempted, starting from the start offset.
223*a96e001bSPeter AvalosThis is suitable for
22479343712SPeter Avalossearching larger binary expressions with variable offsets, using
225327e51cbSPeter Avalos.Dv \e
226*a96e001bSPeter Avalosescapes for special characters.
227*a96e001bSPeter AvalosThe offset works as for regex.
228327e51cbSPeter Avalos.It Dv default
22979343712SPeter AvalosThis is intended to be used with the test
23079343712SPeter Avalos.Em x
231327e51cbSPeter Avalos(which is always true) and a message that is to be used if there are
232327e51cbSPeter Avalosno other matches.
233327e51cbSPeter Avalos.El
23479343712SPeter Avalos.Pp
23579343712SPeter AvalosEach top-level magic pattern (see below for an explanation of levels)
236*a96e001bSPeter Avalosis classified as text or binary according to the types used.
237*a96e001bSPeter AvalosTypes
23879343712SPeter Avalos.Dq regex
23979343712SPeter Avalosand
24079343712SPeter Avalos.Dq search
24179343712SPeter Avalosare classified as text tests, unless non-printable characters are used
242*a96e001bSPeter Avalosin the pattern.
243*a96e001bSPeter AvalosAll other tests are classified as binary.
244*a96e001bSPeter AvalosA top-level
24579343712SPeter Avalospattern is considered to be a test text when all its patterns are text
246*a96e001bSPeter Avalospatterns; otherwise, it is considered to be a binary pattern.
247*a96e001bSPeter AvalosWhen
24879343712SPeter Avalosmatching a file, binary patterns are tried first; if no match is
24979343712SPeter Avalosfound, and the file looks like text, then its encoding is determined
25079343712SPeter Avalosand the text patterns are tried.
251327e51cbSPeter Avalos.Pp
252327e51cbSPeter AvalosThe numeric types may optionally be followed by
253327e51cbSPeter Avalos.Dv \*[Am]
254327e51cbSPeter Avalosand a numeric value,
255327e51cbSPeter Avalosto specify that the value is to be AND'ed with the
256327e51cbSPeter Avalosnumeric value before any comparisons are done.
257327e51cbSPeter AvalosPrepending a
258327e51cbSPeter Avalos.Dv u
259327e51cbSPeter Avalosto the type indicates that ordered comparisons should be unsigned.
260327e51cbSPeter Avalos.It Dv test
261327e51cbSPeter AvalosThe value to be compared with the value from the file.
262327e51cbSPeter AvalosIf the type is
263327e51cbSPeter Avalosnumeric, this value
264327e51cbSPeter Avalosis specified in C form; if it is a string, it is specified as a C string
265327e51cbSPeter Avaloswith the usual escapes permitted (e.g. \en for new-line).
266327e51cbSPeter Avalos.Pp
267327e51cbSPeter AvalosNumeric values
268327e51cbSPeter Avalosmay be preceded by a character indicating the operation to be performed.
269327e51cbSPeter AvalosIt may be
270327e51cbSPeter Avalos.Dv = ,
271327e51cbSPeter Avalosto specify that the value from the file must equal the specified value,
272327e51cbSPeter Avalos.Dv \*[Lt] ,
273327e51cbSPeter Avalosto specify that the value from the file must be less than the specified
274327e51cbSPeter Avalosvalue,
275327e51cbSPeter Avalos.Dv \*[Gt] ,
276327e51cbSPeter Avalosto specify that the value from the file must be greater than the specified
277327e51cbSPeter Avalosvalue,
278327e51cbSPeter Avalos.Dv \*[Am] ,
279327e51cbSPeter Avalosto specify that the value from the file must have set all of the bits
280327e51cbSPeter Avalosthat are set in the specified value,
281327e51cbSPeter Avalos.Dv ^ ,
282327e51cbSPeter Avalosto specify that the value from the file must have clear any of the bits
283327e51cbSPeter Avalosthat are set in the specified value, or
284327e51cbSPeter Avalos.Dv ~ ,
285327e51cbSPeter Avalosthe value specified after is negated before tested.
286327e51cbSPeter Avalos.Dv x ,
287327e51cbSPeter Avalosto specify that any value will match.
288327e51cbSPeter AvalosIf the character is omitted, it is assumed to be
289327e51cbSPeter Avalos.Dv = .
290327e51cbSPeter AvalosOperators
291327e51cbSPeter Avalos.Dv \*[Am] ,
292327e51cbSPeter Avalos.Dv ^ ,
293327e51cbSPeter Avalosand
294327e51cbSPeter Avalos.Dv ~
295327e51cbSPeter Avalosdon't work with floats and doubles.
29679343712SPeter AvalosThe operator
29779343712SPeter Avalos.Dv !\&
298327e51cbSPeter Avalosspecifies that the line matches if the test does
299327e51cbSPeter Avalos.Em not
300327e51cbSPeter Avalossucceed.
301327e51cbSPeter Avalos.Pp
302327e51cbSPeter AvalosNumeric values are specified in C form; e.g.
303327e51cbSPeter Avalos.Dv 13
304327e51cbSPeter Avalosis decimal,
305327e51cbSPeter Avalos.Dv 013
306327e51cbSPeter Avalosis octal, and
307327e51cbSPeter Avalos.Dv 0x13
308327e51cbSPeter Avalosis hexadecimal.
309327e51cbSPeter Avalos.Pp
31079343712SPeter AvalosFor string values, the string from the
31179343712SPeter Avalosfile must match the specified string.
312327e51cbSPeter AvalosThe operators
313327e51cbSPeter Avalos.Dv = ,
314327e51cbSPeter Avalos.Dv \*[Lt]
315327e51cbSPeter Avalosand
316327e51cbSPeter Avalos.Dv \*[Gt]
317327e51cbSPeter Avalos(but not
318327e51cbSPeter Avalos.Dv \*[Am] )
319327e51cbSPeter Avaloscan be applied to strings.
320327e51cbSPeter AvalosThe length used for matching is that of the string argument
321327e51cbSPeter Avalosin the magic file.
32279343712SPeter AvalosThis means that a line can match any non-empty string (usually used to
32379343712SPeter Avalosthen print the string), with
324327e51cbSPeter Avalos.Em \*[Gt]\e0
32579343712SPeter Avalos(because all non-empty strings are greater than the empty string).
326327e51cbSPeter Avalos.Pp
327327e51cbSPeter AvalosThe special test
328327e51cbSPeter Avalos.Em x
329327e51cbSPeter Avalosalways evaluates to true.
330e4d4ce0cSPeter Avalos.It Dv message
331327e51cbSPeter AvalosThe message to be printed if the comparison succeeds.
332327e51cbSPeter AvalosIf the string contains a
333327e51cbSPeter Avalos.Xr printf 3
334327e51cbSPeter Avalosformat specification, the value from the file (with any specified masking
335327e51cbSPeter Avalosperformed) is printed using the message as the format string.
33679343712SPeter AvalosIf the string begins with
33779343712SPeter Avalos.Dq \eb ,
33879343712SPeter Avalosthe message printed is the remainder of the string with no whitespace
33979343712SPeter Avalosadded before it: multiple matches are normally separated by a single
34079343712SPeter Avalosspace.
341327e51cbSPeter Avalos.El
342327e51cbSPeter Avalos.Pp
34379343712SPeter AvalosAn APPLE 4+4 character APPLE creator and type can be specified as:
34479343712SPeter Avalos.Bd -literal -offset indent
34579343712SPeter Avalos!:apple	CREATYPE
34679343712SPeter Avalos.Ed
34779343712SPeter Avalos.Pp
34879343712SPeter AvalosA MIME type is given on a separate line, which must be the next
34979343712SPeter Avalosnon-blank or comment line after the magic line that identifies the
35079343712SPeter Avalosfile type, and has the following format:
35179343712SPeter Avalos.Bd -literal -offset indent
35279343712SPeter Avalos!:mime	MIMETYPE
35379343712SPeter Avalos.Ed
35479343712SPeter Avalos.Pp
35579343712SPeter Avalosi.e. the literal string
35679343712SPeter Avalos.Dq !:mime
35779343712SPeter Avalosfollowed by the MIME type.
35879343712SPeter Avalos.Pp
35979343712SPeter AvalosAn optional strength can be supplied on a separate line which refers to
36079343712SPeter Avalosthe current magic description using the following format:
36179343712SPeter Avalos.Bd -literal -offset indent
36279343712SPeter Avalos!:strength OP VALUE
36379343712SPeter Avalos.Ed
36479343712SPeter Avalos.Pp
36579343712SPeter AvalosThe operand
36679343712SPeter Avalos.Dv OP
36779343712SPeter Avaloscan be:
36879343712SPeter Avalos.Dv + ,
36979343712SPeter Avalos.Dv - ,
37079343712SPeter Avalos.Dv * ,
37179343712SPeter Avalosor
37279343712SPeter Avalos.Dv /
37379343712SPeter Avalosand
37479343712SPeter Avalos.Dv VALUE
37579343712SPeter Avalosis a constant between 0 and 255.
37679343712SPeter AvalosThis constant is applied using the specified operand
37779343712SPeter Avalosto the currently computed default magic strength.
37879343712SPeter Avalos.Pp
379327e51cbSPeter AvalosSome file formats contain additional information which is to be printed
380327e51cbSPeter Avalosalong with the file type or need additional tests to determine the true
381327e51cbSPeter Avalosfile type.
382327e51cbSPeter AvalosThese additional tests are introduced by one or more
383327e51cbSPeter Avalos.Em \*[Gt]
384327e51cbSPeter Avaloscharacters preceding the offset.
385327e51cbSPeter AvalosThe number of
386327e51cbSPeter Avalos.Em \*[Gt]
387327e51cbSPeter Avaloson the line indicates the level of the test; a line with no
388327e51cbSPeter Avalos.Em \*[Gt]
389327e51cbSPeter Avalosat the beginning is considered to be at level 0.
390327e51cbSPeter AvalosTests are arranged in a tree-like hierarchy:
391e4d4ce0cSPeter Avalosif the test on a line at level
392327e51cbSPeter Avalos.Em n
393327e51cbSPeter Avalossucceeds, all following tests at level
394327e51cbSPeter Avalos.Em n+1
395e4d4ce0cSPeter Avalosare performed, and the messages printed if the tests succeed, until a line
396327e51cbSPeter Avaloswith level
397327e51cbSPeter Avalos.Em n
398327e51cbSPeter Avalos(or less) appears.
399327e51cbSPeter AvalosFor more complex files, one can use empty messages to get just the
400327e51cbSPeter Avalos"if/then" effect, in the following way:
401327e51cbSPeter Avalos.Bd -literal -offset indent
402327e51cbSPeter Avalos0      string   MZ
403327e51cbSPeter Avalos\*[Gt]0x18  leshort  \*[Lt]0x40   MS-DOS executable
404327e51cbSPeter Avalos\*[Gt]0x18  leshort  \*[Gt]0x3f   extended PC executable (e.g., MS Windows)
405327e51cbSPeter Avalos.Ed
406327e51cbSPeter Avalos.Pp
407327e51cbSPeter AvalosOffsets do not need to be constant, but can also be read from the file
408327e51cbSPeter Avalosbeing examined.
409327e51cbSPeter AvalosIf the first character following the last
410327e51cbSPeter Avalos.Em \*[Gt]
411327e51cbSPeter Avalosis a
412e4d4ce0cSPeter Avalos.Em \&(
413327e51cbSPeter Avalosthen the string after the parenthesis is interpreted as an indirect offset.
414327e51cbSPeter AvalosThat means that the number after the parenthesis is used as an offset in
415327e51cbSPeter Avalosthe file.
416327e51cbSPeter AvalosThe value at that offset is read, and is used again as an offset
417327e51cbSPeter Avalosin the file.
418327e51cbSPeter AvalosIndirect offsets are of the form:
41979343712SPeter Avalos.Em (( x [.[bislBISL]][+\-][ y ]) .
420327e51cbSPeter AvalosThe value of
421327e51cbSPeter Avalos.Em x
422327e51cbSPeter Avalosis used as an offset in the file.
42379343712SPeter AvalosA byte, id3 length, short or long is read at that offset depending on the
42479343712SPeter Avalos.Em [bislBISLm]
425327e51cbSPeter Avalostype specifier.
426327e51cbSPeter AvalosThe capitalized types interpret the number as a big endian
427327e51cbSPeter Avalosvalue, whereas the small letter versions interpret the number as a little
428327e51cbSPeter Avalosendian value;
429327e51cbSPeter Avalosthe
430327e51cbSPeter Avalos.Em m
431327e51cbSPeter Avalostype interprets the number as a middle endian (PDP-11) value.
432327e51cbSPeter AvalosTo that number the value of
433327e51cbSPeter Avalos.Em y
434327e51cbSPeter Avalosis added and the result is used as an offset in the file.
435327e51cbSPeter AvalosThe default type if one is not specified is long.
436327e51cbSPeter Avalos.Pp
437327e51cbSPeter AvalosThat way variable length structures can be examined:
438327e51cbSPeter Avalos.Bd -literal -offset indent
439327e51cbSPeter Avalos# MS Windows executables are also valid MS-DOS executables
440327e51cbSPeter Avalos0           string  MZ
441327e51cbSPeter Avalos\*[Gt]0x18       leshort \*[Lt]0x40   MZ executable (MS-DOS)
442327e51cbSPeter Avalos# skip the whole block below if it is not an extended executable
443327e51cbSPeter Avalos\*[Gt]0x18       leshort \*[Gt]0x3f
444327e51cbSPeter Avalos\*[Gt]\*[Gt](0x3c.l)  string  PE\e0\e0  PE executable (MS-Windows)
445327e51cbSPeter Avalos\*[Gt]\*[Gt](0x3c.l)  string  LX\e0\e0  LX executable (OS/2)
446327e51cbSPeter Avalos.Ed
447327e51cbSPeter Avalos.Pp
44879343712SPeter AvalosThis strategy of examining has a drawback: You must make sure that
449327e51cbSPeter Avalosyou eventually print something, or users may get empty output (like, when
450327e51cbSPeter Avalosthere is neither PE\e0\e0 nor LE\e0\e0 in the above example)
451327e51cbSPeter Avalos.Pp
45279343712SPeter AvalosIf this indirect offset cannot be used directly, simple calculations are
453327e51cbSPeter Avalospossible: appending
45479343712SPeter Avalos.Em [+-*/%\*[Am]|^]number
455327e51cbSPeter Avalosinside parentheses allows one to modify
456327e51cbSPeter Avalosthe value read from the file before it is used as an offset:
457327e51cbSPeter Avalos.Bd -literal -offset indent
458327e51cbSPeter Avalos# MS Windows executables are also valid MS-DOS executables
459327e51cbSPeter Avalos0           string  MZ
460327e51cbSPeter Avalos# sometimes, the value at 0x18 is less that 0x40 but there's still an
461327e51cbSPeter Avalos# extended executable, simply appended to the file
462327e51cbSPeter Avalos\*[Gt]0x18       leshort \*[Lt]0x40
463327e51cbSPeter Avalos\*[Gt]\*[Gt](4.s*512) leshort 0x014c  COFF executable (MS-DOS, DJGPP)
464327e51cbSPeter Avalos\*[Gt]\*[Gt](4.s*512) leshort !0x014c MZ executable (MS-DOS)
465327e51cbSPeter Avalos.Ed
466327e51cbSPeter Avalos.Pp
467327e51cbSPeter AvalosSometimes you do not know the exact offset as this depends on the length or
468327e51cbSPeter Avalosposition (when indirection was used before) of preceding fields.
469327e51cbSPeter AvalosYou can specify an offset relative to the end of the last up-level
470327e51cbSPeter Avalosfield using
471327e51cbSPeter Avalos.Sq \*[Am]
472327e51cbSPeter Avalosas a prefix to the offset:
473327e51cbSPeter Avalos.Bd -literal -offset indent
474327e51cbSPeter Avalos0           string  MZ
475327e51cbSPeter Avalos\*[Gt]0x18       leshort \*[Gt]0x3f
476327e51cbSPeter Avalos\*[Gt]\*[Gt](0x3c.l)  string  PE\e0\e0    PE executable (MS-Windows)
477327e51cbSPeter Avalos# immediately following the PE signature is the CPU type
478327e51cbSPeter Avalos\*[Gt]\*[Gt]\*[Gt]\*[Am]0       leshort 0x14c     for Intel 80386
479327e51cbSPeter Avalos\*[Gt]\*[Gt]\*[Gt]\*[Am]0       leshort 0x184     for DEC Alpha
480327e51cbSPeter Avalos.Ed
481327e51cbSPeter Avalos.Pp
482327e51cbSPeter AvalosIndirect and relative offsets can be combined:
483327e51cbSPeter Avalos.Bd -literal -offset indent
484327e51cbSPeter Avalos0             string  MZ
485327e51cbSPeter Avalos\*[Gt]0x18         leshort \*[Lt]0x40
486327e51cbSPeter Avalos\*[Gt]\*[Gt](4.s*512)   leshort !0x014c MZ executable (MS-DOS)
487327e51cbSPeter Avalos# if it's not COFF, go back 512 bytes and add the offset taken
488327e51cbSPeter Avalos# from byte 2/3, which is yet another way of finding the start
489327e51cbSPeter Avalos# of the extended executable
490327e51cbSPeter Avalos\*[Gt]\*[Gt]\*[Gt]\*[Am](2.s-514) string  LE      LE executable (MS Windows VxD driver)
491327e51cbSPeter Avalos.Ed
492327e51cbSPeter Avalos.Pp
493327e51cbSPeter AvalosOr the other way around:
494327e51cbSPeter Avalos.Bd -literal -offset indent
495327e51cbSPeter Avalos0                 string  MZ
496327e51cbSPeter Avalos\*[Gt]0x18             leshort \*[Gt]0x3f
497327e51cbSPeter Avalos\*[Gt]\*[Gt](0x3c.l)        string  LE\e0\e0  LE executable (MS-Windows)
498327e51cbSPeter Avalos# at offset 0x80 (-4, since relative offsets start at the end
499327e51cbSPeter Avalos# of the up-level match) inside the LE header, we find the absolute
500327e51cbSPeter Avalos# offset to the code area, where we look for a specific signature
501327e51cbSPeter Avalos\*[Gt]\*[Gt]\*[Gt](\*[Am]0x7c.l+0x26) string  UPX     \eb, UPX compressed
502327e51cbSPeter Avalos.Ed
503327e51cbSPeter Avalos.Pp
504327e51cbSPeter AvalosOr even both!
505327e51cbSPeter Avalos.Bd -literal -offset indent
506327e51cbSPeter Avalos0                string  MZ
507327e51cbSPeter Avalos\*[Gt]0x18            leshort \*[Gt]0x3f
508327e51cbSPeter Avalos\*[Gt]\*[Gt](0x3c.l)       string  LE\e0\e0 LE executable (MS-Windows)
509327e51cbSPeter Avalos# at offset 0x58 inside the LE header, we find the relative offset
510327e51cbSPeter Avalos# to a data area where we look for a specific signature
511327e51cbSPeter Avalos\*[Gt]\*[Gt]\*[Gt]\*[Am](\*[Am]0x54.l-3)  string  UNACE  \eb, ACE self-extracting archive
512327e51cbSPeter Avalos.Ed
513327e51cbSPeter Avalos.Pp
514327e51cbSPeter AvalosFinally, if you have to deal with offset/length pairs in your file, even the
515327e51cbSPeter Avalossecond value in a parenthesized expression can be taken from the file itself,
516327e51cbSPeter Avalosusing another set of parentheses.
517327e51cbSPeter AvalosNote that this additional indirect offset is always relative to the
518327e51cbSPeter Avalosstart of the main indirect offset.
519327e51cbSPeter Avalos.Bd -literal -offset indent
520327e51cbSPeter Avalos0                 string       MZ
521327e51cbSPeter Avalos\*[Gt]0x18             leshort      \*[Gt]0x3f
522327e51cbSPeter Avalos\*[Gt]\*[Gt](0x3c.l)        string       PE\e0\e0 PE executable (MS-Windows)
523327e51cbSPeter Avalos# search for the PE section called ".idata"...
524327e51cbSPeter Avalos\*[Gt]\*[Gt]\*[Gt]\*[Am]0xf4          search/0x140 .idata
525327e51cbSPeter Avalos# ...and go to the end of it, calculated from start+length;
526327e51cbSPeter Avalos# these are located 14 and 10 bytes after the section name
527327e51cbSPeter Avalos\*[Gt]\*[Gt]\*[Gt]\*[Gt](\*[Am]0xe.l+(-4)) string       PK\e3\e4 \eb, ZIP self-extracting archive
528327e51cbSPeter Avalos.Ed
529327e51cbSPeter Avalos.Sh SEE ALSO
530327e51cbSPeter Avalos.Xr file __CSECTION__
531327e51cbSPeter Avalos\- the command that reads this file.
532327e51cbSPeter Avalos.Sh BUGS
533327e51cbSPeter AvalosThe formats
534327e51cbSPeter Avalos.Dv long ,
535327e51cbSPeter Avalos.Dv belong ,
536327e51cbSPeter Avalos.Dv lelong ,
537327e51cbSPeter Avalos.Dv melong ,
538327e51cbSPeter Avalos.Dv short ,
539327e51cbSPeter Avalos.Dv beshort ,
540327e51cbSPeter Avalos.Dv leshort ,
541327e51cbSPeter Avalos.Dv date ,
542327e51cbSPeter Avalos.Dv bedate ,
543327e51cbSPeter Avalos.Dv medate ,
544327e51cbSPeter Avalos.Dv ledate ,
545327e51cbSPeter Avalos.Dv beldate ,
546327e51cbSPeter Avalos.Dv leldate ,
547327e51cbSPeter Avalosand
548327e51cbSPeter Avalos.Dv meldate
549327e51cbSPeter Avalosare system-dependent; perhaps they should be specified as a number
550327e51cbSPeter Avalosof bytes (2B, 4B, etc),
551327e51cbSPeter Avalossince the files being recognized typically come from
552327e51cbSPeter Avalosa system on which the lengths are invariant.
553327e51cbSPeter Avalos.\"
554327e51cbSPeter Avalos.\" From: guy@sun.uucp (Guy Harris)
555327e51cbSPeter Avalos.\" Newsgroups: net.bugs.usg
556327e51cbSPeter Avalos.\" Subject: /etc/magic's format isn't well documented
557327e51cbSPeter Avalos.\" Message-ID: <2752@sun.uucp>
558327e51cbSPeter Avalos.\" Date: 3 Sep 85 08:19:07 GMT
559327e51cbSPeter Avalos.\" Organization: Sun Microsystems, Inc.
560327e51cbSPeter Avalos.\" Lines: 136
561327e51cbSPeter Avalos.\"
562327e51cbSPeter Avalos.\" Here's a manual page for the format accepted by the "file" made by adding
563327e51cbSPeter Avalos.\" the changes I posted to the S5R2 version.
564327e51cbSPeter Avalos.\"
565327e51cbSPeter Avalos.\" Modified for Ian Darwin's version of the file command.
566