magic.man 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. .TH MAGIC __FSECTION__ "Public Domain"
  2. .\" install as magic.4 on USG, magic.5 on V7 or Berkeley systems.
  3. .SH NAME
  4. magic \- file command's magic number file
  5. .SH DESCRIPTION
  6. This manual page documents the format of the magic file as
  7. used by the
  8. .BR file (__CSECTION__)
  9. command, version __VERSION__.
  10. The
  11. .BR file
  12. command identifies the type of a file using,
  13. among other tests,
  14. a test for whether the file begins with a certain
  15. .IR "magic number" .
  16. The file
  17. .I __MAGIC__
  18. specifies what magic numbers are to be tested for,
  19. what message to print if a particular magic number is found,
  20. and additional information to extract from the file.
  21. .PP
  22. Each line of the file specifies a test to be performed.
  23. A test compares the data starting at a particular offset
  24. in the file with a 1-byte, 2-byte, or 4-byte numeric value or
  25. a string.
  26. If the test succeeds, a message is printed.
  27. The line consists of the following fields:
  28. .IP offset \w'message'u+2n
  29. A number specifying the offset, in bytes, into the file of the data
  30. which is to be tested.
  31. .IP type
  32. The type of the data to be tested.
  33. The possible values are:
  34. .RS
  35. .IP byte \w'message'u+2n
  36. A one-byte value.
  37. .IP short
  38. A two-byte value (on most systems) in this machine's native byte order.
  39. .IP long
  40. A four-byte value (on most systems) in this machine's native byte order.
  41. .IP string
  42. A string of bytes.
  43. The string type specification can be optionally followed
  44. by /[Bbc]*.
  45. The ``B'' flag compacts whitespace in the target, which must
  46. contain at least one whitespace character.
  47. If the magic has
  48. .I n
  49. consecutive blanks, the target needs at least
  50. .I n
  51. consecutive blanks to match.
  52. The ``b'' flag treats every blank in the target as an optional blank.
  53. Finally the ``c'' flag, specifies case insensitive matching: lowercase
  54. characters in the magic match both lower and upper case characters in the
  55. targer, whereas upper case characters in the magic, only much uppercase
  56. characters in the target.
  57. .IP date
  58. A four-byte value interpreted as a UNIX date.
  59. .IP ldate
  60. A four-byte value interpreted as a UNIX-style date, but interpreted as
  61. local time rather than UTC.
  62. .IP beshort
  63. A two-byte value (on most systems) in big-endian byte order.
  64. .IP belong
  65. A four-byte value (on most systems) in big-endian byte order.
  66. .IP bedate
  67. A four-byte value (on most systems) in big-endian byte order,
  68. interpreted as a Unix date.
  69. .IP leshort
  70. A two-byte value (on most systems) in little-endian byte order.
  71. .IP lelong
  72. A four-byte value (on most systems) in little-endian byte order.
  73. .IP ledate
  74. A four-byte value (on most systems) in little-endian byte order,
  75. interpreted as a UNIX date.
  76. .IP leldate
  77. A four-byte value (on most systems) in little-endian byte order,
  78. interpreted as a UNIX-style date, but interpreted as local time rather
  79. than UTC.
  80. .RE
  81. .PP
  82. The numeric types may optionally be followed by
  83. .B &
  84. and a numeric value,
  85. to specify that the value is to be AND'ed with the
  86. numeric value before any comparisons are done.
  87. Prepending a
  88. .B u
  89. to the type indicates that ordered comparisons should be unsigned.
  90. .IP test
  91. The value to be compared with the value from the file.
  92. If the type is
  93. numeric, this value
  94. is specified in C form; if it is a string, it is specified as a C string
  95. with the usual escapes permitted (e.g. \en for new-line).
  96. .IP
  97. Numeric values
  98. may be preceded by a character indicating the operation to be performed.
  99. It may be
  100. .BR = ,
  101. to specify that the value from the file must equal the specified value,
  102. .BR < ,
  103. to specify that the value from the file must be less than the specified
  104. value,
  105. .BR > ,
  106. to specify that the value from the file must be greater than the specified
  107. value,
  108. .BR & ,
  109. to specify that the value from the file must have set all of the bits
  110. that are set in the specified value,
  111. .BR ^ ,
  112. to specify that the value from the file must have clear any of the bits
  113. that are set in the specified value, or
  114. .BR x ,
  115. to specify that any value will match.
  116. If the character is omitted, it is assumed to be
  117. .BR = .
  118. .IP
  119. Numeric values are specified in C form; e.g.
  120. .B 13
  121. is decimal,
  122. .B 013
  123. is octal, and
  124. .B 0x13
  125. is hexadecimal.
  126. .IP
  127. For string values, the byte string from the
  128. file must match the specified byte string.
  129. The operators
  130. .BR = ,
  131. .B <
  132. and
  133. .B >
  134. (but not
  135. .BR & )
  136. can be applied to strings.
  137. The length used for matching is that of the string argument
  138. in the magic file.
  139. This means that a line can match any string, and
  140. then presumably print that string, by doing
  141. .B >\e0
  142. (because all strings are greater than the null string).
  143. .IP message
  144. The message to be printed if the comparison succeeds. If the string
  145. contains a
  146. .BR printf (3)
  147. format specification, the value from the file (with any specified masking
  148. performed) is printed using the message as the format string.
  149. .PP
  150. Some file formats contain additional information which is to be printed
  151. along with the file type.
  152. A line which begins with the character
  153. .B >
  154. indicates additional tests and messages to be printed.
  155. The number of
  156. .B >
  157. on the line indicates the level of the test; a line with no
  158. .B >
  159. at the beginning is considered to be at level 0.
  160. Each line at level
  161. .IB n \(pl1
  162. is under the control of the line at level
  163. .IB n
  164. most closely preceding it in the magic file.
  165. If the test on a line at level
  166. .I n
  167. succeeds, the tests specified in all the subsequent lines at level
  168. .IB n \(pl1
  169. are performed, and the messages printed if the tests succeed.
  170. The next line at level
  171. .I n
  172. terminates this.
  173. If the first character following the last
  174. .B >
  175. is a
  176. .B (
  177. then the string after the parenthesis is interpreted as an indirect offset.
  178. That means that the number after the parenthesis is used as an offset in
  179. the file.
  180. The value at that offset is read, and is used again as an offset
  181. in the file.
  182. Indirect offsets are of the form:
  183. .BI (( x [.[bslBSL]][+\-][ y ]).
  184. The value of
  185. .I x
  186. is used as an offset in the file. A byte, short or long is read at that offset
  187. depending on the
  188. .B [bslBSL]
  189. type specifier.
  190. The capitalized types interpret the number as a big endian
  191. value, whereas the small letter versions interpret the number as a little
  192. endian value.
  193. To that number the value of
  194. .I y
  195. is added and the result is used as an offset in the file.
  196. The default type if one is not specified is long.
  197. .PP
  198. Sometimes you do not know the exact offset as this depends on the length of
  199. preceding fields.
  200. You can specify an offset relative to the end of the
  201. last uplevel field (of course this may only be done for sublevel tests, i.e.
  202. test beginning with
  203. .B >
  204. ).
  205. Such a relative offset is specified using
  206. .B &
  207. as a prefix to the offset.
  208. .SH BUGS
  209. The formats
  210. .IR long ,
  211. .IR belong ,
  212. .IR lelong ,
  213. .IR short ,
  214. .IR beshort ,
  215. .IR leshort ,
  216. .IR date ,
  217. .IR bedate ,
  218. and
  219. .I ledate
  220. are system-dependent; perhaps they should be specified as a number
  221. of bytes (2B, 4B, etc),
  222. since the files being recognized typically come from
  223. a system on which the lengths are invariant.
  224. .PP
  225. There is (currently) no support for specified-endian data to be used in
  226. indirect offsets.
  227. .SH SEE ALSO
  228. .BR file (__CSECTION__)
  229. \- the command that reads this file.
  230. .\"
  231. .\" From: guy@sun.uucp (Guy Harris)
  232. .\" Newsgroups: net.bugs.usg
  233. .\" Subject: /etc/magic's format isn't well documented
  234. .\" Message-ID: <2752@sun.uucp>
  235. .\" Date: 3 Sep 85 08:19:07 GMT
  236. .\" Organization: Sun Microsystems, Inc.
  237. .\" Lines: 136
  238. .\"
  239. .\" Here's a manual page for the format accepted by the "file" made by adding
  240. .\" the changes I posted to the S5R2 version.
  241. .\"
  242. .\" Modified for Ian Darwin's version of the file command.
  243. .\" @(#)$Id: magic.man,v 1.27 2003/09/12 19:43:30 christos Exp $