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