1327e51cbSPeter Avalos /*
2327e51cbSPeter Avalos * Copyright (c) Ian F. Darwin 1986-1995.
3327e51cbSPeter Avalos * Software written by Ian F. Darwin and others;
4327e51cbSPeter Avalos * maintained 1995-present by Christos Zoulas and others.
5327e51cbSPeter Avalos *
6327e51cbSPeter Avalos * Redistribution and use in source and binary forms, with or without
7327e51cbSPeter Avalos * modification, are permitted provided that the following conditions
8327e51cbSPeter Avalos * are met:
9327e51cbSPeter Avalos * 1. Redistributions of source code must retain the above copyright
10327e51cbSPeter Avalos * notice immediately at the beginning of the file, without modification,
11327e51cbSPeter Avalos * this list of conditions, and the following disclaimer.
12327e51cbSPeter Avalos * 2. Redistributions in binary form must reproduce the above copyright
13327e51cbSPeter Avalos * notice, this list of conditions and the following disclaimer in the
14327e51cbSPeter Avalos * documentation and/or other materials provided with the distribution.
15327e51cbSPeter Avalos *
16327e51cbSPeter Avalos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17327e51cbSPeter Avalos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18327e51cbSPeter Avalos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19327e51cbSPeter Avalos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20327e51cbSPeter Avalos * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21327e51cbSPeter Avalos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22327e51cbSPeter Avalos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23327e51cbSPeter Avalos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24327e51cbSPeter Avalos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25327e51cbSPeter Avalos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26327e51cbSPeter Avalos * SUCH DAMAGE.
27327e51cbSPeter Avalos */
28327e51cbSPeter Avalos /*
29327e51cbSPeter Avalos * is_tar() -- figure out whether file is a tar archive.
30327e51cbSPeter Avalos *
31327e51cbSPeter Avalos * Stolen (by the author!) from the public domain tar program:
32327e51cbSPeter Avalos * Public Domain version written 26 Aug 1985 John Gilmore (ihnp4!hoptoad!gnu).
33327e51cbSPeter Avalos *
34327e51cbSPeter Avalos * @(#)list.c 1.18 9/23/86 Public Domain - gnu
35327e51cbSPeter Avalos *
36327e51cbSPeter Avalos * Comments changed and some code/comments reformatted
37327e51cbSPeter Avalos * for file command by Ian Darwin.
38327e51cbSPeter Avalos */
39327e51cbSPeter Avalos
40327e51cbSPeter Avalos #include "file.h"
4179343712SPeter Avalos
4279343712SPeter Avalos #ifndef lint
43*3b9cdfa3SAntonio Huete Jimenez FILE_RCSID("@(#)$File: is_tar.c,v 1.47 2022/09/13 18:46:07 christos Exp $")
4479343712SPeter Avalos #endif
4579343712SPeter Avalos
46327e51cbSPeter Avalos #include "magic.h"
47327e51cbSPeter Avalos #include <string.h>
48327e51cbSPeter Avalos #include <ctype.h>
49327e51cbSPeter Avalos #include "tar.h"
50327e51cbSPeter Avalos
51327e51cbSPeter Avalos #define isodigit(c) ( ((c) >= '0') && ((c) <= '7') )
52327e51cbSPeter Avalos
53327e51cbSPeter Avalos private int is_tar(const unsigned char *, size_t);
54c30bd091SSascha Wildner private int from_oct(const char *, size_t); /* Decode octal number */
55327e51cbSPeter Avalos
566fca56fbSSascha Wildner static const char tartype[][32] = { /* should be equal to messages */
576fca56fbSSascha Wildner "tar archive", /* found in ../magic/Magdir/archive */
58327e51cbSPeter Avalos "POSIX tar archive",
596fca56fbSSascha Wildner "POSIX tar archive (GNU)", /* */
60327e51cbSPeter Avalos };
61327e51cbSPeter Avalos
62327e51cbSPeter Avalos protected int
file_is_tar(struct magic_set * ms,const struct buffer * b)636fca56fbSSascha Wildner file_is_tar(struct magic_set *ms, const struct buffer *b)
64327e51cbSPeter Avalos {
656fca56fbSSascha Wildner const unsigned char *buf = CAST(const unsigned char *, b->fbuf);
666fca56fbSSascha Wildner size_t nbytes = b->flen;
67327e51cbSPeter Avalos /*
68327e51cbSPeter Avalos * Do the tar test first, because if the first file in the tar
69327e51cbSPeter Avalos * archive starts with a dot, we can confuse it with an nroff file.
70327e51cbSPeter Avalos */
7179343712SPeter Avalos int tar;
72327e51cbSPeter Avalos int mime = ms->flags & MAGIC_MIME;
73327e51cbSPeter Avalos
74c30bd091SSascha Wildner if ((ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION)) != 0)
7579343712SPeter Avalos return 0;
7679343712SPeter Avalos
7779343712SPeter Avalos tar = is_tar(buf, nbytes);
78327e51cbSPeter Avalos if (tar < 1 || tar > 3)
79327e51cbSPeter Avalos return 0;
80327e51cbSPeter Avalos
816fca56fbSSascha Wildner if (mime == MAGIC_MIME_ENCODING)
826fca56fbSSascha Wildner return 1;
836fca56fbSSascha Wildner
8479343712SPeter Avalos if (file_printf(ms, "%s", mime ? "application/x-tar" :
85327e51cbSPeter Avalos tartype[tar - 1]) == -1)
86327e51cbSPeter Avalos return -1;
876fca56fbSSascha Wildner
88327e51cbSPeter Avalos return 1;
89327e51cbSPeter Avalos }
90327e51cbSPeter Avalos
91327e51cbSPeter Avalos /*
92327e51cbSPeter Avalos * Return
93327e51cbSPeter Avalos * 0 if the checksum is bad (i.e., probably not a tar archive),
94327e51cbSPeter Avalos * 1 for old UNIX tar file,
9579343712SPeter Avalos * 2 for Unix Std (POSIX) tar file,
9679343712SPeter Avalos * 3 for GNU tar file.
97327e51cbSPeter Avalos */
98327e51cbSPeter Avalos private int
is_tar(const unsigned char * buf,size_t nbytes)99327e51cbSPeter Avalos is_tar(const unsigned char *buf, size_t nbytes)
100327e51cbSPeter Avalos {
101*3b9cdfa3SAntonio Huete Jimenez static const char gpkg_match[] = "/gpkg-1";
102*3b9cdfa3SAntonio Huete Jimenez
1036fca56fbSSascha Wildner const union record *header = RCAST(const union record *,
1046fca56fbSSascha Wildner RCAST(const void *, buf));
105c30bd091SSascha Wildner size_t i;
106327e51cbSPeter Avalos int sum, recsum;
107c30bd091SSascha Wildner const unsigned char *p, *ep;
108*3b9cdfa3SAntonio Huete Jimenez const char *nulp;
109327e51cbSPeter Avalos
110c30bd091SSascha Wildner if (nbytes < sizeof(*header))
111327e51cbSPeter Avalos return 0;
112327e51cbSPeter Avalos
113*3b9cdfa3SAntonio Huete Jimenez /* If the file looks like Gentoo GLEP 78 binary package (GPKG),
114*3b9cdfa3SAntonio Huete Jimenez * don't waste time on further checks and fall back to magic rules.
115*3b9cdfa3SAntonio Huete Jimenez */
116*3b9cdfa3SAntonio Huete Jimenez nulp = CAST(const char *,
117*3b9cdfa3SAntonio Huete Jimenez memchr(header->header.name, 0, sizeof(header->header.name)));
118*3b9cdfa3SAntonio Huete Jimenez if (nulp != NULL && nulp >= header->header.name + sizeof(gpkg_match) &&
119*3b9cdfa3SAntonio Huete Jimenez memcmp(nulp - sizeof(gpkg_match) + 1, gpkg_match,
120*3b9cdfa3SAntonio Huete Jimenez sizeof(gpkg_match)) == 0)
121*3b9cdfa3SAntonio Huete Jimenez return 0;
122*3b9cdfa3SAntonio Huete Jimenez
123c30bd091SSascha Wildner recsum = from_oct(header->header.chksum, sizeof(header->header.chksum));
124327e51cbSPeter Avalos
125327e51cbSPeter Avalos sum = 0;
126327e51cbSPeter Avalos p = header->charptr;
127c30bd091SSascha Wildner ep = header->charptr + sizeof(*header);
128c30bd091SSascha Wildner while (p < ep)
129e4d4ce0cSPeter Avalos sum += *p++;
130327e51cbSPeter Avalos
131327e51cbSPeter Avalos /* Adjust checksum to count the "chksum" field as blanks. */
132c30bd091SSascha Wildner for (i = 0; i < sizeof(header->header.chksum); i++)
133e4d4ce0cSPeter Avalos sum -= header->header.chksum[i];
134c30bd091SSascha Wildner sum += ' ' * sizeof(header->header.chksum);
135327e51cbSPeter Avalos
136327e51cbSPeter Avalos if (sum != recsum)
137327e51cbSPeter Avalos return 0; /* Not a tar archive */
138327e51cbSPeter Avalos
139c30bd091SSascha Wildner if (strncmp(header->header.magic, GNUTMAGIC,
140c30bd091SSascha Wildner sizeof(header->header.magic)) == 0)
141327e51cbSPeter Avalos return 3; /* GNU Unix Standard tar archive */
142c30bd091SSascha Wildner
143c30bd091SSascha Wildner if (strncmp(header->header.magic, TMAGIC,
144c30bd091SSascha Wildner sizeof(header->header.magic)) == 0)
145327e51cbSPeter Avalos return 2; /* Unix Standard tar archive */
146327e51cbSPeter Avalos
147327e51cbSPeter Avalos return 1; /* Old fashioned tar archive */
148327e51cbSPeter Avalos }
149327e51cbSPeter Avalos
150327e51cbSPeter Avalos
151327e51cbSPeter Avalos /*
152327e51cbSPeter Avalos * Quick and dirty octal conversion.
153327e51cbSPeter Avalos *
154e4d4ce0cSPeter Avalos * Result is -1 if the field is invalid (all blank, or non-octal).
155327e51cbSPeter Avalos */
156327e51cbSPeter Avalos private int
from_oct(const char * where,size_t digs)157c30bd091SSascha Wildner from_oct(const char *where, size_t digs)
158327e51cbSPeter Avalos {
159327e51cbSPeter Avalos int value;
160327e51cbSPeter Avalos
161c30bd091SSascha Wildner if (digs == 0)
162c30bd091SSascha Wildner return -1;
163c30bd091SSascha Wildner
1646fca56fbSSascha Wildner while (isspace(CAST(unsigned char, *where))) { /* Skip spaces */
165327e51cbSPeter Avalos where++;
166c30bd091SSascha Wildner if (digs-- == 0)
167327e51cbSPeter Avalos return -1; /* All blank field */
168327e51cbSPeter Avalos }
169327e51cbSPeter Avalos value = 0;
170e4d4ce0cSPeter Avalos while (digs > 0 && isodigit(*where)) { /* Scan til non-octal */
171327e51cbSPeter Avalos value = (value << 3) | (*where++ - '0');
172c30bd091SSascha Wildner digs--;
173327e51cbSPeter Avalos }
174327e51cbSPeter Avalos
1756fca56fbSSascha Wildner if (digs > 0 && *where && !isspace(CAST(unsigned char, *where)))
176e4d4ce0cSPeter Avalos return -1; /* Ended on non-(space/NUL) */
177327e51cbSPeter Avalos
178327e51cbSPeter Avalos return value;
179327e51cbSPeter Avalos }
180