#include "file.h"
#ifndef lint
-FILE_RCSID("@(#)$File: compress.c,v 1.91 2015/11/13 15:42:18 christos Exp $")
+FILE_RCSID("@(#)$File: compress.c,v 1.92 2016/02/08 01:29:49 christos Exp $")
#endif
#include "magic.h"
private ssize_t swrite(int, const void *, size_t);
#if HAVE_FORK
private size_t ncompr = sizeof(compr) / sizeof(compr[0]);
-private int uncompressbuf(int, size_t, const unsigned char *, unsigned char **,
- size_t *);
+private int uncompressbuf(int, size_t, size_t, const unsigned char *,
+ unsigned char **, size_t *);
#ifdef BUILTIN_DECOMPRESS
-private int uncompresszlib(const unsigned char *, unsigned char **, size_t *,
- int);
-private int uncompressgzipped(const unsigned char *, unsigned char **,
+private int uncompresszlib(const unsigned char *, unsigned char **, size_t,
+ size_t *, int);
+private int uncompressgzipped(const unsigned char *, unsigned char **, size_t,
size_t *);
#endif
static int makeerror(unsigned char **, size_t *, const char *, ...)
if (!zm)
continue;
nsz = nbytes;
- rv = uncompressbuf(fd, i, buf, &newbuf, &nsz);
+ rv = uncompressbuf(fd, ms->bytes_max, i, buf, &newbuf, &nsz);
DPRINTF("uncompressbuf = %d, %s, %zu\n", rv, (char *)newbuf,
nsz);
switch (rv) {
private int
-uncompressgzipped(const unsigned char *old, unsigned char **newch, size_t *n)
+uncompressgzipped(const unsigned char *old, unsigned char **newch,
+ size_t bytes_max, size_t *n)
{
unsigned char flg = old[3];
size_t data_start = 10;
*n -= data_start;
old += data_start;
- return uncompresszlib(old, newch, n, 0);
+ return uncompresszlib(old, newch, bytes_max, n, 0);
err:
return makeerror(newch, n, "File too short");
}
private int
-uncompresszlib(const unsigned char *old, unsigned char **newch, size_t *n,
- int zlib)
+uncompresszlib(const unsigned char *old, unsigned char **newch,
+ size_t bytes_max, size_t *n, int zlib)
{
int rc;
z_stream z;
- if ((*newch = CAST(unsigned char *, malloc(HOWMANY + 1))) == NULL)
+ if ((*newch = CAST(unsigned char *, malloc(bytes_max + 1))) == NULL)
return makeerror(newch, n, "No buffer, %s", strerror(errno));
z.next_in = CCAST(Bytef *, old);
z.avail_in = CAST(uint32_t, *n);
z.next_out = *newch;
- z.avail_out = HOWMANY;
+ z.avail_out = bytes_max;
z.zalloc = Z_NULL;
z.zfree = Z_NULL;
z.opaque = Z_NULL;
return OKDATA;
err:
- strlcpy((char *)*newch, z.msg, HOWMANY);
+ strlcpy((char *)*newch, z.msg, bytes_max);
*n = strlen((char *)*newch);
return ERRDATA;
}
}
private int
-uncompressbuf(int fd, size_t method, const unsigned char *old,
+uncompressbuf(int fd, size_t bytes_max, size_t method, const unsigned char *old,
unsigned char **newch, size_t* n)
{
int fdp[3][2];
#ifdef BUILTIN_DECOMPRESS
/* FIXME: This doesn't cope with bzip2 */
if (method == 2)
- return uncompressgzipped(old, newch, n);
+ return uncompressgzipped(old, newch, bytes_max, n);
if (compr[method].maglen == 0)
- return uncompresszlib(old, newch, n, 1);
+ return uncompresszlib(old, newch, bytes_max, n, 1);
#endif
(void)fflush(stdout);
(void)fflush(stderr);
if (fd == -1)
writechild(fdp, old, *n);
- *newch = CAST(unsigned char *, malloc(HOWMANY + 1));
+ *newch = CAST(unsigned char *, malloc(bytes_max + 1));
if (*newch == NULL) {
rv = makeerror(newch, n, "No buffer, %s",
strerror(errno));
goto err;
}
rv = OKDATA;
- if ((r = sread(fdp[STDOUT_FILENO][0], *newch, HOWMANY, 0)) > 0)
+ if ((r = sread(fdp[STDOUT_FILENO][0], *newch, bytes_max, 0)) > 0)
break;
DPRINTF("Read stdout failed %d (%s)\n", fdp[STDOUT_FILENO][0],
r != -1 ? strerror(errno) : "no data");
rv = ERRDATA;
if (r == 0 &&
- (r = sread(fdp[STDERR_FILENO][0], *newch, HOWMANY, 0)) > 0)
+ (r = sread(fdp[STDERR_FILENO][0], *newch, bytes_max, 0)) > 0)
{
r = filter_error(*newch, r);
break;
#include "file.h"
#ifndef lint
-FILE_RCSID("@(#)$File: magic.c,v 1.95 2015/09/11 17:24:09 christos Exp $")
+FILE_RCSID("@(#)$File: magic.c,v 1.96 2016/02/08 02:20:36 christos Exp $")
#endif /* lint */
#include "magic.h"
* some overlapping space for matches near EOF
*/
#define SLOP (1 + sizeof(union VALUETYPE))
- if ((buf = CAST(unsigned char *, malloc(HOWMANY + SLOP))) == NULL)
+ if ((buf = CAST(unsigned char *, malloc(ms->bytes_max + SLOP))) == NULL)
return NULL;
switch (file_fsmagic(ms, inname, &sb)) {
}
/*
- * try looking at the first HOWMANY bytes
+ * try looking at the first ms->bytes_max bytes
*/
if (ispipe) {
ssize_t r = 0;
while ((r = sread(fd, (void *)&buf[nbytes],
- (size_t)(HOWMANY - nbytes), 1)) > 0) {
+ (size_t)(ms->bytes_max - nbytes), 1)) > 0) {
nbytes += r;
if (r < PIPE_BUF) break;
}
} else {
/* Windows refuses to read from a big console buffer. */
size_t howmany =
-#if defined(WIN32) && HOWMANY > 8 * 1024
+#if defined(WIN32)
_isatty(fd) ? 8 * 1024 :
#endif
- HOWMANY;
+ ms->bytes_max;
if ((nbytes = read(fd, (char *)buf, howmany)) == -1) {
if (inname == NULL && fd != STDIN_FILENO)
file_error(ms, errno, "cannot read fd %d", fd);
case MAGIC_PARAM_REGEX_MAX:
ms->elf_notes_max = (uint16_t)*(const size_t *)val;
return 0;
+ case MAGIC_PARAM_BYTES_MAX:
+ ms->bytes_max = *(const size_t *)val;
+ return 0;
default:
errno = EINVAL;
return -1;
case MAGIC_PARAM_REGEX_MAX:
*(size_t *)val = ms->regex_max;
return 0;
+ case MAGIC_PARAM_BYTES_MAX:
+ *(size_t *)val = ms->bytes_max;
+ return 0;
default:
errno = EINVAL;
return -1;