xref: /freebsd/contrib/file/doc/libmagic.man (revision c293113327da098a3ce92f3bc75ee69d5f2392eb)
1*c2931133SXin LI.\" $File: libmagic.man,v 1.33 2014/11/28 02:46:39 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*c2931133SXin LI.Dd November 27, 2014
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 ,
38b6cee71dSXin LI.Nm magic_setflags ,
39b6cee71dSXin LI.Nm magic_check ,
40b6cee71dSXin LI.Nm magic_compile ,
41b6cee71dSXin LI.Nm magic_list ,
42b6cee71dSXin LI.Nm magic_load ,
43*c2931133SXin LI.Nm magic_load_buffers ,
44*c2931133SXin LI.Nm magic_setparam ,
45*c2931133SXin LI.Nm magic_getparam ,
46b6cee71dSXin LI.Nm magic_version
47b6cee71dSXin LI.Nd Magic number recognition library
48b6cee71dSXin LI.Sh LIBRARY
49b6cee71dSXin LI.Lb libmagic
50b6cee71dSXin LI.Sh SYNOPSIS
51b6cee71dSXin LI.In magic.h
52b6cee71dSXin LI.Ft magic_t
53b6cee71dSXin LI.Fn magic_open "int flags"
54b6cee71dSXin LI.Ft void
55b6cee71dSXin LI.Fn magic_close "magic_t cookie"
56b6cee71dSXin LI.Ft const char *
57b6cee71dSXin LI.Fn magic_error "magic_t cookie"
58b6cee71dSXin LI.Ft int
59b6cee71dSXin LI.Fn magic_errno "magic_t cookie"
60b6cee71dSXin LI.Ft const char *
61b6cee71dSXin LI.Fn magic_descriptor "magic_t cookie" "int fd"
62b6cee71dSXin LI.Ft const char *
63b6cee71dSXin LI.Fn magic_file "magic_t cookie" "const char *filename"
64b6cee71dSXin LI.Ft const char *
65b6cee71dSXin LI.Fn magic_buffer "magic_t cookie" "const void *buffer" "size_t length"
66b6cee71dSXin LI.Ft int
67b6cee71dSXin LI.Fn magic_setflags "magic_t cookie" "int flags"
68b6cee71dSXin LI.Ft int
69b6cee71dSXin LI.Fn magic_check "magic_t cookie" "const char *filename"
70b6cee71dSXin LI.Ft int
71b6cee71dSXin LI.Fn magic_compile "magic_t cookie" "const char *filename"
72b6cee71dSXin LI.Ft int
73b6cee71dSXin LI.Fn magic_list "magic_t cookie" "const char *filename"
74b6cee71dSXin LI.Ft int
75b6cee71dSXin LI.Fn magic_load "magic_t cookie" "const char *filename"
76b6cee71dSXin LI.Ft int
77*c2931133SXin LI.Fn magic_load_buffers "magic_t cookie" "void **buffers" "size_t *sizes" "size_t nbuffers"
78*c2931133SXin LI.Ft int
79*c2931133SXin LI.Fn magic_getparam "magic_t cookie" "int param" "void *value"
80*c2931133SXin LI.Ft int
81*c2931133SXin LI.Fn magic_setparam "magic_t cookie" "int param" "const void *value"
82*c2931133SXin LI.Ft int
83b6cee71dSXin LI.Fn magic_version "void"
84b6cee71dSXin LI.Sh DESCRIPTION
85b6cee71dSXin LIThese functions
86b6cee71dSXin LIoperate on the magic database file
87b6cee71dSXin LIwhich is described
88b6cee71dSXin LIin
89b6cee71dSXin LI.Xr magic __FSECTION__ .
90b6cee71dSXin LI.Pp
91b6cee71dSXin LIThe function
92b6cee71dSXin LI.Fn magic_open
93b6cee71dSXin LIcreates a magic cookie pointer and returns it.
94b6cee71dSXin LIIt returns
95b6cee71dSXin LI.Dv NULL
96b6cee71dSXin LIif there was an error allocating the magic cookie.
97b6cee71dSXin LIThe
98b6cee71dSXin LI.Ar flags
99b6cee71dSXin LIargument specifies how the other magic functions should behave:
100b6cee71dSXin LI.Bl -tag -width MAGIC_COMPRESS
101b6cee71dSXin LI.It Dv MAGIC_NONE
102b6cee71dSXin LINo special handling.
103b6cee71dSXin LI.It Dv MAGIC_DEBUG
104b6cee71dSXin LIPrint debugging messages to stderr.
105b6cee71dSXin LI.It Dv MAGIC_SYMLINK
106b6cee71dSXin LIIf the file queried is a symlink, follow it.
107b6cee71dSXin LI.It Dv MAGIC_COMPRESS
108b6cee71dSXin LIIf the file is compressed, unpack it and look at the contents.
109b6cee71dSXin LI.It Dv MAGIC_DEVICES
110b6cee71dSXin LIIf the file is a block or character special device, then open the device
111b6cee71dSXin LIand try to look in its contents.
112b6cee71dSXin LI.It Dv MAGIC_MIME_TYPE
113b6cee71dSXin LIReturn a MIME type string, instead of a textual description.
114b6cee71dSXin LI.It Dv MAGIC_MIME_ENCODING
115b6cee71dSXin LIReturn a MIME encoding, instead of a textual description.
116b6cee71dSXin LI.It Dv MAGIC_MIME
117b6cee71dSXin LIA shorthand for MAGIC_MIME_TYPE | MAGIC_MIME_ENCODING.
118b6cee71dSXin LI.It Dv MAGIC_CONTINUE
119b6cee71dSXin LIReturn all matches, not just the first.
120b6cee71dSXin LI.It Dv MAGIC_CHECK
121b6cee71dSXin LICheck the magic database for consistency and print warnings to stderr.
122b6cee71dSXin LI.It Dv MAGIC_PRESERVE_ATIME
123b6cee71dSXin LIOn systems that support
124b6cee71dSXin LI.Xr utime 3
125b6cee71dSXin LIor
126b6cee71dSXin LI.Xr utimes 2 ,
127b6cee71dSXin LIattempt to preserve the access time of files analysed.
128b6cee71dSXin LI.It Dv MAGIC_RAW
129b6cee71dSXin LIDon't translate unprintable characters to a \eooo octal representation.
130b6cee71dSXin LI.It Dv MAGIC_ERROR
131b6cee71dSXin LITreat operating system errors while trying to open files and follow symlinks
132b6cee71dSXin LIas real errors, instead of printing them in the magic buffer.
133b6cee71dSXin LI.It Dv MAGIC_APPLE
134b6cee71dSXin LIReturn the Apple creator and type.
135b6cee71dSXin LI.It Dv MAGIC_NO_CHECK_APPTYPE
136b6cee71dSXin LIDon't check for
137b6cee71dSXin LI.Dv EMX
138b6cee71dSXin LIapplication type (only on EMX).
139b6cee71dSXin LI.It Dv MAGIC_NO_CHECK_CDF
140b6cee71dSXin LIDon't get extra information on MS Composite Document Files.
141b6cee71dSXin LI.It Dv MAGIC_NO_CHECK_COMPRESS
142b6cee71dSXin LIDon't look inside compressed files.
143b6cee71dSXin LI.It Dv MAGIC_NO_CHECK_ELF
144b6cee71dSXin LIDon't print ELF details.
145b6cee71dSXin LI.It Dv MAGIC_NO_CHECK_ENCODING
146b6cee71dSXin LIDon't check text encodings.
147b6cee71dSXin LI.It Dv MAGIC_NO_CHECK_SOFT
148b6cee71dSXin LIDon't consult magic files.
149b6cee71dSXin LI.It Dv MAGIC_NO_CHECK_TAR
150b6cee71dSXin LIDon't examine tar files.
151b6cee71dSXin LI.It Dv MAGIC_NO_CHECK_TEXT
152b6cee71dSXin LIDon't check for various types of text files.
153b6cee71dSXin LI.It Dv MAGIC_NO_CHECK_TOKENS
154b6cee71dSXin LIDon't look for known tokens inside ascii files.
155b6cee71dSXin LI.El
156b6cee71dSXin LI.Pp
157b6cee71dSXin LIThe
158b6cee71dSXin LI.Fn magic_close
159b6cee71dSXin LIfunction closes the
160b6cee71dSXin LI.Xr magic __FSECTION__
161b6cee71dSXin LIdatabase and deallocates any resources used.
162b6cee71dSXin LI.Pp
163b6cee71dSXin LIThe
164b6cee71dSXin LI.Fn magic_error
165b6cee71dSXin LIfunction returns a textual explanation of the last error, or
166b6cee71dSXin LI.Dv NULL
167b6cee71dSXin LIif there was no error.
168b6cee71dSXin LI.Pp
169b6cee71dSXin LIThe
170b6cee71dSXin LI.Fn magic_errno
171b6cee71dSXin LIfunction returns the last operating system error number
172b6cee71dSXin LI.Pq Xr errno 2
173b6cee71dSXin LIthat was encountered by a system call.
174b6cee71dSXin LI.Pp
175b6cee71dSXin LIThe
176b6cee71dSXin LI.Fn magic_file
177b6cee71dSXin LIfunction returns a textual description of the contents of the
178b6cee71dSXin LI.Ar filename
179b6cee71dSXin LIargument, or
180b6cee71dSXin LI.Dv NULL
181b6cee71dSXin LIif an error occurred.
182b6cee71dSXin LIIf the
183b6cee71dSXin LI.Ar filename
184b6cee71dSXin LIis
185b6cee71dSXin LI.Dv NULL ,
186b6cee71dSXin LIthen stdin is used.
187b6cee71dSXin LI.Pp
188b6cee71dSXin LIThe
189b6cee71dSXin LI.Fn magic_descriptor
190b6cee71dSXin LIfunction returns a textual description of the contents of the
191b6cee71dSXin LI.Ar fd
192b6cee71dSXin LIargument, or
193b6cee71dSXin LI.Dv NULL
194b6cee71dSXin LIif an error occurred.
195b6cee71dSXin LI.Pp
196b6cee71dSXin LIThe
197b6cee71dSXin LI.Fn magic_buffer
198b6cee71dSXin LIfunction returns a textual description of the contents of the
199b6cee71dSXin LI.Ar buffer
200b6cee71dSXin LIargument with
201b6cee71dSXin LI.Ar length
202b6cee71dSXin LIbytes size.
203b6cee71dSXin LI.Pp
204b6cee71dSXin LIThe
205b6cee71dSXin LI.Fn magic_setflags
206b6cee71dSXin LIfunction sets the
207b6cee71dSXin LI.Ar flags
208b6cee71dSXin LIdescribed above.
209b6cee71dSXin LINote that using both MIME flags together can also
210b6cee71dSXin LIreturn extra information on the charset.
211b6cee71dSXin LI.Pp
212b6cee71dSXin LIThe
213b6cee71dSXin LI.Fn magic_check
214b6cee71dSXin LIfunction can be used to check the validity of entries in the colon
215b6cee71dSXin LIseparated database files passed in as
216b6cee71dSXin LI.Ar filename ,
217b6cee71dSXin LIor
218b6cee71dSXin LI.Dv NULL
219b6cee71dSXin LIfor the default database.
220b6cee71dSXin LIIt returns 0 on success and \-1 on failure.
221b6cee71dSXin LI.Pp
222b6cee71dSXin LIThe
223b6cee71dSXin LI.Fn magic_compile
224b6cee71dSXin LIfunction can be used to compile the the colon
225b6cee71dSXin LIseparated list of database files passed in as
226b6cee71dSXin LI.Ar filename ,
227b6cee71dSXin LIor
228b6cee71dSXin LI.Dv NULL
229b6cee71dSXin LIfor the default database.
230b6cee71dSXin LIIt returns 0 on success and \-1 on failure.
231b6cee71dSXin LIThe compiled files created are named from the
232b6cee71dSXin LI.Xr basename 1
233b6cee71dSXin LIof each file argument with
234b6cee71dSXin LI.Dq .mgc
235b6cee71dSXin LIappended to it.
236b6cee71dSXin LI.Pp
237b6cee71dSXin LIThe
238b6cee71dSXin LI.Fn magic_list
239b6cee71dSXin LIfunction dumps all magic entries in a human readable format,
240b6cee71dSXin LIdumping first the entries that are matched against binary files and then the
241b6cee71dSXin LIones that match text files.
242b6cee71dSXin LIIt takes and optional
243b6cee71dSXin LI.Fa filename
244b6cee71dSXin LIargument which is a colon separated list of database files, or
245b6cee71dSXin LI.Dv NULL
246b6cee71dSXin LIfor the default database.
247b6cee71dSXin LI.Pp
248b6cee71dSXin LIThe
249b6cee71dSXin LI.Fn magic_load
250b6cee71dSXin LIfunction must be used to load the the colon
251b6cee71dSXin LIseparated list of database files passed in as
252b6cee71dSXin LI.Ar filename ,
253b6cee71dSXin LIor
254b6cee71dSXin LI.Dv NULL
255b6cee71dSXin LIfor the default database file before any magic queries can performed.
256b6cee71dSXin LI.Pp
257b6cee71dSXin LIThe default database file is named by the MAGIC environment variable.
258b6cee71dSXin LIIf that variable is not set, the default database file name is __MAGIC__.
259b6cee71dSXin LI.Fn magic_load
260b6cee71dSXin LIadds
261b6cee71dSXin LI.Dq .mgc
262b6cee71dSXin LIto the database filename as appropriate.
263b6cee71dSXin LI.Pp
264b6cee71dSXin LIThe
265*c2931133SXin LI.Fn magic_load_buffers
266*c2931133SXin LIfunction takes an array of size
267*c2931133SXin LI.Fa nbuffers
268*c2931133SXin LIof
269*c2931133SXin LI.Fa buffers
270*c2931133SXin LIwith a respective size for each in the array of
271*c2931133SXin LI.Fa sizes
272*c2931133SXin LIloaded with the contents of the magic databases from the filesystem.
273*c2931133SXin LIThis function can be used in environment where the magic library does
274*c2931133SXin LInot have direct access to the filesystem, but can access the magic
275*c2931133SXin LIdatabase via shared memory or other IPC means.
276*c2931133SXin LI.Pp
277*c2931133SXin LIThe
278*c2931133SXin LI.Fn magic_getparam
279*c2931133SXin LIand
280*c2931133SXin LI.Fn magic_setparam
281*c2931133SXin LIallow getting and setting various limits related to the the magic
282*c2931133SXin LIlibrary.
283*c2931133SXin LI.Bl -column "MAGIC_PARAM_ELF_PHNUM_MAX" "size_t" "Default" -offset indent
284*c2931133SXin LI.It Sy "Parameter" Ta Sy "Type" Ta Sy "Default"
285*c2931133SXin LI.It Li MAGIC_PARAM_INDIR_MAX Ta size_t Ta 15
286*c2931133SXin LI.It Li MAGIC_PARAM_NAME_MAX Ta size_t Ta 30
287*c2931133SXin LI.It Li MAGIC_PARAM_ELF_PHNUM_MAX Ta size_t Ta 128
288*c2931133SXin LI.It Li MAGIC_PARAM_ELF_SHNUM_MAX Ta size_t Ta 32768
289*c2931133SXin LI.El
290*c2931133SXin LI.Pp
291*c2931133SXin LIThe
292*c2931133SXin LI.Dv MAGIC_PARAM_INDIR_RECURSION
293*c2931133SXin LIparameter controls how many levels of recursion will be followed for
294*c2931133SXin LIindirect magic entries.
295*c2931133SXin LI.Pp
296*c2931133SXin LIThe
297*c2931133SXin LI.Dv MAGIC_PARAM_NAME_RECURSION
298*c2931133SXin LIparameter controls how many levels of recursion will be followed for
299*c2931133SXin LIfor name/use calls.
300*c2931133SXin LI.Pp
301*c2931133SXin LIThe
302*c2931133SXin LI.Dv MAGIC_PARAM_NAME_MAX
303*c2931133SXin LIparameter controls the maximum number of calls for name/use.
304*c2931133SXin LI.Pp
305*c2931133SXin LIThe
306*c2931133SXin LI.Dv MAGIC_PARAM_PHNUM_MAX
307*c2931133SXin LIparameter controls how many elf program sections will be processed.
308*c2931133SXin LI.Pp
309*c2931133SXin LIThe
310*c2931133SXin LI.Dv MAGIC_PARAM_SHNUM_MAX
311*c2931133SXin LIparameter controls how many elf sections will be processed.
312*c2931133SXin LI.Pp
313*c2931133SXin LIThe
314b6cee71dSXin LI.Fn magic_version
315b6cee71dSXin LIcommand returns the version number of this library which is compiled into
316b6cee71dSXin LIthe shared library using the constant
317b6cee71dSXin LI.Dv MAGIC_VERSION
318b6cee71dSXin LIfrom
319b6cee71dSXin LI.In magic.h .
320b6cee71dSXin LIThis can be used by client programs to verify that the version they compile
321b6cee71dSXin LIagainst is the same as the version that they run against.
322b6cee71dSXin LI.Sh RETURN VALUES
323b6cee71dSXin LIThe function
324b6cee71dSXin LI.Fn magic_open
325b6cee71dSXin LIreturns a magic cookie on success and
326b6cee71dSXin LI.Dv NULL
327b6cee71dSXin LIon failure setting errno to an appropriate value.
328b6cee71dSXin LIIt will set errno to
329b6cee71dSXin LI.Er EINVAL
330b6cee71dSXin LIif an unsupported value for flags was given.
331b6cee71dSXin LIThe
332b6cee71dSXin LI.Fn magic_list ,
333b6cee71dSXin LI.Fn magic_load ,
334b6cee71dSXin LI.Fn magic_compile ,
335b6cee71dSXin LIand
336b6cee71dSXin LI.Fn magic_check
337b6cee71dSXin LIfunctions return 0 on success and \-1 on failure.
338b6cee71dSXin LIThe
339b6cee71dSXin LI.Fn magic_buffer ,
340b6cee71dSXin LI.Fn magic_getpath ,
341b6cee71dSXin LIand
342b6cee71dSXin LI.Fn magic_file ,
343b6cee71dSXin LIfunctions return a string on success and
344b6cee71dSXin LI.Dv NULL
345b6cee71dSXin LIon failure.
346b6cee71dSXin LIThe
347b6cee71dSXin LI.Fn magic_error
348b6cee71dSXin LIfunction returns a textual description of the errors of the above
349b6cee71dSXin LIfunctions, or
350b6cee71dSXin LI.Dv NULL
351b6cee71dSXin LIif there was no error.
352b6cee71dSXin LIThe
353b6cee71dSXin LI.Fn magic_version
354b6cee71dSXin LIalways returns the version number of the library.
355b6cee71dSXin LIFinally,
356b6cee71dSXin LI.Fn magic_setflags
357b6cee71dSXin LIreturns \-1 on systems that don't support
358b6cee71dSXin LI.Xr utime 3 ,
359b6cee71dSXin LIor
360b6cee71dSXin LI.Xr utimes 2
361b6cee71dSXin LIwhen
362b6cee71dSXin LI.Dv MAGIC_PRESERVE_ATIME
363b6cee71dSXin LIis set.
364b6cee71dSXin LI.Sh FILES
365b6cee71dSXin LI.Bl -tag -width __MAGIC__.mgc -compact
366b6cee71dSXin LI.It Pa __MAGIC__
367b6cee71dSXin LIThe non-compiled default magic database.
368b6cee71dSXin LI.It Pa __MAGIC__.mgc
369b6cee71dSXin LIThe compiled default magic database.
370b6cee71dSXin LI.El
371b6cee71dSXin LI.Sh SEE ALSO
372b6cee71dSXin LI.Xr file __CSECTION__ ,
373b6cee71dSXin LI.Xr magic __FSECTION__
374b6cee71dSXin LI.Sh AUTHORS
375b6cee71dSXin LI.An M\(oans Rullg\(oard
376b6cee71dSXin LIInitial libmagic implementation, and configuration.
377b6cee71dSXin LI.An Christos Zoulas
378b6cee71dSXin LIAPI cleanup, error code and allocation handling.
379