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