Merge remote-tracking branch 'weltling/libmagic_upgrade'

* weltling/libmagic_upgrade: (29 commits)
  partially revert wrong merge
  after merge fix
  updated libmagic.patch
  catching up
  catching up
  regenerated data file
  more after patch fixes
  restore good patch parts
  stat is done by php
  use zend_strndup
  started to patch for libmagic.next
  fix test outputs
  catch up with the previous cve-2014-3538 patch
  catching up
  catching up
  catching up
  updated the test magic file
  add patchlevel info
  portable off_t
  regenerated data file
  ...
This commit is contained in:
Anatol Belski 2015-03-08 20:31:02 +01:00
commit a44ed26dcf
29 changed files with 87701 additions and 69149 deletions

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -1,15 +1,15 @@
/*
* Adapted from: apptype.c, Written by Eberhard Mattes and put into the
* public domain
*
*
* Notes: 1. Qualify the filename so that DosQueryAppType does not do extraneous
* searches.
*
*
* 2. DosQueryAppType will return FAPPTYP_DOS on a file ending with ".com"
* (other than an OS/2 exe or Win exe with this name). Eberhard Mattes
* remarks Tue, 6 Apr 93: Moreover, it reports the type of the (new and very
* bug ridden) Win Emacs as "OS/2 executable".
*
*
* 3. apptype() uses the filename if given, otherwise a tmp file is created with
* the contents of buf. If buf is not the complete file, apptype can
* incorrectly identify the exe type. The "-z" option of "file" is the reason
@ -18,16 +18,16 @@
/*
* amai: Darrel Hankerson did the changes described here.
*
*
* It remains to check the validity of comments (2.) since it's referred to an
* "old" OS/2 version.
*
*
*/
#include "file.h"
#ifndef lint
FILE_RCSID("@(#)$File: apptype.c,v 1.13 2011/09/07 21:57:15 christos Exp $")
FILE_RCSID("@(#)$File: apptype.c,v 1.12 2011/08/28 07:03:27 christos Exp $")
#endif /* lint */
#include <stdlib.h>

View File

@ -35,7 +35,7 @@
#include "file.h"
#ifndef lint
FILE_RCSID("@(#)$File: ascmagic.c,v 1.88 2014/02/12 23:20:53 christos Exp $")
FILE_RCSID("@(#)$File: ascmagic.c,v 1.90 2014/11/28 02:35:05 christos Exp $")
#endif /* lint */
#include "magic.h"
@ -147,7 +147,8 @@ file_ascmagic_with_encoding(struct magic_set *ms, const unsigned char *buf,
== NULL)
goto done;
if ((rv = file_softmagic(ms, utf8_buf,
(size_t)(utf8_end - utf8_buf), 0, TEXTTEST, text)) == 0)
(size_t)(utf8_end - utf8_buf), 0, NULL,
TEXTTEST, text)) == 0)
rv = -1;
}

View File

