libmagic.man 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. .\" $File: libmagic.man,v 1.49 2023/07/20 14:32:07 christos Exp $
  2. .\"
  3. .\" Copyright (c) Christos Zoulas 2003, 2018, 2022
  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 June 16, 2023
  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. .Ft const char *
  88. .Fn magic_getpath "const char *magicfile" "int action"
  89. .Sh DESCRIPTION
  90. These functions
  91. operate on the magic database file
  92. which is described
  93. in
  94. .Xr magic __FSECTION__ .
  95. .Pp
  96. The function
  97. .Fn magic_open
  98. creates a magic cookie pointer and returns it.
  99. It returns
  100. .Dv NULL
  101. if there was an error allocating the magic cookie.
  102. The
  103. .Ar flags
  104. argument specifies how the other magic functions should behave:
  105. .Bl -tag -width MAGIC_COMPRESS
  106. .It Dv MAGIC_NONE
  107. No special handling.
  108. .It Dv MAGIC_DEBUG
  109. Print debugging messages to stderr.
  110. .It Dv MAGIC_SYMLINK
  111. If the file queried is a symlink, follow it.
  112. .It Dv MAGIC_COMPRESS
  113. If the file is compressed, unpack it and look at the contents.
  114. .It Dv MAGIC_DEVICES
  115. If the file is a block or character special device, then open the device
  116. and try to look in its contents.
  117. .It Dv MAGIC_MIME_TYPE
  118. Return a MIME type string, instead of a textual description.
  119. .It Dv MAGIC_MIME_ENCODING
  120. Return a MIME encoding, instead of a textual description.
  121. .It Dv MAGIC_MIME
  122. A shorthand for MAGIC_MIME_TYPE | MAGIC_MIME_ENCODING.
  123. .It Dv MAGIC_CONTINUE
  124. Return all matches, not just the first.
  125. .It Dv MAGIC_CHECK
  126. Check the magic database for consistency and print warnings to stderr.
  127. .It Dv MAGIC_PRESERVE_ATIME
  128. On systems that support
  129. .Xr utime 3
  130. or
  131. .Xr utimes 2 ,
  132. attempt to preserve the access time of files analysed.
  133. .It Dv MAGIC_RAW
  134. Don't translate unprintable characters to a \eooo octal representation.
  135. .It Dv MAGIC_ERROR
  136. Treat operating system errors while trying to open files and follow symlinks
  137. as real errors, instead of printing them in the magic buffer.
  138. .It Dv MAGIC_APPLE
  139. Return the Apple creator and type.
  140. .It Dv MAGIC_EXTENSION
  141. Return a slash-separated list of extensions for this file type.
  142. .It Dv MAGIC_COMPRESS_TRANSP
  143. Don't report on compression, only report about the uncompressed data.
  144. .It Dv MAGIC_NO_CHECK_APPTYPE
  145. Don't check for
  146. .Dv EMX
  147. application type (only on EMX).
  148. .It Dv MAGIC_NO_COMPRESS_FORK
  149. Don't allow decompressors that use fork.
  150. .It Dv MAGIC_NO_CHECK_CDF
  151. Don't get extra information on MS Composite Document Files.
  152. .It Dv MAGIC_NO_CHECK_COMPRESS
  153. Don't look inside compressed files.
  154. .It Dv MAGIC_NO_CHECK_ELF
  155. Don't print ELF details.
  156. .It Dv MAGIC_NO_CHECK_ENCODING
  157. Don't check text encodings.
  158. .It Dv MAGIC_NO_CHECK_SOFT
  159. Don't consult magic files.
  160. .It Dv MAGIC_NO_CHECK_TAR
  161. Don't examine tar files.
  162. .It Dv MAGIC_NO_CHECK_TEXT
  163. Don't check for various types of text files.
  164. .It Dv MAGIC_NO_CHECK_TOKENS
  165. Don't look for known tokens inside ascii files.
  166. .It Dv MAGIC_NO_CHECK_JSON
  167. Don't examine JSON files.
  168. .It Dv MAGIC_NO_CHECK_CSV
  169. Don't examine CSV files.
  170. .It Dv MAGIC_NO_CHECK_SIMH
  171. Don't examine SIMH tape files.
  172. .El
  173. .Pp
  174. The
  175. .Fn magic_close
  176. function closes the
  177. .Xr magic __FSECTION__
  178. database and deallocates any resources used.
  179. .Pp
  180. The
  181. .Fn magic_error
  182. function returns a textual explanation of the last error, or
  183. .Dv NULL
  184. if there was no error.
  185. .Pp
  186. The
  187. .Fn magic_errno
  188. function returns the last operating system error number
  189. .Pq Xr errno 2
  190. that was encountered by a system call.
  191. .Pp
  192. The
  193. .Fn magic_file
  194. function returns a textual description of the contents of the
  195. .Ar filename
  196. argument, or
  197. .Dv NULL
  198. if an error occurred.
  199. If the
  200. .Ar filename
  201. is
  202. .Dv NULL ,
  203. then stdin is used.
  204. .Pp
  205. The
  206. .Fn magic_descriptor
  207. function returns a textual description of the contents of the
  208. .Ar fd
  209. argument, or
  210. .Dv NULL
  211. if an error occurred.
  212. .Pp
  213. The
  214. .Fn magic_buffer
  215. function returns a textual description of the contents of the
  216. .Ar buffer
  217. argument with
  218. .Ar length
  219. bytes size.
  220. .Pp
  221. The
  222. .Fn magic_getflags
  223. functions returns a value representing current
  224. .Ar flags
  225. set.
  226. .Pp
  227. The
  228. .Fn magic_setflags
  229. function sets the
  230. .Ar flags
  231. described above.
  232. Note that using both MIME flags together can also
  233. return extra information on the charset.
  234. .Pp
  235. The
  236. .Fn magic_check
  237. function can be used to check the validity of entries in the colon
  238. separated database files passed in as
  239. .Ar filename ,
  240. or
  241. .Dv NULL
  242. for the default database.
  243. It returns 0 on success and \-1 on failure.
  244. .Pp
  245. The
  246. .Fn magic_compile
  247. function can be used to compile the colon
  248. separated list of database files passed in as
  249. .Ar filename ,
  250. or
  251. .Dv NULL
  252. for the default database.
  253. It returns 0 on success and \-1 on failure.
  254. The compiled files created are named from the
  255. .Xr basename 1
  256. of each file argument with
  257. .Dq .mgc
  258. appended to it.
  259. .Pp
  260. The
  261. .Fn magic_list
  262. function dumps all magic entries in a human readable format,
  263. dumping first the entries that are matched against binary files and then the
  264. ones that match text files.
  265. It takes and optional
  266. .Fa filename
  267. argument which is a colon separated list of database files, or
  268. .Dv NULL
  269. for the default database.
  270. .Pp
  271. The
  272. .Fn magic_load
  273. function must be used to load the colon
  274. separated list of database files passed in as
  275. .Ar filename ,
  276. or
  277. .Dv NULL
  278. for the default database file before any magic queries can performed.
  279. .Pp
  280. The default database file is named by the MAGIC environment variable.
  281. If that variable is not set, the default database file name is __MAGIC__.
  282. .Fn magic_load
  283. adds
  284. .Dq .mgc
  285. to the database filename as appropriate.
  286. .Pp
  287. The
  288. .Fn magic_load_buffers
  289. function takes an array of size
  290. .Fa nbuffers
  291. of
  292. .Fa buffers
  293. with a respective size for each in the array of
  294. .Fa sizes
  295. loaded with the contents of the magic databases from the filesystem.
  296. This function can be used in environment where the magic library does
  297. not have direct access to the filesystem, but can access the magic
  298. database via shared memory or other IPC means.
  299. .Pp
  300. The
  301. .Fn magic_getparam
  302. and
  303. .Fn magic_setparam
  304. allow getting and setting various limits related to the magic
  305. library.
  306. .Bl -column "MAGIC_PARAM_ELF_PHNUM_MAX" "size_t" "Default" -offset indent
  307. .It Sy "Parameter" Ta Sy "Type" Ta Sy "Default"
  308. .It Li MAGIC_PARAM_INDIR_MAX Ta size_t Ta 15
  309. .It Li MAGIC_PARAM_NAME_MAX Ta size_t Ta 30
  310. .It Li MAGIC_PARAM_ELF_NOTES_MAX Ta size_t Ta 256
  311. .It Li MAGIC_PARAM_ELF_PHNUM_MAX Ta size_t Ta 128
  312. .It Li MAGIC_PARAM_ELF_SHNUM_MAX Ta size_t Ta 32768
  313. .It Li MAGIC_PARAM_REGEX_MAX Ta size_t Ta 8192
  314. .It Li MAGIC_PARAM_BYTES_MAX Ta size_t Ta 1048576
  315. .El
  316. .Pp
  317. The
  318. .Dv MAGIC_PARAM_INDIR_RECURSION
  319. parameter controls how many levels of recursion will be followed for
  320. indirect magic entries.
  321. .Pp
  322. The
  323. .Dv MAGIC_PARAM_NAME_RECURSION
  324. parameter controls how many levels of recursion will be followed for
  325. for name/use calls.
  326. .Pp
  327. The
  328. .Dv MAGIC_PARAM_NAME_MAX
  329. parameter controls the maximum number of calls for name/use.
  330. .Pp
  331. The
  332. .Dv MAGIC_PARAM_NOTES_MAX
  333. parameter controls how many ELF notes will be processed.
  334. .Pp
  335. The
  336. .Dv MAGIC_PARAM_PHNUM_MAX
  337. parameter controls how many ELF program sections will be processed.
  338. .Pp
  339. The
  340. .Dv MAGIC_PARAM_SHNUM_MAX
  341. parameter controls how many ELF sections will be processed.
  342. .Pp
  343. The
  344. .Fn magic_version
  345. command returns the version number of this library which is compiled into
  346. the shared library using the constant
  347. .Dv MAGIC_VERSION
  348. from
  349. .In magic.h .
  350. This can be used by client programs to verify that the version they compile
  351. against is the same as the version that they run against.
  352. .Pp
  353. The
  354. .Fn magic_getpath
  355. command returns the colon separated list of magic database locations.
  356. If the
  357. .Fa filename
  358. is non-NULL, then it is returned.
  359. Otherwise, if the
  360. .Dv MAGIC
  361. environment variable is defined, then it is returned.
  362. Otherwise, if
  363. .Fa action
  364. is 0 (meaning "file load"), then any user-specific magic database file is included.
  365. Otherwise, only the system default magic database path is included.
  366. .Sh RETURN VALUES
  367. The function
  368. .Fn magic_open
  369. returns a magic cookie on success and
  370. .Dv NULL
  371. on failure setting errno to an appropriate value.
  372. It will set errno to
  373. .Er EINVAL
  374. if an unsupported value for flags was given.
  375. The
  376. .Fn magic_list ,
  377. .Fn magic_load ,
  378. .Fn magic_compile ,
  379. and
  380. .Fn magic_check
  381. functions return 0 on success and \-1 on failure.
  382. The
  383. .Fn magic_buffer ,
  384. .Fn magic_getpath ,
  385. and
  386. .Fn magic_file ,
  387. functions return a string on success and
  388. .Dv NULL
  389. on failure.
  390. The
  391. .Fn magic_error
  392. function returns a textual description of the errors of the above
  393. functions, or
  394. .Dv NULL
  395. if there was no error.
  396. The
  397. .Fn magic_version
  398. always returns the version number of the library.
  399. Finally,
  400. .Fn magic_setflags
  401. returns \-1 on systems that don't support
  402. .Xr utime 3 ,
  403. or
  404. .Xr utimes 2
  405. when
  406. .Dv MAGIC_PRESERVE_ATIME
  407. is set.
  408. .Sh FILES
  409. .Bl -tag -width __MAGIC__.mgc -compact
  410. .It Pa __MAGIC__
  411. The non-compiled default magic database.
  412. .It Pa __MAGIC__.mgc
  413. The compiled default magic database.
  414. .El
  415. .Sh SEE ALSO
  416. .Xr file __CSECTION__ ,
  417. .Xr magic __FSECTION__
  418. .Sh BUGS
  419. The results from
  420. .Fn magic_buffer
  421. and
  422. .Fn magic_file
  423. where the buffer and the file contain the same data
  424. can produce different results, because in the
  425. .Fn magic_file
  426. case, the program can
  427. .Xr lseek 2
  428. and
  429. .Xr stat 2
  430. the file descriptor.
  431. .Sh AUTHORS
  432. .An M\(oans Rullg\(oard
  433. Initial libmagic implementation, and configuration.
  434. .An Christos Zoulas
  435. API cleanup, error code and allocation handling.