magic.man 23 KB

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