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