xref: /dflybsd-src/contrib/file/doc/magic.man (revision 884044a574904c56cc0441938f957625f5153d6c)
1*884044a5SPeter Avalos.\" $File: magic.man,v 1.68 2011/04/20 19:08:44 christos Exp $
2*884044a5SPeter 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
54*884044a5SPeter Avalosby /[WwcCtb]*.
55327e51cbSPeter AvalosThe
56*884044a5SPeter 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
65*884044a5SPeter Avalos.Dq w
66327e51cbSPeter Avalosflag treats every blank in the target as an optional blank.
67*884044a5SPeter 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.
73*884044a5SPeter AvalosThe
74*884044a5SPeter Avalos.Dq C
75*884044a5SPeter Avalosflag, specifies case insensitive matching: upper case
76*884044a5SPeter Avaloscharacters in the magic match both lower and upper case characters in the
77*884044a5SPeter Avalostarget, whereas lower case characters in the magic only match upper case
78*884044a5SPeter Avaloscharacters in the target.
79*884044a5SPeter AvalosTo do a complete case insensitive match, specify both
80*884044a5SPeter Avalos.Dq c
81*884044a5SPeter Avalosand
82*884044a5SPeter Avalos.Dq C .
83*884044a5SPeter AvalosThe
84*884044a5SPeter Avalos.Dq t
85*884044a5SPeter Avalosflag, forces the test to be done for text files, while the
86*884044a5SPeter Avalos.Dq b
87*884044a5SPeter 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
19379343712SPeter Avalos(like egrep). Regular expressions can take exponential time to
19479343712SPeter Avalosprocess, and their performance is hard to predict, so their use is
19579343712SPeter Avalosdiscouraged. When used in production environments, their performance
19679343712SPeter Avalosshould be carefully checked. The type specification can be optionally
19779343712SPeter Avalosfollowed by
19879343712SPeter Avalos.Dv /[c][s] .
199327e51cbSPeter AvalosThe
200327e51cbSPeter Avalos.Dq c
201327e51cbSPeter Avalosflag makes the match case insensitive, while the
202327e51cbSPeter Avalos.Dq s
20379343712SPeter Avalosflag update the offset to the start offset of the match, rather than the end.
20479343712SPeter AvalosThe regular expression is tested against line
20579343712SPeter Avalos.Dv N + 1
20679343712SPeter Avalosonwards, where
207327e51cbSPeter Avalos.Dv N
20879343712SPeter Avalosis the given offset.
20979343712SPeter AvalosLine endings are assumed to be in the machine's native format.
210327e51cbSPeter Avalos.Dv ^
211327e51cbSPeter Avalosand
212327e51cbSPeter Avalos.Dv $
21379343712SPeter Avalosmatch the beginning and end of individual lines, respectively,
214327e51cbSPeter Avalosnot beginning and end of file.
215327e51cbSPeter Avalos.It Dv search
21679343712SPeter AvalosA literal string search starting at the given offset. The same
21779343712SPeter Avalosmodifier flags can be used as for string patterns. The modifier flags
21879343712SPeter Avalos(if any) must be followed by
21979343712SPeter Avalos.Dv /number
22079343712SPeter Avalosthe range, that is, the number of positions at which the match will be
22179343712SPeter Avalosattempted, starting from the start offset. This is suitable for
22279343712SPeter Avalossearching larger binary expressions with variable offsets, using
223327e51cbSPeter Avalos.Dv \e
22479343712SPeter Avalosescapes for special characters. The offset works as for regex.
225327e51cbSPeter Avalos.It Dv default
22679343712SPeter AvalosThis is intended to be used with the test
22779343712SPeter Avalos.Em x
228327e51cbSPeter Avalos(which is always true) and a message that is to be used if there are
229327e51cbSPeter Avalosno other matches.
230327e51cbSPeter Avalos.El
23179343712SPeter Avalos.Pp
23279343712SPeter AvalosEach top-level magic pattern (see below for an explanation of levels)
23379343712SPeter Avalosis classified as text or binary according to the types used. Types
23479343712SPeter Avalos.Dq regex
23579343712SPeter Avalosand
23679343712SPeter Avalos.Dq search
23779343712SPeter Avalosare classified as text tests, unless non-printable characters are used
23879343712SPeter Avalosin the pattern. All other tests are classified as binary. A top-level
23979343712SPeter Avalospattern is considered to be a test text when all its patterns are text
24079343712SPeter Avalospatterns; otherwise, it is considered to be a binary pattern. When
24179343712SPeter Avalosmatching a file, binary patterns are tried first; if no match is
24279343712SPeter Avalosfound, and the file looks like text, then its encoding is determined
24379343712SPeter Avalosand the text patterns are tried.
244327e51cbSPeter Avalos.Pp
245327e51cbSPeter AvalosThe numeric types may optionally be followed by
246327e51cbSPeter Avalos.Dv \*[Am]
247327e51cbSPeter Avalosand a numeric value,
248327e51cbSPeter Avalosto specify that the value is to be AND'ed with the
249327e51cbSPeter Avalosnumeric value before any comparisons are done.
250327e51cbSPeter AvalosPrepending a
251327e51cbSPeter Avalos.Dv u
252327e51cbSPeter Avalosto the type indicates that ordered comparisons should be unsigned.
253327e51cbSPeter Avalos.It Dv test
254327e51cbSPeter AvalosThe value to be compared with the value from the file.
255327e51cbSPeter AvalosIf the type is
256327e51cbSPeter Avalosnumeric, this value
257327e51cbSPeter Avalosis specified in C form; if it is a string, it is specified as a C string
258327e51cbSPeter Avaloswith the usual escapes permitted (e.g. \en for new-line).
259327e51cbSPeter Avalos.Pp
260327e51cbSPeter AvalosNumeric values
261327e51cbSPeter Avalosmay be preceded by a character indicating the operation to be performed.
262327e51cbSPeter AvalosIt may be
263327e51cbSPeter Avalos.Dv = ,
264327e51cbSPeter Avalosto specify that the value from the file must equal the specified value,
265327e51cbSPeter Avalos.Dv \*[Lt] ,
266327e51cbSPeter Avalosto specify that the value from the file must be less than the specified
267327e51cbSPeter Avalosvalue,
268327e51cbSPeter Avalos.Dv \*[Gt] ,
269327e51cbSPeter Avalosto specify that the value from the file must be greater than the specified
270327e51cbSPeter Avalosvalue,
271327e51cbSPeter Avalos.Dv \*[Am] ,
272327e51cbSPeter Avalosto specify that the value from the file must have set all of the bits
273327e51cbSPeter Avalosthat are set in the specified value,
274327e51cbSPeter Avalos.Dv ^ ,
275327e51cbSPeter Avalosto specify that the value from the file must have clear any of the bits
276327e51cbSPeter Avalosthat are set in the specified value, or
277327e51cbSPeter Avalos.Dv ~ ,
278327e51cbSPeter Avalosthe value specified after is negated before tested.
279327e51cbSPeter Avalos.Dv x ,
280327e51cbSPeter Avalosto specify that any value will match.
281327e51cbSPeter AvalosIf the character is omitted, it is assumed to be
282327e51cbSPeter Avalos.Dv = .
283327e51cbSPeter AvalosOperators
284327e51cbSPeter Avalos.Dv \*[Am] ,
285327e51cbSPeter Avalos.Dv ^ ,
286327e51cbSPeter Avalosand
287327e51cbSPeter Avalos.Dv ~
288327e51cbSPeter Avalosdon't work with floats and doubles.
28979343712SPeter AvalosThe operator
29079343712SPeter Avalos.Dv !\&
291327e51cbSPeter Avalosspecifies that the line matches if the test does
292327e51cbSPeter Avalos.Em not
293327e51cbSPeter Avalossucceed.
294327e51cbSPeter Avalos.Pp
295327e51cbSPeter AvalosNumeric values are specified in C form; e.g.
296327e51cbSPeter Avalos.Dv 13
297327e51cbSPeter Avalosis decimal,
298327e51cbSPeter Avalos.Dv 013
299327e51cbSPeter Avalosis octal, and
300327e51cbSPeter Avalos.Dv 0x13
301327e51cbSPeter Avalosis hexadecimal.
302327e51cbSPeter Avalos.Pp
30379343712SPeter AvalosFor string values, the string from the
30479343712SPeter Avalosfile must match the specified string.
305327e51cbSPeter AvalosThe operators
306327e51cbSPeter Avalos.Dv = ,
307327e51cbSPeter Avalos.Dv \*[Lt]
308327e51cbSPeter Avalosand
309327e51cbSPeter Avalos.Dv \*[Gt]
310327e51cbSPeter Avalos(but not
311327e51cbSPeter Avalos.Dv \*[Am] )
312327e51cbSPeter Avaloscan be applied to strings.
313327e51cbSPeter AvalosThe length used for matching is that of the string argument
314327e51cbSPeter Avalosin the magic file.
31579343712SPeter AvalosThis means that a line can match any non-empty string (usually used to
31679343712SPeter Avalosthen print the string), with
317327e51cbSPeter Avalos.Em \*[Gt]\e0
31879343712SPeter Avalos(because all non-empty strings are greater than the empty string).
319327e51cbSPeter Avalos.Pp
320327e51cbSPeter AvalosThe special test
321327e51cbSPeter Avalos.Em x
322327e51cbSPeter Avalosalways evaluates to true.
323e4d4ce0cSPeter Avalos.It Dv message
324327e51cbSPeter AvalosThe message to be printed if the comparison succeeds.
325327e51cbSPeter AvalosIf the string contains a
326327e51cbSPeter Avalos.Xr printf 3
327327e51cbSPeter Avalosformat specification, the value from the file (with any specified masking
328327e51cbSPeter Avalosperformed) is printed using the message as the format string.
32979343712SPeter AvalosIf the string begins with
33079343712SPeter Avalos.Dq \eb ,
33179343712SPeter Avalosthe message printed is the remainder of the string with no whitespace
33279343712SPeter Avalosadded before it: multiple matches are normally separated by a single
33379343712SPeter Avalosspace.
334327e51cbSPeter Avalos.El
335327e51cbSPeter Avalos.Pp
33679343712SPeter AvalosAn APPLE 4+4 character APPLE creator and type can be specified as:
33779343712SPeter Avalos.Bd -literal -offset indent
33879343712SPeter Avalos!:apple	CREATYPE
33979343712SPeter Avalos.Ed
34079343712SPeter Avalos.Pp
34179343712SPeter AvalosA MIME type is given on a separate line, which must be the next
34279343712SPeter Avalosnon-blank or comment line after the magic line that identifies the
34379343712SPeter Avalosfile type, and has the following format:
34479343712SPeter Avalos.Bd -literal -offset indent
34579343712SPeter Avalos!:mime	MIMETYPE
34679343712SPeter Avalos.Ed
34779343712SPeter Avalos.Pp
34879343712SPeter Avalosi.e. the literal string
34979343712SPeter Avalos.Dq !:mime
35079343712SPeter Avalosfollowed by the MIME type.
35179343712SPeter Avalos.Pp
35279343712SPeter AvalosAn optional strength can be supplied on a separate line which refers to
35379343712SPeter Avalosthe current magic description using the following format:
35479343712SPeter Avalos.Bd -literal -offset indent
35579343712SPeter Avalos!:strength OP VALUE
35679343712SPeter Avalos.Ed
35779343712SPeter Avalos.Pp
35879343712SPeter AvalosThe operand
35979343712SPeter Avalos.Dv OP
36079343712SPeter Avaloscan be:
36179343712SPeter Avalos.Dv + ,
36279343712SPeter Avalos.Dv - ,
36379343712SPeter Avalos.Dv * ,
36479343712SPeter Avalosor
36579343712SPeter Avalos.Dv /
36679343712SPeter Avalosand
36779343712SPeter Avalos.Dv VALUE
36879343712SPeter Avalosis a constant between 0 and 255.
36979343712SPeter AvalosThis constant is applied using the specified operand
37079343712SPeter Avalosto the currently computed default magic strength.
37179343712SPeter Avalos.Pp
372327e51cbSPeter AvalosSome file formats contain additional information which is to be printed
373327e51cbSPeter Avalosalong with the file type or need additional tests to determine the true
374327e51cbSPeter Avalosfile type.
375327e51cbSPeter AvalosThese additional tests are introduced by one or more
376327e51cbSPeter Avalos.Em \*[Gt]
377327e51cbSPeter Avaloscharacters preceding the offset.
378327e51cbSPeter AvalosThe number of
379327e51cbSPeter Avalos.Em \*[Gt]
380327e51cbSPeter Avaloson the line indicates the level of the test; a line with no
381327e51cbSPeter Avalos.Em \*[Gt]
382327e51cbSPeter Avalosat the beginning is considered to be at level 0.
383327e51cbSPeter AvalosTests are arranged in a tree-like hierarchy:
384e4d4ce0cSPeter Avalosif the test on a line at level
385327e51cbSPeter Avalos.Em n
386327e51cbSPeter Avalossucceeds, all following tests at level
387327e51cbSPeter Avalos.Em n+1
388e4d4ce0cSPeter Avalosare performed, and the messages printed if the tests succeed, until a line
389327e51cbSPeter Avaloswith level
390327e51cbSPeter Avalos.Em n
391327e51cbSPeter Avalos(or less) appears.
392327e51cbSPeter AvalosFor more complex files, one can use empty messages to get just the
393327e51cbSPeter Avalos"if/then" effect, in the following way:
394327e51cbSPeter Avalos.Bd -literal -offset indent
395327e51cbSPeter Avalos0      string   MZ
396327e51cbSPeter Avalos\*[Gt]0x18  leshort  \*[Lt]0x40   MS-DOS executable
397327e51cbSPeter Avalos\*[Gt]0x18  leshort  \*[Gt]0x3f   extended PC executable (e.g., MS Windows)
398327e51cbSPeter Avalos.Ed
399327e51cbSPeter Avalos.Pp
400327e51cbSPeter AvalosOffsets do not need to be constant, but can also be read from the file
401327e51cbSPeter Avalosbeing examined.
402327e51cbSPeter AvalosIf the first character following the last
403327e51cbSPeter Avalos.Em \*[Gt]
404327e51cbSPeter Avalosis a
405e4d4ce0cSPeter Avalos.Em \&(
406327e51cbSPeter Avalosthen the string after the parenthesis is interpreted as an indirect offset.
407327e51cbSPeter AvalosThat means that the number after the parenthesis is used as an offset in
408327e51cbSPeter Avalosthe file.
409327e51cbSPeter AvalosThe value at that offset is read, and is used again as an offset
410327e51cbSPeter Avalosin the file.
411327e51cbSPeter AvalosIndirect offsets are of the form:
41279343712SPeter Avalos.Em (( x [.[bislBISL]][+\-][ y ]) .
413327e51cbSPeter AvalosThe value of
414327e51cbSPeter Avalos.Em x
415327e51cbSPeter Avalosis used as an offset in the file.
41679343712SPeter AvalosA byte, id3 length, short or long is read at that offset depending on the
41779343712SPeter Avalos.Em [bislBISLm]
418327e51cbSPeter Avalostype specifier.
419327e51cbSPeter AvalosThe capitalized types interpret the number as a big endian
420327e51cbSPeter Avalosvalue, whereas the small letter versions interpret the number as a little
421327e51cbSPeter Avalosendian value;
422327e51cbSPeter Avalosthe
423327e51cbSPeter Avalos.Em m
424327e51cbSPeter Avalostype interprets the number as a middle endian (PDP-11) value.
425327e51cbSPeter AvalosTo that number the value of
426327e51cbSPeter Avalos.Em y
427327e51cbSPeter Avalosis added and the result is used as an offset in the file.
428327e51cbSPeter AvalosThe default type if one is not specified is long.
429327e51cbSPeter Avalos.Pp
430327e51cbSPeter AvalosThat way variable length structures can be examined:
431327e51cbSPeter Avalos.Bd -literal -offset indent
432327e51cbSPeter Avalos# MS Windows executables are also valid MS-DOS executables
433327e51cbSPeter Avalos0           string  MZ
434327e51cbSPeter Avalos\*[Gt]0x18       leshort \*[Lt]0x40   MZ executable (MS-DOS)
435327e51cbSPeter Avalos# skip the whole block below if it is not an extended executable
436327e51cbSPeter Avalos\*[Gt]0x18       leshort \*[Gt]0x3f
437327e51cbSPeter Avalos\*[Gt]\*[Gt](0x3c.l)  string  PE\e0\e0  PE executable (MS-Windows)
438327e51cbSPeter Avalos\*[Gt]\*[Gt](0x3c.l)  string  LX\e0\e0  LX executable (OS/2)
439327e51cbSPeter Avalos.Ed
440327e51cbSPeter Avalos.Pp
44179343712SPeter AvalosThis strategy of examining has a drawback: You must make sure that
442327e51cbSPeter Avalosyou eventually print something, or users may get empty output (like, when
443327e51cbSPeter Avalosthere is neither PE\e0\e0 nor LE\e0\e0 in the above example)
444327e51cbSPeter Avalos.Pp
44579343712SPeter AvalosIf this indirect offset cannot be used directly, simple calculations are
446327e51cbSPeter Avalospossible: appending
44779343712SPeter Avalos.Em [+-*/%\*[Am]|^]number
448327e51cbSPeter Avalosinside parentheses allows one to modify
449327e51cbSPeter Avalosthe value read from the file before it is used as an offset:
450327e51cbSPeter Avalos.Bd -literal -offset indent
451327e51cbSPeter Avalos# MS Windows executables are also valid MS-DOS executables
452327e51cbSPeter Avalos0           string  MZ
453327e51cbSPeter Avalos# sometimes, the value at 0x18 is less that 0x40 but there's still an
454327e51cbSPeter Avalos# extended executable, simply appended to the file
455327e51cbSPeter Avalos\*[Gt]0x18       leshort \*[Lt]0x40
456327e51cbSPeter Avalos\*[Gt]\*[Gt](4.s*512) leshort 0x014c  COFF executable (MS-DOS, DJGPP)
457327e51cbSPeter Avalos\*[Gt]\*[Gt](4.s*512) leshort !0x014c MZ executable (MS-DOS)
458327e51cbSPeter Avalos.Ed
459327e51cbSPeter Avalos.Pp
460327e51cbSPeter AvalosSometimes you do not know the exact offset as this depends on the length or
461327e51cbSPeter Avalosposition (when indirection was used before) of preceding fields.
462327e51cbSPeter AvalosYou can specify an offset relative to the end of the last up-level
463327e51cbSPeter Avalosfield using
464327e51cbSPeter Avalos.Sq \*[Am]
465327e51cbSPeter Avalosas a prefix to the offset:
466327e51cbSPeter Avalos.Bd -literal -offset indent
467327e51cbSPeter Avalos0           string  MZ
468327e51cbSPeter Avalos\*[Gt]0x18       leshort \*[Gt]0x3f
469327e51cbSPeter Avalos\*[Gt]\*[Gt](0x3c.l)  string  PE\e0\e0    PE executable (MS-Windows)
470327e51cbSPeter Avalos# immediately following the PE signature is the CPU type
471327e51cbSPeter Avalos\*[Gt]\*[Gt]\*[Gt]\*[Am]0       leshort 0x14c     for Intel 80386
472327e51cbSPeter Avalos\*[Gt]\*[Gt]\*[Gt]\*[Am]0       leshort 0x184     for DEC Alpha
473327e51cbSPeter Avalos.Ed
474327e51cbSPeter Avalos.Pp
475327e51cbSPeter AvalosIndirect and relative offsets can be combined:
476327e51cbSPeter Avalos.Bd -literal -offset indent
477327e51cbSPeter Avalos0             string  MZ
478327e51cbSPeter Avalos\*[Gt]0x18         leshort \*[Lt]0x40
479327e51cbSPeter Avalos\*[Gt]\*[Gt](4.s*512)   leshort !0x014c MZ executable (MS-DOS)
480327e51cbSPeter Avalos# if it's not COFF, go back 512 bytes and add the offset taken
481327e51cbSPeter Avalos# from byte 2/3, which is yet another way of finding the start
482327e51cbSPeter Avalos# of the extended executable
483327e51cbSPeter Avalos\*[Gt]\*[Gt]\*[Gt]\*[Am](2.s-514) string  LE      LE executable (MS Windows VxD driver)
484327e51cbSPeter Avalos.Ed
485327e51cbSPeter Avalos.Pp
486327e51cbSPeter AvalosOr the other way around:
487327e51cbSPeter Avalos.Bd -literal -offset indent
488327e51cbSPeter Avalos0                 string  MZ
489327e51cbSPeter Avalos\*[Gt]0x18             leshort \*[Gt]0x3f
490327e51cbSPeter Avalos\*[Gt]\*[Gt](0x3c.l)        string  LE\e0\e0  LE executable (MS-Windows)
491327e51cbSPeter Avalos# at offset 0x80 (-4, since relative offsets start at the end
492327e51cbSPeter Avalos# of the up-level match) inside the LE header, we find the absolute
493327e51cbSPeter Avalos# offset to the code area, where we look for a specific signature
494327e51cbSPeter Avalos\*[Gt]\*[Gt]\*[Gt](\*[Am]0x7c.l+0x26) string  UPX     \eb, UPX compressed
495327e51cbSPeter Avalos.Ed
496327e51cbSPeter Avalos.Pp
497327e51cbSPeter AvalosOr even both!
498327e51cbSPeter Avalos.Bd -literal -offset indent
499327e51cbSPeter Avalos0                string  MZ
500327e51cbSPeter Avalos\*[Gt]0x18            leshort \*[Gt]0x3f
501327e51cbSPeter Avalos\*[Gt]\*[Gt](0x3c.l)       string  LE\e0\e0 LE executable (MS-Windows)
502327e51cbSPeter Avalos# at offset 0x58 inside the LE header, we find the relative offset
503327e51cbSPeter Avalos# to a data area where we look for a specific signature
504327e51cbSPeter Avalos\*[Gt]\*[Gt]\*[Gt]\*[Am](\*[Am]0x54.l-3)  string  UNACE  \eb, ACE self-extracting archive
505327e51cbSPeter Avalos.Ed
506327e51cbSPeter Avalos.Pp
507327e51cbSPeter AvalosFinally, if you have to deal with offset/length pairs in your file, even the
508327e51cbSPeter Avalossecond value in a parenthesized expression can be taken from the file itself,
509327e51cbSPeter Avalosusing another set of parentheses.
510327e51cbSPeter AvalosNote that this additional indirect offset is always relative to the
511327e51cbSPeter Avalosstart of the main indirect offset.
512327e51cbSPeter Avalos.Bd -literal -offset indent
513327e51cbSPeter Avalos0                 string       MZ
514327e51cbSPeter Avalos\*[Gt]0x18             leshort      \*[Gt]0x3f
515327e51cbSPeter Avalos\*[Gt]\*[Gt](0x3c.l)        string       PE\e0\e0 PE executable (MS-Windows)
516327e51cbSPeter Avalos# search for the PE section called ".idata"...
517327e51cbSPeter Avalos\*[Gt]\*[Gt]\*[Gt]\*[Am]0xf4          search/0x140 .idata
518327e51cbSPeter Avalos# ...and go to the end of it, calculated from start+length;
519327e51cbSPeter Avalos# these are located 14 and 10 bytes after the section name
520327e51cbSPeter Avalos\*[Gt]\*[Gt]\*[Gt]\*[Gt](\*[Am]0xe.l+(-4)) string       PK\e3\e4 \eb, ZIP self-extracting archive
521327e51cbSPeter Avalos.Ed
522327e51cbSPeter Avalos.Sh SEE ALSO
523327e51cbSPeter Avalos.Xr file __CSECTION__
524327e51cbSPeter Avalos\- the command that reads this file.
525327e51cbSPeter Avalos.Sh BUGS
526327e51cbSPeter AvalosThe formats
527327e51cbSPeter Avalos.Dv long ,
528327e51cbSPeter Avalos.Dv belong ,
529327e51cbSPeter Avalos.Dv lelong ,
530327e51cbSPeter Avalos.Dv melong ,
531327e51cbSPeter Avalos.Dv short ,
532327e51cbSPeter Avalos.Dv beshort ,
533327e51cbSPeter Avalos.Dv leshort ,
534327e51cbSPeter Avalos.Dv date ,
535327e51cbSPeter Avalos.Dv bedate ,
536327e51cbSPeter Avalos.Dv medate ,
537327e51cbSPeter Avalos.Dv ledate ,
538327e51cbSPeter Avalos.Dv beldate ,
539327e51cbSPeter Avalos.Dv leldate ,
540327e51cbSPeter Avalosand
541327e51cbSPeter Avalos.Dv meldate
542327e51cbSPeter Avalosare system-dependent; perhaps they should be specified as a number
543327e51cbSPeter Avalosof bytes (2B, 4B, etc),
544327e51cbSPeter Avalossince the files being recognized typically come from
545327e51cbSPeter Avalosa system on which the lengths are invariant.
546327e51cbSPeter Avalos.\"
547327e51cbSPeter Avalos.\" From: guy@sun.uucp (Guy Harris)
548327e51cbSPeter Avalos.\" Newsgroups: net.bugs.usg
549327e51cbSPeter Avalos.\" Subject: /etc/magic's format isn't well documented
550327e51cbSPeter Avalos.\" Message-ID: <2752@sun.uucp>
551327e51cbSPeter Avalos.\" Date: 3 Sep 85 08:19:07 GMT
552327e51cbSPeter Avalos.\" Organization: Sun Microsystems, Inc.
553327e51cbSPeter Avalos.\" Lines: 136
554327e51cbSPeter Avalos.\"
555327e51cbSPeter Avalos.\" Here's a manual page for the format accepted by the "file" made by adding
556327e51cbSPeter Avalos.\" the changes I posted to the S5R2 version.
557327e51cbSPeter Avalos.\"
558327e51cbSPeter Avalos.\" Modified for Ian Darwin's version of the file command.
559