Debian Codegraph @

udf.c

raw symbols SCIP
1 /*
2 * udf.c
3 * Detection of UDF file system
4 *
5 * UDF support integrated by Aaron Geyer of StorageQuest, Inc.
6 *
7 * UDF sniffer based on Richard Freeman's work at StorageQuest, Inc.
8 *
9 * Extensive changes by Christoph Pfisterer
10 *
11 * Copyright (c) 2003 Aaron Geyer
12 *
13 * Permission is hereby granted, free of charge, to any person
14 * obtaining a copy of this software and associated documentation
15 * files (the "Software"), to deal in the Software without
16 * restriction, including without limitation the rights to use, copy,
17 * modify, merge, publish, distribute, sublicense, and/or sell copies
18 * of the Software, and to permit persons to whom the Software is
19 * furnished to do so, subject to the following conditions:
20 *
21 * The above copyright notice and this permission notice shall be
22 * included in all copies or substantial portions of the Software.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 * SOFTWARE.
32 *
33 */
34
35 #include "global.h"
36
37
38 static int probe_udf(SECTION *section, int level, int sector_size);
39 static int validate_tag(unsigned char *buf, u4 sector);
40
41 void detect_udf(SECTION *section, int level)
42 {
43 unsigned char *buffer;
44 unsigned char sig_bea[7] = {0x00, 0x42, 0x45, 0x41, 0x30, 0x31, 0x01};
45 /* B E A 0 1 */
46 unsigned char sig_nsr2[7] = {0x00, 0x4e, 0x53, 0x52, 0x30, 0x32, 0x01};
47 /* N S R 0 2 */
48 unsigned char sig_nsr3[7] = {0x00, 0x4e, 0x53, 0x52, 0x30, 0x33, 0x01};
49 /* N S R 0 3 */
50 unsigned char sig_tea[7] = {0x00, 0x54, 0x45, 0x41, 0x30, 0x31, 0x01};
51 /* T E A 0 1 */
52
53 int recog_state, sector;
54 int detected = 0;
55 int probe_result;
56 int sizes[] = { 2048, 512, 1024, 4096, -1 };
57 int i;
58
59 /* check the Volume Recognition Area (shared with ISO9660) */
60
61 recog_state = 0;
62 for (sector = 16; sector < 64; sector++) {
63 if (get_buffer(section, sector * 2048, 2048, (void **)&buffer) < 2048)
64 return;
65 /* empty ID check (end of sequence) */
66 if (buffer[2] == buffer[1] &&
67 buffer[3] == buffer[1] &&
68 buffer[4] == buffer[1] &&
69 buffer[5] == buffer[1])
70 break;
71 if (recog_state == 0 && memcmp(buffer, sig_bea, 7) == 0)
72 recog_state = 1;
73 if (recog_state == 1 && memcmp(buffer, sig_tea, 7) == 0)
74 recog_state = 0;
75 if (recog_state == 1 && (memcmp(buffer, sig_nsr2, 7) == 0 ||
76 memcmp(buffer, sig_nsr3, 7) == 0)) {
77 detected = 1;
78 break;
79 }
80 }
81 if (!detected)
82 return;
83 /* We now know we should look for an actual UDF file system */
84
85 /*
86 The UDF anchor descriptor resides at sector 256 (and some other
87 places). Unfortunately, the sector size may vary, and it isn't
88 recorded anywhere. The base standard allows any multiple of
89 512 bytes, but the various UDF standards restrict this. For now,
90 we check the common sizes 512, 1K, 2K, and 4K, and give up else.
91 */
92
93 for (i = 0; sizes[i] > 0; i++) {
94 probe_result = probe_udf(section, level, sizes[i]);
95 if (probe_result > 0)
96 return;
97 }
98
99 /* We couldn't find the actual file system, but the stuff found
100 in the recognition area is worth reporting anyway */
101 print_line(level, "UDF recognition sequence, unable to locate anchor descriptor");
102 }
103
104 static int probe_udf(SECTION *section, int level, int sector_size)
105 {
106 unsigned char *buffer;
107
108 u4 count, addr, sect;
109 int seen_primary = 0;
110 int seen_logical = 0;
111 int i;
112 char s[256];
113
114 /* first read the Anchor Volume Descriptor Pointer @ sector 256 */
115 if (get_buffer(section, 256 * sector_size, 512, (void **)&buffer) < 512)
116 return 0;
117 if (!validate_tag(buffer, 256))
118 return 0;
119 /* tag identifier */
120 if (get_le_short(buffer) != 2)
121 return 0;
122
123 print_line(level, "UDF file system");
124 print_line(level + 1, "Sector size %d bytes", sector_size);
125
126 /* get the Volume Descriptor Area */
127 count = get_le_long(buffer + 16) / sector_size;
128 addr = get_le_long(buffer + 20);
129
130 /* look for a Logical Volume Descriptor */
131 for (i = 0; i < count; i++) {
132 sect = addr + i;
133 if (get_buffer(section, (u8)sect * sector_size, 512, (void **)&buffer) < 512)
134 break;
135 if (!validate_tag(buffer, sect))
136 continue;
137
138 /* switch by tag identifier */
139 switch (get_le_short(buffer)) {
140 case 1: /* Primary Volume Descriptor */
141 if (!seen_primary) {
142 seen_primary = 1;
143
144 if (buffer[24] == 8) {
145 get_string(buffer + 25, 30, s);
146 print_line(level+1, "Volume name \"%s\"", s);
147 } else if (buffer[24] == 16) {
148 format_utf16_le(buffer + 25, 30, s);
149 print_line(level+1, "Volume name \"%s\"", s);
150 } else {
151 print_line(level+1, "Volume name encoding not supported");
152 }
153
154 }
155 break;
156
157 case 6: /* Logical Volume Descriptor */
158 if (!seen_logical) {
159 seen_logical = 1;
160
161 if (memcmp(buffer + 216+1, "*OSTA UDF Compliant", 19) == 0) {
162 print_line(level+1, "UDF version %x.%02x",
163 (int)buffer[216+25], (int)buffer[216+24]);
164 }
165
166 }
167 break;
168 }
169 }
170
171 if (!seen_primary) {
172 print_line(level + 1, "Primary Volume Descriptor missing");
173 }
174
175 return 1; /* some problems */
176 }
177
178 static int validate_tag(unsigned char *buf, u4 sector)
179 {
180 int cksum, i;
181
182 /* tag checksum */
183 cksum = 0;
184 for (i = 0; i < 16; i++)
185 if (i != 4)
186 cksum += buf[i];
187 if ((cksum & 0xFF) != buf[4])
188 return 0;
189
190 /* reserverd */
191 if (buf[5] != 0)
192 return 0;
193
194 /* tag location */
195 if (get_le_long(buf + 12) != sector)
196 return 0;
197
198 return 1;
199 }
200
201 /* EOF */