1.\" $NetBSD: magic.5,v 1.10 2013/03/23 16:15:57 christos Exp $ 2.\" 3.\" $File: magic.man,v 1.78 2013/01/09 13:04:30 christos Exp $ 4.Dd January 7, 2013 5.Dt MAGIC 5 6.Os 7.\" install as magic.4 on USG, magic.5 on V7, Berkeley and Linux systems. 8.Sh NAME 9.Nm magic 10.Nd file command's magic pattern file 11.Sh DESCRIPTION 12This manual page documents the format of the magic file as 13used by the 14.Xr file 1 15command, version 5.14. 16The 17.Xr file 1 18command identifies the type of a file using, 19among other tests, 20a test for whether the file contains certain 21.Dq "magic patterns" . 22The file 23.Pa /usr/share/misc/magic 24specifies what patterns are to be tested for, what message or 25MIME type to print if a particular pattern is found, 26and additional information to extract from the file. 27.Pp 28Each line of the file specifies a test to be performed. 29A test compares the data starting at a particular offset 30in the file with a byte value, a string or a numeric value. 31If the test succeeds, a message is printed. 32The line consists of the following fields: 33.Bl -tag -width ".Dv message" 34.It Dv offset 35A number specifying the offset, in bytes, into the file of the data 36which is to be tested. 37.It Dv type 38The type of the data to be tested. 39The possible values are: 40.Bl -tag -width ".Dv lestring16" 41.It Dv byte 42A one-byte value. 43.It Dv short 44A two-byte value in this machine's native byte order. 45.It Dv long 46A four-byte value in this machine's native byte order. 47.It Dv quad 48An eight-byte value in this machine's native byte order. 49.It Dv float 50A 32-bit single precision IEEE floating point number in this machine's native byte order. 51.It Dv double 52A 64-bit double precision IEEE floating point number in this machine's native byte order. 53.It Dv string 54A string of bytes. 55The string type specification can be optionally followed 56by /[WwcCtbT]*. 57The 58.Dq W 59flag compacts whitespace in the target, which must 60contain at least one whitespace character. 61If the magic has 62.Dv n 63consecutive blanks, the target needs at least 64.Dv n 65consecutive blanks to match. 66The 67.Dq w 68flag treats every blank in the magic as an optional blank. 69The 70.Dq c 71flag specifies case insensitive matching: lower case 72characters in the magic match both lower and upper case characters in the 73target, whereas upper case characters in the magic only match upper case 74characters in the target. 75The 76.Dq C 77flag specifies case insensitive matching: upper case 78characters in the magic match both lower and upper case characters in the 79target, whereas lower case characters in the magic only match upper case 80characters in the target. 81To do a complete case insensitive match, specify both 82.Dq c 83and 84.Dq C . 85The 86.Dq t 87flag forces the test to be done for text files, while the 88.Dq b 89flag forces the test to be done for binary files. 90The 91.Dq T 92flag causes the string to be trimmed, i.e. leading and trailing whitespace 93is deleted before the string is printed. 94.It Dv pstring 95A Pascal-style string where the first byte/short/int is interpreted as the 96unsigned length. 97The length defaults to byte and can be specified as a modifier. 98The following modifiers are supported: 99.Bl -tag -compact -width B 100.It B 101A byte length (default). 102.It H 103A 2 byte big endian length. 104.It h 105A 2 byte big little length. 106.It L 107A 4 byte big endian length. 108.It l 109A 4 byte big little length. 110.It J 111The length includes itself in its count. 112.El 113The string is not NUL terminated. 114.Dq J 115is used rather than the more 116valuable 117.Dq I 118because this type of length is a feature of the JPEG 119format. 120.It Dv date 121A four-byte value interpreted as a UNIX date. 122.It Dv qdate 123A eight-byte value interpreted as a UNIX date. 124.It Dv ldate 125A four-byte value interpreted as a UNIX-style date, but interpreted as 126local time rather than UTC. 127.It Dv qldate 128An eight-byte value interpreted as a UNIX-style date, but interpreted as 129local time rather than UTC. 130.It Dv qwdate 131An eight-byte value interpreted as a Windows-style date. 132.It Dv beid3 133A 32-bit ID3 length in big-endian byte order. 134.It Dv beshort 135A two-byte value in big-endian byte order. 136.It Dv belong 137A four-byte value in big-endian byte order. 138.It Dv bequad 139An eight-byte value in big-endian byte order. 140.It Dv befloat 141A 32-bit single precision IEEE floating point number in big-endian byte order. 142.It Dv bedouble 143A 64-bit double precision IEEE floating point number in big-endian byte order. 144.It Dv bedate 145A four-byte value in big-endian byte order, 146interpreted as a Unix date. 147.It Dv beqdate 148An eight-byte value in big-endian byte order, 149interpreted as a Unix date. 150.It Dv beldate 151A four-byte value in big-endian byte order, 152interpreted as a UNIX-style date, but interpreted as local time rather 153than UTC. 154.It Dv beqldate 155An eight-byte value in big-endian byte order, 156interpreted as a UNIX-style date, but interpreted as local time rather 157than UTC. 158.It Dv beqwdate 159An eight-byte value in big-endian byte order, 160interpreted as a Windows-style date. 161.It Dv bestring16 162A two-byte unicode (UCS16) string in big-endian byte order. 163.It Dv leid3 164A 32-bit ID3 length in little-endian byte order. 165.It Dv leshort 166A two-byte value in little-endian byte order. 167.It Dv lelong 168A four-byte value in little-endian byte order. 169.It Dv lequad 170An eight-byte value in little-endian byte order. 171.It Dv lefloat 172A 32-bit single precision IEEE floating point number in little-endian byte order. 173.It Dv ledouble 174A 64-bit double precision IEEE floating point number in little-endian byte order. 175.It Dv ledate 176A four-byte value in little-endian byte order, 177interpreted as a UNIX date. 178.It Dv leqdate 179An eight-byte value in little-endian byte order, 180interpreted as a UNIX date. 181.It Dv leldate 182A four-byte value in little-endian byte order, 183interpreted as a UNIX-style date, but interpreted as local time rather 184than UTC. 185.It Dv leqldate 186An eight-byte value in little-endian byte order, 187interpreted as a UNIX-style date, but interpreted as local time rather 188than UTC. 189.It Dv leqwdate 190An eight-byte value in little-endian byte order, 191interpreted as a Windows-style date. 192.It Dv lestring16 193A two-byte unicode (UCS16) string in little-endian byte order. 194.It Dv melong 195A four-byte value in middle-endian (PDP-11) byte order. 196.It Dv medate 197A four-byte value in middle-endian (PDP-11) byte order, 198interpreted as a UNIX date. 199.It Dv meldate 200A four-byte value in middle-endian (PDP-11) byte order, 201interpreted as a UNIX-style date, but interpreted as local time rather 202than UTC. 203.It Dv indirect 204Starting at the given offset, consult the magic database again. 205.It Dv name 206Define a 207.Dq named 208magic instance that can be called from another 209.Dv use 210magic entry, like a subroutine call. 211Named instance direct magic offsets are relative to the offset of the 212previous matched entry, but indirect offsets are relative to the beginning 213of the file as usual. 214Named magic entries always match. 215.It Dv use 216Recursively call the named magic starting from the current offset. 217If the name of the referenced begins with a 218.Dv ^ 219then the endianness of the magic is switched; if the magic mentioned 220.Dv leshort 221for example, 222it is treated as 223.Dv beshort 224and vice versa. 225This is useful to avoid duplicating the rules for different endianness. 226.It Dv regex 227A regular expression match in extended POSIX regular expression syntax 228(like egrep). 229Regular expressions can take exponential time to process, and their 230performance is hard to predict, so their use is discouraged. 231When used in production environments, their performance 232should be carefully checked. 233The type specification can be optionally followed by 234.Dv /[c][s] . 235The 236.Dq c 237flag makes the match case insensitive, while the 238.Dq s 239flag update the offset to the start offset of the match, rather than the end. 240The regular expression is tested against line 241.Dv N + 1 242onwards, where 243.Dv N 244is the given offset. 245Line endings are assumed to be in the machine's native format. 246.Dv ^ 247and 248.Dv $ 249match the beginning and end of individual lines, respectively, 250not beginning and end of file. 251.It Dv search 252A literal string search starting at the given offset. 253The same modifier flags can be used as for string patterns. 254The modifier flags (if any) must be followed by 255.Dv /number 256the range, that is, the number of positions at which the match will be 257attempted, starting from the start offset. 258This is suitable for 259searching larger binary expressions with variable offsets, using 260.Dv \e 261escapes for special characters. 262The offset works as for regex. 263.It Dv default 264This is intended to be used with the test 265.Em x 266(which is always true) and a message that is to be used if there are 267no other matches. 268.El 269.Pp 270For compatibility with the Single 271.Ux 272Standard, the type specifiers 273.Dv dC 274and 275.Dv d1 276are equivalent to 277.Dv byte , 278the type specifiers 279.Dv uC 280and 281.Dv u1 282are equivalent to 283.Dv ubyte , 284the type specifiers 285.Dv dS 286and 287.Dv d2 288are equivalent to 289.Dv short , 290the type specifiers 291.Dv uS 292and 293.Dv u2 294are equivalent to 295.Dv ushort , 296the type specifiers 297.Dv dI , 298.Dv dL , 299and 300.Dv d4 301are equivalent to 302.Dv long , 303the type specifiers 304.Dv uI , 305.Dv uL , 306and 307.Dv u4 308are equivalent to 309.Dv ulong , 310the type specifier 311.Dv d8 312is equivalent to 313.Dv quad , 314the type specifier 315.Dv u8 316is equivalent to 317.Dv uquad , 318and the type specifier 319.Dv s 320is equivalent to 321.Dv string . 322In addition, the type specifier 323.Dv dQ 324is equivalent to 325.Dv quad 326and the type specifier 327.Dv uQ 328is equivalent to 329.Dv uquad . 330.Pp 331Each top-level magic pattern (see below for an explanation of levels) 332is classified as text or binary according to the types used. 333Types 334.Dq regex 335and 336.Dq search 337are classified as text tests, unless non-printable characters are used 338in the pattern. 339All other tests are classified as binary. 340A top-level 341pattern is considered to be a test text when all its patterns are text 342patterns; otherwise, it is considered to be a binary pattern. 343When 344matching a file, binary patterns are tried first; if no match is 345found, and the file looks like text, then its encoding is determined 346and the text patterns are tried. 347.Pp 348The numeric types may optionally be followed by 349.Dv \*[Am] 350and a numeric value, 351to specify that the value is to be AND'ed with the 352numeric value before any comparisons are done. 353Prepending a 354.Dv u 355to the type indicates that ordered comparisons should be unsigned. 356.It Dv test 357The value to be compared with the value from the file. 358If the type is 359numeric, this value 360is specified in C form; if it is a string, it is specified as a C string 361with the usual escapes permitted (e.g. \en for new-line). 362.Pp 363Numeric values 364may be preceded by a character indicating the operation to be performed. 365It may be 366.Dv = , 367to specify that the value from the file must equal the specified value, 368.Dv \*[Lt] , 369to specify that the value from the file must be less than the specified 370value, 371.Dv \*[Gt] , 372to specify that the value from the file must be greater than the specified 373value, 374.Dv \*[Am] , 375to specify that the value from the file must have set all of the bits 376that are set in the specified value, 377.Dv ^ , 378to specify that the value from the file must have clear any of the bits 379that are set in the specified value, or 380.Dv ~ , 381the value specified after is negated before tested. 382.Dv x , 383to specify that any value will match. 384If the character is omitted, it is assumed to be 385.Dv = . 386Operators 387.Dv \*[Am] , 388.Dv ^ , 389and 390.Dv ~ 391don't work with floats and doubles. 392The operator 393.Dv !\& 394specifies that the line matches if the test does 395.Em not 396succeed. 397.Pp 398Numeric values are specified in C form; e.g. 399.Dv 13 400is decimal, 401.Dv 013 402is octal, and 403.Dv 0x13 404is hexadecimal. 405.Pp 406For string values, the string from the 407file must match the specified string. 408The operators 409.Dv = , 410.Dv \*[Lt] 411and 412.Dv \*[Gt] 413(but not 414.Dv \*[Am] ) 415can be applied to strings. 416The length used for matching is that of the string argument 417in the magic file. 418This means that a line can match any non-empty string (usually used to 419then print the string), with 420.Em \*[Gt]\e0 421(because all non-empty strings are greater than the empty string). 422.Pp 423The special test 424.Em x 425always evaluates to true. 426.It Dv message 427The message to be printed if the comparison succeeds. 428If the string contains a 429.Xr printf 3 430format specification, the value from the file (with any specified masking 431performed) is printed using the message as the format string. 432If the string begins with 433.Dq \eb , 434the message printed is the remainder of the string with no whitespace 435added before it: multiple matches are normally separated by a single 436space. 437.El 438.Pp 439An APPLE 4+4 character APPLE creator and type can be specified as: 440.Bd -literal -offset indent 441!:apple CREATYPE 442.Ed 443.Pp 444A MIME type is given on a separate line, which must be the next 445non-blank or comment line after the magic line that identifies the 446file type, and has the following format: 447.Bd -literal -offset indent 448!:mime MIMETYPE 449.Ed 450.Pp 451i.e. the literal string 452.Dq !:mime 453followed by the MIME type. 454.Pp 455An optional strength can be supplied on a separate line which refers to 456the current magic description using the following format: 457.Bd -literal -offset indent 458!:strength OP VALUE 459.Ed 460.Pp 461The operand 462.Dv OP 463can be: 464.Dv + , 465.Dv - , 466.Dv * , 467or 468.Dv / 469and 470.Dv VALUE 471is a constant between 0 and 255. 472This constant is applied using the specified operand 473to the currently computed default magic strength. 474.Pp 475Some file formats contain additional information which is to be printed 476along with the file type or need additional tests to determine the true 477file type. 478These additional tests are introduced by one or more 479.Em \*[Gt] 480characters preceding the offset. 481The number of 482.Em \*[Gt] 483on the line indicates the level of the test; a line with no 484.Em \*[Gt] 485at the beginning is considered to be at level 0. 486Tests are arranged in a tree-like hierarchy: 487if the test on a line at level 488.Em n 489succeeds, all following tests at level 490.Em n+1 491are performed, and the messages printed if the tests succeed, until a line 492with level 493.Em n 494(or less) appears. 495For more complex files, one can use empty messages to get just the 496"if/then" effect, in the following way: 497.Bd -literal -offset indent 4980 string MZ 499\*[Gt]0x18 leshort \*[Lt]0x40 MS-DOS executable 500\*[Gt]0x18 leshort \*[Gt]0x3f extended PC executable (e.g., MS Windows) 501.Ed 502.Pp 503Offsets do not need to be constant, but can also be read from the file 504being examined. 505If the first character following the last 506.Em \*[Gt] 507is a 508.Em \&( 509then the string after the parenthesis is interpreted as an indirect offset. 510That means that the number after the parenthesis is used as an offset in 511the file. 512The value at that offset is read, and is used again as an offset 513in the file. 514Indirect offsets are of the form: 515.Em (( x [.[bislBISL]][+\-][ y ]) . 516The value of 517.Em x 518is used as an offset in the file. 519A byte, id3 length, short or long is read at that offset depending on the 520.Em [bislBISLm] 521type specifier. 522The capitalized types interpret the number as a big endian 523value, whereas the small letter versions interpret the number as a little 524endian value; 525the 526.Em m 527type interprets the number as a middle endian (PDP-11) value. 528To that number the value of 529.Em y 530is added and the result is used as an offset in the file. 531The default type if one is not specified is long. 532.Pp 533That way variable length structures can be examined: 534.Bd -literal -offset indent 535# MS Windows executables are also valid MS-DOS executables 5360 string MZ 537\*[Gt]0x18 leshort \*[Lt]0x40 MZ executable (MS-DOS) 538# skip the whole block below if it is not an extended executable 539\*[Gt]0x18 leshort \*[Gt]0x3f 540\*[Gt]\*[Gt](0x3c.l) string PE\e0\e0 PE executable (MS-Windows) 541\*[Gt]\*[Gt](0x3c.l) string LX\e0\e0 LX executable (OS/2) 542.Ed 543.Pp 544This strategy of examining has a drawback: You must make sure that 545you eventually print something, or users may get empty output (like, when 546there is neither PE\e0\e0 nor LE\e0\e0 in the above example) 547.Pp 548If this indirect offset cannot be used directly, simple calculations are 549possible: appending 550.Em [+-*/%\*[Am]|^]number 551inside parentheses allows one to modify 552the value read from the file before it is used as an offset: 553.Bd -literal -offset indent 554# MS Windows executables are also valid MS-DOS executables 5550 string MZ 556# sometimes, the value at 0x18 is less that 0x40 but there's still an 557# extended executable, simply appended to the file 558\*[Gt]0x18 leshort \*[Lt]0x40 559\*[Gt]\*[Gt](4.s*512) leshort 0x014c COFF executable (MS-DOS, DJGPP) 560\*[Gt]\*[Gt](4.s*512) leshort !0x014c MZ executable (MS-DOS) 561.Ed 562.Pp 563Sometimes you do not know the exact offset as this depends on the length or 564position (when indirection was used before) of preceding fields. 565You can specify an offset relative to the end of the last up-level 566field using 567.Sq \*[Am] 568as a prefix to the offset: 569.Bd -literal -offset indent 5700 string MZ 571\*[Gt]0x18 leshort \*[Gt]0x3f 572\*[Gt]\*[Gt](0x3c.l) string PE\e0\e0 PE executable (MS-Windows) 573# immediately following the PE signature is the CPU type 574\*[Gt]\*[Gt]\*[Gt]\*[Am]0 leshort 0x14c for Intel 80386 575\*[Gt]\*[Gt]\*[Gt]\*[Am]0 leshort 0x184 for DEC Alpha 576.Ed 577.Pp 578Indirect and relative offsets can be combined: 579.Bd -literal -offset indent 5800 string MZ 581\*[Gt]0x18 leshort \*[Lt]0x40 582\*[Gt]\*[Gt](4.s*512) leshort !0x014c MZ executable (MS-DOS) 583# if it's not COFF, go back 512 bytes and add the offset taken 584# from byte 2/3, which is yet another way of finding the start 585# of the extended executable 586\*[Gt]\*[Gt]\*[Gt]\*[Am](2.s-514) string LE LE executable (MS Windows VxD driver) 587.Ed 588.Pp 589Or the other way around: 590.Bd -literal -offset indent 5910 string MZ 592\*[Gt]0x18 leshort \*[Gt]0x3f 593\*[Gt]\*[Gt](0x3c.l) string LE\e0\e0 LE executable (MS-Windows) 594# at offset 0x80 (-4, since relative offsets start at the end 595# of the up-level match) inside the LE header, we find the absolute 596# offset to the code area, where we look for a specific signature 597\*[Gt]\*[Gt]\*[Gt](\*[Am]0x7c.l+0x26) string UPX \eb, UPX compressed 598.Ed 599.Pp 600Or even both! 601.Bd -literal -offset indent 6020 string MZ 603\*[Gt]0x18 leshort \*[Gt]0x3f 604\*[Gt]\*[Gt](0x3c.l) string LE\e0\e0 LE executable (MS-Windows) 605# at offset 0x58 inside the LE header, we find the relative offset 606# to a data area where we look for a specific signature 607\*[Gt]\*[Gt]\*[Gt]\*[Am](\*[Am]0x54.l-3) string UNACE \eb, ACE self-extracting archive 608.Ed 609.Pp 610Finally, if you have to deal with offset/length pairs in your file, even the 611second value in a parenthesized expression can be taken from the file itself, 612using another set of parentheses. 613Note that this additional indirect offset is always relative to the 614start of the main indirect offset. 615.Bd -literal -offset indent 6160 string MZ 617\*[Gt]0x18 leshort \*[Gt]0x3f 618\*[Gt]\*[Gt](0x3c.l) string PE\e0\e0 PE executable (MS-Windows) 619# search for the PE section called ".idata"... 620\*[Gt]\*[Gt]\*[Gt]\*[Am]0xf4 search/0x140 .idata 621# ...and go to the end of it, calculated from start+length; 622# these are located 14 and 10 bytes after the section name 623\*[Gt]\*[Gt]\*[Gt]\*[Gt](\*[Am]0xe.l+(-4)) string PK\e3\e4 \eb, ZIP self-extracting archive 624.Ed 625.Sh SEE ALSO 626.Xr file 1 627\- the command that reads this file. 628.Sh BUGS 629The formats 630.Dv long , 631.Dv belong , 632.Dv lelong , 633.Dv melong , 634.Dv short , 635.Dv beshort , 636and 637.Dv leshort 638do not depend on the length of the C data types 639.Dv short 640and 641.Dv long 642on the platform, even though the Single 643.Ux 644Specification implies that they do. However, as OS X Mountain Lion has 645passed the Single 646.Ux 647Specification validation suite, and supplies a version of 648.Xr file 1 649in which they do not depend on the sizes of the C data types and that is 650built for a 64-bit environment in which 651.Dv long 652is 8 bytes rather than 4 bytes, presumably the validation suite does not 653test whether, for example 654.Dv long 655refers to an item with the same size as the C data type 656.Dv long . 657There should probably be 658.Dv type 659names 660.Dv int8 , 661.Dv uint8 , 662.Dv int16 , 663.Dv uint16 , 664.Dv int32 , 665.Dv uint32 , 666.Dv int64 , 667and 668.Dv uint64 , 669and specified-byte-order variants of them, 670to make it clearer that those types have specified widths. 671.\" 672.\" From: guy@sun.uucp (Guy Harris) 673.\" Newsgroups: net.bugs.usg 674.\" Subject: /etc/magic's format isn't well documented 675.\" Message-ID: <2752@sun.uucp> 676.\" Date: 3 Sep 85 08:19:07 GMT 677.\" Organization: Sun Microsystems, Inc. 678.\" Lines: 136 679.\" 680.\" Here's a manual page for the format accepted by the "file" made by adding 681.\" the changes I posted to the S5R2 version. 682.\" 683.\" Modified for Ian Darwin's version of the file command. 684