#include <sys/param.h>
#include <sys/stat.h>
#include <lib/libkern/libkern.h>
#include <isofs/cd9660/iso.h>
#include "stand.h"
#include "cd9660.h"
struct file {
off_t off;
daddr32_t bno;
off_t size;
};
struct ptable_ent {
char namlen [ISODCL( 1, 1)];
char extlen [ISODCL( 2, 2)];
char block [ISODCL( 3, 6)];
char parent [ISODCL( 7, 8)];
char name [1];
};
#define PTFIXSZ 8
#define PTSIZE(pp) roundup(PTFIXSZ + isonum_711((pp)->namlen), 2)
#define cdb2devb(bno) ((bno) * ISO_DEFAULT_BLOCK_SIZE / DEV_BSIZE)
static int
pnmatch(const char *path, struct ptable_ent *pp)
{
const char *cp;
int i;
cp = pp->name;
for (i = isonum_711(pp->namlen); --i >= 0; path++, cp++) {
if (toupper(*path) == *cp)
continue;
return 0;
}
if (*path != '/')
return 0;
return 1;
}
static int
dirmatch(const char *path, struct iso_directory_record *dp)
{
const char *cp;
int i;
if (dp->flags[0] & 6)
return 0;
cp = dp->name;
for (i = isonum_711(dp->name_len); --i >= 0; path++, cp++) {
if (!*path)
break;
if (toupper(*path) == *cp)
continue;
return 0;
}
if (*path)
return 0;
if (i >= 0 && (*cp == ';' || *cp == '.')) {
if (*cp == '.' && cp[1] != ';')
return 0;
while (--i >= 0)
if (*++cp != ';' && (*cp < '0' || *cp > '9'))
return 0;
}
return 1;
}
int
cd9660_open(char *path, struct open_file *f)
{
struct file *fp = 0;
char *buf;
struct iso_primary_descriptor *vd;
size_t buf_size, nread, psize, dsize;
daddr32_t bno;
int parent, ent;
struct ptable_ent *pp;
struct iso_directory_record *dp;
int rc;
buf = alloc(buf_size = ISO_DEFAULT_BLOCK_SIZE);
dp = (struct iso_directory_record *)buf;
vd = (struct iso_primary_descriptor *)buf;
for (bno = 16;; bno++) {
twiddle();
rc = f->f_dev->dv_strategy(f->f_devdata, F_READ, cdb2devb(bno),
ISO_DEFAULT_BLOCK_SIZE, buf, &nread);
if (rc)
goto out;
if (nread != ISO_DEFAULT_BLOCK_SIZE) {
rc = EIO;
goto out;
}
rc = EINVAL;
if (bcmp(vd->id, ISO_STANDARD_ID, sizeof vd->id) != 0)
goto out;
if (isonum_711(vd->type) == ISO_VD_END)
goto out;
if (isonum_711(vd->type) == ISO_VD_PRIMARY)
break;
}
if (isonum_723(vd->logical_block_size) != ISO_DEFAULT_BLOCK_SIZE)
goto out;
bno = isonum_732(vd->type_m_path_table);
psize = isonum_733(vd->path_table_size);
if (psize > ISO_DEFAULT_BLOCK_SIZE) {
free(buf, ISO_DEFAULT_BLOCK_SIZE);
buf = alloc(buf_size = roundup(psize, ISO_DEFAULT_BLOCK_SIZE));
}
twiddle();
rc = f->f_dev->dv_strategy(f->f_devdata, F_READ, cdb2devb(bno),
buf_size, buf, &nread);
if (rc)
goto out;
if (nread != buf_size) {
rc = EIO;
goto out;
}
parent = 1;
pp = (struct ptable_ent *)buf;
ent = 1;
bno = isonum_732(pp->block) + isonum_711(pp->extlen);
rc = ENOENT;
while (*path == '/')
path++;
while (*path) {
if ((char *)pp >= buf + psize)
break;
if (isonum_722(pp->parent) != parent)
break;
if (!pnmatch(path, pp)) {
pp = (struct ptable_ent *)((char *)pp + PTSIZE(pp));
ent++;
continue;
}
path += isonum_711(pp->namlen) + 1;
parent = ent;
bno = isonum_732(pp->block) + isonum_711(pp->extlen);
while ((char *)pp < buf + psize) {
if (isonum_722(pp->parent) == parent)
break;
pp = (struct ptable_ent *)((char *)pp + PTSIZE(pp));
ent++;
}
}
bno--;
dsize = 1;
for (psize = 0; psize < dsize;) {
if (!(psize % ISO_DEFAULT_BLOCK_SIZE)) {
bno++;
twiddle();
rc = f->f_dev->dv_strategy(f->f_devdata, F_READ,
cdb2devb(bno),
ISO_DEFAULT_BLOCK_SIZE,
buf, &nread);
if (rc)
goto out;
if (nread != ISO_DEFAULT_BLOCK_SIZE) {
rc = EIO;
goto out;
}
dp = (struct iso_directory_record *)buf;
}
if (!isonum_711(dp->length)) {
if ((void *)dp == buf)
psize += ISO_DEFAULT_BLOCK_SIZE;
else
psize = roundup(psize, ISO_DEFAULT_BLOCK_SIZE);
continue;
}
if (dsize == 1)
dsize = isonum_733(dp->size);
if (dirmatch(path, dp))
break;
psize += isonum_711(dp->length);
dp = (struct iso_directory_record *)((char *)dp +
isonum_711(dp->length));
}
if (psize >= dsize) {
rc = ENOENT;
goto out;
}
fp = alloc(sizeof(struct file));
bzero(fp, sizeof(struct file));
f->f_fsdata = (void *)fp;
fp->off = 0;
fp->bno = isonum_733(dp->extent);
fp->size = isonum_733(dp->size);
free(buf, buf_size);
return 0;
out:
if (fp)
free(fp, sizeof(struct file));
free(buf, buf_size);
return rc;
}
int
cd9660_close(struct open_file *f)
{
struct file *fp = (struct file *)f->f_fsdata;
f->f_fsdata = 0;
free(fp, sizeof *fp);
return 0;
}
int
cd9660_read(struct open_file *f, void *start, size_t size, size_t *resid)
{
struct file *fp = (struct file *)f->f_fsdata;
int rc = 0;
daddr32_t bno;
char buf[ISO_DEFAULT_BLOCK_SIZE];
char *dp, *st = start;
size_t nread, off;
while (size) {
if (fp->off < 0 || fp->off >= fp->size)
break;
bno = (fp->off >> ISO_DEFAULT_BLOCK_SHIFT) + fp->bno;
if (fp->off & (ISO_DEFAULT_BLOCK_SIZE - 1)
|| size < ISO_DEFAULT_BLOCK_SIZE)
dp = buf;
else
dp = st;
twiddle();
rc = f->f_dev->dv_strategy(f->f_devdata, F_READ, cdb2devb(bno),
ISO_DEFAULT_BLOCK_SIZE, dp, &nread);
if (rc)
return rc;
if (nread != ISO_DEFAULT_BLOCK_SIZE)
return EIO;
off = fp->off & (ISO_DEFAULT_BLOCK_SIZE - 1);
nread -= off;
if (nread > size)
nread = size;
if (nread > (fp->size - fp->off))
nread = (fp->size - fp->off);
if (dp == buf)
bcopy(buf + off, st, nread);
st += nread;
fp->off += nread;
size -= nread;
}
if (resid)
*resid = size;
return rc;
}
int
cd9660_write(struct open_file *f, void *start, size_t size, size_t *resid)
{
return EROFS;
}
off_t
cd9660_seek(struct open_file *f, off_t offset, int where)
{
struct file *fp = (struct file *)f->f_fsdata;
switch (where) {
case SEEK_SET:
fp->off = offset;
break;
case SEEK_CUR:
fp->off += offset;
break;
case SEEK_END:
fp->off = fp->size - offset;
break;
default:
return -1;
}
return fp->off;
}
int
cd9660_stat(struct open_file *f, struct stat *sb)
{
struct file *fp = (struct file *)f->f_fsdata;
sb->st_mode = S_IFREG | S_IRUSR | S_IRGRP | S_IROTH;
sb->st_uid = sb->st_gid = 0;
sb->st_size = fp->size;
return 0;
}
#ifndef NO_READDIR
int
cd9660_readdir(struct open_file *f, char *name)
{
return (EROFS);
}
#endif