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