magic.man 26 KB

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