magic.man 24 KB

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