magic.man 7.3 KB

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