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