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