is_tar.c revision 191736
1178361Ssam/* 2178361Ssam * Copyright (c) Ian F. Darwin 1986-1995. 3178361Ssam * Software written by Ian F. Darwin and others; 4178361Ssam * maintained 1995-present by Christos Zoulas and others. 5178361Ssam * 6178361Ssam * Redistribution and use in source and binary forms, with or without 7178361Ssam * modification, are permitted provided that the following conditions 8178361Ssam * are met: 9178361Ssam * 1. Redistributions of source code must retain the above copyright 10178361Ssam * notice immediately at the beginning of the file, without modification, 11178361Ssam * this list of conditions, and the following disclaimer. 12178361Ssam * 2. Redistributions in binary form must reproduce the above copyright 13178361Ssam * notice, this list of conditions and the following disclaimer in the 14178361Ssam * documentation and/or other materials provided with the distribution. 15178361Ssam * 16178361Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17178361Ssam * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18178361Ssam * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19178361Ssam * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 20178361Ssam * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21178361Ssam * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22178361Ssam * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23178361Ssam * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24178361Ssam * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25178361Ssam * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26178361Ssam * SUCH DAMAGE. 27178361Ssam */ 28178361Ssam/* 29178361Ssam * is_tar() -- figure out whether file is a tar archive. 30178361Ssam * 31178361Ssam * Stolen (by the author!) from the public domain tar program: 32178361Ssam * Public Domain version written 26 Aug 1985 John Gilmore (ihnp4!hoptoad!gnu). 33178361Ssam * 34178361Ssam * @(#)list.c 1.18 9/23/86 Public Domain - gnu 35178361Ssam * 36178361Ssam * Comments changed and some code/comments reformatted 37178361Ssam * for file command by Ian Darwin. 38178361Ssam */ 39178361Ssam 40178361Ssam#include "file.h" 41178361Ssam 42178361Ssam#ifndef lint 43178361SsamFILE_RCSID("@(#)$File: is_tar.c,v 1.36 2009/02/03 20:27:51 christos Exp $") 44178361Ssam#endif 45178361Ssam 46178361Ssam#include "magic.h" 47178361Ssam#include <string.h> 48178361Ssam#include <ctype.h> 49178361Ssam#include "tar.h" 50178361Ssam 51178361Ssam#define isodigit(c) ( ((c) >= '0') && ((c) <= '7') ) 52178361Ssam 53178361Ssamprivate int is_tar(const unsigned char *, size_t); 54private int from_oct(int, const char *); /* Decode octal number */ 55 56static const char tartype[][32] = { 57 "tar archive", 58 "POSIX tar archive", 59 "POSIX tar archive (GNU)", 60}; 61 62protected int 63file_is_tar(struct magic_set *ms, const unsigned char *buf, size_t nbytes) 64{ 65 /* 66 * Do the tar test first, because if the first file in the tar 67 * archive starts with a dot, we can confuse it with an nroff file. 68 */ 69 int tar; 70 int mime = ms->flags & MAGIC_MIME; 71 72 if ((ms->flags & MAGIC_APPLE) != 0) 73 return 0; 74 75 tar = is_tar(buf, nbytes); 76 if (tar < 1 || tar > 3) 77 return 0; 78 79 if (file_printf(ms, "%s", mime ? "application/x-tar" : 80 tartype[tar - 1]) == -1) 81 return -1; 82 return 1; 83} 84 85/* 86 * Return 87 * 0 if the checksum is bad (i.e., probably not a tar archive), 88 * 1 for old UNIX tar file, 89 * 2 for Unix Std (POSIX) tar file, 90 * 3 for GNU tar file. 91 */ 92private int 93is_tar(const unsigned char *buf, size_t nbytes) 94{ 95 const union record *header = (const union record *)(const void *)buf; 96 int i; 97 int sum, recsum; 98 const char *p; 99 100 if (nbytes < sizeof(union record)) 101 return 0; 102 103 recsum = from_oct(8, header->header.chksum); 104 105 sum = 0; 106 p = header->charptr; 107 for (i = sizeof(union record); --i >= 0;) { 108 /* 109 * We cannot use unsigned char here because of old compilers, 110 * e.g. V7. 111 */ 112 sum += 0xFF & *p++; 113 } 114 115 /* Adjust checksum to count the "chksum" field as blanks. */ 116 for (i = sizeof(header->header.chksum); --i >= 0;) 117 sum -= 0xFF & header->header.chksum[i]; 118 sum += ' '* sizeof header->header.chksum; 119 120 if (sum != recsum) 121 return 0; /* Not a tar archive */ 122 123 if (strcmp(header->header.magic, GNUTMAGIC) == 0) 124 return 3; /* GNU Unix Standard tar archive */ 125 if (strcmp(header->header.magic, TMAGIC) == 0) 126 return 2; /* Unix Standard tar archive */ 127 128 return 1; /* Old fashioned tar archive */ 129} 130 131 132/* 133 * Quick and dirty octal conversion. 134 * 135 * Result is -1 if the field is invalid (all blank, or nonoctal). 136 */ 137private int 138from_oct(int digs, const char *where) 139{ 140 int value; 141 142 while (isspace((unsigned char)*where)) { /* Skip spaces */ 143 where++; 144 if (--digs <= 0) 145 return -1; /* All blank field */ 146 } 147 value = 0; 148 while (digs > 0 && isodigit(*where)) { /* Scan til nonoctal */ 149 value = (value << 3) | (*where++ - '0'); 150 --digs; 151 } 152 153 if (digs > 0 && *where && !isspace((unsigned char)*where)) 154 return -1; /* Ended on non-space/nul */ 155 156 return value; 157} 158