Magic file linux примеры

Magic file linux примеры

Each line of the file specifies a test to be performed. A test compares the data starting at a particular offset in the file with a 1-byte, 2-byte, or 4-byte numeric value or a string. If the test succeeds, a message is printed. The line consists of the following fields: offset A number specifying the offset, in bytes, into the file of the data which is to be tested. type The type of the data to be tested. The possible values are: byte A one-byte value. short A two-byte value (on most systems) in this machine’s native byte order. long A four-byte value (on most systems) in this machine’s native byte order. string A string of bytes. The string type specification can be optionally followed by /[Bbc]*. The «B» flag compacts whitespace in the target, which must contain at least one whitespace character. If the magic has n consecutive blanks, the target needs at least n consecutive blanks to match. The «b» flag treats every blank in the target as an optional blank. Finally the «c» flag, specifies case insensitive matching: lowercase characters in the magic match both lower and upper case characters in the targer, whereas upper case characters in the magic, only much uppercase characters in the target. pstring A pascal style string where the first byte is interpreted as the an unsigned length. The string is not NUL terminated. date A four-byte value interpreted as a UNIX date. ldate A four-byte value interpreted as a UNIX-style date, but interpreted as local time rather than UTC. beshort A two-byte value (on most systems) in big-endian byte order. belong A four-byte value (on most systems) in big-endian byte order. bedate A four-byte value (on most systems) in big-endian byte order, interpreted as a Unix date. beldate A four-byte value (on most systems) in big-endian byte order, interpreted as a UNIX-style date, but interpreted as local time rather than UTC. bestring16 A two-byte unicode (UCS16) string in big-endian byte order. leshort A two-byte value (on most systems) in little-endian byte order. lelong A four-byte value (on most systems) in little-endian byte order. ledate A four-byte value (on most systems) in little-endian byte order, interpreted as a UNIX date. leldate A four-byte value (on most systems) in little-endian byte order, interpreted as a UNIX-style date, but interpreted as local time rather than UTC. lestring16 A two-byte unicode (UCS16) string in little-endian byte order. melong A four-byte value (on most systems) in middle-endian (PDP-11) byte order. medate A four-byte value (on most systems) in middle-endian (PDP-11) byte order, interpreted as a UNIX date. meldate A four-byte value (on most systems) in middle-endian (PDP-11) byte order, interpreted as a UNIX-style date, but interpreted as local time rather than UTC. regex A regular expression match in extended POSIX regular expression syntax (much like egrep). The type specification can be optionally followed by /c for case-insensitive matches. The regular expression is always tested against the first N lines, where N is the given offset, thus it is only useful for (single-byte encoded) text. ^ and $ will match the beginning and end of individual lines, respectively, not beginning and end of file. search A literal string search starting at the given offset. It must be followed by / which specifies how many matches shall be attempted (the range). This is suitable for searching larger binary expressions with variable offsets, using \ escapes for special characters.

The numeric types may optionally be followed by & and a numeric value, to specify that the value is to be AND’ed with the numeric value before any comparisons are done. Prepending a u to the type indicates that ordered comparisons should be unsigned. test The value to be compared with the value from the file. If the type is numeric, this value is specified in C form; if it is a string, it is specified as a C string with the usual escapes permitted (e.g. \n for new-line). Numeric values may be preceded by a character indicating the operation to be performed. It may be = , to specify that the value from the file must equal the specified value, , to specify that the value from the file must be less than the specified value, > , to specify that the value from the file must be greater than the specified value, & , to specify that the value from the file must have set all of the bits that are set in the specified value, ^ , to specify that the value from the file must have clear any of the bits that are set in the specified value, or

Читайте также:  Что делать если эта копия windows не прошла проверку подлинности

, the value specified after is negated before tested. x , to specify that any value will match. If the character is omitted, it is assumed to be = . For all tests except string and regex, operation ! specifies that the line matches if the test does not succeed. Numeric values are specified in C form; e.g. 13 is decimal, 013 is octal, and 0x13 is hexadecimal. For string values, the byte string from the file must match the specified byte string. The operators = , and > (but not & ) can be applied to strings. The length used for matching is that of the string argument in the magic file. This means that a line can match any string, and then presumably print that string, by doing >\0 (because all strings are greater than the null string). message The message to be printed if the comparison succeeds. If the string contains a printf (3) format specification, the value from the file (with any specified masking performed) is printed using the message as the format string.

