]> granicus.if.org Git - file/commitdiff
Slight improvements to tar code, and fix to -e soft.
authorReuben Thomas <rrt@sc3d.org>
Tue, 30 Nov 2010 14:58:53 +0000 (14:58 +0000)
committerReuben Thomas <rrt@sc3d.org>
Tue, 30 Nov 2010 14:58:53 +0000 (14:58 +0000)
ChangeLog
src/ascmagic.c
src/file.c
src/is_tar.c
src/tar.h

index a5386de89e3c397331211be7651319e9d1b6a379..eefdc712a4eae0955b5b8457bc2c5667cc2dc1cd 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2010-11-26  18:39  Reuben Thomas <rrt@sc3d.org>
+       * 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 <rrt@sc3d.org>
        * Make bug reporting addresses more visible.
 
index c86d1feaafce4ade46ddf77cce738a6f37430b35..237a3bdee98c7d465f0f14eaf1db2f87910d2c87 100644 (file)
@@ -36,7 +36,7 @@
 #include "file.h"
 
 #ifndef        lint
-FILE_RCSID("@(#)$File: ascmagic.c,v 1.75 2009/02/03 20:27:51 christos Exp $")
+FILE_RCSID("@(#)$File: ascmagic.c,v 1.76 2010/10/08 21:58:44 christos 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)
index fbc715ef437be7ce14e58acdae4d4a5307f30986..85d26c1526bd0061ddeaf33f915a1baf679d17e7 100644 (file)
@@ -32,7 +32,7 @@
 #include "file.h"
 
 #ifndef        lint
-FILE_RCSID("@(#)$File: file.c,v 1.138 2010/09/20 21:00:44 rrt Exp $")
+FILE_RCSID("@(#)$File: file.c,v 1.139 2010/10/08 22:03:57 christos 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))), "");
index ab657d904f9b8d491bb5546d8677febc3472931c..0c43d63013ecf9e49bf79ead620d0c45434e69de 100644 (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
@@ -40,7 +40,7 @@
 #include "file.h"
 
 #ifndef lint
-FILE_RCSID("@(#)$File: is_tar.c,v 1.35 2008/11/07 18:57:28 christos Exp $")
+FILE_RCSID("@(#)$File: is_tar.c,v 1.36 2009/02/03 20:27:51 christos 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;
 }
index 25b4ba7d953d22935837256e240603c6ac994cea..c3d0297d117f215622b65a486eb623cf14d04500 100644 (file)
--- 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.11 2007/01/16 14:56:45 ljt Exp $ # checkin only
+ * $File: tar.h,v 1.12 2008/02/07 00:58:52 christos 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];