Back to index

php5  5.3.10
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 
00042 #ifndef lint
00043 FILE_RCSID("@(#)$File: is_tar.c,v 1.36 2009/02/03 20:27:51 christos Exp $")
00044 #endif
00045 
00046 #include "magic.h"
00047 #include <string.h>
00048 #include <ctype.h>
00049 #include "tar.h"
00050 
00051 #define       isodigit(c)   ( ((c) >= '0') && ((c) <= '7') )
00052 
00053 private int is_tar(const unsigned char *, size_t);
00054 private int from_oct(int, const char *);  /* Decode octal number */
00055 
00056 static const char tartype[][32] = {
00057        "tar archive",
00058        "POSIX tar archive",
00059        "POSIX tar archive (GNU)",
00060 };
00061 
00062 protected int
00063 file_is_tar(struct magic_set *ms, const unsigned char *buf, size_t nbytes)
00064 {
00065        /*
00066         * Do the tar test first, because if the first file in the tar
00067         * archive starts with a dot, we can confuse it with an nroff file.
00068         */
00069        int tar;
00070        int mime = ms->flags & MAGIC_MIME;
00071 
00072        if ((ms->flags & MAGIC_APPLE) != 0)
00073               return 0;
00074 
00075        tar = is_tar(buf, nbytes);
00076        if (tar < 1 || tar > 3)
00077               return 0;
00078 
00079        if (file_printf(ms, "%s", mime ? "application/x-tar" :
00080            tartype[tar - 1]) == -1)
00081               return -1;
00082        return 1;
00083 }
00084 
00085 /*
00086  * Return 
00087  *     0 if the checksum is bad (i.e., probably not a tar archive), 
00088  *     1 for old UNIX tar file,
00089  *     2 for Unix Std (POSIX) tar file,
00090  *     3 for GNU tar file.
00091  */
00092 private int
00093 is_tar(const unsigned char *buf, size_t nbytes)
00094 {
00095        const union record *header = (const union record *)(const void *)buf;
00096        int    i;
00097        int    sum, recsum;
00098        const char    *p;
00099 
00100        if (nbytes < sizeof(union record))
00101               return 0;
00102 
00103        recsum = from_oct(8,  header->header.chksum);
00104 
00105        sum = 0;
00106        p = header->charptr;
00107        for (i = sizeof(union record); --i >= 0;) {
00108               /*
00109                * We cannot use unsigned char here because of old compilers,
00110                * e.g. V7.
00111                */
00112               sum += 0xFF & *p++;
00113        }
00114 
00115        /* Adjust checksum to count the "chksum" field as blanks. */
00116        for (i = sizeof(header->header.chksum); --i >= 0;)
00117               sum -= 0xFF & header->header.chksum[i];
00118        sum += ' '* sizeof header->header.chksum; 
00119 
00120        if (sum != recsum)
00121               return 0;     /* Not a tar archive */
00122        
00123        if (strcmp(header->header.magic, GNUTMAGIC) == 0) 
00124               return 3;            /* GNU Unix Standard tar archive */
00125        if (strcmp(header->header.magic, TMAGIC) == 0) 
00126               return 2;            /* Unix Standard tar archive */
00127 
00128        return 1;                   /* Old fashioned tar archive */
00129 }
00130 
00131 
00132 /*
00133  * Quick and dirty octal conversion.
00134  *
00135  * Result is -1 if the field is invalid (all blank, or nonoctal).
00136  */
00137 private int
00138 from_oct(int digs, const char *where)
00139 {
00140        int    value;
00141 
00142        while (isspace((unsigned char)*where)) {  /* Skip spaces */
00143               where++;
00144               if (--digs <= 0)
00145                      return -1;           /* All blank field */
00146        }
00147        value = 0;
00148        while (digs > 0 && isodigit(*where)) {    /* Scan til nonoctal */
00149               value = (value << 3) | (*where++ - '0');
00150               --digs;
00151        }
00152 
00153        if (digs > 0 && *where && !isspace((unsigned char)*where))
00154               return -1;                  /* Ended on non-space/nul */
00155 
00156        return value;
00157 }