From 18726eb5fd61c19f6ba82aefeead820568dededd Mon Sep 17 00:00:00 2001 From: Christos Zoulas Date: Thu, 2 Nov 2017 20:25:39 +0000 Subject: [PATCH] - Encapsulate the buffer, size and file descriptor in a struct buffer. - Prepare for signed offset (negative offsets will be relative to the end of file). - Fix name collision for file_err(). - fix file_encoding to not return a unicode buffer if NULL is passed. --- src/Makefile.am | 2 +- src/apprentice.c | 12 ++++----- src/ascmagic.c | 29 +++++++++++++--------- src/buffer.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++ src/compress.c | 8 +++--- src/encoding.c | 16 ++++++++++-- src/file.h | 38 ++++++++++++++++------------ src/funcs.c | 27 ++++++++++---------- src/is_tar.c | 6 +++-- src/readcdf.c | 8 +++--- src/readelf.c | 8 +++--- src/softmagic.c | 43 +++++++++++++++++++------------- 12 files changed, 184 insertions(+), 77 deletions(-) create mode 100644 src/buffer.c diff --git a/src/Makefile.am b/src/Makefile.am index cbdccac2..bbd3f501 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -7,7 +7,7 @@ bin_PROGRAMS = file AM_CPPFLAGS = -DMAGIC='"$(MAGIC)"' AM_CFLAGS = $(CFLAG_VISIBILITY) @WARNINGS@ -libmagic_la_SOURCES = magic.c apprentice.c softmagic.c ascmagic.c \ +libmagic_la_SOURCES = buffer.c magic.c apprentice.c softmagic.c ascmagic.c \ encoding.c compress.c is_tar.c readelf.c print.c fsmagic.c \ funcs.c file.h readelf.h tar.h apptype.c der.c der.h \ file_opts.h elfclass.h mygetopt.h cdf.c cdf_time.c readcdf.c cdf.h diff --git a/src/apprentice.c b/src/apprentice.c index 3ff37fc1..d5bc3fbe 100644 --- a/src/apprentice.c +++ b/src/apprentice.c @@ -32,7 +32,7 @@ #include "file.h" #ifndef lint -FILE_RCSID("@(#)$File: apprentice.c,v 1.264 2017/09/26 12:21:59 christos Exp $") +FILE_RCSID("@(#)$File: apprentice.c,v 1.265 2017/11/02 20:25:39 christos Exp $") #endif /* lint */ #include "magic.h" @@ -652,7 +652,7 @@ protected int file_apprentice(struct magic_set *ms, const char *fn, int action) { char *p, *mfn; - int file_err, errs = -1; + int fileerr, errs = -1; size_t i; (void)file_reset(ms, 0); @@ -687,8 +687,8 @@ file_apprentice(struct magic_set *ms, const char *fn, int action) *p++ = '\0'; if (*fn == '\0') break; - file_err = apprentice_1(ms, fn, action); - errs = MAX(errs, file_err); + fileerr = apprentice_1(ms, fn, action); + errs = MAX(errs, fileerr); fn = p; } @@ -1910,7 +1910,7 @@ parse(struct magic_set *ms, struct magic_entry *me, const char *line, } /* get offset, then skip over it */ - m->offset = (uint32_t)strtoul(l, &t, 0); + m->offset = (int32_t)strtol(l, &t, 0); if (l == t) { if (ms->flags & MAGIC_CHECK) file_magwarn(ms, "offset `%s' invalid", l); @@ -3324,7 +3324,7 @@ private void bs1(struct magic *m) { m->cont_level = swap2(m->cont_level); - m->offset = swap4((uint32_t)m->offset); + m->offset = swap4((int32_t)m->offset); m->in_offset = swap4((uint32_t)m->in_offset); m->lineno = swap4((uint32_t)m->lineno); if (IS_STRING(m->type)) { diff --git a/src/ascmagic.c b/src/ascmagic.c index 0321f40b..2d1abe54 100644 --- a/src/ascmagic.c +++ b/src/ascmagic.c @@ -35,7 +35,7 @@ #include "file.h" #ifndef lint -FILE_RCSID("@(#)$File: ascmagic.c,v 1.96 2016/06/16 11:37:55 christos Exp $") +FILE_RCSID("@(#)$File: ascmagic.c,v 1.98 2017/11/02 20:25:39 christos Exp $") #endif /* lint */ #include "magic.h" @@ -68,26 +68,27 @@ trim_nuls(const unsigned char *buf, size_t nbytes) } protected int -file_ascmagic(struct magic_set *ms, const unsigned char *buf, size_t nbytes, - int text) +file_ascmagic(struct magic_set *ms, const struct buffer *b, int text) { unichar *ubuf = NULL; size_t ulen = 0; int rv = 1; + struct buffer bb; const char *code = NULL; const char *code_mime = NULL; const char *type = NULL; - nbytes = trim_nuls(buf, nbytes); + bb = *b; + bb.flen = trim_nuls(b->fbuf, b->flen); /* If file doesn't look like any sort of text, give up. */ - if (file_encoding(ms, buf, nbytes, &ubuf, &ulen, &code, &code_mime, + if (file_encoding(ms, &bb, &ubuf, &ulen, &code, &code_mime, &type) == 0) rv = 0; else - rv = file_ascmagic_with_encoding(ms, buf, nbytes, ubuf, ulen, code, - type, text); + rv = file_ascmagic_with_encoding(ms, &bb, + ubuf, ulen, code, type, text); free(ubuf); @@ -95,10 +96,13 @@ file_ascmagic(struct magic_set *ms, const unsigned char *buf, size_t nbytes, } protected int -file_ascmagic_with_encoding(struct magic_set *ms, const unsigned char *buf, - size_t nbytes, unichar *ubuf, size_t ulen, const char *code, +file_ascmagic_with_encoding(struct magic_set *ms, + const struct buffer *b, unichar *ubuf, size_t ulen, const char *code, const char *type, int text) { + struct buffer bb; + const unsigned char *buf = b->fbuf; + size_t nbytes = b->flen; unsigned char *utf8_buf = NULL, *utf8_end; size_t mlen, i; int rv = -1; @@ -140,10 +144,13 @@ file_ascmagic_with_encoding(struct magic_set *ms, const unsigned char *buf, if ((utf8_end = encode_utf8(utf8_buf, mlen, ubuf, ulen)) == NULL) goto done; - if ((rv = file_softmagic(ms, utf8_buf, - (size_t)(utf8_end - utf8_buf), NULL, NULL, + buffer_init(&bb, b->fd, utf8_buf, + (size_t)(utf8_end - utf8_buf)); + + if ((rv = file_softmagic(ms, &bb, NULL, NULL, TEXTTEST, text)) == 0) rv = -1; + buffer_fini(&bb); if ((ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION))) { rv = rv == -1 ? 0 : 1; goto done; diff --git a/src/buffer.c b/src/buffer.c new file mode 100644 index 00000000..293558e6 --- /dev/null +++ b/src/buffer.c @@ -0,0 +1,64 @@ +/* + * Copyright (c) Christos Zoulas 2017. + * 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 "file.h" + +#ifndef lint +FILE_RCSID("@(#)$File: buffer.c,v 1.1 2017/11/02 20:25:39 christos Exp $") +#endif /* lint */ + +#include "magic.h" +#include +#include +#include +#include +#include +#if defined(HAVE_WCHAR_H) +#include +#endif +#if defined(HAVE_WCTYPE_H) +#include +#endif +#if defined(HAVE_LIMITS_H) +#include +#endif + + +void +buffer_init(struct buffer *b, int fd, const void *data, size_t len) +{ + b->fd = fd; + b->fbuf = data; + b->flen = len; + b->ebuf = NULL; + b->elen = 0; +} + +void +buffer_fini(struct buffer *b) +{ + free(b->ebuf); +} diff --git a/src/compress.c b/src/compress.c index 2f789cd2..184011b9 100644 --- a/src/compress.c +++ b/src/compress.c @@ -35,7 +35,7 @@ #include "file.h" #ifndef lint -FILE_RCSID("@(#)$File: compress.c,v 1.105 2017/05/25 00:13:03 christos Exp $") +FILE_RCSID("@(#)$File: compress.c,v 1.106 2017/11/02 20:25:39 christos Exp $") #endif #include "magic.h" @@ -184,8 +184,7 @@ static int makeerror(unsigned char **, size_t *, const char *, ...) private const char *methodname(size_t); protected int -file_zmagic(struct magic_set *ms, int fd, const char *name, - const unsigned char *buf, size_t nbytes) +file_zmagic(struct magic_set *ms, const struct buffer *b, const char *name) { unsigned char *newbuf = NULL; size_t i, nsz; @@ -193,6 +192,9 @@ file_zmagic(struct magic_set *ms, int fd, const char *name, file_pushbuf_t *pb; int urv, prv, rv = 0; int mime = ms->flags & MAGIC_MIME; + int fd = b->fd; + const unsigned char *buf = b->fbuf; + size_t nbytes = b->flen; #ifdef HAVE_SIGNAL_H sig_t osigpipe; #endif diff --git a/src/encoding.c b/src/encoding.c index c429abcc..3e7b9e58 100644 --- a/src/encoding.c +++ b/src/encoding.c @@ -35,7 +35,7 @@ #include "file.h" #ifndef lint -FILE_RCSID("@(#)$File: encoding.c,v 1.12 2015/06/03 19:51:27 christos Exp $") +FILE_RCSID("@(#)$File: encoding.c,v 1.14 2017/11/02 20:25:39 christos Exp $") #endif /* lint */ #include "magic.h" @@ -66,11 +66,21 @@ private void from_ebcdic(const unsigned char *, size_t, unsigned char *); * ubuf, and the number of characters converted in ulen. */ protected int -file_encoding(struct magic_set *ms, const unsigned char *buf, size_t nbytes, unichar **ubuf, size_t *ulen, const char **code, const char **code_mime, const char **type) +file_encoding(struct magic_set *ms, const struct buffer *b, unichar **ubuf, + size_t *ulen, const char **code, const char **code_mime, const char **type) { + const unsigned char *buf = b->fbuf; + size_t nbytes = b->flen; size_t mlen; int rv = 1, ucs_type; unsigned char *nbuf = NULL; + unichar *udefbuf; + size_t udeflen; + + if (ubuf == NULL) + ubuf = &udefbuf; + if (ulen == NULL) + ulen = &udeflen; *type = "text"; *ulen = 0; @@ -144,6 +154,8 @@ file_encoding(struct magic_set *ms, const unsigned char *buf, size_t nbytes, uni done: free(nbuf); + if (ubuf == &udefbuf) + free(udefbuf); return rv; } diff --git a/src/file.h b/src/file.h index 79490508..214b675a 100644 --- a/src/file.h +++ b/src/file.h @@ -27,7 +27,7 @@ */ /* * file.h - definitions for file(1) program - * @(#)$File: file.h,v 1.185 2017/09/24 16:04:56 christos Exp $ + * @(#)$File: file.h,v 1.186 2017/11/02 20:25:39 christos Exp $ */ #ifndef __file_h__ @@ -145,6 +145,14 @@ #define FILE_COMPILE 2 #define FILE_LIST 3 +struct buffer { + int fd; + const void *fbuf; + size_t flen; + void *ebuf; + size_t elen; +}; + union VALUETYPE { uint8_t b; uint16_t h; @@ -287,7 +295,7 @@ struct magic { #endif /* ENABLE_CONDITIONALS */ /* Word 4 */ - uint32_t offset; /* offset to magic number */ + int32_t offset; /* offset to magic number */ /* Word 5 */ int32_t in_offset; /* offset from indirection */ /* Word 6 */ @@ -394,7 +402,7 @@ struct magic_set { char *buf; /* Accumulation buffer */ char *pbuf; /* Printable buffer */ } o; - uint32_t offset; + int32_t offset; int error; int flags; /* Control magic tests. */ int event_flags; /* Note things that happened. */ @@ -448,23 +456,20 @@ protected int file_replace(struct magic_set *, const char *, const char *); protected int file_printf(struct magic_set *, const char *, ...) __attribute__((__format__(__printf__, 2, 3))); protected int file_reset(struct magic_set *, int); -protected int file_tryelf(struct magic_set *, int, const unsigned char *, - size_t); -protected int file_trycdf(struct magic_set *, int, const unsigned char *, - size_t); +protected int file_tryelf(struct magic_set *, const struct buffer *); +protected int file_trycdf(struct magic_set *, const struct buffer *); #if HAVE_FORK -protected int file_zmagic(struct magic_set *, int, const char *, - const unsigned char *, size_t); +protected int file_zmagic(struct magic_set *, const struct buffer *, + const char *); #endif -protected int file_ascmagic(struct magic_set *, const unsigned char *, size_t, +protected int file_ascmagic(struct magic_set *, const struct buffer *, int); protected int file_ascmagic_with_encoding(struct magic_set *, - const unsigned char *, size_t, unichar *, size_t, const char *, - const char *, int); -protected int file_encoding(struct magic_set *, const unsigned char *, size_t, + const struct buffer *, unichar *, size_t, const char *, const char *, int); +protected int file_encoding(struct magic_set *, const struct buffer *, unichar **, size_t *, const char **, const char **, const char **); -protected int file_is_tar(struct magic_set *, const unsigned char *, size_t); -protected int file_softmagic(struct magic_set *, const unsigned char *, size_t, +protected int file_is_tar(struct magic_set *, const struct buffer *); +protected int file_softmagic(struct magic_set *, const struct buffer *, uint16_t *, uint16_t *, int, int); protected int file_apprentice(struct magic_set *, const char *, int); protected int buffer_apprentice(struct magic_set *, struct magic **, @@ -497,6 +502,9 @@ protected int file_os2_apptype(struct magic_set *, const char *, const void *, size_t); #endif /* __EMX__ */ +protected void buffer_init(struct buffer *, int, const void *, size_t); +protected void buffer_fini(struct buffer *); + #if defined(HAVE_LOCALE_H) #include #endif diff --git a/src/funcs.c b/src/funcs.c index d7a18f45..f59f4a1b 100644 --- a/src/funcs.c +++ b/src/funcs.c @@ -27,7 +27,7 @@ #include "file.h" #ifndef lint -FILE_RCSID("@(#)$File: funcs.c,v 1.93 2017/08/28 13:39:18 christos Exp $") +FILE_RCSID("@(#)$File: funcs.c,v 1.94 2017/11/02 20:25:39 christos Exp $") #endif /* lint */ #include "magic.h" @@ -178,14 +178,14 @@ file_buffer(struct magic_set *ms, int fd, const char *inname __attribute__ ((__u const void *buf, size_t nb) { int m = 0, rv = 0, looks_text = 0; - const unsigned char *ubuf = CAST(const unsigned char *, buf); - unichar *u8buf = NULL; - size_t ulen; const char *code = NULL; const char *code_mime = "binary"; const char *type = "application/octet-stream"; const char *def = "data"; const char *ftype = NULL; + struct buffer b; + + buffer_init(&b, fd, buf, nb); if (nb == 0) { def = "empty"; @@ -197,13 +197,13 @@ file_buffer(struct magic_set *ms, int fd, const char *inname __attribute__ ((__u } if ((ms->flags & MAGIC_NO_CHECK_ENCODING) == 0) { - looks_text = file_encoding(ms, ubuf, nb, &u8buf, &ulen, + looks_text = file_encoding(ms, &b, NULL, 0, &code, &code_mime, &ftype); } #ifdef __EMX__ if ((ms->flags & MAGIC_NO_CHECK_APPTYPE) == 0 && inname) { - m = file_os2_apptype(ms, inname, buf, nb); + m = file_os2_apptype(ms, inname, &b); if ((ms->flags & MAGIC_DEBUG) != 0) (void)fprintf(stderr, "[try os2_apptype %d]\n", m); switch (m) { @@ -219,7 +219,7 @@ file_buffer(struct magic_set *ms, int fd, const char *inname __attribute__ ((__u #if HAVE_FORK /* try compression stuff */ if ((ms->flags & MAGIC_NO_CHECK_COMPRESS) == 0) { - m = file_zmagic(ms, fd, inname, ubuf, nb); + m = file_zmagic(ms, &b, inname); if ((ms->flags & MAGIC_DEBUG) != 0) (void)fprintf(stderr, "[try zmagic %d]\n", m); if (m) { @@ -229,7 +229,7 @@ file_buffer(struct magic_set *ms, int fd, const char *inname __attribute__ ((__u #endif /* Check if we have a tar file */ if ((ms->flags & MAGIC_NO_CHECK_TAR) == 0) { - m = file_is_tar(ms, ubuf, nb); + m = file_is_tar(ms, &b); if ((ms->flags & MAGIC_DEBUG) != 0) (void)fprintf(stderr, "[try tar %d]\n", m); if (m) { @@ -240,7 +240,7 @@ file_buffer(struct magic_set *ms, int fd, const char *inname __attribute__ ((__u /* Check if we have a CDF file */ if ((ms->flags & MAGIC_NO_CHECK_CDF) == 0) { - m = file_trycdf(ms, fd, ubuf, nb); + m = file_trycdf(ms, &b); if ((ms->flags & MAGIC_DEBUG) != 0) (void)fprintf(stderr, "[try cdf %d]\n", m); if (m) { @@ -251,8 +251,7 @@ file_buffer(struct magic_set *ms, int fd, const char *inname __attribute__ ((__u /* try soft magic tests */ if ((ms->flags & MAGIC_NO_CHECK_SOFT) == 0) { - m = file_softmagic(ms, ubuf, nb, NULL, NULL, BINTEST, - looks_text); + m = file_softmagic(ms, &b, NULL, NULL, BINTEST, looks_text); if ((ms->flags & MAGIC_DEBUG) != 0) (void)fprintf(stderr, "[try softmagic %d]\n", m); if (m) { @@ -268,7 +267,7 @@ file_buffer(struct magic_set *ms, int fd, const char *inname __attribute__ ((__u * ELF headers that cannot easily * be * extracted with rules in the magic file. */ - m = file_tryelf(ms, fd, ubuf, nb); + m = file_tryelf(ms, &b); if ((ms->flags & MAGIC_DEBUG) != 0) (void)fprintf(stderr, "[try elf %d]\n", m); @@ -282,7 +281,7 @@ file_buffer(struct magic_set *ms, int fd, const char *inname __attribute__ ((__u /* try text properties */ if ((ms->flags & MAGIC_NO_CHECK_TEXT) == 0) { - m = file_ascmagic(ms, ubuf, nb, looks_text); + m = file_ascmagic(ms, &b, looks_text); if ((ms->flags & MAGIC_DEBUG) != 0) (void)fprintf(stderr, "[try ascmagic %d]\n", m); if (m) { @@ -319,7 +318,7 @@ simple: #if HAVE_FORK done_encoding: #endif - free(u8buf); + buffer_fini(&b); if (rv) return rv; diff --git a/src/is_tar.c b/src/is_tar.c index 4b18a8a0..7110604f 100644 --- a/src/is_tar.c +++ b/src/is_tar.c @@ -40,7 +40,7 @@ #include "file.h" #ifndef lint -FILE_RCSID("@(#)$File: is_tar.c,v 1.40 2017/10/13 15:25:12 christos Exp $") +FILE_RCSID("@(#)$File: is_tar.c,v 1.41 2017/11/02 20:25:39 christos Exp $") #endif #include "magic.h" @@ -60,8 +60,10 @@ static const char tartype[][32] = { /* should be equal to messages */ }; protected int -file_is_tar(struct magic_set *ms, const unsigned char *buf, size_t nbytes) +file_is_tar(struct magic_set *ms, const struct buffer *b) { + const unsigned char *buf = b->fbuf; + size_t nbytes = b->flen; /* * Do the tar test first, because if the first file in the tar * archive starts with a dot, we can confuse it with an nroff file. diff --git a/src/readcdf.c b/src/readcdf.c index 80c8d26e..51b862cf 100644 --- a/src/readcdf.c +++ b/src/readcdf.c @@ -26,7 +26,7 @@ #include "file.h" #ifndef lint -FILE_RCSID("@(#)$File: readcdf.c,v 1.65 2017/04/08 20:58:03 christos Exp $") +FILE_RCSID("@(#)$File: readcdf.c,v 1.66 2017/11/02 20:25:39 christos Exp $") #endif #include @@ -537,9 +537,11 @@ cdf_file_dir_info(struct magic_set *ms, const cdf_dir_t *dir) } protected int -file_trycdf(struct magic_set *ms, int fd, const unsigned char *buf, - size_t nbytes) +file_trycdf(struct magic_set *ms, const struct buffer *b) { + int fd = b->fd; + const unsigned char *buf = b->fbuf; + size_t nbytes = b->flen; cdf_info_t info; cdf_header_t h; cdf_sat_t sat, ssat; diff --git a/src/readelf.c b/src/readelf.c index 013b9f48..e04c603d 100644 --- a/src/readelf.c +++ b/src/readelf.c @@ -27,7 +27,7 @@ #include "file.h" #ifndef lint -FILE_RCSID("@(#)$File: readelf.c,v 1.139 2017/09/08 13:44:09 christos Exp $") +FILE_RCSID("@(#)$File: readelf.c,v 1.140 2017/11/02 20:25:39 christos Exp $") #endif #ifdef BUILTIN_ELF @@ -1571,9 +1571,11 @@ dophn_exec(struct magic_set *ms, int clazz, int swap, int fd, off_t off, protected int -file_tryelf(struct magic_set *ms, int fd, const unsigned char *buf, - size_t nbytes) +file_tryelf(struct magic_set *ms, const struct buffer *b) { + int fd = b->fd; + const unsigned char *buf = b->fbuf; + size_t nbytes = b->flen; union { int32_t l; char c[sizeof (int32_t)]; diff --git a/src/softmagic.c b/src/softmagic.c index 94d5b827..685b1270 100644 --- a/src/softmagic.c +++ b/src/softmagic.c @@ -32,7 +32,7 @@ #include "file.h" #ifndef lint -FILE_RCSID("@(#)$File: softmagic.c,v 1.250 2017/09/05 20:41:59 christos Exp $") +FILE_RCSID("@(#)$File: softmagic.c,v 1.251 2017/11/02 20:25:39 christos Exp $") #endif /* lint */ #include "magic.h" @@ -44,14 +44,15 @@ FILE_RCSID("@(#)$File: softmagic.c,v 1.250 2017/09/05 20:41:59 christos Exp $") #include "der.h" private int match(struct magic_set *, struct magic *, uint32_t, - const unsigned char *, size_t, size_t, int, int, int, uint16_t *, + const struct buffer *, size_t, int, int, int, uint16_t *, uint16_t *, int *, int *, int *); -private int mget(struct magic_set *, const unsigned char *, - struct magic *, size_t, size_t, unsigned int, int, int, int, uint16_t *, +private int mget(struct magic_set *, struct magic *, const struct buffer *, + size_t, unsigned int, int, int, int, uint16_t *, uint16_t *, int *, int *, int *); private int magiccheck(struct magic_set *, struct magic *); private int32_t mprint(struct magic_set *, struct magic *); -private int moffset(struct magic_set *, struct magic *, size_t, int32_t *); +private int moffset(struct magic_set *, struct magic *, const struct buffer *, + int32_t *); private void mdebug(uint32_t, const char *, size_t); private int mcopy(struct magic_set *, union VALUETYPE *, int, int, const unsigned char *, uint32_t, size_t, struct magic *); @@ -88,7 +89,7 @@ private int cvt_64(union VALUETYPE *, const struct magic *); */ /*ARGSUSED1*/ /* nbytes passed for regularity, maybe need later */ protected int -file_softmagic(struct magic_set *ms, const unsigned char *buf, size_t nbytes, +file_softmagic(struct magic_set *ms, const struct buffer *b, uint16_t *indir_count, uint16_t *name_count, int mode, int text) { struct mlist *ml; @@ -105,7 +106,7 @@ file_softmagic(struct magic_set *ms, const unsigned char *buf, size_t nbytes, } for (ml = ms->mlist[0]->next; ml != ms->mlist[0]; ml = ml->next) - if ((rv = match(ms, ml->magic, ml->nmagic, buf, nbytes, 0, mode, + if ((rv = match(ms, ml->magic, ml->nmagic, b, 0, mode, text, 0, indir_count, name_count, &printed_something, &need_separator, NULL)) != 0) return rv; @@ -161,7 +162,7 @@ file_fmtcheck(struct magic_set *ms, const struct magic *m, const char *def, */ private int match(struct magic_set *ms, struct magic *magic, uint32_t nmagic, - const unsigned char *s, size_t nbytes, size_t offset, int mode, int text, + const struct buffer *b, size_t offset, int mode, int text, int flip, uint16_t *indir_count, uint16_t *name_count, int *printed_something, int *need_separator, int *returnval) { @@ -200,7 +201,7 @@ flush: ms->line = m->lineno; /* if main entry matches, print it... */ - switch (mget(ms, s, m, nbytes, offset, cont_level, mode, text, + switch (mget(ms, m, b, offset, cont_level, mode, text, flip, indir_count, name_count, printed_something, need_separator, returnval)) { case -1: @@ -254,7 +255,7 @@ flush: if (print && mprint(ms, m) == -1) return -1; - switch (moffset(ms, m, nbytes, &ms->c.li[cont_level].off)) { + switch (moffset(ms, m, b, &ms->c.li[cont_level].off)) { case -1: case 0: goto flush; @@ -293,7 +294,7 @@ flush: continue; } #endif - switch (mget(ms, s, m, nbytes, offset, cont_level, mode, + switch (mget(ms, m, b, offset, cont_level, mode, text, flip, indir_count, name_count, printed_something, need_separator, returnval)) { case -1: @@ -366,7 +367,7 @@ flush: if (print && mprint(ms, m) == -1) return -1; - switch (moffset(ms, m, nbytes, + switch (moffset(ms, m, b, &ms->c.li[cont_level].off)) { case -1: case 0: @@ -717,8 +718,10 @@ mprint(struct magic_set *ms, struct magic *m) } private int -moffset(struct magic_set *ms, struct magic *m, size_t nbytes, int32_t *op) +moffset(struct magic_set *ms, struct magic *m, const struct buffer *b, + int32_t *op) { + size_t nbytes = b->flen; int32_t o; switch (m->type) { @@ -1337,11 +1340,14 @@ do_ops(struct magic *m, intmax_t lhs, intmax_t off) } private int -mget(struct magic_set *ms, const unsigned char *s, struct magic *m, - size_t nbytes, size_t o, unsigned int cont_level, int mode, int text, +mget(struct magic_set *ms, struct magic *m, const struct buffer *b, + size_t o, unsigned int cont_level, int mode, int text, int flip, uint16_t *indir_count, uint16_t *name_count, int *printed_something, int *need_separator, int *returnval) { + struct buffer bb; + const unsigned char *s = b->fbuf; + size_t nbytes = b->flen; uint32_t offset = ms->offset; intmax_t lhs; file_pushbuf_t *pb; @@ -1559,7 +1565,10 @@ mget(struct magic_set *ms, const unsigned char *s, struct magic *m, return -1; (*indir_count)++; - rv = file_softmagic(ms, s + offset, nbytes - offset, + bb = *b; + bb.fbuf = s + offset; + bb.flen = nbytes - offset; + rv = file_softmagic(ms, &bb, indir_count, name_count, BINTEST, text); if ((ms->flags & MAGIC_DEBUG) != 0) @@ -1599,7 +1608,7 @@ mget(struct magic_set *ms, const unsigned char *s, struct magic *m, oneed_separator = *need_separator; if (m->flag & NOSPACE) *need_separator = 0; - rv = match(ms, ml.magic, ml.nmagic, s, nbytes, offset + o, + rv = match(ms, ml.magic, ml.nmagic, b, offset + o, mode, text, flip, indir_count, name_count, printed_something, need_separator, returnval); if (rv != 1) -- 2.40.0