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