164f9afd1SToomas Soome /*
264f9afd1SToomas Soome * Copyright (C) 1996 Wolfgang Solfrank.
364f9afd1SToomas Soome * Copyright (C) 1996 TooLs GmbH.
464f9afd1SToomas Soome * All rights reserved.
564f9afd1SToomas Soome *
664f9afd1SToomas Soome * Redistribution and use in source and binary forms, with or without
764f9afd1SToomas Soome * modification, are permitted provided that the following conditions
864f9afd1SToomas Soome * are met:
964f9afd1SToomas Soome * 1. Redistributions of source code must retain the above copyright
1064f9afd1SToomas Soome * notice, this list of conditions and the following disclaimer.
1164f9afd1SToomas Soome * 2. Redistributions in binary form must reproduce the above copyright
1264f9afd1SToomas Soome * notice, this list of conditions and the following disclaimer in the
1364f9afd1SToomas Soome * documentation and/or other materials provided with the distribution.
1464f9afd1SToomas Soome * 3. All advertising materials mentioning features or use of this software
1564f9afd1SToomas Soome * must display the following acknowledgement:
1664f9afd1SToomas Soome * This product includes software developed by TooLs GmbH.
1764f9afd1SToomas Soome * 4. The name of TooLs GmbH may not be used to endorse or promote products
1864f9afd1SToomas Soome * derived from this software without specific prior written permission.
1964f9afd1SToomas Soome *
2064f9afd1SToomas Soome * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
2164f9afd1SToomas Soome * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2264f9afd1SToomas Soome * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2364f9afd1SToomas Soome * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2464f9afd1SToomas Soome * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
2564f9afd1SToomas Soome * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
2664f9afd1SToomas Soome * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
2764f9afd1SToomas Soome * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
2864f9afd1SToomas Soome * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
2964f9afd1SToomas Soome * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3064f9afd1SToomas Soome */
3164f9afd1SToomas Soome
3264f9afd1SToomas Soome /* Originally derived from libsa/cd9660.c: */
3364f9afd1SToomas Soome /* $NetBSD: cd9660.c,v 1.5 1997/06/26 19:11:33 drochner Exp $ */
3464f9afd1SToomas Soome
3564f9afd1SToomas Soome #include <sys/cdefs.h>
36e58ba7f2SToomas Soome #include <sys/param.h>
3764f9afd1SToomas Soome
3864f9afd1SToomas Soome #include <fs/cd9660/iso.h>
3964f9afd1SToomas Soome #include <fs/cd9660/cd9660_rrip.h>
4064f9afd1SToomas Soome
4164f9afd1SToomas Soome static uint64_t cd9660_lookup(const char *);
4264f9afd1SToomas Soome static ssize_t cd9660_fsread(uint64_t, void *, size_t);
4364f9afd1SToomas Soome
4464f9afd1SToomas Soome #define SUSP_CONTINUATION "CE"
4564f9afd1SToomas Soome #define SUSP_PRESENT "SP"
4664f9afd1SToomas Soome #define SUSP_STOP "ST"
4764f9afd1SToomas Soome #define SUSP_EXTREF "ER"
4864f9afd1SToomas Soome #define RRIP_NAME "NM"
4964f9afd1SToomas Soome
5064f9afd1SToomas Soome typedef struct {
5164f9afd1SToomas Soome ISO_SUSP_HEADER h;
5264f9afd1SToomas Soome uint8_t signature [ISODCL(5, 6)];
5364f9afd1SToomas Soome uint8_t len_skp [ISODCL(7, 7)]; /* 711 */
5464f9afd1SToomas Soome } ISO_SUSP_PRESENT;
5564f9afd1SToomas Soome
5664f9afd1SToomas Soome #define cdb2devb(bno) ((bno) * ISO_DEFAULT_BLOCK_SIZE / DEV_BSIZE)
5764f9afd1SToomas Soome
5864f9afd1SToomas Soome static int
read_iso_block(void * buffer,daddr_t blkno)5964f9afd1SToomas Soome read_iso_block(void *buffer, daddr_t blkno)
6064f9afd1SToomas Soome {
6164f9afd1SToomas Soome
6264f9afd1SToomas Soome return (drvread(&dsk, buffer, cdb2devb(blkno),
6364f9afd1SToomas Soome ISO_DEFAULT_BLOCK_SIZE / DEV_BSIZE));
6464f9afd1SToomas Soome }
6564f9afd1SToomas Soome
6664f9afd1SToomas Soome static ISO_SUSP_HEADER *
susp_lookup_record(const char * identifier,struct iso_directory_record * dp,int lenskip)6764f9afd1SToomas Soome susp_lookup_record(const char *identifier, struct iso_directory_record *dp,
6864f9afd1SToomas Soome int lenskip)
6964f9afd1SToomas Soome {
7064f9afd1SToomas Soome static char susp_buffer[ISO_DEFAULT_BLOCK_SIZE];
7164f9afd1SToomas Soome ISO_SUSP_HEADER *sh;
7264f9afd1SToomas Soome ISO_RRIP_CONT *shc;
7364f9afd1SToomas Soome char *p, *end;
7464f9afd1SToomas Soome int error;
7564f9afd1SToomas Soome
7664f9afd1SToomas Soome p = dp->name + isonum_711(dp->name_len) + lenskip;
7764f9afd1SToomas Soome /* Names of even length have a padding byte after the name. */
7864f9afd1SToomas Soome if ((isonum_711(dp->name_len) & 1) == 0)
7964f9afd1SToomas Soome p++;
8064f9afd1SToomas Soome end = (char *)dp + isonum_711(dp->length);
8164f9afd1SToomas Soome while (p + 3 < end) {
8264f9afd1SToomas Soome sh = (ISO_SUSP_HEADER *)p;
8364f9afd1SToomas Soome if (bcmp(sh->type, identifier, 2) == 0)
8464f9afd1SToomas Soome return (sh);
8564f9afd1SToomas Soome if (bcmp(sh->type, SUSP_STOP, 2) == 0)
8664f9afd1SToomas Soome return (NULL);
8764f9afd1SToomas Soome if (bcmp(sh->type, SUSP_CONTINUATION, 2) == 0) {
8864f9afd1SToomas Soome shc = (ISO_RRIP_CONT *)sh;
8964f9afd1SToomas Soome error = read_iso_block(susp_buffer,
9064f9afd1SToomas Soome isonum_733(shc->location));
9164f9afd1SToomas Soome
9264f9afd1SToomas Soome /* Bail if it fails. */
9364f9afd1SToomas Soome if (error != 0)
9464f9afd1SToomas Soome return (NULL);
9564f9afd1SToomas Soome p = susp_buffer + isonum_733(shc->offset);
9664f9afd1SToomas Soome end = p + isonum_733(shc->length);
9764f9afd1SToomas Soome } else {
9864f9afd1SToomas Soome /* Ignore this record and skip to the next. */
9964f9afd1SToomas Soome p += isonum_711(sh->length);
10064f9afd1SToomas Soome
10164f9afd1SToomas Soome /* Avoid infinite loops with corrupted file systems */
10264f9afd1SToomas Soome if (isonum_711(sh->length) == 0)
10364f9afd1SToomas Soome return (NULL);
10464f9afd1SToomas Soome }
10564f9afd1SToomas Soome }
10664f9afd1SToomas Soome return (NULL);
10764f9afd1SToomas Soome }
10864f9afd1SToomas Soome
10964f9afd1SToomas Soome static const char *
rrip_lookup_name(struct iso_directory_record * dp,int lenskip,size_t * len)11064f9afd1SToomas Soome rrip_lookup_name(struct iso_directory_record *dp, int lenskip, size_t *len)
11164f9afd1SToomas Soome {
11264f9afd1SToomas Soome ISO_RRIP_ALTNAME *p;
11364f9afd1SToomas Soome
11464f9afd1SToomas Soome if (len == NULL)
11564f9afd1SToomas Soome return (NULL);
11664f9afd1SToomas Soome
11764f9afd1SToomas Soome p = (ISO_RRIP_ALTNAME *)susp_lookup_record(RRIP_NAME, dp, lenskip);
11864f9afd1SToomas Soome if (p == NULL)
11964f9afd1SToomas Soome return (NULL);
12064f9afd1SToomas Soome switch (*p->flags) {
12164f9afd1SToomas Soome case ISO_SUSP_CFLAG_CURRENT:
12264f9afd1SToomas Soome *len = 1;
12364f9afd1SToomas Soome return (".");
12464f9afd1SToomas Soome case ISO_SUSP_CFLAG_PARENT:
12564f9afd1SToomas Soome *len = 2;
12664f9afd1SToomas Soome return ("..");
12764f9afd1SToomas Soome case 0:
12864f9afd1SToomas Soome *len = isonum_711(p->h.length) - 5;
12964f9afd1SToomas Soome return ((char *)p + 5);
13064f9afd1SToomas Soome default:
13164f9afd1SToomas Soome /*
13264f9afd1SToomas Soome * We don't handle hostnames or continued names as they are
13364f9afd1SToomas Soome * too hard, so just bail and use the default name.
13464f9afd1SToomas Soome */
13564f9afd1SToomas Soome return (NULL);
13664f9afd1SToomas Soome }
13764f9afd1SToomas Soome }
13864f9afd1SToomas Soome
13964f9afd1SToomas Soome static int
rrip_check(struct iso_directory_record * dp,int * lenskip)14064f9afd1SToomas Soome rrip_check(struct iso_directory_record *dp, int *lenskip)
14164f9afd1SToomas Soome {
14264f9afd1SToomas Soome ISO_SUSP_PRESENT *sp;
14364f9afd1SToomas Soome ISO_RRIP_EXTREF *er;
14464f9afd1SToomas Soome char *p;
14564f9afd1SToomas Soome
14664f9afd1SToomas Soome /* First, see if we can find a SP field. */
14764f9afd1SToomas Soome p = dp->name + isonum_711(dp->name_len);
14864f9afd1SToomas Soome if (p > (char *)dp + isonum_711(dp->length)) {
14964f9afd1SToomas Soome return (0);
15064f9afd1SToomas Soome }
15164f9afd1SToomas Soome sp = (ISO_SUSP_PRESENT *)p;
15264f9afd1SToomas Soome if (bcmp(sp->h.type, SUSP_PRESENT, 2) != 0) {
15364f9afd1SToomas Soome return (0);
15464f9afd1SToomas Soome }
15564f9afd1SToomas Soome if (isonum_711(sp->h.length) != sizeof (ISO_SUSP_PRESENT)) {
15664f9afd1SToomas Soome return (0);
15764f9afd1SToomas Soome }
15864f9afd1SToomas Soome if (sp->signature[0] != 0xbe || sp->signature[1] != 0xef) {
15964f9afd1SToomas Soome return (0);
16064f9afd1SToomas Soome }
16164f9afd1SToomas Soome *lenskip = isonum_711(sp->len_skp);
16264f9afd1SToomas Soome
16364f9afd1SToomas Soome /*
16464f9afd1SToomas Soome * Now look for an ER field. If RRIP is present, then there must
16564f9afd1SToomas Soome * be at least one of these. It would be more pedantic to walk
16664f9afd1SToomas Soome * through the list of fields looking for a Rock Ridge ER field.
16764f9afd1SToomas Soome */
16864f9afd1SToomas Soome er = (ISO_RRIP_EXTREF *)susp_lookup_record(SUSP_EXTREF, dp, 0);
16964f9afd1SToomas Soome if (er == NULL) {
17064f9afd1SToomas Soome return (0);
17164f9afd1SToomas Soome }
17264f9afd1SToomas Soome return (1);
17364f9afd1SToomas Soome }
17464f9afd1SToomas Soome
17564f9afd1SToomas Soome static int
dirmatch(const char * path,struct iso_directory_record * dp,int use_rrip,int lenskip)17664f9afd1SToomas Soome dirmatch(const char *path, struct iso_directory_record *dp, int use_rrip,
17764f9afd1SToomas Soome int lenskip)
17864f9afd1SToomas Soome {
17964f9afd1SToomas Soome size_t len;
18064f9afd1SToomas Soome const char *cp = NULL;
18164f9afd1SToomas Soome int i, icase;
18264f9afd1SToomas Soome
18364f9afd1SToomas Soome if (use_rrip)
18464f9afd1SToomas Soome cp = rrip_lookup_name(dp, lenskip, &len);
18564f9afd1SToomas Soome else
18664f9afd1SToomas Soome cp = NULL;
18764f9afd1SToomas Soome if (cp == NULL) {
18864f9afd1SToomas Soome len = isonum_711(dp->name_len);
18964f9afd1SToomas Soome cp = dp->name;
19064f9afd1SToomas Soome icase = 1;
19164f9afd1SToomas Soome } else
19264f9afd1SToomas Soome icase = 0;
19364f9afd1SToomas Soome for (i = len; --i >= 0; path++, cp++) {
19464f9afd1SToomas Soome if (!*path || *path == '/')
19564f9afd1SToomas Soome break;
19664f9afd1SToomas Soome if (*path == *cp)
19764f9afd1SToomas Soome continue;
19864f9afd1SToomas Soome if (!icase && toupper(*path) == *cp)
19964f9afd1SToomas Soome continue;
20064f9afd1SToomas Soome return (0);
20164f9afd1SToomas Soome }
20264f9afd1SToomas Soome if (*path && *path != '/') {
20364f9afd1SToomas Soome return (0);
20464f9afd1SToomas Soome }
20564f9afd1SToomas Soome /*
20664f9afd1SToomas Soome * Allow stripping of trailing dots and the version number.
20764f9afd1SToomas Soome * Note that this will find the first instead of the last version
20864f9afd1SToomas Soome * of a file.
20964f9afd1SToomas Soome */
21064f9afd1SToomas Soome if (i >= 0 && (*cp == ';' || *cp == '.')) {
21164f9afd1SToomas Soome /* This is to prevent matching of numeric extensions */
21264f9afd1SToomas Soome if (*cp == '.' && cp[1] != ';') {
21364f9afd1SToomas Soome return (0);
21464f9afd1SToomas Soome }
21564f9afd1SToomas Soome while (--i >= 0)
21664f9afd1SToomas Soome if (*++cp != ';' && (*cp < '0' || *cp > '9')) {
21764f9afd1SToomas Soome return (0);
21864f9afd1SToomas Soome }
21964f9afd1SToomas Soome }
22064f9afd1SToomas Soome return (1);
22164f9afd1SToomas Soome }
22264f9afd1SToomas Soome
22364f9afd1SToomas Soome static uint64_t
cd9660_lookup(const char * path)22464f9afd1SToomas Soome cd9660_lookup(const char *path)
22564f9afd1SToomas Soome {
226e58ba7f2SToomas Soome static char blkbuf[MAX(ISO_DEFAULT_BLOCK_SIZE,
227e58ba7f2SToomas Soome sizeof (struct iso_primary_descriptor))];
22864f9afd1SToomas Soome struct iso_primary_descriptor *vd;
22964f9afd1SToomas Soome struct iso_directory_record rec;
23064f9afd1SToomas Soome struct iso_directory_record *dp = NULL;
23164f9afd1SToomas Soome size_t dsize, off;
23264f9afd1SToomas Soome daddr_t bno, boff;
23364f9afd1SToomas Soome int rc, first, use_rrip, lenskip;
23464f9afd1SToomas Soome uint64_t cookie;
23564f9afd1SToomas Soome
23664f9afd1SToomas Soome for (bno = 16; ; bno++) {
23764f9afd1SToomas Soome rc = read_iso_block(blkbuf, bno);
23864f9afd1SToomas Soome if (rc != 0)
23964f9afd1SToomas Soome return (0);
24064f9afd1SToomas Soome vd = (struct iso_primary_descriptor *)blkbuf;
24164f9afd1SToomas Soome
24264f9afd1SToomas Soome if (bcmp(vd->id, ISO_STANDARD_ID, sizeof (vd->id)) != 0)
24364f9afd1SToomas Soome return (0);
24464f9afd1SToomas Soome if (isonum_711(vd->type) == ISO_VD_END)
24564f9afd1SToomas Soome return (0);
24664f9afd1SToomas Soome if (isonum_711(vd->type) == ISO_VD_PRIMARY)
24764f9afd1SToomas Soome break;
24864f9afd1SToomas Soome }
24964f9afd1SToomas Soome
25057aea934SToomas Soome bcopy(vd->root_directory_record, &rec, sizeof (rec));
25164f9afd1SToomas Soome if (*path == '/') path++; /* eat leading '/' */
25264f9afd1SToomas Soome
25364f9afd1SToomas Soome first = 1;
25464f9afd1SToomas Soome use_rrip = 0;
25564f9afd1SToomas Soome lenskip = 0;
25664f9afd1SToomas Soome while (*path) {
25764f9afd1SToomas Soome bno = isonum_733(rec.extent) + isonum_711(rec.ext_attr_length);
25864f9afd1SToomas Soome dsize = isonum_733(rec.size);
25964f9afd1SToomas Soome off = 0;
26064f9afd1SToomas Soome boff = 0;
26164f9afd1SToomas Soome
26264f9afd1SToomas Soome while (off < dsize) {
26364f9afd1SToomas Soome if ((off % ISO_DEFAULT_BLOCK_SIZE) == 0) {
26464f9afd1SToomas Soome rc = read_iso_block(blkbuf, bno + boff);
26564f9afd1SToomas Soome if (rc) {
26664f9afd1SToomas Soome return (0);
26764f9afd1SToomas Soome }
26864f9afd1SToomas Soome boff++;
26964f9afd1SToomas Soome dp = (struct iso_directory_record *)blkbuf;
27064f9afd1SToomas Soome }
27164f9afd1SToomas Soome if (isonum_711(dp->length) == 0) {
27264f9afd1SToomas Soome /* skip to next block, if any */
27364f9afd1SToomas Soome off = boff * ISO_DEFAULT_BLOCK_SIZE;
27464f9afd1SToomas Soome continue;
27564f9afd1SToomas Soome }
27664f9afd1SToomas Soome
27764f9afd1SToomas Soome /* See if RRIP is in use. */
27864f9afd1SToomas Soome if (first)
27964f9afd1SToomas Soome use_rrip = rrip_check(dp, &lenskip);
28064f9afd1SToomas Soome
28164f9afd1SToomas Soome if (dirmatch(path, dp, use_rrip,
28264f9afd1SToomas Soome first ? 0 : lenskip)) {
28364f9afd1SToomas Soome first = 0;
28464f9afd1SToomas Soome break;
28564f9afd1SToomas Soome } else
28664f9afd1SToomas Soome first = 0;
28764f9afd1SToomas Soome
28864f9afd1SToomas Soome dp = (struct iso_directory_record *)
28964f9afd1SToomas Soome ((char *)dp + isonum_711(dp->length));
29064f9afd1SToomas Soome /* If the new block has zero length, it is padding. */
29164f9afd1SToomas Soome if (isonum_711(dp->length) == 0) {
29264f9afd1SToomas Soome /* Skip to next block, if any. */
29364f9afd1SToomas Soome off = boff * ISO_DEFAULT_BLOCK_SIZE;
29464f9afd1SToomas Soome continue;
29564f9afd1SToomas Soome }
29664f9afd1SToomas Soome off += isonum_711(dp->length);
29764f9afd1SToomas Soome }
29864f9afd1SToomas Soome if (off >= dsize) {
29964f9afd1SToomas Soome return (0);
30064f9afd1SToomas Soome }
30164f9afd1SToomas Soome
30264f9afd1SToomas Soome rec = *dp;
30364f9afd1SToomas Soome while (*path && *path != '/') /* look for next component */
30464f9afd1SToomas Soome path++;
30564f9afd1SToomas Soome if (*path) path++; /* skip '/' */
30664f9afd1SToomas Soome }
30764f9afd1SToomas Soome
30864f9afd1SToomas Soome if ((isonum_711(rec.flags) & 2) != 0) {
30964f9afd1SToomas Soome return (0);
31064f9afd1SToomas Soome }
31164f9afd1SToomas Soome
31264f9afd1SToomas Soome cookie = isonum_733(rec.extent) + isonum_711(rec.ext_attr_length);
31364f9afd1SToomas Soome cookie = (cookie << 32) | isonum_733(rec.size);
31464f9afd1SToomas Soome
31564f9afd1SToomas Soome return (cookie);
31664f9afd1SToomas Soome }
31764f9afd1SToomas Soome
31864f9afd1SToomas Soome static ssize_t
cd9660_fsread(uint64_t cookie,void * buf,size_t nbytes)31964f9afd1SToomas Soome cd9660_fsread(uint64_t cookie, void *buf, size_t nbytes)
32064f9afd1SToomas Soome {
32164f9afd1SToomas Soome static char blkbuf[ISO_DEFAULT_BLOCK_SIZE];
32264f9afd1SToomas Soome static daddr_t curstart = 0, curblk = 0;
32364f9afd1SToomas Soome daddr_t blk, blk_off;
32464f9afd1SToomas Soome off_t byte_off;
32564f9afd1SToomas Soome size_t size, remaining, n;
32664f9afd1SToomas Soome char *s;
32764f9afd1SToomas Soome
32864f9afd1SToomas Soome size = cookie & 0xffffffff;
32964f9afd1SToomas Soome blk = (cookie >> 32) & 0xffffffff;
33064f9afd1SToomas Soome
33164f9afd1SToomas Soome /* Make sure we're looking at the right file. */
33264f9afd1SToomas Soome if ((uint64_t)((blk << 32) | size) != cookie) {
33364f9afd1SToomas Soome return (-1);
33464f9afd1SToomas Soome }
33564f9afd1SToomas Soome
33664f9afd1SToomas Soome if (blk != curstart) {
33764f9afd1SToomas Soome curstart = blk;
33864f9afd1SToomas Soome fs_off = 0;
33964f9afd1SToomas Soome }
34064f9afd1SToomas Soome
34164f9afd1SToomas Soome size -= fs_off;
34264f9afd1SToomas Soome if (size < nbytes) {
34364f9afd1SToomas Soome nbytes = size;
34464f9afd1SToomas Soome }
34564f9afd1SToomas Soome remaining = nbytes;
34664f9afd1SToomas Soome s = buf;
34764f9afd1SToomas Soome
34864f9afd1SToomas Soome while (remaining > 0) {
34964f9afd1SToomas Soome blk_off = fs_off >> ISO_DEFAULT_BLOCK_SHIFT;
35064f9afd1SToomas Soome byte_off = fs_off & (ISO_DEFAULT_BLOCK_SIZE - 1);
35164f9afd1SToomas Soome
35264f9afd1SToomas Soome if (curblk != curstart + blk_off) {
35364f9afd1SToomas Soome curblk = curstart + blk_off;
35464f9afd1SToomas Soome read_iso_block(blkbuf, curblk);
35564f9afd1SToomas Soome }
35664f9afd1SToomas Soome
35764f9afd1SToomas Soome if (remaining < ISO_DEFAULT_BLOCK_SIZE - byte_off) {
35864f9afd1SToomas Soome n = remaining;
35964f9afd1SToomas Soome } else {
36064f9afd1SToomas Soome n = ISO_DEFAULT_BLOCK_SIZE - byte_off;
36164f9afd1SToomas Soome }
36264f9afd1SToomas Soome memcpy(s, blkbuf + byte_off, n);
36364f9afd1SToomas Soome remaining -= n;
36464f9afd1SToomas Soome s += n;
36564f9afd1SToomas Soome
36664f9afd1SToomas Soome fs_off += n;
36764f9afd1SToomas Soome }
36864f9afd1SToomas Soome
36964f9afd1SToomas Soome return (nbytes);
37064f9afd1SToomas Soome }
371