xref: /freebsd/stand/libsa/cd9660.c (revision 52f72944b8f5abb2386eae924357dee8aea17d5b)
1 /*	$NetBSD: cd9660.c,v 1.5 1997/06/26 19:11:33 drochner 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 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 
37 /*
38  * Stand-alone ISO9660 file reading package.
39  *
40  * Note: This doesn't support Rock Ridge extensions, extended attributes,
41  * blocksizes other than 2048 bytes, multi-extent files, etc.
42  */
43 #include <sys/param.h>
44 #include <string.h>
45 #include <sys/dirent.h>
46 #include <fs/cd9660/iso.h>
47 #include <fs/cd9660/cd9660_rrip.h>
48 
49 #include "stand.h"
50 
51 #define	SUSP_CONTINUATION	"CE"
52 #define	SUSP_PRESENT		"SP"
53 #define	SUSP_STOP		"ST"
54 #define	SUSP_EXTREF		"ER"
55 #define	RRIP_NAME		"NM"
56 
57 typedef struct {
58 	ISO_SUSP_HEADER		h;
59 	u_char signature	[ISODCL (  5,    6)];
60 	u_char len_skp		[ISODCL (  7,    7)]; /* 711 */
61 } ISO_SUSP_PRESENT;
62 
63 static int	buf_read_file(struct open_file *f, char **buf_p,
64 		    size_t *size_p);
65 static int	cd9660_open(const char *path, struct open_file *f);
66 static int	cd9660_close(struct open_file *f);
67 static int	cd9660_read(struct open_file *f, void *buf, size_t size,
68 		    size_t *resid);
69 static off_t	cd9660_seek(struct open_file *f, off_t offset, int where);
70 static int	cd9660_stat(struct open_file *f, struct stat *sb);
71 static int	cd9660_readdir(struct open_file *f, struct dirent *d);
72 static int	dirmatch(struct open_file *f, const char *path,
73 		    struct iso_directory_record *dp, int use_rrip, int lenskip);
74 static int	rrip_check(struct open_file *f, struct iso_directory_record *dp,
75 		    int *lenskip);
76 static char	*rrip_lookup_name(struct open_file *f,
77 		    struct iso_directory_record *dp, int lenskip, size_t *len);
78 static ISO_SUSP_HEADER *susp_lookup_record(struct open_file *f,
79 		    const char *identifier, struct iso_directory_record *dp,
80 		    int lenskip);
81 
82 struct fs_ops cd9660_fsops = {
83 	"cd9660",
84 	cd9660_open,
85 	cd9660_close,
86 	cd9660_read,
87 	null_write,
88 	cd9660_seek,
89 	cd9660_stat,
90 	cd9660_readdir
91 };
92 
93 #define	F_ISDIR		0x0001		/* Directory */
94 #define	F_ROOTDIR	0x0002		/* Root directory */
95 #define	F_RR		0x0004		/* Rock Ridge on this volume */
96 
97 struct file {
98 	int 		f_flags;	/* file flags */
99 	off_t 		f_off;		/* Current offset within file */
100 	daddr_t 	f_bno;		/* Starting block number */
101 	off_t 		f_size;		/* Size of file */
102 	daddr_t		f_buf_blkno;	/* block number of data block */
103 	char		*f_buf;		/* buffer for data block */
104 	int		f_susp_skip;	/* len_skip for SUSP records */
105 };
106 
107 struct ptable_ent {
108 	char namlen	[ISODCL( 1, 1)];	/* 711 */
109 	char extlen	[ISODCL( 2, 2)];	/* 711 */
110 	char block	[ISODCL( 3, 6)];	/* 732 */
111 	char parent	[ISODCL( 7, 8)];	/* 722 */
112 	char name	[1];
113 };
114 #define	PTFIXSZ		8
115 #define	PTSIZE(pp)	roundup(PTFIXSZ + isonum_711((pp)->namlen), 2)
116 
117 #define	cdb2devb(bno)	((bno) * ISO_DEFAULT_BLOCK_SIZE / DEV_BSIZE)
118 
119 static ISO_SUSP_HEADER *
120 susp_lookup_record(struct open_file *f, const char *identifier,
121     struct iso_directory_record *dp, int lenskip)
122 {
123 	static char susp_buffer[ISO_DEFAULT_BLOCK_SIZE];
124 	ISO_SUSP_HEADER *sh;
125 	ISO_RRIP_CONT *shc;
126 	char *p, *end;
127 	int error;
128 	size_t read;
129 
130 	p = dp->name + isonum_711(dp->name_len) + lenskip;
131 	/* Names of even length have a padding byte after the name. */
132 	if ((isonum_711(dp->name_len) & 1) == 0)
133 		p++;
134 	end = (char *)dp + isonum_711(dp->length);
135 	while (p + 3 < end) {
136 		sh = (ISO_SUSP_HEADER *)p;
137 		if (bcmp(sh->type, identifier, 2) == 0)
138 			return (sh);
139 		if (bcmp(sh->type, SUSP_STOP, 2) == 0)
140 			return (NULL);
141 		if (bcmp(sh->type, SUSP_CONTINUATION, 2) == 0) {
142 			shc = (ISO_RRIP_CONT *)sh;
143 			error = f->f_dev->dv_strategy(f->f_devdata, F_READ,
144 			    cdb2devb(isonum_733(shc->location)),
145 			    ISO_DEFAULT_BLOCK_SIZE, susp_buffer, &read);
146 
147 			/* Bail if it fails. */
148 			if (error != 0 || read != ISO_DEFAULT_BLOCK_SIZE)
149 				return (NULL);
150 			p = susp_buffer + isonum_733(shc->offset);
151 			end = p + isonum_733(shc->length);
152 		} else {
153 			/* Ignore this record and skip to the next. */
154 			p += isonum_711(sh->length);
155 
156 			/* Avoid infinite loops with corrupted file systems */
157 			if (isonum_711(sh->length) == 0)
158 				return (NULL);
159 		}
160 	}
161 	return (NULL);
162 }
163 
164 static char *
165 rrip_lookup_name(struct open_file *f, struct iso_directory_record *dp,
166     int lenskip, size_t *len)
167 {
168 	ISO_RRIP_ALTNAME *p;
169 
170 	if (len == NULL)
171 		return (NULL);
172 
173 	p = (ISO_RRIP_ALTNAME *)susp_lookup_record(f, RRIP_NAME, dp, lenskip);
174 	if (p == NULL)
175 		return (NULL);
176 	switch (*p->flags) {
177 	case ISO_SUSP_CFLAG_CURRENT:
178 		*len = 1;
179 		return (".");
180 	case ISO_SUSP_CFLAG_PARENT:
181 		*len = 2;
182 		return ("..");
183 	case 0:
184 		*len = isonum_711(p->h.length) - 5;
185 		return ((char *)p + 5);
186 	default:
187 		/*
188 		 * We don't handle hostnames or continued names as they are
189 		 * too hard, so just bail and use the default name.
190 		 */
191 		return (NULL);
192 	}
193 }
194 
195 static int
196 rrip_check(struct open_file *f, struct iso_directory_record *dp, int *lenskip)
197 {
198 	ISO_SUSP_PRESENT *sp;
199 	ISO_RRIP_EXTREF *er;
200 	char *p;
201 
202 	/* First, see if we can find a SP field. */
203 	p = dp->name + isonum_711(dp->name_len);
204 	if (p > (char *)dp + isonum_711(dp->length))
205 		return (0);
206 	sp = (ISO_SUSP_PRESENT *)p;
207 	if (bcmp(sp->h.type, SUSP_PRESENT, 2) != 0)
208 		return (0);
209 	if (isonum_711(sp->h.length) != sizeof(ISO_SUSP_PRESENT))
210 		return (0);
211 	if (sp->signature[0] != 0xbe || sp->signature[1] != 0xef)
212 		return (0);
213 	*lenskip = isonum_711(sp->len_skp);
214 
215 	/*
216 	 * Now look for an ER field.  If RRIP is present, then there must
217 	 * be at least one of these.  It would be more pedantic to walk
218 	 * through the list of fields looking for a Rock Ridge ER field.
219 	 */
220 	er = (ISO_RRIP_EXTREF *)susp_lookup_record(f, SUSP_EXTREF, dp, 0);
221 	if (er == NULL)
222 		return (0);
223 	return (1);
224 }
225 
226 static int
227 dirmatch(struct open_file *f, const char *path, struct iso_directory_record *dp,
228     int use_rrip, int lenskip)
229 {
230 	size_t len;
231 	char *cp;
232 	int i, icase;
233 
234 	if (use_rrip)
235 		cp = rrip_lookup_name(f, dp, lenskip, &len);
236 	else
237 		cp = NULL;
238 	if (cp == NULL) {
239 		len = isonum_711(dp->name_len);
240 		cp = dp->name;
241 		icase = 1;
242 	} else
243 		icase = 0;
244 	for (i = len; --i >= 0; path++, cp++) {
245 		if (!*path || *path == '/')
246 			break;
247 		if (*path == *cp)
248 			continue;
249 		if (!icase && toupper(*path) == *cp)
250 			continue;
251 		return 0;
252 	}
253 	if (*path && *path != '/')
254 		return 0;
255 	/*
256 	 * Allow stripping of trailing dots and the version number.
257 	 * Note that this will find the first instead of the last version
258 	 * of a file.
259 	 */
260 	if (i >= 0 && (*cp == ';' || *cp == '.')) {
261 		/* This is to prevent matching of numeric extensions */
262 		if (*cp == '.' && cp[1] != ';')
263 			return 0;
264 		while (--i >= 0)
265 			if (*++cp != ';' && (*cp < '0' || *cp > '9'))
266 				return 0;
267 	}
268 	return 1;
269 }
270 
271 static int
272 cd9660_open(const char *path, struct open_file *f)
273 {
274 	struct file *fp = NULL;
275 	void *buf;
276 	struct iso_primary_descriptor *vd;
277 	size_t buf_size, read, dsize, off;
278 	daddr_t bno, boff;
279 	struct iso_directory_record rec;
280 	struct iso_directory_record *dp = NULL;
281 	int rc, first, use_rrip, lenskip;
282 
283 	/* First find the volume descriptor */
284 	buf = malloc(buf_size = ISO_DEFAULT_BLOCK_SIZE);
285 	vd = buf;
286 	for (bno = 16;; bno++) {
287 		twiddle(1);
288 		rc = f->f_dev->dv_strategy(f->f_devdata, F_READ, cdb2devb(bno),
289 					ISO_DEFAULT_BLOCK_SIZE, buf, &read);
290 		if (rc)
291 			goto out;
292 		if (read != ISO_DEFAULT_BLOCK_SIZE) {
293 			rc = EIO;
294 			goto out;
295 		}
296 		rc = EINVAL;
297 		if (bcmp(vd->id, ISO_STANDARD_ID, sizeof vd->id) != 0)
298 			goto out;
299 		if (isonum_711(vd->type) == ISO_VD_END)
300 			goto out;
301 		if (isonum_711(vd->type) == ISO_VD_PRIMARY)
302 			break;
303 	}
304 	if (isonum_723(vd->logical_block_size) != ISO_DEFAULT_BLOCK_SIZE)
305 		goto out;
306 
307 	rec = *(struct iso_directory_record *) vd->root_directory_record;
308 	if (*path == '/') path++; /* eat leading '/' */
309 
310 	first = 1;
311 	use_rrip = 0;
312 	while (*path) {
313 		bno = isonum_733(rec.extent) + isonum_711(rec.ext_attr_length);
314 		dsize = isonum_733(rec.size);
315 		off = 0;
316 		boff = 0;
317 
318 		while (off < dsize) {
319 			if ((off % ISO_DEFAULT_BLOCK_SIZE) == 0) {
320 				twiddle(1);
321 				rc = f->f_dev->dv_strategy
322 					(f->f_devdata, F_READ,
323 					 cdb2devb(bno + boff),
324 					 ISO_DEFAULT_BLOCK_SIZE,
325 					 buf, &read);
326 				if (rc)
327 					goto out;
328 				if (read != ISO_DEFAULT_BLOCK_SIZE) {
329 					rc = EIO;
330 					goto out;
331 				}
332 				boff++;
333 				dp = (struct iso_directory_record *) buf;
334 			}
335 			if (isonum_711(dp->length) == 0) {
336 			    /* skip to next block, if any */
337 			    off = boff * ISO_DEFAULT_BLOCK_SIZE;
338 			    continue;
339 			}
340 
341 			/* See if RRIP is in use. */
342 			if (first)
343 				use_rrip = rrip_check(f, dp, &lenskip);
344 
345 			if (dirmatch(f, path, dp, use_rrip,
346 				first ? 0 : lenskip)) {
347 				first = 0;
348 				break;
349 			} else
350 				first = 0;
351 
352 			dp = (struct iso_directory_record *)
353 				((char *) dp + isonum_711(dp->length));
354 			/* If the new block has zero length, it is padding. */
355 			if (isonum_711(dp->length) == 0) {
356 				/* Skip to next block, if any. */
357 				off = boff * ISO_DEFAULT_BLOCK_SIZE;
358 				continue;
359 			}
360 			off += isonum_711(dp->length);
361 		}
362 		if (off >= dsize) {
363 			rc = ENOENT;
364 			goto out;
365 		}
366 
367 		rec = *dp;
368 		while (*path && *path != '/') /* look for next component */
369 			path++;
370 		if (*path) path++; /* skip '/' */
371 	}
372 
373 	/* allocate file system specific data structure */
374 	fp = malloc(sizeof(struct file));
375 	bzero(fp, sizeof(struct file));
376 	f->f_fsdata = (void *)fp;
377 
378 	if ((isonum_711(rec.flags) & 2) != 0) {
379 		fp->f_flags = F_ISDIR;
380 	}
381 	if (first) {
382 		fp->f_flags |= F_ROOTDIR;
383 
384 		/* Check for Rock Ridge since we didn't in the loop above. */
385 		bno = isonum_733(rec.extent) + isonum_711(rec.ext_attr_length);
386 		twiddle(1);
387 		rc = f->f_dev->dv_strategy(f->f_devdata, F_READ, cdb2devb(bno),
388 		    ISO_DEFAULT_BLOCK_SIZE, buf, &read);
389 		if (rc)
390 			goto out;
391 		if (read != ISO_DEFAULT_BLOCK_SIZE) {
392 			rc = EIO;
393 			goto out;
394 		}
395 		dp = (struct iso_directory_record *)buf;
396 		use_rrip = rrip_check(f, dp, &lenskip);
397 	}
398 	if (use_rrip) {
399 		fp->f_flags |= F_RR;
400 		fp->f_susp_skip = lenskip;
401 	}
402 	fp->f_off = 0;
403 	fp->f_bno = isonum_733(rec.extent) + isonum_711(rec.ext_attr_length);
404 	fp->f_size = isonum_733(rec.size);
405 	free(buf);
406 
407 	return 0;
408 
409 out:
410 	if (fp)
411 		free(fp);
412 	free(buf);
413 
414 	return rc;
415 }
416 
417 static int
418 cd9660_close(struct open_file *f)
419 {
420 	struct file *fp = (struct file *)f->f_fsdata;
421 
422 	f->f_fsdata = NULL;
423 	free(fp);
424 
425 	return 0;
426 }
427 
428 static int
429 buf_read_file(struct open_file *f, char **buf_p, size_t *size_p)
430 {
431 	struct file *fp = (struct file *)f->f_fsdata;
432 	daddr_t blkno, blkoff;
433 	int rc = 0;
434 	size_t read;
435 
436 	blkno = fp->f_off / ISO_DEFAULT_BLOCK_SIZE + fp->f_bno;
437 	blkoff = fp->f_off % ISO_DEFAULT_BLOCK_SIZE;
438 
439 	if (blkno != fp->f_buf_blkno) {
440 		if (fp->f_buf == (char *)0)
441 			fp->f_buf = malloc(ISO_DEFAULT_BLOCK_SIZE);
442 
443 		twiddle(16);
444 		rc = f->f_dev->dv_strategy(f->f_devdata, F_READ,
445 		    cdb2devb(blkno), ISO_DEFAULT_BLOCK_SIZE,
446 		    fp->f_buf, &read);
447 		if (rc)
448 			return (rc);
449 		if (read != ISO_DEFAULT_BLOCK_SIZE)
450 			return (EIO);
451 
452 		fp->f_buf_blkno = blkno;
453 	}
454 
455 	*buf_p = fp->f_buf + blkoff;
456 	*size_p = ISO_DEFAULT_BLOCK_SIZE - blkoff;
457 
458 	if (*size_p > fp->f_size - fp->f_off)
459 		*size_p = fp->f_size - fp->f_off;
460 	return (rc);
461 }
462 
463 static int
464 cd9660_read(struct open_file *f, void *start, size_t size, size_t *resid)
465 {
466 	struct file *fp = (struct file *)f->f_fsdata;
467 	char *buf, *addr;
468 	size_t buf_size, csize;
469 	int rc = 0;
470 
471 	addr = start;
472 	while (size) {
473 		if (fp->f_off < 0 || fp->f_off >= fp->f_size)
474 			break;
475 
476 		rc = buf_read_file(f, &buf, &buf_size);
477 		if (rc)
478 			break;
479 
480 		csize = size > buf_size ? buf_size : size;
481 		bcopy(buf, addr, csize);
482 
483 		fp->f_off += csize;
484 		addr += csize;
485 		size -= csize;
486 	}
487 	if (resid)
488 		*resid = size;
489 	return (rc);
490 }
491 
492 static int
493 cd9660_readdir(struct open_file *f, struct dirent *d)
494 {
495 	struct file *fp = (struct file *)f->f_fsdata;
496 	struct iso_directory_record *ep;
497 	size_t buf_size, reclen, namelen;
498 	int error = 0;
499 	int lenskip;
500 	char *buf, *name;
501 
502 again:
503 	if (fp->f_off >= fp->f_size)
504 		return (ENOENT);
505 	error = buf_read_file(f, &buf, &buf_size);
506 	if (error)
507 		return (error);
508 	ep = (struct iso_directory_record *)buf;
509 
510 	if (isonum_711(ep->length) == 0) {
511 		daddr_t blkno;
512 
513 		/* skip to next block, if any */
514 		blkno = fp->f_off / ISO_DEFAULT_BLOCK_SIZE;
515 		fp->f_off = (blkno + 1) * ISO_DEFAULT_BLOCK_SIZE;
516 		goto again;
517 	}
518 
519 	if (fp->f_flags & F_RR) {
520 		if (fp->f_flags & F_ROOTDIR && fp->f_off == 0)
521 			lenskip = 0;
522 		else
523 			lenskip = fp->f_susp_skip;
524 		name = rrip_lookup_name(f, ep, lenskip, &namelen);
525 	} else
526 		name = NULL;
527 	if (name == NULL) {
528 		namelen = isonum_711(ep->name_len);
529 		name = ep->name;
530 		if (namelen == 1) {
531 			if (ep->name[0] == 0)
532 				name = ".";
533 			else if (ep->name[0] == 1) {
534 				namelen = 2;
535 				name = "..";
536 			}
537 		}
538 	}
539 	reclen = sizeof(struct dirent) - (MAXNAMLEN+1) + namelen + 1;
540 	reclen = (reclen + 3) & ~3;
541 
542 	d->d_fileno = isonum_733(ep->extent);
543 	d->d_reclen = reclen;
544 	if (isonum_711(ep->flags) & 2)
545 		d->d_type = DT_DIR;
546 	else
547 		d->d_type = DT_REG;
548 	d->d_namlen = namelen;
549 
550 	bcopy(name, d->d_name, d->d_namlen);
551 	d->d_name[d->d_namlen] = 0;
552 
553 	fp->f_off += isonum_711(ep->length);
554 	return (0);
555 }
556 
557 static off_t
558 cd9660_seek(struct open_file *f, off_t offset, int where)
559 {
560 	struct file *fp = (struct file *)f->f_fsdata;
561 
562 	switch (where) {
563 	case SEEK_SET:
564 		fp->f_off = offset;
565 		break;
566 	case SEEK_CUR:
567 		fp->f_off += offset;
568 		break;
569 	case SEEK_END:
570 		fp->f_off = fp->f_size - offset;
571 		break;
572 	default:
573 		return -1;
574 	}
575 	return fp->f_off;
576 }
577 
578 static int
579 cd9660_stat(struct open_file *f, struct stat *sb)
580 {
581 	struct file *fp = (struct file *)f->f_fsdata;
582 
583 	/* only important stuff */
584 	sb->st_mode = S_IRUSR | S_IRGRP | S_IROTH;
585 	if (fp->f_flags & F_ISDIR)
586 		sb->st_mode |= S_IFDIR;
587 	else
588 		sb->st_mode |= S_IFREG;
589 	sb->st_uid = sb->st_gid = 0;
590 	sb->st_size = fp->f_size;
591 	return 0;
592 }
593