mirror of
https://github.com/radareorg/radare2.git
synced 2024-10-06 18:13:46 +00:00
Introduce the r_magic_load_buffer() api and boolify a bit ##search (#15617)
This commit is contained in:
parent
631cb78fc3
commit
f680995474
@ -1,6 +1,6 @@
|
||||
/* radare - LGPL - Copyright 2009-2018 - pancake */
|
||||
/* radare - LGPL - Copyright 2009-2019 - pancake */
|
||||
|
||||
#include "r_core.h"
|
||||
#include <r_core.h>
|
||||
|
||||
/* ugly global vars */
|
||||
static int magicdepth = 99; //XXX: do not use global var here
|
||||
@ -67,7 +67,7 @@ static int r_core_magic_at(RCore *core, const char *file, ut64 addr, int depth,
|
||||
if (file) {
|
||||
free (ofile);
|
||||
ofile = strdup (file);
|
||||
if (r_magic_load (ck, file) == -1) {
|
||||
if (!r_magic_load (ck, file)) {
|
||||
eprintf ("failed r_magic_load (\"%s\") %s\n", file, r_magic_error (ck));
|
||||
ck = NULL;
|
||||
ret = -1;
|
||||
@ -75,7 +75,7 @@ static int r_core_magic_at(RCore *core, const char *file, ut64 addr, int depth,
|
||||
}
|
||||
} else {
|
||||
const char *magicpath = r_config_get (core->config, "dir.magic");
|
||||
if (r_magic_load (ck, magicpath) == -1) {
|
||||
if (!r_magic_load (ck, magicpath)) {
|
||||
ck = NULL;
|
||||
eprintf ("failed r_magic_load (dir.magic) %s\n", r_magic_error (ck));
|
||||
ret = -1;
|
||||
@ -172,10 +172,6 @@ static int r_core_magic_at(RCore *core, const char *file, ut64 addr, int depth,
|
||||
}
|
||||
adelta ++;
|
||||
delta ++;
|
||||
#if 0
|
||||
if((core->blocksize-delta)>16)
|
||||
goto repeat;
|
||||
#endif
|
||||
#if 0
|
||||
r_magic_free (ck);
|
||||
ck = NULL;
|
||||
|
@ -304,9 +304,10 @@ R_API const char *r_magic_buffer(RMagic*, const void *, size_t);
|
||||
R_API const char *r_magic_error(RMagic*);
|
||||
R_API void r_magic_setflags(RMagic*, int);
|
||||
|
||||
R_API int r_magic_load(RMagic*, const char *);
|
||||
R_API int r_magic_compile(RMagic*, const char *);
|
||||
R_API int r_magic_check(RMagic*, const char *);
|
||||
R_API bool r_magic_load(RMagic*, const char *);
|
||||
R_API bool r_magic_load_buffer(RMagic*, const char *);
|
||||
R_API bool r_magic_compile(RMagic*, const char *);
|
||||
R_API bool r_magic_check(RMagic*, const char *);
|
||||
R_API int r_magic_errno(RMagic*);
|
||||
#endif
|
||||
|
||||
|
@ -35,26 +35,29 @@
|
||||
|
||||
#include <r_util.h>
|
||||
#include <ctype.h>
|
||||
#ifndef _MSC_VER
|
||||
#include <sys/param.h>
|
||||
#endif
|
||||
#if __UNIX__
|
||||
#define QUICK 1
|
||||
#include <sys/mman.h>
|
||||
#endif
|
||||
#include "file.h"
|
||||
#include "patchlevel.h"
|
||||
#ifdef _MSC_VER
|
||||
#include <sys\stat.h>
|
||||
#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
|
||||
#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
|
||||
#define MAXPATHLEN 255
|
||||
|
||||
#if __UNIX__
|
||||
# define QUICK 1
|
||||
# include <sys/mman.h>
|
||||
# include <sys/param.h>
|
||||
#else
|
||||
# define QUICK 0
|
||||
#endif
|
||||
#define EATAB {while (isascii((ut8) *l) && isspace((ut8) *l)) ++l;}
|
||||
|
||||
#ifdef _MSC_VER
|
||||
# include <sys\stat.h>
|
||||
# define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
|
||||
# define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
|
||||
# define MAXPATHLEN 255
|
||||
#endif
|
||||
|
||||
#define EATAB {while (isascii((ut8) *l) && isspace((ut8) *l)) { l++; }}
|
||||
#define LOWCASE(l) (isupper((ut8) (l)) ? tolower((ut8) (l)) : (l))
|
||||
|
||||
#ifndef MAP_FILE
|
||||
#define MAP_FILE 0
|
||||
# define MAP_FILE 0
|
||||
#endif
|
||||
|
||||
struct r_magic_entry {
|
||||
@ -163,12 +166,12 @@ static int get_type(const char *l, const char **t) {
|
||||
}
|
||||
|
||||
static void init_file_tables(void) {
|
||||
static int done = 0;
|
||||
static bool done = false;
|
||||
const struct type_tbl_s *p;
|
||||
if (done) {
|
||||
return;
|
||||
}
|
||||
done++;
|
||||
done = true;
|
||||
for (p = type_tbl; p->len; p++) {
|
||||
if (p->type >= FILE_NAMES_SIZE) {
|
||||
continue;
|
||||
@ -245,8 +248,8 @@ static int apprentice_1(RMagic *ms, const char *fn, int action, struct mlist *ml
|
||||
|
||||
void file_delmagic(struct r_magic *p, int type, size_t entries) {
|
||||
if (p) {
|
||||
switch (type) {
|
||||
#ifdef QUICK
|
||||
switch (type) {
|
||||
#if QUICK
|
||||
case 2:
|
||||
p--;
|
||||
(void)munmap ((void *)p, sizeof (*p) * (entries + 1));
|
||||
@ -259,7 +262,7 @@ void file_delmagic(struct r_magic *p, int type, size_t entries) {
|
||||
R_FREE (p);
|
||||
break;
|
||||
default:
|
||||
abort ();
|
||||
abort (); // do not abort, ever XXX this is a lib, so it shouldn just report an error
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -271,12 +274,8 @@ struct mlist * file_apprentice(RMagic *ms, const char *fn, int action) {
|
||||
struct mlist *mlist;
|
||||
|
||||
init_file_tables ();
|
||||
|
||||
if (!fn) {
|
||||
fn = getenv ("MAGIC");
|
||||
}
|
||||
if (!fn) {
|
||||
fn = MAGICFILE;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!(mfn = strdup (fn))) {
|
||||
@ -480,10 +479,61 @@ static void set_test_type(struct r_magic *mstart, struct r_magic *m) {
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Load and parse from buffer.
|
||||
*/
|
||||
static const char* bgets (char *line, size_t line_sz, const char *data) {
|
||||
const char *nl = strchr (data, '\n');
|
||||
const int nlsz = nl
|
||||
? (nl? nl-data: strlen (data))
|
||||
: R_MIN (line_sz, strlen (data));
|
||||
r_str_ncpy (line, data, nlsz);
|
||||
return data + nlsz;
|
||||
}
|
||||
|
||||
static void load_b(RMagic *ms, int action, const char *data, int *errs, struct r_magic_entry **marray, ut32 *marraycount) {
|
||||
const char *ndata;
|
||||
char line[BUFSIZ];
|
||||
size_t lineno = 0;
|
||||
/* read and parse this file */
|
||||
for (ms->line = 1; (ndata = bgets (line, sizeof (line), data)) != NULL; ms->line++) {
|
||||
size_t len = strlen (line);
|
||||
if (len == 0) { /* null line, garbage, etc */
|
||||
continue;
|
||||
}
|
||||
if (line[len - 1] == '\n') {
|
||||
lineno++;
|
||||
line[len - 1] = '\0'; /* delete newline */
|
||||
}
|
||||
if (line[0] == '\0') { /* empty, do not parse */
|
||||
continue;
|
||||
}
|
||||
if (line[0] == '#') { /* comment, do not parse */
|
||||
continue;
|
||||
}
|
||||
if (len > mime_marker_len && !memcmp (line, mime_marker, mime_marker_len)) {
|
||||
/* MIME type */
|
||||
if (parse_mime (ms, marray, marraycount,
|
||||
line + mime_marker_len) != 0) {
|
||||
(*errs)++;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (parse (ms, marray, marraycount, line, lineno, action) != 0) {
|
||||
(*errs)++;
|
||||
}
|
||||
data = ndata;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Load and parse one file.
|
||||
*/
|
||||
static void load_1(RMagic *ms, int action, const char *file, int *errs, struct r_magic_entry **marray, ut32 *marraycount) {
|
||||
if (*file == '#') {
|
||||
load_b (ms, action, file, errs, marray, marraycount);
|
||||
return;
|
||||
}
|
||||
char line[BUFSIZ];
|
||||
size_t lineno = 0;
|
||||
FILE *f = r_sandbox_fopen (ms->file = file, "r");
|
||||
@ -492,38 +542,38 @@ static void load_1(RMagic *ms, int action, const char *file, int *errs, struct r
|
||||
file_error (ms, errno, "cannot read magic file `%s'", file);
|
||||
}
|
||||
(*errs)++;
|
||||
} else {
|
||||
/* read and parse this file */
|
||||
for (ms->line = 1; fgets (line, sizeof (line), f) != NULL; ms->line++) {
|
||||
size_t len = strlen (line);
|
||||
if (len == 0) { /* null line, garbage, etc */
|
||||
continue;
|
||||
}
|
||||
if (line[len - 1] == '\n') {
|
||||
lineno++;
|
||||
line[len - 1] = '\0'; /* delete newline */
|
||||
}
|
||||
if (line[0] == '\0') { /* empty, do not parse */
|
||||
continue;
|
||||
}
|
||||
if (line[0] == '#') { /* comment, do not parse */
|
||||
continue;
|
||||
}
|
||||
if (len > mime_marker_len &&
|
||||
memcmp (line, mime_marker, mime_marker_len) == 0) {
|
||||
/* MIME type */
|
||||
if (parse_mime (ms, marray, marraycount,
|
||||
line + mime_marker_len) != 0) {
|
||||
(*errs)++;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (parse (ms, marray, marraycount, line, lineno, action) != 0) {
|
||||
return;
|
||||
}
|
||||
/* read and parse this file */
|
||||
for (ms->line = 1; fgets (line, sizeof (line), f) != NULL; ms->line++) {
|
||||
size_t len = strlen (line);
|
||||
if (len == 0) { /* null line, garbage, etc */
|
||||
continue;
|
||||
}
|
||||
if (line[len - 1] == '\n') {
|
||||
lineno++;
|
||||
line[len - 1] = '\0'; /* delete newline */
|
||||
}
|
||||
if (line[0] == '\0') { /* empty, do not parse */
|
||||
continue;
|
||||
}
|
||||
if (line[0] == '#') { /* comment, do not parse */
|
||||
continue;
|
||||
}
|
||||
if (len > mime_marker_len &&
|
||||
memcmp (line, mime_marker, mime_marker_len) == 0) {
|
||||
/* MIME type */
|
||||
if (parse_mime (ms, marray, marraycount,
|
||||
line + mime_marker_len) != 0) {
|
||||
(*errs)++;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (parse (ms, marray, marraycount, line, lineno, action) != 0) {
|
||||
(*errs)++;
|
||||
}
|
||||
fclose (f);
|
||||
}
|
||||
fclose (f);
|
||||
}
|
||||
|
||||
/*
|
||||
@ -535,17 +585,17 @@ static int apprentice_load(RMagic *ms, struct r_magic **magicp, ut32 *nmagicp, c
|
||||
struct r_magic_entry *marray;
|
||||
struct stat st;
|
||||
int errs = 0;
|
||||
#if !__WINDOWS__
|
||||
DIR *dir;
|
||||
struct dirent *d;
|
||||
char subfn[MAXPATHLEN];
|
||||
#else
|
||||
#if __WINDOWS__
|
||||
HANDLE hdir;
|
||||
WIN32_FIND_DATAW entry;
|
||||
wchar_t dir[MAX_PATH];
|
||||
wchar_t *wcpath;
|
||||
char *cfname;
|
||||
char subfn[1024];
|
||||
char subfn[1024];
|
||||
#else
|
||||
DIR *dir;
|
||||
struct dirent *d;
|
||||
char subfn[MAXPATHLEN];
|
||||
#endif
|
||||
ms->flags |= R_MAGIC_CHECK; /* Enable checks for parsed files */
|
||||
|
||||
@ -630,8 +680,7 @@ static int apprentice_load(RMagic *ms, struct r_magic **magicp, ut32 *nmagicp, c
|
||||
marray[i].mp->desc[0] ? marray[i].mp->desc : "(no description)",
|
||||
marray[i].mp->flag & BINTEST ? "binary" : "text");
|
||||
if (marray[i].mp->flag & BINTEST) {
|
||||
#define SYMBOL "text"
|
||||
#define SYMLEN sizeof (SYMBOL)
|
||||
#define SYMLEN 4 /* strlen("text") */
|
||||
char *p = strstr(marray[i].mp->desc, "text");
|
||||
if (p && (p == marray[i].mp->desc || isspace((unsigned char)p[-1])) &&
|
||||
(p + SYMLEN - marray[i].mp->desc == MAXstring ||
|
||||
@ -639,8 +688,6 @@ static int apprentice_load(RMagic *ms, struct r_magic **magicp, ut32 *nmagicp, c
|
||||
(void)fprintf(stderr,
|
||||
"*** Possible binary test for text type\n");
|
||||
}
|
||||
#undef SYMBOL
|
||||
#undef SYMLEN
|
||||
}
|
||||
}
|
||||
} while (++i < marraycount && marray[i].mp->cont_level != 0);
|
||||
|
@ -1,4 +1,3 @@
|
||||
|
||||
#------------------------------------------------------------------------------
|
||||
# $File: macintosh,v 1.22 2011/05/17 17:40:31 rrt Exp $
|
||||
# macintosh description
|
||||
|
@ -1,500 +0,0 @@
|
||||
.\" $OpenBSD: file.1,v 1.33 2010/10/28 21:32:54 jmc Exp $
|
||||
.\" $FreeBSD: src/usr.bin/file/file.1,v 1.16 2000/03/01 12:19:39 sheldonh Exp $
|
||||
.\"
|
||||
.\" Copyright (c) Ian F. Darwin 1986-1995.
|
||||
.\" Software written by Ian F. Darwin and others;
|
||||
.\" maintained 1995-present by Christos Zoulas and others.
|
||||
.\"
|
||||
.\" Redistribution and use in source and binary forms, with or without
|
||||
.\" modification, are permitted provided that the following conditions
|
||||
.\" are met:
|
||||
.\" 1. Redistributions of source code must retain the above copyright
|
||||
.\" notice immediately at the beginning of the file, without modification,
|
||||
.\" this list of conditions, and the following disclaimer.
|
||||
.\" 2. Redistributions in binary form must reproduce the above copyright
|
||||
.\" notice, this list of conditions and the following disclaimer in the
|
||||
.\" documentation and/or other materials provided with the distribution.
|
||||
.\"
|
||||
.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
|
||||
.\" ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
.\" SUCH DAMAGE.
|
||||
.\"
|
||||
.Dd $Mdocdate: October 28 2010 $
|
||||
.Dt FILE 1
|
||||
.Os
|
||||
.Sh NAME
|
||||
.Nm file
|
||||
.Nd determine file type
|
||||
.Sh SYNOPSIS
|
||||
.Nm
|
||||
.Bk -words
|
||||
.Op Fl 0bCcehikLNnprsvz
|
||||
.Op Fl -help
|
||||
.Op Fl -mime-encoding
|
||||
.Op Fl -mime-type
|
||||
.Op Fl F Ar separator
|
||||
.Op Fl f Ar namefile
|
||||
.Op Fl m Ar magicfiles
|
||||
.Ar file
|
||||
.Ek
|
||||
.Sh DESCRIPTION
|
||||
The
|
||||
.Nm
|
||||
utility tests each argument in an attempt to classify it.
|
||||
There are three sets of tests, performed in this order:
|
||||
filesystem tests, magic tests, and language tests.
|
||||
The first test that succeeds causes the file type to be printed.
|
||||
.Pp
|
||||
The type printed will usually contain one of the words
|
||||
.Em text
|
||||
(the file contains only
|
||||
printing characters and a few common control
|
||||
characters and is probably safe to read on an
|
||||
ASCII terminal),
|
||||
.Em executable
|
||||
(the file contains the result of compiling a program
|
||||
in a form understandable to some
|
||||
.Ux
|
||||
kernel or another),
|
||||
or
|
||||
.Em data
|
||||
meaning anything else (data is usually
|
||||
.Dq binary
|
||||
or non-printable).
|
||||
Exceptions are well-known file formats (core files, tar archives)
|
||||
that are known to contain binary data.
|
||||
When modifying magic files or the program itself, make sure to
|
||||
.Em preserve these keywords .
|
||||
Users depend on knowing that all the readable files in a directory
|
||||
have the word
|
||||
.Dq text
|
||||
printed.
|
||||
Don't do as Berkeley did and change
|
||||
.Dq shell commands text
|
||||
to
|
||||
.Dq shell script .
|
||||
.Pp
|
||||
The filesystem tests are based on examining the return from a
|
||||
.Xr 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,
|
||||
such as sockets, symbolic links, and named pipes (FIFOs),
|
||||
are intuited if they are defined in
|
||||
the system header file
|
||||
.Aq Pa sys/stat.h .
|
||||
.Pp
|
||||
The magic 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
|
||||
.Aq Pa elf.h ,
|
||||
.Aq Pa a.out.h ,
|
||||
and possibly
|
||||
.Aq Pa exec.h
|
||||
in the standard include directory.
|
||||
These files have a
|
||||
.Dq magic number
|
||||
stored in a particular place
|
||||
near the beginning of the file that tells the
|
||||
.Ux
|
||||
operating system
|
||||
that the file is a binary executable, and which of several types thereof.
|
||||
The concept of a
|
||||
.Dq magic
|
||||
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 magic file
|
||||
.Pa /etc/magic .
|
||||
In addition, if
|
||||
.Pa $HOME/.magic.mgc
|
||||
or
|
||||
.Pa $HOME/.magic
|
||||
exists, it will be used in preference to the system magic files.
|
||||
.Pp
|
||||
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
|
||||
.Dq text
|
||||
because they will be mostly readable on nearly any terminal;
|
||||
UTF-16 and EBCDIC are only
|
||||
.Dq character data
|
||||
because, while
|
||||
they contain text, it is text that will require translation
|
||||
before it can be read.
|
||||
In addition,
|
||||
.Nm
|
||||
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.
|
||||
.Pp
|
||||
Once
|
||||
.Nm
|
||||
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.\&
|
||||
.Aq Pa names.h )
|
||||
that can appear anywhere in the first few blocks of a file.
|
||||
For example, the keyword
|
||||
.Em .br
|
||||
indicates that the file is most likely a
|
||||
troff input file, just as the keyword
|
||||
.Em 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
|
||||
.Xr tar 1
|
||||
archives).
|
||||
.Pp
|
||||
Any file that cannot be identified as having been written
|
||||
in any of the character sets listed above is simply said to be
|
||||
.Dq data .
|
||||
.Sh OPTIONS
|
||||
.Bl -tag -width indent
|
||||
.It Fl 0 , -print0
|
||||
Output a null character
|
||||
.Sq \e0
|
||||
after the end of the filename.
|
||||
Nice to
|
||||
.Xr cut 1
|
||||
the output.
|
||||
This does not affect the separator which is still printed.
|
||||
.It Fl b , -brief
|
||||
Do not prepend filenames to output lines (brief mode).
|
||||
.It Fl C , -compile
|
||||
Write a
|
||||
.Pa magic.mgc
|
||||
output file that contains a pre-parsed version of the magic file or directory.
|
||||
.It Fl c , -checking-printout
|
||||
Cause a checking printout of the parsed form of the magic file.
|
||||
This is usually used in conjunction with the
|
||||
.Fl m
|
||||
flag to debug a new magic file before installing it.
|
||||
.It Fl e , -exclude Ar testname
|
||||
Exclude the test named in
|
||||
.Ar testname
|
||||
from the list of tests made to determine the file type.
|
||||
Valid test names are:
|
||||
.Bl -tag -width compress
|
||||
.It apptype
|
||||
Check for
|
||||
.Dv EMX
|
||||
application type (only on EMX).
|
||||
.It ascii
|
||||
Check for various types of ASCII files.
|
||||
.It compress
|
||||
Don't look for, or inside, compressed files.
|
||||
.It elf
|
||||
Don't print elf details.
|
||||
.It fortran
|
||||
Don't look for fortran sequences inside ASCII files.
|
||||
.It soft
|
||||
Don't consult magic files.
|
||||
.It tar
|
||||
Don't examine tar files.
|
||||
.It token
|
||||
Don't look for known tokens inside ASCII files.
|
||||
.It troff
|
||||
Don't look for troff sequences inside ASCII files.
|
||||
.El
|
||||
.It Fl F , -separator Ar separator
|
||||
Use the specified string as the separator between the filename and the
|
||||
file result returned.
|
||||
Defaults to
|
||||
.Sq \&: .
|
||||
.It Fl f , -files-from Ar namefile
|
||||
Read the names of the files to be examined from
|
||||
.Ar namefile
|
||||
(one per line)
|
||||
before the argument list.
|
||||
Either
|
||||
.Ar namefile
|
||||
or at least one filename argument must be present;
|
||||
to test the standard input, use
|
||||
.Sq -
|
||||
as a filename argument.
|
||||
.It Fl h , -no-dereference
|
||||
Causes symlinks not to be followed.
|
||||
This is the default if the environment variable
|
||||
.Dv POSIXLY_CORRECT
|
||||
is not defined.
|
||||
.It Fl -help
|
||||
Print a help message and exit.
|
||||
.It Fl i , -mime
|
||||
Causes the file command to output mime type strings rather than the more
|
||||
traditional human readable ones.
|
||||
Thus it may say
|
||||
.Dq text/plain charset=us-ascii
|
||||
rather than
|
||||
.Dq ASCII text .
|
||||
In order for this option to work,
|
||||
.Nm
|
||||
changes the way it handles files recognized by the command itself
|
||||
(such as many of the text file types, directories etc.),
|
||||
and makes use of an alternative
|
||||
.Dq magic
|
||||
file.
|
||||
See also
|
||||
.Sx FILES ,
|
||||
below.
|
||||
.It Fl -mime-encoding , -mime-type
|
||||
Like
|
||||
.Fl i ,
|
||||
but print only the specified element(s).
|
||||
.It Fl k , -keep-going
|
||||
Don't stop at the first match, keep going.
|
||||
Subsequent matches will have the string
|
||||
.Dq "\[rs]012\- "
|
||||
prepended.
|
||||
(If a newline is required, see the
|
||||
.Fl r
|
||||
option.)
|
||||
.It Fl L , -dereference
|
||||
Causes symlinks to be followed;
|
||||
analogous to the option of the same name in
|
||||
.Xr ls 1 .
|
||||
This is the default if the environment variable
|
||||
.Dv POSIXLY_CORRECT
|
||||
is defined.
|
||||
.It Fl m , -magic-file Ar magicfiles
|
||||
Specify an alternate list of files and directories containing magic.
|
||||
This can be a single item, or a colon-separated list.
|
||||
If a compiled magic file is found alongside a file or directory,
|
||||
it will be used instead.
|
||||
.It Fl N , -no-pad
|
||||
Don't pad filenames so that they align in the output.
|
||||
.It Fl n , -no-buffer
|
||||
Force stdout to be flushed after checking each file.
|
||||
This is only useful if checking a list of files.
|
||||
It is intended to be used by programs that want filetype output from a pipe.
|
||||
.It Fl p , -preserve-date
|
||||
On systems that support
|
||||
.Xr utime 3
|
||||
or
|
||||
.Xr utimes 2 ,
|
||||
attempt to preserve the access time of files analyzed, to pretend that
|
||||
.Nm
|
||||
never read them.
|
||||
.It Fl r , -raw
|
||||
Don't translate unprintable characters to \eooo.
|
||||
Normally
|
||||
.Nm
|
||||
translates unprintable characters to their octal representation.
|
||||
.It Fl s , -special-files
|
||||
Normally,
|
||||
.Nm
|
||||
only attempts to read and determine the type of argument files which
|
||||
.Xr stat 2
|
||||
reports are ordinary files.
|
||||
This prevents problems, because reading special files may have peculiar
|
||||
consequences.
|
||||
Specifying the
|
||||
.Fl s
|
||||
option causes
|
||||
.Nm
|
||||
to also read argument files which are block or character special files.
|
||||
This is useful for determining the filesystem types of the data in raw
|
||||
disk partitions, which are block special files.
|
||||
This option also causes
|
||||
.Nm
|
||||
to disregard the file size as reported by
|
||||
.Xr stat 2
|
||||
since on some systems it reports a zero size for raw disk partitions.
|
||||
.It Fl v , -version
|
||||
Print the version of the program and exit.
|
||||
.It Fl z , -uncompress
|
||||
Try to look inside compressed files.
|
||||
.El
|
||||
.Sh ENVIRONMENT
|
||||
The environment variable
|
||||
.Dv MAGIC
|
||||
can be used to set the default magic file name.
|
||||
If that variable is set, then
|
||||
.Nm
|
||||
will not attempt to open
|
||||
.Pa $HOME/.magic .
|
||||
.Nm
|
||||
adds
|
||||
.Dq .mgc
|
||||
to the value of this variable as appropriate.
|
||||
The environment variable
|
||||
.Dv POSIXLY_CORRECT
|
||||
controls whether
|
||||
.Nm
|
||||
will attempt to follow symlinks or not.
|
||||
If set, then
|
||||
.Nm
|
||||
follows symlinks; otherwise it does not.
|
||||
This is also controlled by the
|
||||
.Fl L
|
||||
and
|
||||
.Fl h
|
||||
options.
|
||||
.Sh FILES
|
||||
.Bl -tag -width /etc/magic -compact
|
||||
.It Pa /etc/magic
|
||||
default list of magic numbers
|
||||
.El
|
||||
.Sh EXIT STATUS
|
||||
.Ex -std file
|
||||
.Sh SEE ALSO
|
||||
.Xr hexdump 1 ,
|
||||
.Xr od 1 ,
|
||||
.Xr strings 1 ,
|
||||
.Xr magic 5
|
||||
.Sh STANDARDS CONFORMANCE
|
||||
This program is believed to exceed the System V Interface Definition
|
||||
of FILE(CMD), as near as one can determine from the vague language
|
||||
contained therein.
|
||||
Its behavior is mostly compatible with the System V program of the same name.
|
||||
This version knows more magic, however, so it will produce
|
||||
different (albeit more accurate) output in many cases.
|
||||
.\" URL: http://www.opengroup.org/onlinepubs/009695399/utilities/file.html
|
||||
.Pp
|
||||
The one significant difference
|
||||
between this version and System V
|
||||
is that this version treats any whitespace
|
||||
as a delimiter, so that spaces in pattern strings must be escaped.
|
||||
For example,
|
||||
.Bd -literal -offset indent
|
||||
\*(Gt10 string language impress\ (imPRESS data)
|
||||
.Ed
|
||||
.Pp
|
||||
in an existing magic file would have to be changed to
|
||||
.Bd -literal -offset indent
|
||||
\*(Gt10 string language\e impress (imPRESS data)
|
||||
.Ed
|
||||
.Pp
|
||||
In addition, in this version, if a pattern string contains a backslash,
|
||||
it must be escaped.
|
||||
For example
|
||||
.Bd -literal -offset indent
|
||||
0 string \ebegindata Andrew Toolkit document
|
||||
.Ed
|
||||
.Pp
|
||||
in an existing magic file would have to be changed to
|
||||
.Bd -literal -offset indent
|
||||
0 string \e\ebegindata Andrew Toolkit document
|
||||
.Ed
|
||||
.Pp
|
||||
SunOS releases 3.2 and later from Sun Microsystems include a
|
||||
.Nm
|
||||
command derived from the System V one, but with some extensions.
|
||||
This version differs from Sun's only in minor ways.
|
||||
It includes the extension of the
|
||||
.Sq &
|
||||
operator, used as,
|
||||
for example,
|
||||
.Bd -literal -offset indent
|
||||
\*(Gt16 long&0x7fffffff \*(Gt0 not stripped
|
||||
.Ed
|
||||
.Sh HISTORY
|
||||
There has been a
|
||||
.Nm
|
||||
command in every
|
||||
.Ux
|
||||
since at least Research Version 4
|
||||
(man page dated November, 1973).
|
||||
The System V version introduced one significant major change:
|
||||
the external list of magic types.
|
||||
This slowed the program down slightly but made it a lot more flexible.
|
||||
.Pp
|
||||
This program, based on the System V version,
|
||||
was written by Ian Darwin
|
||||
without looking at anybody else's source code.
|
||||
.Pp
|
||||
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, 1989.
|
||||
.Pp
|
||||
Guy Harris, made many changes from 1993 to the present.
|
||||
.Pp
|
||||
Primary development and maintenance from 1990 to the present by
|
||||
Christos Zoulas.
|
||||
.Pp
|
||||
Altered by Chris Lowth, 2000:
|
||||
Handle the
|
||||
.Fl i
|
||||
option to output mime type strings, using an alternative
|
||||
magic file and internal logic.
|
||||
.Pp
|
||||
Altered by Eric Fischer, July, 2000,
|
||||
to identify character codes and attempt to identify the languages
|
||||
of non-ASCII files.
|
||||
.Pp
|
||||
Altered by Reuben Thomas, 2007 to 2008, to improve MIME
|
||||
support and merge MIME and non-MIME magic, support directories as well
|
||||
as files of magic, apply many bug fixes and improve the build system.
|
||||
.Pp
|
||||
The list of contributors to the
|
||||
.Dq magic
|
||||
directory (magic files)
|
||||
is too long to include here.
|
||||
You know who you are; thank you.
|
||||
Many contributors are listed in the source files.
|
||||
.Sh BUGS
|
||||
.Pp
|
||||
There must be a better way to automate the construction of the Magic
|
||||
file from all the glop in Magdir.
|
||||
What is it?
|
||||
.Pp
|
||||
.Nm
|
||||
uses several algorithms that favor speed over accuracy,
|
||||
thus it can be misled about the contents of
|
||||
text
|
||||
files.
|
||||
.Pp
|
||||
The support for text files (primarily for programming languages)
|
||||
is simplistic, inefficient and requires recompilation to update.
|
||||
.Pp
|
||||
The list of keywords in
|
||||
.Pa ascmagic
|
||||
probably belongs in the Magic file.
|
||||
This could be done by using some keyword like
|
||||
.Sq *
|
||||
for the offset value.
|
||||
.Pp
|
||||
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?
|
||||
.Pp
|
||||
The program should provide a way to give an estimate
|
||||
of
|
||||
.Dq how good
|
||||
a guess is.
|
||||
We end up removing guesses (e.g.
|
||||
.Dq From\
|
||||
as first 5 chars of file) because
|
||||
they are not as good as other guesses (e.g.\&
|
||||
.Dq Newsgroups:
|
||||
versus
|
||||
.Dq Return-Path: ) .
|
||||
Still, if the others don't pan out, it should be possible to use the
|
||||
first guess.
|
||||
.Pp
|
||||
This manual page, and particularly this section, is too long.
|
@ -55,17 +55,13 @@ typedef unsigned long unichar;
|
||||
|
||||
struct stat;
|
||||
const char *file_fmttime(unsigned int, int);
|
||||
int file_buffer(struct r_magic_set *, int, const char *, const void *,
|
||||
size_t);
|
||||
int file_buffer(struct r_magic_set *, int, const char *, const void *, size_t);
|
||||
int file_fsmagic(struct r_magic_set *, const char *, struct stat *);
|
||||
int file_pipe2file(struct r_magic_set *, int, const void *, size_t);
|
||||
int file_printf(struct r_magic_set *, const char *, ...);
|
||||
int file_vprintf(struct r_magic_set *, const char *, va_list ap); // OPENBSDBUG
|
||||
int file_reset(struct r_magic_set *);
|
||||
int file_tryelf(struct r_magic_set *, int, const unsigned char *,
|
||||
size_t);
|
||||
int file_zmagic(struct r_magic_set *, int, const char *,
|
||||
const unsigned char *, size_t);
|
||||
int file_tryelf(struct r_magic_set *, int, const unsigned char *, size_t);
|
||||
int file_zmagic(struct r_magic_set *, int, const char *, const ut8*, size_t);
|
||||
int file_ascmagic(struct r_magic_set *, const unsigned char *, size_t);
|
||||
int file_is_tar(struct r_magic_set *, const unsigned char *, size_t);
|
||||
int file_softmagic(struct r_magic_set *, const unsigned char *, size_t, int);
|
||||
@ -86,11 +82,6 @@ ssize_t sread(int, void *, size_t, int);
|
||||
int file_check_mem(struct r_magic_set *, unsigned int);
|
||||
int file_looks_utf8(const unsigned char *, size_t, unichar *, size_t *);
|
||||
|
||||
/*
|
||||
extern const char *magic_file_names[FILE_NAMES_SIZE];
|
||||
extern const size_t file_nnames;
|
||||
*/
|
||||
|
||||
#ifndef HAVE_VASPRINTF
|
||||
int vasprintf(char **ptr, const char *format_string, va_list vargs);
|
||||
#endif
|
||||
@ -98,12 +89,8 @@ int vasprintf(char **ptr, const char *format_string, va_list vargs);
|
||||
int asprintf(char **ptr, const char *format_string, ...);
|
||||
#endif
|
||||
|
||||
#if defined(HAVE_MMAP) && defined(HAVE_SYS_MMAN_H) && !defined(QUICK)
|
||||
#define QUICK
|
||||
#endif
|
||||
|
||||
#ifndef O_BINARY
|
||||
#define O_BINARY 0
|
||||
#define O_BINARY 0
|
||||
#endif
|
||||
|
||||
#endif /* __file_h__ */
|
||||
|
@ -40,21 +40,8 @@
|
||||
#include <wchar.h>
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Like printf, only we append to a buffer.
|
||||
*/
|
||||
int file_printf(RMagic *ms, const char *fmt, ...) {
|
||||
va_list ap;
|
||||
int ret;
|
||||
|
||||
va_start (ap, fmt);
|
||||
ret = file_vprintf (ms, fmt, ap);
|
||||
va_end (ap);
|
||||
return ret;
|
||||
}
|
||||
|
||||
// copypasta to fix an OPENBSDBUG
|
||||
int file_vprintf(RMagic *ms, const char *fmt, va_list ap) {
|
||||
static int file_vprintf(RMagic *ms, const char *fmt, va_list ap) {
|
||||
va_list ap2;
|
||||
int len;
|
||||
char cbuf[4096];
|
||||
@ -101,6 +88,19 @@ out:
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Like printf, only we append to a buffer.
|
||||
*/
|
||||
int file_printf(RMagic *ms, const char *fmt, ...) {
|
||||
va_list ap;
|
||||
int ret;
|
||||
|
||||
va_start (ap, fmt);
|
||||
ret = file_vprintf (ms, fmt, ap);
|
||||
va_end (ap);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
* error - print best error message possible
|
||||
*/
|
||||
|
@ -1,123 +1,48 @@
|
||||
/* radare - Copyright 2011 pancake<nopcode.org> */
|
||||
|
||||
/* radare - Copyright 2011-2019 pancake<nopcode.org> */
|
||||
/* $OpenBSD: magic.c,v 1.8 2009/10/27 23:59:37 deraadt Exp $ */
|
||||
/*
|
||||
* Copyright (c) Christos Zoulas 2003.
|
||||
* All Rights Reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice immediately at the beginning of the file, without modification,
|
||||
* this list of conditions, and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
|
||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <r_userconf.h>
|
||||
#include <r_types.h>
|
||||
#include <r_magic.h>
|
||||
|
||||
R_LIB_VERSION (r_magic);
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#include <io.h>
|
||||
#include <sys\stat.h>
|
||||
#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
|
||||
#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
|
||||
#define S_IFIFO (-1)
|
||||
#define S_ISFIFO(m) (((m) & S_IFIFO) == S_IFIFO)
|
||||
#define MAXPATHLEN 255
|
||||
# include <io.h>
|
||||
# include <sys\stat.h>
|
||||
# define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
|
||||
# define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
|
||||
# define S_IFIFO (-1)
|
||||
# define S_ISFIFO(m) (((m) & S_IFIFO) == S_IFIFO)
|
||||
# define MAXPATHLEN 255
|
||||
#endif
|
||||
|
||||
#if USE_LIB_MAGIC
|
||||
|
||||
// we keep this code just to make debian happy, but we should use
|
||||
// our own magic implementation for consistency reasons
|
||||
#include <magic.h>
|
||||
#define RMagic void
|
||||
#undef R_API
|
||||
#define R_API
|
||||
|
||||
R_API RMagic* r_magic_new(int flags) {
|
||||
return magic_open (flags);
|
||||
}
|
||||
|
||||
R_API void r_magic_free(RMagic* m) {
|
||||
#if !USE_LIB_MAGIC
|
||||
free (m->magic);
|
||||
#endif
|
||||
if (m) magic_close (m);
|
||||
}
|
||||
|
||||
R_API const char *r_magic_file(RMagic* m, const char * f) {
|
||||
return magic_file (m, f);
|
||||
}
|
||||
|
||||
R_API const char *r_magic_descriptor(RMagic* m, int fd) {
|
||||
return magic_descriptor (m, fd);
|
||||
}
|
||||
|
||||
R_API const char *r_magic_buffer(RMagic* m, const void *b, size_t s) {
|
||||
return magic_buffer (m, b, s);
|
||||
}
|
||||
|
||||
R_API const char *r_magic_error(RMagic* m) {
|
||||
return magic_error (m);
|
||||
}
|
||||
|
||||
R_API void r_magic_setflags(RMagic* m, int f) {
|
||||
magic_setflags (m, f);
|
||||
}
|
||||
|
||||
R_API int r_magic_load(RMagic* m, const char *f) {
|
||||
return magic_load (m, f);
|
||||
}
|
||||
|
||||
R_API int r_magic_compile(RMagic* m, const char *x) {
|
||||
return magic_compile (m, x);
|
||||
}
|
||||
|
||||
R_API int r_magic_check(RMagic* m, const char *x) {
|
||||
return magic_check (m, x);
|
||||
}
|
||||
|
||||
R_API int r_magic_errno(RMagic* m) {
|
||||
return magic_errno (m);
|
||||
}
|
||||
R_API RMagic* r_magic_new(int flags) { return magic_open (flags); }
|
||||
R_API void r_magic_free(RMagic* m) { if (m) { magic_close (m); } }
|
||||
R_API const char *r_magic_file(RMagic* m, const char * f) { return magic_file (m, f); }
|
||||
R_API const char *r_magic_descriptor(RMagic* m, int fd) { return magic_descriptor (m, fd); }
|
||||
R_API const char *r_magic_buffer(RMagic* m, const void *b, size_t s) { return magic_buffer (m, b, s); }
|
||||
R_API const char *r_magic_error(RMagic* m) { return magic_error (m); }
|
||||
R_API void r_magic_setflags(RMagic* m, int f) { magic_setflags (m, f); }
|
||||
R_API bool r_magic_load(RMagic* m, const char *f) { return magic_load (m, f) != -1; }
|
||||
R_API bool r_magic_compile(RMagic* m, const char *x) { return magic_compile (m, x) != -1; }
|
||||
R_API bool r_magic_check(RMagic* m, const char *x) { return magic_check (m, x) != -1; }
|
||||
R_API int r_magic_errno(RMagic* m) { return magic_errno (m); }
|
||||
|
||||
#else
|
||||
|
||||
#ifndef _MSC_VER
|
||||
#include <sys/param.h> /* for MAXPATHLEN */
|
||||
#endif
|
||||
#include <r_magic.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
R_LIB_VERSION (r_magic);
|
||||
/* use embedded magic library */
|
||||
|
||||
#include "file.h"
|
||||
|
||||
#ifdef QUICK
|
||||
#include <sys/mman.h>
|
||||
#endif
|
||||
#include <limits.h> /* for PIPE_BUF */
|
||||
|
||||
#if __UNIX__
|
||||
#include <netinet/in.h> /* for byte swapping */
|
||||
#else
|
||||
#undef O_NONBLOCK
|
||||
#endif
|
||||
|
||||
#include "patchlevel.h"
|
||||
|
||||
#ifndef PIPE_BUF
|
||||
/* Get the PIPE_BUF from pathconf */
|
||||
#ifdef _PC_PIPE_BUF
|
||||
@ -166,16 +91,17 @@ static int info_from_stat(RMagic *ms, unsigned short md) {
|
||||
}
|
||||
|
||||
static void close_and_restore (const RMagic *ms, const char *name, int fd, const struct stat *sb) {
|
||||
if (fd > 0) {
|
||||
if (fd >= 0) {
|
||||
close (fd);
|
||||
}
|
||||
}
|
||||
|
||||
static const char *file_or_fd(RMagic *ms, const char *inname, int fd) {
|
||||
int ispipe = 0, rv = -1;
|
||||
bool ispipe = false;
|
||||
int rv = -1;
|
||||
unsigned char *buf;
|
||||
struct stat sb;
|
||||
int nbytes = 0; /* number of bytes read from a datafile */
|
||||
int nbytes = 0; /* number of bytes read from a datafile */
|
||||
|
||||
/*
|
||||
* one extra for terminating '\0', and
|
||||
@ -198,7 +124,7 @@ static const char *file_or_fd(RMagic *ms, const char *inname, int fd) {
|
||||
|
||||
if (!inname) {
|
||||
if (fstat (fd, &sb) == 0 && S_ISFIFO (sb.st_mode)) {
|
||||
ispipe = 1;
|
||||
ispipe = true;
|
||||
}
|
||||
} else {
|
||||
int flags = O_RDONLY|O_BINARY;
|
||||
@ -207,7 +133,7 @@ static const char *file_or_fd(RMagic *ms, const char *inname, int fd) {
|
||||
#if O_NONBLOCK
|
||||
flags |= O_NONBLOCK;
|
||||
#endif
|
||||
ispipe = 1;
|
||||
ispipe = true;
|
||||
}
|
||||
errno = 0;
|
||||
if ((fd = open (inname, flags)) < 0) {
|
||||
@ -294,36 +220,49 @@ R_API RMagic* r_magic_new(int flags) {
|
||||
}
|
||||
|
||||
R_API void r_magic_free(RMagic *ms) {
|
||||
if (!ms) {
|
||||
return;
|
||||
if (ms) {
|
||||
free_mlist (ms->mlist);
|
||||
free (ms->o.pbuf);
|
||||
free (ms->o.buf);
|
||||
free (ms->c.li);
|
||||
free (ms);
|
||||
}
|
||||
free_mlist (ms->mlist);
|
||||
free (ms->o.pbuf);
|
||||
free (ms->o.buf);
|
||||
free (ms->c.li);
|
||||
free (ms);
|
||||
}
|
||||
|
||||
R_API int r_magic_load(RMagic* ms, const char *magicfile) {
|
||||
R_API bool r_magic_load_buffer(RMagic* ms, const char *magicdata) {
|
||||
if (*magicdata == '#') {
|
||||
struct mlist *ml = file_apprentice (ms, magicdata, FILE_LOAD);
|
||||
if (ml) {
|
||||
free_mlist (ms->mlist);
|
||||
ms->mlist = ml;
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
eprintf ("Magic buffers should start with #\n");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
R_API bool r_magic_load(RMagic* ms, const char *magicfile) {
|
||||
struct mlist *ml = file_apprentice (ms, magicfile, FILE_LOAD);
|
||||
if (ml) {
|
||||
free_mlist (ms->mlist);
|
||||
ms->mlist = ml;
|
||||
return 0;
|
||||
return true;
|
||||
}
|
||||
return -1;
|
||||
return false;
|
||||
}
|
||||
|
||||
R_API int r_magic_compile(RMagic *ms, const char *magicfile) {
|
||||
R_API bool r_magic_compile(RMagic *ms, const char *magicfile) {
|
||||
struct mlist *ml = file_apprentice (ms, magicfile, FILE_COMPILE);
|
||||
free_mlist (ml);
|
||||
return ml ? 0 : -1;
|
||||
return ml != NULL;
|
||||
}
|
||||
|
||||
R_API int r_magic_check(RMagic *ms, const char *magicfile) {
|
||||
R_API bool r_magic_check(RMagic *ms, const char *magicfile) {
|
||||
struct mlist *ml = file_apprentice (ms, magicfile, FILE_CHECK);
|
||||
free_mlist (ml);
|
||||
return ml ? 0 : -1;
|
||||
return ml != NULL;
|
||||
}
|
||||
|
||||
R_API const char* r_magic_descriptor(RMagic *ms, int fd) {
|
||||
@ -338,28 +277,24 @@ R_API const char * r_magic_buffer(RMagic *ms, const void *buf, size_t nb) {
|
||||
if (file_reset (ms) == -1) {
|
||||
return NULL;
|
||||
}
|
||||
/*
|
||||
* The main work is done here!
|
||||
* We have the file name and/or the data buffer to be identified.
|
||||
*/
|
||||
if (file_buffer (ms, -1, NULL, buf, nb) == -1) {
|
||||
return NULL;
|
||||
}
|
||||
return file_getbuffer (ms);
|
||||
}
|
||||
|
||||
R_API const char * r_magic_error(RMagic *ms) {
|
||||
if (!ms) {
|
||||
return 0;
|
||||
R_API const char *r_magic_error(RMagic *ms) {
|
||||
if (ms && ms->haderr) {
|
||||
return ms->o.buf;
|
||||
}
|
||||
return ms->haderr ? ms->o.buf : NULL;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
R_API int r_magic_errno(RMagic *ms) {
|
||||
if (!ms) {
|
||||
return 0;
|
||||
if (ms && ms->haderr) {
|
||||
return ms->error;
|
||||
}
|
||||
return ms->haderr ? ms->error : 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
R_API void r_magic_setflags(RMagic *ms, int flags) {
|
||||
|
@ -2,347 +2,3 @@
|
||||
|
||||
#define FILE_VERSION_MAJOR 4
|
||||
#define patchlevel 24
|
||||
|
||||
/*
|
||||
* Patchlevel file for Ian Darwin's MAGIC command.
|
||||
* $File: patchlevel.h,v 1.68 2008/03/22 21:39:43 christos Exp $
|
||||
*
|
||||
* $Log: patchlevel.h,v $
|
||||
* Revision 1.9 2009/04/24 18:54:34 chl
|
||||
* file update to 4.24
|
||||
*
|
||||
* The '-i' switch is now enabled so file(1) can output mime type strings.
|
||||
*
|
||||
* ok ian@
|
||||
* builk ports build test on amd64 by jasper@
|
||||
*
|
||||
* ok ray@ gilles@ on a almost identical diff
|
||||
* builk ports build test on sparc64 on this almost identical diff by ajacoutot@
|
||||
* also tested by landry@
|
||||
*
|
||||
* Revision 1.68 2008/03/22 21:39:43 christos
|
||||
* file 4.24
|
||||
*
|
||||
* Revision 1.67 2007/12/28 20:08:40 christos
|
||||
* welcome to 4.23.
|
||||
*
|
||||
* Revision 1.66 2007/12/27 16:38:24 christos
|
||||
* welcome to 4.22
|
||||
*
|
||||
* Revision 1.65 2007/05/24 17:22:27 christos
|
||||
* Welcome to 4.21
|
||||
*
|
||||
* Revision 1.64 2007/03/01 22:14:55 christos
|
||||
* welcome to 4.20
|
||||
*
|
||||
* Revision 1.63 2007/01/12 17:38:28 christos
|
||||
* Use File id.
|
||||
*
|
||||
* Revision 1.62 2006/12/11 21:49:58 christos
|
||||
* time for 4.19
|
||||
*
|
||||
* Revision 1.61 2006/10/31 21:18:09 christos
|
||||
* bump
|
||||
*
|
||||
* Revision 1.60 2006/03/02 22:15:12 christos
|
||||
* welcome to 4.17
|
||||
*
|
||||
* Revision 1.59 2005/10/17 17:15:21 christos
|
||||
* welcome to 4.16
|
||||
*
|
||||
* Revision 1.58 2005/08/18 15:52:56 christos
|
||||
* welcome to 4.15
|
||||
*
|
||||
* Revision 1.57 2005/06/25 15:52:14 christos
|
||||
* Welcome to 4.14
|
||||
*
|
||||
* Revision 1.56 2005/02/09 19:25:13 christos
|
||||
* Welcome to 4.13
|
||||
*
|
||||
* Revision 1.55 2004/11/24 18:57:47 christos
|
||||
* Re-do the autoconf stuff once more; passes make dist now.
|
||||
*
|
||||
* Revision 1.54 2004/11/21 05:52:05 christos
|
||||
* ready for 4.11
|
||||
*
|
||||
* Revision 1.53 2004/07/24 20:40:46 christos
|
||||
* welcome to 4.10
|
||||
*
|
||||
* Revision 1.52 2004/04/07 00:32:25 christos
|
||||
* welcome to 4.09
|
||||
*
|
||||
* Revision 1.51 2004/03/22 21:17:11 christos
|
||||
* welcome to 4.08.
|
||||
*
|
||||
* Revision 1.50 2003/12/23 17:34:04 christos
|
||||
* 4.07
|
||||
*
|
||||
* Revision 1.49 2003/10/15 02:08:27 christos
|
||||
* welcome to 4.06
|
||||
*
|
||||
* Revision 1.48 2003/09/12 19:41:14 christos
|
||||
* this is 4.04
|
||||
*
|
||||
* Revision 1.47 2003/05/23 21:38:21 christos
|
||||
* welcome to 4.03
|
||||
*
|
||||
* Revision 1.46 2003/04/02 18:57:43 christos
|
||||
* prepare for 4.02
|
||||
*
|
||||
* Revision 1.45 2003/03/26 15:37:25 christos
|
||||
* - Pass lint
|
||||
* - make NULL in magic_file mean stdin
|
||||
* - Fix "-" argument to file to pass NULL to magic_file
|
||||
* - avoid pointer casts by using memcpy
|
||||
* - rename magic_buf -> magic_buffer
|
||||
* - keep only the first error
|
||||
* - manual page: new sentence, new line
|
||||
* - fix typo in api function (magic_buf -> magic_buffer)
|
||||
*
|
||||
* Revision 1.44 2003/03/23 22:23:31 christos
|
||||
* finish librarification.
|
||||
*
|
||||
* Revision 1.43 2003/03/23 21:16:26 christos
|
||||
* update copyrights.
|
||||
*
|
||||
* Revision 1.42 2003/03/23 04:06:05 christos
|
||||
* Library re-organization
|
||||
*
|
||||
* Revision 1.41 2003/02/27 20:53:45 christos
|
||||
* - fix memory allocation problem (Jeff Johnson)
|
||||
* - fix stack overflow corruption (David Endler)
|
||||
* - fixes from NetBSD source (Antti Kantee)
|
||||
* - magic fixes
|
||||
*
|
||||
* Revision 1.40 2003/02/08 18:33:53 christos
|
||||
* - detect inttypes.h too (Dave Love <d.love@dl.ac.uk>)
|
||||
* - eliminate unsigned char warnings (Petter Reinholdtsen <pere@hungry.com>)
|
||||
* - better elf PT_NOTE handling (Nalin Dahyabhai <nalin@redhat.com>)
|
||||
* - add options to format the output differently
|
||||
* - much more magic.
|
||||
*
|
||||
* Revision 1.39 2002/07/03 18:57:52 christos
|
||||
* - ansify/c99ize
|
||||
* - more magic
|
||||
* - better COMPILE_ONLY support.
|
||||
* - new magic files.
|
||||
* - fix solaris compilation problems.
|
||||
*
|
||||
* Revision 1.38 2002/05/16 18:45:56 christos
|
||||
* - pt_note elf additions from NetBSD
|
||||
* - EMX os specific changes (Alexander Mai)
|
||||
* - stdint.h detection, acconfig.h fixes (Maciej W. Rozycki, Franz Korntner)
|
||||
* - regex file additions (Kim Cromie)
|
||||
* - getopt_long support and misc cleanups (Michael Piefel)
|
||||
* - many magic fixes and additions
|
||||
*
|
||||
* Revision 1.37 2001/09/03 14:44:22 christos
|
||||
* daylight/tm_isdst detection
|
||||
* magic fixes
|
||||
* don't eat the whole file if it has only nulls
|
||||
*
|
||||
* Revision 1.36 2001/07/22 21:04:15 christos
|
||||
* - magic fixes
|
||||
* - add new operators, pascal strings, UTC date printing, $HOME/.magic
|
||||
* [from "Tom N Harris" <telliamed@mac.com>]
|
||||
*
|
||||
* Revision 1.35 2001/04/24 14:40:25 christos
|
||||
* - rename magic file sgi to mips and fix it
|
||||
* - add support for building magic.mgc
|
||||
* - portability fixes for mmap()
|
||||
* - try gzip before uncompress, because uncompress sometimes hangs
|
||||
* - be more conservative about pipe reads and writes
|
||||
* - many magic fixes
|
||||
*
|
||||
* Revision 1.34 2001/03/12 05:05:57 christos
|
||||
* - new compiled magic format
|
||||
* - lots of magic additions
|
||||
*
|
||||
* Revision 1.33 2000/11/13 00:30:50 christos
|
||||
* - wordperfect magic fix: freebsd pr 9388
|
||||
* - more msdos fixes from freebsd pr's 20131 and 20812
|
||||
* - sas and spss magic [Bruce Foster]
|
||||
* - mkinstalldirs [John Fremlin]
|
||||
* - sgi opengl fixes [Michael Pruett]
|
||||
* - netbsd magic fixes [Ignatios Souvatzis]
|
||||
* - audio additions [Michael Pruett]
|
||||
* - fix problem with non ansi RCSID [Andreas Ley]
|
||||
* - oggs magic [Felix von Leitner]
|
||||
* - gmon magic [Eugen Dedu]
|
||||
* - TNEF magic [Joomy]
|
||||
* - netpbm magic and misc other image stuff [Bryan Henderson]
|
||||
*
|
||||
* Revision 1.32 2000/08/05 18:24:18 christos
|
||||
* Correct indianness detection in elf (Charles Hannum)
|
||||
* FreeBSD elf core support (Guy Harris)
|
||||
* Use gzip in systems that don't have uncompress (Anthon van der Neut)
|
||||
* Internationalization/EBCDIC support (Eric Fisher)
|
||||
* Many many magic changes
|
||||
*
|
||||
* Revision 1.31 2000/05/14 17:58:36 christos
|
||||
* - new magic for claris files
|
||||
* - new magic for mathematica and maple files
|
||||
* - new magic for msvc files
|
||||
* - new -k flag to keep going matching all possible entries
|
||||
* - add the word executable on #! magic files, and fix the usage of
|
||||
* the word script
|
||||
* - lots of other magic fixes
|
||||
* - fix typo test -> text
|
||||
*
|
||||
* Revision 1.30 2000/04/11 02:41:17 christos
|
||||
* - add support for mime output (-i)
|
||||
* - make sure we free memory in case realloc fails
|
||||
* - magic fixes
|
||||
*
|
||||
* Revision 1.29 1999/11/28 20:02:29 christos
|
||||
* new string/[Bcb] magic from anthon, and adjustments to the magic files to
|
||||
* use it.
|
||||
*
|
||||
* Revision 1.28 1999/10/31 22:11:48 christos
|
||||
* - add "char" type for compatibility with HP/UX
|
||||
* - recognize HP/UX syntax &=n etc.
|
||||
* - include errno.h for CYGWIN
|
||||
* - conditionalize the S_IS* macros
|
||||
* - revert the SHT_DYNSYM test that broke the linux stripped binaries test
|
||||
* - lots of Magdir changes
|
||||
*
|
||||
* Revision 1.27 1999/02/14 17:21:41 christos
|
||||
* Automake support and misc cleanups from Rainer Orth
|
||||
* Enable reading character and block special files from Dale R. Worley
|
||||
*
|
||||
* Revision 1.26 1998/09/12 13:19:39 christos
|
||||
* - add support for bi-endian indirect offsets (Richard Verhoeven)
|
||||
* - add recognition for bcpl (Joseph Myers)
|
||||
* - remove non magic files from Magdir to avoid difficulties building
|
||||
* on os2 where files are case independent
|
||||
* - magic fixes.
|
||||
*
|
||||
* Revision 1.25 1998/06/27 14:04:04 christos
|
||||
* OLF patch Guy Harris
|
||||
* Recognize java/html (debian linux)
|
||||
* Const poisoning (debian linux)
|
||||
* More magic!
|
||||
*
|
||||
* Revision 1.24 1998/02/15 23:20:38 christos
|
||||
* Autoconf patch: Felix von Leitner <leitner@math.fu-berlin.de>
|
||||
* More magic fixes
|
||||
* Elf64 fixes
|
||||
*
|
||||
* Revision 1.23 1997/11/05 16:03:37 christos
|
||||
* - correct elf prps offset for SunOS-2.5.1 [guy@netapp.com]
|
||||
* - handle 64 bit time_t's correctly [ewt@redhat.com]
|
||||
* - new mime style magic [clarosse@netvista.net]
|
||||
* - new TI calculator magic [rmcguire@freenet.columbus.oh.us]
|
||||
* - new figlet fonts [obrien@freebsd.org]
|
||||
* - new cisco magic, and elf fixes [jhawk@bbnplanet.com]
|
||||
* - -b flag addition, and x86 filesystem magic [vax@linkhead.paranoia.com]
|
||||
* - s/Mpeg/MPEG, header and elf typo fixes [guy@netapp.com]
|
||||
* - Windows/NT registry files, audio code [guy@netapp.com]
|
||||
* - libGrx graphics lib fonts [guy@netapp.com]
|
||||
* - PNG fixes [guy@netapp.com]
|
||||
* - more m$ document magic [guy@netapp.com]
|
||||
* - PPD files [guy@netapp.com]
|
||||
* - archive magic cleanup [guy@netapp.com]
|
||||
* - linux kernel magic cleanup [guy@netapp.com]
|
||||
* - lecter magic [guy@netapp.com]
|
||||
* - vgetty magic [guy@netapp.com]
|
||||
* - sniffer additions [guy@netapp.com]
|
||||
*
|
||||
* Revision 1.22 1997/01/15 17:23:24 christos
|
||||
* - add support for elf core files: find the program name under SVR4 [Ken Pizzini]
|
||||
* - print strings only up to the first carriage return [various]
|
||||
* - freebsd international ascii support [J Wunsch]
|
||||
* - magic fixes and additions [Guy Harris]
|
||||
* - 64 bit fixes [Larry Schwimmer]
|
||||
* - support for both utime and utimes, but don't restore file access times
|
||||
* by default [various]
|
||||
* - \xXX only takes 2 hex digits, not 3.
|
||||
* - re-implement support for core files [Guy Harris]
|
||||
*
|
||||
* Revision 1.21 1996/10/05 18:15:29 christos
|
||||
* Segregate elf stuff and conditionally enable it with -DBUILTIN_ELF
|
||||
* More magic fixes
|
||||
*
|
||||
* Revision 1.20 1996/06/22 22:15:52 christos
|
||||
* - support relative offsets of the form >&
|
||||
* - fix bug with truncating magic strings that contain \n
|
||||
* - file -f - did not read from stdin as documented
|
||||
* - support elf file parsing using our own elf support.
|
||||
* - as always magdir fixes and additions.
|
||||
*
|
||||
* Revision 1.19 1995/10/27 23:14:46 christos
|
||||
* Ability to parse colon separated list of magic files
|
||||
* New LEGAL.NOTICE
|
||||
* Various magic file changes
|
||||
*
|
||||
* Revision 1.18 1995/05/20 22:09:21 christos
|
||||
* Passed incorrect argument to eatsize().
|
||||
* Use %ld and %lx where appropriate.
|
||||
* Remove unused variables
|
||||
* ELF support for both big and little endian
|
||||
* Fixes for small files again.
|
||||
*
|
||||
* Revision 1.17 1995/04/28 17:29:13 christos
|
||||
* - Incorrect nroff detection fix from der Mouse
|
||||
* - Lost and incorrect magic entries.
|
||||
* - Added ELF stripped binary detection [in C; ugh]
|
||||
* - Look for $MAGIC to find the magic file.
|
||||
* - Eat trailing size specifications from numbers i.e. ignore 10L
|
||||
* - More fixes for very short files
|
||||
*
|
||||
* Revision 1.16 1995/03/25 22:06:45 christos
|
||||
* - use strtoul() where it exists.
|
||||
* - fix sign-extend bug
|
||||
* - try to detect tar archives before nroff files, otherwise
|
||||
* tar files where the first file starts with a . will not work
|
||||
*
|
||||
* Revision 1.15 1995/01/21 21:03:35 christos
|
||||
* Added CSECTION for the file man page
|
||||
* Added version flag -v
|
||||
* Fixed bug with -f input flag (from iorio@violet.berkeley.edu)
|
||||
* Lots of magic fixes and reorganization...
|
||||
*
|
||||
* Revision 1.14 1994/05/03 17:58:23 christos
|
||||
* changes from mycroft@gnu.ai.mit.edu (Charles Hannum) for unsigned
|
||||
*
|
||||
* Revision 1.13 1994/01/21 01:27:01 christos
|
||||
* Fixed null termination bug from Don Seeley at BSDI in ascmagic.c
|
||||
*
|
||||
* Revision 1.12 1993/10/27 20:59:05 christos
|
||||
* Changed -z flag to understand gzip format too.
|
||||
* Moved builtin compression detection to a table, and move
|
||||
* the compress magic entry out of the source.
|
||||
* Made printing of numbers unsigned, and added the mask to it.
|
||||
* Changed the buffer size to 8k, because gzip will refuse to
|
||||
* unzip just a few bytes.
|
||||
*
|
||||
* Revision 1.11 1993/09/24 18:49:06 christos
|
||||
* Fixed small bug in softmagic.c introduced by
|
||||
* copying the data to be examined out of the input
|
||||
* buffer. Changed the Makefile to use sed to create
|
||||
* the correct man pages.
|
||||
*
|
||||
* Revision 1.10 1993/09/23 21:56:23 christos
|
||||
* Passed purify. Fixed indirections. Fixed byte order printing.
|
||||
* Fixed segmentation faults caused by referencing past the end
|
||||
* of the magic buffer. Fixed bus errors caused by referencing
|
||||
* unaligned shorts or longs.
|
||||
*
|
||||
* Revision 1.9 1993/03/24 14:23:40 ian
|
||||
* Batch of minor changes from several contributors.
|
||||
*
|
||||
* Revision 1.8 93/02/19 15:01:26 ian
|
||||
* Numerous changes from Guy Harris too numerous to mention but including
|
||||
* byte-order independance, fixing "old-style masking", etc. etc. A bugfix
|
||||
* for broken symlinks from martin@@d255s004.zfe.siemens.de.
|
||||
*
|
||||
* Revision 1.7 93/01/05 14:57:27 ian
|
||||
* Couple of nits picked by Christos (again, thanks).
|
||||
*
|
||||
* Revision 1.6 93/01/05 13:51:09 ian
|
||||
* Lotsa work on the Magic directory.
|
||||
*
|
||||
* Revision 1.5 92/09/14 14:54:51 ian
|
||||
* Fix a tiny null-pointer bug in previous fix for tar archive + uncompress.
|
||||
*
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user