1 /* $NetBSD: is_tar.c,v 1.1.1.3 2014/06/13 01:48:22 christos Exp $ */ 2 /* 3 * Copyright (c) Ian F. Darwin 1986-1995. 4 * Software written by Ian F. Darwin and others; 5 * maintained 1995-present by Christos Zoulas and others. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice immediately at the beginning of the file, without modification, 12 * this list of conditions, and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 21 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 /* 30 * is_tar() -- figure out whether file is a tar archive. 31 * 32 * Stolen (by the author!) from the public domain tar program: 33 * Public Domain version written 26 Aug 1985 John Gilmore (ihnp4!hoptoad!gnu). 34 * 35 * @(#)list.c 1.18 9/23/86 Public Domain - gnu 36 * 37 * Comments changed and some code/comments reformatted 38 * for file command by Ian Darwin. 39 */ 40 41 #include "file.h" 42 43 #ifndef lint 44 #if 0 45 FILE_RCSID("@(#)$File: is_tar.c,v 1.37 2010/11/30 14:58:53 rrt Exp $") 46 #else 47 __RCSID("$NetBSD: is_tar.c,v 1.1.1.3 2014/06/13 01:48:22 christos Exp $"); 48 #endif 49 #endif 50 51 #include "magic.h" 52 #include <string.h> 53 #include <ctype.h> 54 #include "tar.h" 55 56 #define isodigit(c) ( ((c) >= '0') && ((c) <= '7') ) 57 58 private int is_tar(const unsigned char *, size_t); 59 private int from_oct(int, const char *); /* Decode octal number */ 60 61 static const char tartype[][32] = { 62 "tar archive", 63 "POSIX tar archive", 64 "POSIX tar archive (GNU)", 65 }; 66 67 protected int 68 file_is_tar(struct magic_set *ms, const unsigned char *buf, size_t nbytes) 69 { 70 /* 71 * Do the tar test first, because if the first file in the tar 72 * archive starts with a dot, we can confuse it with an nroff file. 73 */ 74 int tar; 75 int mime = ms->flags & MAGIC_MIME; 76 77 if ((ms->flags & MAGIC_APPLE) != 0) 78 return 0; 79 80 tar = is_tar(buf, nbytes); 81 if (tar < 1 || tar > 3) 82 return 0; 83 84 if (file_printf(ms, "%s", mime ? "application/x-tar" : 85 tartype[tar - 1]) == -1) 86 return -1; 87 return 1; 88 } 89 90 /* 91 * Return 92 * 0 if the checksum is bad (i.e., probably not a tar archive), 93 * 1 for old UNIX tar file, 94 * 2 for Unix Std (POSIX) tar file, 95 * 3 for GNU tar file. 96 */ 97 private int 98 is_tar(const unsigned char *buf, size_t nbytes) 99 { 100 const union record *header = (const union record *)(const void *)buf; 101 int i; 102 int sum, recsum; 103 const unsigned char *p; 104 105 if (nbytes < sizeof(union record)) 106 return 0; 107 108 recsum = from_oct(8, header->header.chksum); 109 110 sum = 0; 111 p = header->charptr; 112 for (i = sizeof(union record); --i >= 0;) 113 sum += *p++; 114 115 /* Adjust checksum to count the "chksum" field as blanks. */ 116 for (i = sizeof(header->header.chksum); --i >= 0;) 117 sum -= 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 non-octal). 136 */ 137 private int 138 from_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 non-octal */ 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