magic.man 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. .\" $File: magic.man,v 1.101 2022/10/09 18:51:04 christos Exp $
  2. .Dd October 9, 2022
  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 by a /<width>
  72. option and optionally followed by a set of flags /[bCcftTtWw]*.
  73. The width limits the number of characters to be copied.
  74. Zero means all characters.
  75. The following flags are supported:
  76. .Bl -tag -width B -compact -offset XXXX
  77. .It b
  78. Force binary file test.
  79. .It C
  80. Use upper case insensitive matching: upper case
  81. characters in the magic match both lower and upper case characters in the
  82. target, whereas lower case characters in the magic only match upper case
  83. characters in the target.
  84. .It c
  85. Use lower case insensitive matching: lower case
  86. characters in the magic match both lower and upper case characters in the
  87. target, whereas upper case characters in the magic only match upper case
  88. characters in the target.
  89. To do a complete case insensitive match, specify both
  90. .Dq c
  91. and
  92. .Dq C .
  93. .It f
  94. Require that the matched string is a full word, not a partial word match.
  95. .It T
  96. Trim the string, i.e. leading and trailing whitespace
  97. .It t
  98. Force text file test.
  99. .It W
  100. Compact whitespace in the target, which must
  101. contain at least one whitespace character.
  102. If the magic has
  103. .Dv n
  104. consecutive blanks, the target needs at least
  105. .Dv n
  106. consecutive blanks to match.
  107. .It w
  108. Treat every blank in the magic as an optional blank.
  109. is deleted before the string is printed.
  110. .El
  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 -width B -compact -offset XXXX
  117. .It B
  118. A byte length (default).
  119. .It H
  120. A 2 byte big endian length.
  121. .It h
  122. A 2 byte little endian length.
  123. .It L
  124. A 4 byte big endian length.
  125. .It l
  126. A 4 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. An 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. .It Dv der
  305. Parse the file as a DER Certificate file.
  306. The test field is used as a der type that needs to be matched.
  307. The DER types are:
  308. .Dv eoc ,
  309. .Dv bool ,
  310. .Dv int ,
  311. .Dv bit_str ,
  312. .Dv octet_str ,
  313. .Dv null ,
  314. .Dv obj_id ,
  315. .Dv obj_desc ,
  316. .Dv ext ,
  317. .Dv real ,
  318. .Dv enum ,
  319. .Dv embed ,
  320. .Dv utf8_str ,
  321. .Dv rel_oid ,
  322. .Dv time ,
  323. .Dv res2 ,
  324. .Dv seq ,
  325. .Dv set ,
  326. .Dv num_str ,
  327. .Dv prt_str ,
  328. .Dv t61_str ,
  329. .Dv vid_str ,
  330. .Dv ia5_str ,
  331. .Dv utc_time ,
  332. .Dv gen_time ,
  333. .Dv gr_str ,
  334. .Dv vis_str ,
  335. .Dv gen_str ,
  336. .Dv univ_str ,
  337. .Dv char_str ,
  338. .Dv bmp_str ,
  339. .Dv date ,
  340. .Dv tod ,
  341. .Dv datetime ,
  342. .Dv duration ,
  343. .Dv oid-iri ,
  344. .Dv rel-oid-iri .
  345. These types can be followed by an optional numeric size, which indicates
  346. the field width in bytes.
  347. .It Dv guid
  348. A Globally Unique Identifier, parsed and printed as
  349. XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX.
  350. It's format is a string.
  351. .It Dv offset
  352. This is a quad value indicating the current offset of the file.
  353. It can be used to determine the size of the file or the magic buffer.
  354. For example the magic entries:
  355. .Bd -literal -offset indent
  356. -0 offset x this file is %lld bytes
  357. -0 offset <=100 must be more than 100 \e
  358. bytes and is only %lld
  359. .Ed
  360. .It Dv octal
  361. A string representing an octal number.
  362. .El
  363. .El
  364. .Pp
  365. For compatibility with the Single
  366. .Ux
  367. Standard, the type specifiers
  368. .Dv dC
  369. and
  370. .Dv d1
  371. are equivalent to
  372. .Dv byte ,
  373. the type specifiers
  374. .Dv uC
  375. and
  376. .Dv u1
  377. are equivalent to
  378. .Dv ubyte ,
  379. the type specifiers
  380. .Dv dS
  381. and
  382. .Dv d2
  383. are equivalent to
  384. .Dv short ,
  385. the type specifiers
  386. .Dv uS
  387. and
  388. .Dv u2
  389. are equivalent to
  390. .Dv ushort ,
  391. the type specifiers
  392. .Dv dI ,
  393. .Dv dL ,
  394. and
  395. .Dv d4
  396. are equivalent to
  397. .Dv long ,
  398. the type specifiers
  399. .Dv uI ,
  400. .Dv uL ,
  401. and
  402. .Dv u4
  403. are equivalent to
  404. .Dv ulong ,
  405. the type specifier
  406. .Dv d8
  407. is equivalent to
  408. .Dv quad ,
  409. the type specifier
  410. .Dv u8
  411. is equivalent to
  412. .Dv uquad ,
  413. and the type specifier
  414. .Dv s
  415. is equivalent to
  416. .Dv string .
  417. In addition, the type specifier
  418. .Dv dQ
  419. is equivalent to
  420. .Dv quad
  421. and the type specifier
  422. .Dv uQ
  423. is equivalent to
  424. .Dv uquad .
  425. .Pp
  426. Each top-level magic pattern (see below for an explanation of levels)
  427. is classified as text or binary according to the types used.
  428. Types
  429. .Dq regex
  430. and
  431. .Dq search
  432. are classified as text tests, unless non-printable characters are used
  433. in the pattern.
  434. All other tests are classified as binary.
  435. A top-level
  436. pattern is considered to be a test text when all its patterns are text
  437. patterns; otherwise, it is considered to be a binary pattern.
  438. When
  439. matching a file, binary patterns are tried first; if no match is
  440. found, and the file looks like text, then its encoding is determined
  441. and the text patterns are tried.
  442. .Pp
  443. The numeric types may optionally be followed by
  444. .Dv \*[Am]
  445. and a numeric value,
  446. to specify that the value is to be AND'ed with the
  447. numeric value before any comparisons are done.
  448. Prepending a
  449. .Dv u
  450. to the type indicates that ordered comparisons should be unsigned.
  451. .It Dv test
  452. The value to be compared with the value from the file.
  453. If the type is
  454. numeric, this value
  455. is specified in C form; if it is a string, it is specified as a C string
  456. with the usual escapes permitted (e.g. \en for new-line).
  457. .Pp
  458. Numeric values
  459. may be preceded by a character indicating the operation to be performed.
  460. It may be
  461. .Dv = ,
  462. to specify that the value from the file must equal the specified value,
  463. .Dv \*[Lt] ,
  464. to specify that the value from the file must be less than the specified
  465. value,
  466. .Dv \*[Gt] ,
  467. to specify that the value from the file must be greater than the specified
  468. value,
  469. .Dv \*[Am] ,
  470. to specify that the value from the file must have set all of the bits
  471. that are set in the specified value,
  472. .Dv ^ ,
  473. to specify that the value from the file must have clear any of the bits
  474. that are set in the specified value, or
  475. .Dv ~ ,
  476. the value specified after is negated before tested.
  477. .Dv x ,
  478. to specify that any value will match.
  479. If the character is omitted, it is assumed to be
  480. .Dv = .
  481. Operators
  482. .Dv \*[Am] ,
  483. .Dv ^ ,
  484. and
  485. .Dv ~
  486. don't work with floats and doubles.
  487. The operator
  488. .Dv !\&
  489. specifies that the line matches if the test does
  490. .Em not
  491. succeed.
  492. .Pp
  493. Numeric values are specified in C form; e.g.
  494. .Dv 13
  495. is decimal,
  496. .Dv 013
  497. is octal, and
  498. .Dv 0x13
  499. is hexadecimal.
  500. .Pp
  501. Numeric operations are not performed on date types, instead the numeric
  502. value is interpreted as an offset.
  503. .Pp
  504. For string values, the string from the
  505. file must match the specified string.
  506. The operators
  507. .Dv = ,
  508. .Dv \*[Lt]
  509. and
  510. .Dv \*[Gt]
  511. (but not
  512. .Dv \*[Am] )
  513. can be applied to strings.
  514. The length used for matching is that of the string argument
  515. in the magic file.
  516. This means that a line can match any non-empty string (usually used to
  517. then print the string), with
  518. .Em \*[Gt]\e0
  519. (because all non-empty strings are greater than the empty string).
  520. .Pp
  521. Dates are treated as numerical values in the respective internal
  522. representation.
  523. .Pp
  524. The special test
  525. .Em x
  526. always evaluates to true.
  527. .It Dv message
  528. The message to be printed if the comparison succeeds.
  529. If the string contains a
  530. .Xr printf 3
  531. format specification, the value from the file (with any specified masking
  532. performed) is printed using the message as the format string.
  533. If the string begins with
  534. .Dq \eb ,
  535. the message printed is the remainder of the string with no whitespace
  536. added before it: multiple matches are normally separated by a single
  537. space.
  538. .El
  539. .Pp
  540. An APPLE 4+4 character APPLE creator and type can be specified as:
  541. .Bd -literal -offset indent
  542. !:apple CREATYPE
  543. .Ed
  544. .Pp
  545. A MIME type is given on a separate line, which must be the next
  546. non-blank or comment line after the magic line that identifies the
  547. file type, and has the following format:
  548. .Bd -literal -offset indent
  549. !:mime MIMETYPE
  550. .Ed
  551. .Pp
  552. i.e. the literal string
  553. .Dq !:mime
  554. followed by the MIME type.
  555. .Pp
  556. An optional strength can be supplied on a separate line which refers to
  557. the current magic description using the following format:
  558. .Bd -literal -offset indent
  559. !:strength OP VALUE
  560. .Ed
  561. .Pp
  562. The operand
  563. .Dv OP
  564. can be:
  565. .Dv + ,
  566. .Dv - ,
  567. .Dv * ,
  568. or
  569. .Dv /
  570. and
  571. .Dv VALUE
  572. is a constant between 0 and 255.
  573. This constant is applied using the specified operand
  574. to the currently computed default magic strength.
  575. .Pp
  576. Some file formats contain additional information which is to be printed
  577. along with the file type or need additional tests to determine the true
  578. file type.
  579. These additional tests are introduced by one or more
  580. .Em \*[Gt]
  581. characters preceding the offset.
  582. The number of
  583. .Em \*[Gt]
  584. on the line indicates the level of the test; a line with no
  585. .Em \*[Gt]
  586. at the beginning is considered to be at level 0.
  587. Tests are arranged in a tree-like hierarchy:
  588. if the test on a line at level
  589. .Em n
  590. succeeds, all following tests at level
  591. .Em n+1
  592. are performed, and the messages printed if the tests succeed, until a line
  593. with level
  594. .Em n
  595. (or less) appears.
  596. For more complex files, one can use empty messages to get just the
  597. "if/then" effect, in the following way:
  598. .Bd -literal -offset indent
  599. 0 string MZ
  600. \*[Gt]0x18 leshort \*[Lt]0x40 MS-DOS executable
  601. \*[Gt]0x18 leshort \*[Gt]0x3f extended PC executable (e.g., MS Windows)
  602. .Ed
  603. .Pp
  604. Offsets do not need to be constant, but can also be read from the file
  605. being examined.
  606. If the first character following the last
  607. .Em \*[Gt]
  608. is a
  609. .Em \&(
  610. then the string after the parenthesis is interpreted as an indirect offset.
  611. That means that the number after the parenthesis is used as an offset in
  612. the file.
  613. The value at that offset is read, and is used again as an offset
  614. in the file.
  615. Indirect offsets are of the form:
  616. .Em (( x [[.,][bBcCeEfFgGhHiIlmsSqQ]][+\-][ y ]) .
  617. The value of
  618. .Em x
  619. is used as an offset in the file.
  620. A byte, id3 length, short or long is read at that offset depending on the
  621. .Em [bBcCeEfFgGhHiIlmsSqQ]
  622. type specifier.
  623. The value is treated as signed if
  624. .Dq ,
  625. is specified or unsigned if
  626. .Dq .
  627. is specified.
  628. The capitalized types interpret the number as a big endian
  629. value, whereas the small letter versions interpret the number as a little
  630. endian value;
  631. the
  632. .Em m
  633. type interprets the number as a middle endian (PDP-11) value.
  634. To that number the value of
  635. .Em y
  636. is added and the result is used as an offset in the file.
  637. The default type if one is not specified is long.
  638. The following types are recognized:
  639. .Bl -column -offset indent "Type" "Half/Short" "Little" "Size"
  640. .It Sy Type Sy Mnemonic Sy Endian Sy Size
  641. .It bcBc Byte/Char N/A 1
  642. .It efg Double Little 8
  643. .It EFG Double Big 8
  644. .It hs Half/Short Little 2
  645. .It HS Half/Short Big 2
  646. .It i ID3 Little 4
  647. .It I ID3 Big 4
  648. .It m Middle Middle 4
  649. .It o Octal Textual Variable
  650. .It q Quad Little 8
  651. .It Q Quad Big 8
  652. .El
  653. .Pp
  654. That way variable length structures can be examined:
  655. .Bd -literal -offset indent
  656. # MS Windows executables are also valid MS-DOS executables
  657. 0 string MZ
  658. \*[Gt]0x18 leshort \*[Lt]0x40 MZ executable (MS-DOS)
  659. # skip the whole block below if it is not an extended executable
  660. \*[Gt]0x18 leshort \*[Gt]0x3f
  661. \*[Gt]\*[Gt](0x3c.l) string PE\e0\e0 PE executable (MS-Windows)
  662. \*[Gt]\*[Gt](0x3c.l) string LX\e0\e0 LX executable (OS/2)
  663. .Ed
  664. .Pp
  665. This strategy of examining has a drawback: you must make sure that you
  666. eventually print something, or users may get empty output (such as when
  667. there is neither PE\e0\e0 nor LE\e0\e0 in the above example).
  668. .Pp
  669. If this indirect offset cannot be used directly, simple calculations are
  670. possible: appending
  671. .Em [+-*/%\*[Am]|^]number
  672. inside parentheses allows one to modify
  673. the value read from the file before it is used as an offset:
  674. .Bd -literal -offset indent
  675. # MS Windows executables are also valid MS-DOS executables
  676. 0 string MZ
  677. # sometimes, the value at 0x18 is less that 0x40 but there's still an
  678. # extended executable, simply appended to the file
  679. \*[Gt]0x18 leshort \*[Lt]0x40
  680. \*[Gt]\*[Gt](4.s*512) leshort 0x014c COFF executable (MS-DOS, DJGPP)
  681. \*[Gt]\*[Gt](4.s*512) leshort !0x014c MZ executable (MS-DOS)
  682. .Ed
  683. .Pp
  684. Sometimes you do not know the exact offset as this depends on the length or
  685. position (when indirection was used before) of preceding fields.
  686. You can specify an offset relative to the end of the last up-level
  687. field using
  688. .Sq \*[Am]
  689. as a prefix to the offset:
  690. .Bd -literal -offset indent
  691. 0 string MZ
  692. \*[Gt]0x18 leshort \*[Gt]0x3f
  693. \*[Gt]\*[Gt](0x3c.l) string PE\e0\e0 PE executable (MS-Windows)
  694. # immediately following the PE signature is the CPU type
  695. \*[Gt]\*[Gt]\*[Gt]\*[Am]0 leshort 0x14c for Intel 80386
  696. \*[Gt]\*[Gt]\*[Gt]\*[Am]0 leshort 0x184 for DEC Alpha
  697. .Ed
  698. .Pp
  699. Indirect and relative offsets can be combined:
  700. .Bd -literal -offset indent
  701. 0 string MZ
  702. \*[Gt]0x18 leshort \*[Lt]0x40
  703. \*[Gt]\*[Gt](4.s*512) leshort !0x014c MZ executable (MS-DOS)
  704. # if it's not COFF, go back 512 bytes and add the offset taken
  705. # from byte 2/3, which is yet another way of finding the start
  706. # of the extended executable
  707. \*[Gt]\*[Gt]\*[Gt]\*[Am](2.s-514) string LE LE executable (MS Windows VxD driver)
  708. .Ed
  709. .Pp
  710. Or the other way around:
  711. .Bd -literal -offset indent
  712. 0 string MZ
  713. \*[Gt]0x18 leshort \*[Gt]0x3f
  714. \*[Gt]\*[Gt](0x3c.l) string LE\e0\e0 LE executable (MS-Windows)
  715. # at offset 0x80 (-4, since relative offsets start at the end
  716. # of the up-level match) inside the LE header, we find the absolute
  717. # offset to the code area, where we look for a specific signature
  718. \*[Gt]\*[Gt]\*[Gt](\*[Am]0x7c.l+0x26) string UPX \eb, UPX compressed
  719. .Ed
  720. .Pp
  721. Or even both!
  722. .Bd -literal -offset indent
  723. 0 string MZ
  724. \*[Gt]0x18 leshort \*[Gt]0x3f
  725. \*[Gt]\*[Gt](0x3c.l) string LE\e0\e0 LE executable (MS-Windows)
  726. # at offset 0x58 inside the LE header, we find the relative offset
  727. # to a data area where we look for a specific signature
  728. \*[Gt]\*[Gt]\*[Gt]\*[Am](\*[Am]0x54.l-3) string UNACE \eb, ACE self-extracting archive
  729. .Ed
  730. .Pp
  731. If you have to deal with offset/length pairs in your file, even the
  732. second value in a parenthesized expression can be taken from the file itself,
  733. using another set of parentheses.
  734. Note that this additional indirect offset is always relative to the
  735. start of the main indirect offset.
  736. .Bd -literal -offset indent
  737. 0 string MZ
  738. \*[Gt]0x18 leshort \*[Gt]0x3f
  739. \*[Gt]\*[Gt](0x3c.l) string PE\e0\e0 PE executable (MS-Windows)
  740. # search for the PE section called ".idata"...
  741. \*[Gt]\*[Gt]\*[Gt]\*[Am]0xf4 search/0x140 .idata
  742. # ...and go to the end of it, calculated from start+length;
  743. # these are located 14 and 10 bytes after the section name
  744. \*[Gt]\*[Gt]\*[Gt]\*[Gt](\*[Am]0xe.l+(-4)) string PK\e3\e4 \eb, ZIP self-extracting archive
  745. .Ed
  746. .Pp
  747. If you have a list of known values at a particular continuation level,
  748. and you want to provide a switch-like default case:
  749. .Bd -literal -offset indent
  750. # clear that continuation level match
  751. \*[Gt]18 clear
  752. \*[Gt]18 lelong 1 one
  753. \*[Gt]18 lelong 2 two
  754. \*[Gt]18 default x
  755. # print default match
  756. \*[Gt]\*[Gt]18 lelong x unmatched 0x%x
  757. .Ed
  758. .Sh SEE ALSO
  759. .Xr file __CSECTION__
  760. \- the command that reads this file.
  761. .Sh BUGS
  762. The formats
  763. .Dv long ,
  764. .Dv belong ,
  765. .Dv lelong ,
  766. .Dv melong ,
  767. .Dv short ,
  768. .Dv beshort ,
  769. and
  770. .Dv leshort
  771. do not depend on the length of the C data types
  772. .Dv short
  773. and
  774. .Dv long
  775. on the platform, even though the Single
  776. .Ux
  777. Specification implies that they do. However, as OS X Mountain Lion has
  778. passed the Single
  779. .Ux
  780. Specification validation suite, and supplies a version of
  781. .Xr file __CSECTION__
  782. in which they do not depend on the sizes of the C data types and that is
  783. built for a 64-bit environment in which
  784. .Dv long
  785. is 8 bytes rather than 4 bytes, presumably the validation suite does not
  786. test whether, for example
  787. .Dv long
  788. refers to an item with the same size as the C data type
  789. .Dv long .
  790. There should probably be
  791. .Dv type
  792. names
  793. .Dv int8 ,
  794. .Dv uint8 ,
  795. .Dv int16 ,
  796. .Dv uint16 ,
  797. .Dv int32 ,
  798. .Dv uint32 ,
  799. .Dv int64 ,
  800. and
  801. .Dv uint64 ,
  802. and specified-byte-order variants of them,
  803. to make it clearer that those types have specified widths.
  804. .\"
  805. .\" From: guy@sun.uucp (Guy Harris)
  806. .\" Newsgroups: net.bugs.usg
  807. .\" Subject: /etc/magic's format isn't well documented
  808. .\" Message-ID: <2752@sun.uucp>
  809. .\" Date: 3 Sep 85 08:19:07 GMT
  810. .\" Organization: Sun Microsystems, Inc.
  811. .\" Lines: 136
  812. .\"
  813. .\" Here's a manual page for the format accepted by the "file" made by adding
  814. .\" the changes I posted to the S5R2 version.
  815. .\"
  816. .\" Modified for Ian Darwin's version of the file command.