xref: /freebsd/contrib/file/doc/libmagic.man (revision 052c5ec4d0bb39b02a20ea7970614249afcbf158)
1.\" $File: libmagic.man,v 1.45 2019/06/08 22:16:24 christos Exp $
2.\"
3.\" Copyright (c) Christos Zoulas 2003, 2018.
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 8, 2019
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.Sh DESCRIPTION
88These functions
89operate on the magic database file
90which is described
91in
92.Xr magic __FSECTION__ .
93.Pp
94The function
95.Fn magic_open
96creates a magic cookie pointer and returns it.
97It returns
98.Dv NULL
99if there was an error allocating the magic cookie.
100The
101.Ar flags
102argument specifies how the other magic functions should behave:
103.Bl -tag -width MAGIC_COMPRESS
104.It Dv MAGIC_NONE
105No special handling.
106.It Dv MAGIC_DEBUG
107Print debugging messages to stderr.
108.It Dv MAGIC_SYMLINK
109If the file queried is a symlink, follow it.
110.It Dv MAGIC_COMPRESS
111If the file is compressed, unpack it and look at the contents.
112.It Dv MAGIC_DEVICES
113If the file is a block or character special device, then open the device
114and try to look in its contents.
115.It Dv MAGIC_MIME_TYPE
116Return a MIME type string, instead of a textual description.
117.It Dv MAGIC_MIME_ENCODING
118Return a MIME encoding, instead of a textual description.
119.It Dv MAGIC_MIME
120A shorthand for MAGIC_MIME_TYPE | MAGIC_MIME_ENCODING.
121.It Dv MAGIC_CONTINUE
122Return all matches, not just the first.
123.It Dv MAGIC_CHECK
124Check the magic database for consistency and print warnings to stderr.
125.It Dv MAGIC_PRESERVE_ATIME
126On systems that support
127.Xr utime 3
128or
129.Xr utimes 2 ,
130attempt to preserve the access time of files analysed.
131.It Dv MAGIC_RAW
132Don't translate unprintable characters to a \eooo octal representation.
133.It Dv MAGIC_ERROR
134Treat operating system errors while trying to open files and follow symlinks
135as real errors, instead of printing them in the magic buffer.
136.It Dv MAGIC_APPLE
137Return the Apple creator and type.
138.It Dv MAGIC_EXTENSION
139Return a slash-separated list of extensions for this file type.
140.It Dv MAGIC_COMPRESS_TRANSP
141Don't report on compression, only report about the uncompressed data.
142.It Dv MAGIC_NO_CHECK_APPTYPE
143Don't check for
144.Dv EMX
145application type (only on EMX).
146.It Dv MAGIC_NO_CHECK_CDF
147Don't get extra information on MS Composite Document Files.
148.It Dv MAGIC_NO_CHECK_COMPRESS
149Don't look inside compressed files.
150.It Dv MAGIC_NO_CHECK_ELF
151Don't print ELF details.
152.It Dv MAGIC_NO_CHECK_ENCODING
153Don't check text encodings.
154.It Dv MAGIC_NO_CHECK_SOFT
155Don't consult magic files.
156.It Dv MAGIC_NO_CHECK_TAR
157Don't examine tar files.
158.It Dv MAGIC_NO_CHECK_TEXT
159Don't check for various types of text files.
160.It Dv MAGIC_NO_CHECK_TOKENS
161Don't look for known tokens inside ascii files.
162.It Dv MAGIC_NO_CHECK_JSON
163Don't examine JSON files.
164.It Dv MAGIC_NO_CHECK_CSV
165Don't examine CSV files.
166.El
167.Pp
168The
169.Fn magic_close
170function closes the
171.Xr magic __FSECTION__
172database and deallocates any resources used.
173.Pp
174The
175.Fn magic_error
176function returns a textual explanation of the last error, or
177.Dv NULL
178if there was no error.
179.Pp
180The
181.Fn magic_errno
182function returns the last operating system error number
183.Pq Xr errno 2
184that was encountered by a system call.
185.Pp
186The
187.Fn magic_file
188function returns a textual description of the contents of the
189.Ar filename
190argument, or
191.Dv NULL
192if an error occurred.
193If the
194.Ar filename
195is
196.Dv NULL ,
197then stdin is used.
198.Pp
199The
200.Fn magic_descriptor
201function returns a textual description of the contents of the
202.Ar fd
203argument, or
204.Dv NULL
205if an error occurred.
206.Pp
207The
208.Fn magic_buffer
209function returns a textual description of the contents of the
210.Ar buffer
211argument with
212.Ar length
213bytes size.
214.Pp
215The
216.Fn magic_getflags
217functions returns a value representing current
218.Ar flags
219set.
220.Pp
221The
222.Fn magic_setflags
223function sets the
224.Ar flags
225described above.
226Note that using both MIME flags together can also
227return extra information on the charset.
228.Pp
229The
230.Fn magic_check
231function can be used to check the validity of entries in the colon
232separated database files passed in as
233.Ar filename ,
234or
235.Dv NULL
236for the default database.
237It returns 0 on success and \-1 on failure.
238.Pp
239The
240.Fn magic_compile
241function can be used to compile the colon
242separated list of database files passed in as
243.Ar filename ,
244or
245.Dv NULL
246for the default database.
247It returns 0 on success and \-1 on failure.
248The compiled files created are named from the
249.Xr basename 1
250of each file argument with
251.Dq .mgc
252appended to it.
253.Pp
254The
255.Fn magic_list
256function dumps all magic entries in a human readable format,
257dumping first the entries that are matched against binary files and then the
258ones that match text files.
259It takes and optional
260.Fa filename
261argument which is a colon separated list of database files, or
262.Dv NULL
263for the default database.
264.Pp
265The
266.Fn magic_load
267function must be used to load the colon
268separated list of database files passed in as
269.Ar filename ,
270or
271.Dv NULL
272for the default database file before any magic queries can performed.
273.Pp
274The default database file is named by the MAGIC environment variable.
275If that variable is not set, the default database file name is __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 __MAGIC__.mgc -compact
390.It Pa __MAGIC__
391The non-compiled default magic database.
392.It Pa __MAGIC__.mgc
393The compiled default magic database.
394.El
395.Sh SEE ALSO
396.Xr file __CSECTION__ ,
397.Xr magic __FSECTION__
398.Sh BUGS
399The results from
400.Fn magic_buffer
401and
402.Fn magic_file
403where the buffer and the file contain the same data
404can produce different results, because in the
405.Fn magic_file
406case, the program can
407.Xr lseek 2
408and
409.Xr stat 2
410the file descriptor.
411.Sh AUTHORS
412.An M\(oans Rullg\(oard
413Initial libmagic implementation, and configuration.
414.An Christos Zoulas
415API cleanup, error code and allocation handling.
416