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