xref: /netbsd-src/external/bsd/file/dist/doc/libmagic.3 (revision af4088eb76884cc4644b7a61d0625d9177b5094b)
1.\"	$NetBSD: libmagic.3,v 1.17 2017/10/22 11:17:39 abhinav Exp $
2.\"
3.\" $File: libmagic.man,v 1.41 2017/05/23 21:54:07 christos Exp $
4.\"
5.\" Copyright (c) Christos Zoulas 2003.
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 May 23, 2017
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.El
166.Pp
167The
168.Fn magic_close
169function closes the
170.Xr magic 5
171database and deallocates any resources used.
172.Pp
173The
174.Fn magic_error
175function returns a textual explanation of the last error, or
176.Dv NULL
177if there was no error.
178.Pp
179The
180.Fn magic_errno
181function returns the last operating system error number
182.Pq Xr errno 2
183that was encountered by a system call.
184.Pp
185The
186.Fn magic_file
187function returns a textual description of the contents of the
188.Ar filename
189argument, or
190.Dv NULL
191if an error occurred.
192If the
193.Ar filename
194is
195.Dv NULL ,
196then stdin is used.
197.Pp
198The
199.Fn magic_descriptor
200function returns a textual description of the contents of the
201.Ar fd
202argument, or
203.Dv NULL
204if an error occurred.
205.Pp
206The
207.Fn magic_buffer
208function returns a textual description of the contents of the
209.Ar buffer
210argument with
211.Ar length
212bytes size.
213.Pp
214The
215.Fn magic_getflags
216functions returns a value representing current
217.Ar flags
218set.
219.Pp
220The
221.Fn magic_setflags
222function sets the
223.Ar flags
224described above.
225Note that using both MIME flags together can also
226return extra information on the charset.
227.Pp
228The
229.Fn magic_check
230function can be used to check the validity of entries in the colon
231separated database files passed in as
232.Ar filename ,
233or
234.Dv NULL
235for the default database.
236It returns 0 on success and \-1 on failure.
237.Pp
238The
239.Fn magic_compile
240function can be used to compile the colon
241separated list of database files passed in as
242.Ar filename ,
243or
244.Dv NULL
245for the default database.
246It returns 0 on success and \-1 on failure.
247The compiled files created are named from the
248.Xr basename 1
249of each file argument with
250.Dq .mgc
251appended to it.
252.Pp
253The
254.Fn magic_list
255function dumps all magic entries in a human readable format,
256dumping first the entries that are matched against binary files and then the
257ones that match text files.
258It takes and optional
259.Fa filename
260argument which is a colon separated list of database files, or
261.Dv NULL
262for the default database.
263.Pp
264The
265.Fn magic_load
266function must be used to load the colon
267separated list of database files passed in as
268.Ar filename ,
269or
270.Dv NULL
271for the default database file before any magic queries can performed.
272.Pp
273The default database file is named by the MAGIC environment variable.
274If that variable is not set, the default database file name is
275.Pa /usr/share/misc/magic .
276.Fn magic_load
277adds
278.Dq .mgc
279to the database filename as appropriate.
280.Pp
281The
282.Fn magic_load_buffers
283function takes an array of size
284.Fa nbuffers
285of
286.Fa buffers
287with a respective size for each in the array of
288.Fa sizes
289loaded with the contents of the magic databases from the filesystem.
290This function can be used in environment where the magic library does
291not have direct access to the filesystem, but can access the magic
292database via shared memory or other IPC means.
293.Pp
294The
295.Fn magic_getparam
296and
297.Fn magic_setparam
298allow getting and setting various limits related to the magic
299library.
300.Bl -column "MAGIC_PARAM_ELF_PHNUM_MAX" "size_t" "Default" -offset indent
301.It Sy "Parameter" Ta Sy "Type" Ta Sy "Default"
302.It Li MAGIC_PARAM_INDIR_MAX Ta size_t Ta 15
303.It Li MAGIC_PARAM_NAME_MAX Ta size_t Ta 30
304.It Li MAGIC_PARAM_ELF_NOTES_MAX Ta size_t Ta 256
305.It Li MAGIC_PARAM_ELF_PHNUM_MAX Ta size_t Ta 128
306.It Li MAGIC_PARAM_ELF_SHNUM_MAX Ta size_t Ta 32768
307.It Li MAGIC_PARAM_REGEX_MAX Ta size_t Ta 8192
308.It Li MAGIC_PARAM_BYTES_MAX Ta size_t Ta 1048576
309.El
310.Pp
311The
312.Dv MAGIC_PARAM_INDIR_RECURSION
313parameter controls how many levels of recursion will be followed for
314indirect magic entries.
315.Pp
316The
317.Dv MAGIC_PARAM_NAME_RECURSION
318parameter controls how many levels of recursion will be followed for
319for name/use calls.
320.Pp
321The
322.Dv MAGIC_PARAM_NAME_MAX
323parameter controls the maximum number of calls for name/use.
324.Pp
325The
326.Dv MAGIC_PARAM_NOTES_MAX
327parameter controls how many ELF notes will be processed.
328.Pp
329The
330.Dv MAGIC_PARAM_PHNUM_MAX
331parameter controls how many ELF program sections will be processed.
332.Pp
333The
334.Dv MAGIC_PARAM_SHNUM_MAX
335parameter controls how many ELF sections will be processed.
336.Pp
337The
338.Fn magic_version
339command returns the version number of this library which is compiled into
340the shared library using the constant
341.Dv MAGIC_VERSION
342from
343.In magic.h .
344This can be used by client programs to verify that the version they compile
345against is the same as the version that they run against.
346.Sh RETURN VALUES
347The function
348.Fn magic_open
349returns a magic cookie on success and
350.Dv NULL
351on failure setting errno to an appropriate value.
352It will set errno to
353.Er EINVAL
354if an unsupported value for flags was given.
355The
356.Fn magic_list ,
357.Fn magic_load ,
358.Fn magic_compile ,
359and
360.Fn magic_check
361functions return 0 on success and \-1 on failure.
362The
363.Fn magic_buffer ,
364.Fn magic_getpath ,
365and
366.Fn magic_file ,
367functions return a string on success and
368.Dv NULL
369on failure.
370The
371.Fn magic_error
372function returns a textual description of the errors of the above
373functions, or
374.Dv NULL
375if there was no error.
376The
377.Fn magic_version
378always returns the version number of the library.
379Finally,
380.Fn magic_setflags
381returns \-1 on systems that don't support
382.Xr utime 3 ,
383or
384.Xr utimes 2
385when
386.Dv MAGIC_PRESERVE_ATIME
387is set.
388.Sh FILES
389.Bl -tag -width /usr/share/misc/magic.mgc -compact
390.It Pa /usr/share/misc/magic
391The non-compiled default magic database.
392.It Pa /usr/share/misc/magic.mgc
393The compiled default magic database.
394.El
395.Sh SEE ALSO
396.Xr file 1 ,
397.Xr magic 5
398.Sh AUTHORS
399.An M\(oans Rullg\(oard
400Initial libmagic implementation, and configuration.
401.An Christos Zoulas
402API cleanup, error code and allocation handling.
403