xref: /freebsd/contrib/file/doc/libmagic.man (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
1.\" $File: libmagic.man,v 1.50 2023/12/29 18:04:47 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 December 29, 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 7340032
315.It Li MAGIC_PARAM_ENCODING_MAX Ta size_t Ta 1048576
316.It Li MAGIC_PARAM_ELF_SHSIZE_MAX Ta size_t Ta 134217728
317.It Li MAGIC_PARAM_MAGWARN_MAX Ta size_t Ta 64
318.El
319.Pp
320The
321.Dv MAGIC_PARAM_INDIR_RECURSION
322parameter controls how many levels of recursion will be followed for
323indirect magic entries.
324.Pp
325The
326.Dv MAGIC_PARAM_NAME_RECURSION
327parameter controls how many levels of recursion will be followed for
328for name/use calls.
329.Pp
330The
331.Dv MAGIC_PARAM_NAME_MAX
332parameter controls the maximum number of calls for name/use.
333.Pp
334The
335.Dv MAGIC_PARAM_NOTES_MAX
336parameter controls how many ELF notes will be processed.
337.Pp
338The
339.Dv MAGIC_PARAM_PHNUM_MAX
340parameter controls how many ELF program sections will be processed.
341.Pp
342The
343.Dv MAGIC_PARAM_SHNUM_MAX
344parameter controls how many ELF sections will be processed.
345.Pp
346The
347.Dv MAGIC_PARAM_REGEX_MAX
348parameter controls the maximum length for regex searches.
349.Pp
350The
351.Dv MAGIC_PARAM_BYTES_MAX
352parameter controls the maximum number of bytes to look inside a file.
353.Pp
354The
355.Dv MAGIC_PARAM_ENCODING_MAX
356parameter controls the maximum number of bytes to scan for encoding detection.
357.Pp
358The
359.Dv MAGIC_PARAM_ELF_SHSIZE_MAX
360parameter controls the maximum number of bytes in an elf section.
361.Pp
362The
363.Dv MAGIC_PARAM_MAGWARN_MAX
364parameter controls the maximum number of warnings to tolerate in a magic file.
365.Pp
366The
367.Fn magic_version
368command returns the version number of this library which is compiled into
369the shared library using the constant
370.Dv MAGIC_VERSION
371from
372.In magic.h .
373This can be used by client programs to verify that the version they compile
374against is the same as the version that they run against.
375.Pp
376The
377.Fn magic_getpath
378command returns the colon separated list of magic database locations.
379If the
380.Fa filename
381is non-NULL, then it is returned.
382Otherwise, if the
383.Dv MAGIC
384environment variable is defined, then it is returned.
385Otherwise, if
386.Fa action
387is 0 (meaning "file load"), then any user-specific magic database file is included.
388Otherwise, only the system default magic database path is included.
389.Sh RETURN VALUES
390The function
391.Fn magic_open
392returns a magic cookie on success and
393.Dv NULL
394on failure setting errno to an appropriate value.
395It will set errno to
396.Er EINVAL
397if an unsupported value for flags was given.
398The
399.Fn magic_list ,
400.Fn magic_load ,
401.Fn magic_compile ,
402and
403.Fn magic_check
404functions return 0 on success and \-1 on failure.
405The
406.Fn magic_buffer ,
407.Fn magic_getpath ,
408and
409.Fn magic_file ,
410functions return a string on success and
411.Dv NULL
412on failure.
413The
414.Fn magic_error
415function returns a textual description of the errors of the above
416functions, or
417.Dv NULL
418if there was no error.
419The
420.Fn magic_version
421always returns the version number of the library.
422Finally,
423.Fn magic_setflags
424returns \-1 on systems that don't support
425.Xr utime 3 ,
426or
427.Xr utimes 2
428when
429.Dv MAGIC_PRESERVE_ATIME
430is set.
431.Sh FILES
432.Bl -tag -width __MAGIC__.mgc -compact
433.It Pa __MAGIC__
434The non-compiled default magic database.
435.It Pa __MAGIC__.mgc
436The compiled default magic database.
437.El
438.Sh SEE ALSO
439.Xr file __CSECTION__ ,
440.Xr magic __FSECTION__
441.Sh BUGS
442The results from
443.Fn magic_buffer
444and
445.Fn magic_file
446where the buffer and the file contain the same data
447can produce different results, because in the
448.Fn magic_file
449case, the program can
450.Xr lseek 2
451and
452.Xr stat 2
453the file descriptor.
454.Sh AUTHORS
455.An M\(oans Rullg\(oard
456Initial libmagic implementation, and configuration.
457.An Christos Zoulas
458API cleanup, error code and allocation handling.
459