xref: /freebsd/contrib/file/doc/libmagic.man (revision 61e21613)
1.\" $File: libmagic.man,v 1.49 2023/07/20 14:32:07 christos Exp $
2.\"
3.\" Copyright (c) Christos Zoulas 2003, 2018, 2022
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 16, 2023
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.Ft const char *
88.Fn magic_getpath "const char *magicfile" "int action"
89.Sh DESCRIPTION
90These functions
91operate on the magic database file
92which is described
93in
94.Xr magic __FSECTION__ .
95.Pp
96The function
97.Fn magic_open
98creates a magic cookie pointer and returns it.
99It returns
100.Dv NULL
101if there was an error allocating the magic cookie.
102The
103.Ar flags
104argument specifies how the other magic functions should behave:
105.Bl -tag -width MAGIC_COMPRESS
106.It Dv MAGIC_NONE
107No special handling.
108.It Dv MAGIC_DEBUG
109Print debugging messages to stderr.
110.It Dv MAGIC_SYMLINK
111If the file queried is a symlink, follow it.
112.It Dv MAGIC_COMPRESS
113If the file is compressed, unpack it and look at the contents.
114.It Dv MAGIC_DEVICES
115If the file is a block or character special device, then open the device
116and try to look in its contents.
117.It Dv MAGIC_MIME_TYPE
118Return a MIME type string, instead of a textual description.
119.It Dv MAGIC_MIME_ENCODING
120Return a MIME encoding, instead of a textual description.
121.It Dv MAGIC_MIME
122A shorthand for MAGIC_MIME_TYPE | MAGIC_MIME_ENCODING.
123.It Dv MAGIC_CONTINUE
124Return all matches, not just the first.
125.It Dv MAGIC_CHECK
126Check the magic database for consistency and print warnings to stderr.
127.It Dv MAGIC_PRESERVE_ATIME
128On systems that support
129.Xr utime 3
130or
131.Xr utimes 2 ,
132attempt to preserve the access time of files analysed.
133.It Dv MAGIC_RAW
134Don't translate unprintable characters to a \eooo octal representation.
135.It Dv MAGIC_ERROR
136Treat operating system errors while trying to open files and follow symlinks
137as real errors, instead of printing them in the magic buffer.
138.It Dv MAGIC_APPLE
139Return the Apple creator and type.
140.It Dv MAGIC_EXTENSION
141Return a slash-separated list of extensions for this file type.
142.It Dv MAGIC_COMPRESS_TRANSP
143Don't report on compression, only report about the uncompressed data.
144.It Dv MAGIC_NO_CHECK_APPTYPE
145Don't check for
146.Dv EMX
147application type (only on EMX).
148.It Dv MAGIC_NO_COMPRESS_FORK
149Don't allow decompressors that use fork.
150.It Dv MAGIC_NO_CHECK_CDF
151Don't get extra information on MS Composite Document Files.
152.It Dv MAGIC_NO_CHECK_COMPRESS
153Don't look inside compressed files.
154.It Dv MAGIC_NO_CHECK_ELF
155Don't print ELF details.
156.It Dv MAGIC_NO_CHECK_ENCODING
157Don't check text encodings.
158.It Dv MAGIC_NO_CHECK_SOFT
159Don't consult magic files.
160.It Dv MAGIC_NO_CHECK_TAR
161Don't examine tar files.
162.It Dv MAGIC_NO_CHECK_TEXT
163Don't check for various types of text files.
164.It Dv MAGIC_NO_CHECK_TOKENS
165Don't look for known tokens inside ascii files.
166.It Dv MAGIC_NO_CHECK_JSON
167Don't examine JSON files.
168.It Dv MAGIC_NO_CHECK_CSV
169Don't examine CSV files.
170.It Dv MAGIC_NO_CHECK_SIMH
171Don't examine SIMH tape files.
172.El
173.Pp
174The
175.Fn magic_close
176function closes the
177.Xr magic __FSECTION__
178database and deallocates any resources used.
179.Pp
180The
181.Fn magic_error
182function returns a textual explanation of the last error, or
183.Dv NULL
184if there was no error.
185.Pp
186The
187.Fn magic_errno
188function returns the last operating system error number
189.Pq Xr errno 2
190that was encountered by a system call.
191.Pp
192The
193.Fn magic_file
194function returns a textual description of the contents of the
195.Ar filename
196argument, or
197.Dv NULL
198if an error occurred.
199If the
200.Ar filename
201is
202.Dv NULL ,
203then stdin is used.
204.Pp
205The
206.Fn magic_descriptor
207function returns a textual description of the contents of the
208.Ar fd
209argument, or
210.Dv NULL
211if an error occurred.
212.Pp
213The
214.Fn magic_buffer
215function returns a textual description of the contents of the
216.Ar buffer
217argument with
218.Ar length
219bytes size.
220.Pp
221The
222.Fn magic_getflags
223functions returns a value representing current
224.Ar flags
225set.
226.Pp
227The
228.Fn magic_setflags
229function sets the
230.Ar flags
231described above.
232Note that using both MIME flags together can also
233return extra information on the charset.
234.Pp
235The
236.Fn magic_check
237function can be used to check the validity of entries in the colon
238separated database files passed in as
239.Ar filename ,
240or
241.Dv NULL
242for the default database.
243It returns 0 on success and \-1 on failure.
244.Pp
245The
246.Fn magic_compile
247function can be used to compile the colon
248separated list of database files passed in as
249.Ar filename ,
250or
251.Dv NULL
252for the default database.
253It returns 0 on success and \-1 on failure.
254The compiled files created are named from the
255.Xr basename 1
256of each file argument with
257.Dq .mgc
258appended to it.
259.Pp
260The
261.Fn magic_list
262function dumps all magic entries in a human readable format,
263dumping first the entries that are matched against binary files and then the
264ones that match text files.
265It takes and optional
266.Fa filename
267argument which is a colon separated list of database files, or
268.Dv NULL
269for the default database.
270.Pp
271The
272.Fn magic_load
273function must be used to load the colon
274separated list of database files passed in as
275.Ar filename ,
276or
277.Dv NULL
278for the default database file before any magic queries can performed.
279.Pp
280The default database file is named by the MAGIC environment variable.
281If that variable is not set, the default database file name is __MAGIC__.
282.Fn magic_load
283adds
284.Dq .mgc
285to the database filename as appropriate.
286.Pp
287The
288.Fn magic_load_buffers
289function takes an array of size
290.Fa nbuffers
291of
292.Fa buffers
293with a respective size for each in the array of
294.Fa sizes
295loaded with the contents of the magic databases from the filesystem.
296This function can be used in environment where the magic library does
297not have direct access to the filesystem, but can access the magic
298database via shared memory or other IPC means.
299.Pp
300The
301.Fn magic_getparam
302and
303.Fn magic_setparam
304allow getting and setting various limits related to the magic
305library.
306.Bl -column "MAGIC_PARAM_ELF_PHNUM_MAX" "size_t" "Default" -offset indent
307.It Sy "Parameter" Ta Sy "Type" Ta Sy "Default"
308.It Li MAGIC_PARAM_INDIR_MAX Ta size_t Ta 15
309.It Li MAGIC_PARAM_NAME_MAX Ta size_t Ta 30
310.It Li MAGIC_PARAM_ELF_NOTES_MAX Ta size_t Ta 256
311.It Li MAGIC_PARAM_ELF_PHNUM_MAX Ta size_t Ta 128
312.It Li MAGIC_PARAM_ELF_SHNUM_MAX Ta size_t Ta 32768
313.It Li MAGIC_PARAM_REGEX_MAX Ta size_t Ta 8192
314.It Li MAGIC_PARAM_BYTES_MAX Ta size_t Ta 1048576
315.El
316.Pp
317The
318.Dv MAGIC_PARAM_INDIR_RECURSION
319parameter controls how many levels of recursion will be followed for
320indirect magic entries.
321.Pp
322The
323.Dv MAGIC_PARAM_NAME_RECURSION
324parameter controls how many levels of recursion will be followed for
325for name/use calls.
326.Pp
327The
328.Dv MAGIC_PARAM_NAME_MAX
329parameter controls the maximum number of calls for name/use.
330.Pp
331The
332.Dv MAGIC_PARAM_NOTES_MAX
333parameter controls how many ELF notes will be processed.
334.Pp
335The
336.Dv MAGIC_PARAM_PHNUM_MAX
337parameter controls how many ELF program sections will be processed.
338.Pp
339The
340.Dv MAGIC_PARAM_SHNUM_MAX
341parameter controls how many ELF sections will be processed.
342.Pp
343The
344.Fn magic_version
345command returns the version number of this library which is compiled into
346the shared library using the constant
347.Dv MAGIC_VERSION
348from
349.In magic.h .
350This can be used by client programs to verify that the version they compile
351against is the same as the version that they run against.
352.Pp
353The
354.Fn magic_getpath
355command returns the colon separated list of magic database locations.
356If the
357.Fa filename
358is non-NULL, then it is returned.
359Otherwise, if the
360.Dv MAGIC
361environment variable is defined, then it is returned.
362Otherwise, if
363.Fa action
364is 0 (meaning "file load"), then any user-specific magic database file is included.
365Otherwise, only the system default magic database path is included.
366.Sh RETURN VALUES
367The function
368.Fn magic_open
369returns a magic cookie on success and
370.Dv NULL
371on failure setting errno to an appropriate value.
372It will set errno to
373.Er EINVAL
374if an unsupported value for flags was given.
375The
376.Fn magic_list ,
377.Fn magic_load ,
378.Fn magic_compile ,
379and
380.Fn magic_check
381functions return 0 on success and \-1 on failure.
382The
383.Fn magic_buffer ,
384.Fn magic_getpath ,
385and
386.Fn magic_file ,
387functions return a string on success and
388.Dv NULL
389on failure.
390The
391.Fn magic_error
392function returns a textual description of the errors of the above
393functions, or
394.Dv NULL
395if there was no error.
396The
397.Fn magic_version
398always returns the version number of the library.
399Finally,
400.Fn magic_setflags
401returns \-1 on systems that don't support
402.Xr utime 3 ,
403or
404.Xr utimes 2
405when
406.Dv MAGIC_PRESERVE_ATIME
407is set.
408.Sh FILES
409.Bl -tag -width __MAGIC__.mgc -compact
410.It Pa __MAGIC__
411The non-compiled default magic database.
412.It Pa __MAGIC__.mgc
413The compiled default magic database.
414.El
415.Sh SEE ALSO
416.Xr file __CSECTION__ ,
417.Xr magic __FSECTION__
418.Sh BUGS
419The results from
420.Fn magic_buffer
421and
422.Fn magic_file
423where the buffer and the file contain the same data
424can produce different results, because in the
425.Fn magic_file
426case, the program can
427.Xr lseek 2
428and
429.Xr stat 2
430the file descriptor.
431.Sh AUTHORS
432.An M\(oans Rullg\(oard
433Initial libmagic implementation, and configuration.
434.An Christos Zoulas
435API cleanup, error code and allocation handling.
436