xref: /minix3/external/bsd/file/dist/doc/magic.5 (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc.\"	$NetBSD: magic.5,v 1.14 2015/01/02 21:15:32 christos Exp $
2ef01931fSBen Gras.\"
3*0a6a1f1dSLionel Sambuc.\" $File: magic.man,v 1.85 2015/01/01 17:07:34 christos Exp $
4*0a6a1f1dSLionel Sambuc.Dd January 1, 2015
5ef01931fSBen Gras.Dt MAGIC 5
6ef01931fSBen Gras.Os
7ef01931fSBen Gras.\" install as magic.4 on USG, magic.5 on V7, Berkeley and Linux systems.
8ef01931fSBen Gras.Sh NAME
9ef01931fSBen Gras.Nm magic
10ef01931fSBen Gras.Nd file command's magic pattern file
11ef01931fSBen Gras.Sh DESCRIPTION
12ef01931fSBen GrasThis manual page documents the format of the magic file as
13ef01931fSBen Grasused by the
14ef01931fSBen Gras.Xr file 1
15*0a6a1f1dSLionel Sambuccommand, version 5.22.
16ef01931fSBen GrasThe
17ef01931fSBen Gras.Xr file 1
18ef01931fSBen Grascommand identifies the type of a file using,
19ef01931fSBen Grasamong other tests,
20ef01931fSBen Grasa test for whether the file contains certain
21ef01931fSBen Gras.Dq "magic patterns" .
22ef01931fSBen GrasThe file
23ef01931fSBen Gras.Pa /usr/share/misc/magic
24ef01931fSBen Grasspecifies what patterns are to be tested for, what message or
25ef01931fSBen GrasMIME type to print if a particular pattern is found,
26ef01931fSBen Grasand additional information to extract from the file.
27ef01931fSBen Gras.Pp
28ef01931fSBen GrasEach line of the file specifies a test to be performed.
29ef01931fSBen GrasA test compares the data starting at a particular offset
30ef01931fSBen Grasin the file with a byte value, a string or a numeric value.
31ef01931fSBen GrasIf the test succeeds, a message is printed.
32ef01931fSBen GrasThe line consists of the following fields:
33ef01931fSBen Gras.Bl -tag -width ".Dv message"
34ef01931fSBen Gras.It Dv offset
35ef01931fSBen GrasA number specifying the offset, in bytes, into the file of the data
36ef01931fSBen Graswhich is to be tested.
37ef01931fSBen Gras.It Dv type
38ef01931fSBen GrasThe type of the data to be tested.
39ef01931fSBen GrasThe possible values are:
40ef01931fSBen Gras.Bl -tag -width ".Dv lestring16"
41ef01931fSBen Gras.It Dv byte
42ef01931fSBen GrasA one-byte value.
43ef01931fSBen Gras.It Dv short
44ef01931fSBen GrasA two-byte value in this machine's native byte order.
45ef01931fSBen Gras.It Dv long
46ef01931fSBen GrasA four-byte value in this machine's native byte order.
47ef01931fSBen Gras.It Dv quad
48ef01931fSBen GrasAn eight-byte value in this machine's native byte order.
49ef01931fSBen Gras.It Dv float
50ef01931fSBen GrasA 32-bit single precision IEEE floating point number in this machine's native byte order.
51ef01931fSBen Gras.It Dv double
52ef01931fSBen GrasA 64-bit double precision IEEE floating point number in this machine's native byte order.
53ef01931fSBen Gras.It Dv string
54ef01931fSBen GrasA string of bytes.
55ef01931fSBen GrasThe string type specification can be optionally followed
5684d9c625SLionel Sambucby /[WwcCtbT]*.
57ef01931fSBen GrasThe
58835f6802SDirk Vogt.Dq W
59ef01931fSBen Grasflag compacts whitespace in the target, which must
60ef01931fSBen Grascontain at least one whitespace character.
61ef01931fSBen GrasIf the magic has
62ef01931fSBen Gras.Dv n
63ef01931fSBen Grasconsecutive blanks, the target needs at least
64ef01931fSBen Gras.Dv n
65ef01931fSBen Grasconsecutive blanks to match.
66ef01931fSBen GrasThe
67835f6802SDirk Vogt.Dq w
6808ff44c4SLionel Sambucflag treats every blank in the magic as an optional blank.
69835f6802SDirk VogtThe
70ef01931fSBen Gras.Dq c
7108ff44c4SLionel Sambucflag specifies case insensitive matching: lower case
72ef01931fSBen Grascharacters in the magic match both lower and upper case characters in the
73ef01931fSBen Grastarget, whereas upper case characters in the magic only match upper case
74ef01931fSBen Grascharacters in the target.
75835f6802SDirk VogtThe
76835f6802SDirk Vogt.Dq C
7708ff44c4SLionel Sambucflag specifies case insensitive matching: upper case
78835f6802SDirk Vogtcharacters in the magic match both lower and upper case characters in the
79835f6802SDirk Vogttarget, whereas lower case characters in the magic only match upper case
80835f6802SDirk Vogtcharacters in the target.
81835f6802SDirk VogtTo do a complete case insensitive match, specify both
82835f6802SDirk Vogt.Dq c
83835f6802SDirk Vogtand
84835f6802SDirk Vogt.Dq C .
85835f6802SDirk VogtThe
86835f6802SDirk Vogt.Dq t
8708ff44c4SLionel Sambucflag forces the test to be done for text files, while the
88835f6802SDirk Vogt.Dq b
8908ff44c4SLionel Sambucflag forces the test to be done for binary files.
9084d9c625SLionel SambucThe
9184d9c625SLionel Sambuc.Dq T
9284d9c625SLionel Sambucflag causes the string to be trimmed, i.e. leading and trailing whitespace
9384d9c625SLionel Sambucis deleted before the string is printed.
94ef01931fSBen Gras.It Dv pstring
9584d9c625SLionel SambucA Pascal-style string where the first byte/short/int is interpreted as the
96ef01931fSBen Grasunsigned length.
97835f6802SDirk VogtThe length defaults to byte and can be specified as a modifier.
98835f6802SDirk VogtThe following modifiers are supported:
99835f6802SDirk Vogt.Bl -tag -compact -width B
100835f6802SDirk Vogt.It B
101835f6802SDirk VogtA byte length (default).
102835f6802SDirk Vogt.It H
103835f6802SDirk VogtA 2 byte big endian length.
104835f6802SDirk Vogt.It h
105835f6802SDirk VogtA 2 byte big little length.
106835f6802SDirk Vogt.It L
107835f6802SDirk VogtA 4 byte big endian length.
108835f6802SDirk Vogt.It l
109835f6802SDirk VogtA 4 byte big little length.
110835f6802SDirk Vogt.It J
111835f6802SDirk VogtThe length includes itself in its count.
112835f6802SDirk Vogt.El
113ef01931fSBen GrasThe string is not NUL terminated.
114835f6802SDirk Vogt.Dq J
115835f6802SDirk Vogtis used rather than the more
116835f6802SDirk Vogtvaluable
117835f6802SDirk Vogt.Dq I
118835f6802SDirk Vogtbecause this type of length is a feature of the JPEG
119835f6802SDirk Vogtformat.
120ef01931fSBen Gras.It Dv date
121ef01931fSBen GrasA four-byte value interpreted as a UNIX date.
122ef01931fSBen Gras.It Dv qdate
123ef01931fSBen GrasA eight-byte value interpreted as a UNIX date.
124ef01931fSBen Gras.It Dv ldate
125ef01931fSBen GrasA four-byte value interpreted as a UNIX-style date, but interpreted as
126ef01931fSBen Graslocal time rather than UTC.
127ef01931fSBen Gras.It Dv qldate
128ef01931fSBen GrasAn eight-byte value interpreted as a UNIX-style date, but interpreted as
129ef01931fSBen Graslocal time rather than UTC.
13084d9c625SLionel Sambuc.It Dv qwdate
13184d9c625SLionel SambucAn eight-byte value interpreted as a Windows-style date.
132ef01931fSBen Gras.It Dv beid3
133ef01931fSBen GrasA 32-bit ID3 length in big-endian byte order.
134ef01931fSBen Gras.It Dv beshort
135ef01931fSBen GrasA two-byte value in big-endian byte order.
136ef01931fSBen Gras.It Dv belong
137ef01931fSBen GrasA four-byte value in big-endian byte order.
138ef01931fSBen Gras.It Dv bequad
139ef01931fSBen GrasAn eight-byte value in big-endian byte order.
140ef01931fSBen Gras.It Dv befloat
141ef01931fSBen GrasA 32-bit single precision IEEE floating point number in big-endian byte order.
142ef01931fSBen Gras.It Dv bedouble
143ef01931fSBen GrasA 64-bit double precision IEEE floating point number in big-endian byte order.
144ef01931fSBen Gras.It Dv bedate
145ef01931fSBen GrasA four-byte value in big-endian byte order,
146ef01931fSBen Grasinterpreted as a Unix date.
147ef01931fSBen Gras.It Dv beqdate
148ef01931fSBen GrasAn eight-byte value in big-endian byte order,
149ef01931fSBen Grasinterpreted as a Unix date.
150ef01931fSBen Gras.It Dv beldate
151ef01931fSBen GrasA four-byte value in big-endian byte order,
152ef01931fSBen Grasinterpreted as a UNIX-style date, but interpreted as local time rather
153ef01931fSBen Grasthan UTC.
154ef01931fSBen Gras.It Dv beqldate
155ef01931fSBen GrasAn eight-byte value in big-endian byte order,
156ef01931fSBen Grasinterpreted as a UNIX-style date, but interpreted as local time rather
157ef01931fSBen Grasthan UTC.
15884d9c625SLionel Sambuc.It Dv beqwdate
15984d9c625SLionel SambucAn eight-byte value in big-endian byte order,
16084d9c625SLionel Sambucinterpreted as a Windows-style date.
161ef01931fSBen Gras.It Dv bestring16
162ef01931fSBen GrasA two-byte unicode (UCS16) string in big-endian byte order.
163ef01931fSBen Gras.It Dv leid3
164ef01931fSBen GrasA 32-bit ID3 length in little-endian byte order.
165ef01931fSBen Gras.It Dv leshort
166ef01931fSBen GrasA two-byte value in little-endian byte order.
167ef01931fSBen Gras.It Dv lelong
168ef01931fSBen GrasA four-byte value in little-endian byte order.
169ef01931fSBen Gras.It Dv lequad
170ef01931fSBen GrasAn eight-byte value in little-endian byte order.
171ef01931fSBen Gras.It Dv lefloat
172ef01931fSBen GrasA 32-bit single precision IEEE floating point number in little-endian byte order.
173ef01931fSBen Gras.It Dv ledouble
174ef01931fSBen GrasA 64-bit double precision IEEE floating point number in little-endian byte order.
175ef01931fSBen Gras.It Dv ledate
176ef01931fSBen GrasA four-byte value in little-endian byte order,
177ef01931fSBen Grasinterpreted as a UNIX date.
178ef01931fSBen Gras.It Dv leqdate
179ef01931fSBen GrasAn eight-byte value in little-endian byte order,
180ef01931fSBen Grasinterpreted as a UNIX date.
181ef01931fSBen Gras.It Dv leldate
182ef01931fSBen GrasA four-byte value in little-endian byte order,
183ef01931fSBen Grasinterpreted as a UNIX-style date, but interpreted as local time rather
184ef01931fSBen Grasthan UTC.
185ef01931fSBen Gras.It Dv leqldate
186ef01931fSBen GrasAn eight-byte value in little-endian byte order,
187ef01931fSBen Grasinterpreted as a UNIX-style date, but interpreted as local time rather
188ef01931fSBen Grasthan UTC.
18984d9c625SLionel Sambuc.It Dv leqwdate
19084d9c625SLionel SambucAn eight-byte value in little-endian byte order,
19184d9c625SLionel Sambucinterpreted as a Windows-style date.
192ef01931fSBen Gras.It Dv lestring16
193ef01931fSBen GrasA two-byte unicode (UCS16) string in little-endian byte order.
194ef01931fSBen Gras.It Dv melong
195ef01931fSBen GrasA four-byte value in middle-endian (PDP-11) byte order.
196ef01931fSBen Gras.It Dv medate
197ef01931fSBen GrasA four-byte value in middle-endian (PDP-11) byte order,
198ef01931fSBen Grasinterpreted as a UNIX date.
199ef01931fSBen Gras.It Dv meldate
200ef01931fSBen GrasA four-byte value in middle-endian (PDP-11) byte order,
201ef01931fSBen Grasinterpreted as a UNIX-style date, but interpreted as local time rather
202ef01931fSBen Grasthan UTC.
203ef01931fSBen Gras.It Dv indirect
204ef01931fSBen GrasStarting at the given offset, consult the magic database again.
205*0a6a1f1dSLionel SambucThe offset of th
206*0a6a1f1dSLionel Sambuc.Dv indirect
207*0a6a1f1dSLionel Sambucmagic is by default absolute in the file, but one can specify
208*0a6a1f1dSLionel Sambuc.Dv /r
209*0a6a1f1dSLionel Sambucto indicate that the offset is relative from the beginning of the entry.
21084d9c625SLionel Sambuc.It Dv name
21184d9c625SLionel SambucDefine a
21284d9c625SLionel Sambuc.Dq named
21384d9c625SLionel Sambucmagic instance that can be called from another
21484d9c625SLionel Sambuc.Dv use
21584d9c625SLionel Sambucmagic entry, like a subroutine call.
21684d9c625SLionel SambucNamed instance direct magic offsets are relative to the offset of the
21784d9c625SLionel Sambucprevious matched entry, but indirect offsets are relative to the beginning
21884d9c625SLionel Sambucof the file as usual.
21984d9c625SLionel SambucNamed magic entries always match.
22084d9c625SLionel Sambuc.It Dv use
22184d9c625SLionel SambucRecursively call the named magic starting from the current offset.
22284d9c625SLionel SambucIf the name of the referenced begins with a
22384d9c625SLionel Sambuc.Dv ^
22484d9c625SLionel Sambucthen the endianness of the magic is switched; if the magic mentioned
22584d9c625SLionel Sambuc.Dv leshort
22684d9c625SLionel Sambucfor example,
22784d9c625SLionel Sambucit is treated as
22884d9c625SLionel Sambuc.Dv beshort
22984d9c625SLionel Sambucand vice versa.
23084d9c625SLionel SambucThis is useful to avoid duplicating the rules for different endianness.
231ef01931fSBen Gras.It Dv regex
232ef01931fSBen GrasA regular expression match in extended POSIX regular expression syntax
233ef01931fSBen Gras(like egrep).
234ef01931fSBen GrasRegular expressions can take exponential time to process, and their
235ef01931fSBen Grasperformance is hard to predict, so their use is discouraged.
236ef01931fSBen GrasWhen used in production environments, their performance
237ef01931fSBen Grasshould be carefully checked.
238*0a6a1f1dSLionel SambucThe size of the string to search should also be limited by specifying
239*0a6a1f1dSLionel Sambuc.Dv /<length> ,
240*0a6a1f1dSLionel Sambucto avoid performance issues scanning long files.
241*0a6a1f1dSLionel SambucThe type specification can also be optionally followed by
242*0a6a1f1dSLionel Sambuc.Dv /[c][s][l] .
243ef01931fSBen GrasThe
244ef01931fSBen Gras.Dq c
245ef01931fSBen Grasflag makes the match case insensitive, while the
246ef01931fSBen Gras.Dq s
247ef01931fSBen Grasflag update the offset to the start offset of the match, rather than the end.
248*0a6a1f1dSLionel SambucThe
249*0a6a1f1dSLionel Sambuc.Dq l
250*0a6a1f1dSLionel Sambucmodifier, changes the limit of length to mean number of lines instead of a
251*0a6a1f1dSLionel Sambucbyte count.
252*0a6a1f1dSLionel SambucLines are delimited by the platforms native line delimiter.
253*0a6a1f1dSLionel SambucWhen a line count is specified, an implicit byte count also computed assuming
254*0a6a1f1dSLionel Sambuceach line is 80 characters long.
255*0a6a1f1dSLionel SambucIf neither a byte or line count is specified, the search is limited automatically
256*0a6a1f1dSLionel Sambucto 8KiB.
257ef01931fSBen Gras.Dv ^
258ef01931fSBen Grasand
259ef01931fSBen Gras.Dv $
260ef01931fSBen Grasmatch the beginning and end of individual lines, respectively,
261ef01931fSBen Grasnot beginning and end of file.
262ef01931fSBen Gras.It Dv search
263ef01931fSBen GrasA literal string search starting at the given offset.
264ef01931fSBen GrasThe same modifier flags can be used as for string patterns.
265*0a6a1f1dSLionel SambucThe search expression must contain the range in the form
266*0a6a1f1dSLionel Sambuc.Dv /number,
267*0a6a1f1dSLionel Sambucthat is the number of positions at which the match will be
268ef01931fSBen Grasattempted, starting from the start offset.
269ef01931fSBen GrasThis is suitable for
270ef01931fSBen Grassearching larger binary expressions with variable offsets, using
271ef01931fSBen Gras.Dv \e
272ef01931fSBen Grasescapes for special characters.
273*0a6a1f1dSLionel SambucThe order of modifier and number is not relevant.
274ef01931fSBen Gras.It Dv default
275ef01931fSBen GrasThis is intended to be used with the test
276ef01931fSBen Gras.Em x
277*0a6a1f1dSLionel Sambuc(which is always true) and it has no type.
278*0a6a1f1dSLionel SambucIt matches when no other test at that continuation level has matched before.
279*0a6a1f1dSLionel SambucClearing that matched tests for a continuation level, can be done using the
280*0a6a1f1dSLionel Sambuc.Dv clear
281*0a6a1f1dSLionel Sambuctest.
282*0a6a1f1dSLionel Sambuc.It Dv clear
283*0a6a1f1dSLionel SambucThis test is always true and clears the match flag for that continuation level.
284*0a6a1f1dSLionel SambucIt is intended to be used with the
285*0a6a1f1dSLionel Sambuc.Dv default
286*0a6a1f1dSLionel Sambuctest.
287ef01931fSBen Gras.El
288ef01931fSBen Gras.Pp
28984d9c625SLionel SambucFor compatibility with the Single
29084d9c625SLionel Sambuc.Ux
29184d9c625SLionel SambucStandard, the type specifiers
29284d9c625SLionel Sambuc.Dv dC
29384d9c625SLionel Sambucand
29484d9c625SLionel Sambuc.Dv d1
29584d9c625SLionel Sambucare equivalent to
29684d9c625SLionel Sambuc.Dv byte ,
29784d9c625SLionel Sambucthe type specifiers
29884d9c625SLionel Sambuc.Dv uC
29984d9c625SLionel Sambucand
30084d9c625SLionel Sambuc.Dv u1
30184d9c625SLionel Sambucare equivalent to
30284d9c625SLionel Sambuc.Dv ubyte ,
30384d9c625SLionel Sambucthe type specifiers
30484d9c625SLionel Sambuc.Dv dS
30584d9c625SLionel Sambucand
30684d9c625SLionel Sambuc.Dv d2
30784d9c625SLionel Sambucare equivalent to
30884d9c625SLionel Sambuc.Dv short ,
30984d9c625SLionel Sambucthe type specifiers
31084d9c625SLionel Sambuc.Dv uS
31184d9c625SLionel Sambucand
31284d9c625SLionel Sambuc.Dv u2
31384d9c625SLionel Sambucare equivalent to
31484d9c625SLionel Sambuc.Dv ushort ,
31584d9c625SLionel Sambucthe type specifiers
31684d9c625SLionel Sambuc.Dv dI ,
31784d9c625SLionel Sambuc.Dv dL ,
31884d9c625SLionel Sambucand
31984d9c625SLionel Sambuc.Dv d4
32084d9c625SLionel Sambucare equivalent to
32184d9c625SLionel Sambuc.Dv long ,
32284d9c625SLionel Sambucthe type specifiers
32384d9c625SLionel Sambuc.Dv uI ,
32484d9c625SLionel Sambuc.Dv uL ,
32584d9c625SLionel Sambucand
32684d9c625SLionel Sambuc.Dv u4
32784d9c625SLionel Sambucare equivalent to
32884d9c625SLionel Sambuc.Dv ulong ,
32984d9c625SLionel Sambucthe type specifier
33084d9c625SLionel Sambuc.Dv d8
33184d9c625SLionel Sambucis equivalent to
33284d9c625SLionel Sambuc.Dv quad ,
33384d9c625SLionel Sambucthe type specifier
33484d9c625SLionel Sambuc.Dv u8
33584d9c625SLionel Sambucis equivalent to
33684d9c625SLionel Sambuc.Dv uquad ,
33784d9c625SLionel Sambucand the type specifier
33884d9c625SLionel Sambuc.Dv s
33984d9c625SLionel Sambucis equivalent to
34084d9c625SLionel Sambuc.Dv string .
34184d9c625SLionel SambucIn addition, the type specifier
34284d9c625SLionel Sambuc.Dv dQ
34384d9c625SLionel Sambucis equivalent to
34484d9c625SLionel Sambuc.Dv quad
34584d9c625SLionel Sambucand the type specifier
34684d9c625SLionel Sambuc.Dv uQ
34784d9c625SLionel Sambucis equivalent to
34884d9c625SLionel Sambuc.Dv uquad .
34984d9c625SLionel Sambuc.Pp
350ef01931fSBen GrasEach top-level magic pattern (see below for an explanation of levels)
351ef01931fSBen Grasis classified as text or binary according to the types used.
352ef01931fSBen GrasTypes
353ef01931fSBen Gras.Dq regex
354ef01931fSBen Grasand
355ef01931fSBen Gras.Dq search
356ef01931fSBen Grasare classified as text tests, unless non-printable characters are used
357ef01931fSBen Grasin the pattern.
358ef01931fSBen GrasAll other tests are classified as binary.
359ef01931fSBen GrasA top-level
360ef01931fSBen Graspattern is considered to be a test text when all its patterns are text
361ef01931fSBen Graspatterns; otherwise, it is considered to be a binary pattern.
362ef01931fSBen GrasWhen
363ef01931fSBen Grasmatching a file, binary patterns are tried first; if no match is
364ef01931fSBen Grasfound, and the file looks like text, then its encoding is determined
365ef01931fSBen Grasand the text patterns are tried.
366ef01931fSBen Gras.Pp
367ef01931fSBen GrasThe numeric types may optionally be followed by
368ef01931fSBen Gras.Dv \*[Am]
369ef01931fSBen Grasand a numeric value,
370ef01931fSBen Grasto specify that the value is to be AND'ed with the
371ef01931fSBen Grasnumeric value before any comparisons are done.
372ef01931fSBen GrasPrepending a
373ef01931fSBen Gras.Dv u
374ef01931fSBen Grasto the type indicates that ordered comparisons should be unsigned.
375ef01931fSBen Gras.It Dv test
376ef01931fSBen GrasThe value to be compared with the value from the file.
377ef01931fSBen GrasIf the type is
378ef01931fSBen Grasnumeric, this value
379ef01931fSBen Grasis specified in C form; if it is a string, it is specified as a C string
380ef01931fSBen Graswith the usual escapes permitted (e.g. \en for new-line).
381ef01931fSBen Gras.Pp
382ef01931fSBen GrasNumeric values
383ef01931fSBen Grasmay be preceded by a character indicating the operation to be performed.
384ef01931fSBen GrasIt may be
385ef01931fSBen Gras.Dv = ,
386ef01931fSBen Grasto specify that the value from the file must equal the specified value,
387ef01931fSBen Gras.Dv \*[Lt] ,
388ef01931fSBen Grasto specify that the value from the file must be less than the specified
389ef01931fSBen Grasvalue,
390ef01931fSBen Gras.Dv \*[Gt] ,
391ef01931fSBen Grasto specify that the value from the file must be greater than the specified
392ef01931fSBen Grasvalue,
393ef01931fSBen Gras.Dv \*[Am] ,
394ef01931fSBen Grasto specify that the value from the file must have set all of the bits
395ef01931fSBen Grasthat are set in the specified value,
396ef01931fSBen Gras.Dv ^ ,
397ef01931fSBen Grasto specify that the value from the file must have clear any of the bits
398ef01931fSBen Grasthat are set in the specified value, or
399ef01931fSBen Gras.Dv ~ ,
400ef01931fSBen Grasthe value specified after is negated before tested.
401ef01931fSBen Gras.Dv x ,
402ef01931fSBen Grasto specify that any value will match.
403ef01931fSBen GrasIf the character is omitted, it is assumed to be
404ef01931fSBen Gras.Dv = .
405ef01931fSBen GrasOperators
406ef01931fSBen Gras.Dv \*[Am] ,
407ef01931fSBen Gras.Dv ^ ,
408ef01931fSBen Grasand
409ef01931fSBen Gras.Dv ~
410ef01931fSBen Grasdon't work with floats and doubles.
411ef01931fSBen GrasThe operator
412ef01931fSBen Gras.Dv !\&
413ef01931fSBen Grasspecifies that the line matches if the test does
414ef01931fSBen Gras.Em not
415ef01931fSBen Grassucceed.
416ef01931fSBen Gras.Pp
417ef01931fSBen GrasNumeric values are specified in C form; e.g.
418ef01931fSBen Gras.Dv 13
419ef01931fSBen Grasis decimal,
420ef01931fSBen Gras.Dv 013
421ef01931fSBen Grasis octal, and
422ef01931fSBen Gras.Dv 0x13
423ef01931fSBen Grasis hexadecimal.
424ef01931fSBen Gras.Pp
425*0a6a1f1dSLionel SambucNumeric operations are not performed on date types, instead the numeric
426*0a6a1f1dSLionel Sambucvalue is interpreted as an offset.
427*0a6a1f1dSLionel Sambuc.Pp
428ef01931fSBen GrasFor string values, the string from the
429ef01931fSBen Grasfile must match the specified string.
430ef01931fSBen GrasThe operators
431ef01931fSBen Gras.Dv = ,
432ef01931fSBen Gras.Dv \*[Lt]
433ef01931fSBen Grasand
434ef01931fSBen Gras.Dv \*[Gt]
435ef01931fSBen Gras(but not
436ef01931fSBen Gras.Dv \*[Am] )
437ef01931fSBen Grascan be applied to strings.
438ef01931fSBen GrasThe length used for matching is that of the string argument
439ef01931fSBen Grasin the magic file.
440ef01931fSBen GrasThis means that a line can match any non-empty string (usually used to
441ef01931fSBen Grasthen print the string), with
442ef01931fSBen Gras.Em \*[Gt]\e0
443ef01931fSBen Gras(because all non-empty strings are greater than the empty string).
444ef01931fSBen Gras.Pp
445*0a6a1f1dSLionel SambucDates are treated as numerical values in the respective internal
446*0a6a1f1dSLionel Sambucrepresentation.
447*0a6a1f1dSLionel Sambuc.Pp
448ef01931fSBen GrasThe special test
449ef01931fSBen Gras.Em x
450ef01931fSBen Grasalways evaluates to true.
451835f6802SDirk Vogt.It Dv message
452ef01931fSBen GrasThe message to be printed if the comparison succeeds.
453ef01931fSBen GrasIf the string contains a
454ef01931fSBen Gras.Xr printf 3
455ef01931fSBen Grasformat specification, the value from the file (with any specified masking
456ef01931fSBen Grasperformed) is printed using the message as the format string.
457ef01931fSBen GrasIf the string begins with
458ef01931fSBen Gras.Dq \eb ,
459ef01931fSBen Grasthe message printed is the remainder of the string with no whitespace
460ef01931fSBen Grasadded before it: multiple matches are normally separated by a single
461ef01931fSBen Grasspace.
462ef01931fSBen Gras.El
463ef01931fSBen Gras.Pp
464ef01931fSBen GrasAn APPLE 4+4 character APPLE creator and type can be specified as:
465ef01931fSBen Gras.Bd -literal -offset indent
466ef01931fSBen Gras!:apple	CREATYPE
467ef01931fSBen Gras.Ed
468ef01931fSBen Gras.Pp
469ef01931fSBen GrasA MIME type is given on a separate line, which must be the next
470ef01931fSBen Grasnon-blank or comment line after the magic line that identifies the
471ef01931fSBen Grasfile type, and has the following format:
472ef01931fSBen Gras.Bd -literal -offset indent
473ef01931fSBen Gras!:mime	MIMETYPE
474ef01931fSBen Gras.Ed
475ef01931fSBen Gras.Pp
476ef01931fSBen Grasi.e. the literal string
477ef01931fSBen Gras.Dq !:mime
478ef01931fSBen Grasfollowed by the MIME type.
479ef01931fSBen Gras.Pp
480ef01931fSBen GrasAn optional strength can be supplied on a separate line which refers to
481ef01931fSBen Grasthe current magic description using the following format:
482ef01931fSBen Gras.Bd -literal -offset indent
483ef01931fSBen Gras!:strength OP VALUE
484ef01931fSBen Gras.Ed
485ef01931fSBen Gras.Pp
486ef01931fSBen GrasThe operand
487ef01931fSBen Gras.Dv OP
488ef01931fSBen Grascan be:
489ef01931fSBen Gras.Dv + ,
490ef01931fSBen Gras.Dv - ,
491ef01931fSBen Gras.Dv * ,
492ef01931fSBen Grasor
493ef01931fSBen Gras.Dv /
494ef01931fSBen Grasand
495ef01931fSBen Gras.Dv VALUE
496ef01931fSBen Grasis a constant between 0 and 255.
497ef01931fSBen GrasThis constant is applied using the specified operand
498ef01931fSBen Grasto the currently computed default magic strength.
499ef01931fSBen Gras.Pp
500ef01931fSBen GrasSome file formats contain additional information which is to be printed
501ef01931fSBen Grasalong with the file type or need additional tests to determine the true
502ef01931fSBen Grasfile type.
503ef01931fSBen GrasThese additional tests are introduced by one or more
504ef01931fSBen Gras.Em \*[Gt]
505ef01931fSBen Grascharacters preceding the offset.
506ef01931fSBen GrasThe number of
507ef01931fSBen Gras.Em \*[Gt]
508ef01931fSBen Grason the line indicates the level of the test; a line with no
509ef01931fSBen Gras.Em \*[Gt]
510ef01931fSBen Grasat the beginning is considered to be at level 0.
511ef01931fSBen GrasTests are arranged in a tree-like hierarchy:
512835f6802SDirk Vogtif the test on a line at level
513ef01931fSBen Gras.Em n
514ef01931fSBen Grassucceeds, all following tests at level
515ef01931fSBen Gras.Em n+1
516835f6802SDirk Vogtare performed, and the messages printed if the tests succeed, until a line
517ef01931fSBen Graswith level
518ef01931fSBen Gras.Em n
519ef01931fSBen Gras(or less) appears.
520ef01931fSBen GrasFor more complex files, one can use empty messages to get just the
521ef01931fSBen Gras"if/then" effect, in the following way:
522ef01931fSBen Gras.Bd -literal -offset indent
523ef01931fSBen Gras0      string   MZ
524ef01931fSBen Gras\*[Gt]0x18  leshort  \*[Lt]0x40   MS-DOS executable
525ef01931fSBen Gras\*[Gt]0x18  leshort  \*[Gt]0x3f   extended PC executable (e.g., MS Windows)
526ef01931fSBen Gras.Ed
527ef01931fSBen Gras.Pp
528ef01931fSBen GrasOffsets do not need to be constant, but can also be read from the file
529ef01931fSBen Grasbeing examined.
530ef01931fSBen GrasIf the first character following the last
531ef01931fSBen Gras.Em \*[Gt]
532ef01931fSBen Grasis a
533835f6802SDirk Vogt.Em \&(
534ef01931fSBen Grasthen the string after the parenthesis is interpreted as an indirect offset.
535ef01931fSBen GrasThat means that the number after the parenthesis is used as an offset in
536ef01931fSBen Grasthe file.
537ef01931fSBen GrasThe value at that offset is read, and is used again as an offset
538ef01931fSBen Grasin the file.
539ef01931fSBen GrasIndirect offsets are of the form:
540ef01931fSBen Gras.Em (( x [.[bislBISL]][+\-][ y ]) .
541ef01931fSBen GrasThe value of
542ef01931fSBen Gras.Em x
543ef01931fSBen Grasis used as an offset in the file.
544ef01931fSBen GrasA byte, id3 length, short or long is read at that offset depending on the
545ef01931fSBen Gras.Em [bislBISLm]
546ef01931fSBen Grastype specifier.
547ef01931fSBen GrasThe capitalized types interpret the number as a big endian
548ef01931fSBen Grasvalue, whereas the small letter versions interpret the number as a little
549ef01931fSBen Grasendian value;
550ef01931fSBen Grasthe
551ef01931fSBen Gras.Em m
552ef01931fSBen Grastype interprets the number as a middle endian (PDP-11) value.
553ef01931fSBen GrasTo that number the value of
554ef01931fSBen Gras.Em y
555ef01931fSBen Grasis added and the result is used as an offset in the file.
556ef01931fSBen GrasThe default type if one is not specified is long.
557ef01931fSBen Gras.Pp
558ef01931fSBen GrasThat way variable length structures can be examined:
559ef01931fSBen Gras.Bd -literal -offset indent
560ef01931fSBen Gras# MS Windows executables are also valid MS-DOS executables
561ef01931fSBen Gras0           string  MZ
562ef01931fSBen Gras\*[Gt]0x18       leshort \*[Lt]0x40   MZ executable (MS-DOS)
563ef01931fSBen Gras# skip the whole block below if it is not an extended executable
564ef01931fSBen Gras\*[Gt]0x18       leshort \*[Gt]0x3f
565ef01931fSBen Gras\*[Gt]\*[Gt](0x3c.l)  string  PE\e0\e0  PE executable (MS-Windows)
566ef01931fSBen Gras\*[Gt]\*[Gt](0x3c.l)  string  LX\e0\e0  LX executable (OS/2)
567ef01931fSBen Gras.Ed
568ef01931fSBen Gras.Pp
569ef01931fSBen GrasThis strategy of examining has a drawback: You must make sure that
570ef01931fSBen Grasyou eventually print something, or users may get empty output (like, when
571ef01931fSBen Grasthere is neither PE\e0\e0 nor LE\e0\e0 in the above example)
572ef01931fSBen Gras.Pp
573ef01931fSBen GrasIf this indirect offset cannot be used directly, simple calculations are
574ef01931fSBen Graspossible: appending
575ef01931fSBen Gras.Em [+-*/%\*[Am]|^]number
576ef01931fSBen Grasinside parentheses allows one to modify
577ef01931fSBen Grasthe value read from the file before it is used as an offset:
578ef01931fSBen Gras.Bd -literal -offset indent
579ef01931fSBen Gras# MS Windows executables are also valid MS-DOS executables
580ef01931fSBen Gras0           string  MZ
581ef01931fSBen Gras# sometimes, the value at 0x18 is less that 0x40 but there's still an
582ef01931fSBen Gras# extended executable, simply appended to the file
583ef01931fSBen Gras\*[Gt]0x18       leshort \*[Lt]0x40
584ef01931fSBen Gras\*[Gt]\*[Gt](4.s*512) leshort 0x014c  COFF executable (MS-DOS, DJGPP)
585ef01931fSBen Gras\*[Gt]\*[Gt](4.s*512) leshort !0x014c MZ executable (MS-DOS)
586ef01931fSBen Gras.Ed
587ef01931fSBen Gras.Pp
588ef01931fSBen GrasSometimes you do not know the exact offset as this depends on the length or
589ef01931fSBen Grasposition (when indirection was used before) of preceding fields.
590ef01931fSBen GrasYou can specify an offset relative to the end of the last up-level
591ef01931fSBen Grasfield using
592ef01931fSBen Gras.Sq \*[Am]
593ef01931fSBen Grasas a prefix to the offset:
594ef01931fSBen Gras.Bd -literal -offset indent
595ef01931fSBen Gras0           string  MZ
596ef01931fSBen Gras\*[Gt]0x18       leshort \*[Gt]0x3f
597ef01931fSBen Gras\*[Gt]\*[Gt](0x3c.l)  string  PE\e0\e0    PE executable (MS-Windows)
598ef01931fSBen Gras# immediately following the PE signature is the CPU type
599ef01931fSBen Gras\*[Gt]\*[Gt]\*[Gt]\*[Am]0       leshort 0x14c     for Intel 80386
600ef01931fSBen Gras\*[Gt]\*[Gt]\*[Gt]\*[Am]0       leshort 0x184     for DEC Alpha
601ef01931fSBen Gras.Ed
602ef01931fSBen Gras.Pp
603ef01931fSBen GrasIndirect and relative offsets can be combined:
604ef01931fSBen Gras.Bd -literal -offset indent
605ef01931fSBen Gras0             string  MZ
606ef01931fSBen Gras\*[Gt]0x18         leshort \*[Lt]0x40
607ef01931fSBen Gras\*[Gt]\*[Gt](4.s*512)   leshort !0x014c MZ executable (MS-DOS)
608ef01931fSBen Gras# if it's not COFF, go back 512 bytes and add the offset taken
609ef01931fSBen Gras# from byte 2/3, which is yet another way of finding the start
610ef01931fSBen Gras# of the extended executable
611ef01931fSBen Gras\*[Gt]\*[Gt]\*[Gt]\*[Am](2.s-514) string  LE      LE executable (MS Windows VxD driver)
612ef01931fSBen Gras.Ed
613ef01931fSBen Gras.Pp
614ef01931fSBen GrasOr the other way around:
615ef01931fSBen Gras.Bd -literal -offset indent
616ef01931fSBen Gras0                 string  MZ
617ef01931fSBen Gras\*[Gt]0x18             leshort \*[Gt]0x3f
618ef01931fSBen Gras\*[Gt]\*[Gt](0x3c.l)        string  LE\e0\e0  LE executable (MS-Windows)
619ef01931fSBen Gras# at offset 0x80 (-4, since relative offsets start at the end
620ef01931fSBen Gras# of the up-level match) inside the LE header, we find the absolute
621ef01931fSBen Gras# offset to the code area, where we look for a specific signature
622ef01931fSBen Gras\*[Gt]\*[Gt]\*[Gt](\*[Am]0x7c.l+0x26) string  UPX     \eb, UPX compressed
623ef01931fSBen Gras.Ed
624ef01931fSBen Gras.Pp
625ef01931fSBen GrasOr even both!
626ef01931fSBen Gras.Bd -literal -offset indent
627ef01931fSBen Gras0                string  MZ
628ef01931fSBen Gras\*[Gt]0x18            leshort \*[Gt]0x3f
629ef01931fSBen Gras\*[Gt]\*[Gt](0x3c.l)       string  LE\e0\e0 LE executable (MS-Windows)
630ef01931fSBen Gras# at offset 0x58 inside the LE header, we find the relative offset
631ef01931fSBen Gras# to a data area where we look for a specific signature
632ef01931fSBen Gras\*[Gt]\*[Gt]\*[Gt]\*[Am](\*[Am]0x54.l-3)  string  UNACE  \eb, ACE self-extracting archive
633ef01931fSBen Gras.Ed
634ef01931fSBen Gras.Pp
635*0a6a1f1dSLionel SambucIf you have to deal with offset/length pairs in your file, even the
636ef01931fSBen Grassecond value in a parenthesized expression can be taken from the file itself,
637ef01931fSBen Grasusing another set of parentheses.
638ef01931fSBen GrasNote that this additional indirect offset is always relative to the
639ef01931fSBen Grasstart of the main indirect offset.
640ef01931fSBen Gras.Bd -literal -offset indent
641ef01931fSBen Gras0                 string       MZ
642ef01931fSBen Gras\*[Gt]0x18             leshort      \*[Gt]0x3f
643ef01931fSBen Gras\*[Gt]\*[Gt](0x3c.l)        string       PE\e0\e0 PE executable (MS-Windows)
644ef01931fSBen Gras# search for the PE section called ".idata"...
645ef01931fSBen Gras\*[Gt]\*[Gt]\*[Gt]\*[Am]0xf4          search/0x140 .idata
646ef01931fSBen Gras# ...and go to the end of it, calculated from start+length;
647ef01931fSBen Gras# these are located 14 and 10 bytes after the section name
648ef01931fSBen Gras\*[Gt]\*[Gt]\*[Gt]\*[Gt](\*[Am]0xe.l+(-4)) string       PK\e3\e4 \eb, ZIP self-extracting archive
649ef01931fSBen Gras.Ed
650*0a6a1f1dSLionel Sambuc.Pp
651*0a6a1f1dSLionel SambucIf you have a list of known avalues at a particular continuation level,
652*0a6a1f1dSLionel Sambucand you want to provide a switch-like default case:
653*0a6a1f1dSLionel Sambuc.Bd -literal -offset indent
654*0a6a1f1dSLionel Sambuc# clear that continuation level match
655*0a6a1f1dSLionel Sambuc\*[Gt]18	clear
656*0a6a1f1dSLionel Sambuc\*[Gt]18	lelong	1	one
657*0a6a1f1dSLionel Sambuc\*[Gt]18	lelong	2	two
658*0a6a1f1dSLionel Sambuc\*[Gt]18	default	x
659*0a6a1f1dSLionel Sambuc# print default match
660*0a6a1f1dSLionel Sambuc\*[Gt]\*[Gt]18	lelong	x	unmatched 0x%x
661*0a6a1f1dSLionel Sambuc.Ed
662ef01931fSBen Gras.Sh SEE ALSO
663ef01931fSBen Gras.Xr file 1
664ef01931fSBen Gras\- the command that reads this file.
665ef01931fSBen Gras.Sh BUGS
666ef01931fSBen GrasThe formats
667ef01931fSBen Gras.Dv long ,
668ef01931fSBen Gras.Dv belong ,
669ef01931fSBen Gras.Dv lelong ,
670ef01931fSBen Gras.Dv melong ,
671ef01931fSBen Gras.Dv short ,
672ef01931fSBen Gras.Dv beshort ,
673ef01931fSBen Grasand
67484d9c625SLionel Sambuc.Dv leshort
67584d9c625SLionel Sambucdo not depend on the length of the C data types
67684d9c625SLionel Sambuc.Dv short
67784d9c625SLionel Sambucand
67884d9c625SLionel Sambuc.Dv long
67984d9c625SLionel Sambucon the platform, even though the Single
68084d9c625SLionel Sambuc.Ux
68184d9c625SLionel SambucSpecification implies that they do.  However, as OS X Mountain Lion has
68284d9c625SLionel Sambucpassed the Single
68384d9c625SLionel Sambuc.Ux
68484d9c625SLionel SambucSpecification validation suite, and supplies a version of
68584d9c625SLionel Sambuc.Xr file 1
68684d9c625SLionel Sambucin which they do not depend on the sizes of the C data types and that is
68784d9c625SLionel Sambucbuilt for a 64-bit environment in which
68884d9c625SLionel Sambuc.Dv long
68984d9c625SLionel Sambucis 8 bytes rather than 4 bytes, presumably the validation suite does not
69084d9c625SLionel Sambuctest whether, for example
69184d9c625SLionel Sambuc.Dv long
69284d9c625SLionel Sambucrefers to an item with the same size as the C data type
69384d9c625SLionel Sambuc.Dv long .
69484d9c625SLionel SambucThere should probably be
69584d9c625SLionel Sambuc.Dv type
69684d9c625SLionel Sambucnames
69784d9c625SLionel Sambuc.Dv int8 ,
69884d9c625SLionel Sambuc.Dv uint8 ,
69984d9c625SLionel Sambuc.Dv int16 ,
70084d9c625SLionel Sambuc.Dv uint16 ,
70184d9c625SLionel Sambuc.Dv int32 ,
70284d9c625SLionel Sambuc.Dv uint32 ,
70384d9c625SLionel Sambuc.Dv int64 ,
70484d9c625SLionel Sambucand
70584d9c625SLionel Sambuc.Dv uint64 ,
70684d9c625SLionel Sambucand specified-byte-order variants of them,
70784d9c625SLionel Sambucto make it clearer that those types have specified widths.
708ef01931fSBen Gras.\"
709ef01931fSBen Gras.\" From: guy@sun.uucp (Guy Harris)
710ef01931fSBen Gras.\" Newsgroups: net.bugs.usg
711ef01931fSBen Gras.\" Subject: /etc/magic's format isn't well documented
712ef01931fSBen Gras.\" Message-ID: <2752@sun.uucp>
713ef01931fSBen Gras.\" Date: 3 Sep 85 08:19:07 GMT
714ef01931fSBen Gras.\" Organization: Sun Microsystems, Inc.
715ef01931fSBen Gras.\" Lines: 136
716ef01931fSBen Gras.\"
717ef01931fSBen Gras.\" Here's a manual page for the format accepted by the "file" made by adding
718ef01931fSBen Gras.\" the changes I posted to the S5R2 version.
719ef01931fSBen Gras.\"
720ef01931fSBen Gras.\" Modified for Ian Darwin's version of the file command.
721