2 * Copyright (c) Ian F. Darwin 1986-1995.
3 * Software written by Ian F. Darwin and others;
4 * maintained 1995-present by Christos Zoulas and others.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice immediately at the beginning of the file, without modification,
11 * this list of conditions, and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * is_tar() -- figure out whether file is a tar archive.
31 * Stolen (by the author!) from the public domain tar program:
32 * Public Domain version written 26 Aug 1985 John Gilmore (ihnp4!hoptoad!gnu).
34 * @(#)list.c 1.18 9/23/86 Public Domain - gnu
36 * Comments changed and some code/comments reformatted
37 * for file command by Ian Darwin.
43 FILE_RCSID("@(#)$File: is_tar.c,v 1.44 2019/02/20 02:35:27 christos Exp $")
51 #define isodigit(c) ( ((c) >= '0') && ((c) <= '7') )
53 private int is_tar(const unsigned char *, size_t);
54 private int from_oct(const char *, size_t); /* Decode octal number */
56 static const char tartype[][32] = { /* should be equal to messages */
57 "tar archive", /* found in ../magic/Magdir/archive */
59 "POSIX tar archive (GNU)", /* */
63 file_is_tar(struct magic_set *ms, const struct buffer *b)
65 const unsigned char *buf = CAST(const unsigned char *, b->fbuf);
66 size_t nbytes = b->flen;
68 * Do the tar test first, because if the first file in the tar
69 * archive starts with a dot, we can confuse it with an nroff file.
72 int mime = ms->flags & MAGIC_MIME;
74 if ((ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION)) != 0)
77 tar = is_tar(buf, nbytes);
78 if (tar < 1 || tar > 3)
81 if (mime == MAGIC_MIME_ENCODING)
84 if (file_printf(ms, "%s", mime ? "application/x-tar" :
85 tartype[tar - 1]) == -1)
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,
99 is_tar(const unsigned char *buf, size_t nbytes)
101 const union record *header = RCAST(const union record *,
102 RCAST(const void *, buf));
105 const unsigned char *p, *ep;
107 if (nbytes < sizeof(*header))
110 recsum = from_oct(header->header.chksum, sizeof(header->header.chksum));
114 ep = header->charptr + sizeof(*header);
118 /* Adjust checksum to count the "chksum" field as blanks. */
119 for (i = 0; i < sizeof(header->header.chksum); i++)
120 sum -= header->header.chksum[i];
121 sum += ' ' * sizeof(header->header.chksum);
124 return 0; /* Not a tar archive */
126 if (strncmp(header->header.magic, GNUTMAGIC,
127 sizeof(header->header.magic)) == 0)
128 return 3; /* GNU Unix Standard tar archive */
130 if (strncmp(header->header.magic, TMAGIC,
131 sizeof(header->header.magic)) == 0)
132 return 2; /* Unix Standard tar archive */
134 return 1; /* Old fashioned tar archive */
139 * Quick and dirty octal conversion.
141 * Result is -1 if the field is invalid (all blank, or non-octal).
144 from_oct(const char *where, size_t digs)
151 while (isspace(CAST(unsigned char, *where))) { /* Skip spaces */
154 return -1; /* All blank field */
157 while (digs > 0 && isodigit(*where)) { /* Scan til non-octal */
158 value = (value << 3) | (*where++ - '0');
162 if (digs > 0 && *where && !isspace(CAST(unsigned char, *where)))
163 return -1; /* Ended on non-(space/NUL) */