magic.man 23 KB

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