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