+2003-05-23 17:03 Christos Zoulas <christos@zoulas.com>
+
+ * documentation fixes from Michael Piefel <piefel@debian.org>
+ * magic fixes (various)
+ * revert basename magic in .mgc name determination
+ * buffer protection in uncompress,
+ signness issues,
+ close files
+ Maciej W. Rozycki <macro@ds2.pg.gda.pl
+
2003-04-21 20:12 Christos Zoulas <christos@zoulas.com>
* fix zsh magic
** README for file(1) Command **
-@(#) $Id: README,v 1.32 2003/03/29 21:40:53 ian Exp $
+@(#) $Id: README,v 1.33 2003/05/23 21:31:56 christos Exp $
This is Release 4.x of Ian Darwin's (copyright but distributable)
file(1) command. This version is the standard "file" command for Linux,
for easier(?) maintenance. I will act as a clearinghouse for
magic numbers assigned to all sorts of data files that
are in reasonable circulation. Send your magic numbers,
-in magic(4) format please, to the maintainer, Christos Zoulas.
+in magic(5) format please, to the maintainer, Christos Zoulas.
LEGAL.NOTICE - read this first.
README - read this second (you are currently reading this file).
.TH FILE __CSECTION__ "Copyright but distributable"
-.\" $Id: file.man,v 1.48 2003/03/27 22:47:13 christos Exp $
+.\" $Id: file.man,v 1.49 2003/05/23 21:31:57 christos Exp $
.SH NAME
file
\- determine file type
in any of the character sets listed above is simply said to be ``data''.
.SH OPTIONS
.TP 8
-.B \-b
+.B "\-b, \-\-brief"
Do not prepend filenames to output lines (brief mode).
.TP 8
-.B \-c
+.B "\-c, \-\-checking\-printout"
Cause a checking printout of the parsed form of the magic file.
This is usually used in conjunction with
.B \-m
to debug a new magic file before installing it.
.TP 8
-.B \-C
+.B "\-C, \-\-compile"
Write a magic.mgc output file that contains a pre-parsed version of
file.
.TP 8
-.BI \-f " namefile"
+.BI "\-f, \-\-files\-from" " namefile"
Read the names of the files to be examined from
.I namefile
(one per line)
or at least one filename argument must be present;
to test the standard input, use ``\-'' as a filename argument.
.TP 8
-.BI \-F " separator"
+.BI "\-F, \-\-separator" " separator"
Use the specified string as the separator between the filename and the
file result returned. Defaults to ``:''.
.TP 8
-.B \-i
+.B "\-i, \-\-mime"
Causes the file command to output mime type strings rather than the more
traditional human readable ones. Thus it may say
``text/plain; charset=us-ascii''
``magic'' file.
(See ``FILES'' section, below).
.TP 8
-.B \-k
+.B "\-k, \-\-keep\-going"
Don't stop at the first match, keep going.
.TP 8
-.B \-L
+.B "\-L, \-\-dereference"
option causes symlinks to be followed, as the like-named option in
.BR ls (1).
(on systems that support symbolic links).
.TP 8
-.BI \-m " list"
+.BI "\-m, \-\-magic\-file" " list"
Specify an alternate list of files containing magic numbers.
This can be a single file, or a colon-separated list of files.
+If a compiled magic file is found alongside, it will be used instead.
.TP 8
-.B \-n
+.B "\-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.
.TP 8
-.B \-N
+.B "\-N, \-\-no\-pad"
Don't pad filenames so that they align in the output.
.TP 8
-.B \-s
+.B "\-s, \-\-special\-files"
Normally,
.B file
only attempts to read and determine the type of argument files which
.BR stat (2)
since on some systems it reports a zero size for raw disk partitions.
.TP 8
-.B \-v
+.B "\-v, \-\-version"
Print the version of the program and exit.
.TP 8
-.B \-z
+.B "\-z, \-\-uncompress"
Try to look inside compressed files.
+.TP 8
+.B "\-\-help"
+Print a help message and exit.
.SH FILES
+.TP
.I __MAGIC__.mgc
-\- default compiled list of magic numbers
-.PP
+Default compiled list of magic numbers
+.TP
.I __MAGIC__
-\- default list of magic numbers
-.PP
+Default list of magic numbers
+.TP
.I __MAGIC__.mime.mgc
-\- default compiled list of magic numbers, used to output mime types when
+Default compiled list of magic numbers, used to output mime types when
the -i option is specified.
-.PP
+.TP
.I __MAGIC__.mime
-\- default list of magic numbers, used to output mime types when the -i option
+Default list of magic numbers, used to output mime types when the -i option
is specified.
+.TP
+.I /etc/magic
+Local additions to magic wisdom.
.SH ENVIRONMENT
The environment variable
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"
+If the magic has
+.I n
+consecutive blanks, the target needs at least
+.I 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
The value at that offset is read, and is used again as an offset
in the file.
Indirect offsets are of the form:
-.BI (( x [.[bslBSL]][+-][ y ]).
+.BI (( x [.[bslBSL]][+\-][ y ]).
The value of
.I x
is used as an offset in the file. A byte, short or long is read at that offset
.\" the changes I posted to the S5R2 version.
.\"
.\" Modified for Ian Darwin's version of the file command.
-.\" @(#)$Id: magic.man,v 1.25 2003/03/31 17:52:04 christos Exp $
+.\" @(#)$Id: magic.man,v 1.26 2003/05/23 21:31:57 christos Exp $
>4 byte 0 invalid class
>4 byte 1 32-bit
# only for MIPS - in the future, the ABI field of e_flags should be used.
+>>18 leshort 8
+>>>36 lelong &0x20 N32
+>>18 leshort 10
+>>>36 lelong &0x20 N32
>>18 beshort 8
+>>>36 belong &0x20 N32
>>18 beshort 10
->>>36 belong &0x20 N32
+>>>36 belong &0x20 N32
>4 byte 2 64-bit
>5 byte 0 invalid byte order
>5 byte 1 LSB
#
# RPM: file(1) magic for Red Hat Packages Erik Troan (ewt@redhat.com)
#
-0 beshort 0xedab
+0 beshort 0xedab
>2 beshort 0xeedb RPM
>>4 byte x v%d
>>6 beshort 0 bin
>>8 beshort 5 PowerPC
>>8 beshort 6 68000
>>8 beshort 7 SGI
+>>8 beshort 8 RS6000
+>>8 beshort 9 IA64
+>>8 beshort 10 Sparc64
+>>8 beshort 11 MIPSel
+>>8 beshort 12 ARM
>>10 string x %s
done >> $@
magic.mgc: magic
- $(top_builddir)/src/file -C -m $(srcdir)/magic
+ $(top_builddir)/src/file -C -m magic
magic.mime.mgc: magic.mime
- $(top_builddir)/src/file -C -m $(srcdir)/magic.mime
+ $(top_builddir)/src/file -C -m magic.mime
magic_FRAGMENTS = \
Magdir/acorn \
Magdir/claris \
Magdir/clipper \
Magdir/commands \
+Magdir/communications \
Magdir/compress \
Magdir/console \
Magdir/convex \
Magdir/elf \
Magdir/encore \
Magdir/epoc \
+Magdir/fcs \
Magdir/filesystems \
Magdir/flash \
Magdir/fonts \
Magdir/frame \
Magdir/freebsd \
Magdir/fsav \
+Magdir/games \
Magdir/gimp \
Magdir/gnu \
Magdir/grace \
Magdir/mime \
Magdir/mips \
Magdir/mirage \
+Magdir/misctools \
Magdir/mkid \
Magdir/mlssa \
Magdir/mmdf \
Magdir/netscape \
Magdir/news \
Magdir/nitpicker \
+Magdir/ocaml \
Magdir/octave \
Magdir/olf \
Magdir/os2 \
Magdir/sequent \
Magdir/sgml \
Magdir/sharc \
+Magdir/sinclair \
Magdir/sketch \
Magdir/smalltalk \
Magdir/sniffer \
Magdir/vmware \
Magdir/vorbis \
Magdir/vxl \
-Magdir/wordperfect \
+Magdir/wordprocessors \
Magdir/xdelta \
Magdir/xenix \
Magdir/zilog \
Magdir/claris \
Magdir/clipper \
Magdir/commands \
+Magdir/communications \
Magdir/compress \
Magdir/console \
Magdir/convex \
Magdir/elf \
Magdir/encore \
Magdir/epoc \
+Magdir/fcs \
Magdir/filesystems \
Magdir/flash \
Magdir/fonts \
Magdir/frame \
Magdir/freebsd \
Magdir/fsav \
+Magdir/games \
Magdir/gimp \
Magdir/gnu \
Magdir/grace \
Magdir/mime \
Magdir/mips \
Magdir/mirage \
+Magdir/misctools \
Magdir/mkid \
Magdir/mlssa \
Magdir/mmdf \
Magdir/netscape \
Magdir/news \
Magdir/nitpicker \
+Magdir/ocaml \
Magdir/octave \
Magdir/olf \
Magdir/os2 \
Magdir/sequent \
Magdir/sgml \
Magdir/sharc \
+Magdir/sinclair \
Magdir/sketch \
Magdir/smalltalk \
Magdir/sniffer \
Magdir/vmware \
Magdir/vorbis \
Magdir/vxl \
-Magdir/wordperfect \
+Magdir/wordprocessors \
Magdir/xdelta \
Magdir/xenix \
Magdir/zilog \
done >> $@
magic.mgc: magic
- $(top_builddir)/src/file -C -m $(srcdir)/magic
+ $(top_builddir)/src/file -C -m magic
magic.mime.mgc: magic.mime
- $(top_builddir)/src/file -C -m $(srcdir)/magic.mime
+ $(top_builddir)/src/file -C -m magic.mime
# Tell versions [3.59,3.63) of GNU make to not export all variables.
# Otherwise a system limit (for SysV at least) may be exceeded.
.NOEXPORT:
0 string PK\003\004 application/x-zip
+# RAR archiver (Greg Roelofs, newt@uchicago.edu)
+0 string Rar! application/x-rar
+
# According to gzip.h, this is the correct byte order for packed data.
0 string \037\036 application/octet-stream
#
0 lelong&0x8080ffff 0x0000041a application/x-arc squeezed
0 lelong&0x8080ffff 0x0000061a application/x-arc crunched
-0 leshort 0xea60 application/octet-stream x-arj
+0 leshort 0xea60 application/x-arj
# LHARC/LHA archiver (Greg Roelofs, newt@uchicago.edu)
2 string -lh0- application/x-lharc lh0
0 string \<!doctype\ HTML text/html
0 string \<!DOCTYPE\ HTML text/html
0 string \<!doctype\ html text/html
+0 string \<!doctype\ HTML text/html
#------------------------------------------------------------------------------
# images: file(1) magic for image formats (see also "c-lang" for XPM bitmaps)
# MNG Video Format, <URL:http://www.libpng.org/pub/mng/spec/>
0 string \x8aMNG video/x-mng
+0 string \x8aJNG video/x-jng
#------------------------------------------------------------------------------
# Hierarchical Data Format, used to facilitate scientific data exchange
# Adobe Photoshop
0 string 8BPS image/x-photoshop
+
+# Felix von Leitner <felix-file@fefe.de>
+0 string d8:announce application/x-bittorrent
+
+
+# lotus 1-2-3 document
+0 belong 0x00001a00 application/x-123
+0 belong 0x00000200 application/x-123
+
+# MS Access database
+4 string Standard\ Jet\ DB application/msaccess
+
+## magic for XBase files
+#0 byte 0x02
+#>8 leshort >0
+#>>12 leshort 0 application/x-dbf
+#
+#0 byte 0x03
+#>8 leshort >0
+#>>12 leshort 0 application/x-dbf
+#
+#0 byte 0x04
+#>8 leshort >0
+#>>12 leshort 0 application/x-dbf
+#
+#0 byte 0x05
+#>8 leshort >0
+#>>12 leshort 0 application/x-dbf
+#
+#0 byte 0x30
+#>8 leshort >0
+#>>12 leshort 0 application/x-dbf
+#
+#0 byte 0x43
+#>8 leshort >0
+#>>12 leshort 0 application/x-dbf
+#
+#0 byte 0x7b
+#>8 leshort >0
+#>>12 leshort 0 application/x-dbf
+#
+#0 byte 0x83
+#>8 leshort >0
+#>>12 leshort 0 application/x-dbf
+#
+#0 byte 0x8b
+#>8 leshort >0
+#>>12 leshort 0 application/x-dbf
+#
+#0 byte 0x8e
+#>8 leshort >0
+#>>12 leshort 0 application/x-dbf
+#
+#0 byte 0xb3
+#>8 leshort >0
+#>>12 leshort 0 application/x-dbf
+#
+#0 byte 0xf5
+#>8 leshort >0
+#>>12 leshort 0 application/x-dbf
+#
+#0 leshort 0x0006 application/x-dbt
#endif
#ifndef lint
-FILE_RCSID("@(#)$Id: apprentice.c,v 1.57 2003/03/28 21:02:03 christos Exp $")
+FILE_RCSID("@(#)$Id: apprentice.c,v 1.58 2003/05/23 21:31:58 christos Exp $")
#endif /* lint */
#define EATAB {while (isascii((unsigned char) *l) && \
}
/* get offset, then skip over it */
- m->offset = (int) strtoul(l, &t, 0);
+ m->offset = (uint32_t)strtoul(l, &t, 0);
if (l == t)
if (ms->flags & MAGIC_CHECK)
file_magwarn("offset %s invalid", l);
return -1;
}
- if (write(fd, ar, sizeof(ar)) != sizeof(ar)) {
+ if (write(fd, ar, sizeof(ar)) != (ssize_t)sizeof(ar)) {
file_error(ms, "Error writing `%s' (%s)", dbname,
strerror(errno));
return -1;
return -1;
}
- if (write(fd, *magicp, sizeof(struct magic) * *nmagicp)
- != sizeof(struct magic) * *nmagicp) {
+ if (write(fd, *magicp, (sizeof(struct magic) * *nmagicp))
+ != (ssize_t)(sizeof(struct magic) * *nmagicp)) {
file_error(ms, "Error writing `%s' (%s)", dbname,
strerror(errno));
return -1;
private char *
mkdbname(const char *fn, char *buf, size_t bufsiz)
{
+#ifdef notdef
const char *p;
if ((p = strrchr(fn, '/')) != NULL)
- p++;
- else
- p = fn;
- (void)snprintf(buf, bufsiz, "%s%s", p, ext);
+ fn = ++p;
+#endif
+ (void)snprintf(buf, bufsiz, "%s%s", fn, ext);
return buf;
}
#include "names.h"
#ifndef lint
-FILE_RCSID("@(#)$Id: ascmagic.c,v 1.37 2003/03/27 18:34:21 christos Exp $")
+FILE_RCSID("@(#)$Id: ascmagic.c,v 1.38 2003/05/23 21:31:58 christos Exp $")
#endif /* lint */
typedef unsigned long unichar;
while (nbytes > 1 && buf[nbytes - 1] == '\0')
nbytes--;
+ /* nbuf and ubuf relies on this */
+ if (nbytes > HOWMANY)
+ nbytes = HOWMANY;
+
/*
* Then try to determine whether it's any character code we can
* identify. Each of these tests, if it succeeds, will leave
#endif
#ifndef lint
-FILE_RCSID("@(#)$Id: compress.c,v 1.31 2003/03/26 16:25:25 christos Exp $")
+FILE_RCSID("@(#)$Id: compress.c,v 1.32 2003/05/23 21:31:58 christos Exp $")
#endif
private int ncompr = sizeof(compr) / sizeof(compr[0]);
-private int swrite(int, const void *, size_t);
-private int sread(int, void *, size_t);
+private ssize_t swrite(int, const void *, size_t);
+private ssize_t sread(int, void *, size_t);
private size_t uncompressbuf(struct magic_set *, size_t, const unsigned char *,
unsigned char **, size_t);
#ifdef HAVE_LIBZ
/*
* `safe' write for sockets and pipes.
*/
-private int
+private ssize_t
swrite(int fd, const void *buf, size_t n)
{
int rv;
/*
* `safe' read for sockets and pipes.
*/
-private int
+private ssize_t
sread(int fd, void *buf, size_t n)
{
int rv;
return -1;
}
- if (swrite(tfd, startbuf, nbytes) != nbytes)
+ if (swrite(tfd, startbuf, nbytes) != (ssize_t)nbytes)
r = 1;
else {
while ((r = sread(fd, buf, sizeof(buf))) > 0)
unsigned char **newch, size_t n)
{
unsigned char flg = old[3];
- int data_start = 10;
+ size_t data_start = 10;
z_stream z;
int rc;
- if (flg & FEXTRA)
+ if (flg & FEXTRA) {
+ if (data_start+1 >= n)
+ return 0;
data_start += 2 + old[data_start] + old[data_start + 1] * 256;
+ }
if (flg & FNAME) {
- while(old[data_start])
+ while(data_start < n && old[data_start])
data_start++;
data_start++;
}
if(flg & FCOMMENT) {
- while(old[data_start])
+ while(data_start < n && old[data_start])
data_start++;
data_start++;
}
if(flg & FHCRC)
data_start += 2;
+ if (data_start >= n)
+ return 0;
if ((*newch = (unsigned char *)malloc(HOWMANY + 1)) == NULL) {
return 0;
}
default: /* parent */
(void) close(fdin[0]);
(void) close(fdout[1]);
- if (swrite(fdin[1], old, n) != n) {
+ if (swrite(fdin[1], old, n) != (ssize_t)n) {
n = 0;
goto err;
}
*/
/*
* file.h - definitions for file(1) program
- * @(#)$Id: file.h,v 1.54 2003/04/04 21:59:27 christos Exp $
+ * @(#)$Id: file.h,v 1.55 2003/05/23 21:31:58 christos Exp $
*/
#ifndef __file_h__
#define FILE_OPMODULO 7
#define FILE_OPINVERSE 0x80
/* Word 4 */
- int32_t offset; /* offset to magic number */
+ uint32_t offset; /* offset to magic number */
/* Word 5 */
- int32_t in_offset; /* offset from indirection */
+ uint32_t in_offset; /* offset from indirection */
/* Word 6 */
uint32_t mask; /* mask before comparison with value */
/* Word 7 */
#undef HAVE_MAJOR
#ifndef lint
-FILE_RCSID("@(#)$Id: fsmagic.c,v 1.40 2003/03/27 19:09:57 christos Exp $")
+FILE_RCSID("@(#)$Id: fsmagic.c,v 1.41 2003/05/23 21:31:58 christos Exp $")
#endif /* lint */
protected int
tmp = buf; /* in current directory anyway */
}
else {
+ if (tmp - fn + 1 > BUFSIZ) {
+ file_printf(ms, "path too long: `%s'", fn);
+ return -1;
+ }
strcpy(buf2, fn); /* take directory part */
buf2[tmp-fn+1] = '\0';
strcat(buf2, buf); /* plus (relative) symlink */
#include "patchlevel.h"
#ifndef lint
-FILE_RCSID("@(#)$Id: magic.c,v 1.6 2003/03/26 15:35:30 christos Exp $")
+FILE_RCSID("@(#)$Id: magic.c,v 1.7 2003/05/23 21:31:58 christos Exp $")
#endif /* lint */
#ifdef __EMX__
int fd = 0;
unsigned char buf[HOWMANY+1]; /* one extra for terminating '\0' */
struct stat sb;
- int nbytes = 0; /* number of bytes read from a datafile */
+ ssize_t nbytes = 0; /* number of bytes read from a datafile */
if (file_reset(ms) == -1)
return NULL;
*/
if ((nbytes = read(fd, (char *)buf, HOWMANY)) == -1) {
file_error(ms, "Cannot read `%s' %s", inname, strerror(errno));
+ (void)close(fd);
return NULL;
}
if (nbytes == 0) {
if (file_printf(ms, (ms->flags & MAGIC_MIME) ?
- "application/x-empty" : "empty") == -1)
+ "application/x-empty" : "empty") == -1) {
+ (void)close(fd);
return NULL;
+ }
} else {
buf[nbytes++] = '\0'; /* null-terminate it */
#ifdef __EMX__
switch (file_os2_apptype(ms, inname, buf, nbytes)) {
case -1:
+ (void)close(fd);
return NULL;
case 0:
break;
return ms->o.buf;
}
#endif
- if (file_buffer(ms, buf, (size_t)nbytes) == -1)
+ if (file_buffer(ms, buf, (size_t)nbytes) == -1) {
+ (void)close(fd);
return NULL;
+ }
#ifdef BUILTIN_ELF
if (nbytes > 5) {
/*
#endif
}
+ close(fd);
return ms->haderr ? NULL : ms->o.buf;
}
#include "readelf.h"
#ifndef lint
-FILE_RCSID("@(#)$Id: readelf.c,v 1.31 2003/03/26 15:35:30 christos Exp $")
+FILE_RCSID("@(#)$Id: readelf.c,v 1.32 2003/05/23 21:31:59 christos Exp $")
#endif
#ifdef ELFCORE
Elf64_Phdr ph64;
size_t offset;
unsigned char nbuf[BUFSIZ];
- int bufsize;
+ ssize_t bufsize;
if (size != ph_size) {
file_error(ms, "Corrupted program header size");
}
offset = 0;
for (;;) {
- if (offset >= bufsize)
+ if (offset >= (size_t)bufsize)
break;
offset = donote(ms, nbuf, offset, (size_t)bufsize,
class, swap, 4);
return size;
return size;
} else if (os_style != OS_STYLE_NETBSD && nh_type == NT_PRPSINFO) {
- int i, j;
+ size_t i, j;
unsigned char c;
/*
* Extract the program name. We assume
}
offset = 0;
for (;;) {
- if (offset >= bufsize)
+ if (offset >= (size_t)bufsize)
break;
offset = donote(ms, nbuf, offset,
(size_t)bufsize, class, swap, ph_align);
#ifndef lint
-FILE_RCSID("@(#)$Id: softmagic.c,v 1.58 2003/03/26 15:35:30 christos Exp $")
+FILE_RCSID("@(#)$Id: softmagic.c,v 1.59 2003/05/23 21:31:59 christos Exp $")
#endif /* lint */
private int match(struct magic_set *, struct magic *, uint32_t,
match(struct magic_set *ms, struct magic *magic, uint32_t nmagic,
const unsigned char *s, size_t nbytes)
{
- int magindex = 0;
- int cont_level = 0;
+ uint32_t magindex = 0;
+ unsigned int cont_level = 0;
int need_separator = 0;
union VALUETYPE p;
int32_t oldoff = 0;
case FILE_PSTRING:
{
char *ptr1 = p->s, *ptr2 = ptr1 + 1;
- int n = *p->s;
+ unsigned int n = *p->s;
if (n >= sizeof(p->s))
n = sizeof(p->s) - 1;
while (n--)
mget(struct magic_set *ms, union VALUETYPE *p, const unsigned char *s,
struct magic *m, size_t nbytes)
{
- int32_t offset = m->offset;
+ uint32_t offset = m->offset;
if (m->type == FILE_REGEX) {
/*
* the usefulness of padding with zeroes eludes me, it
* might even cause problems
*/
- int32_t have = nbytes - offset;
memset(p, 0, sizeof(union VALUETYPE));
- if (have > 0)
- memcpy(p, s + offset, (size_t)have);
+ if (offset < nbytes)
+ memcpy(p, s + offset, nbytes - offset);
}
if ((ms->flags & MAGIC_DEBUG) != 0) {
break;
}
- if (offset + sizeof(union VALUETYPE) > nbytes)
+ if (nbytes < sizeof(union VALUETYPE) ||
+ nbytes - sizeof(union VALUETYPE) < offset)
return 0;
memcpy(p, s + offset, sizeof(union VALUETYPE));