libmagic.man 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. .\" $File: libmagic.man,v 1.44 2018/09/09 20:33:28 christos Exp $
  2. .\"
  3. .\" Copyright (c) Christos Zoulas 2003, 2018.
  4. .\" All Rights Reserved.
  5. .\"
  6. .\" Redistribution and use in source and binary forms, with or without
  7. .\" modification, are permitted provided that the following conditions
  8. .\" are met:
  9. .\" 1. Redistributions of source code must retain the above copyright
  10. .\" notice immediately at the beginning of the file, without modification,
  11. .\" this list of conditions, and the following disclaimer.
  12. .\" 2. Redistributions in binary form must reproduce the above copyright
  13. .\" notice, this list of conditions and the following disclaimer in the
  14. .\" documentation and/or other materials provided with the distribution.
  15. .\"
  16. .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  17. .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. .\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
  20. .\" ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  22. .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26. .\" SUCH DAMAGE.
  27. .\"
  28. .Dd August 18, 2018
  29. .Dt LIBMAGIC 3
  30. .Os
  31. .Sh NAME
  32. .Nm magic_open ,
  33. .Nm magic_close ,
  34. .Nm magic_error ,
  35. .Nm magic_errno ,
  36. .Nm magic_descriptor ,
  37. .Nm magic_buffer ,
  38. .Nm magic_getflags ,
  39. .Nm magic_setflags ,
  40. .Nm magic_check ,
  41. .Nm magic_compile ,
  42. .Nm magic_list ,
  43. .Nm magic_load ,
  44. .Nm magic_load_buffers ,
  45. .Nm magic_setparam ,
  46. .Nm magic_getparam ,
  47. .Nm magic_version
  48. .Nd Magic number recognition library
  49. .Sh LIBRARY
  50. .Lb libmagic
  51. .Sh SYNOPSIS
  52. .In magic.h
  53. .Ft magic_t
  54. .Fn magic_open "int flags"
  55. .Ft void
  56. .Fn magic_close "magic_t cookie"
  57. .Ft const char *
  58. .Fn magic_error "magic_t cookie"
  59. .Ft int
  60. .Fn magic_errno "magic_t cookie"
  61. .Ft const char *
  62. .Fn magic_descriptor "magic_t cookie" "int fd"
  63. .Ft const char *
  64. .Fn magic_file "magic_t cookie" "const char *filename"
  65. .Ft const char *
  66. .Fn magic_buffer "magic_t cookie" "const void *buffer" "size_t length"
  67. .Ft int
  68. .Fn magic_getflags "magic_t cookie"
  69. .Ft int
  70. .Fn magic_setflags "magic_t cookie" "int flags"
  71. .Ft int
  72. .Fn magic_check "magic_t cookie" "const char *filename"
  73. .Ft int
  74. .Fn magic_compile "magic_t cookie" "const char *filename"
  75. .Ft int
  76. .Fn magic_list "magic_t cookie" "const char *filename"
  77. .Ft int
  78. .Fn magic_load "magic_t cookie" "const char *filename"
  79. .Ft int
  80. .Fn magic_load_buffers "magic_t cookie" "void **buffers" "size_t *sizes" "size_t nbuffers"
  81. .Ft int
  82. .Fn magic_getparam "magic_t cookie" "int param" "void *value"
  83. .Ft int
  84. .Fn magic_setparam "magic_t cookie" "int param" "const void *value"
  85. .Ft int
  86. .Fn magic_version "void"
  87. .Sh DESCRIPTION
  88. These functions
  89. operate on the magic database file
  90. which is described
  91. in
  92. .Xr magic __FSECTION__ .
  93. .Pp
  94. The function
  95. .Fn magic_open
  96. creates a magic cookie pointer and returns it.
  97. It returns
  98. .Dv NULL
  99. if there was an error allocating the magic cookie.
  100. The
  101. .Ar flags
  102. argument specifies how the other magic functions should behave:
  103. .Bl -tag -width MAGIC_COMPRESS
  104. .It Dv MAGIC_NONE
  105. No special handling.
  106. .It Dv MAGIC_DEBUG
  107. Print debugging messages to stderr.
  108. .It Dv MAGIC_SYMLINK
  109. If the file queried is a symlink, follow it.
  110. .It Dv MAGIC_COMPRESS
  111. If the file is compressed, unpack it and look at the contents.
  112. .It Dv MAGIC_DEVICES
  113. If the file is a block or character special device, then open the device
  114. and try to look in its contents.
  115. .It Dv MAGIC_MIME_TYPE
  116. Return a MIME type string, instead of a textual description.
  117. .It Dv MAGIC_MIME_ENCODING
  118. Return a MIME encoding, instead of a textual description.
  119. .It Dv MAGIC_MIME
  120. A shorthand for MAGIC_MIME_TYPE | MAGIC_MIME_ENCODING.
  121. .It Dv MAGIC_CONTINUE
  122. Return all matches, not just the first.
  123. .It Dv MAGIC_CHECK
  124. Check the magic database for consistency and print warnings to stderr.
  125. .It Dv MAGIC_PRESERVE_ATIME
  126. On systems that support
  127. .Xr utime 3
  128. or
  129. .Xr utimes 2 ,
  130. attempt to preserve the access time of files analysed.
  131. .It Dv MAGIC_RAW
  132. Don't translate unprintable characters to a \eooo octal representation.
  133. .It Dv MAGIC_ERROR
  134. Treat operating system errors while trying to open files and follow symlinks
  135. as real errors, instead of printing them in the magic buffer.
  136. .It Dv MAGIC_APPLE
  137. Return the Apple creator and type.
  138. .It Dv MAGIC_EXTENSION
  139. Return a slash-separated list of extensions for this file type.
  140. .It Dv MAGIC_COMPRESS_TRANSP
  141. Don't report on compression, only report about the uncompressed data.
  142. .It Dv MAGIC_NO_CHECK_APPTYPE
  143. Don't check for
  144. .Dv EMX
  145. application type (only on EMX).
  146. .It Dv MAGIC_NO_CHECK_CDF
  147. Don't get extra information on MS Composite Document Files.
  148. .It Dv MAGIC_NO_CHECK_COMPRESS
  149. Don't look inside compressed files.
  150. .It Dv MAGIC_NO_CHECK_ELF
  151. Don't print ELF details.
  152. .It Dv MAGIC_NO_CHECK_ENCODING
  153. Don't check text encodings.
  154. .It Dv MAGIC_NO_CHECK_SOFT
  155. Don't consult magic files.
  156. .It Dv MAGIC_NO_CHECK_TAR
  157. Don't examine tar files.
  158. .It Dv MAGIC_NO_CHECK_TEXT
  159. Don't check for various types of text files.
  160. .It Dv MAGIC_NO_CHECK_TOKENS
  161. Don't look for known tokens inside ascii files.
  162. .It Dv MAGIC_NO_CHECK_JSON
  163. Don't example JSON files.
  164. .El
  165. .Pp
  166. The
  167. .Fn magic_close
  168. function closes the
  169. .Xr magic __FSECTION__
  170. database and deallocates any resources used.
  171. .Pp
  172. The
  173. .Fn magic_error
  174. function returns a textual explanation of the last error, or
  175. .Dv NULL
  176. if there was no error.
  177. .Pp
  178. The
  179. .Fn magic_errno
  180. function returns the last operating system error number
  181. .Pq Xr errno 2
  182. that was encountered by a system call.
  183. .Pp
  184. The
  185. .Fn magic_file
  186. function returns a textual description of the contents of the
  187. .Ar filename
  188. argument, or
  189. .Dv NULL
  190. if an error occurred.
  191. If the
  192. .Ar filename
  193. is
  194. .Dv NULL ,
  195. then stdin is used.
  196. .Pp
  197. The
  198. .Fn magic_descriptor
  199. function returns a textual description of the contents of the
  200. .Ar fd
  201. argument, or
  202. .Dv NULL
  203. if an error occurred.
  204. .Pp
  205. The
  206. .Fn magic_buffer
  207. function returns a textual description of the contents of the
  208. .Ar buffer
  209. argument with
  210. .Ar length
  211. bytes size.
  212. .Pp
  213. The
  214. .Fn magic_getflags
  215. functions returns a value representing current
  216. .Ar flags
  217. set.
  218. .Pp
  219. The
  220. .Fn magic_setflags
  221. function sets the
  222. .Ar flags
  223. described above.
  224. Note that using both MIME flags together can also
  225. return extra information on the charset.
  226. .Pp
  227. The
  228. .Fn magic_check
  229. function can be used to check the validity of entries in the colon
  230. separated database files passed in as
  231. .Ar filename ,
  232. or
  233. .Dv NULL
  234. for the default database.
  235. It returns 0 on success and \-1 on failure.
  236. .Pp
  237. The
  238. .Fn magic_compile
  239. function can be used to compile the colon
  240. separated list of database files passed in as
  241. .Ar filename ,
  242. or
  243. .Dv NULL
  244. for the default database.
  245. It returns 0 on success and \-1 on failure.
  246. The compiled files created are named from the
  247. .Xr basename 1
  248. of each file argument with
  249. .Dq .mgc
  250. appended to it.
  251. .Pp
  252. The
  253. .Fn magic_list
  254. function dumps all magic entries in a human readable format,
  255. dumping first the entries that are matched against binary files and then the
  256. ones that match text files.
  257. It takes and optional
  258. .Fa filename
  259. argument which is a colon separated list of database files, or
  260. .Dv NULL
  261. for the default database.
  262. .Pp
  263. The
  264. .Fn magic_load
  265. function must be used to load the colon
  266. separated list of database files passed in as
  267. .Ar filename ,
  268. or
  269. .Dv NULL
  270. for the default database file before any magic queries can performed.
  271. .Pp
  272. The default database file is named by the MAGIC environment variable.
  273. If that variable is not set, the default database file name is __MAGIC__.
  274. .Fn magic_load
  275. adds
  276. .Dq .mgc
  277. to the database filename as appropriate.
  278. .Pp
  279. The
  280. .Fn magic_load_buffers
  281. function takes an array of size
  282. .Fa nbuffers
  283. of
  284. .Fa buffers
  285. with a respective size for each in the array of
  286. .Fa sizes
  287. loaded with the contents of the magic databases from the filesystem.
  288. This function can be used in environment where the magic library does
  289. not have direct access to the filesystem, but can access the magic
  290. database via shared memory or other IPC means.
  291. .Pp
  292. The
  293. .Fn magic_getparam
  294. and
  295. .Fn magic_setparam
  296. allow getting and setting various limits related to the magic
  297. library.
  298. .Bl -column "MAGIC_PARAM_ELF_PHNUM_MAX" "size_t" "Default" -offset indent
  299. .It Sy "Parameter" Ta Sy "Type" Ta Sy "Default"
  300. .It Li MAGIC_PARAM_INDIR_MAX Ta size_t Ta 15
  301. .It Li MAGIC_PARAM_NAME_MAX Ta size_t Ta 30
  302. .It Li MAGIC_PARAM_ELF_NOTES_MAX Ta size_t Ta 256
  303. .It Li MAGIC_PARAM_ELF_PHNUM_MAX Ta size_t Ta 128
  304. .It Li MAGIC_PARAM_ELF_SHNUM_MAX Ta size_t Ta 32768
  305. .It Li MAGIC_PARAM_REGEX_MAX Ta size_t Ta 8192
  306. .It Li MAGIC_PARAM_BYTES_MAX Ta size_t Ta 1048576
  307. .El
  308. .Pp
  309. The
  310. .Dv MAGIC_PARAM_INDIR_RECURSION
  311. parameter controls how many levels of recursion will be followed for
  312. indirect magic entries.
  313. .Pp
  314. The
  315. .Dv MAGIC_PARAM_NAME_RECURSION
  316. parameter controls how many levels of recursion will be followed for
  317. for name/use calls.
  318. .Pp
  319. The
  320. .Dv MAGIC_PARAM_NAME_MAX
  321. parameter controls the maximum number of calls for name/use.
  322. .Pp
  323. The
  324. .Dv MAGIC_PARAM_NOTES_MAX
  325. parameter controls how many ELF notes will be processed.
  326. .Pp
  327. The
  328. .Dv MAGIC_PARAM_PHNUM_MAX
  329. parameter controls how many ELF program sections will be processed.
  330. .Pp
  331. The
  332. .Dv MAGIC_PARAM_SHNUM_MAX
  333. parameter controls how many ELF sections will be processed.
  334. .Pp
  335. The
  336. .Fn magic_version
  337. command returns the version number of this library which is compiled into
  338. the shared library using the constant
  339. .Dv MAGIC_VERSION
  340. from
  341. .In magic.h .
  342. This can be used by client programs to verify that the version they compile
  343. against is the same as the version that they run against.
  344. .Sh RETURN VALUES
  345. The function
  346. .Fn magic_open
  347. returns a magic cookie on success and
  348. .Dv NULL
  349. on failure setting errno to an appropriate value.
  350. It will set errno to
  351. .Er EINVAL
  352. if an unsupported value for flags was given.
  353. The
  354. .Fn magic_list ,
  355. .Fn magic_load ,
  356. .Fn magic_compile ,
  357. and
  358. .Fn magic_check
  359. functions return 0 on success and \-1 on failure.
  360. The
  361. .Fn magic_buffer ,
  362. .Fn magic_getpath ,
  363. and
  364. .Fn magic_file ,
  365. functions return a string on success and
  366. .Dv NULL
  367. on failure.
  368. The
  369. .Fn magic_error
  370. function returns a textual description of the errors of the above
  371. functions, or
  372. .Dv NULL
  373. if there was no error.
  374. The
  375. .Fn magic_version
  376. always returns the version number of the library.
  377. Finally,
  378. .Fn magic_setflags
  379. returns \-1 on systems that don't support
  380. .Xr utime 3 ,
  381. or
  382. .Xr utimes 2
  383. when
  384. .Dv MAGIC_PRESERVE_ATIME
  385. is set.
  386. .Sh FILES
  387. .Bl -tag -width __MAGIC__.mgc -compact
  388. .It Pa __MAGIC__
  389. The non-compiled default magic database.
  390. .It Pa __MAGIC__.mgc
  391. The compiled default magic database.
  392. .El
  393. .Sh SEE ALSO
  394. .Xr file __CSECTION__ ,
  395. .Xr magic __FSECTION__
  396. .Sh BUGS
  397. The results from
  398. .Fn magic_buffer
  399. and
  400. .Fn magic_file
  401. where the buffer and the file contain the same data
  402. can produce different results, because in the
  403. .Fn magic_file
  404. case, the program can
  405. .Xr lseek 2
  406. and
  407. .Xr stat 2
  408. the file descriptor.
  409. .Sh AUTHORS
  410. .An M\(oans Rullg\(oard
  411. Initial libmagic implementation, and configuration.
  412. .An Christos Zoulas
  413. API cleanup, error code and allocation handling.