Update to file 4.13. Put the contrib files into contrib/file-4 instead
[dragonfly.git] / contrib / file-4 / src / is_tar.c
blobb6ad2e3c1bc2208f17ee9292e3dce68f1f112882
1 /*
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.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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
26 * SUCH DAMAGE.
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.
40 #include "file.h"
41 #include "magic.h"
42 #include <string.h>
43 #include <ctype.h>
44 #include <sys/types.h>
45 #include "tar.h"
47 #ifndef lint
48 FILE_RCSID("@(#)$Id: is_tar.c,v 1.25 2004/09/11 19:15:57 christos Exp $")
49 #endif
51 #define isodigit(c) ( ((c) >= '0') && ((c) <= '7') )
53 private int is_tar(const unsigned char *, size_t);
54 private int from_oct(int, const char *); /* Decode octal number */
56 protected int
57 file_is_tar(struct magic_set *ms, const unsigned char *buf, size_t nbytes)
60 * Do the tar test first, because if the first file in the tar
61 * archive starts with a dot, we can confuse it with an nroff file.
63 switch (is_tar(buf, nbytes)) {
64 case 1:
65 if (file_printf(ms, (ms->flags & MAGIC_MIME) ?
66 "application/x-tar" : "tar archive") == -1)
67 return -1;
68 return 1;
69 case 2:
70 if (file_printf(ms, (ms->flags & MAGIC_MIME) ?
71 "application/x-tar, POSIX" : "POSIX tar archive") == -1)
72 return -1;
73 return 1;
74 default:
75 return 0;
80 * Return
81 * 0 if the checksum is bad (i.e., probably not a tar archive),
82 * 1 for old UNIX tar file,
83 * 2 for Unix Std (POSIX) tar file.
85 private int
86 is_tar(const unsigned char *buf, size_t nbytes)
88 const union record *header = (const union record *)(const void *)buf;
89 int i;
90 int sum, recsum;
91 const char *p;
93 if (nbytes < sizeof(union record))
94 return 0;
96 recsum = from_oct(8, header->header.chksum);
98 sum = 0;
99 p = header->charptr;
100 for (i = sizeof(union record); --i >= 0;) {
102 * We cannot use unsigned char here because of old compilers,
103 * e.g. V7.
105 sum += 0xFF & *p++;
108 /* Adjust checksum to count the "chksum" field as blanks. */
109 for (i = sizeof(header->header.chksum); --i >= 0;)
110 sum -= 0xFF & header->header.chksum[i];
111 sum += ' '* sizeof header->header.chksum;
113 if (sum != recsum)
114 return 0; /* Not a tar archive */
116 if (0==strcmp(header->header.magic, TMAGIC))
117 return 2; /* Unix Standard tar archive */
119 return 1; /* Old fashioned tar archive */
124 * Quick and dirty octal conversion.
126 * Result is -1 if the field is invalid (all blank, or nonoctal).
128 private int
129 from_oct(int digs, const char *where)
131 int value;
133 while (isspace((unsigned char)*where)) { /* Skip spaces */
134 where++;
135 if (--digs <= 0)
136 return -1; /* All blank field */
138 value = 0;
139 while (digs > 0 && isodigit(*where)) { /* Scan til nonoctal */
140 value = (value << 3) | (*where++ - '0');
141 --digs;
144 if (digs > 0 && *where && !isspace((unsigned char)*where))
145 return -1; /* Ended on non-space/nul */
147 return value;