From: Christos Zoulas Date: Thu, 9 Oct 2003 15:15:23 +0000 (+0000) Subject: Add a raw flag, and translate unprintable chars. X-Git-Tag: FILE4_06~13 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=e1f57c21787606d32a62d24f159a805f689c4624;p=file Add a raw flag, and translate unprintable chars. --- diff --git a/src/file.c b/src/file.c index 2a262f87..751a1423 100644 --- a/src/file.c +++ b/src/file.c @@ -72,7 +72,7 @@ #include "patchlevel.h" #ifndef lint -FILE_RCSID("@(#)$Id: file.c,v 1.83 2003/10/08 17:09:26 christos Exp $") +FILE_RCSID("@(#)$Id: file.c,v 1.84 2003/10/09 15:15:23 christos Exp $") #endif /* lint */ @@ -128,7 +128,7 @@ main(int argc, char *argv[]) int flags = 0; char *mime, *home, *usermagic; struct stat sb; -#define OPTSTRING "bcCdf:F:ikLm:nNpsvz" +#define OPTSTRING "bcCdf:F:ikLm:nNprsvz" #ifdef HAVE_GETOPT_LONG int longindex; private struct option long_options[] = @@ -150,6 +150,7 @@ main(int argc, char *argv[]) {"preserve-date", 0, 0, 'p'}, #endif {"uncompress", 0, 0, 'z'}, + {"raw", 0, 0, 'r'}, {"no-buffer", 0, 0, 'n'}, {"no-pad", 0, 0, 'N'}, {"special-files", 0, 0, 's'}, @@ -242,6 +243,9 @@ main(int argc, char *argv[]) flags |= MAGIC_PRESERVE_ATIME; break; #endif + case 'r': + flags |= MAGIC_RAW; + break; case 's': flags |= MAGIC_DEVICES; break; diff --git a/src/file.h b/src/file.h index c05a4fe2..3d2cb2cf 100644 --- a/src/file.h +++ b/src/file.h @@ -32,7 +32,7 @@ */ /* * file.h - definitions for file(1) program - * @(#)$Id: file.h,v 1.57 2003/10/08 16:37:27 christos Exp $ + * @(#)$Id: file.h,v 1.58 2003/10/09 15:15:23 christos Exp $ */ #ifndef __file_h__ @@ -178,10 +178,14 @@ struct magic_set { int32_t *off; } c; struct out { + /* Accumulation buffer */ char *buf; char *ptr; size_t len; size_t size; + /* Printable buffer */ + char *pbuf; + size_t psize; } o; int flags; int haderr; @@ -209,6 +213,7 @@ protected void file_error(struct magic_set *, const char *, ...); protected void file_magwarn(const char *, ...); protected void file_mdump(struct magic *); protected void file_showstr(FILE *, const char *, size_t); +protected const char *file_getbuffer(struct magic_set *); #ifndef HAVE_STRERROR extern int sys_nerr; diff --git a/src/funcs.c b/src/funcs.c index 7d72bd99..afb7555c 100644 --- a/src/funcs.c +++ b/src/funcs.c @@ -31,6 +31,7 @@ #include #include #include +#include /* * Like printf, only we print to a buffer and advance it. @@ -135,3 +136,39 @@ file_reset(struct magic_set *ms) ms->haderr = 0; return 0; } + +protected const char * +file_getbuffer(struct magic_set *ms) +{ + char *nbuf, *op, *np; + size_t nsize; + + if (ms->haderr) + return NULL; + + if (ms->flags & MAGIC_RAW) + return ms->o.buf; + + nsize = ms->o.len * 4 + 1; + if (ms->o.psize < nsize) { + if ((nbuf = realloc(ms->o.pbuf, nsize)) == NULL) { + file_oomem(ms); + return NULL; + } + ms->o.psize = nsize; + ms->o.pbuf = nbuf; + } + + for (np = ms->o.pbuf, op = ms->o.buf; *op; op++) { + if (isprint((unsigned char)*op)) { + *np++ = *op; + } else { + *np++ = '\\'; + *np++ = ((*op >> 6) & 3) + '0'; + *np++ = ((*op >> 3) & 7) + '0'; + *np++ = ((*op >> 0) & 7) + '0'; + } + } + *np = '\0'; + return ms->o.pbuf; +} diff --git a/src/magic.c b/src/magic.c index a0eccb4c..f5473aad 100644 --- a/src/magic.c +++ b/src/magic.c @@ -65,7 +65,7 @@ #include "patchlevel.h" #ifndef lint -FILE_RCSID("@(#)$Id: magic.c,v 1.11 2003/10/08 16:37:27 christos Exp $") +FILE_RCSID("@(#)$Id: magic.c,v 1.12 2003/10/09 15:15:23 christos Exp $") #endif /* lint */ #ifdef __EMX__ @@ -98,8 +98,15 @@ magic_open(int flags) free(ms); return NULL; } + ms->o.pbuf = malloc(ms->o.psize = 1024); + if (ms->o.pbuf == NULL) { + free(ms->o.buf); + free(ms); + return NULL; + } ms->c.off = malloc((ms->c.len = 10) * sizeof(*ms->c.off)); if (ms->c.off == NULL) { + free(ms->o.pbuf); free(ms->o.buf); free(ms); return NULL; @@ -216,7 +223,7 @@ magic_file(struct magic_set *ms, const char *inname) case 0: break; default: - return ms->o.buf; + return file_getbuffer(ms); } #ifndef STDIN_FILENO @@ -232,7 +239,7 @@ magic_file(struct magic_set *ms, const char *inname) if (sb.st_mode & 0111) if (file_printf(ms, "executable, ") == -1) return NULL; - return ms->o.buf; + return file_getbuffer(ms); } /* @@ -258,7 +265,7 @@ magic_file(struct magic_set *ms, const char *inname) case 0: break; default: - return ms->o.buf; + return file_getbuffer(ms); } #endif if (file_buffer(ms, buf, (size_t)nbytes) == -1) @@ -279,7 +286,7 @@ magic_file(struct magic_set *ms, const char *inname) } close_and_restore(ms, inname, fd, &sb); - return ms->haderr ? NULL : ms->o.buf; + return file_getbuffer(ms); done: close_and_restore(ms, inname, fd, &sb); return NULL; @@ -298,7 +305,7 @@ magic_buffer(struct magic_set *ms, const void *buf, size_t nb) if (file_buffer(ms, buf, nb) == -1) { return NULL; } - return ms->haderr ? NULL : ms->o.buf; + return file_getbuffer(ms); } public const char * diff --git a/src/magic.h b/src/magic.h index ee120d2f..9d839d97 100644 --- a/src/magic.h +++ b/src/magic.h @@ -31,16 +31,16 @@ #include -#define MAGIC_NONE 0x00 /* No flags */ -#define MAGIC_DEBUG 0x01 /* Turn on debugging */ -#define MAGIC_SYMLINK 0x02 /* Follow symlinks */ -#define MAGIC_COMPRESS 0x04 /* Check inside compressed files */ -#define MAGIC_DEVICES 0x08 /* Look at the contents of devices */ -#define MAGIC_MIME 0x10 /* Return a mime string */ -#define MAGIC_CONTINUE 0x20 /* Return all matches */ -#define MAGIC_CHECK 0x40 /* Print warnings to stderr */ -#define MAGIC_PRESERVE_ATIME 0x80 /* Restore access time on exit */ - +#define MAGIC_NONE 0x000 /* No flags */ +#define MAGIC_DEBUG 0x001 /* Turn on debugging */ +#define MAGIC_SYMLINK 0x002 /* Follow symlinks */ +#define MAGIC_COMPRESS 0x004 /* Check inside compressed files */ +#define MAGIC_DEVICES 0x008 /* Look at the contents of devices */ +#define MAGIC_MIME 0x010 /* Return a mime string */ +#define MAGIC_CONTINUE 0x020 /* Return all matches */ +#define MAGIC_CHECK 0x040 /* Print warnings to stderr */ +#define MAGIC_PRESERVE_ATIME 0x080 /* Restore access time on exit */ +#define MAGIC_RAW 0x100 /* Don't translate unprintable chars */ #ifdef __cplusplus extern "C" {