xref: /openbsd-src/usr.bin/file/magic.5 (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1.\" $OpenBSD: magic.5,v 1.6 2000/04/12 21:47:48 aaron Exp $
2.\"
3.\" @(#)$FreeBSD: src/usr.bin/file/magic.5,v 1.11 2000/03/01 12:19:39 sheldonh Exp $
4.\"
5.\" install as magic.4 on USG, magic.5 on V7 or Berkeley systems.
6.\"
7.Dd September 3, 1994
8.Dt MAGIC 5
9.Os
10.Sh NAME
11.Nm magic
12.Nd file command's magic number file
13.Sh DESCRIPTION
14This manual page documents the format of the magic file as
15used by the
16.Xr file 1
17command, version 3.22.
18The
19.Nm file
20command identifies the type of a file using,
21among other tests,
22a test for whether the file begins with a certain
23.Dq magic number .
24.Pp
25The file
26.Pa /etc/magic
27specifies what magic numbers are to be tested for,
28what message to print if a particular magic number is found,
29and additional information to extract from the file.
30.Pp
31Each line of the file specifies a test to be performed.
32A test compares the data starting at a particular offset
33in the file with a 1-byte, 2-byte, or 4-byte numeric value or
34a string.
35If the test succeeds, a message is printed.
36The line consists of the following fields:
37.Bl -tag -width indent
38.It Sy offset
39A number specifying the offset, in bytes, into the file of the data
40which is to be tested.
41.It Sy type
42The type of the data to be tested.
43The possible values are:
44.Bl -tag -width beshort
45.It Sy byte
46A one-byte value.
47.It Sy short
48A two-byte value (on most systems) in this machine's native byte order.
49.It Sy long
50A four-byte value (on most systems) in this machine's native byte order.
51.It Sy string
52A string of bytes.
53.It Sy date
54A four-byte value interpreted as a
55.Ux
56date.
57.It Sy beshort
58A two-byte value (on most systems) in big-endian byte order.
59.It Sy belong
60A four-byte value (on most systems) in big-endian byte order.
61.It Sy bedate
62A four-byte value (on most systems) in big-endian byte order,
63interpreted as a
64.Ux
65date.
66.It Sy leshort
67A two-byte value (on most systems) in little-endian byte order.
68.It Sy lelong
69A four-byte value (on most systems) in little-endian byte order.
70.It Sy ledate
71A four-byte value (on most systems) in little-endian byte order,
72interpreted as a
73.Ux
74date.
75.El
76.El
77.Pp
78The numeric types may optionally be followed by
79.Ql &
80and a numeric value,
81to specify that the value is to be AND'ed with the
82numeric value before any comparisons are done.
83Prepending a
84.Sq u
85to the type indicates that ordered comparisons should be unsigned.
86.Bl -tag -width indent
87.It Sy test
88The value to be compared with the value from the file.
89If the type is
90numeric, this value
91is specified in C form; if it is a string, it is specified as a C string
92with the usual escapes permitted (e.g.,
93.Ql \en
94for newline).
95.It Sy ""
96Numeric values
97may be preceded by a character indicating the operation to be performed.
98It may be
99.Ql =
100to specify that the value from the file must equal the specified value,
101.Ql <
102to specify that the value from the file must be less than the specified
103value,
104.Ql >
105to specify that the value from the file must be greater than the specified
106value,
107.Ql &
108to specify that the value from the file must have set all of the bits
109that are set in the specified value,
110.Ql ^
111to specify that the value from the file must have clear any of the bits
112that are set in the specified value, or
113.Sq x
114to specify that any value will match.
115If the character is omitted,
116it is assumed to be
117.Ql = .
118.It Sy ""
119Numeric values are specified in C form; e.g.,
120.Dq 13
121is decimal,
122.Dq 013
123is octal, and
124.Dq 0x13
125is hexadecimal.
126.It Sy ""
127For string values, the byte string from the
128file must match the specified byte string.
129The operators
130.Ql = ,
131.Ql < ,
132and
133.Ql >
134(but not
135.Ql & )
136can be applied to strings.
137The length used for matching is that of the string argument
138in the magic file.
139This means that a line can match any string, and
140then presumably print that string, by doing
141.Ql >\e0
142(because all strings are greater than the null string).
143.It Sy message
144The message to be printed if the comparison succeeds.
145If the string
146contains a
147.Xr printf 3
148format specification, the value from the file (with any specified masking
149performed) is printed using the message as the format string.
150.El
151.Pp
152Some file formats contain additional information which is to be printed
153along with the file type.
154A line which begins with the character
155.Ql >
156indicates additional tests and messages to be printed.
157The number of
158.Ql >
159on the line indicates the level of the test; a line with no
160.Ql >
161at the beginning is considered to be at level 0.
162.Pp
163Each line at level
164.Em n+1
165is under the control of the line at level
166.Em n
167most closely preceding it in the magic file.
168If the test on a line at level
169.Em n
170succeeds, the tests specified in all the subsequent lines at level
171.Em n+1
172are performed, and the messages printed if the tests succeed.
173The next
174line at level
175.Em n
176terminates this.
177.Pp
178If the first character following the last
179.Ql >
180is a
181.Ql (
182then the string after the parenthesis is interpreted as an indirect offset.
183That means that the number after the parenthesis is used as an offset in
184the file.
185The value at that offset is read, and is used again as an offset
186in the file.
187.Pp
188Indirect offsets are of the form:
189.Dq (x[.[bsl]][+-][y]) .
190The value of
191.Sq x
192is used as an offset in the file.
193A byte, short or long is read at that offset
194depending on the
195.Dq [bsl]
196type specifier.
197To that number the value of
198.Sq y
199is added and the result is used as an offset in the file.
200The default type
201if one is not specified is long.
202.Pp
203Sometimes you do not know the exact offset as this depends on the length of
204preceding fields.
205You can specify an offset relative to the end of the
206last uplevel field (of course this may only be done for sublevel tests, i.e.,
207test beginning with
208.Ql > ) .
209Such a relative offset is specified using
210.Ql &
211as a prefix to the offset.
212.Sh BUGS
213The formats
214.Li long ,
215.Li belong ,
216.Li lelong ,
217.Li short ,
218.Li beshort ,
219.Li leshort ,
220.Li date ,
221.Li bedate ,
222and
223.Li ledate
224are system-dependent; perhaps they should be specified as a number
225of bytes (2B, 4B, etc),
226since the files being recognized typically come from
227a system on which the lengths are invariant.
228.Pp
229There is (currently) no support for specified-endian data to be used in
230indirect offsets.
231.Sh FILES
232.Bl -tag -width /etc/magic
233.It Pa /etc/magic
234.El
235.Sh SEE ALSO
236.Xr file 1
237.\"
238.\" From: guy@sun.uucp (Guy Harris)
239.\" Newsgroups: net.bugs.usg
240.\" Subject: /etc/magic's format isn't well documented
241.\" Message-ID: <2752@sun.uucp>
242.\" Date: 3 Sep 85 08:19:07 GMT
243.\" Organization: Sun Microsystems, Inc.
244.\" Lines: 136
245.\"
246.\" Here's a manual page for the format accepted by the "file" made by adding
247.\" the changes I posted to the S5R2 version.
248.\"
249.\" Modified for Ian Darwin's version of the file command.
250