xref: /freebsd/contrib/file/doc/magic.man (revision dece5b05f577fc4c6b89af7ad48dfbdf7cc4945c)
1.\" $File: magic.man,v 1.118 2026/04/19 19:56:49 christos Exp $
2.Dd April 19, 2026
3.Dt MAGIC __FSECTION__
4.Os
5.\" install as magic.4 on USG, magic.5 on V7, Berkeley and Linux systems.
6.Sh NAME
7.Nm magic
8.Nd file command's magic pattern file
9.Sh DESCRIPTION
10This manual page documents the format of magic files as
11used by the
12.Xr file __CSECTION__
13command, version __VERSION__.
14The
15.Xr file __CSECTION__
16command identifies the type of a file using,
17among other tests,
18a test for whether the file contains certain
19.Dq "magic patterns" .
20The database of these
21.Dq "magic patterns"
22is usually located in a binary file in
23.Pa __MAGIC__.mgc
24or a directory of source text magic pattern fragment files in
25.Pa __MAGIC__ .
26The database specifies what patterns are to be tested for, what message or
27MIME type to print if a particular pattern is found,
28and additional information to extract from the file.
29.Pp
30The format of the source fragment files that are used to build this database
31is as follows:
32Each line of a fragment file specifies a test to be performed.
33A test compares the data starting at a particular offset
34in the file with a byte value, a string or a numeric value.
35If the test succeeds, a message is printed.
36The line consists of the following fields:
37.Bl -tag -width ".Dv message"
38.It Dv offset
39A number specifying the offset (in bytes) into the file of the data
40which is to be tested.
41This offset can be a negative number if it is:
42.Bl -bullet  -compact
43.It
44The first direct offset of the magic entry (at continuation level 0),
45in which case it is interpreted an offset from end end of the file
46going backwards.
47This works only when a file descriptor to the file is available and it
48is a regular file.
49.It
50A continuation offset relative to the end of the last up-level field
51.Dv ( \*[Am] ) .
52.El
53If the offset starts with the symbol
54.Dq + ,
55then all offsets are interpreted as from the beginning of the file (the
56default).
57.It Dv type
58The type of the data to be tested.
59The possible values are:
60.Bl -tag -width ".Dv lestring16"
61.It Dv byte
62A one-byte value.
63.It Dv short
64A two-byte value in this machine's native byte order.
65.It Dv long
66A four-byte value in this machine's native byte order.
67.It Dv quad
68An eight-byte value in this machine's native byte order.
69.It Dv float
70A 32-bit single precision IEEE floating point number in this machine's native byte order.
71.It Dv double
72A 64-bit double precision IEEE floating point number in this machine's native byte order.
73.It Dv string
74A string of bytes.
75The string type specification can be optionally followed by a /<width>
76option and optionally followed by a set of flags [bCcftTtWw]*.
77Slash characters can be used to separate options for readability.
78The width limits the number of characters to be copied.
79Zero means all characters.
80The following flags are supported:
81.Bl -tag -width B -compact -offset XXXX
82.It b
83Force binary file test.
84.It C
85Use upper case insensitive matching: upper case
86characters in the magic match both lower and upper case characters in the
87target, whereas lower case characters in the magic only match upper case
88characters in the target.
89For regex, this turns on REG_ICASE.
90.It c
91Use lower case insensitive matching: lower case
92characters in the magic match both lower and upper case characters in the
93target, whereas upper case characters in the magic only match lower case
94characters in the target.
95To do a complete case insensitive match, specify both
96.Dq c
97and
98.Dq C .
99For regex, this turns on REG_ICASE.
100.It f
101Require that the matched string is a full word, not a partial word match.
102.It s
103Don't include the match length in the offset computation.
104(only valid for search and regex)
105.It T
106Trim the string, i.e. leading and trailing whitespace
107is deleted before the string is printed.
108.It t
109Force text file test.
110.It W
111Compact whitespace in the target, which must
112contain at least one whitespace character.
113(not valid for regex)
114If the magic has
115.Dv n
116consecutive blanks, the target needs at least
117.Dv n
118consecutive blanks to match.
119.It w
120Treat every blank in the magic as an optional blank.
121(not valid for regex)
122.El
123.It Dv pstring
124A Pascal-style string where the first byte/short/int is interpreted as the
125unsigned length.
126The length defaults to byte and can be specified as a modifier.
127The following modifiers are supported:
128.Bl -tag -width B -compact -offset XXXX
129.It B
130A byte length (default).
131.It H
132A 2 byte big endian length.
133.It h
134A 2 byte little endian length.
135.It L
136A 4 byte big endian length.
137.It l
138A 4 byte little endian length.
139.It J
140The length includes itself in its count.
141.El
142The string is not NUL terminated.
143.Dq J
144is used rather than the more
145valuable
146.Dq I
147because this type of length is a feature of the JPEG
148format.
149.It Dv date
150A four-byte value interpreted as a UNIX date.
151.It Dv qdate
152An eight-byte value interpreted as a UNIX date.
153.It Dv ldate
154A four-byte value interpreted as a UNIX-style date, but interpreted as
155local time rather than UTC.
156.It Dv qldate
157An eight-byte value interpreted as a UNIX-style date, but interpreted as
158local time rather than UTC.
159.It Dv qwdate
160An eight-byte value interpreted as a Windows-style date.
161.It Dv msdosdate
162A two-byte value interpreted as FAT/DOS-style date.
163.It Dv msdostime
164A two-byte value interpreted as FAT/DOS-style time.
165.It Dv beid3
166A 32-bit ID3 length in big-endian byte order.
167.It Dv beshort
168A two-byte value in big-endian byte order.
169.It Dv belong
170A four-byte value in big-endian byte order.
171.It Dv bequad
172An eight-byte value in big-endian byte order.
173.It Dv befloat
174A 32-bit single precision IEEE floating point number in big-endian byte order.
175.It Dv bedouble
176A 64-bit double precision IEEE floating point number in big-endian byte order.
177.It Dv bedate
178A four-byte value in big-endian byte order,
179interpreted as a Unix date.
180.It Dv beqdate
181An eight-byte value in big-endian byte order,
182interpreted as a Unix date.
183.It Dv beldate
184A four-byte value in big-endian byte order,
185interpreted as a UNIX-style date, but interpreted as local time rather
186than UTC.
187.It Dv beqldate
188An eight-byte value in big-endian byte order,
189interpreted as a UNIX-style date, but interpreted as local time rather
190than UTC.
191.It Dv beqwdate
192An eight-byte value in big-endian byte order,
193interpreted as a Windows-style date.
194.It Dv bemsdosdate
195A two-byte value in big-endian byte order,
196interpreted as FAT/DOS-style date.
197.It Dv bemsdostime
198A two-byte value in big-endian byte order,
199interpreted as FAT/DOS-style time.
200.It Dv bestring16
201A two-byte unicode (UCS16) string in big-endian byte order.
202.It Dv leid3
203A 32-bit ID3 length in little-endian byte order.
204.It Dv leshort
205A two-byte value in little-endian byte order.
206.It Dv lelong
207A four-byte value in little-endian byte order.
208.It Dv lequad
209An eight-byte value in little-endian byte order.
210.It Dv lefloat
211A 32-bit single precision IEEE floating point number in little-endian byte order.
212.It Dv ledouble
213A 64-bit double precision IEEE floating point number in little-endian byte order.
214.It Dv ledate
215A four-byte value in little-endian byte order,
216interpreted as a UNIX date.
217.It Dv leqdate
218An eight-byte value in little-endian byte order,
219interpreted as a UNIX date.
220.It Dv leldate
221A four-byte value in little-endian byte order,
222interpreted as a UNIX-style date, but interpreted as local time rather
223than UTC.
224.It Dv leqldate
225An eight-byte value in little-endian byte order,
226interpreted as a UNIX-style date, but interpreted as local time rather
227than UTC.
228.It Dv leqwdate
229An eight-byte value in little-endian byte order,
230interpreted as a Windows-style date.
231.It Dv lemsdosdate
232A two-byte value in little-endian byte order,
233interpreted as FAT/DOS-style date.
234.It Dv lemsdostime
235A two-byte value in little-endian byte order,
236interpreted as FAT/DOS-style time.
237.It Dv lestring16
238A two-byte unicode (UCS16) string in little-endian byte order.
239.It Dv melong
240A four-byte value in middle-endian (PDP-11) byte order.
241.It Dv medate
242A four-byte value in middle-endian (PDP-11) byte order,
243interpreted as a UNIX date.
244.It Dv meldate
245A four-byte value in middle-endian (PDP-11) byte order,
246interpreted as a UNIX-style date, but interpreted as local time rather
247than UTC.
248.It Dv indirect
249Starting at the given offset, consult the magic database again.
250The offset of the
251.Dv indirect
252magic is by default absolute in the file, but one can specify
253.Dv /r
254to indicate that the offset is relative from the beginning of the entry.
255.It Dv name
256Define a
257.Dq named
258magic instance that can be called from another
259.Dv use
260magic entry, like a subroutine call.
261Named instance direct magic offsets are relative to the offset of the
262previous matched entry, but indirect offsets are relative to the beginning
263of the file as usual.
264Named magic entries return true if there was a match in the evaluation
265of the entry, or if there was a previous existing match.
266.It Dv use
267Recursively call the named magic starting from the current offset.
268If the name of the referenced begins with a
269.Dv ^
270then the endianness of the magic is switched; if the magic mentioned
271.Dv leshort
272for example,
273it is treated as
274.Dv beshort
275and vice versa.
276This is useful to avoid duplicating the rules for different endianness.
277.It Dv regex
278A regular expression match in extended POSIX regular expression syntax
279(like egrep).
280Regular expressions can take exponential time to process, and their
281performance is hard to predict, so their use is discouraged.
282When used in production environments, their performance
283should be carefully checked.
284The size of the string to search should also be limited by specifying
285.Dv /<length> ,
286to avoid performance issues scanning long files.
287The type specification can also be optionally followed by
288.Dv /[c][s][l] .
289The
290.Dq c
291flag makes the match case insensitive, while the
292.Dq s
293flag update the offset to the start offset of the match, rather than the end.
294The
295.Dq l
296modifier, changes the limit of length to mean number of lines instead of a
297byte count.
298Lines are delimited by the platforms native line delimiter.
299When a line count is specified, an implicit byte count is also computed assuming
300each line is 80 characters long.
301If neither a byte or line count is specified, the search is limited automatically
302to 8KiB.
303.Dv ^
304and
305.Dv $
306match the beginning and end of individual lines, respectively,
307not beginning and end of file.
308.It Dv search
309A literal string search starting at the given offset.
310The same modifier flags can be used as for string patterns.
311The search expression must contain the range in the form
312.Dv /number,
313that is the number of positions at which the match will be
314attempted, starting from the start offset.
315This is suitable for
316searching larger binary expressions with variable offsets, using
317.Dv \e
318escapes for special characters.
319The order of modifier and number is not relevant.
320.It Dv default
321This is intended to be used with the test
322.Em x
323(which is always true) and it has no type.
324It matches when no other test at that continuation level has matched before.
325Clearing that matched tests for a continuation level, can be done using the
326.Dv clear
327test.
328.It Dv clear
329This test is always true and clears the match flag for that continuation level.
330It is intended to be used with the
331.Dv default
332test.
333.It Dv der
334Parse the file as a DER Certificate file.
335The test field is used as a der type that needs to be matched.
336The DER types are:
337.Dv eoc ,
338.Dv bool ,
339.Dv int ,
340.Dv bit_str ,
341.Dv octet_str ,
342.Dv null ,
343.Dv obj_id ,
344.Dv obj_desc ,
345.Dv ext ,
346.Dv real ,
347.Dv enum ,
348.Dv embed ,
349.Dv utf8_str ,
350.Dv rel_oid ,
351.Dv time ,
352.Dv res2 ,
353.Dv seq ,
354.Dv set ,
355.Dv num_str ,
356.Dv prt_str ,
357.Dv t61_str ,
358.Dv vid_str ,
359.Dv ia5_str ,
360.Dv utc_time ,
361.Dv gen_time ,
362.Dv gr_str ,
363.Dv vis_str ,
364.Dv gen_str ,
365.Dv univ_str ,
366.Dv char_str ,
367.Dv bmp_str ,
368.Dv date ,
369.Dv tod ,
370.Dv datetime ,
371.Dv duration ,
372.Dv oid-iri ,
373.Dv rel-oid-iri .
374These types can be followed by an optional numeric size, which indicates
375the field width in bytes.
376.It Dv guid ,
377.It Dv leguid ,
378.It Dv beguid
379A Globally Unique Identifier, parsed and printed as
380XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX.
381Its format is a string.
382.It Dv offset
383This is a quad value indicating the current offset of the file.
384It can be used to determine the size of the file or the magic buffer.
385For example the magic entries:
386.Bd -literal -offset indent
387-0	offset	x	this file is %lld bytes
388-0	offset	<=100	must be more than 100 \e
389    bytes and is only %lld
390.Ed
391.It Dv octal
392A string representing an octal number.
393.El
394.Pp
395For compatibility with the Single
396.Ux
397Standard, the type specifiers
398.Dv dC
399and
400.Dv d1
401are equivalent to
402.Dv byte ,
403the type specifiers
404.Dv uC
405and
406.Dv u1
407are equivalent to
408.Dv ubyte ,
409the type specifiers
410.Dv dS
411and
412.Dv d2
413are equivalent to
414.Dv short ,
415the type specifiers
416.Dv uS
417and
418.Dv u2
419are equivalent to
420.Dv ushort ,
421the type specifiers
422.Dv dI ,
423.Dv dL ,
424and
425.Dv d4
426are equivalent to
427.Dv long ,
428the type specifiers
429.Dv uI ,
430.Dv uL ,
431and
432.Dv u4
433are equivalent to
434.Dv ulong ,
435the type specifier
436.Dv d8
437is equivalent to
438.Dv quad ,
439the type specifier
440.Dv u8
441is equivalent to
442.Dv uquad ,
443and the type specifier
444.Dv s
445is equivalent to
446.Dv string .
447In addition, the type specifier
448.Dv dQ
449is equivalent to
450.Dv quad
451and the type specifier
452.Dv uQ
453is equivalent to
454.Dv uquad .
455.Pp
456Each top-level magic pattern (see below for an explanation of levels)
457is classified as text or binary according to the types used.
458Types
459.Dq regex
460and
461.Dq search
462are classified as text tests, unless non-printable characters are used
463in the pattern.
464All other tests are classified as binary.
465A top-level
466pattern is considered to be a test text when all its patterns are text
467patterns; otherwise, it is considered to be a binary pattern.
468When
469matching a file, binary patterns are tried first; if no match is
470found, and the file looks like text, then its encoding is determined
471and the text patterns are tried.
472.Pp
473The numeric types may optionally be followed by an operand
474and a numeric value,
475to specify that the value is to be modified according to the operand and the
476numeric value before any comparisons are done.
477The following operands are supported:
478.Dv \*[Am], \*[Ba], \*[ua], +, -, \&*, /, %.
479Prepending a
480.Dv u
481to the type indicates that ordered comparisons should be unsigned.
482.It Dv test
483The value to be compared with the value from the file.
484If the type is
485numeric, this value
486is specified in C form; if it is a string, it is specified as a C string
487with the usual escapes permitted (e.g. \en for new-line).
488.Pp
489Numeric values
490may be preceded by a character indicating the operation to be performed.
491It may be
492.Dv = ,
493to specify that the value from the file must equal the specified value,
494.Dv \*[Lt] ,
495to specify that the value from the file must be less than the specified
496value,
497.Dv \*[Gt] ,
498to specify that the value from the file must be greater than the specified
499value,
500.Dv \*[Am] ,
501to specify that the value from the file must have set all of the bits
502that are set in the specified value,
503.Dv ^ ,
504to specify that the value from the file must have clear any of the bits
505that are set in the specified value, or
506.Dv ~ ,
507the value specified after is negated before tested.
508.Dv x ,
509to specify that any value will match.
510If the character is omitted, it is assumed to be
511.Dv = .
512Operators
513.Dv \*[Am] ,
514.Dv ^ ,
515and
516.Dv ~
517don't work with floats and doubles.
518The operator
519.Dv !\&
520specifies that the line matches if the test does
521.Em not
522succeed.
523.Pp
524Numeric values are specified in C form; e.g.
525.Dv 13
526is decimal,
527.Dv 013
528is octal, and
529.Dv 0x13
530is hexadecimal.
531.Pp
532Numeric operations are not performed on date types, instead the numeric
533value is interpreted as an offset.
534.Pp
535For string values, the string from the
536file must match the specified string.
537The operators
538.Dv = ,
539.Dv \*[Lt]
540and
541.Dv \*[Gt]
542(but not
543.Dv \*[Am] )
544can be applied to strings.
545The length used for matching is that of the string argument
546in the magic file.
547This means that a line can match any non-empty string (usually used to
548then print the string), with
549.Em \*[Gt]\e0
550(because all non-empty strings are greater than the empty string).
551.Pp
552Dates are treated as numerical values in the respective internal
553representation.
554.Pp
555The special test
556.Em x
557always evaluates to true.
558.It Dv message
559The message to be printed if the comparison succeeds.
560If the string contains a
561.Xr printf 3
562format specification, the value from the file (with any specified masking
563performed) is printed using the message as the format string.
564If the string begins with
565.Dq \eb ,
566the message printed is the remainder of the string with no whitespace
567added before it: multiple matches are normally separated by a single
568space.
569.El
570.Pp
571An APPLE 4+4 character APPLE creator and type can be specified as:
572.Bd -literal -offset indent
573!:apple	CREATYPE
574.Ed
575.Pp
576A slash-separated list of commonly found filename extensions can be specified
577as:
578.Bd -literal -offset indent
579!:ext	ext[/ext...]
580.Ed
581.Pp
582i.e. the literal string
583.Dq !:ext
584followed by a slash-separated list of commonly found extensions; for example
585for JPEG images:
586.Bd -literal -offset indent
587!:ext jpeg/jpg/jpe/jfif
588.Ed
589.Pp
590A MIME type is given on a separate line, which must be the next
591non-blank or comment line after the magic line that identifies the
592file type, and has the following format:
593.Bd -literal -offset indent
594!:mime	MIMETYPE
595.Ed
596.Pp
597i.e. the literal string
598.Dq !:mime
599followed by the MIME type.
600.Pp
601An optional strength can be supplied on a separate line which refers to
602the current magic description using the following format:
603.Bd -literal -offset indent
604!:strength OP VALUE
605.Ed
606.Pp
607The operator
608.Dv OP
609can be:
610.Dv + ,
611.Dv - ,
612.Dv * ,
613or
614.Dv /
615and
616.Dv VALUE
617is a constant between 0 and 255.
618This constant is applied using the specified operator
619to the currently computed default magic strength.
620.Pp
621Some file formats contain additional information which is to be printed
622along with the file type or need additional tests to determine the true
623file type.
624These additional tests are introduced by one or more
625.Em \*[Gt]
626characters preceding the offset.
627The number of
628.Em \*[Gt]
629on the line indicates the level of the test; a line with no
630.Em \*[Gt]
631at the beginning is considered to be at level 0.
632Tests are arranged in a tree-like hierarchy:
633if the test on a line at level
634.Em n
635succeeds, all following tests at level
636.Em n+1
637are performed, and the messages printed if the tests succeed, until a line
638with level
639.Em n
640(or less) appears.
641For more complex files, one can use empty messages to get just the
642"if/then" effect, in the following way:
643.Bd -literal -offset indent
6440      string    MZ
645\*[Gt]0x18  uleshort  \*[Lt]0x40   MS-DOS executable
646\*[Gt]0x18  uleshort  \*[Gt]0x3f   extended PC executable (e.g., MS Windows)
647.Ed
648.Pp
649Offsets do not need to be constant, but can also be read from the file
650being examined.
651If the first character following the last
652.Em \*[Gt]
653is a
654.Em \&(
655then the string after the parenthesis is interpreted as an indirect offset.
656That means that the number after the parenthesis is used as an offset in
657the file.
658The value at that offset is read, and is used again as an offset
659in the file.
660Indirect offsets are of the form:
661.Em ( x [[.,][bBcCeEfFgGhHiIlmosSqQ]][+\-][ y ]) .
662The value of
663.Em x
664is used as an offset in the file.
665A byte, id3 length, short or long is read at that offset depending on the
666.Em [bBcCeEfFgGhHiIlLmsSqQ]
667type specifier.
668The value is treated as signed if
669.Dq \&,
670is specified or unsigned if
671.Dq \&.
672is specified.
673The capitalized types interpret the number as a big endian
674value, whereas the small letter versions interpret the number as a little
675endian value;
676the
677.Em m
678type interprets the number as a middle endian (PDP-11) value.
679To that number the value of
680.Em y
681is added and the result is used as an offset in the file.
682The default type if one is not specified is long.
683The following types are recognized:
684.Bl -column -offset indent "Type" "Half/Short" "Little" "Size"
685.It Sy Type	Sy Mnemonic	Sy Endian	Sy Size
686.It bcBC	Byte/Char	N/A	1
687.It efg	Double	Little	8
688.It EFG	Double	Big	8
689.It hs	Half/Short	Little	2
690.It HS	Half/Short	Big	2
691.It i	ID3	Little	4
692.It I	ID3	Big	4
693.It l	Long	Little	4
694.It L	Long	Big	4
695.It m	Middle	Middle	4
696.It o	Octal	Textual	Variable
697.It q	Quad	Little	8
698.It Q	Quad	Big	8
699.El
700.Pp
701That way variable length structures can be examined:
702.Bd -literal -offset indent
703# MS Windows executables are also valid MS-DOS executables
7040           string   MZ
705\*[Gt]0x18       uleshort \*[Lt]0x40  MZ executable (MS-DOS)
706# skip the whole block below if it is not an extended executable
707\*[Gt]0x18       uleshort \*[Gt]0x3f
708\*[Gt]\*[Gt](0x3c.l)  string   PE\e0\e0 PE executable (MS-Windows)
709\*[Gt]\*[Gt](0x3c.l)  string   LX\e0\e0 LX executable (OS/2)
710.Ed
711.Pp
712This strategy of examining has a drawback: you must make sure that you
713eventually print something, or users may get empty output (such as when
714there is neither PE\e0\e0 nor LX\e0\e0 in the above example).
715.Pp
716If this indirect offset cannot be used directly, simple calculations are
717possible: appending
718.Em [+-*/%\*[Am]|^]number
719inside parentheses allows one to modify
720the value read from the file before it is used as an offset:
721.Bd -literal -offset indent
722# MS Windows executables are also valid MS-DOS executables
7230           string   MZ
724# sometimes, the value at 0x18 is less that 0x40 but there's still an
725# extended executable, simply appended to the file
726\*[Gt]0x18       uleshort \*[Lt]0x40
727\*[Gt]\*[Gt](4.s*512) leshort  0x014c  COFF executable (MS-DOS, DJGPP)
728\*[Gt]\*[Gt](4.s*512) leshort  !0x014c MZ executable (MS-DOS)
729.Ed
730.Pp
731Sometimes you do not know the exact offset as this depends on the length or
732position (when indirection was used before) of preceding fields.
733You can specify an offset relative to the end of the last up-level
734field using
735.Sq \*[Am]
736as a prefix to the offset:
737.Bd -literal -offset indent
7380           string   MZ
739\*[Gt]0x18       uleshort \*[Gt]0x3f
740\*[Gt]\*[Gt](0x3c.l)  string   PE\e0\e0    PE executable (MS-Windows)
741# immediately following the PE signature is the CPU type
742\*[Gt]\*[Gt]\*[Gt]\*[Am]0       leshort  0x14c     for Intel 80386
743\*[Gt]\*[Gt]\*[Gt]\*[Am]0       leshort  0x8664    for x86-64
744\*[Gt]\*[Gt]\*[Gt]\*[Am]0       leshort  0x184     for DEC Alpha
745.Ed
746.Pp
747Indirect and relative offsets can be combined:
748.Bd -literal -offset indent
7490             string   MZ
750\*[Gt]0x18         uleshort \*[Lt]0x40
751\*[Gt]\*[Gt](4.s*512)   leshort  !0x014c MZ executable (MS-DOS)
752# if it's not COFF, go back 512 bytes and add the offset taken
753# from byte 2/3, which is yet another way of finding the start
754# of the extended executable
755\*[Gt]\*[Gt]\*[Gt]\*[Am](2.s-514) string   LE      LE executable (MS Windows VxD driver)
756.Ed
757.Pp
758Or the other way around:
759.Bd -literal -offset indent
7600                 string   MZ
761\*[Gt]0x18             uleshort \*[Gt]0x3f
762\*[Gt]\*[Gt](0x3c.l)        string   LE\e0\e0  LE executable (MS-Windows)
763# at offset 0x80 (-4, since relative offsets start at the end
764# of the up-level match) inside the LE header, we find the absolute
765# offset to the code area, where we look for a specific signature
766\*[Gt]\*[Gt]\*[Gt](\*[Am]0x7c.l+0x26) string   UPX     \eb, UPX compressed
767.Ed
768.Pp
769Or even both!
770.Bd -literal -offset indent
7710                string   MZ
772\*[Gt]0x18            uleshort \*[Gt]0x3f
773\*[Gt]\*[Gt](0x3c.l)       string   LE\e0\e0 LE executable (MS-Windows)
774# at offset 0x58 inside the LE header, we find the relative offset
775# to a data area where we look for a specific signature
776\*[Gt]\*[Gt]\*[Gt]\*[Am](\*[Am]0x54.l-3)  string   UNACE  \eb, ACE self-extracting archive
777.Ed
778.Pp
779If you have to deal with offset/length pairs in your file, even the
780second value in a parenthesized expression can be taken from the file itself,
781using another set of parentheses.
782Note that this additional indirect offset is always relative to the
783start of the main indirect offset.
784.Bd -literal -offset indent
7850                 string       MZ
786\*[Gt]0x18             uleshort     \*[Gt]0x3f
787\*[Gt]\*[Gt](0x3c.l)        string       PE\e0\e0 PE executable (MS-Windows)
788# search for the PE section called ".idata"...
789\*[Gt]\*[Gt]\*[Gt]\*[Am]0xf4          search/0x140 .idata
790# ...and go to the end of it, calculated from start+length;
791# these are located 14 and 10 bytes after the section name
792\*[Gt]\*[Gt]\*[Gt]\*[Gt](\*[Am]0xe.l+(-4)) string       PK\e3\e4 \eb, ZIP self-extracting archive
793.Ed
794.Pp
795If you have a list of known values at a particular continuation level,
796and you want to provide a switch-like default case:
797.Bd -literal -offset indent
798# clear that continuation level match
799\*[Gt]18	clear	x
800\*[Gt]18	lelong	1	one
801\*[Gt]18	lelong	2	two
802\*[Gt]18	default	x
803# print default match
804\*[Gt]\*[Gt]18	lelong	x	unmatched 0x%x
805.Ed
806.Sh SEE ALSO
807.Xr file __CSECTION__
808\- the command that reads this file.
809.Sh BUGS
810The formats
811.Dv long ,
812.Dv belong ,
813.Dv lelong ,
814.Dv melong ,
815.Dv short ,
816.Dv beshort ,
817and
818.Dv leshort
819do not depend on the length of the C data types
820.Dv short
821and
822.Dv long
823on the platform, even though the Single
824.Ux
825Specification implies that they do.
826However, as OS X Mountain Lion has passed the Single
827.Ux
828Specification validation suite, and supplies a version of
829.Xr file __CSECTION__
830in which they do not depend on the sizes of the C data types and that is
831built for a 64-bit environment in which
832.Dv long
833is 8 bytes rather than 4 bytes, presumably the validation suite does not
834test whether, for example
835.Dv long
836refers to an item with the same size as the C data type
837.Dv long .
838There should probably be
839.Dv type
840names
841.Dv int8 ,
842.Dv uint8 ,
843.Dv int16 ,
844.Dv uint16 ,
845.Dv int32 ,
846.Dv uint32 ,
847.Dv int64 ,
848and
849.Dv uint64 ,
850and specified-byte-order variants of them,
851to make it clearer that those types have specified widths.
852.\"
853.\" From: guy@sun.uucp (Guy Harris)
854.\" Newsgroups: net.bugs.usg
855.\" Subject: /etc/magic's format isn't well documented
856.\" Message-ID: <2752@sun.uucp>
857.\" Date: 3 Sep 85 08:19:07 GMT
858.\" Organization: Sun Microsystems, Inc.
859.\" Lines: 136
860.\"
861.\" Here's a manual page for the format accepted by the "file" made by adding
862.\" the changes I posted to the S5R2 version.
863.\"
864.\" Modified for Ian Darwin's version of the file command.
865.\"
866.\" For emacs editor
867.\" Local Variables:
868.\" eval: (add-hook 'before-save-hook 'time-stamp)
869.\" time-stamp-start: ".Dd "
870.\" time-stamp-end: "$"
871.\" time-stamp-format: "%:B %02d, %:Y"
872.\" time-stamp-time-zone: "UTC0"
873.\" system-time-locale: "C"
874.\" eval:(setq compile-command (concat "groff -Tlatin1 -m man " (buffer-file-name)) )
875.\" End:
876.\"
877