@ -35,7 +35,7 @@
#include "file.h"
#ifndef lint
FILE_RCSID("@(#)$File: cdf.c,v 1.55 2014/02/27 23:26:17 christos Exp $")
FILE_RCSID("@(#)$File: cdf.c,v 1.73 2015/01/11 16:58:25 christos Exp $")
#endif
#include <assert.h>
@ -83,6 +83,11 @@ static union {
#define CDF_TOLE8(x) ((uint64_t)(NEED_SWAP ? _cdf_tole8(x) : (uint64_t)(x)))
#define CDF_TOLE4(x) ((uint32_t)(NEED_SWAP ? _cdf_tole4(x) : (uint32_t)(x)))
#define CDF_TOLE2(x) ((uint16_t)(NEED_SWAP ? _cdf_tole2(x) : (uint16_t)(x)))
#define CDF_TOLE(x) (/*CONSTCOND*/sizeof(x) == 2 ? \
CDF_TOLE2(CAST(uint16_t, x)) : \
(/*CONSTCOND*/sizeof(x) == 4 ? \
CDF_TOLE4(CAST(uint32_t, x)) : \
CDF_TOLE8(CAST(uint64_t, x))))
#define CDF_GETUINT32(x, y) cdf_getuint32(x, y)
@ -279,7 +284,7 @@ cdf_check_stream_offset(const cdf_stream_t *sst, const cdf_header_t *h,
const char *e = ((const char *)p) + tail;
size_t ss = sst->sst_dirlen < h->h_min_size_standard_stream ?
CDF_SHORT_SEC_SIZE(h) : CDF_SEC_SIZE(h);
(void)&line;
/*LINTED*/(void)&line;
if (e >= b && (size_t)(e - b) <= ss * sst->sst_len)
return 0;
DPRINTF(("%d: offset begin %p < end %p || %" SIZE_T_FORMAT "u"
@ -474,6 +479,12 @@ cdf_count_chain(const cdf_sat_t *sat, cdf_secid_t sid, size_t size)
/ sizeof(maxsector));
DPRINTF(("Chain:"));
if (sid == CDF_SECID_END_OF_CHAIN) {
/* 0-length chain. */
DPRINTF((" empty\n"));
return 0;
}
for (j = i = 0; sid >= 0; i++, j++) {
DPRINTF((" %d", sid));
if (j >= CDF_LOOP_LIMIT) {
@ -488,6 +499,11 @@ cdf_count_chain(const cdf_sat_t *sat, cdf_secid_t sid, size_t size)
}
sid = CDF_TOLE4((uint32_t)sat->sat_tab[sid]);
}
if (i == 0) {
DPRINTF((" none, sid: %d\n", sid));
return (size_t)-1;
}
DPRINTF(("\n"));
return i;
}
@ -735,26 +751,44 @@ cdf_read_summary_info(const cdf_info_t *info, const cdf_header_t *h,
const cdf_sat_t *sat, const cdf_sat_t *ssat, const cdf_stream_t *sst,
const cdf_dir_t *dir, cdf_stream_t *scn)
{
size_t i;
return cdf_read_user_stream(info, h, sat, ssat, sst, dir,
"\05SummaryInformation", scn);
}
int
cdf_read_user_stream(const cdf_info_t *info, const cdf_header_t *h,
const cdf_sat_t *sat, const cdf_sat_t *ssat, const cdf_stream_t *sst,
const cdf_dir_t *dir, const char *name, cdf_stream_t *scn)
{
const cdf_directory_t *d;
static const char name[] = "\05SummaryInformation";
int i = cdf_find_stream(dir, name, CDF_DIR_TYPE_USER_STREAM);
for (i = dir->dir_len; i > 0; i--)
if (dir->dir_tab[i - 1].d_type == CDF_DIR_TYPE_USER_STREAM &&
cdf_namecmp(name, dir->dir_tab[i - 1].d_name, sizeof(name))
== 0)
break;
if (i == 0) {
DPRINTF(("Cannot find summary information section\n"));
errno = ESRCH;
if (i <= 0)
return -1;
}
d = &dir->dir_tab[i - 1];
return cdf_read_sector_chain(info, h, sat, ssat, sst,
d->d_stream_first_sector, d->d_size, scn);
}
int
cdf_find_stream(const cdf_dir_t *dir, const char *name, int type)
{
size_t i, name_len = strlen(name) + 1;
for (i = dir->dir_len; i > 0; i--)
if (dir->dir_tab[i - 1].d_type == type &&
cdf_namecmp(name, dir->dir_tab[i - 1].d_name, name_len)
== 0)
break;
if (i > 0)
return i;
DPRINTF(("Cannot find type %d `%s'\n", type, name));
errno = ESRCH;
return 0;
}
int
cdf_read_property_info(const cdf_stream_t *sst, const cdf_header_t *h,
uint32_t offs, cdf_property_info_t **info, size_t *count, size_t *maxcount)
@ -815,7 +849,8 @@ cdf_read_property_info(const cdf_stream_t *sst, const cdf_header_t *h,
if (cdf_check_stream_offset(sst, h, e, 0, __LINE__) == -1)
goto out;
for (i = 0; i < sh.sh_properties; i++) {
size_t ofs, tail = (i << 1) + 1;
size_t tail = (i << 1) + 1;
size_t ofs;
if (cdf_check_stream_offset(sst, h, p, tail * sizeof(uint32_t),
__LINE__) == -1)
goto out;
@ -823,7 +858,11 @@ cdf_read_property_info(const cdf_stream_t *sst, const cdf_header_t *h,
q = (const uint8_t *)(const void *)
((const char *)(const void *)p + ofs
- 2 * sizeof(uint32_t));
if (q < p || q > e) {
if (q < p) {
DPRINTF(("Wrapped around %p < %p\n", q, p));
goto out;
}
if (q > e) {
DPRINTF(("Ran of the end %p > %p\n", q, e));
goto out;
}
@ -978,12 +1017,77 @@ cdf_unpack_summary_info(const cdf_stream_t *sst, const cdf_header_t *h,
maxcount = 0;
*info = NULL;
if (cdf_read_property_info(sst, h, CDF_TOLE4(sd->sd_offset), info,
count, &maxcount) == -1)
return -1;
count, &maxcount) == -1)
return -1;
return 0;
}
#define extract_catalog_field(t, f, l) \
if (b + l + sizeof(cep->f) > eb) { \
cep->ce_namlen = 0; \
break; \
} \
memcpy(&cep->f, b + (l), sizeof(cep->f)); \
ce[i].f = CAST(t, CDF_TOLE(cep->f))
int
cdf_unpack_catalog(const cdf_header_t *h, const cdf_stream_t *sst,
cdf_catalog_t **cat)
{
size_t ss = sst->sst_dirlen < h->h_min_size_standard_stream ?
CDF_SHORT_SEC_SIZE(h) : CDF_SEC_SIZE(h);
const char *b = CAST(const char *, sst->sst_tab);
const char *eb = b + ss * sst->sst_len;
size_t nr, i, k;
cdf_catalog_entry_t *ce;
uint16_t reclen;
const uint16_t *np;
for (nr = 0; b < eb; nr++) {
memcpy(&reclen, b, sizeof(reclen));
reclen = CDF_TOLE2(reclen);
if (reclen == 0)
break;
b += reclen;
}
*cat = CAST(cdf_catalog_t *,
malloc(sizeof(cdf_catalog_t) + nr * sizeof(*ce)));
(*cat)->cat_num = nr;
ce = (*cat)->cat_e;
memset(ce, 0, nr * sizeof(*ce));
b = CAST(const char *, sst->sst_tab);
for (i = 0; i < nr; i++, b += reclen) {
cdf_catalog_entry_t *cep = &ce[i];
uint16_t rlen;
extract_catalog_field(uint16_t, ce_namlen, 0);
extract_catalog_field(uint16_t, ce_num, 2);
extract_catalog_field(uint64_t, ce_timestamp, 6);
reclen = cep->ce_namlen;
if (reclen < 14) {
cep->ce_namlen = 0;
continue;
}
cep->ce_namlen = __arraycount(cep->ce_name) - 1;
rlen = reclen - 14;
if (cep->ce_namlen > rlen)
cep->ce_namlen = rlen;
np = CAST(const uint16_t *, CAST(const void *, (b + 16)));
if (CAST(const char *, np + cep->ce_namlen) > eb) {
cep->ce_namlen = 0;
break;
}
for (k = 0; k < cep->ce_namlen; k++)
cep->ce_name[k] = np[k]; /* XXX: CDF_TOLE2? */
cep->ce_name[cep->ce_namlen] = 0;
}
return 0;
}
int
cdf_print_classid(char *buf, size_t buflen, const cdf_classid_t *id)
@ -1067,6 +1171,15 @@ cdf_print_elapsed_time(char *buf, size_t bufsiz, cdf_timestamp_t ts)
return len;
}
char *
cdf_u16tos8(char *buf, size_t len, const uint16_t *p)
{
size_t i;
for (i = 0; i < len && p[i]; i++)
buf[i] = (char)p[i];
buf[i] = '\0';
return buf;
}
#ifdef CDF_DEBUG
void
@ -1092,7 +1205,7 @@ cdf_dump_header(const cdf_header_t *h)
for (i = 0; i < __arraycount(h->h_master_sat); i++) {
if (h->h_master_sat[i] == CDF_SECID_FREE)
break;
(void)fprintf(stderr, "%35.35s[%.3zu] = %d\n",
(void)fprintf(stderr, "%35.35s[%.3" SIZE_T_FORMAT "u] = %d\n",
"master_sat", i, h->h_master_sat[i]);
}
}
@ -1117,11 +1230,12 @@ cdf_dump_sat(const char *prefix, const cdf_sat_t *sat, size_t size)
}
void
cdf_dump(void *v, size_t len)
cdf_dump(const void *v, size_t len)
{
size_t i, j;
unsigned char *p = v;
const unsigned char *p = v;
char abuf[16];
(void)fprintf(stderr, "%.4x: ", 0);
for (i = 0, j = 0; i < len; i++, p++) {
(void)fprintf(stderr, "%.2x ", *p);
@ -1159,6 +1273,7 @@ cdf_dump_dir(const cdf_info_t *info, const cdf_header_t *h,
"user stream", "lockbytes", "property", "root storage" };
for (i = 0; i < dir->dir_len; i++) {
char buf[26];
d = &dir->dir_tab[i];
for (j = 0; j < sizeof(name); j++)
name[j] = (char)CDF_TOLE2(d->d_name[j]);
@ -1174,9 +1289,10 @@ cdf_dump_dir(const cdf_info_t *info, const cdf_header_t *h,
(void)fprintf(stderr, "Right child: %d\n", d->d_right_child);
(void)fprintf(stderr, "Flags: 0x%x\n", d->d_flags);
cdf_timestamp_to_timespec(&ts, d->d_created);
(void)fprintf(stderr, "Created %s", cdf_ctime(&ts.tv_sec));
(void)fprintf(stderr, "Created %s", cdf_ctime(&ts.tv_sec, buf));
cdf_timestamp_to_timespec(&ts, d->d_modified);
(void)fprintf(stderr, "Modified %s", cdf_ctime(&ts.tv_sec));
(void)fprintf(stderr, "Modified %s",
cdf_ctime(&ts.tv_sec, buf));
(void)fprintf(stderr, "Stream %d\n", d->d_stream_first_sector);
(void)fprintf(stderr, "Size %d\n", d->d_size);
switch (d->d_type) {
@ -1258,9 +1374,10 @@ cdf_dump_property_info(const cdf_property_info_t *info, size_t count)
cdf_print_elapsed_time(buf, sizeof(buf), tp);
(void)fprintf(stderr, "timestamp %s\n", buf);
} else {
char tbuf[26];
cdf_timestamp_to_timespec(&ts, tp);
(void)fprintf(stderr, "timestamp %s",
cdf_ctime(&ts.tv_sec));
cdf_ctime(&ts.tv_sec, tbuf));
}
break;
case CDF_CLIPBOARD:
@ -1288,7 +1405,7 @@ cdf_dump_summary_info(const cdf_header_t *h, const cdf_stream_t *sst)
return;
(void)fprintf(stderr, "Endian: %x\n", ssi.si_byte_order);
(void)fprintf(stderr, "Os Version %d.%d\n", ssi.si_os_version & 0xff,
ssi.si_os_version >> 8);
ssi.si_os_version >> 8);
(void)fprintf(stderr, "Os %d\n", ssi.si_os);
cdf_print_classid(buf, sizeof(buf), &ssi.si_class);
(void)fprintf(stderr, "Class %s\n", buf);
@ -1297,6 +1414,27 @@ cdf_dump_summary_info(const cdf_header_t *h, const cdf_stream_t *sst)
free(info);
}
void
cdf_dump_catalog(const cdf_header_t *h, const cdf_stream_t *sst)
{
cdf_catalog_t *cat;
cdf_unpack_catalog(h, sst, &cat);
const cdf_catalog_entry_t *ce = cat->cat_e;
struct timespec ts;
char tbuf[64], sbuf[256];
size_t i;
printf("Catalog:\n");
for (i = 0; i < cat->cat_num; i++) {
cdf_timestamp_to_timespec(&ts, ce[i].ce_timestamp);
printf("\t%d %s %s", ce[i].ce_num,
cdf_u16tos8(sbuf, ce[i].ce_namlen, ce[i].ce_name),
cdf_ctime(&ts.tv_sec, tbuf));
}
free(cat);
}
#endif
#ifdef TEST
@ -1309,6 +1447,7 @@ main(int argc, char *argv[])
cdf_stream_t sst, scn;
cdf_dir_t dir;
cdf_info_t info;
const cdf_directory_t *root;
if (argc < 2) {
(void)fprintf(stderr, "Usage: %s <filename>\n", getprogname());
@ -1342,7 +1481,8 @@ main(int argc, char *argv[])
if (cdf_read_dir(&info, &h, &sat, &dir) == -1)
err(1, "Cannot read dir");
if (cdf_read_short_stream(&info, &h, &sat, &dir, &sst) == -1)
if (cdf_read_short_stream(&info, &h, &sat, &dir, &sst, &root)
== -1)
err(1, "Cannot read short stream");
#ifdef CDF_DEBUG
cdf_dump_stream(&h, &sst);
@ -1355,9 +1495,17 @@ main(int argc, char *argv[])
if (cdf_read_summary_info(&info, &h, &sat, &ssat, &sst, &dir,
&scn) == -1)
err(1, "Cannot read summary info");
warn("Cannot read summary info");
#ifdef CDF_DEBUG
cdf_dump_summary_info(&h, &scn);
else
cdf_dump_summary_info(&h, &scn);
#endif
if (cdf_read_catalog(&info, &h, &sat, &ssat, &sst, &dir,
&scn) == -1)
warn("Cannot read catalog");
#ifdef CDF_DEBUG
else
cdf_dump_catalog(&h, &scn);
#endif
(void)close(info.i_fd);

View File

@ -273,6 +273,19 @@ typedef struct {
size_t i_len;
} cdf_info_t;
typedef struct {
uint16_t ce_namlen;
uint32_t ce_num;
uint64_t ce_timestamp;
uint16_t ce_name[256];
} cdf_catalog_entry_t;
typedef struct {
size_t cat_num;
cdf_catalog_entry_t cat_e[0];
} cdf_catalog_t;
struct timeval;
int cdf_timestamp_to_timespec(struct timeval *, cdf_timestamp_t);
int cdf_timespec_to_timestamp(cdf_timestamp_t *, const struct timeval *);
@ -304,11 +317,17 @@ int cdf_read_short_stream(const cdf_info_t *, const cdf_header_t *,
const cdf_directory_t **);
int cdf_read_property_info(const cdf_stream_t *, const cdf_header_t *, uint32_t,
cdf_property_info_t **, size_t *, size_t *);
int cdf_read_user_stream(const cdf_info_t *, const cdf_header_t *,
const cdf_sat_t *, const cdf_sat_t *, const cdf_stream_t *,
const cdf_dir_t *, const char *, cdf_stream_t *);
int cdf_find_stream(const cdf_dir_t *, const char *, int);
int cdf_read_summary_info(const cdf_info_t *, const cdf_header_t *,
const cdf_sat_t *, const cdf_sat_t *, const cdf_stream_t *,
const cdf_dir_t *, cdf_stream_t *);
int cdf_unpack_summary_info(const cdf_stream_t *, const cdf_header_t *,
cdf_summary_info_header_t *, cdf_property_info_t **, size_t *);
int cdf_unpack_catalog(const cdf_header_t *, const cdf_stream_t *,
cdf_catalog_t **);
int cdf_print_classid(char *, size_t, const cdf_classid_t *);
int cdf_print_property_name(char *, size_t, uint32_t);
int cdf_print_elapsed_time(char *, size_t, cdf_timestamp_t);
@ -316,16 +335,18 @@ uint16_t cdf_tole2(uint16_t);
uint32_t cdf_tole4(uint32_t);
uint64_t cdf_tole8(uint64_t);
char *cdf_ctime(const time_t *, char *);
char *cdf_u16tos8(char *, size_t, const uint16_t *);
#ifdef CDF_DEBUG
void cdf_dump_header(const cdf_header_t *);
void cdf_dump_sat(const char *, const cdf_sat_t *, size_t);
void cdf_dump(void *, size_t);
void cdf_dump(const void *, size_t);
void cdf_dump_stream(const cdf_header_t *, const cdf_stream_t *);
void cdf_dump_dir(const cdf_info_t *, const cdf_header_t *, const cdf_sat_t *,
const cdf_sat_t *, const cdf_stream_t *, const cdf_dir_t *);
void cdf_dump_property_info(const cdf_property_info_t *, size_t);
void cdf_dump_summary_info(const cdf_header_t *, const cdf_stream_t *);
void cdf_dump_catalog(const cdf_header_t *, const cdf_stream_t *);
#endif

View File

@ -27,7 +27,7 @@
#include "file.h"
#ifndef lint
FILE_RCSID("@(#)$File: cdf_time.c,v 1.12 2012/05/15 17:14:36 christos Exp $")
FILE_RCSID("@(#)$File: cdf_time.c,v 1.14 2014/04/17 12:44:01 christos Exp $")
#endif
#include <time.h>
@ -56,7 +56,7 @@ cdf_getdays(int year)
for (y = CDF_BASE_YEAR; y < year; y++)
days += isleap(y) + 365;
return days;
}
@ -77,7 +77,7 @@ cdf_getday(int year, int days)
return days;
}
/*
/*
* Return the 0...11 month number.
*/
static int
@ -172,12 +172,13 @@ cdf_ctime(const time_t *sec, char *buf)
char *ptr = ctime_r(sec, buf);
if (ptr != NULL)
return buf;
(void)snprintf(buf, 26, "*Bad* 0x%16.16llx\n", (long long)*sec);
(void)snprintf(buf, 26, "*Bad* 0x%16.16" INT64_T_FORMAT "x\n",
(long long)*sec);
return buf;
}
#ifdef TEST
#ifdef TEST_TIME
int
main(int argc, char *argv[])
{

View File

@ -2,7 +2,7 @@
* 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:
@ -12,7 +12,7 @@
* 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
@ -29,14 +29,14 @@
* compress routines:
* zmagic() - returns 0 if not recognized, uncompresses and prints
* information if recognized
* uncompress(method, old, n, newch) - uncompress old into new,
* uncompress(method, old, n, newch) - uncompress old into new,
* using method, return sizeof new
*/
#include "config.h"
#include "file.h"
#ifndef lint
FILE_RCSID("@(#)$File: compress.c,v 1.73 2014/01/05 15:55:21 christos Exp $")
FILE_RCSID("@(#)$File: compress.c,v 1.77 2014/12/12 16:33:01 christos Exp $")
#endif
#include "magic.h"
@ -46,7 +46,7 @@ FILE_RCSID("@(#)$File: compress.c,v 1.73 2014/01/05 15:55:21 christos Exp $")
#endif
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <signal.h>
#ifndef PHP_WIN32
#include <sys/ioctl.h>
#endif
@ -107,13 +107,12 @@ file_zmagic(struct magic_set *ms, int fd, const char *name,
size_t i, nsz;
int rv = 0;
int mime = ms->flags & MAGIC_MIME;
size_t ncompr;
sig_t osigpipe;
if ((ms->flags & MAGIC_COMPRESS) == 0)
return 0;
ncompr = sizeof(compr) / sizeof(compr[0]);
osigpipe = signal(SIGPIPE, SIG_IGN);
for (i = 0; i < ncompr; i++) {
if (nbytes < compr[i].maglen)
continue;
@ -140,6 +139,8 @@ file_zmagic(struct magic_set *ms, int fd, const char *name,
}
}
error:
(void)signal(SIGPIPE, osigpipe);
if (newbuf)
efree(newbuf);
ms->flags |= MAGIC_COMPRESS;
@ -347,7 +348,7 @@ uncompressgzipped(struct magic_set *ms, const unsigned char *old,
if ((*newch = CAST(unsigned char *, emalloc(HOWMANY + 1))) == NULL) {
return 0;
}
/* XXX: const castaway, via strchr */
z.next_in = (Bytef *)strchr((const char *)old + data_start,
old[data_start]);
@ -373,7 +374,7 @@ uncompressgzipped(struct magic_set *ms, const unsigned char *old,
n = (size_t)z.total_out;
(void)inflateEnd(&z);
/* let's keep the nul-terminate tradition */
(*newch)[n] = '\0';
@ -386,8 +387,8 @@ uncompressbuf(struct magic_set *ms, int fd, size_t method,
const unsigned char *old, unsigned char **newch, size_t n)
{
int fdin[2], fdout[2];
int status;
ssize_t r;
pid_t pid;
#ifdef BUILTIN_DECOMPRESS
/* FIXME: This doesn't cope with bzip2 */
@ -398,10 +399,10 @@ uncompressbuf(struct magic_set *ms, int fd, size_t method,
(void)fflush(stderr);
if ((fd != -1 && pipe(fdin) == -1) || pipe(fdout) == -1) {
file_error(ms, errno, "cannot create pipe");
file_error(ms, errno, "cannot create pipe");
return NODATA;
}
switch (pid = fork()) {
switch (fork()) {
case 0: /* child */
(void) close(0);
if (fd != -1) {
@ -438,7 +439,7 @@ uncompressbuf(struct magic_set *ms, int fd, size_t method,
(void) close(fdout[1]);
if (fd == -1) {
(void) close(fdin[0]);
/*
/*
* fork again, to avoid blocking because both
* pipes filled
*/
@ -479,7 +480,7 @@ uncompressbuf(struct magic_set *ms, int fd, size_t method,
strerror(errno));
#endif
efree(*newch);
n = 0;
n = NODATA-;
*newch = NULL;
goto err;
} else {
@ -497,8 +498,9 @@ err:
#else
(void)wait(NULL);
#endif
(void) close(fdin[0]);
(void) close(fdin[0]);
return n;
}
}

View File

@ -1,7 +1,7 @@
/*
* Copyright (c) Christos Zoulas 2008.
* All Rights Reserved.
*
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
@ -11,7 +11,7 @@
* 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
@ -32,39 +32,51 @@
swap = (u.c[sizeof(int32_t) - 1] + 1) != elfhdr.e_ident[EI_DATA];
type = elf_getu16(swap, elfhdr.e_type);
notecount = ms->elf_notes_max;
switch (type) {
#ifdef ELFCORE
case ET_CORE:
phnum = elf_getu16(swap, elfhdr.e_phnum);
if (phnum > ms->elf_phnum_max)
return toomany(ms, "program headers", phnum);
flags |= FLAGS_IS_CORE;
if (dophn_core(ms, clazz, swap, fd,
(zend_off_t)elf_getu(swap, elfhdr.e_phoff),
elf_getu16(swap, elfhdr.e_phnum),
(zend_off_t)elf_getu(swap, elfhdr.e_phoff), phnum,
(size_t)elf_getu16(swap, elfhdr.e_phentsize),
fsize, &flags) == -1)
fsize, &flags, &notecount) == -1)
return -1;
break;
#endif
case ET_EXEC:
case ET_DYN:
phnum = elf_getu16(swap, elfhdr.e_phnum);
if (phnum > ms->elf_phnum_max)
return toomany(ms, "program", phnum);
shnum = elf_getu16(swap, elfhdr.e_shnum);
if (shnum > ms->elf_shnum_max)
return toomany(ms, "section", shnum);
if (dophn_exec(ms, clazz, swap, fd,
(zend_off_t)elf_getu(swap, elfhdr.e_phoff),
elf_getu16(swap, elfhdr.e_phnum),
(zend_off_t)elf_getu(swap, elfhdr.e_phoff), phnum,
(size_t)elf_getu16(swap, elfhdr.e_phentsize),
fsize, &flags, elf_getu16(swap, elfhdr.e_shnum))
== -1)
fsize, shnum, &flags, &notecount) == -1)
return -1;
/*FALLTHROUGH*/
case ET_REL:
shnum = elf_getu16(swap, elfhdr.e_shnum);
if (shnum > ms->elf_shnum_max)
return toomany(ms, "section headers", shnum);
if (doshn(ms, clazz, swap, fd,
(zend_off_t)elf_getu(swap, elfhdr.e_shoff),
elf_getu16(swap, elfhdr.e_shnum),
(zend_off_t)elf_getu(swap, elfhdr.e_shoff), shnum,
(size_t)elf_getu16(swap, elfhdr.e_shentsize),
fsize, &flags, elf_getu16(swap, elfhdr.e_machine),
(int)elf_getu16(swap, elfhdr.e_shstrndx)) == -1)
fsize, elf_getu16(swap, elfhdr.e_machine),
(int)elf_getu16(swap, elfhdr.e_shstrndx),
&flags, &notecount) == -1)
return -1;
break;
default:
break;
}
if (notecount == 0)
return toomany(ms, "notes", ms->elf_notes_max);
return 1;

View File

@ -35,7 +35,7 @@
#include "file.h"
#ifndef lint
FILE_RCSID("@(#)$File: encoding.c,v 1.9 2013/11/19 20:45:50 christos Exp $")
FILE_RCSID("@(#)$File: encoding.c,v 1.10 2014/09/11 12:08:52 christos Exp $")
#endif /* lint */
#include "magic.h"
@ -97,7 +97,6 @@ file_encoding(struct magic_set *ms, const unsigned char *buf, size_t nbytes, uni
*code_mime = "utf-8";
} else if (file_looks_utf8(buf, nbytes, *ubuf, ulen) > 1) {
DPRINTF(("utf8 %" SIZE_T_FORMAT "u\n", *ulen));
*code = "UTF-8 Unicode (with BOM)";
*code = "UTF-8 Unicode";
*code_mime = "utf-8";
} else if ((ucs_type = looks_ucs16(buf, nbytes, *ubuf, ulen)) != 0) {
@ -200,8 +199,8 @@ file_encoding(struct magic_set *ms, const unsigned char *buf, size_t nbytes, uni
#define X 3 /* character appears in non-ISO extended ASCII (Mac, IBM PC) */
private char text_chars[256] = {
/* BEL BS HT LF FF CR */
F, F, F, F, F, F, F, T, T, T, T, F, T, T, F, F, /* 0x0X */
/* BEL BS HT LF VT FF CR */
F, F, F, F, F, F, F, T, T, T, T, T, T, T, F, F, /* 0x0X */
/* ESC */
F, F, F, F, F, F, F, F, F, F, F, T, F, F, F, F, /* 0x1X */
T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, /* 0x2X */

View File

@ -27,7 +27,7 @@
*/
/*
* file.h - definitions for file(1) program
* @(#)$File: file.h,v 1.148 2014/02/12 23:20:53 christos Exp $
* @(#)$File: file.h,v 1.166 2015/01/09 19:28:32 christos Exp $
*/
#ifndef __file_h__
@ -91,7 +91,7 @@
#define private static
#if HAVE_VISIBILITY
#if HAVE_VISIBILITY && !defined(WIN32)
#define public __attribute__ ((__visibility__("default")))
#ifndef protected
#define protected __attribute__ ((__visibility__("hidden")))
@ -126,7 +126,7 @@
#endif
#ifndef HOWMANY
# define HOWMANY (256 * 1024) /* how much of the file to look at */
# define HOWMANY (1024 * 1024) /* how much of the file to look at */
#endif
#define MAXMAGIS 8192 /* max entries in any one magic file
or directory */
@ -135,7 +135,7 @@
#define MAXstring 64 /* max len of "string" types */
#define MAGICNO 0xF11E041C
#define VERSIONNO 11
#define VERSIONNO 12
#define FILE_MAGICSIZE 248
#define FILE_LOAD 0
@ -234,6 +234,7 @@ struct magic {
(t) == FILE_LESTRING16 || \
(t) == FILE_REGEX || \
(t) == FILE_SEARCH || \
(t) == FILE_INDIRECT || \
(t) == FILE_NAME || \
(t) == FILE_USE)
@ -323,6 +324,7 @@ struct magic {
#define PSTRING_2_LE BIT(9)
#define PSTRING_4_BE BIT(10)
#define PSTRING_4_LE BIT(11)
#define REGEX_LINE_COUNT BIT(11)
#define PSTRING_LEN \
(PSTRING_1_BE|PSTRING_2_LE|PSTRING_2_BE|PSTRING_4_LE|PSTRING_4_BE)
#define PSTRING_LENGTH_INCLUDES_ITSELF BIT(12)
@ -345,6 +347,8 @@ struct magic {
#define STRING_IGNORE_CASE (STRING_IGNORE_LOWERCASE|STRING_IGNORE_UPPERCASE)
#define STRING_DEFAULT_RANGE 100
#define INDIRECT_RELATIVE BIT(0)
#define CHAR_INDIRECT_RELATIVE 'r'
/* list of magic entries */
struct mlist {
@ -402,6 +406,16 @@ struct magic_set {
/* FIXME: Make the string dynamically allocated so that e.g.
strings matched in files can be longer than MAXstring */
union VALUETYPE ms_value; /* either number or string */
uint16_t indir_max;
uint16_t name_max;
uint16_t elf_shnum_max;
uint16_t elf_phnum_max;
uint16_t elf_notes_max;
#define FILE_INDIR_MAX 15
#define FILE_NAME_MAX 30
#define FILE_ELF_SHNUM_MAX 32768
#define FILE_ELF_PHNUM_MAX 128
#define FILE_ELF_NOTES_MAX 256
};
/* Type for Unicode characters */
@ -416,6 +430,7 @@ protected int file_buffer(struct magic_set *, php_stream *, const char *, const
size_t);
protected int file_fsmagic(struct magic_set *, const char *, zend_stat_t *, php_stream *);
protected int file_pipe2file(struct magic_set *, int, const void *, size_t);
protected size_t file_printedlen(const struct magic_set *);
protected int file_replace(struct magic_set *, const char *, const char *);
protected int file_printf(struct magic_set *, const char *, ...);
protected int file_reset(struct magic_set *);
@ -434,8 +449,10 @@ protected int file_encoding(struct magic_set *, const unsigned char *, size_t,
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,
size_t, int, int);
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 **,
size_t *, size_t);
protected int file_magicfind(struct magic_set *, const char *, struct mlist *);
protected uint64_t file_signextend(struct magic_set *, struct magic *,
uint64_t);
@ -455,12 +472,20 @@ protected int file_looks_utf8(const unsigned char *, size_t, unichar *,
size_t *);
protected size_t file_pstring_length_size(const struct magic *);
protected size_t file_pstring_get_length(const struct magic *, const char *);
protected size_t file_printedlen(const struct magic_set *ms);
protected char * file_printable(char *, size_t, const char *);
#ifdef __EMX__
protected int file_os2_apptype(struct magic_set *, const char *, const void *,
size_t);
#endif /* __EMX__ */
typedef struct {
char *buf;
uint32_t offset;
} file_pushbuf_t;
protected file_pushbuf_t *file_push_buffer(struct magic_set *);
protected char *file_pop_buffer(struct magic_set *, file_pushbuf_t *);
extern const char *file_names[];
extern const size_t file_nnames;
@ -524,5 +549,8 @@ static const char *rcsid(const char *p) { \
#define FINFO_LSEEK_FUNC lseek
#define FINFO_READ_FUNC read
#endif
#ifndef __RCSID
#define __RCSID(a)
#endif
#endif /* __file_h__ */

View File

@ -2,7 +2,7 @@
* 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:
@ -12,7 +12,7 @@
* 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
@ -32,7 +32,7 @@
#include "file.h"
#ifndef lint
FILE_RCSID("@(#)$File: fsmagic.c,v 1.71 2013/12/01 18:01:07 christos Exp $")
FILE_RCSID("@(#)$File: fsmagic.c,v 1.74 2014/10/13 20:21:49 christos Exp $")
#endif /* lint */
#include "magic.h"
@ -53,6 +53,10 @@ FILE_RCSID("@(#)$File: fsmagic.c,v 1.71 2013/12/01 18:01:07 christos Exp $")
#ifdef major /* Might be defined in sys/types.h. */
# define HAVE_MAJOR
#endif
#ifdef WIN32
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
#endif
#ifndef HAVE_MAJOR
# define major(dev) (((dev) >> 8) & 0xff)
@ -132,17 +136,17 @@ file_fsmagic(struct magic_set *ms, const char *fn, zend_stat_t *sb, php_stream *
return -1;
#endif
#ifdef S_ISGID
if (sb->st_mode & S_ISGID)
if (sb->st_mode & S_ISGID)
if (file_printf(ms, "%ssetgid", COMMA) == -1)
return -1;
#endif
#ifdef S_ISVTX
if (sb->st_mode & S_ISVTX)
if (sb->st_mode & S_ISVTX)
if (file_printf(ms, "%ssticky", COMMA) == -1)
return -1;
#endif
}
switch (sb->st_mode & S_IFMT) {
#ifndef PHP_WIN32
# ifdef S_IFCHR

View File

@ -27,7 +27,7 @@
#include "file.h"
#ifndef lint
FILE_RCSID("@(#)$File: funcs.c,v 1.68 2014/02/18 11:09:31 kim Exp $")
FILE_RCSID("@(#)$File: funcs.c,v 1.79 2014/12/16 20:52:49 christos Exp $")
#endif /* lint */
#include "magic.h"
@ -46,7 +46,7 @@ FILE_RCSID("@(#)$File: funcs.c,v 1.68 2014/02/18 11:09:31 kim Exp $")
#endif
#ifndef SIZE_MAX
# define SIZE_MAX ((size_t) -1)
#define SIZE_MAX ((size_t)~0)
#endif
#include "php.h"
@ -61,6 +61,7 @@ extern public void convert_libmagic_pattern(zval *pattern, char *val, int len, i
protected int
file_printf(struct magic_set *ms, const char *fmt, ...)
{
int rv;
va_list ap;
int len;
char *buf = NULL, *newstr;
@ -232,7 +233,7 @@ file_buffer(struct magic_set *ms, php_stream *stream, const char *inname, const
/* try soft magic tests */
if ((ms->flags & MAGIC_NO_CHECK_SOFT) == 0)
if ((m = file_softmagic(ms, ubuf, nb, 0, BINTEST,
if ((m = file_softmagic(ms, ubuf, nb, 0, NULL, BINTEST,
looks_text)) != 0) {
if ((ms->flags & MAGIC_DEBUG) != 0)
(void)fprintf(stderr, "softmagic %d\n", m);
@ -265,19 +266,6 @@ file_buffer(struct magic_set *ms, php_stream *stream, const char *inname, const
(void)fprintf(stderr, "ascmagic %d\n", m);
goto done;
}
/* try to discover text encoding */
if ((ms->flags & MAGIC_NO_CHECK_ENCODING) == 0) {
if (looks_text == 0)
if ((m = file_ascmagic_with_encoding( ms, ubuf,
nb, u8buf, ulen, code, ftype, looks_text))
!= 0) {
if ((ms->flags & MAGIC_DEBUG) != 0)
(void)fprintf(stderr,
"ascmagic/enc %d\n", m);
goto done;
}
}
}
simple:
@ -349,6 +337,7 @@ file_getbuffer(struct magic_set *ms)
/* * 4 is for octal representation, + 1 is for NUL */
len = strlen(ms->o.buf);
if (len > (SIZE_MAX - 1) / 4) {
file_oomem(ms, len);
return NULL;
}
psize = len * 4 + 1;
@ -436,6 +425,7 @@ file_printedlen(const struct magic_set *ms)
return ms->o.buf == NULL ? 0 : strlen(ms->o.buf);
}
protected int
file_replace(struct magic_set *ms, const char *pat, const char *rep)
{
zval patt;
@ -474,3 +464,69 @@ out:
(void)setlocale(LC_CTYPE, "");
return rep_cnt;
}
protected file_pushbuf_t *
file_push_buffer(struct magic_set *ms)
{
file_pushbuf_t *pb;
if (ms->event_flags & EVENT_HAD_ERR)
return NULL;
if ((pb = (CAST(file_pushbuf_t *, emalloc(sizeof(*pb))))) == NULL)
return NULL;
pb->buf = ms->o.buf;
pb->offset = ms->offset;
ms->o.buf = NULL;
ms->offset = 0;
return pb;
}
protected char *
file_pop_buffer(struct magic_set *ms, file_pushbuf_t *pb)
{
char *rbuf;
if (ms->event_flags & EVENT_HAD_ERR) {
efree(pb->buf);
efree(pb);
return NULL;
}
rbuf = ms->o.buf;
ms->o.buf = pb->buf;
ms->offset = pb->offset;
efree(pb);
return rbuf;
}
/*
* convert string to ascii printable format.
*/
protected char *
file_printable(char *buf, size_t bufsiz, const char *str)
{
char *ptr, *eptr;
const unsigned char *s = (const unsigned char *)str;
for (ptr = buf, eptr = ptr + bufsiz - 1; ptr < eptr && *s; s++) {
if (isprint(*s)) {
*ptr++ = *s;
continue;
}
if (ptr >= eptr - 3)
break;
*ptr++ = '\\';
*ptr++ = ((*s >> 6) & 7) + '0';
*ptr++ = ((*s >> 3) & 7) + '0';
*ptr++ = ((*s >> 0) & 7) + '0';
}
*ptr = '\0';
return buf;
}

View File

@ -40,7 +40,7 @@
#include "file.h"
#ifndef lint
FILE_RCSID("@(#)$File: is_tar.c,v 1.37 2010/11/30 14:58:53 rrt Exp $")
FILE_RCSID("@(#)$File: is_tar.c,v 1.36 2009/02/03 20:27:51 christos Exp $")
#endif
#include "magic.h"

View File

@ -28,7 +28,7 @@
#include "file.h"
#ifndef lint
FILE_RCSID("@(#)$File: magic.c,v 1.81 2013/11/29 15:42:51 christos Exp $")
FILE_RCSID("@(#)$File: magic.c,v 1.91 2014/12/16 23:18:40 christos Exp $")
#endif /* lint */
#include "magic.h"
@ -157,7 +157,7 @@ out:
tmppath = NULL; \
} \
} while (/*CONSTCOND*/0)
if (default_magic) {
free(default_magic);
default_magic = NULL;
@ -233,13 +233,15 @@ magic_open(int flags)
private int
unreadable_info(struct magic_set *ms, mode_t md, const char *file)
{
/* We cannot open it, but we were able to stat it. */
if (access(file, W_OK) == 0)
if (file_printf(ms, "writable, ") == -1)
return -1;
if (access(file, X_OK) == 0)
if (file_printf(ms, "executable, ") == -1)
return -1;
if (file) {
/* We cannot open it, but we were able to stat it. */
if (access(file, W_OK) == 0)
if (file_printf(ms, "writable, ") == -1)
return -1;
if (access(file, X_OK) == 0)
if (file_printf(ms, "executable, ") == -1)
return -1;
}
if (S_ISREG(md))
if (file_printf(ms, "regular file, ") == -1)
return -1;
@ -429,7 +431,7 @@ magic_buffer(struct magic_set *ms, const void *buf, size_t nb)
return NULL;
/*
* The main work is done here!
* We have the file name and/or the data buffer to be identified.
* We have the file name and/or the data buffer to be identified.
*/
if (file_buffer(ms, NULL, NULL, buf, nb) == -1) {
return NULL;
@ -471,3 +473,53 @@ magic_version(void)
{
return MAGIC_VERSION;
}
public int
magic_setparam(struct magic_set *ms, int param, const void *val)
{
switch (param) {
case MAGIC_PARAM_INDIR_MAX:
ms->indir_max = (uint16_t)*(const size_t *)val;
return 0;
case MAGIC_PARAM_NAME_MAX:
ms->name_max = (uint16_t)*(const size_t *)val;
return 0;
case MAGIC_PARAM_ELF_PHNUM_MAX:
ms->elf_phnum_max = (uint16_t)*(const size_t *)val;
return 0;
case MAGIC_PARAM_ELF_SHNUM_MAX:
ms->elf_shnum_max = (uint16_t)*(const size_t *)val;
return 0;
case MAGIC_PARAM_ELF_NOTES_MAX:
ms->elf_notes_max = (uint16_t)*(const size_t *)val;
return 0;
default:
errno = EINVAL;
return -1;
}
}
public int
magic_getparam(struct magic_set *ms, int param, void *val)
{
switch (param) {
case MAGIC_PARAM_INDIR_MAX:
*(size_t *)val = ms->indir_max;
return 0;
case MAGIC_PARAM_NAME_MAX:
*(size_t *)val = ms->name_max;
return 0;
case MAGIC_PARAM_ELF_PHNUM_MAX:
*(size_t *)val = ms->elf_phnum_max;
return 0;
case MAGIC_PARAM_ELF_SHNUM_MAX:
*(size_t *)val = ms->elf_shnum_max;
return 0;
case MAGIC_PARAM_ELF_NOTES_MAX:
*(size_t *)val = ms->elf_notes_max;
return 0;
default:
errno = EINVAL;
return -1;
}
}

View File

@ -75,7 +75,7 @@
#define MAGIC_NO_CHECK_FORTRAN 0x000000 /* Don't check ascii/fortran */
#define MAGIC_NO_CHECK_TROFF 0x000000 /* Don't check ascii/troff */
#define MAGIC_VERSION 517 /* This implementation */
#define MAGIC_VERSION 522 /* This implementation */
#ifdef __cplusplus
@ -97,10 +97,21 @@ int magic_setflags(magic_t, int);
int magic_version(void);
int magic_load(magic_t, const char *);
int magic_load_buffers(magic_t, void **, size_t *, size_t);
int magic_compile(magic_t, const char *);
int magic_list(magic_t, const char *);
int magic_errno(magic_t);
#define MAGIC_PARAM_INDIR_MAX 0
#define MAGIC_PARAM_NAME_MAX 1
#define MAGIC_PARAM_ELF_PHNUM_MAX 2
#define MAGIC_PARAM_ELF_SHNUM_MAX 3
#define MAGIC_PARAM_ELF_NOTES_MAX 4
int magic_setparam(magic_t, int, const void *);
int magic_getparam(magic_t, int, void *);
#ifdef __cplusplus
};
#endif

View File

@ -1,173 +0,0 @@
/*
* 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.
*/
/*
* Names.h - names and types used by ascmagic in file(1).
* These tokens are here because they can appear anywhere in
* the first HOWMANY bytes, while tokens in MAGIC must
* appear at fixed offsets into the file. Don't make HOWMANY
* too high unless you have a very fast CPU.
*
* $File: names.h,v 1.32 2008/02/11 00:19:29 rrt Exp $
*/
/*
modified by Chris Lowth - 9 April 2000
to add mime type strings to the types table.
*/
/* these types are used to index the table 'types': keep em in sync! */
#define L_C 0 /* first and foremost on UNIX */
#define L_CC 1 /* Bjarne's postincrement */
#define L_MAKE 2 /* Makefiles */
#define L_PLI 3 /* PL/1 */
#define L_MACH 4 /* some kinda assembler */
#define L_ENG 5 /* English */
#define L_PAS 6 /* Pascal */
#define L_MAIL 7 /* Electronic mail */
#define L_NEWS 8 /* Usenet Netnews */
#define L_JAVA 9 /* Java code */
#define L_HTML 10 /* HTML */
#define L_BCPL 11 /* BCPL */
#define L_M4 12 /* M4 */
#define L_PO 13 /* PO */
static const struct {
char human[48];
char mime[16];
} types[] = {
{ "C program", "text/x-c", },
{ "C++ program", "text/x-c++" },
{ "make commands", "text/x-makefile" },
{ "PL/1 program", "text/x-pl1" },
{ "assembler program", "text/x-asm" },
{ "English", "text/plain" },
{ "Pascal program", "text/x-pascal" },
{ "mail", "text/x-mail" },
{ "news", "text/x-news" },
{ "Java program", "text/x-java" },
{ "HTML document", "text/html", },
{ "BCPL program", "text/x-bcpl" },
{ "M4 macro language pre-processor", "text/x-m4" },
{ "PO (gettext message catalogue)", "text/x-po" },
{ "cannot happen error on names.h/types", "error/x-error" }
};
/*
* XXX - how should we distinguish Java from C++?
* The trick used in a Debian snapshot, of having "extends" or "implements"
* as tags for Java, doesn't work very well, given that those keywords
* are often preceded by "class", which flags it as C++.
*
* Perhaps we need to be able to say
*
* If "class" then
*
* if "extends" or "implements" then
* Java
* else
* C++
* endif
*
* Or should we use other keywords, such as "package" or "import"?
* Unfortunately, Ada95 uses "package", and Modula-3 uses "import",
* although I infer from the language spec at
*
* http://www.research.digital.com/SRC/m3defn/html/m3.html
*
* that Modula-3 uses "IMPORT" rather than "import", i.e. it must be
* in all caps.
*
* So, for now, we go with "import". We must put it before the C++
* stuff, so that we don't misidentify Java as C++. Not using "package"
* means we won't identify stuff that defines a package but imports
* nothing; hopefully, very little Java code imports nothing (one of the
* reasons for doing OO programming is to import as much as possible
* and write only what you need to, right?).
*
* Unfortunately, "import" may cause us to misidentify English text
* as Java, as it comes after "the" and "The". Perhaps we need a fancier
* heuristic to identify Java?
*/
static const struct names {
char name[14];
short type;
} names[] = {
/* These must be sorted by eye for optimal hit rate */
/* Add to this list only after substantial meditation */
{"msgid", L_PO},
{"dnl", L_M4},
{"import", L_JAVA},
{"\"libhdr\"", L_BCPL},
{"\"LIBHDR\"", L_BCPL},
{"//", L_CC},
{"template", L_CC},
{"virtual", L_CC},
{"class", L_CC},
{"public:", L_CC},
{"private:", L_CC},
{"/*", L_C}, /* must precede "The", "the", etc. */
{"#include", L_C},
{"char", L_C},
{"The", L_ENG},
{"the", L_ENG},
{"double", L_C},
{"extern", L_C},
{"float", L_C},
{"struct", L_C},
{"union", L_C},
{"CFLAGS", L_MAKE},
{"LDFLAGS", L_MAKE},
{"all:", L_MAKE},
{".PRECIOUS", L_MAKE},
{".ascii", L_MACH},
{".asciiz", L_MACH},
{".byte", L_MACH},
{".even", L_MACH},
{".globl", L_MACH},
{".text", L_MACH},
{"clr", L_MACH},
{"(input,", L_PAS},
{"program", L_PAS},
{"record", L_PAS},
{"dcl", L_PLI},
{"Received:", L_MAIL},
{">From", L_MAIL},
{"Return-Path:",L_MAIL},
{"Cc:", L_MAIL},
{"Newsgroups:", L_NEWS},
{"Path:", L_NEWS},
{"Organization:",L_NEWS},
{"href=", L_HTML},
{"HREF=", L_HTML},
{"<body", L_HTML},
{"<BODY", L_HTML},
{"<html", L_HTML},
{"<HTML", L_HTML},
{"<!--", L_HTML},
};
#define NNAMES (sizeof(names)/sizeof(struct names))

View File

@ -1,11 +1,15 @@
#define FILE_VERSION_MAJOR 5
#define patchlevel 17
#define patchlevel 22
/*
* Patchlevel file for Ian Darwin's MAGIC command.
* $File: patchlevel.h,v 1.68 2008/03/22 21:39:43 christos Exp $
*
* $Log$
* Revision 1.9 2015/03/06 22:27:12 ab
* Update libmagic to 5.2X
*
* $Log$
* Revision 1.8 2014/02/18 22:27:12 ab
* Update libmagic to 5.17
*
@ -351,16 +355,16 @@
*
* Revision 1.8 93/02/19 15:01:26 ian
* Numerous changes from Guy Harris too numerous to mention but including
* byte-order independence, fixing "old-style masking", etc. etc. A bugfix
* 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.
*
*
*/

View File

@ -2,7 +2,7 @@
* 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:
@ -12,7 +12,7 @@
* 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
@ -35,7 +35,7 @@
#include "cdf.h"
#ifndef lint
FILE_RCSID("@(#)$File: print.c,v 1.76 2013/02/26 18:25:00 christos Exp $")
FILE_RCSID("@(#)$File: print.c,v 1.78 2015/01/06 02:04:10 christos Exp $")
#endif /* lint */
#include <stdio.h>
@ -84,6 +84,10 @@ file_fmttime(uint64_t v, int flags, char *buf)
struct timeval ts;
cdf_timestamp_to_timespec(&ts, t);
t = ts.tv_sec;
} else {
// XXX: perhaps detect and print something if overflow
// on 32 bit time_t?
t = (time_t)v;
}
if (flags & FILE_T_LOCAL) {
@ -111,6 +115,9 @@ file_fmttime(uint64_t v, int flags, char *buf)
goto out;
pp = asctime_r(tm, buf);
}
if (tm == NULL)
goto out;
pp = asctime_r(tm, buf);
if (pp == NULL)
goto out;

View File

@ -26,7 +26,7 @@
#include "file.h"
#ifndef lint
FILE_RCSID("@(#)$File: readcdf.c,v 1.40 2014/03/06 15:23:33 christos Exp $")
FILE_RCSID("@(#)$File: readcdf.c,v 1.50 2015/01/02 21:29:39 christos Exp $")
#endif
#include <stdlib.h>
@ -45,6 +45,10 @@ FILE_RCSID("@(#)$File: readcdf.c,v 1.40 2014/03/06 15:23:33 christos Exp $")
#include "cdf.h"
#include "magic.h"
#ifndef __arraycount
#define __arraycount(a) (sizeof(a) / sizeof(a[0]))
#endif
#define NOTMIME(ms) (((ms)->flags & MAGIC_MIME) == 0)
static const struct nv {
@ -103,7 +107,7 @@ static const struct cv {
},
{ { 0, 0 },
NULL,
}
},
};
private const char *
@ -148,7 +152,8 @@ cdf_file_property_info(struct magic_set *ms, const cdf_property_info_t *info,
memset(&ts, 0, sizeof(ts));
if (!NOTMIME(ms) && root_storage)
str = cdf_clsid_to_mime(root_storage->d_storage_uuid, clsid2mime);
str = cdf_clsid_to_mime(root_storage->d_storage_uuid,
clsid2mime);
for (i = 0; i < count; i++) {
cdf_print_property_name(buf, sizeof(buf), info[i].pi_id);
@ -190,12 +195,11 @@ cdf_file_property_info(struct magic_set *ms, const cdf_property_info_t *info,
if (info[i].pi_type == CDF_LENGTH32_WSTRING)
k++;
s = info[i].pi_str.s_buf;
for (j = 0; j < sizeof(vbuf) && len--;
j++, s += k) {
for (j = 0; j < sizeof(vbuf) && len--; s += k) {
if (*s == '\0')
break;
if (isprint((unsigned char)*s))
vbuf[j] = *s;
vbuf[j++] = *s;
}
if (j == sizeof(vbuf))
--j;
@ -258,6 +262,37 @@ cdf_file_property_info(struct magic_set *ms, const cdf_property_info_t *info,
return 1;
}
private int
cdf_file_catalog(struct magic_set *ms, const cdf_header_t *h,
const cdf_stream_t *sst)
{
cdf_catalog_t *cat;
size_t i;
char buf[256];
cdf_catalog_entry_t *ce;
if (NOTMIME(ms)) {
if (file_printf(ms, "Microsoft Thumbs.db [") == -1)
return -1;
if (cdf_unpack_catalog(h, sst, &cat) == -1)
return -1;
ce = cat->cat_e;
/* skip first entry since it has a , or paren */
for (i = 1; i < cat->cat_num; i++)
if (file_printf(ms, "%s%s",
cdf_u16tos8(buf, ce[i].ce_namlen, ce[i].ce_name),
i == cat->cat_num - 1 ? "]" : ", ") == -1) {
free(cat);
return -1;
}
free(cat);
} else {
if (file_printf(ms, "application/CDFV2") == -1)
return -1;
}
return 1;
}
private int
cdf_file_summary_info(struct magic_set *ms, const cdf_header_t *h,
const cdf_stream_t *sst, const cdf_directory_t *root_storage)
@ -301,12 +336,14 @@ cdf_file_summary_info(struct magic_set *ms, const cdf_header_t *h,
break;
}
if (root_storage) {
str = cdf_clsid_to_mime(root_storage->d_storage_uuid, clsid2desc);
if (str)
str = cdf_clsid_to_mime(root_storage->d_storage_uuid,
clsid2desc);
if (str) {
if (file_printf(ms, ", %s", str) == -1)
return -2;
}
}
}
m = cdf_file_property_info(ms, info, count, root_storage);
free(info);
@ -314,6 +351,104 @@ cdf_file_summary_info(struct magic_set *ms, const cdf_header_t *h,
return m == -1 ? -2 : m;
}
#ifdef notdef
private char *
format_clsid(char *buf, size_t len, const uint64_t uuid[2]) {
snprintf(buf, len, "%.8" PRIx64 "-%.4" PRIx64 "-%.4" PRIx64 "-%.4"
PRIx64 "-%.12" PRIx64,
(uuid[0] >> 32) & (uint64_t)0x000000000ffffffffULL,
(uuid[0] >> 16) & (uint64_t)0x0000000000000ffffULL,
(uuid[0] >> 0) & (uint64_t)0x0000000000000ffffULL,
(uuid[1] >> 48) & (uint64_t)0x0000000000000ffffULL,
(uuid[1] >> 0) & (uint64_t)0x0000fffffffffffffULL);
return buf;
}
#endif
private int
cdf_file_catalog_info(struct magic_set *ms, const cdf_info_t *info,
const cdf_header_t *h, const cdf_sat_t *sat, const cdf_sat_t *ssat,
const cdf_stream_t *sst, const cdf_dir_t *dir, cdf_stream_t *scn)
{
int i;
if ((i = cdf_read_user_stream(info, h, sat, ssat, sst,
dir, "Catalog", scn)) <= 0)
return i;
#ifdef CDF_DEBUG
cdf_dump_catalog(&h, &scn);
#endif
if ((i = cdf_file_catalog(ms, h, scn)) == -1)
return -1;
return i;
}
private struct sinfo {
const char *name;
const char *mime;
const char *sections[5];
const int types[5];
} sectioninfo[] = {
{ "Encrypted", "encrypted",
{
"EncryptedPackage", NULL, NULL, NULL, NULL,
},
{
CDF_DIR_TYPE_USER_STREAM, 0, 0, 0, 0,
},
},
{ "QuickBooks", "quickbooks",
{
#if 0
"TaxForms", "PDFTaxForms", "modulesInBackup",
#endif
"mfbu_header", NULL, NULL, NULL, NULL,
},
{
#if 0
CDF_DIR_TYPE_USER_STORAGE,
CDF_DIR_TYPE_USER_STORAGE,
CDF_DIR_TYPE_USER_STREAM,
#endif
CDF_DIR_TYPE_USER_STREAM,
0, 0, 0, 0
},
},
};
private int
cdf_file_dir_info(struct magic_set *ms, const cdf_dir_t *dir)
{
size_t sd, j;
for (sd = 0; sd < __arraycount(sectioninfo); sd++) {
const struct sinfo *si = &sectioninfo[sd];
for (j = 0; si->sections[j]; j++) {
if (cdf_find_stream(dir, si->sections[j], si->types[j])
<= 0) {
#ifdef CDF_DEBUG
fprintf(stderr, "Can't read %s\n",
si->sections[j]);
#endif
break;
}
}
if (si->sections[j] != NULL)
continue;
if (NOTMIME(ms)) {
if (file_printf(ms, "CDFV2 %s", si->name) == -1)
return -1;
} else {
if (file_printf(ms, "application/CDFV2-%s",
si->mime) == -1)
return -1;
}
return 1;
}
return -1;
}
protected int
file_trycdf(struct magic_set *ms, int fd, const unsigned char *buf,
size_t nbytes)
@ -325,7 +460,6 @@ file_trycdf(struct magic_set *ms, int fd, const unsigned char *buf,
cdf_dir_t dir;
int i;
const char *expn = "";
const char *corrupt = "corrupt: ";
const cdf_directory_t *root_storage;
info.i_fd = fd;
@ -368,17 +502,59 @@ file_trycdf(struct magic_set *ms, int fd, const unsigned char *buf,
#ifdef CDF_DEBUG
cdf_dump_dir(&info, &h, &sat, &ssat, &sst, &dir);
#endif
#ifdef notdef
if (root_storage) {
if (NOTMIME(ms)) {
char clsbuf[128];
if (file_printf(ms, "CLSID %s, ",
format_clsid(clsbuf, sizeof(clsbuf),
root_storage->d_storage_uuid)) == -1)
return -1;
}
}
#endif
if ((i = cdf_read_user_stream(&info, &h, &sat, &ssat, &sst, &dir,
"FileHeader", &scn)) != -1) {
#define HWP5_SIGNATURE "HWP Document File"
if (scn.sst_dirlen >= sizeof(HWP5_SIGNATURE) - 1
&& memcmp(scn.sst_tab, HWP5_SIGNATURE,
sizeof(HWP5_SIGNATURE) - 1) == 0) {
if (NOTMIME(ms)) {
if (file_printf(ms,
"Hangul (Korean) Word Processor File 5.x") == -1)
return -1;
} else {
if (file_printf(ms, "application/x-hwp") == -1)
return -1;
}
i = 1;
goto out5;
} else {
free(scn.sst_tab);
scn.sst_tab = NULL;
scn.sst_len = 0;
scn.sst_dirlen = 0;
}
}
if ((i = cdf_read_summary_info(&info, &h, &sat, &ssat, &sst, &dir,
&scn)) == -1) {
if (errno == ESRCH) {
corrupt = expn;
expn = "No summary info";
} else {
if (errno != ESRCH) {
expn = "Cannot read summary info";
}
goto out4;
}
goto out4;
}
i = cdf_file_catalog_info(ms, &info, &h, &sat, &ssat, &sst,
&dir, &scn);
if (i > 0)
goto out4;
i = cdf_file_dir_info(ms, &dir);
if (i < 0)
expn = "Cannot read section info";
goto out4;
}
#ifdef CDF_DEBUG
cdf_dump_summary_info(&h, &scn);
#endif
@ -412,6 +588,7 @@ file_trycdf(struct magic_set *ms, int fd, const unsigned char *buf,
i = 1;
}
}
out5:
free(scn.sst_tab);
out4:
free(sst.sst_tab);
@ -428,10 +605,10 @@ out0:
"Composite Document File V2 Document") == -1)
return -1;
if (*expn)
if (file_printf(ms, ", %s%s", corrupt, expn) == -1)
if (file_printf(ms, ", %s", expn) == -1)
return -1;
} else {
if (file_printf(ms, "application/CDFV2-corrupt") == -1)
if (file_printf(ms, "application/CDFV2-unknown") == -1)
return -1;
}
i = 1;

View File

@ -32,16 +32,10 @@
#include "file.h"
#ifndef lint
FILE_RCSID("@(#)$File: softmagic.c,v 1.174 2014/02/12 23:20:53 christos Exp $")
FILE_RCSID("@(#)$File: softmagic.c,v 1.212 2015/01/24 22:11:25 christos Exp $")
#endif /* lint */
#include "magic.h"
#ifdef HAVE_FMTCHECK
#include <stdio.h>
#define F(a, b) fmtcheck((a), (b))
#else
#define F(a, b) (a)
#endif
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
@ -57,11 +51,11 @@ FILE_RCSID("@(#)$File: softmagic.c,v 1.174 2014/02/12 23:20:53 christos Exp $")
private int match(struct magic_set *, struct magic *, uint32_t,
const unsigned char *, size_t, size_t, int, int, int, int, int *, int *,
int *);
const unsigned char *, size_t, 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, int, int *,
int *, int *);
struct magic *, size_t, 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 int32_t moffset(struct magic_set *, struct magic *);
@ -85,19 +79,47 @@ private void 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,
size_t level, int mode, int text)
uint16_t indir_level, uint16_t *name_count, int mode, int text)
{
struct mlist *ml;
int rv, printed_something = 0, need_separator = 0;
uint16_t nc;
if (name_count == NULL) {
nc = 0;
name_count = &nc;
}
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,
text, 0, level, &printed_something, &need_separator,
NULL)) != 0)
text, 0, indir_level, name_count,
&printed_something, &need_separator, NULL)) != 0)
return rv;
return 0;
}
#if defined(FILE_FMTDEBUG) && defined(HAVE_FMTCHECK)
#define F(a, b, c) file_fmtcheck((a), (b), (c), __FILE__, __LINE__)
private const char * __attribute__((__format_arg__(3)))
file_fmtcheck(struct magic_set *ms, const struct magic *m, const char *def,
const char *file, size_t line)
{
const char *ptr = fmtcheck(m->desc, def);
if (ptr == def)
file_magerror(ms,
"%s, %" SIZE_T_FORMAT "u: format `%s' does not match"
" with `%s'", file, line, m->desc, def);
return ptr;
}
#elif defined(HAVE_FMTCHECK)
#define F(a, b, c) fmtcheck((b)->desc, (c))
#else
#define F(a, b, c) ((b)->desc)
#endif
/*
* Go through the whole list, stopping if you find a match. Process all
* the continuations of that match before returning.
@ -128,8 +150,8 @@ file_softmagic(struct magic_set *ms, const unsigned char *buf, size_t nbytes,
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,
int flip, int recursion_level, int *printed_something, int *need_separator,
int *returnval)
int flip, uint16_t indir_level, uint16_t *name_count,
int *printed_something, int *need_separator, int *returnval)
{
uint32_t magindex = 0;
unsigned int cont_level = 0;
@ -166,8 +188,8 @@ match(struct magic_set *ms, struct magic *magic, uint32_t nmagic,
/* if main entry matches, print it... */
switch (mget(ms, s, m, nbytes, offset, cont_level, mode, text,
flip, recursion_level + 1, printed_something,
need_separator, returnval)) {
flip, indir_level, name_count,
printed_something, need_separator, returnval)) {
case -1:
return -1;
case 0:
@ -227,9 +249,9 @@ match(struct magic_set *ms, struct magic *magic, uint32_t nmagic,
if (file_check_mem(ms, ++cont_level) == -1)
return -1;
while (magindex + 1 < nmagic && magic[magindex+1].cont_level != 0 &&
++magindex) {
m = &magic[magindex];
while (magindex + 1 < nmagic &&
magic[magindex + 1].cont_level != 0) {
m = &magic[++magindex];
ms->line = m->lineno; /* for messages */
if (cont_level < m->cont_level)
@ -255,8 +277,8 @@ match(struct magic_set *ms, struct magic *magic, uint32_t nmagic,
}
#endif
switch (mget(ms, s, m, nbytes, offset, cont_level, mode,
text, flip, recursion_level + 1, printed_something,
need_separator, returnval)) {
text, flip, indir_level, name_count,
printed_something, need_separator, returnval)) {
case -1:
return -1;
case 0:
@ -349,6 +371,7 @@ match(struct magic_set *ms, struct magic *magic, uint32_t nmagic,
if ((ms->flags & MAGIC_CONTINUE) == 0 && *printed_something) {
return *returnval; /* don't keep searching */
}
cont_level = 0;
}
return *returnval; /* This is hit if -k is set or there is no match */
}
@ -383,7 +406,7 @@ mprint(struct magic_set *ms, struct magic *m)
float vf;
double vd;
int64_t t = 0;
char buf[128], tbuf[26];
char buf[128], tbuf[26], sbuf[512];
union VALUETYPE *p = &ms->ms_value;
switch (m->type) {
@ -393,13 +416,13 @@ mprint(struct magic_set *ms, struct magic *m)
case -1:
return -1;
case 1:
(void)snprintf(buf, sizeof(buf), "%c",
(void)snprintf(buf, sizeof(buf), "%d",
(unsigned char)v);
if (file_printf(ms, F(m->desc, "%s"), buf) == -1)
if (file_printf(ms, F(ms, m, "%s"), buf) == -1)
return -1;
break;
default:
if (file_printf(ms, F(m->desc, "%c"),
if (file_printf(ms, F(ms, m, "%d"),
(unsigned char) v) == -1)
return -1;
break;
@ -415,13 +438,13 @@ mprint(struct magic_set *ms, struct magic *m)
case -1:
return -1;
case 1:
(void)snprintf(buf, sizeof(buf), "%hu",
(void)snprintf(buf, sizeof(buf), "%u",
(unsigned short)v);
if (file_printf(ms, F(m->desc, "%s"), buf) == -1)
if (file_printf(ms, F(ms, m, "%s"), buf) == -1)
return -1;
break;
default:
if (file_printf(ms, F(m->desc, "%hu"),
if (file_printf(ms, F(ms, m, "%u"),
(unsigned short) v) == -1)
return -1;
break;
@ -438,13 +461,12 @@ mprint(struct magic_set *ms, struct magic *m)
case -1:
return -1;
case 1:
(void)snprintf(buf, sizeof(buf), "%u", (uint32_t)v);
if (file_printf(ms, F(m->desc, "%s"), buf) == -1)
(void)snprintf(buf, sizeof(buf), "%u", (uint32_t) v);
if (file_printf(ms, F(ms, m, "%s"), buf) == -1)
return -1;
break;
default:
if (file_printf(ms, F(m->desc, "%u"),
(uint32_t) v) == -1)
if (file_printf(ms, F(ms, m, "%u"), (uint32_t) v) == -1)
return -1;
break;
}
@ -459,13 +481,13 @@ mprint(struct magic_set *ms, struct magic *m)
case -1:
return -1;
case 1:
(void)snprintf(buf, sizeof(buf), "%llu",
(void)snprintf(buf, sizeof(buf), "%" INT64_T_FORMAT "u",
(unsigned long long)v);
if (file_printf(ms, F(m->desc, "%s"), buf) == -1)
if (file_printf(ms, F(ms, m, "%s"), buf) == -1)
return -1;
break;
default:
if (file_printf(ms, F(m->desc, "%llu"),
if (file_printf(ms, F(ms, m, "%" INT64_T_FORMAT "u"),
(unsigned long long) v) == -1)
return -1;
break;
@ -478,7 +500,9 @@ mprint(struct magic_set *ms, struct magic *m)
case FILE_BESTRING16:
case FILE_LESTRING16:
if (m->reln == '=' || m->reln == '!') {
if (file_printf(ms, F(m->desc, "%s"), m->value.s) == -1)
if (file_printf(ms, F(ms, m, "%s"),
file_printable(sbuf, sizeof(sbuf), m->value.s))
== -1)
return -1;
t = ms->offset + m->vallen;
}
@ -489,7 +513,7 @@ mprint(struct magic_set *ms, struct magic *m)
t = ms->offset + strlen(str);
if (*m->value.s == '\0')
str[strcspn(str, "\n")] = '\0';
str[strcspn(str, "\r\n")] = '\0';
if (m->str_flags & STRING_TRIM) {
char *last;
@ -504,7 +528,8 @@ mprint(struct magic_set *ms, struct magic *m)
*++last = '\0';
}
if (file_printf(ms, F(m->desc, "%s"), str) == -1)
if (file_printf(ms, F(ms, m, "%s"),
file_printable(sbuf, sizeof(sbuf), str)) == -1)
return -1;
if (m->type == FILE_PSTRING)
@ -516,9 +541,8 @@ mprint(struct magic_set *ms, struct magic *m)
case FILE_BEDATE:
case FILE_LEDATE:
case FILE_MEDATE:
if (file_printf(ms, F(m->desc, "%s"),
file_fmttime(p->l, FILE_T_LOCAL,
tbuf)) == -1)
if (file_printf(ms, F(ms, m, "%s"),
file_fmttime(p->l, 0, tbuf)) == -1)
return -1;
t = ms->offset + sizeof(uint32_t);
break;
@ -527,8 +551,8 @@ mprint(struct magic_set *ms, struct magic *m)
case FILE_BELDATE:
case FILE_LELDATE:
case FILE_MELDATE:
if (file_printf(ms, F(m->desc, "%s"),
file_fmttime(p->l, 0, tbuf)) == -1)
if (file_printf(ms, F(ms, m, "%s"),
file_fmttime(p->l, FILE_T_LOCAL, tbuf)) == -1)
return -1;
t = ms->offset + sizeof(uint32_t);
break;
@ -536,8 +560,8 @@ mprint(struct magic_set *ms, struct magic *m)
case FILE_QDATE:
case FILE_BEQDATE:
case FILE_LEQDATE:
if (file_printf(ms, F(m->desc, "%s"),
file_fmttime(p->q, FILE_T_LOCAL, tbuf)) == -1)
if (file_printf(ms, F(ms, m, "%s"),
file_fmttime(p->q, 0, tbuf)) == -1)
return -1;
t = ms->offset + sizeof(uint64_t);
break;
@ -545,8 +569,8 @@ mprint(struct magic_set *ms, struct magic *m)
case FILE_QLDATE:
case FILE_BEQLDATE:
case FILE_LEQLDATE:
if (file_printf(ms, F(m->desc, "%s"),
file_fmttime(p->q, 0, tbuf)) == -1)
if (file_printf(ms, F(ms, m, "%s"),
file_fmttime(p->q, FILE_T_LOCAL, tbuf)) == -1)
return -1;
t = ms->offset + sizeof(uint64_t);
break;
@ -554,52 +578,53 @@ mprint(struct magic_set *ms, struct magic *m)
case FILE_QWDATE:
case FILE_BEQWDATE:
case FILE_LEQWDATE:
if (file_printf(ms, F(m->desc, "%s"),
if (file_printf(ms, F(ms, m, "%s"),
file_fmttime(p->q, FILE_T_WINDOWS, tbuf)) == -1)
return -1;
t = ms->offset + sizeof(uint64_t);
break;
case FILE_FLOAT:
case FILE_BEFLOAT:
case FILE_LEFLOAT:
case FILE_FLOAT:
case FILE_BEFLOAT:
case FILE_LEFLOAT:
vf = p->f;
switch (check_fmt(ms, m)) {
case -1:
return -1;
case 1:
(void)snprintf(buf, sizeof(buf), "%g", vf);
if (file_printf(ms, F(m->desc, "%s"), buf) == -1)
if (file_printf(ms, F(ms, m, "%s"), buf) == -1)
return -1;
break;
default:
if (file_printf(ms, F(m->desc, "%g"), vf) == -1)
if (file_printf(ms, F(ms, m, "%g"), vf) == -1)
return -1;
break;
}
t = ms->offset + sizeof(float);
break;
case FILE_DOUBLE:
case FILE_BEDOUBLE:
case FILE_LEDOUBLE:
case FILE_DOUBLE:
case FILE_BEDOUBLE:
case FILE_LEDOUBLE:
vd = p->d;
switch (check_fmt(ms, m)) {
case -1:
return -1;
case 1:
(void)snprintf(buf, sizeof(buf), "%g", vd);
if (file_printf(ms, F(m->desc, "%s"), buf) == -1)
if (file_printf(ms, F(ms, m, "%s"), buf) == -1)
return -1;
break;
default:
if (file_printf(ms, F(m->desc, "%g"), vd) == -1)
if (file_printf(ms, F(ms, m, "%g"), vd) == -1)
return -1;
break;
}
t = ms->offset + sizeof(double);
break;
case FILE_SEARCH:
case FILE_REGEX: {
char *cp;
int rval;
@ -609,7 +634,8 @@ mprint(struct magic_set *ms, struct magic *m)
file_oomem(ms, ms->search.rm_len);
return -1;
}
rval = file_printf(ms, F(m->desc, "%s"), cp);
rval = file_printf(ms, F(ms, m, "%s"),
file_printable(sbuf, sizeof(sbuf), cp));
efree(cp);
if (rval == -1)
@ -622,15 +648,6 @@ mprint(struct magic_set *ms, struct magic *m)
break;
}
case FILE_SEARCH:
if (file_printf(ms, F(m->desc, "%s"), m->value.s) == -1)
return -1;
if ((m->str_flags & REGEX_OFFSET_START))
t = ms->search.offset;
else
t = ms->search.offset + m->vallen;
break;
case FILE_DEFAULT:
case FILE_CLEAR:
if (file_printf(ms, "%s", m->desc) == -1)
@ -685,7 +702,7 @@ moffset(struct magic_set *ms, struct magic *m)
uint32_t t;
if (*m->value.s == '\0')
p->s[strcspn(p->s, "\n")] = '\0';
p->s[strcspn(p->s, "\r\n")] = '\0';
t = CAST(uint32_t, (ms->offset + strlen(p->s)));
if (m->type == FILE_PSTRING)
t += (uint32_t)file_pstring_length_size(m);
@ -890,8 +907,9 @@ private int
mconvert(struct magic_set *ms, struct magic *m, int flip)
{
union VALUETYPE *p = &ms->ms_value;
uint8_t type;
switch (cvt_flip(m->type, flip)) {
switch (type = cvt_flip(m->type, flip)) {
case FILE_BYTE:
cvt_8(p, m);
return 1;
@ -929,7 +947,7 @@ mconvert(struct magic_set *ms, struct magic *m, int flip)
* string by p->s, so we need to deduct sz.
* Because we can use one of the bytes of the length
* after we shifted as NUL termination.
*/
*/
len = sz;
}
while (len--)
@ -1035,7 +1053,7 @@ mconvert(struct magic_set *ms, struct magic *m, int flip)
private void
mdebug(uint32_t offset, const char *str, size_t len)
{
(void) fprintf(stderr, "mget/%zu @%d: ", len, offset);
(void) fprintf(stderr, "mget/%" SIZE_T_FORMAT "u @%d: ", len, offset);
file_showstr(stderr, str, len);
(void) fputc('\n', stderr);
(void) fputc('\n', stderr);
@ -1063,22 +1081,32 @@ mcopy(struct magic_set *ms, union VALUETYPE *p, int type, int indir,
const char *last; /* end of search region */
const char *buf; /* start of search region */
const char *end;
size_t lines, linecnt, bytecnt;
size_t lines, linecnt, bytecnt, bytecnt_max;
linecnt = m->str_range;
bytecnt = linecnt * 80;
if (bytecnt == 0) {
bytecnt = 8192;
}
if (bytecnt > nbytes) {
bytecnt = nbytes;
}
if (s == NULL) {
ms->search.s_len = 0;
ms->search.s = NULL;
return 0;
}
if (m->str_flags & REGEX_LINE_COUNT) {
linecnt = m->str_range;
bytecnt = linecnt * 80;
} else {
linecnt = 0;
bytecnt = m->str_range;
}
/* XXX bytecnt_max is to be kept for PHP, see cve-2014-3538.
PCRE might stuck if the input buffer is too big. To ensure
the correctness, the check for bytecnt > nbytes is also
kept (might be abundant). */
bytecnt_max = nbytes - offset;
bytecnt_max = bytecnt_max > (1 << 14) ? (1 << 14) : bytecnt_max;
bytecnt_max = bytecnt > nbytes ? nbytes : bytecnt_max;
if (bytecnt == 0 || bytecnt > bytecnt_max)
bytecnt = bytecnt_max;
buf = RCAST(const char *, s) + offset;
end = last = RCAST(const char *, s) + bytecnt;
/* mget() guarantees buf <= last */
@ -1160,17 +1188,26 @@ mcopy(struct magic_set *ms, union VALUETYPE *p, int type, int indir,
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,
int flip, int recursion_level, int *printed_something,
int *need_separator, int *returnval)
int flip, uint16_t indir_level, uint16_t *name_count,
int *printed_something, int *need_separator, int *returnval)
{
uint32_t soffset, offset = ms->offset;
uint32_t offset = ms->offset;
uint32_t lhs;
file_pushbuf_t *pb;
int rv, oneed_separator, in_type;
char *sbuf, *rbuf;
char *rbuf;
union VALUETYPE *p = &ms->ms_value;
struct mlist ml;
if (recursion_level >= 20) {
file_error(ms, 0, "recursion nesting exceeded");
if (indir_level >= ms->indir_max) {
file_error(ms, 0, "indirect recursion nesting (%hu) exceeded",
indir_level);
return -1;
}
if (*name_count >= ms->name_max) {
file_error(ms, 0, "name use count (%hu) exceeded",
*name_count);
return -1;
}
@ -1179,8 +1216,11 @@ mget(struct magic_set *ms, const unsigned char *s, struct magic *m,
return -1;
if ((ms->flags & MAGIC_DEBUG) != 0) {
fprintf(stderr, "mget(type=%d, flag=%x, offset=%u, o=%zu, "
"nbytes=%zu)\n", m->type, m->flag, offset, o, nbytes);
fprintf(stderr, "mget(type=%d, flag=%x, offset=%u, o=%"
SIZE_T_FORMAT "u, " "nbytes=%" SIZE_T_FORMAT
"u, il=%hu, nc=%hu)\n",
m->type, m->flag, offset, o, nbytes,
indir_level, *name_count);
mdebug(offset, (char *)(void *)p, sizeof(union VALUETYPE));
}
@ -1262,104 +1302,72 @@ mget(struct magic_set *ms, const unsigned char *s, struct magic *m,
case FILE_BESHORT:
if (OFFSET_OOB(nbytes, offset, 2))
return 0;
lhs = (p->hs[0] << 8) | p->hs[1];
if (off) {
switch (m->in_op & FILE_OPS_MASK) {
case FILE_OPAND:
offset = (short)((p->hs[0]<<8)|
(p->hs[1])) &
off;
offset = lhs & off;
break;
case FILE_OPOR:
offset = (short)((p->hs[0]<<8)|
(p->hs[1])) |
off;
offset = lhs | off;
break;
case FILE_OPXOR:
offset = (short)((p->hs[0]<<8)|
(p->hs[1])) ^
off;
offset = lhs ^ off;
break;
case FILE_OPADD:
offset = (short)((p->hs[0]<<8)|
(p->hs[1])) +
off;
offset = lhs + off;
break;
case FILE_OPMINUS:
offset = (short)((p->hs[0]<<8)|
(p->hs[1])) -
off;
offset = lhs - off;
break;
case FILE_OPMULTIPLY:
offset = (short)((p->hs[0]<<8)|
(p->hs[1])) *
off;
offset = lhs * off;
break;
case FILE_OPDIVIDE:
offset = (short)((p->hs[0]<<8)|
(p->hs[1])) /
off;
offset = lhs / off;
break;
case FILE_OPMODULO:
offset = (short)((p->hs[0]<<8)|
(p->hs[1])) %
off;
offset = lhs % off;
break;
}
} else
offset = (short)((p->hs[0]<<8)|
(p->hs[1]));
offset = lhs;
if (m->in_op & FILE_OPINVERSE)
offset = ~offset;
break;
case FILE_LESHORT:
if (OFFSET_OOB(nbytes, offset, 2))
return 0;
lhs = (p->hs[1] << 8) | p->hs[0];
if (off) {
switch (m->in_op & FILE_OPS_MASK) {
case FILE_OPAND:
offset = (short)((p->hs[1]<<8)|
(p->hs[0])) &
off;
offset = lhs & off;
break;
case FILE_OPOR:
offset = (short)((p->hs[1]<<8)|
(p->hs[0])) |
off;
offset = lhs | off;
break;
case FILE_OPXOR:
offset = (short)((p->hs[1]<<8)|
(p->hs[0])) ^
off;
offset = lhs ^ off;
break;
case FILE_OPADD:
offset = (short)((p->hs[1]<<8)|
(p->hs[0])) +
off;
offset = lhs + off;
break;
case FILE_OPMINUS:
offset = (short)((p->hs[1]<<8)|
(p->hs[0])) -
off;
offset = lhs - off;
break;
case FILE_OPMULTIPLY:
offset = (short)((p->hs[1]<<8)|
(p->hs[0])) *
off;
offset = lhs * off;
break;
case FILE_OPDIVIDE:
offset = (short)((p->hs[1]<<8)|
(p->hs[0])) /
off;
offset = lhs / off;
break;
case FILE_OPMODULO:
offset = (short)((p->hs[1]<<8)|
(p->hs[0])) %
off;
offset = lhs % off;
break;
}
} else
offset = (short)((p->hs[1]<<8)|
(p->hs[0]));
offset = lhs;
if (m->in_op & FILE_OPINVERSE)
offset = ~offset;
break;
@ -1403,70 +1411,37 @@ mget(struct magic_set *ms, const unsigned char *s, struct magic *m,
case FILE_BEID3:
if (OFFSET_OOB(nbytes, offset, 4))
return 0;
lhs = (p->hl[0] << 24) | (p->hl[1] << 16) |
(p->hl[2] << 8) | p->hl[3];
if (off) {
switch (m->in_op & FILE_OPS_MASK) {
case FILE_OPAND:
offset = (int32_t)((p->hl[0]<<24)|
(p->hl[1]<<16)|
(p->hl[2]<<8)|
(p->hl[3])) &
off;
offset = lhs & off;
break;
case FILE_OPOR:
offset = (int32_t)((p->hl[0]<<24)|
(p->hl[1]<<16)|
(p->hl[2]<<8)|
(p->hl[3])) |
off;
offset = lhs | off;
break;
case FILE_OPXOR:
offset = (int32_t)((p->hl[0]<<24)|
(p->hl[1]<<16)|
(p->hl[2]<<8)|
(p->hl[3])) ^
off;
offset = lhs ^ off;
break;
case FILE_OPADD:
offset = (int32_t)((p->hl[0]<<24)|
(p->hl[1]<<16)|
(p->hl[2]<<8)|
(p->hl[3])) +
off;
offset = lhs + off;
break;
case FILE_OPMINUS:
offset = (int32_t)((p->hl[0]<<24)|
(p->hl[1]<<16)|
(p->hl[2]<<8)|
(p->hl[3])) -
off;
offset = lhs - off;
break;
case FILE_OPMULTIPLY:
offset = (int32_t)((p->hl[0]<<24)|
(p->hl[1]<<16)|
(p->hl[2]<<8)|
(p->hl[3])) *
off;
offset = lhs * off;
break;
case FILE_OPDIVIDE:
offset = (int32_t)((p->hl[0]<<24)|
(p->hl[1]<<16)|
(p->hl[2]<<8)|
(p->hl[3])) /
off;
offset = lhs / off;
break;
case FILE_OPMODULO:
offset = (int32_t)((p->hl[0]<<24)|
(p->hl[1]<<16)|
(p->hl[2]<<8)|
(p->hl[3])) %
off;
offset = lhs % off;
break;
}
} else
offset = (int32_t)((p->hl[0]<<24)|
(p->hl[1]<<16)|
(p->hl[2]<<8)|
(p->hl[3]));
offset = lhs;
if (m->in_op & FILE_OPINVERSE)
offset = ~offset;
break;
@ -1474,140 +1449,74 @@ mget(struct magic_set *ms, const unsigned char *s, struct magic *m,
case FILE_LEID3:
if (OFFSET_OOB(nbytes, offset, 4))
return 0;
lhs = (p->hl[3] << 24) | (p->hl[2] << 16) |
(p->hl[1] << 8) | p->hl[0];
if (off) {
switch (m->in_op & FILE_OPS_MASK) {
case FILE_OPAND:
offset = (int32_t)((p->hl[3]<<24)|
(p->hl[2]<<16)|
(p->hl[1]<<8)|
(p->hl[0])) &
off;
offset = lhs & off;
break;
case FILE_OPOR:
offset = (int32_t)((p->hl[3]<<24)|
(p->hl[2]<<16)|
(p->hl[1]<<8)|
(p->hl[0])) |
off;
offset = lhs | off;
break;
case FILE_OPXOR:
offset = (int32_t)((p->hl[3]<<24)|
(p->hl[2]<<16)|
(p->hl[1]<<8)|
(p->hl[0])) ^
off;
offset = lhs ^ off;
break;
case FILE_OPADD:
offset = (int32_t)((p->hl[3]<<24)|
(p->hl[2]<<16)|
(p->hl[1]<<8)|
(p->hl[0])) +
off;
offset = lhs + off;
break;
case FILE_OPMINUS:
offset = (int32_t)((p->hl[3]<<24)|
(p->hl[2]<<16)|
(p->hl[1]<<8)|
(p->hl[0])) -
off;
offset = lhs - off;
break;
case FILE_OPMULTIPLY:
offset = (int32_t)((p->hl[3]<<24)|
(p->hl[2]<<16)|
(p->hl[1]<<8)|
(p->hl[0])) *
off;
offset = lhs * off;
break;
case FILE_OPDIVIDE:
offset = (int32_t)((p->hl[3]<<24)|
(p->hl[2]<<16)|
(p->hl[1]<<8)|
(p->hl[0])) /
off;
offset = lhs / off;
break;
case FILE_OPMODULO:
offset = (int32_t)((p->hl[3]<<24)|
(p->hl[2]<<16)|
(p->hl[1]<<8)|
(p->hl[0])) %
off;
offset = lhs % off;
break;
}
} else
offset = (int32_t)((p->hl[3]<<24)|
(p->hl[2]<<16)|
(p->hl[1]<<8)|
(p->hl[0]));
offset = lhs;
if (m->in_op & FILE_OPINVERSE)
offset = ~offset;
break;
case FILE_MELONG:
if (OFFSET_OOB(nbytes, offset, 4))
return 0;
lhs = (p->hl[1] << 24) | (p->hl[0] << 16) |
(p->hl[3] << 8) | p->hl[2];
if (off) {
switch (m->in_op & FILE_OPS_MASK) {
case FILE_OPAND:
offset = (int32_t)((p->hl[1]<<24)|
(p->hl[0]<<16)|
(p->hl[3]<<8)|
(p->hl[2])) &
off;
offset = lhs & off;
break;
case FILE_OPOR:
offset = (int32_t)((p->hl[1]<<24)|
(p->hl[0]<<16)|
(p->hl[3]<<8)|
(p->hl[2])) |
off;
offset = lhs | off;
break;
case FILE_OPXOR:
offset = (int32_t)((p->hl[1]<<24)|
(p->hl[0]<<16)|
(p->hl[3]<<8)|
(p->hl[2])) ^
off;
offset = lhs ^ off;
break;
case FILE_OPADD:
offset = (int32_t)((p->hl[1]<<24)|
(p->hl[0]<<16)|
(p->hl[3]<<8)|
(p->hl[2])) +
off;
offset = lhs + off;
break;
case FILE_OPMINUS:
offset = (int32_t)((p->hl[1]<<24)|
(p->hl[0]<<16)|
(p->hl[3]<<8)|
(p->hl[2])) -
off;
offset = lhs - off;
break;
case FILE_OPMULTIPLY:
offset = (int32_t)((p->hl[1]<<24)|
(p->hl[0]<<16)|
(p->hl[3]<<8)|
(p->hl[2])) *
off;
offset = lhs * off;
break;
case FILE_OPDIVIDE:
offset = (int32_t)((p->hl[1]<<24)|
(p->hl[0]<<16)|
(p->hl[3]<<8)|
(p->hl[2])) /
off;
offset = lhs / off;
break;
case FILE_OPMODULO:
offset = (int32_t)((p->hl[1]<<24)|
(p->hl[0]<<16)|
(p->hl[3]<<8)|
(p->hl[2])) %
off;
offset = lhs % off;
break;
}
} else
offset = (int32_t)((p->hl[1]<<24)|
(p->hl[0]<<16)|
(p->hl[3]<<8)|
(p->hl[2]));
offset = lhs;
if (m->in_op & FILE_OPINVERSE)
offset = ~offset;
break;
@ -1656,7 +1565,9 @@ mget(struct magic_set *ms, const unsigned char *s, struct magic *m,
offset = ((((offset >> 0) & 0x7f) << 0) |
(((offset >> 8) & 0x7f) << 7) |
(((offset >> 16) & 0x7f) << 14) |
(((offset >> 24) & 0x7f) << 21)) + 10;
(((offset >> 24) & 0x7f) << 21));
if ((ms->flags & MAGIC_DEBUG) != 0)
fprintf(stderr, "id3 offs=%u\n", offset);
break;
default:
break;
@ -1736,60 +1647,59 @@ mget(struct magic_set *ms, const unsigned char *s, struct magic *m,
break;
case FILE_INDIRECT:
if (m->str_flags & INDIRECT_RELATIVE)
offset += CAST(uint32_t, o);
if (offset == 0)
return 0;
if (OFFSET_OOB(nbytes, offset, 0))
return 0;
sbuf = ms->o.buf;
soffset = ms->offset;
ms->o.buf = NULL;
ms->offset = 0;
if ((pb = file_push_buffer(ms)) == NULL)
return -1;
rv = file_softmagic(ms, s + offset, nbytes - offset,
recursion_level, BINTEST, text);
indir_level + 1, name_count, BINTEST, text);
if ((ms->flags & MAGIC_DEBUG) != 0)
fprintf(stderr, "indirect @offs=%u[%d]\n", offset, rv);
rbuf = ms->o.buf;
ms->o.buf = sbuf;
ms->offset = soffset;
rbuf = file_pop_buffer(ms, pb);
if (rbuf == NULL && ms->event_flags & EVENT_HAD_ERR)
return -1;
if (rv == 1) {
if ((ms->flags & (MAGIC_MIME|MAGIC_APPLE)) == 0 &&
file_printf(ms, m->desc, offset) == -1) {
if (rbuf) {
efree(rbuf);
}
file_printf(ms, F(ms, m, "%u"), offset) == -1) {
if (rbuf) efree(rbuf);
return -1;
}
if (file_printf(ms, "%s", rbuf) == -1) {
if (rbuf) {
efree(rbuf);
}
if (rbuf) efree(rbuf);
return -1;
}
}
if (rbuf) {
efree(rbuf);
}
if (rbuf) efree(rbuf);
return rv;
case FILE_USE:
if (OFFSET_OOB(nbytes, offset, 0))
return 0;
sbuf = m->value.s;
if (*sbuf == '^') {
sbuf++;
rbuf = m->value.s;
if (*rbuf == '^') {
rbuf++;
flip = !flip;
}
if (file_magicfind(ms, sbuf, &ml) == -1) {
file_error(ms, 0, "cannot find entry `%s'", sbuf);
if (file_magicfind(ms, rbuf, &ml) == -1) {
file_error(ms, 0, "cannot find entry `%s'", rbuf);
return -1;
}
(*name_count)++;
oneed_separator = *need_separator;
if (m->flag & NOSPACE)
*need_separator = 0;
rv = match(ms, ml.magic, ml.nmagic, s, nbytes, offset + o,
mode, text, flip, recursion_level, printed_something,
need_separator, returnval);
mode, text, flip, indir_level, name_count,
printed_something, need_separator, returnval);
if (rv != 1)
*need_separator = oneed_separator;
return rv;
@ -1906,7 +1816,7 @@ convert_libmagic_pattern(zval *pattern, char *val, int len, int options)
}
t->val[j++] = '~';
if (options & PCRE_CASELESS)
if (options & PCRE_CASELESS)
t->val[j++] = 'i';
if (options & PCRE_MULTILINE)
@ -1996,7 +1906,6 @@ magiccheck(struct magic_set *ms, struct magic *m)
break;
default:
matched = 0;
file_magerror(ms, "cannot happen with float: invalid relation `%c'",
m->reln);
return -1;
@ -2030,7 +1939,6 @@ magiccheck(struct magic_set *ms, struct magic *m)
break;
default:
matched = 0;
file_magerror(ms, "cannot happen with double: invalid relation `%c'", m->reln);
return -1;
}
@ -2069,9 +1977,11 @@ magiccheck(struct magic_set *ms, struct magic *m)
if (slen + idx > ms->search.s_len)
break;
v = file_strncmp(m->value.s, ms->search.s + idx, slen, m->str_flags);
v = file_strncmp(m->value.s, ms->search.s + idx, slen,
m->str_flags);
if (v == 0) { /* found match */
ms->search.offset += idx;
ms->search.rm_len = m->str_range - idx;
break;
}
}
@ -2277,7 +2187,6 @@ magiccheck(struct magic_set *ms, struct magic *m)
break;
default:
matched = 0;
file_magerror(ms, "cannot happen: invalid relation `%c'",
m->reln);
return -1;

View File

@ -37,6 +37,8 @@ __RCSID("$NetBSD: strcasestr.c,v 1.3 2005/11/29 03:12:00 christos Exp $");
__RCSID("$NetBSD: strncasecmp.c,v 1.2 2007/06/04 18:19:27 christos Exp $");
#endif /* LIBC_SCCS and not lint */
#include "file.h"
#include "php_stdint.h"
#include <assert.h>

View File

@ -32,7 +32,7 @@
*
* Created 25 August 1985 by John Gilmore, ihnp4!hoptoad!gnu.
*
* $File: tar.h,v 1.13 2010/11/30 14:58:53 rrt Exp $ # checkin only
* $File: tar.h,v 1.12 2008/02/07 00:58:52 christos Exp $ # checkin only
*/
/*

View File

@ -12,5 +12,5 @@ Bug #68735 fileinfo out-of-bounds memory access
?>
===DONE===
--EXPECTF--
string(%d) "JPEG image data, JFIF standard 1.01, comment: "%S""
string(%d) "JPEG image data, JFIF standard 1.01, resolution (DPI), density 180x52, segment length 16, comment: "%S""
===DONE===

View File

@ -34,6 +34,6 @@ Done
--EXPECTF--
string(%d) "%s"
Warning: finfo_file(): Failed identify data 0:(null) in %s on line %d
Warning: finfo_file(): Failed identify data 0:indirect recursion nesting (%d) exceeded in %s on line %d
bool(false)
Done

View File

@ -25,5 +25,5 @@ var_dump( finfo_file( $finfo, $file, FILEINFO_CONTINUE ) );
--EXPECTF--
*** Testing finfo_file() : regex rules ***
string(28) "text/plain; charset=us-ascii"
string(22) "awk script, ASCII text"
string(30) "awk or perl script, ASCII text"
===DONE===

File diff suppressed because it is too large Load Diff

View File

@ -15,4 +15,4 @@ $test->method();
?>
--EXPECT--
string(28) "text/plain; charset=us-ascii"
string(28) "text/x-php; charset=us-ascii"