Home | History | Annotate | Line # | Download | only in libsa
cd9660.c revision 1.6
      1 /*	$NetBSD: cd9660.c,v 1.6 1999/02/11 09:10:44 pk 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 struct file {
     52 	off_t off;			/* Current offset within file */
     53 	daddr_t bno;			/* Starting block number  */
     54 	off_t size;			/* Size of file */
     55 };
     56 
     57 struct ptable_ent {
     58 	char namlen	[ISODCL( 1, 1)];	/* 711 */
     59 	char extlen	[ISODCL( 2, 2)];	/* 711 */
     60 	char block	[ISODCL( 3, 6)];	/* 732 */
     61 	char parent	[ISODCL( 7, 8)];	/* 722 */
     62 	char name	[1];
     63 };
     64 #define	PTFIXSZ		8
     65 #define	PTSIZE(pp)	roundup(PTFIXSZ + isonum_711((pp)->namlen), 2)
     66 
     67 #define	cdb2devb(bno)	((bno) * ISO_DEFAULT_BLOCK_SIZE / DEV_BSIZE)
     68 
     69 static int	toupper __P((int));
     70 static int	pnmatch __P((char *, struct ptable_ent *));
     71 static int	dirmatch __P((char *, struct iso_directory_record *));
     72 
     73 static int
     74 toupper(c)
     75 	int c;
     76 {
     77 	return c >= 'a' && c <= 'z' ? c - 'a' + 'A' : c;
     78 }
     79 
     80 static int
     81 pnmatch(path, pp)
     82 	char *path;
     83 	struct ptable_ent *pp;
     84 {
     85 	char *cp;
     86 	int i;
     87 
     88 	cp = pp->name;
     89 	for (i = isonum_711(pp->namlen); --i >= 0; path++, cp++) {
     90 		if (toupper(*path) == *cp)
     91 			continue;
     92 		return 0;
     93 	}
     94 	if (*path != '/')
     95 		return 0;
     96 	return 1;
     97 }
     98 
     99 static int
    100 dirmatch(path, dp)
    101 	char *path;
    102 	struct iso_directory_record *dp;
    103 {
    104 	char *cp;
    105 	int i;
    106 
    107 	/* This needs to be a regular file */
    108 	if (dp->flags[0] & 6)
    109 		return 0;
    110 
    111 	cp = dp->name;
    112 	for (i = isonum_711(dp->name_len); --i >= 0; path++, cp++) {
    113 		if (!*path)
    114 			break;
    115 		if (toupper(*path) == *cp)
    116 			continue;
    117 		return 0;
    118 	}
    119 	if (*path)
    120 		return 0;
    121 	/*
    122 	 * Allow stripping of trailing dots and the version number.
    123 	 * Note that this will find the first instead of the last version
    124 	 * of a file.
    125 	 */
    126 	if (i >= 0 && (*cp == ';' || *cp == '.')) {
    127 		/* This is to prevent matching of numeric extensions */
    128 		if (*cp == '.' && cp[1] != ';')
    129 			return 0;
    130 		while (--i >= 0)
    131 			if (*++cp != ';' && (*cp < '0' || *cp > '9'))
    132 				return 0;
    133 	}
    134 	return 1;
    135 }
    136 
    137 int
    138 cd9660_open(path, f)
    139 	char *path;
    140 	struct open_file *f;
    141 {
    142 	struct file *fp = 0;
    143 	void *buf;
    144 	struct iso_primary_descriptor *vd;
    145 	size_t buf_size, read, psize, dsize;
    146 	daddr_t bno;
    147 	int parent, ent;
    148 	struct ptable_ent *pp;
    149 	struct iso_directory_record *dp = 0;
    150 	int rc;
    151 
    152 	/* First find the volume descriptor */
    153 	buf = alloc(buf_size = ISO_DEFAULT_BLOCK_SIZE);
    154 	vd = buf;
    155 	for (bno = 16;; bno++) {
    156 		twiddle();
    157 		rc = f->f_dev->dv_strategy(f->f_devdata, F_READ, cdb2devb(bno),
    158 					   ISO_DEFAULT_BLOCK_SIZE, buf, &read);
    159 		if (rc)
    160 			goto out;
    161 		if (read != ISO_DEFAULT_BLOCK_SIZE) {
    162 			rc = EIO;
    163 			goto out;
    164 		}
    165 		rc = EINVAL;
    166 		if (bcmp(vd->id, ISO_STANDARD_ID, sizeof vd->id) != 0)
    167 			goto out;
    168 		if (isonum_711(vd->type) == ISO_VD_END)
    169 			goto out;
    170 		if (isonum_711(vd->type) == ISO_VD_PRIMARY)
    171 			break;
    172 	}
    173 	if (isonum_723(vd->logical_block_size) != ISO_DEFAULT_BLOCK_SIZE)
    174 		goto out;
    175 
    176 	/* Now get the path table and lookup the directory of the file */
    177 	bno = isonum_732(vd->type_m_path_table);
    178 	psize = isonum_733(vd->path_table_size);
    179 
    180 	if (psize > ISO_DEFAULT_BLOCK_SIZE) {
    181 		free(buf, ISO_DEFAULT_BLOCK_SIZE);
    182 		buf = alloc(buf_size = roundup(psize, ISO_DEFAULT_BLOCK_SIZE));
    183 	}
    184 
    185 	twiddle();
    186 	rc = f->f_dev->dv_strategy(f->f_devdata, F_READ, cdb2devb(bno),
    187 				   buf_size, buf, &read);
    188 	if (rc)
    189 		goto out;
    190 	if (read != buf_size) {
    191 		rc = EIO;
    192 		goto out;
    193 	}
    194 
    195 	parent = 1;
    196 	pp = (struct ptable_ent *)buf;
    197 	ent = 1;
    198 	bno = isonum_732(pp->block) + isonum_711(pp->extlen);
    199 
    200 	rc = ENOENT;
    201 	while (*path) {
    202 		if ((void *)pp >= buf + psize)
    203 			break;
    204 		if (isonum_722(pp->parent) != parent)
    205 			break;
    206 		if (!pnmatch(path, pp)) {
    207 			pp = (struct ptable_ent *)((void *)pp + PTSIZE(pp));
    208 			ent++;
    209 			continue;
    210 		}
    211 		path += isonum_711(pp->namlen) + 1;
    212 		parent = ent;
    213 		bno = isonum_732(pp->block) + isonum_711(pp->extlen);
    214 		while ((void *)pp < buf + psize) {
    215 			if (isonum_722(pp->parent) == parent)
    216 				break;
    217 			pp = (struct ptable_ent *)((void *)pp + PTSIZE(pp));
    218 			ent++;
    219 		}
    220 	}
    221 
    222 	/* Now bno has the start of the directory that supposedly contains the file */
    223 	bno--;
    224 	dsize = 1;		/* Something stupid, but > 0			XXX */
    225 	for (psize = 0; psize < dsize;) {
    226 		if (!(psize % ISO_DEFAULT_BLOCK_SIZE)) {
    227 			bno++;
    228 			twiddle();
    229 			rc = f->f_dev->dv_strategy(f->f_devdata, F_READ,
    230 						   cdb2devb(bno),
    231 						   ISO_DEFAULT_BLOCK_SIZE,
    232 						   buf, &read);
    233 			if (rc)
    234 				goto out;
    235 			if (read != ISO_DEFAULT_BLOCK_SIZE) {
    236 				rc = EIO;
    237 				goto out;
    238 			}
    239 			dp = (struct iso_directory_record *)buf;
    240 		}
    241 		if (!isonum_711(dp->length)) {
    242 			if ((void *)dp == buf)
    243 				psize += ISO_DEFAULT_BLOCK_SIZE;
    244 			else
    245 				psize = roundup(psize, ISO_DEFAULT_BLOCK_SIZE);
    246 			continue;
    247 		}
    248 		if (dsize == 1)
    249 			dsize = isonum_733(dp->size);
    250 		if (dirmatch(path, dp))
    251 			break;
    252 		psize += isonum_711(dp->length);
    253 		dp = (struct iso_directory_record *)((void *)dp + isonum_711(dp->length));
    254 	}
    255 
    256 	if (psize >= dsize) {
    257 		rc = ENOENT;
    258 		goto out;
    259 	}
    260 
    261 	/* allocate file system specific data structure */
    262 	fp = alloc(sizeof(struct file));
    263 	bzero(fp, sizeof(struct file));
    264 	f->f_fsdata = (void *)fp;
    265 
    266 	fp->off = 0;
    267 	fp->bno = isonum_733(dp->extent);
    268 	fp->size = isonum_733(dp->size);
    269 	free(buf, buf_size);
    270 
    271 	return 0;
    272 
    273 out:
    274 	if (fp)
    275 		free(fp, sizeof(struct file));
    276 	free(buf, buf_size);
    277 
    278 	return rc;
    279 }
    280 
    281 int
    282 cd9660_close(f)
    283 	struct open_file *f;
    284 {
    285 	struct file *fp = (struct file *)f->f_fsdata;
    286 
    287 	f->f_fsdata = 0;
    288 	free(fp, sizeof *fp);
    289 
    290 	return 0;
    291 }
    292 
    293 int
    294 cd9660_read(f, start, size, resid)
    295 	struct open_file *f;
    296 	void *start;
    297 	size_t size;
    298 	size_t *resid;
    299 {
    300 	struct file *fp = (struct file *)f->f_fsdata;
    301 	int rc = 0;
    302 	daddr_t bno;
    303 	char buf[ISO_DEFAULT_BLOCK_SIZE];
    304 	char *dp;
    305 	size_t read, off;
    306 
    307 	while (size) {
    308 		if (fp->off < 0 || fp->off >= fp->size)
    309 			break;
    310 		bno = fp->off / ISO_DEFAULT_BLOCK_SIZE + fp->bno;
    311 		if (fp->off & (ISO_DEFAULT_BLOCK_SIZE - 1)
    312 		    || size < ISO_DEFAULT_BLOCK_SIZE)
    313 			dp = buf;
    314 		else
    315 			dp = start;
    316 		twiddle();
    317 		rc = f->f_dev->dv_strategy(f->f_devdata, F_READ, cdb2devb(bno),
    318 					   ISO_DEFAULT_BLOCK_SIZE, dp, &read);
    319 		if (rc)
    320 			return rc;
    321 		if (read != ISO_DEFAULT_BLOCK_SIZE)
    322 			return EIO;
    323 		if (dp == buf) {
    324 			off = fp->off & (ISO_DEFAULT_BLOCK_SIZE - 1);
    325 			if (read > off + size)
    326 				read = off + size;
    327 			read -= off;
    328 			bcopy(buf + off, start, read);
    329 			start += read;
    330 			fp->off += read;
    331 			size -= read;
    332 		} else {
    333 			start += ISO_DEFAULT_BLOCK_SIZE;
    334 			fp->off += ISO_DEFAULT_BLOCK_SIZE;
    335 			size -= ISO_DEFAULT_BLOCK_SIZE;
    336 		}
    337 	}
    338 	if (resid)
    339 		*resid = size;
    340 	return rc;
    341 }
    342 
    343 int
    344 cd9660_write(f, start, size, resid)
    345 	struct open_file *f;
    346 	void *start;
    347 	size_t size;
    348 	size_t *resid;
    349 {
    350 	return EROFS;
    351 }
    352 
    353 off_t
    354 cd9660_seek(f, offset, where)
    355 	struct open_file *f;
    356 	off_t offset;
    357 	int where;
    358 {
    359 	struct file *fp = (struct file *)f->f_fsdata;
    360 
    361 	switch (where) {
    362 	case SEEK_SET:
    363 		fp->off = offset;
    364 		break;
    365 	case SEEK_CUR:
    366 		fp->off += offset;
    367 		break;
    368 	case SEEK_END:
    369 		fp->off = fp->size - offset;
    370 		break;
    371 	default:
    372 		return -1;
    373 	}
    374 	return fp->off;
    375 }
    376 
    377 int
    378 cd9660_stat(f, sb)
    379 	struct open_file *f;
    380 	struct stat *sb;
    381 {
    382 	struct file *fp = (struct file *)f->f_fsdata;
    383 
    384 	/* only importatn stuff */
    385 	sb->st_mode = S_IFREG | S_IRUSR | S_IRGRP | S_IROTH;
    386 	sb->st_uid = sb->st_gid = 0;
    387 	sb->st_size = fp->size;
    388 	return 0;
    389 }
    390