file/src/is_tar.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) Ian F. Darwin 1986-1995.
00003  * Software written by Ian F. Darwin and others;
00004  * maintained 1995-present by Christos Zoulas and others.
00005  * 
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions
00008  * are met:
00009  * 1. Redistributions of source code must retain the above copyright
00010  *    notice immediately at the beginning of the file, without modification,
00011  *    this list of conditions, and the following disclaimer.
00012  * 2. Redistributions in binary form must reproduce the above copyright
00013  *    notice, this list of conditions and the following disclaimer in the
00014  *    documentation and/or other materials provided with the distribution.
00015  *  
00016  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
00017  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00019  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
00020  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00022  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00023  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00024  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00025  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00026  * SUCH DAMAGE.
00027  */
00028 /*
00029  * is_tar() -- figure out whether file is a tar archive.
00030  *
00031  * Stolen (by the author!) from the public domain tar program:
00032  * Public Domain version written 26 Aug 1985 John Gilmore (ihnp4!hoptoad!gnu).
00033  *
00034  * @(#)list.c 1.18 9/23/86 Public Domain - gnu
00035  *
00036  * Comments changed and some code/comments reformatted
00037  * for file command by Ian Darwin.
00038  */
00039 
00040 #include "file.h"
00041 #include "magic.h"
00042 #include <string.h>
00043 #include <ctype.h>
00044 #include <sys/types.h>
00045 #include "tar.h"
00046 
00047 #ifndef lint
00048 FILE_RCSID("@(#)$File: is_tar.c,v 1.27 2007/01/12 17:38:28 christos Exp $")
00049 #endif
00050 
00051 #define isodigit(c)     ( ((c) >= '0') && ((c) <= '7') )
00052 
00053 private int is_tar(const unsigned char *, size_t)
00054         /*@*/;
00055 private int from_oct(int, const char *)         /* Decode octal number */
00056         /*@*/;
00057 
00058 protected int
00059 file_is_tar(struct magic_set *ms, const unsigned char *buf, size_t nbytes)
00060 {
00061         /*
00062          * Do the tar test first, because if the first file in the tar
00063          * archive starts with a dot, we can confuse it with an nroff file.
00064          */
00065         switch (is_tar(buf, nbytes)) {
00066         case 1:
00067                 if (file_printf(ms, (ms->flags & MAGIC_MIME) ?
00068                     "application/x-tar" : "tar archive") == -1)
00069                         return -1;
00070                 return 1;
00071         case 2:
00072                 if (file_printf(ms, (ms->flags & MAGIC_MIME) ?
00073                     "application/x-tar, POSIX" : "POSIX tar archive") == -1)
00074                         return -1;
00075                 return 1;
00076         case 3:
00077                 if (file_printf(ms, (ms->flags & MAGIC_MIME) ?
00078                     "application/x-tar, POSIX (GNU)" :
00079                     "POSIX tar archive (GNU)") == -1)
00080                         return -1;
00081                 return 1;
00082         default:
00083                 return 0;
00084         }
00085 }
00086 
00087 /*
00088  * Return 
00089  *      0 if the checksum is bad (i.e., probably not a tar archive), 
00090  *      1 for old UNIX tar file,
00091  *      2 for Unix Std (POSIX) tar file.
00092  */
00093 private int
00094 is_tar(const unsigned char *buf, size_t nbytes)
00095 {
00096         const union record *header = (const union record *)(const void *)buf;
00097         int     i;
00098         int     sum, recsum;
00099         const char      *p;
00100 
00101         if (nbytes < sizeof(union record))
00102                 return 0;
00103 
00104         recsum = from_oct(8,  header->header.chksum);
00105 
00106         sum = 0;
00107         p = header->charptr;
00108         for (i = sizeof(union record); --i >= 0;) {
00109                 /*
00110                  * We cannot use unsigned char here because of old compilers,
00111                  * e.g. V7.
00112                  */
00113                 sum += 0xFF & *p++;
00114         }
00115 
00116         /* Adjust checksum to count the "chksum" field as blanks. */
00117         for (i = sizeof(header->header.chksum); --i >= 0;)
00118                 sum -= 0xFF & header->header.chksum[i];
00119         sum += ' '* sizeof header->header.chksum;       
00120 
00121         if (sum != recsum)
00122                 return 0;       /* Not a tar archive */
00123         
00124         if (strcmp(header->header.magic, GNUTMAGIC) == 0) 
00125                 return 3;               /* GNU Unix Standard tar archive */
00126         if (strcmp(header->header.magic, TMAGIC) == 0) 
00127                 return 2;               /* Unix Standard tar archive */
00128 
00129         return 1;                       /* Old fashioned tar archive */
00130 }
00131 
00132 
00133 /*
00134  * Quick and dirty octal conversion.
00135  *
00136  * Result is -1 if the field is invalid (all blank, or nonoctal).
00137  */
00138 private int
00139 from_oct(int digs, const char *where)
00140 {
00141         int     value;
00142 
00143         while (isspace((unsigned char)*where)) {        /* Skip spaces */
00144                 where++;
00145                 if (--digs <= 0)
00146                         return -1;              /* All blank field */
00147         }
00148         value = 0;
00149         while (digs > 0 && isodigit(*where)) {  /* Scan til nonoctal */
00150                 value = (value << 3) | (*where++ - '0');
00151                 --digs;
00152         }
00153 
00154         if (digs > 0 && *where && !isspace((unsigned char)*where))
00155                 return -1;                      /* Ended on non-space/nul */
00156 
00157         return value;
00158 }

Generated on Fri Aug 31 12:48:24 2007 for rpm by  doxygen 1.5.1