magic.man 22 KB

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