From: Reuben Thomas Date: Tue, 30 Nov 2010 14:58:53 +0000 (+0000) Subject: Slight improvements to tar code, and fix to -e soft. X-Git-Tag: FILE5_05~31 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=a1d79b486f20e4be1d66c5b5d149bcbcd3661e2b;p=file Slight improvements to tar code, and fix to -e soft. --- diff --git a/ChangeLog b/ChangeLog index a5386de8..eefdc712 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2010-11-26 18:39 Reuben Thomas + * Fix "-e soft": it was ignored when softmagic was called + during asciimagic. + * Improve comments and use "unsigned char" in tar.h/is_tar.c. + 2010-11-05 17:26 Reuben Thomas * Make bug reporting addresses more visible. diff --git a/src/ascmagic.c b/src/ascmagic.c index 964ba043..94db560f 100644 --- a/src/ascmagic.c +++ b/src/ascmagic.c @@ -36,7 +36,7 @@ #include "file.h" #ifndef lint -FILE_RCSID("@(#)$File: ascmagic.c,v 1.76 2010/10/08 21:58:44 christos Exp $") +FILE_RCSID("@(#)$File: ascmagic.c,v 1.77 2010/11/30 14:58:53 rrt Exp $") #endif /* lint */ #include "magic.h" @@ -93,7 +93,7 @@ file_ascmagic(struct magic_set *ms, const unsigned char *buf, size_t nbytes) goto done; } - rv = file_ascmagic_with_encoding(ms, buf, nbytes, ubuf, ulen, code, + rv = file_ascmagic_with_encoding(ms, buf, nbytes, ubuf, ulen, code, type); done: @@ -141,21 +141,23 @@ file_ascmagic_with_encoding(struct magic_set *ms, const unsigned char *buf, goto done; } - /* Convert ubuf to UTF-8 and try text soft magic */ - /* malloc size is a conservative overestimate; could be - improved, or at least realloced after conversion. */ - mlen = ulen * 6; - if ((utf8_buf = CAST(unsigned char *, malloc(mlen))) == NULL) { - file_oomem(ms, mlen); - goto done; + if ((ms->flags & MAGIC_NO_CHECK_SOFT) == 0) { + /* Convert ubuf to UTF-8 and try text soft magic */ + /* malloc size is a conservative overestimate; could be + improved, or at least realloced after conversion. */ + mlen = ulen * 6; + if ((utf8_buf = CAST(unsigned char *, malloc(mlen))) == NULL) { + file_oomem(ms, mlen); + goto done; + } + if ((utf8_end = encode_utf8(utf8_buf, mlen, ubuf, ulen)) == NULL) + goto done; + if ((rv = file_softmagic(ms, utf8_buf, (size_t)(utf8_end - utf8_buf), + TEXTTEST)) != 0) + goto done; + else + rv = -1; } - if ((utf8_end = encode_utf8(utf8_buf, mlen, ubuf, ulen)) == NULL) - goto done; - if ((rv = file_softmagic(ms, utf8_buf, (size_t)(utf8_end - utf8_buf), - TEXTTEST)) != 0) - goto done; - else - rv = -1; /* look for tokens from names.h - this is expensive! */ if ((ms->flags & MAGIC_NO_CHECK_TOKENS) != 0) diff --git a/src/file.c b/src/file.c index 554cc182..89769f5e 100644 --- a/src/file.c +++ b/src/file.c @@ -32,7 +32,7 @@ #include "file.h" #ifndef lint -FILE_RCSID("@(#)$File: file.c,v 1.139 2010/10/08 22:03:57 christos Exp $") +FILE_RCSID("@(#)$File: file.c,v 1.140 2010/11/30 14:58:53 rrt Exp $") #endif /* lint */ #include "magic.h" @@ -253,7 +253,7 @@ main(int argc, char *argv[]) flags |= MAGIC_DEVICES; break; case 'v': - if (magicfile == NULL) + if (magicfile == NULL) magicfile = magic_getpath(magicfile, action); (void)fprintf(stderr, "%s-%d.%.2d\n", progname, FILE_VERSION_MAJOR, patchlevel); @@ -423,7 +423,7 @@ process(struct magic_set *ms, const char *inname, int wid) if (wid > 0 && !bflag) { (void)printf("%s", std_in ? "/dev/stdin" : inname); if (nulsep) - (void)putc('\0', stdout); + (void)putc('\0', stdout); (void)printf("%s", separator); (void)printf("%*s ", (int) (nopad ? 0 : (wid - file_mbswidth(inname))), ""); diff --git a/src/is_tar.c b/src/is_tar.c index f962edbd..876c631b 100644 --- a/src/is_tar.c +++ b/src/is_tar.c @@ -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 @@ -40,7 +40,7 @@ #include "file.h" #ifndef lint -FILE_RCSID("@(#)$File: is_tar.c,v 1.36 2009/02/03 20:27:51 christos Exp $") +FILE_RCSID("@(#)$File: is_tar.c,v 1.37 2010/11/30 14:58:53 rrt Exp $") #endif #include "magic.h" @@ -83,8 +83,8 @@ file_is_tar(struct magic_set *ms, const unsigned char *buf, size_t nbytes) } /* - * Return - * 0 if the checksum is bad (i.e., probably not a tar archive), + * Return + * 0 if the checksum is bad (i.e., probably not a tar archive), * 1 for old UNIX tar file, * 2 for Unix Std (POSIX) tar file, * 3 for GNU tar file. @@ -95,7 +95,7 @@ is_tar(const unsigned char *buf, size_t nbytes) const union record *header = (const union record *)(const void *)buf; int i; int sum, recsum; - const char *p; + const unsigned char *p; if (nbytes < sizeof(union record)) return 0; @@ -104,25 +104,20 @@ is_tar(const unsigned char *buf, size_t nbytes) sum = 0; p = header->charptr; - for (i = sizeof(union record); --i >= 0;) { - /* - * We cannot use unsigned char here because of old compilers, - * e.g. V7. - */ - sum += 0xFF & *p++; - } + for (i = sizeof(union record); --i >= 0;) + sum += *p++; /* Adjust checksum to count the "chksum" field as blanks. */ for (i = sizeof(header->header.chksum); --i >= 0;) - sum -= 0xFF & header->header.chksum[i]; - sum += ' '* sizeof header->header.chksum; + sum -= header->header.chksum[i]; + sum += ' ' * sizeof header->header.chksum; if (sum != recsum) return 0; /* Not a tar archive */ - - if (strcmp(header->header.magic, GNUTMAGIC) == 0) + + if (strcmp(header->header.magic, GNUTMAGIC) == 0) return 3; /* GNU Unix Standard tar archive */ - if (strcmp(header->header.magic, TMAGIC) == 0) + if (strcmp(header->header.magic, TMAGIC) == 0) return 2; /* Unix Standard tar archive */ return 1; /* Old fashioned tar archive */ @@ -132,7 +127,7 @@ is_tar(const unsigned char *buf, size_t nbytes) /* * Quick and dirty octal conversion. * - * Result is -1 if the field is invalid (all blank, or nonoctal). + * Result is -1 if the field is invalid (all blank, or non-octal). */ private int from_oct(int digs, const char *where) @@ -145,13 +140,13 @@ from_oct(int digs, const char *where) return -1; /* All blank field */ } value = 0; - while (digs > 0 && isodigit(*where)) { /* Scan til nonoctal */ + while (digs > 0 && isodigit(*where)) { /* Scan til non-octal */ value = (value << 3) | (*where++ - '0'); --digs; } if (digs > 0 && *where && !isspace((unsigned char)*where)) - return -1; /* Ended on non-space/nul */ + return -1; /* Ended on non-(space/NUL) */ return value; } diff --git a/src/tar.h b/src/tar.h index fa2390ab..854d4552 100644 --- a/src/tar.h +++ b/src/tar.h @@ -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 @@ * * Created 25 August 1985 by John Gilmore, ihnp4!hoptoad!gnu. * - * $File: tar.h,v 1.12 2008/02/07 00:58:52 christos Exp $ # checkin only + * $File: tar.h,v 1.13 2010/11/30 14:58:53 rrt Exp $ # checkin only */ /* @@ -49,7 +49,7 @@ #define TGNMLEN 32 union record { - char charptr[RECORDSIZE]; + unsigned char charptr[RECORDSIZE]; struct header { char name[NAMSIZ]; char mode[8];