2 * Copyright (C) 1996 Wolfgang Solfrank.
3 * Copyright (C) 1996 TooLs GmbH.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by TooLs GmbH.
17 * 4. The name of TooLs GmbH may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 /* Originally derived from libsa/cd9660.c: */
33 /* $NetBSD: cd9660.c,v 1.5 1997/06/26 19:11:33 drochner Exp $ */
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <fs/cd9660/iso.h>
39 #include <fs/cd9660/cd9660_rrip.h>
41 static uint64_t cd9660_lookup(const char *);
42 static ssize_t cd9660_fsread(uint64_t, void *, size_t);
44 #define SUSP_CONTINUATION "CE"
45 #define SUSP_PRESENT "SP"
46 #define SUSP_STOP "ST"
47 #define SUSP_EXTREF "ER"
48 #define RRIP_NAME "NM"
52 u_char signature [ISODCL ( 5, 6)];
53 u_char len_skp [ISODCL ( 7, 7)]; /* 711 */
57 read_iso_block(void *buffer, daddr_t blkno)
60 return (drvread(&dsk, buffer, blkno * 4, 4));
63 static ISO_SUSP_HEADER *
64 susp_lookup_record(const char *identifier, struct iso_directory_record *dp,
67 static char susp_buffer[ISO_DEFAULT_BLOCK_SIZE];
73 p = dp->name + isonum_711(dp->name_len) + lenskip;
74 /* Names of even length have a padding byte after the name. */
75 if ((isonum_711(dp->name_len) & 1) == 0)
77 end = (char *)dp + isonum_711(dp->length);
79 sh = (ISO_SUSP_HEADER *)p;
80 if (bcmp(sh->type, identifier, 2) == 0)
82 if (bcmp(sh->type, SUSP_STOP, 2) == 0)
84 if (bcmp(sh->type, SUSP_CONTINUATION, 2) == 0) {
85 shc = (ISO_RRIP_CONT *)sh;
86 error = read_iso_block(susp_buffer,
87 isonum_733(shc->location));
89 /* Bail if it fails. */
92 p = susp_buffer + isonum_733(shc->offset);
93 end = p + isonum_733(shc->length);
95 /* Ignore this record and skip to the next. */
96 p += isonum_711(sh->length);
98 /* Avoid infinite loops with corrupted file systems */
99 if (isonum_711(sh->length) == 0)
107 rrip_lookup_name(struct iso_directory_record *dp, int lenskip, size_t *len)
114 p = (ISO_RRIP_ALTNAME *)susp_lookup_record(RRIP_NAME, dp, lenskip);
118 case ISO_SUSP_CFLAG_CURRENT:
121 case ISO_SUSP_CFLAG_PARENT:
125 *len = isonum_711(p->h.length) - 5;
126 return ((char *)p + 5);
129 * We don't handle hostnames or continued names as they are
130 * too hard, so just bail and use the default name.
137 rrip_check(struct iso_directory_record *dp, int *lenskip)
139 ISO_SUSP_PRESENT *sp;
143 /* First, see if we can find a SP field. */
144 p = dp->name + isonum_711(dp->name_len);
145 if (p > (char *)dp + isonum_711(dp->length)) {
148 sp = (ISO_SUSP_PRESENT *)p;
149 if (bcmp(sp->h.type, SUSP_PRESENT, 2) != 0) {
152 if (isonum_711(sp->h.length) != sizeof(ISO_SUSP_PRESENT)) {
155 if (sp->signature[0] != 0xbe || sp->signature[1] != 0xef) {
158 *lenskip = isonum_711(sp->len_skp);
161 * Now look for an ER field. If RRIP is present, then there must
162 * be at least one of these. It would be more pedantic to walk
163 * through the list of fields looking for a Rock Ridge ER field.
165 er = (ISO_RRIP_EXTREF *)susp_lookup_record(SUSP_EXTREF, dp, 0);
173 dirmatch(const char *path, struct iso_directory_record *dp, int use_rrip,
177 const char *cp = NULL;
181 cp = rrip_lookup_name(dp, lenskip, &len);
185 len = isonum_711(dp->name_len);
190 for (i = len; --i >= 0; path++, cp++) {
191 if (!*path || *path == '/')
195 if (!icase && toupper(*path) == *cp)
199 if (*path && *path != '/') {
203 * Allow stripping of trailing dots and the version number.
204 * Note that this will find the first instead of the last version
207 if (i >= 0 && (*cp == ';' || *cp == '.')) {
208 /* This is to prevent matching of numeric extensions */
209 if (*cp == '.' && cp[1] != ';') {
213 if (*++cp != ';' && (*cp < '0' || *cp > '9')) {
221 cd9660_lookup(const char *path)
223 static char blkbuf[ISO_DEFAULT_BLOCK_SIZE];
224 struct iso_primary_descriptor *vd;
225 struct iso_directory_record rec;
226 struct iso_directory_record *dp = NULL;
229 int rc, first, use_rrip, lenskip;
232 for (bno = 16;; bno++) {
233 rc = read_iso_block(blkbuf, bno);
234 vd = (struct iso_primary_descriptor *)blkbuf;
236 if (bcmp(vd->id, ISO_STANDARD_ID, sizeof vd->id) != 0)
238 if (isonum_711(vd->type) == ISO_VD_END)
240 if (isonum_711(vd->type) == ISO_VD_PRIMARY)
244 bcopy(vd->root_directory_record, &rec, sizeof(rec));
245 if (*path == '/') path++; /* eat leading '/' */
251 bno = isonum_733(rec.extent) + isonum_711(rec.ext_attr_length);
252 dsize = isonum_733(rec.size);
256 while (off < dsize) {
257 if ((off % ISO_DEFAULT_BLOCK_SIZE) == 0) {
258 rc = read_iso_block(blkbuf, bno + boff);
263 dp = (struct iso_directory_record *) blkbuf;
265 if (isonum_711(dp->length) == 0) {
266 /* skip to next block, if any */
267 off = boff * ISO_DEFAULT_BLOCK_SIZE;
271 /* See if RRIP is in use. */
273 use_rrip = rrip_check(dp, &lenskip);
275 if (dirmatch(path, dp, use_rrip,
276 first ? 0 : lenskip)) {
282 dp = (struct iso_directory_record *)
283 ((char *) dp + isonum_711(dp->length));
284 /* If the new block has zero length, it is padding. */
285 if (isonum_711(dp->length) == 0) {
286 /* Skip to next block, if any. */
287 off = boff * ISO_DEFAULT_BLOCK_SIZE;
290 off += isonum_711(dp->length);
297 while (*path && *path != '/') /* look for next component */
299 if (*path) path++; /* skip '/' */
302 if ((isonum_711(rec.flags) & 2) != 0) {
306 cookie = isonum_733(rec.extent) + isonum_711(rec.ext_attr_length);
307 cookie = (cookie << 32) | isonum_733(rec.size);
313 cd9660_fsread(uint64_t cookie, void *buf, size_t nbytes)
315 static char blkbuf[ISO_DEFAULT_BLOCK_SIZE];
316 static daddr_t curstart = 0, curblk = 0;
317 daddr_t blk, blk_off;
319 size_t size, remaining, n;
322 size = cookie & 0xffffffff;
323 blk = (cookie >> 32) & 0xffffffff;
325 /* Make sure we're looking at the right file. */
326 if (((blk << 32) | size) != cookie) {
330 if (blk != curstart) {
342 while (remaining > 0) {
343 blk_off = fs_off >> ISO_DEFAULT_BLOCK_SHIFT;
344 byte_off = fs_off & (ISO_DEFAULT_BLOCK_SIZE - 1);
346 if (curblk != curstart + blk_off) {
347 curblk = curstart + blk_off;
348 read_iso_block(blkbuf, curblk);
351 if (remaining < ISO_DEFAULT_BLOCK_SIZE - byte_off) {
354 n = ISO_DEFAULT_BLOCK_SIZE - byte_off;
356 memcpy(s, blkbuf + byte_off, n);