Debian Codegraph @

upstream/debian-unstable/man5/magic.5

raw
1 .\" $File: magic.man,v 1.115 2025/11/24 15:45:00 christos Exp $
2 .Dd November 24, 2025
3 .Dt MAGIC 5
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
10 This manual page documents the format of magic files as
11 used by the
12 .Xr file 1
13 command, version 5.47.
14 The
15 .Xr file 1
16 command identifies the type of a file using,
17 among other tests,
18 a test for whether the file contains certain
19 .Dq "magic patterns" .
20 The database of these
21 .Dq "magic patterns"
22 is usually located in a binary file in
23 .Pa /usr/share/misc/magic.mgc
24 or a directory of source text magic pattern fragment files in
25 .Pa /usr/share/misc/magic .
26 The database specifies what patterns are to be tested for, what message or
27 MIME type to print if a particular pattern is found,
28 and additional information to extract from the file.
29 .Pp
30 The format of the source fragment files that are used to build this database
31 is as follows:
32 Each line of a fragment file specifies a test to be performed.
33 A test compares the data starting at a particular offset
34 in the file with a byte value, a string or a numeric value.
35 If the test succeeds, a message is printed.
36 The line consists of the following fields:
37 .Bl -tag -width ".Dv message"
38 .It Dv offset
39 A number specifying the offset (in bytes) into the file of the data
40 which is to be tested.
41 This offset can be a negative number if it is:
42 .Bl -bullet -compact
43 .It
44 The first direct offset of the magic entry (at continuation level 0),
45 in which case it is interpreted an offset from end end of the file
46 going backwards.
47 This works only when a file descriptor to the file is available and it
48 is a regular file.
49 .It
50 A continuation offset relative to the end of the last up-level field
51 .Dv ( \*[Am] ) .
52 .El
53 If the offset starts with the symbol
54 .Dq + ,
55 then all offsets are interpreted as from the beginning of the file (the
56 default).
57 .It Dv type
58 The type of the data to be tested.
59 The possible values are:
60 .Bl -tag -width ".Dv lestring16"
61 .It Dv byte
62 A one-byte value.
63 .It Dv short
64 A two-byte value in this machine's native byte order.
65 .It Dv long
66 A four-byte value in this machine's native byte order.
67 .It Dv quad
68 An eight-byte value in this machine's native byte order.
69 .It Dv float
70 A 32-bit single precision IEEE floating point number in this machine's native byte order.
71 .It Dv double
72 A 64-bit double precision IEEE floating point number in this machine's native byte order.
73 .It Dv string
74 A string of bytes.
75 The string type specification can be optionally followed by a /<width>
76 option and optionally followed by a set of flags [bCcftTtWw]*.
77 Slash characters can be used to separate options for readability.
78 The width limits the number of characters to be copied.
79 Zero means all characters.
80 The following flags are supported:
81 .Bl -tag -width B -compact -offset XXXX
82 .It b
83 Force binary file test.
84 .It C
85 Use upper case insensitive matching: upper case
86 characters in the magic match both lower and upper case characters in the
87 target, whereas lower case characters in the magic only match upper case
88 characters in the target.
89 (not valid for regex)
90 .It c
91 Use lower case insensitive matching: lower case
92 characters in the magic match both lower and upper case characters in the
93 target, whereas upper case characters in the magic only match upper case
94 characters in the target.
95 (not valid for regex)
96 To do a complete case insensitive match, specify both
97 .Dq c
98 and
99 .Dq C .
100 .It f
101 Require that the matched string is a full word, not a partial word match.
102 .It s
103 Don't include the match length in the offset computation.
104 (only valid for search and regex)
105 .It T
106 Trim the string, i.e. leading and trailing whitespace
107 is deleted before the string is printed.
108 .It t
109 Force text file test.
110 .It W
111 Compact whitespace in the target, which must
112 contain at least one whitespace character.
113 (not valid for regex)
114 If the magic has
115 .Dv n
116 consecutive blanks, the target needs at least
117 .Dv n
118 consecutive blanks to match.
119 .It w
120 Treat every blank in the magic as an optional blank.
121 (not valid for regex)
122 .El
123 .It Dv pstring
124 A Pascal-style string where the first byte/short/int is interpreted as the
125 unsigned length.
126 The length defaults to byte and can be specified as a modifier.
127 The following modifiers are supported:
128 .Bl -tag -width B -compact -offset XXXX
129 .It B
130 A byte length (default).
131 .It H
132 A 2 byte big endian length.
133 .It h
134 A 2 byte little endian length.
135 .It L
136 A 4 byte big endian length.
137 .It l
138 A 4 byte little endian length.
139 .It J
140 The length includes itself in its count.
141 .El
142 The string is not NUL terminated.
143 .Dq J
144 is used rather than the more
145 valuable
146 .Dq I
147 because this type of length is a feature of the JPEG
148 format.
149 .It Dv date
150 A four-byte value interpreted as a UNIX date.
151 .It Dv qdate
152 An eight-byte value interpreted as a UNIX date.
153 .It Dv ldate
154 A four-byte value interpreted as a UNIX-style date, but interpreted as
155 local time rather than UTC.
156 .It Dv qldate
157 An eight-byte value interpreted as a UNIX-style date, but interpreted as
158 local time rather than UTC.
159 .It Dv qwdate
160 An eight-byte value interpreted as a Windows-style date.
161 .It Dv msdosdate
162 A two-byte value interpreted as FAT/DOS-style date.
163 .It Dv msdostime
164 A two-byte value interpreted as FAT/DOS-style time.
165 .It Dv beid3
166 A 32-bit ID3 length in big-endian byte order.
167 .It Dv beshort
168 A two-byte value in big-endian byte order.
169 .It Dv belong
170 A four-byte value in big-endian byte order.
171 .It Dv bequad
172 An eight-byte value in big-endian byte order.
173 .It Dv befloat
174 A 32-bit single precision IEEE floating point number in big-endian byte order.
175 .It Dv bedouble
176 A 64-bit double precision IEEE floating point number in big-endian byte order.
177 .It Dv bedate
178 A four-byte value in big-endian byte order,
179 interpreted as a Unix date.
180 .It Dv beqdate
181 An eight-byte value in big-endian byte order,
182 interpreted as a Unix date.
183 .It Dv beldate
184 A four-byte value in big-endian byte order,
185 interpreted as a UNIX-style date, but interpreted as local time rather
186 than UTC.
187 .It Dv beqldate
188 An eight-byte value in big-endian byte order,
189 interpreted as a UNIX-style date, but interpreted as local time rather
190 than UTC.
191 .It Dv beqwdate
192 An eight-byte value in big-endian byte order,
193 interpreted as a Windows-style date.
194 .It Dv bemsdosdate
195 A two-byte value in big-endian byte order,
196 interpreted as FAT/DOS-style date.
197 .It Dv bemsdostime
198 A two-byte value in big-endian byte order,
199 interpreted as FAT/DOS-style time.
200 .It Dv bestring16
201 A two-byte unicode (UCS16) string in big-endian byte order.
202 .It Dv leid3
203 A 32-bit ID3 length in little-endian byte order.
204 .It Dv leshort
205 A two-byte value in little-endian byte order.
206 .It Dv lelong
207 A four-byte value in little-endian byte order.
208 .It Dv lequad
209 An eight-byte value in little-endian byte order.
210 .It Dv lefloat
211 A 32-bit single precision IEEE floating point number in little-endian byte order.
212 .It Dv ledouble
213 A 64-bit double precision IEEE floating point number in little-endian byte order.
214 .It Dv ledate
215 A four-byte value in little-endian byte order,
216 interpreted as a UNIX date.
217 .It Dv leqdate
218 An eight-byte value in little-endian byte order,
219 interpreted as a UNIX date.
220 .It Dv leldate
221 A four-byte value in little-endian byte order,
222 interpreted as a UNIX-style date, but interpreted as local time rather
223 than UTC.
224 .It Dv leqldate
225 An eight-byte value in little-endian byte order,
226 interpreted as a UNIX-style date, but interpreted as local time rather
227 than UTC.
228 .It Dv leqwdate
229 An eight-byte value in little-endian byte order,
230 interpreted as a Windows-style date.
231 .It Dv lemsdosdate
232 A two-byte value in little-endian byte order,
233 interpreted as FAT/DOS-style date.
234 .It Dv lemsdostime
235 A two-byte value in little-endian byte order,
236 interpreted as FAT/DOS-style time.
237 .It Dv lestring16
238 A two-byte unicode (UCS16) string in little-endian byte order.
239 .It Dv melong
240 A four-byte value in middle-endian (PDP-11) byte order.
241 .It Dv medate
242 A four-byte value in middle-endian (PDP-11) byte order,
243 interpreted as a UNIX date.
244 .It Dv meldate
245 A four-byte value in middle-endian (PDP-11) byte order,
246 interpreted as a UNIX-style date, but interpreted as local time rather
247 than UTC.
248 .It Dv indirect
249 Starting at the given offset, consult the magic database again.
250 The offset of the
251 .Dv indirect
252 magic is by default absolute in the file, but one can specify
253 .Dv /r
254 to indicate that the offset is relative from the beginning of the entry.
255 .It Dv name
256 Define a
257 .Dq named
258 magic instance that can be called from another
259 .Dv use
260 magic entry, like a subroutine call.
261 Named instance direct magic offsets are relative to the offset of the
262 previous matched entry, but indirect offsets are relative to the beginning
263 of the file as usual.
264 Named magic entries return true if there was a match in the evaluation
265 of the entry, or if there was a previous existing match.
266 .It Dv use
267 Recursively call the named magic starting from the current offset.
268 If the name of the referenced begins with a
269 .Dv ^
270 then the endianness of the magic is switched; if the magic mentioned
271 .Dv leshort
272 for example,
273 it is treated as
274 .Dv beshort
275 and vice versa.
276 This is useful to avoid duplicating the rules for different endianness.
277 .It Dv regex
278 A regular expression match in extended POSIX regular expression syntax
279 (like egrep).
280 Regular expressions can take exponential time to process, and their
281 performance is hard to predict, so their use is discouraged.
282 When used in production environments, their performance
283 should be carefully checked.
284 The size of the string to search should also be limited by specifying
285 .Dv /<length> ,
286 to avoid performance issues scanning long files.
287 The type specification can also be optionally followed by
288 .Dv /[c][s][l] .
289 The
290 .Dq c
291 flag makes the match case insensitive, while the
292 .Dq s
293 flag update the offset to the start offset of the match, rather than the end.
294 The
295 .Dq l
296 modifier, changes the limit of length to mean number of lines instead of a
297 byte count.
298 Lines are delimited by the platforms native line delimiter.
299 When a line count is specified, an implicit byte count also computed assuming
300 each line is 80 characters long.
301 If neither a byte or line count is specified, the search is limited automatically
302 to 8KiB.
303 .Dv ^
304 and
305 .Dv $
306 match the beginning and end of individual lines, respectively,
307 not beginning and end of file.
308 .It Dv search
309 A literal string search starting at the given offset.
310 The same modifier flags can be used as for string patterns.
311 The search expression must contain the range in the form
312 .Dv /number,
313 that is the number of positions at which the match will be
314 attempted, starting from the start offset.
315 This is suitable for
316 searching larger binary expressions with variable offsets, using
317 .Dv \e
318 escapes for special characters.
319 The order of modifier and number is not relevant.
320 .It Dv default
321 This is intended to be used with the test
322 .Em x
323 (which is always true) and it has no type.
324 It matches when no other test at that continuation level has matched before.
325 Clearing that matched tests for a continuation level, can be done using the
326 .Dv clear
327 test.
328 .It Dv clear
329 This test is always true and clears the match flag for that continuation level.
330 It is intended to be used with the
331 .Dv default
332 test.
333 .It Dv der
334 Parse the file as a DER Certificate file.
335 The test field is used as a der type that needs to be matched.
336 The 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 .
374 These types can be followed by an optional numeric size, which indicates
375 the field width in bytes.
376 .It Dv guid ,
377 .It Dv leguid ,
378 .It Dv beguid
379 A Globally Unique Identifier, parsed and printed as
380 XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX.
381 It's format is a string.
382 .It Dv offset
383 This is a quad value indicating the current offset of the file.
384 It can be used to determine the size of the file or the magic buffer.
385 For 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
392 A string representing an octal number.
393 .El
394 .Pp
395 For compatibility with the Single
396 .Ux
397 Standard, the type specifiers
398 .Dv dC
399 and
400 .Dv d1
401 are equivalent to
402 .Dv byte ,
403 the type specifiers
404 .Dv uC
405 and
406 .Dv u1
407 are equivalent to
408 .Dv ubyte ,
409 the type specifiers
410 .Dv dS
411 and
412 .Dv d2
413 are equivalent to
414 .Dv short ,
415 the type specifiers
416 .Dv uS
417 and
418 .Dv u2
419 are equivalent to
420 .Dv ushort ,
421 the type specifiers
422 .Dv dI ,
423 .Dv dL ,
424 and
425 .Dv d4
426 are equivalent to
427 .Dv long ,
428 the type specifiers
429 .Dv uI ,
430 .Dv uL ,
431 and
432 .Dv u4
433 are equivalent to
434 .Dv ulong ,
435 the type specifier
436 .Dv d8
437 is equivalent to
438 .Dv quad ,
439 the type specifier
440 .Dv u8
441 is equivalent to
442 .Dv uquad ,
443 and the type specifier
444 .Dv s
445 is equivalent to
446 .Dv string .
447 In addition, the type specifier
448 .Dv dQ
449 is equivalent to
450 .Dv quad
451 and the type specifier
452 .Dv uQ
453 is equivalent to
454 .Dv uquad .
455 .Pp
456 Each top-level magic pattern (see below for an explanation of levels)
457 is classified as text or binary according to the types used.
458 Types
459 .Dq regex
460 and
461 .Dq search
462 are classified as text tests, unless non-printable characters are used
463 in the pattern.
464 All other tests are classified as binary.
465 A top-level
466 pattern is considered to be a test text when all its patterns are text
467 patterns; otherwise, it is considered to be a binary pattern.
468 When
469 matching a file, binary patterns are tried first; if no match is
470 found, and the file looks like text, then its encoding is determined
471 and the text patterns are tried.
472 .Pp
473 The numeric types may optionally be followed by an operand
474 and a numeric value,
475 to specify that the value is to be modified according to the operand and the
476 numeric value before any comparisons are done.
477 The following operands are supported:
478 .Dv \*[Am], \*[Ba], \*[ua], +, -, \&*, /, %.
479 Prepending a
480 .Dv u
481 to the type indicates that ordered comparisons should be unsigned.
482 .It Dv test
483 The value to be compared with the value from the file.
484 If the type is
485 numeric, this value
486 is specified in C form; if it is a string, it is specified as a C string
487 with the usual escapes permitted (e.g. \en for new-line).
488 .Pp
489 Numeric values
490 may be preceded by a character indicating the operation to be performed.
491 It may be
492 .Dv = ,
493 to specify that the value from the file must equal the specified value,
494 .Dv \*[Lt] ,
495 to specify that the value from the file must be less than the specified
496 value,
497 .Dv \*[Gt] ,
498 to specify that the value from the file must be greater than the specified
499 value,
500 .Dv \*[Am] ,
501 to specify that the value from the file must have set all of the bits
502 that are set in the specified value,
503 .Dv ^ ,
504 to specify that the value from the file must have clear any of the bits
505 that are set in the specified value, or
506 .Dv ~ ,
507 the value specified after is negated before tested.
508 .Dv x ,
509 to specify that any value will match.
510 If the character is omitted, it is assumed to be
511 .Dv = .
512 Operators
513 .Dv \*[Am] ,
514 .Dv ^ ,
515 and
516 .Dv ~
517 don't work with floats and doubles.
518 The operator
519 .Dv !\&
520 specifies that the line matches if the test does
521 .Em not
522 succeed.
523 .Pp
524 Numeric values are specified in C form; e.g.
525 .Dv 13
526 is decimal,
527 .Dv 013
528 is octal, and
529 .Dv 0x13
530 is hexadecimal.
531 .Pp
532 Numeric operations are not performed on date types, instead the numeric
533 value is interpreted as an offset.
534 .Pp
535 For string values, the string from the
536 file must match the specified string.
537 The operators
538 .Dv = ,
539 .Dv \*[Lt]
540 and
541 .Dv \*[Gt]
542 (but not
543 .Dv \*[Am] )
544 can be applied to strings.
545 The length used for matching is that of the string argument
546 in the magic file.
547 This means that a line can match any non-empty string (usually used to
548 then print the string), with
549 .Em \*[Gt]\e0
550 (because all non-empty strings are greater than the empty string).
551 .Pp
552 Dates are treated as numerical values in the respective internal
553 representation.
554 .Pp
555 The special test
556 .Em x
557 always evaluates to true.
558 .It Dv message
559 The message to be printed if the comparison succeeds.
560 If the string contains a
561 .Xr printf 3
562 format specification, the value from the file (with any specified masking
563 performed) is printed using the message as the format string.
564 If the string begins with
565 .Dq \eb ,
566 the message printed is the remainder of the string with no whitespace
567 added before it: multiple matches are normally separated by a single
568 space.
569 .El
570 .Pp
571 An APPLE 4+4 character APPLE creator and type can be specified as:
572 .Bd -literal -offset indent
573 !:apple CREATYPE
574 .Ed
575 .Pp
576 A slash-separated list of commonly found filename extensions can be specified
577 as:
578 .Bd -literal -offset indent
579 !:ext ext[/ext...]
580 .Ed
581 .Pp
582 i.e. the literal string
583 .Dq !:ext
584 followed by a slash-separated list of commonly found extensions; for example
585 for JPEG images:
586 .Bd -literal -offset indent
587 !:ext jpeg/jpg/jpe/jfif
588 .Ed
589 .Pp
590 A MIME type is given on a separate line, which must be the next
591 non-blank or comment line after the magic line that identifies the
592 file type, and has the following format:
593 .Bd -literal -offset indent
594 !:mime MIMETYPE
595 .Ed
596 .Pp
597 i.e. the literal string
598 .Dq !:mime
599 followed by the MIME type.
600 .Pp
601 An optional strength can be supplied on a separate line which refers to
602 the current magic description using the following format:
603 .Bd -literal -offset indent
604 !:strength OP VALUE
605 .Ed
606 .Pp
607 The operand
608 .Dv OP
609 can be:
610 .Dv + ,
611 .Dv - ,
612 .Dv * ,
613 or
614 .Dv /
615 and
616 .Dv VALUE
617 is a constant between 0 and 255.
618 This constant is applied using the specified operand
619 to the currently computed default magic strength.
620 .Pp
621 Some file formats contain additional information which is to be printed
622 along with the file type or need additional tests to determine the true
623 file type.
624 These additional tests are introduced by one or more
625 .Em \*[Gt]
626 characters preceding the offset.
627 The number of
628 .Em \*[Gt]
629 on the line indicates the level of the test; a line with no
630 .Em \*[Gt]
631 at the beginning is considered to be at level 0.
632 Tests are arranged in a tree-like hierarchy:
633 if the test on a line at level
634 .Em n
635 succeeds, all following tests at level
636 .Em n+1
637 are performed, and the messages printed if the tests succeed, until a line
638 with level
639 .Em n
640 (or less) appears.
641 For 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
644 0 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
649 Offsets do not need to be constant, but can also be read from the file
650 being examined.
651 If the first character following the last
652 .Em \*[Gt]
653 is a
654 .Em \&(
655 then the string after the parenthesis is interpreted as an indirect offset.
656 That means that the number after the parenthesis is used as an offset in
657 the file.
658 The value at that offset is read, and is used again as an offset
659 in the file.
660 Indirect offsets are of the form:
661 .Em ( x [[.,][bBcCeEfFgGhHiIlmosSqQ]][+\-][ y ]) .
662 The value of
663 .Em x
664 is used as an offset in the file.
665 A byte, id3 length, short or long is read at that offset depending on the
666 .Em [bBcCeEfFgGhHiIlLmsSqQ]
667 type specifier.
668 The value is treated as signed if
669 .Dq \&,
670 is specified or unsigned if
671 .Dq \&.
672 is specified.
673 The capitalized types interpret the number as a big endian
674 value, whereas the small letter versions interpret the number as a little
675 endian value;
676 the
677 .Em m
678 type interprets the number as a middle endian (PDP-11) value.
679 To that number the value of
680 .Em y
681 is added and the result is used as an offset in the file.
682 The default type if one is not specified is long.
683 The 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
701 That way variable length structures can be examined:
702 .Bd -literal -offset indent
703 # MS Windows executables are also valid MS-DOS executables
704 0 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
712 This strategy of examining has a drawback: you must make sure that you
713 eventually print something, or users may get empty output (such as when
714 there is neither PE\e0\e0 nor LE\e0\e0 in the above example).
715 .Pp
716 If this indirect offset cannot be used directly, simple calculations are
717 possible: appending
718 .Em [+-*/%\*[Am]|^]number
719 inside parentheses allows one to modify
720 the 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
723 0 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
731 Sometimes you do not know the exact offset as this depends on the length or
732 position (when indirection was used before) of preceding fields.
733 You can specify an offset relative to the end of the last up-level
734 field using
735 .Sq \*[Am]
736 as a prefix to the offset:
737 .Bd -literal -offset indent
738 0 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
747 Indirect and relative offsets can be combined:
748 .Bd -literal -offset indent
749 0 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
758 Or the other way around:
759 .Bd -literal -offset indent
760 0 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
769 Or even both!
770 .Bd -literal -offset indent
771 0 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
779 If you have to deal with offset/length pairs in your file, even the
780 second value in a parenthesized expression can be taken from the file itself,
781 using another set of parentheses.
782 Note that this additional indirect offset is always relative to the
783 start of the main indirect offset.
784 .Bd -literal -offset indent
785 0 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
795 If you have a list of known values at a particular continuation level,
796 and 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 1
808 \- the command that reads this file.
809 .Sh BUGS
810 The formats
811 .Dv long ,
812 .Dv belong ,
813 .Dv lelong ,
814 .Dv melong ,
815 .Dv short ,
816 .Dv beshort ,
817 and
818 .Dv leshort
819 do not depend on the length of the C data types
820 .Dv short
821 and
822 .Dv long
823 on the platform, even though the Single
824 .Ux
825 Specification implies that they do.
826 However, as OS X Mountain Lion has passed the Single
827 .Ux
828 Specification validation suite, and supplies a version of
829 .Xr file 1
830 in which they do not depend on the sizes of the C data types and that is
831 built for a 64-bit environment in which
832 .Dv long
833 is 8 bytes rather than 4 bytes, presumably the validation suite does not
834 test whether, for example
835 .Dv long
836 refers to an item with the same size as the C data type
837 .Dv long .
838 There should probably be
839 .Dv type
840 names
841 .Dv int8 ,
842 .Dv uint8 ,
843 .Dv int16 ,
844 .Dv uint16 ,
845 .Dv int32 ,
846 .Dv uint32 ,
847 .Dv int64 ,
848 and
849 .Dv uint64 ,
850 and specified-byte-order variants of them,
851 to make it clearer that those types have specified widths.
852 .\"
853 .\" From: [email protected] (Guy Harris)
854 .\" Newsgroups: net.bugs.usg
855 .\" Subject: /etc/magic's format isn't well documented
856 .\" Message-ID: <[email protected]>
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 .\"