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*48c779cdSXin LI FILE_RCSID("@(#)$File: is_tar.c,v 1.44 2019/02/20 02:35:27 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 5658a0f0d0SEitan Adler static const char tartype[][32] = { /* should be equal to messages */ 5758a0f0d0SEitan Adler "tar archive", /* found in ../magic/Magdir/archive */ 58b6cee71dSXin LI "POSIX tar archive", 5958a0f0d0SEitan Adler "POSIX tar archive (GNU)", /* */ 60b6cee71dSXin LI }; 61b6cee71dSXin LI 62b6cee71dSXin LI protected int 6358a0f0d0SEitan Adler file_is_tar(struct magic_set *ms, const struct buffer *b) 64b6cee71dSXin LI { 65*48c779cdSXin LI const unsigned char *buf = CAST(const unsigned char *, b->fbuf); 6658a0f0d0SEitan 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 81*48c779cdSXin LI if (mime == MAGIC_MIME_ENCODING) 82*48c779cdSXin LI return 1; 83*48c779cdSXin LI 84b6cee71dSXin LI if (file_printf(ms, "%s", mime ? "application/x-tar" : 85b6cee71dSXin LI tartype[tar - 1]) == -1) 86b6cee71dSXin LI return -1; 87*48c779cdSXin LI 88b6cee71dSXin LI return 1; 89b6cee71dSXin LI } 90b6cee71dSXin LI 91b6cee71dSXin LI /* 92b6cee71dSXin LI * Return 93b6cee71dSXin LI * 0 if the checksum is bad (i.e., probably not a tar archive), 94b6cee71dSXin LI * 1 for old UNIX tar file, 95b6cee71dSXin LI * 2 for Unix Std (POSIX) tar file, 96b6cee71dSXin LI * 3 for GNU tar file. 97b6cee71dSXin LI */ 98b6cee71dSXin LI private int 99b6cee71dSXin LI is_tar(const unsigned char *buf, size_t nbytes) 100b6cee71dSXin LI { 101*48c779cdSXin LI const union record *header = RCAST(const union record *, 102*48c779cdSXin LI RCAST(const void *, buf)); 10340427ccaSGordon Tetlow size_t i; 104b6cee71dSXin LI int sum, recsum; 10540427ccaSGordon Tetlow const unsigned char *p, *ep; 106b6cee71dSXin LI 10740427ccaSGordon Tetlow if (nbytes < sizeof(*header)) 108b6cee71dSXin LI return 0; 109b6cee71dSXin LI 11040427ccaSGordon Tetlow recsum = from_oct(header->header.chksum, sizeof(header->header.chksum)); 111b6cee71dSXin LI 112b6cee71dSXin LI sum = 0; 113b6cee71dSXin LI p = header->charptr; 11440427ccaSGordon Tetlow ep = header->charptr + sizeof(*header); 11540427ccaSGordon Tetlow while (p < ep) 116b6cee71dSXin LI sum += *p++; 117b6cee71dSXin LI 118b6cee71dSXin LI /* Adjust checksum to count the "chksum" field as blanks. */ 11940427ccaSGordon Tetlow for (i = 0; i < sizeof(header->header.chksum); i++) 120b6cee71dSXin LI sum -= header->header.chksum[i]; 12140427ccaSGordon Tetlow sum += ' ' * sizeof(header->header.chksum); 122b6cee71dSXin LI 123b6cee71dSXin LI if (sum != recsum) 124b6cee71dSXin LI return 0; /* Not a tar archive */ 125b6cee71dSXin LI 12640427ccaSGordon Tetlow if (strncmp(header->header.magic, GNUTMAGIC, 12740427ccaSGordon Tetlow sizeof(header->header.magic)) == 0) 128b6cee71dSXin LI return 3; /* GNU Unix Standard tar archive */ 12940427ccaSGordon Tetlow 13040427ccaSGordon Tetlow if (strncmp(header->header.magic, TMAGIC, 13140427ccaSGordon Tetlow sizeof(header->header.magic)) == 0) 132b6cee71dSXin LI return 2; /* Unix Standard tar archive */ 133b6cee71dSXin LI 134b6cee71dSXin LI return 1; /* Old fashioned tar archive */ 135b6cee71dSXin LI } 136b6cee71dSXin LI 137b6cee71dSXin LI 138b6cee71dSXin LI /* 139b6cee71dSXin LI * Quick and dirty octal conversion. 140b6cee71dSXin LI * 141b6cee71dSXin LI * Result is -1 if the field is invalid (all blank, or non-octal). 142b6cee71dSXin LI */ 143b6cee71dSXin LI private int 14440427ccaSGordon Tetlow from_oct(const char *where, size_t digs) 145b6cee71dSXin LI { 146b6cee71dSXin LI int value; 147b6cee71dSXin LI 14840427ccaSGordon Tetlow if (digs == 0) 14940427ccaSGordon Tetlow return -1; 15040427ccaSGordon Tetlow 151*48c779cdSXin LI while (isspace(CAST(unsigned char, *where))) { /* Skip spaces */ 152b6cee71dSXin LI where++; 15340427ccaSGordon Tetlow if (digs-- == 0) 154b6cee71dSXin LI return -1; /* All blank field */ 155b6cee71dSXin LI } 156b6cee71dSXin LI value = 0; 157b6cee71dSXin LI while (digs > 0 && isodigit(*where)) { /* Scan til non-octal */ 158b6cee71dSXin LI value = (value << 3) | (*where++ - '0'); 15940427ccaSGordon Tetlow digs--; 160b6cee71dSXin LI } 161b6cee71dSXin LI 162*48c779cdSXin LI if (digs > 0 && *where && !isspace(CAST(unsigned char, *where))) 163b6cee71dSXin LI return -1; /* Ended on non-(space/NUL) */ 164b6cee71dSXin LI 165b6cee71dSXin LI return value; 166b6cee71dSXin LI } 167