cd9660.c revision 1.10 1 /* $NetBSD: cd9660.c,v 1.10 1999/11/13 21:17:56 thorpej Exp $ */
2
3 /*
4 * Copyright (C) 1996 Wolfgang Solfrank.
5 * Copyright (C) 1996 TooLs GmbH.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by TooLs GmbH.
19 * 4. The name of TooLs GmbH may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 /*
35 * Stand-alone ISO9660 file reading package.
36 *
37 * Note: This doesn't support Rock Ridge extensions, extended attributes,
38 * blocksizes other than 2048 bytes, multi-extent files, etc.
39 */
40 #include <sys/param.h>
41 #ifdef _STANDALONE
42 #include <lib/libkern/libkern.h>
43 #else
44 #include <string.h>
45 #endif
46 #include <isofs/cd9660/iso.h>
47
48 #include "stand.h"
49 #include "cd9660.h"
50
51 /*
52 * XXX Does not currently implement:
53 * XXX
54 * XXX LIBSA_NO_FS_SYMLINK (does this even make sense?)
55 * XXX LIBSA_FS_SINGLECOMPONENT
56 */
57
58 struct file {
59 off_t off; /* Current offset within file */
60 daddr_t bno; /* Starting block number */
61 off_t size; /* Size of file */
62 };
63
64 struct ptable_ent {
65 char namlen [ISODCL( 1, 1)]; /* 711 */
66 char extlen [ISODCL( 2, 2)]; /* 711 */
67 char block [ISODCL( 3, 6)]; /* 732 */
68 char parent [ISODCL( 7, 8)]; /* 722 */
69 char name [1];
70 };
71 #define PTFIXSZ 8
72 #define PTSIZE(pp) roundup(PTFIXSZ + isonum_711((pp)->namlen), 2)
73
74 #define cdb2devb(bno) ((bno) * ISO_DEFAULT_BLOCK_SIZE / DEV_BSIZE)
75
76 static int toupper __P((int));
77 static int pnmatch __P((char *, struct ptable_ent *));
78 static int dirmatch __P((char *, struct iso_directory_record *));
79
80 static int
81 toupper(c)
82 int c;
83 {
84 return c >= 'a' && c <= 'z' ? c - 'a' + 'A' : c;
85 }
86
87 static int
88 pnmatch(path, pp)
89 char *path;
90 struct ptable_ent *pp;
91 {
92 char *cp;
93 int i;
94
95 cp = pp->name;
96 for (i = isonum_711(pp->namlen); --i >= 0; path++, cp++) {
97 if (toupper(*path) == *cp)
98 continue;
99 return 0;
100 }
101 if (*path != '/')
102 return 0;
103 return 1;
104 }
105
106 static int
107 dirmatch(path, dp)
108 char *path;
109 struct iso_directory_record *dp;
110 {
111 char *cp;
112 int i;
113
114 /* This needs to be a regular file */
115 if (dp->flags[0] & 6)
116 return 0;
117
118 cp = dp->name;
119 for (i = isonum_711(dp->name_len); --i >= 0; path++, cp++) {
120 if (!*path)
121 break;
122 if (toupper(*path) == *cp)
123 continue;
124 return 0;
125 }
126 if (*path)
127 return 0;
128 /*
129 * Allow stripping of trailing dots and the version number.
130 * Note that this will find the first instead of the last version
131 * of a file.
132 */
133 if (i >= 0 && (*cp == ';' || *cp == '.')) {
134 /* This is to prevent matching of numeric extensions */
135 if (*cp == '.' && cp[1] != ';')
136 return 0;
137 while (--i >= 0)
138 if (*++cp != ';' && (*cp < '0' || *cp > '9'))
139 return 0;
140 }
141 return 1;
142 }
143
144 int
145 cd9660_open(path, f)
146 char *path;
147 struct open_file *f;
148 {
149 struct file *fp = 0;
150 void *buf;
151 struct iso_primary_descriptor *vd;
152 size_t buf_size, read, psize, dsize;
153 daddr_t bno;
154 int parent, ent;
155 struct ptable_ent *pp;
156 struct iso_directory_record *dp = 0;
157 int rc;
158
159 /* First find the volume descriptor */
160 buf = alloc(buf_size = ISO_DEFAULT_BLOCK_SIZE);
161 vd = buf;
162 for (bno = 16;; bno++) {
163 #if !defined(LIBSA_NO_TWIDDLE)
164 twiddle();
165 #endif
166 rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ, cdb2devb(bno),
167 ISO_DEFAULT_BLOCK_SIZE, buf, &read);
168 if (rc)
169 goto out;
170 if (read != ISO_DEFAULT_BLOCK_SIZE) {
171 rc = EIO;
172 goto out;
173 }
174 rc = EINVAL;
175 if (bcmp(vd->id, ISO_STANDARD_ID, sizeof vd->id) != 0)
176 goto out;
177 if (isonum_711(vd->type) == ISO_VD_END)
178 goto out;
179 if (isonum_711(vd->type) == ISO_VD_PRIMARY)
180 break;
181 }
182 if (isonum_723(vd->logical_block_size) != ISO_DEFAULT_BLOCK_SIZE)
183 goto out;
184
185 /* Now get the path table and lookup the directory of the file */
186 bno = isonum_732(vd->type_m_path_table);
187 psize = isonum_733(vd->path_table_size);
188
189 if (psize > ISO_DEFAULT_BLOCK_SIZE) {
190 free(buf, ISO_DEFAULT_BLOCK_SIZE);
191 buf = alloc(buf_size = roundup(psize, ISO_DEFAULT_BLOCK_SIZE));
192 }
193
194 #if !defined(LIBSA_NO_TWIDDLE)
195 twiddle();
196 #endif
197 rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ, cdb2devb(bno),
198 buf_size, buf, &read);
199 if (rc)
200 goto out;
201 if (read != buf_size) {
202 rc = EIO;
203 goto out;
204 }
205
206 parent = 1;
207 pp = (struct ptable_ent *)buf;
208 ent = 1;
209 bno = isonum_732(pp->block) + isonum_711(pp->extlen);
210
211 rc = ENOENT;
212 while (*path) {
213 if ((caddr_t)pp >= (caddr_t)buf + psize)
214 break;
215 if (isonum_722(pp->parent) != parent)
216 break;
217 if (!pnmatch(path, pp)) {
218 pp = (struct ptable_ent *)((caddr_t)pp + PTSIZE(pp));
219 ent++;
220 continue;
221 }
222 path += isonum_711(pp->namlen) + 1;
223 parent = ent;
224 bno = isonum_732(pp->block) + isonum_711(pp->extlen);
225 while ((caddr_t)pp < (caddr_t)buf + psize) {
226 if (isonum_722(pp->parent) == parent)
227 break;
228 pp = (struct ptable_ent *)((caddr_t)pp + PTSIZE(pp));
229 ent++;
230 }
231 }
232
233 /* Now bno has the start of the directory that supposedly contains the file */
234 bno--;
235 dsize = 1; /* Something stupid, but > 0 XXX */
236 for (psize = 0; psize < dsize;) {
237 if (!(psize % ISO_DEFAULT_BLOCK_SIZE)) {
238 bno++;
239 #if !defined(LIBSA_NO_TWIDDLE)
240 twiddle();
241 #endif
242 rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ,
243 cdb2devb(bno),
244 ISO_DEFAULT_BLOCK_SIZE,
245 buf, &read);
246 if (rc)
247 goto out;
248 if (read != ISO_DEFAULT_BLOCK_SIZE) {
249 rc = EIO;
250 goto out;
251 }
252 dp = (struct iso_directory_record *)buf;
253 }
254 if (!isonum_711(dp->length)) {
255 if ((void *)dp == buf)
256 psize += ISO_DEFAULT_BLOCK_SIZE;
257 else
258 psize = roundup(psize, ISO_DEFAULT_BLOCK_SIZE);
259 continue;
260 }
261 if (dsize == 1)
262 dsize = isonum_733(dp->size);
263 if (dirmatch(path, dp))
264 break;
265 psize += isonum_711(dp->length);
266 dp = (struct iso_directory_record *)((caddr_t)dp + isonum_711(dp->length));
267 }
268
269 if (psize >= dsize) {
270 rc = ENOENT;
271 goto out;
272 }
273
274 /* allocate file system specific data structure */
275 fp = alloc(sizeof(struct file));
276 bzero(fp, sizeof(struct file));
277 f->f_fsdata = (void *)fp;
278
279 fp->off = 0;
280 fp->bno = isonum_733(dp->extent);
281 fp->size = isonum_733(dp->size);
282 free(buf, buf_size);
283
284 return 0;
285
286 out:
287 if (fp)
288 free(fp, sizeof(struct file));
289 free(buf, buf_size);
290
291 return rc;
292 }
293
294 #if !defined(LIBSA_NO_FS_CLOSE)
295 int
296 cd9660_close(f)
297 struct open_file *f;
298 {
299 struct file *fp = (struct file *)f->f_fsdata;
300
301 f->f_fsdata = 0;
302 free(fp, sizeof *fp);
303
304 return 0;
305 }
306 #endif /* !defined(LIBSA_NO_FS_CLOSE) */
307
308 int
309 cd9660_read(f, start, size, resid)
310 struct open_file *f;
311 void *start;
312 size_t size;
313 size_t *resid;
314 {
315 struct file *fp = (struct file *)f->f_fsdata;
316 int rc = 0;
317 daddr_t bno;
318 char buf[ISO_DEFAULT_BLOCK_SIZE];
319 char *dp;
320 size_t read, off;
321
322 while (size) {
323 if (fp->off < 0 || fp->off >= fp->size)
324 break;
325 bno = fp->off / ISO_DEFAULT_BLOCK_SIZE + fp->bno;
326 if (fp->off & (ISO_DEFAULT_BLOCK_SIZE - 1)
327 || size < ISO_DEFAULT_BLOCK_SIZE)
328 dp = buf;
329 else
330 dp = start;
331 #if !defined(LIBSA_NO_TWIDDLE)
332 twiddle();
333 #endif
334 rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ, cdb2devb(bno),
335 ISO_DEFAULT_BLOCK_SIZE, dp, &read);
336 if (rc)
337 return rc;
338 if (read != ISO_DEFAULT_BLOCK_SIZE)
339 return EIO;
340 if (dp == buf) {
341 off = fp->off & (ISO_DEFAULT_BLOCK_SIZE - 1);
342 if (read > off + size)
343 read = off + size;
344 read -= off;
345 bcopy(buf + off, start, read);
346 start = (caddr_t)start + read;
347 fp->off += read;
348 size -= read;
349 } else {
350 start = (caddr_t)start + ISO_DEFAULT_BLOCK_SIZE;
351 fp->off += ISO_DEFAULT_BLOCK_SIZE;
352 size -= ISO_DEFAULT_BLOCK_SIZE;
353 }
354 }
355 if (resid)
356 *resid = size;
357 return rc;
358 }
359
360 #if !defined(LIBSA_NO_FS_WRITE)
361 int
362 cd9660_write(f, start, size, resid)
363 struct open_file *f;
364 void *start;
365 size_t size;
366 size_t *resid;
367 {
368 return EROFS;
369 }
370 #endif /* !defined(LIBSA_NO_FS_WRITE) */
371
372 #if !defined(LIBSA_NO_FS_SEEK)
373 off_t
374 cd9660_seek(f, offset, where)
375 struct open_file *f;
376 off_t offset;
377 int where;
378 {
379 struct file *fp = (struct file *)f->f_fsdata;
380
381 switch (where) {
382 case SEEK_SET:
383 fp->off = offset;
384 break;
385 case SEEK_CUR:
386 fp->off += offset;
387 break;
388 case SEEK_END:
389 fp->off = fp->size - offset;
390 break;
391 default:
392 return -1;
393 }
394 return fp->off;
395 }
396 #endif /* !defined(LIBSA_NO_FS_SEEK) */
397
398 int
399 cd9660_stat(f, sb)
400 struct open_file *f;
401 struct stat *sb;
402 {
403 struct file *fp = (struct file *)f->f_fsdata;
404
405 /* only importatn stuff */
406 sb->st_mode = S_IFREG | S_IRUSR | S_IRGRP | S_IROTH;
407 sb->st_uid = sb->st_gid = 0;
408 sb->st_size = fp->size;
409 return 0;
410 }
411