Some file formats contain additional information which is to be printed along with the file type or need additional tests to determine the true file type. These additional tests are introduced by one or more > characters preceding the offset. The number of > on the line indicates the level of the test; a line with no > at the beginning is considered to be at level 0. Tests are arranged in a tree-like hierarchy: If a the test on a line at level n succeeds, all following tests at level n+1 are performed, and the messages printed if the tests succeed, untile a line with level n (or less) appears. For more complex files, one can use empty messages to get just the «if/then» effect, in the following way:

Offsets do not need to be constant, but can also be read from the file being examined. If the first character following the last > is a ( then the string after the parenthesis is interpreted as an indirect offset. That means that the number after the parenthesis is used as an offset in the file. The value at that offset is read, and is used again as an offset in the file. Indirect offsets are of the form: (( x [.[bslBSL]][+-][ y ]). The value of x is used as an offset in the file. A byte, short or long is read at that offset depending on the [bslBSLm] type specifier. The capitalized types interpret the number as a big endian value, whereas the small letter versions interpret the number as a little endian value; the m type interprets the number as a middle endian (PDP-11) value. To that number the value of y is added and the result is used as an offset in the file. The default type if one is not specified is long.

That way variable length structures can be examined:

This strategy of examining has one drawback: You must make sure that you eventually print something, or users may get empty output (like, when there is neither PE\0\0 nor LE\0\0 in the above example)

If this indirect offset cannot be used as-is, there are simple calculations possible: appending [+-*/%&|^] inside parentheses allows one to modify the value read from the file before it is used as an offset:

Sometimes you do not know the exact offset as this depends on the length or position (when indirection was used before) of preceding fields. You can specify an offset relative to the end of the last uplevel field using & as a prefix to the offset:

Indirect and relative offsets can be combined:

Or the other way around:

Finally, if you have to deal with offset/length pairs in your file, even the second value in a parenthesed expression can be taken from the file itself, using another set of parentheses. Note that this additional indirect offset is always relative to the start of the main indirect offset.

Читайте также:  Windows 10 как закрыть меню пуск

Источник

Magic file linux примеры

File tests each argument in an attempt to classify it. There are three sets of tests, performed in this order: filesystem tests, magic number tests, and language tests. The first test that succeeds causes the file type to be printed.

The type printed will usually contain one of the words text (the file contains only printing characters and a few common control characters and is probably safe to read on an ASCII terminal), executable (the file contains the result of compiling a program in a form understandable to some UNIX kernel or another), or data meaning anything else (data is usually `binary’ or non-printable). Exceptions are well-known file formats (core files, tar archives) that are known to contain binary data. When modifying the file /usr/share/file/magic or the program itself, preserve these keywords . People depend on knowing that all the readable files in a directory have the word «text» printed. Don’t do as Berkeley did and change «shell commands text» to «shell script». Note that the file /usr/share/file/magic is built mechanically from a large number of small files in the subdirectory Magdir in the source distribution of this program.

The filesystem tests are based on examining the return from a stat (2) system call. The program checks to see if the file is empty, or if it’s some sort of special file. Any known file types appropriate to the system you are running on (sockets, symbolic links, or named pipes (FIFOs) on those systems that implement them) are intuited if they are defined in the system header file .

The magic number tests are used to check for files with data in particular fixed formats. The canonical example of this is a binary executable (compiled program) a.out file, whose format is defined in a.out.h and possibly exec.h in the standard include directory. These files have a `magic number’ stored in a particular place near the beginning of the file that tells the UNIX operating system that the file is a binary executable, and which of several types thereof. The concept of `magic number’ has been applied by extension to data files. Any file with some invariant identifier at a small fixed offset into the file can usually be described in this way. The information identifying these files is read from the compiled magic file /usr/share/file/magic.mgc , or /usr/share/file/magic if the compile file does not exist. In addition file will look in $HOME/.magic.mgc , or $HOME/.magic for magic entries.

If a file does not match any of the entries in the magic file, it is examined to see if it seems to be a text file. ASCII, ISO-8859-x, non-ISO 8-bit extended-ASCII character sets (such as those used on Macintosh and IBM PC systems), UTF-8-encoded Unicode, UTF-16-encoded Unicode, and EBCDIC character sets can be distinguished by the different ranges and sequences of bytes that constitute printable text in each set. If a file passes any of these tests, its character set is reported. ASCII, ISO-8859-x, UTF-8, and extended-ASCII files are identified as «text» because they will be mostly readable on nearly any terminal; UTF-16 and EBCDIC are only «character data» because, while they contain text, it is text that will require translation before it can be read. In addition, file will attempt to determine other characteristics of text-type files. If the lines of a file are terminated by CR, CRLF, or NEL, instead of the Unix-standard LF, this will be reported. Files that contain embedded escape sequences or overstriking will also be identified.

Once file has determined the character set used in a text-type file, it will attempt to determine in what language the file is written. The language tests look for particular strings (cf names.h ) that can appear anywhere in the first few blocks of a file. For example, the keyword .br indicates that the file is most likely a troff (1) input file, just as the keyword struct indicates a C program. These tests are less reliable than the previous two groups, so they are performed last. The language test routines also test for some miscellany (such as tar (1) archives).

Any file that cannot be identified as having been written in any of the character sets listed above is simply said to be «data».

Читайте также:  Ide python linux 32 bit

OPTIONS


FILES


ENVIRONMENT


SEE ALSO


STANDARDS CONFORMANCE

The one significant difference between this version and System V is that this version treats any white space as a delimiter, so that spaces in pattern strings must be escaped. For example,
>10 string language impress (imPRESS data)

in an existing magic file would have to be changed to
>10 string language\ impress (imPRESS data)

In addition, in this version, if a pattern string contains a backslash, it must be escaped. For example
0 string \begindata Andrew Toolkit document

in an existing magic file would have to be changed to
0 string \\begindata Andrew Toolkit document

SunOS releases 3.2 and later from Sun Microsystems include a file (1) command derived from the System V one, but with some extensions. My version differs from Sun’s only in minor ways. It includes the extension of the `&’ operator, used as, for example,
>16 long&0x7fffffff >0 not stripped

MAGIC DIRECTORY

The order of entries in the magic file is significant. Depending on what system you are using, the order that they are put together may be incorrect. If your old file command uses a magic file, keep the old magic file around for comparison purposes (rename it to /usr/share/file/magic.orig ).

EXAMPLES


HISTORY

This program, based on the System V version, was written by Ian Darwin without looking at anybody else’s source code.

John Gilmore revised the code extensively, making it better than the first version. Geoff Collyer found several inadequacies and provided some magic file entries. Contributions by the `&’ operator by Rob McMahon, cudcv@warwick.ac.uk, 1989.

Guy Harris, guy@netapp.com, made many changes from 1993 to the present.

Primary development and maintenance from 1990 to the present by Christos Zoulas (christos@astron.com).

Altered by Chris Lowth, chris@lowth.com, 2000: Handle the «-i» option to output mime type strings and using an alternative magic file and internal logic.

Altered by Eric Fischer (enf@pobox.com), July, 2000, to identify character codes and attempt to identify the languages of non-ASCII files.

The list of contributors to the «Magdir» directory (source for the /usr/share/file/magic file) is too long to include here. You know who you are; thank you.

The files tar.h and is_tar.c were written by John Gilmore from his public-domain tar program, and are not covered by the above license.

File uses several algorithms that favor speed over accuracy, thus it can be misled about the contents of text files.

The support for text files (primarily for programming languages) is simplistic, inefficient and requires recompilation to update.

There should be an «else» clause to follow a series of continuation lines.

The magic file and keywords should have regular expression support. Their use of ASCII TAB as a field delimiter is ugly and makes it hard to edit the files, but is entrenched.

It might be advisable to allow upper-case letters in keywords for e.g., troff (1) commands vs man page macros. Regular expression support would make this easy.

The program doesn’t grok FORTRAN . It should be able to figure FORTRAN by seeing some keywords which appear indented at the start of line. Regular expression support would make this easy.

The list of keywords in ascmagic probably belongs in the Magic file. This could be done by using some keyword like `*’ for the offset value.

Another optimisation would be to sort the magic file so that we can just run down all the tests for the first byte, first word, first long, etc, once we have fetched it. Complain about conflicts in the magic file entries. Make a rule that the magic entries sort based on file offset rather than position within the magic file?

The program should provide a way to give an estimate of «how good» a guess is. We end up removing guesses (e.g. «From » as first 5 chars of file) because they are not as good as other guesses (e.g. «Newsgroups:» versus «Return-Path:»). Still, if the others don’t pan out, it should be possible to use the first guess.

This program is slower than some vendors’ file commands. The new support for multiple character codes makes it even slower.

This manual page, and particularly this section, is too long.

Источник

Оцените статью