Debian Codegraph @

archives.c

raw symbols SCIP
1 /*
2 * archives.c
3 * Detection of (Unix) archive formats
4 *
5 * Copyright (c) 2003 Christoph Pfisterer
6 *
7 * Permission is hereby granted, free of charge, to any person
8 * obtaining a copy of this software and associated documentation
9 * files (the "Software"), to deal in the Software without
10 * restriction, including without limitation the rights to use, copy,
11 * modify, merge, publish, distribute, sublicense, and/or sell copies
12 * of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
22 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
23 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
26 */
27
28 #include "global.h"
29
30 /*
31 * detection of various archive headers
32 *
33 * Most of this is based on the tapetype program by Doug Merritt,
34 * posted to the net in 1992. Some parts are augmented with stuff from
35 * /etc/magic.
36 */
37
38 void detect_archive(SECTION *section, int level)
39 {
40 int fill, i, stored_sum, sum, en;
41 u4 magic;
42 unsigned char *buf;
43
44 fill = get_buffer(section, 0, 512, (void **)&buf);
45 if (fill < 512)
46 return;
47
48 /* tar archives */
49 sum = 0;
50 for (i = 0; i < 148; i++)
51 sum += (char)buf[i];
52 for (; i < 156; i++)
53 sum += ' ';
54 for (; i < 512; i++)
55 sum += (char)buf[i];
56 stored_sum = 0;
57 for (i = 148; i < 156; i++) {
58 if (buf[i] == 0)
59 break;
60 else if (buf[i] >= '0' && buf[i] <= '7')
61 stored_sum = (stored_sum * 8) + (buf[i] - '0');
62 else if (buf[i] != ' ') {
63 stored_sum = -1; /* make it mismatch, since this is an error */
64 break;
65 }
66 }
67 if (sum == stored_sum) {
68 if (memcmp((char *)buf + 257, "ustar \0", 8) == 0) {
69 print_line(level, "GNU tar archive");
70 } else if (memcmp((char *)buf + 257, "ustar\0", 6) == 0) {
71 print_line(level, "POSIX tar archive");
72 } else {
73 print_line(level, "Pre-POSIX tar archive");
74 }
75 }
76
77 /* cpio */
78 if (get_le_short(buf) == 070707) {
79 print_line(level, "cpio archive, little-endian binary");
80 } else if (get_be_short(buf) == 070707) {
81 print_line(level, "cpio archive, big-endian binary");
82 } else if (memcmp(buf, "07070", 5) == 0) {
83 print_line(level, "cpio archive, ascii");
84 }
85
86 /* bar */
87 if (memcmp(buf + 65, "\x56\0", 2) == 0) {
88 print_line(level, "bar archive");
89 }
90
91 /* dump */
92 for (en = 0; en < 2; en++) {
93 magic = get_ve_long(en, buf + 24);
94
95 if (magic == 60011) {
96 print_line(level, "dump: 4.1BSD (or older) or Sun OFS, %s", get_ve_name(en));
97 } else if (magic == 60012) {
98 print_line(level, "dump: 4.2BSD (or newer) without IDC or Sun NFS, %s", get_ve_name(en));
99 } else if (magic == 60013) {
100 print_line(level, "dump: 4.2BSD (or newer) with IDC, %s", get_ve_name(en));
101 } else if (magic == 60014) {
102 print_line(level, "dump: Convex Storage Manager, %s", get_ve_name(en));
103 }
104 }
105 }
106
107 /* EOF */