xref: /freebsd/contrib/file/src/is_tar.c (revision 898496ee09ed2b7d25f6807edc4515628196ec0a)
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  *
31*898496eeSXin LI  * Stolen (by the author!) from the file_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*898496eeSXin LI FILE_RCSID("@(#)$File: is_tar.c,v 1.50 2022/12/26 17:31:14 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 
53*898496eeSXin LI file_private int is_tar(const unsigned char *, size_t);
54*898496eeSXin LI file_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 
62*898496eeSXin LI file_protected int
file_is_tar(struct magic_set * ms,const struct buffer * b)6358a0f0d0SEitan Adler file_is_tar(struct magic_set *ms, const struct buffer *b)
64b6cee71dSXin LI {
6548c779cdSXin 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 
8148c779cdSXin LI 	if (mime == MAGIC_MIME_ENCODING)
8248c779cdSXin LI 		return 1;
8348c779cdSXin LI 
84b6cee71dSXin LI 	if (file_printf(ms, "%s", mime ? "application/x-tar" :
85b6cee71dSXin LI 	    tartype[tar - 1]) == -1)
86b6cee71dSXin LI 		return -1;
8748c779cdSXin 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  */
98*898496eeSXin LI file_private int
is_tar(const unsigned char * buf,size_t nbytes)99b6cee71dSXin LI is_tar(const unsigned char *buf, size_t nbytes)
100b6cee71dSXin LI {
101a2dfb722SXin LI 	static const char gpkg_match[] = "/gpkg-1";
102a2dfb722SXin LI 
10348c779cdSXin LI 	const union record *header = RCAST(const union record *,
10448c779cdSXin LI 	    RCAST(const void *, buf));
10540427ccaSGordon Tetlow 	size_t i;
106b6cee71dSXin LI 	int sum, recsum;
10740427ccaSGordon Tetlow 	const unsigned char *p, *ep;
108a2dfb722SXin LI 	const char *nulp;
109b6cee71dSXin LI 
11040427ccaSGordon Tetlow 	if (nbytes < sizeof(*header))
111b6cee71dSXin LI 		return 0;
112b6cee71dSXin LI 
113a2dfb722SXin LI 	/* If the file looks like Gentoo GLEP 78 binary package (GPKG),
114a2dfb722SXin LI 	 * don't waste time on further checks and fall back to magic rules.
115a2dfb722SXin LI 	 */
116a2dfb722SXin LI 	nulp = CAST(const char *,
117a2dfb722SXin LI 	    memchr(header->header.name, 0, sizeof(header->header.name)));
118a2dfb722SXin LI 	if (nulp != NULL && nulp >= header->header.name + sizeof(gpkg_match) &&
119a2dfb722SXin LI 	    memcmp(nulp - sizeof(gpkg_match) + 1, gpkg_match,
120a2dfb722SXin LI 	    sizeof(gpkg_match)) == 0)
121a2dfb722SXin LI 	    return 0;
122a2dfb722SXin LI 
12340427ccaSGordon Tetlow 	recsum = from_oct(header->header.chksum, sizeof(header->header.chksum));
124b6cee71dSXin LI 
125b6cee71dSXin LI 	sum = 0;
126b6cee71dSXin LI 	p = header->charptr;
12740427ccaSGordon Tetlow 	ep = header->charptr + sizeof(*header);
12840427ccaSGordon Tetlow 	while (p < ep)
129b6cee71dSXin LI 		sum += *p++;
130b6cee71dSXin LI 
131b6cee71dSXin LI 	/* Adjust checksum to count the "chksum" field as blanks. */
13240427ccaSGordon Tetlow 	for (i = 0; i < sizeof(header->header.chksum); i++)
133b6cee71dSXin LI 		sum -= header->header.chksum[i];
13440427ccaSGordon Tetlow 	sum += ' ' * sizeof(header->header.chksum);
135b6cee71dSXin LI 
136b6cee71dSXin LI 	if (sum != recsum)
137b6cee71dSXin LI 		return 0;	/* Not a tar archive */
138b6cee71dSXin LI 
13940427ccaSGordon Tetlow 	if (strncmp(header->header.magic, GNUTMAGIC,
14040427ccaSGordon Tetlow 	    sizeof(header->header.magic)) == 0)
141b6cee71dSXin LI 		return 3;		/* GNU Unix Standard tar archive */
14240427ccaSGordon Tetlow 
14340427ccaSGordon Tetlow 	if (strncmp(header->header.magic, TMAGIC,
14440427ccaSGordon Tetlow 	    sizeof(header->header.magic)) == 0)
145b6cee71dSXin LI 		return 2;		/* Unix Standard tar archive */
146b6cee71dSXin LI 
147b6cee71dSXin LI 	return 1;			/* Old fashioned tar archive */
148b6cee71dSXin LI }
149b6cee71dSXin LI 
150b6cee71dSXin LI 
151b6cee71dSXin LI /*
152b6cee71dSXin LI  * Quick and dirty octal conversion.
153b6cee71dSXin LI  *
154b6cee71dSXin LI  * Result is -1 if the field is invalid (all blank, or non-octal).
155b6cee71dSXin LI  */
156*898496eeSXin LI file_private int
from_oct(const char * where,size_t digs)15740427ccaSGordon Tetlow from_oct(const char *where, size_t digs)
158b6cee71dSXin LI {
159b6cee71dSXin LI 	int	value;
160b6cee71dSXin LI 
16140427ccaSGordon Tetlow 	if (digs == 0)
16240427ccaSGordon Tetlow 		return -1;
16340427ccaSGordon Tetlow 
16448c779cdSXin LI 	while (isspace(CAST(unsigned char, *where))) {	/* Skip spaces */
165b6cee71dSXin LI 		where++;
16640427ccaSGordon Tetlow 		if (digs-- == 0)
167b6cee71dSXin LI 			return -1;		/* All blank field */
168b6cee71dSXin LI 	}
169b6cee71dSXin LI 	value = 0;
170b6cee71dSXin LI 	while (digs > 0 && isodigit(*where)) {	/* Scan til non-octal */
171b6cee71dSXin LI 		value = (value << 3) | (*where++ - '0');
17240427ccaSGordon Tetlow 		digs--;
173b6cee71dSXin LI 	}
174b6cee71dSXin LI 
17548c779cdSXin LI 	if (digs > 0 && *where && !isspace(CAST(unsigned char, *where)))
176b6cee71dSXin LI 		return -1;			/* Ended on non-(space/NUL) */
177b6cee71dSXin LI 
178b6cee71dSXin LI 	return value;
179b6cee71dSXin LI }
180