| MAGIC(3) | C LIBRARY FUNCTIONS | MAGIC(3) |
|---|
#include <magic.h>
Magic_t
{
unsigned long flags;
};
Magic_t* magicopen(unsigned long flags);
void magicclose(Magic_t* magic);
int magicload(Magic_t* magic, const char* path, unsigned long flags);
int magiclist(Magic_t* magic, Sfio_t* sp);
char* magictype(Magic_t* magic, const char* path, struct stat* st);
magicclose closes the magic session.
magicload loads the magic file named by path into the magic session. flags are the same as with magicopen. More than one magic file can be loaded into a session; the files are searched in load order. If path is 0 then the default magic file is loaded.
magiclist
lists the magic file contents on the
magictype
returns the type string for
path
with optional
A magic file entry describes a procedure for determining a single file type based on the file pathname, stat (2) information, and the file data. An entry is a sequence of lines, each line being a record of space separated fields. The general record format is:
[op]offset type [mask]expression description [mimetype]
#
in the first column introduces a comment.
The first record in an entry contains no
op;
the remaining records for an entry contain an
op.
Integer constants are as in C:
0x*
or
0X*
for hexadecimal,
0*
for octal and decimal otherwise.
The op field may be one of:
The offset field is either the offset into the data upon which the current entry operates or a file metadata identifier. Offsets are either integer constants or offset expressions. An offset expression is contained in (...) and is a combination of integral arithmetic operators and the @ indirection operator. Indirections take the form
The type field specifies the type of the data at offset. Integral types may be prefixed by le or be for specifying exact little-endian or big-endian representation, but the internal algorithm automatically loops through the standard representations to find integral matches, so representation prefixes are rarely used. However, this looping may cause some magic entry conflicts; use the le or be prefix in these cases. Only one representation is used for all the records in an entry. Valid types are:
The optional mask field takes the form
The contents of the expression field depends on the type. String type expression are described in the type field entries above. * means any value and applies to all types. Integral type expression take the form [operator] operand\P where operand is compared with the data value at offset using operator. operator may be one of <. <=, ==, >= or >. operator defaults to == if omitted. operand may be an integral constant or one of the following builtin function calls:
The description field is the most important because it is this field that is presented to the outside world. When constructing description fields one must be very careful to follow the style layed out in the magic file, lest yet another layer of inconsistency creep into the system. The description for each matching record in an entry are concatenated to form the complete magic type. If the previous matching description in the current entry does not end with space and the current description is not empty and does not start with comma , dot or backspace then a space is placed between the descriptions (most optional descriptions start with comma.) The data value at offset can be referenced in the description using %s for the string types and %ld or %lu for the integral types.
The mimetype field specifies the MIME type, usually in the form a/b.
0 long 0x020c0108 hp s200 executable, pure
o{
+36 long >0 , not stripped
+4 short >0 , version %ld
}
0 long 0x020c0107 hp s200 executable
o()
0 long 0x020c010b hp s200 executable, demand-load
o()
The function
o(),
shared by 3 entries,
determines if the executable is stripped and also extracts the version number.
0 long 0407 bsd 386 executable
&mode long &0111!=0
+16 long >0 , not stripped
This entry requires that the file also has execute permission.
| November 07, 2006 |