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