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*58a0f0d0SEitan Adler FILE_RCSID("@(#)$File: is_tar.c,v 1.41 2017/11/02 20:25:39 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); 5440427ccaSGordon Tetlow private int from_oct(const char *, size_t); /* Decode octal number */ 55b6cee71dSXin LI 56*58a0f0d0SEitan Adler static const char tartype[][32] = { /* should be equal to messages */ 57*58a0f0d0SEitan Adler "tar archive", /* found in ../magic/Magdir/archive */ 58b6cee71dSXin LI "POSIX tar archive", 59*58a0f0d0SEitan Adler "POSIX tar archive (GNU)", /* */ 60b6cee71dSXin LI }; 61b6cee71dSXin LI 62b6cee71dSXin LI protected int 63*58a0f0d0SEitan Adler file_is_tar(struct magic_set *ms, const struct buffer *b) 64b6cee71dSXin LI { 65*58a0f0d0SEitan Adler const unsigned char *buf = b->fbuf; 66*58a0f0d0SEitan Adler size_t nbytes = b->flen; 67b6cee71dSXin LI /* 68b6cee71dSXin LI * Do the tar test first, because if the first file in the tar 69b6cee71dSXin LI * archive starts with a dot, we can confuse it with an nroff file. 70b6cee71dSXin LI */ 71b6cee71dSXin LI int tar; 72b6cee71dSXin LI int mime = ms->flags & MAGIC_MIME; 73b6cee71dSXin LI 745f0216bdSXin LI if ((ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION)) != 0) 75b6cee71dSXin LI return 0; 76b6cee71dSXin LI 77b6cee71dSXin LI tar = is_tar(buf, nbytes); 78b6cee71dSXin LI if (tar < 1 || tar > 3) 79b6cee71dSXin LI return 0; 80b6cee71dSXin LI 81b6cee71dSXin LI if (file_printf(ms, "%s", mime ? "application/x-tar" : 82b6cee71dSXin LI tartype[tar - 1]) == -1) 83b6cee71dSXin LI return -1; 84b6cee71dSXin LI return 1; 85b6cee71dSXin LI } 86b6cee71dSXin LI 87b6cee71dSXin LI /* 88b6cee71dSXin LI * Return 89b6cee71dSXin LI * 0 if the checksum is bad (i.e., probably not a tar archive), 90b6cee71dSXin LI * 1 for old UNIX tar file, 91b6cee71dSXin LI * 2 for Unix Std (POSIX) tar file, 92b6cee71dSXin LI * 3 for GNU tar file. 93b6cee71dSXin LI */ 94b6cee71dSXin LI private int 95b6cee71dSXin LI is_tar(const unsigned char *buf, size_t nbytes) 96b6cee71dSXin LI { 97b6cee71dSXin LI const union record *header = (const union record *)(const void *)buf; 9840427ccaSGordon Tetlow size_t i; 99b6cee71dSXin LI int sum, recsum; 10040427ccaSGordon Tetlow const unsigned char *p, *ep; 101b6cee71dSXin LI 10240427ccaSGordon Tetlow if (nbytes < sizeof(*header)) 103b6cee71dSXin LI return 0; 104b6cee71dSXin LI 10540427ccaSGordon Tetlow recsum = from_oct(header->header.chksum, sizeof(header->header.chksum)); 106b6cee71dSXin LI 107b6cee71dSXin LI sum = 0; 108b6cee71dSXin LI p = header->charptr; 10940427ccaSGordon Tetlow ep = header->charptr + sizeof(*header); 11040427ccaSGordon Tetlow while (p < ep) 111b6cee71dSXin LI sum += *p++; 112b6cee71dSXin LI 113b6cee71dSXin LI /* Adjust checksum to count the "chksum" field as blanks. */ 11440427ccaSGordon Tetlow for (i = 0; i < sizeof(header->header.chksum); i++) 115b6cee71dSXin LI sum -= header->header.chksum[i]; 11640427ccaSGordon Tetlow sum += ' ' * sizeof(header->header.chksum); 117b6cee71dSXin LI 118b6cee71dSXin LI if (sum != recsum) 119b6cee71dSXin LI return 0; /* Not a tar archive */ 120b6cee71dSXin LI 12140427ccaSGordon Tetlow if (strncmp(header->header.magic, GNUTMAGIC, 12240427ccaSGordon Tetlow sizeof(header->header.magic)) == 0) 123b6cee71dSXin LI return 3; /* GNU Unix Standard tar archive */ 12440427ccaSGordon Tetlow 12540427ccaSGordon Tetlow if (strncmp(header->header.magic, TMAGIC, 12640427ccaSGordon Tetlow sizeof(header->header.magic)) == 0) 127b6cee71dSXin LI return 2; /* Unix Standard tar archive */ 128b6cee71dSXin LI 129b6cee71dSXin LI return 1; /* Old fashioned tar archive */ 130b6cee71dSXin LI } 131b6cee71dSXin LI 132b6cee71dSXin LI 133b6cee71dSXin LI /* 134b6cee71dSXin LI * Quick and dirty octal conversion. 135b6cee71dSXin LI * 136b6cee71dSXin LI * Result is -1 if the field is invalid (all blank, or non-octal). 137b6cee71dSXin LI */ 138b6cee71dSXin LI private int 13940427ccaSGordon Tetlow from_oct(const char *where, size_t digs) 140b6cee71dSXin LI { 141b6cee71dSXin LI int value; 142b6cee71dSXin LI 14340427ccaSGordon Tetlow if (digs == 0) 14440427ccaSGordon Tetlow return -1; 14540427ccaSGordon Tetlow 146b6cee71dSXin LI while (isspace((unsigned char)*where)) { /* Skip spaces */ 147b6cee71dSXin LI where++; 14840427ccaSGordon Tetlow if (digs-- == 0) 149b6cee71dSXin LI return -1; /* All blank field */ 150b6cee71dSXin LI } 151b6cee71dSXin LI value = 0; 152b6cee71dSXin LI while (digs > 0 && isodigit(*where)) { /* Scan til non-octal */ 153b6cee71dSXin LI value = (value << 3) | (*where++ - '0'); 15440427ccaSGordon Tetlow digs--; 155b6cee71dSXin LI } 156b6cee71dSXin LI 157b6cee71dSXin LI if (digs > 0 && *where && !isspace((unsigned char)*where)) 158b6cee71dSXin LI return -1; /* Ended on non-(space/NUL) */ 159b6cee71dSXin LI 160b6cee71dSXin LI return value; 161b6cee71dSXin LI } 162