From 8de5594fa69a030da4f3c81a6592aeafb97a2873 Mon Sep 17 00:00:00 2001 From: Christos Zoulas Date: Sat, 20 May 1995 22:09:21 +0000 Subject: [PATCH] Passed incorrect argument to eatsize(). Use %ld and %lx where appropriate. Remove unused variables ELF support for both big and little endian Fixes for small files again. --- src/apprentice.c | 8 +++-- src/ascmagic.c | 12 +++---- src/compress.c | 17 ++++------ src/file.c | 88 +++++++++++++++++++++++++++--------------------- src/file.h | 4 +-- src/patchlevel.h | 13 +++++-- src/print.c | 8 ++--- src/softmagic.c | 20 +++++------ 8 files changed, 93 insertions(+), 77 deletions(-) diff --git a/src/apprentice.c b/src/apprentice.c index 76e90d46..08b5055e 100644 --- a/src/apprentice.c +++ b/src/apprentice.c @@ -33,7 +33,7 @@ #ifndef lint static char *moduleid = - "@(#)$Id: apprentice.c,v 1.21 1995/04/28 17:29:13 christos Exp $"; + "@(#)$Id: apprentice.c,v 1.22 1995/05/20 22:09:21 christos Exp $"; #endif /* lint */ #define EATAB {while (isascii((unsigned char) *l) && \ @@ -151,8 +151,9 @@ int *ndx, check; struct magic *m; char *t, *s; +#define ALLOC_INCR 20 if (nd+1 >= maxmagic){ - maxmagic += 20; + maxmagic += ALLOC_INCR; if ((magic = (struct magic *) realloc(magic, sizeof(struct magic) * maxmagic)) == NULL) { @@ -162,6 +163,7 @@ int *ndx, check; else exit(1); } + memset(&magic[*ndx], 0, sizeof(struct magic) * ALLOC_INCR); } m = &magic[*ndx]; m->flag = 0; @@ -370,7 +372,7 @@ char **p; } else if (m->reln != 'x') { m->value.l = signextend(m, strtoul(*p, p, 0)); - eatsize(&p); + eatsize(p); } return 0; } diff --git a/src/ascmagic.c b/src/ascmagic.c index 9f41a76a..600d468b 100644 --- a/src/ascmagic.c +++ b/src/ascmagic.c @@ -1,5 +1,5 @@ /* - * Ascii magic -- file types that we know based on keywords + * ASCII magic -- file types that we know based on keywords * that can appear anywhere in the file. * * Copyright (c) Ian F. Darwin, 1987. @@ -36,7 +36,7 @@ #ifndef lint static char *moduleid = - "@(#)$Id: ascmagic.c,v 1.19 1995/04/28 17:29:13 christos Exp $"; + "@(#)$Id: ascmagic.c,v 1.20 1995/05/20 22:09:21 christos Exp $"; #endif /* lint */ /* an optimisation over plain strcmp() */ @@ -47,7 +47,7 @@ ascmagic(buf, nbytes) unsigned char *buf; int nbytes; /* size actually read */ { - int i, isblock, has_escapes = 0; + int i, has_escapes = 0; unsigned char *s; char nbuf[HOWMANY+1]; /* one extra for terminating '\0' */ char *token; @@ -109,11 +109,11 @@ int nbytes; /* size actually read */ for (i = 0; i < nbytes; i++) { if (!isascii(buf[i])) - return 0; /* not all ascii */ + return 0; /* not all ASCII */ } - /* all else fails, but it is ascii... */ - ckfputs("ascii text", stdout); + /* all else fails, but it is ASCII... */ + ckfputs("ASCII text", stdout); if (has_escapes) { ckfputs(" (with escape sequences)", stdout); } diff --git a/src/compress.c b/src/compress.c index a665306a..096abb61 100644 --- a/src/compress.c +++ b/src/compress.c @@ -4,7 +4,7 @@ * information if recognized * uncompress(method, old, n, newch) - uncompress old into new, * using method, return sizeof new - * $Id: compress.c,v 1.8 1994/01/21 01:38:24 christos Exp $ + * $Id: compress.c,v 1.9 1995/05/20 22:09:21 christos Exp $ */ #include #include @@ -20,13 +20,12 @@ static struct { char *argv[3]; int silent; } compr[] = { - { "\037\235", 2, { "uncompress", "-c", NULL }, 0 }, - { "\037\213", 2, { "gzip", "-dq", NULL }, 1 }, - /* - * XXX pcat does not work, cause I don't know how to make it read stdin, - * so we use gzip - */ - { "\037\036", 2, { "gzip", "-dq", NULL }, 0 }, + { "\037\235", 2, { "uncompress", "-c", NULL }, 0 }, /* compressed */ + { "\037\213", 2, { "gzip", "-cdq", NULL }, 1 }, /* gzipped */ + { "\037\236", 2, { "gzip", "-cdq", NULL }, 1 }, /* frozen */ + { "\037\240", 2, { "gzip", "-cdq", NULL }, 1 }, /* SCO LZH */ + /* the standard pack utilities do not accept standard input */ + { "\037\036", 2, { "gzip", "-cdq", NULL }, 0 }, /* packed */ }; static int ncompr = sizeof(compr) / sizeof(compr[0]); @@ -121,5 +120,3 @@ int n; return n; } } - - diff --git a/src/file.c b/src/file.c index 409959cd..8e480f97 100644 --- a/src/file.c +++ b/src/file.c @@ -26,7 +26,7 @@ */ #ifndef lint static char *moduleid = - "@(#)$Id: file.c,v 1.32 1995/04/28 17:29:13 christos Exp $"; + "@(#)$Id: file.c,v 1.33 1995/05/20 22:09:21 christos Exp $"; #endif /* lint */ #include @@ -43,7 +43,7 @@ static char *moduleid = #endif #include /* for read() */ -#ifdef HAVE_ELF +#ifdef __ELF__ #include #endif @@ -216,10 +216,11 @@ int wid; struct utimbuf utbuf; struct stat sb; int nbytes = 0; /* number of bytes read from a datafile */ + char match = '\0'; if (strcmp("-", inname) == 0) { if (fstat(0, &sb)<0) { - error("cannot fstat `%s' (%s).\n", stdname, + error("cannot fstat `%s' (%s).\n", stdname, strerror(errno)); /*NOTREACHED*/ } @@ -238,7 +239,7 @@ int wid; putchar('\n'); return; } - + if ((fd = open(inname, O_RDONLY)) < 0) { /* We can't open it, but we were able to stat it. */ if (sb.st_mode & 0002) ckfputs("writeable, ", stdout); @@ -258,14 +259,13 @@ int wid; /*NOTREACHED*/ } - if (nbytes == 0) + if (nbytes == 0) ckfputs("empty", stdout); else { - buf[nbytes++] = '\0'; /* NULL terminate */ - tryit(buf, nbytes, zflag); + buf[nbytes++] = '\0'; /* null-terminate it */ + match = tryit(buf, nbytes, zflag); } - -#ifdef HAVE_ELF +#ifdef __ELF__ /* * ELF executables have multiple section headers in arbitrary * file locations and thus file(1) cannot determine it from easily. @@ -275,30 +275,41 @@ int wid; * Should come up with a better fix. */ - if (nbytes > sizeof (Elf32_Ehdr) && + if (match == 's' && nbytes > sizeof (Elf32_Ehdr) && buf[EI_MAG0] == ELFMAG0 && buf[EI_MAG1] == ELFMAG1 && buf[EI_MAG2] == ELFMAG2 && - buf[EI_MAG3] == ELFMAG3 ) { + buf[EI_MAG3] == ELFMAG3) { + union { + long l; + char c[sizeof (long)]; + } u; Elf32_Ehdr elfhdr; int stripped = 1; - + + u.l = 1; (void) memcpy(&elfhdr, buf, sizeof elfhdr); - - if (lseek(fd, elfhdr.e_shoff, SEEK_SET)<0) - error("lseek failed (%s).\n", strerror(errno)); - - for ( ; elfhdr.e_shnum ; elfhdr.e_shnum--) { + + /* + * If the system byteorder does not equal the object byteorder + * then don't test. + */ + if ((u.c[sizeof(long) - 1] + 1) == elfhdr.e_ident[5]) { + if (lseek(fd, elfhdr.e_shoff, SEEK_SET)<0) + error("lseek failed (%s).\n", strerror(errno)); + + for ( ; elfhdr.e_shnum ; elfhdr.e_shnum--) { if (read(fd, buf, elfhdr.e_shentsize)<0) - error("read failed (%s).\n", strerror(errno)); + error("read failed (%s).\n", strerror(errno)); if (((Elf32_Shdr *)&buf)->sh_type == SHT_SYMTAB) { - stripped = 0; - break; + stripped = 0; + break; } + } + if (stripped) + (void) printf (", stripped"); } - if (stripped) - (void) printf (" - stripped"); } #endif @@ -315,25 +326,24 @@ int wid; } -void +int tryit(buf, nb, zflag) unsigned char *buf; int nb, zflag; { - /* - * Try compression stuff - */ - if (!zflag || zmagic(buf, nb) != 1) - /* - * try tests in /etc/magic (or surrogate magic file) - */ - if (softmagic(buf, nb) != 1) - /* - * try known keywords, check for ascii-ness too. - */ - if (ascmagic(buf, nb) != 1) - /* - * abandon hope, all ye who remain here - */ - ckfputs("data", stdout); + /* try compression stuff */ + if (zflag && zmagic(buf, nb)) + return 'z'; + + /* try tests in /etc/magic (or surrogate magic file) */ + if (softmagic(buf, nb)) + return 's'; + + /* try known keywords, check whether it is ASCII */ + if (ascmagic(buf, nb)) + return 'a'; + + /* abandon hope, all ye who remain here */ + ckfputs("data", stdout); + return '\0'; } diff --git a/src/file.h b/src/file.h index 772bd8b4..c0bbf0e0 100644 --- a/src/file.h +++ b/src/file.h @@ -1,6 +1,6 @@ /* * file.h - definitions for file(1) program - * @(#)$Id: file.h,v 1.21 1995/04/28 17:29:13 christos Exp $ + * @(#)$Id: file.h,v 1.22 1995/05/20 22:09:21 christos Exp $ * * Copyright (c) Ian F. Darwin, 1987. * Written by Ian F. Darwin. @@ -94,7 +94,7 @@ extern void mdump __P((struct magic *)); extern void process __P((const char *, int)); extern void showstr __P((FILE *, const char *, int)); extern int softmagic __P((unsigned char *, int)); -extern void tryit __P((unsigned char *, int, int)); +extern int tryit __P((unsigned char *, int, int)); extern int zmagic __P((unsigned char *, int)); extern void ckfprintf __P((FILE *, const char *, ...)); extern unsigned long signextend __P((struct magic *, unsigned long)); diff --git a/src/patchlevel.h b/src/patchlevel.h index 48454540..2d22adf2 100644 --- a/src/patchlevel.h +++ b/src/patchlevel.h @@ -1,12 +1,19 @@ #define FILE_VERSION_MAJOR 3 -#define patchlevel 17 +#define patchlevel 18 /* * Patchlevel file for Ian Darwin's MAGIC command. - * $Id: patchlevel.h,v 1.17 1995/04/28 17:29:13 christos Exp $ + * $Id: patchlevel.h,v 1.18 1995/05/20 22:09:21 christos Exp $ * * $Log: patchlevel.h,v $ - * Revision 1.17 1995/04/28 17:29:13 christos + * Revision 1.18 1995/05/20 22:09:21 christos + * Passed incorrect argument to eatsize(). + * Use %ld and %lx where appropriate. + * Remove unused variables + * ELF support for both big and little endian + * Fixes for small files again. + * + * Revision 1.17 1995/04/28 17:29:13 christos * - Incorrect nroff detection fix from der Mouse * - Lost and incorrect magic entries. * - Added ELF stripped binary detection [in C; ugh] diff --git a/src/print.c b/src/print.c index a91f4297..17e1136a 100644 --- a/src/print.c +++ b/src/print.c @@ -40,7 +40,7 @@ #ifndef lint static char *moduleid = - "@(#)$Id: print.c,v 1.21 1994/05/03 17:58:23 christos Exp $"; + "@(#)$Id: print.c,v 1.22 1995/05/20 22:09:21 christos Exp $"; #endif /* lint */ #define SZOF(a) (sizeof(a) / sizeof(a[0])) @@ -58,7 +58,7 @@ struct magic *m; m->offset); if (m->flag & INDIR) - (void) fprintf(stderr, "(%s,%d),", + (void) fprintf(stderr, "(%s,%ld),", (m->in.type >= 0 && m->in.type < SZOF(typ)) ? typ[(unsigned char) m->in.type] : "*bad*", @@ -69,7 +69,7 @@ struct magic *m; typ[(unsigned char) m->type] : "*bad*"); if (m->mask != ~0L) - (void) fprintf(stderr, " & %.8x", m->mask); + (void) fprintf(stderr, " & %.8lx", m->mask); (void) fprintf(stderr, ",%c", m->reln); @@ -82,7 +82,7 @@ struct magic *m; case LELONG: case BESHORT: case BELONG: - (void) fprintf(stderr, "%d", m->value.l); + (void) fprintf(stderr, "%ld", m->value.l); break; case STRING: showstr(stderr, m->value.s, -1); diff --git a/src/softmagic.c b/src/softmagic.c index 92d4d773..76fd6d81 100644 --- a/src/softmagic.c +++ b/src/softmagic.c @@ -34,7 +34,7 @@ #ifndef lint static char *moduleid = - "@(#)$Id: softmagic.c,v 1.29 1995/04/28 19:13:08 christos Exp $"; + "@(#)$Id: softmagic.c,v 1.30 1995/05/20 22:09:21 christos Exp $"; #endif /* lint */ static int match __P((unsigned char *, int)); @@ -273,7 +273,7 @@ long offset; char *str; int len; { - (void) fprintf(stderr, "mget @%d: ", offset); + (void) fprintf(stderr, "mget @%ld: ", offset); showstr(stderr, (char *) str, len); (void) fputc('\n', stderr); (void) fputc('\n', stderr); @@ -287,18 +287,18 @@ struct magic *m; int nbytes; { long offset = m->offset; - long diff = nbytes - (offset + sizeof(union VALUETYPE)); - if (diff >= 0) + + if (offset + sizeof(union VALUETYPE) <= nbytes) memcpy(p, s + offset, sizeof(union VALUETYPE)); else { - /* Not enough space; zeropad */ - long have = sizeof(union VALUETYPE) + diff; + /* + * the usefulness of padding with zeroes eludes me, it + * might even cause problems + */ + long have = nbytes - offset; + memset(p, 0, sizeof(union VALUETYPE)); if (have > 0) memcpy(p, s + offset, have); - else - have = 0; - - memset(p + have, 0, sizeof(union VALUETYPE) - have); } -- 2.40.0