/*1* Copyright (c) Ian F. Darwin 1986-1995.2* Software written by Ian F. Darwin and others;3* maintained 1995-present by Christos Zoulas and others.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8* 1. Redistributions of source code must retain the above copyright9* notice immediately at the beginning of the file, without modification,10* this list of conditions, and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND16* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR19* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25* SUCH DAMAGE.26*/27/*28* is_tar() -- figure out whether file is a tar archive.29*30* Stolen (by the author!) from the file_public domain tar program:31* Public Domain version written 26 Aug 1985 John Gilmore (ihnp4!hoptoad!gnu).32*33* @(#)list.c 1.18 9/23/86 Public Domain - gnu34*35* Comments changed and some code/comments reformatted36* for file command by Ian Darwin.37*/3839#include "file.h"4041#ifndef lint42FILE_RCSID("@(#)$File: is_tar.c,v 1.50 2022/12/26 17:31:14 christos Exp $")43#endif4445#include "magic.h"46#include <string.h>47#include <ctype.h>48#include "tar.h"4950#define isodigit(c) ( ((c) >= '0') && ((c) <= '7') )5152file_private int is_tar(const unsigned char *, size_t);53file_private int from_oct(const char *, size_t); /* Decode octal number */5455static const char tartype[][32] = { /* should be equal to messages */56"tar archive", /* found in ../magic/Magdir/archive */57"POSIX tar archive",58"POSIX tar archive (GNU)", /* */59};6061file_protected int62file_is_tar(struct magic_set *ms, const struct buffer *b)63{64const unsigned char *buf = CAST(const unsigned char *, b->fbuf);65size_t nbytes = b->flen;66/*67* Do the tar test first, because if the first file in the tar68* archive starts with a dot, we can confuse it with an nroff file.69*/70int tar;71int mime = ms->flags & MAGIC_MIME;7273if ((ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION)) != 0)74return 0;7576tar = is_tar(buf, nbytes);77if (tar < 1 || tar > 3)78return 0;7980if (mime == MAGIC_MIME_ENCODING)81return 1;8283if (file_printf(ms, "%s", mime ? "application/x-tar" :84tartype[tar - 1]) == -1)85return -1;8687return 1;88}8990/*91* Return92* 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*/97file_private int98is_tar(const unsigned char *buf, size_t nbytes)99{100static const char gpkg_match[] = "/gpkg-1";101102const union record *header = RCAST(const union record *,103RCAST(const void *, buf));104size_t i;105int sum, recsum;106const unsigned char *p, *ep;107const char *nulp;108109if (nbytes < sizeof(*header))110return 0;111112/* If the file looks like Gentoo GLEP 78 binary package (GPKG),113* don't waste time on further checks and fall back to magic rules.114*/115nulp = CAST(const char *,116memchr(header->header.name, 0, sizeof(header->header.name)));117if (nulp != NULL && nulp >= header->header.name + sizeof(gpkg_match) &&118memcmp(nulp - sizeof(gpkg_match) + 1, gpkg_match,119sizeof(gpkg_match)) == 0)120return 0;121122recsum = from_oct(header->header.chksum, sizeof(header->header.chksum));123124sum = 0;125p = header->charptr;126ep = header->charptr + sizeof(*header);127while (p < ep)128sum += *p++;129130/* Adjust checksum to count the "chksum" field as blanks. */131for (i = 0; i < sizeof(header->header.chksum); i++)132sum -= header->header.chksum[i];133sum += ' ' * sizeof(header->header.chksum);134135if (sum != recsum)136return 0; /* Not a tar archive */137138if (strncmp(header->header.magic, GNUTMAGIC,139sizeof(header->header.magic)) == 0)140return 3; /* GNU Unix Standard tar archive */141142if (strncmp(header->header.magic, TMAGIC,143sizeof(header->header.magic)) == 0)144return 2; /* Unix Standard tar archive */145146return 1; /* Old fashioned tar archive */147}148149150/*151* Quick and dirty octal conversion.152*153* Result is -1 if the field is invalid (all blank, or non-octal).154*/155file_private int156from_oct(const char *where, size_t digs)157{158int value;159160if (digs == 0)161return -1;162163while (isspace(CAST(unsigned char, *where))) { /* Skip spaces */164where++;165if (digs-- == 0)166return -1; /* All blank field */167}168value = 0;169while (digs > 0 && isodigit(*where)) { /* Scan til non-octal */170value = (value << 3) | (*where++ - '0');171digs--;172}173174if (digs > 0 && *where && !isspace(CAST(unsigned char, *where)))175return -1; /* Ended on non-(space/NUL) */176177return value;178}179180181