11541Srgrimes/*-
21541Srgrimes * Copyright (c) 1993, 1994
31541Srgrimes *	The Regents of the University of California.  All rights reserved.
41541Srgrimes *
51541Srgrimes * This code is derived from software contributed to Berkeley
61541Srgrimes * by Pace Willisson (pace@blitz.com).  The Rock Ridge Extension
71541Srgrimes * Support code is derived from software contributed to Berkeley
81541Srgrimes * by Atsushi Murai (amurai@spec.co.jp).
91541Srgrimes *
101541Srgrimes * Redistribution and use in source and binary forms, with or without
111541Srgrimes * modification, are permitted provided that the following conditions
121541Srgrimes * are met:
131541Srgrimes * 1. Redistributions of source code must retain the above copyright
141541Srgrimes *    notice, this list of conditions and the following disclaimer.
151541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
161541Srgrimes *    notice, this list of conditions and the following disclaimer in the
171541Srgrimes *    documentation and/or other materials provided with the distribution.
181541Srgrimes * 4. Neither the name of the University nor the names of its contributors
191541Srgrimes *    may be used to endorse or promote products derived from this software
201541Srgrimes *    without specific prior written permission.
211541Srgrimes *
221541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
231541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
241541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
251541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
261541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
271541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
281541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
291541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
301541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
311541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
321541Srgrimes * SUCH DAMAGE.
331541Srgrimes *
3422521Sdyson *	@(#)cd9660_rrip.c	8.6 (Berkeley) 12/5/94
351541Srgrimes */
361541Srgrimes
37116181Sobrien#include <sys/cdefs.h>
38116181Sobrien__FBSDID("$FreeBSD$");
39116181Sobrien
401541Srgrimes#include <sys/param.h>
412806Sbde#include <sys/systm.h>
4260041Sphk#include <sys/bio.h>
431541Srgrimes#include <sys/buf.h>
44193066Sjamie#include <sys/jail.h>
451541Srgrimes#include <sys/vnode.h>
461541Srgrimes#include <sys/mount.h>
471541Srgrimes#include <sys/kernel.h>
481541Srgrimes
49166639Srodrigc#include <fs/cd9660/iso.h>
50166639Srodrigc#include <fs/cd9660/cd9660_node.h>
51166639Srodrigc#include <fs/cd9660/cd9660_rrip.h>
52166639Srodrigc#include <fs/cd9660/iso_rrip.h>
531541Srgrimes
5492765Salfredtypedef int	rrt_func_t(void *, ISO_RRIP_ANALYZE *ana);
5512597Sbde
5612597Sbdetypedef struct {
5712597Sbde	char type[2];
5812597Sbde	rrt_func_t *func;
5992765Salfred	void (*func2)(struct iso_directory_record *isodir, ISO_RRIP_ANALYZE *ana);
6012597Sbde	int result;
6112597Sbde} RRIP_TABLE;
6212597Sbde
6392765Salfredstatic int	cd9660_rrip_altname(ISO_RRIP_ALTNAME *p, ISO_RRIP_ANALYZE *ana);
6492765Salfredstatic int	cd9660_rrip_attr(ISO_RRIP_ATTR *p, ISO_RRIP_ANALYZE *ana);
6592765Salfredstatic int	cd9660_rrip_cont(ISO_RRIP_CONT *p, ISO_RRIP_ANALYZE *ana);
6692765Salfredstatic void	cd9660_rrip_defattr(struct iso_directory_record *isodir,
6793075Sbde		    ISO_RRIP_ANALYZE *ana);
6892765Salfredstatic void	cd9660_rrip_defname(struct iso_directory_record *isodir,
6993075Sbde		    ISO_RRIP_ANALYZE *ana);
7092765Salfredstatic void	cd9660_rrip_deftstamp(struct iso_directory_record *isodir,
7193075Sbde		    ISO_RRIP_ANALYZE *ana);
7292765Salfredstatic int	cd9660_rrip_device(ISO_RRIP_DEVICE *p, ISO_RRIP_ANALYZE *ana);
7392765Salfredstatic int	cd9660_rrip_extref(ISO_RRIP_EXTREF *p, ISO_RRIP_ANALYZE *ana);
7492765Salfredstatic int	cd9660_rrip_idflag(ISO_RRIP_IDFLAG *p, ISO_RRIP_ANALYZE *ana);
7592765Salfredstatic int	cd9660_rrip_loop(struct iso_directory_record *isodir,
7693075Sbde		    ISO_RRIP_ANALYZE *ana, RRIP_TABLE *table);
7792765Salfredstatic int	cd9660_rrip_pclink(ISO_RRIP_CLINK *p, ISO_RRIP_ANALYZE *ana);
7892765Salfredstatic int	cd9660_rrip_reldir(ISO_RRIP_RELDIR *p, ISO_RRIP_ANALYZE *ana);
7992765Salfredstatic int	cd9660_rrip_slink(ISO_RRIP_SLINK *p, ISO_RRIP_ANALYZE *ana);
8092765Salfredstatic int	cd9660_rrip_stop(ISO_SUSP_HEADER *p, ISO_RRIP_ANALYZE *ana);
8192765Salfredstatic int	cd9660_rrip_tstamp(ISO_RRIP_TSTAMP *p, ISO_RRIP_ANALYZE *ana);
8212597Sbde
831541Srgrimes/*
841541Srgrimes * POSIX file attribute
851541Srgrimes */
861541Srgrimesstatic int
871541Srgrimescd9660_rrip_attr(p,ana)
881541Srgrimes	ISO_RRIP_ATTR *p;
891541Srgrimes	ISO_RRIP_ANALYZE *ana;
901541Srgrimes{
9122521Sdyson	ana->inop->inode.iso_mode = isonum_733(p->mode);
9222521Sdyson	ana->inop->inode.iso_uid = isonum_733(p->uid);
9322521Sdyson	ana->inop->inode.iso_gid = isonum_733(p->gid);
9422521Sdyson	ana->inop->inode.iso_links = isonum_733(p->links);
951541Srgrimes	ana->fields &= ~ISO_SUSP_ATTR;
961541Srgrimes	return ISO_SUSP_ATTR;
971541Srgrimes}
981541Srgrimes
991541Srgrimesstatic void
1001541Srgrimescd9660_rrip_defattr(isodir,ana)
1011541Srgrimes	struct iso_directory_record *isodir;
1021541Srgrimes	ISO_RRIP_ANALYZE *ana;
1031541Srgrimes{
1041541Srgrimes	/* But this is a required field! */
1051541Srgrimes	printf("RRIP without PX field?\n");
1065651Sjoerg	cd9660_defattr(isodir,ana->inop,NULL,ISO_FTYPE_RRIP);
1071541Srgrimes}
1081541Srgrimes
1091541Srgrimes/*
1101541Srgrimes * Symbolic Links
1111541Srgrimes */
1121541Srgrimesstatic int
1131541Srgrimescd9660_rrip_slink(p,ana)
1145651Sjoerg	ISO_RRIP_SLINK	*p;
1151541Srgrimes	ISO_RRIP_ANALYZE *ana;
1161541Srgrimes{
117131526Sphk	ISO_RRIP_SLINK_COMPONENT *pcomp;
118131526Sphk	ISO_RRIP_SLINK_COMPONENT *pcompe;
1191541Srgrimes	int len, wlen, cont;
1201541Srgrimes	char *outbuf, *inbuf;
121193066Sjamie	char hostbuf[MAXHOSTNAMELEN];
1228876Srgrimes
1231541Srgrimes	pcomp = (ISO_RRIP_SLINK_COMPONENT *)p->component;
1241541Srgrimes	pcompe = (ISO_RRIP_SLINK_COMPONENT *)((char *)p + isonum_711(p->h.length));
1251541Srgrimes	len = *ana->outlen;
1261541Srgrimes	outbuf = ana->outbuf;
1271541Srgrimes	cont = ana->cont;
1288876Srgrimes
1291541Srgrimes	/*
1301541Srgrimes	 * Gathering a Symbolic name from each component with path
1311541Srgrimes	 */
1321541Srgrimes	for (;
1331541Srgrimes	     pcomp < pcompe;
1341541Srgrimes	     pcomp = (ISO_RRIP_SLINK_COMPONENT *)((char *)pcomp + ISO_RRIP_SLSIZ
1351541Srgrimes						  + isonum_711(pcomp->clen))) {
1368876Srgrimes
1371541Srgrimes		if (!cont) {
1381541Srgrimes			if (len < ana->maxlen) {
1391541Srgrimes				len++;
1401541Srgrimes				*outbuf++ = '/';
1411541Srgrimes			}
1421541Srgrimes		}
1431541Srgrimes		cont = 0;
1448876Srgrimes
1451541Srgrimes		inbuf = "..";
1461541Srgrimes		wlen = 0;
1478876Srgrimes
1481541Srgrimes		switch (*pcomp->cflag) {
1498876Srgrimes
1501541Srgrimes		case ISO_SUSP_CFLAG_CURRENT:
1511541Srgrimes			/* Inserting Current */
1521541Srgrimes			wlen = 1;
1531541Srgrimes			break;
1548876Srgrimes
1551541Srgrimes		case ISO_SUSP_CFLAG_PARENT:
1561541Srgrimes			/* Inserting Parent */
1571541Srgrimes			wlen = 2;
1581541Srgrimes			break;
1598876Srgrimes
1601541Srgrimes		case ISO_SUSP_CFLAG_ROOT:
1611541Srgrimes			/* Inserting slash for ROOT */
162156693Sjoerg			/* Double slash, nothing really to do here. */
1631541Srgrimes			break;
1648876Srgrimes
1651541Srgrimes		case ISO_SUSP_CFLAG_VOLROOT:
1661541Srgrimes			/* Inserting a mount point i.e. "/cdrom" */
1671541Srgrimes			/* same as above */
1681541Srgrimes			outbuf -= len;
1691541Srgrimes			len = 0;
1701541Srgrimes			inbuf = ana->imp->im_mountp->mnt_stat.f_mntonname;
1711541Srgrimes			wlen = strlen(inbuf);
1721541Srgrimes			break;
1738876Srgrimes
1741541Srgrimes		case ISO_SUSP_CFLAG_HOST:
1751541Srgrimes			/* Inserting hostname i.e. "kurt.tools.de" */
176193066Sjamie			getcredhostname(curthread->td_ucred, hostbuf,
177193066Sjamie			    sizeof(hostbuf));
178193066Sjamie			inbuf = hostbuf;
179193066Sjamie			wlen = strlen(inbuf);
1801541Srgrimes			break;
1818876Srgrimes
1821541Srgrimes		case ISO_SUSP_CFLAG_CONTINUE:
1831541Srgrimes			cont = 1;
184102412Scharnier			/* FALLTHROUGH */
1851541Srgrimes		case 0:
1861541Srgrimes			/* Inserting component */
1871541Srgrimes			wlen = isonum_711(pcomp->clen);
1881541Srgrimes			inbuf = pcomp->name;
1891541Srgrimes			break;
1901541Srgrimes		default:
1911541Srgrimes			printf("RRIP with incorrect flags?");
1921541Srgrimes			wlen = ana->maxlen + 1;
1931541Srgrimes			break;
1941541Srgrimes		}
1958876Srgrimes
1961541Srgrimes		if (len + wlen > ana->maxlen) {
1971541Srgrimes			/* indicate error to caller */
1981541Srgrimes			ana->cont = 1;
1991541Srgrimes			ana->fields = 0;
2001541Srgrimes			ana->outbuf -= *ana->outlen;
2011541Srgrimes			*ana->outlen = 0;
2021541Srgrimes			return 0;
2031541Srgrimes		}
2048876Srgrimes
2051541Srgrimes		bcopy(inbuf,outbuf,wlen);
2061541Srgrimes		outbuf += wlen;
2071541Srgrimes		len += wlen;
2088876Srgrimes
2091541Srgrimes	}
2101541Srgrimes	ana->outbuf = outbuf;
2111541Srgrimes	*ana->outlen = len;
2121541Srgrimes	ana->cont = cont;
2138876Srgrimes
2141541Srgrimes	if (!isonum_711(p->flags)) {
2151541Srgrimes		ana->fields &= ~ISO_SUSP_SLINK;
2161541Srgrimes		return ISO_SUSP_SLINK;
2171541Srgrimes	}
2181541Srgrimes	return 0;
2191541Srgrimes}
2201541Srgrimes
2211541Srgrimes/*
2221541Srgrimes * Alternate name
2231541Srgrimes */
2241541Srgrimesstatic int
2251541Srgrimescd9660_rrip_altname(p,ana)
2261541Srgrimes	ISO_RRIP_ALTNAME *p;
2271541Srgrimes	ISO_RRIP_ANALYZE *ana;
2281541Srgrimes{
2291541Srgrimes	char *inbuf;
2301541Srgrimes	int wlen;
2311541Srgrimes	int cont;
232193066Sjamie	char hostbuf[MAXHOSTNAMELEN];
2338876Srgrimes
2341541Srgrimes	inbuf = "..";
2351541Srgrimes	wlen = 0;
2361541Srgrimes	cont = 0;
2378876Srgrimes
2381541Srgrimes	switch (*p->flags) {
2391541Srgrimes	case ISO_SUSP_CFLAG_CURRENT:
2401541Srgrimes		/* Inserting Current */
2411541Srgrimes		wlen = 1;
2421541Srgrimes		break;
2438876Srgrimes
2441541Srgrimes	case ISO_SUSP_CFLAG_PARENT:
2451541Srgrimes		/* Inserting Parent */
2461541Srgrimes		wlen = 2;
2471541Srgrimes		break;
2488876Srgrimes
2491541Srgrimes	case ISO_SUSP_CFLAG_HOST:
2501541Srgrimes		/* Inserting hostname i.e. "kurt.tools.de" */
251193066Sjamie		getcredhostname(curthread->td_ucred, hostbuf, sizeof(hostbuf));
252193066Sjamie		inbuf = hostbuf;
253193066Sjamie		wlen = strlen(inbuf);
2541541Srgrimes		break;
2558876Srgrimes
2561541Srgrimes	case ISO_SUSP_CFLAG_CONTINUE:
2571541Srgrimes		cont = 1;
258102412Scharnier		/* FALLTHROUGH */
2591541Srgrimes	case 0:
2601541Srgrimes		/* Inserting component */
2611541Srgrimes		wlen = isonum_711(p->h.length) - 5;
2621541Srgrimes		inbuf = (char *)p + 5;
2631541Srgrimes		break;
2648876Srgrimes
2651541Srgrimes	default:
2661541Srgrimes		printf("RRIP with incorrect NM flags?\n");
2671541Srgrimes		wlen = ana->maxlen + 1;
2681541Srgrimes		break;
2691541Srgrimes	}
2708876Srgrimes
2711541Srgrimes	if ((*ana->outlen += wlen) > ana->maxlen) {
2721541Srgrimes		/* treat as no name field */
2731541Srgrimes		ana->fields &= ~ISO_SUSP_ALTNAME;
2741541Srgrimes		ana->outbuf -= *ana->outlen - wlen;
2751541Srgrimes		*ana->outlen = 0;
2761541Srgrimes		return 0;
2771541Srgrimes	}
2788876Srgrimes
2791541Srgrimes	bcopy(inbuf,ana->outbuf,wlen);
2801541Srgrimes	ana->outbuf += wlen;
2818876Srgrimes
2821541Srgrimes	if (!cont) {
2831541Srgrimes		ana->fields &= ~ISO_SUSP_ALTNAME;
2841541Srgrimes		return ISO_SUSP_ALTNAME;
2851541Srgrimes	}
2861541Srgrimes	return 0;
2871541Srgrimes}
2881541Srgrimes
2891541Srgrimesstatic void
2901541Srgrimescd9660_rrip_defname(isodir,ana)
2911541Srgrimes	struct iso_directory_record *isodir;
2921541Srgrimes	ISO_RRIP_ANALYZE *ana;
2931541Srgrimes{
29445773Sdcs	isofntrans(isodir->name,isonum_711(isodir->name_len),
29545773Sdcs		   ana->outbuf,ana->outlen,
296120492Sfjoe		   1,isonum_711(isodir->flags)&4, ana->imp->joliet_level,
297120492Sfjoe		   ana->imp->im_flags, ana->imp->im_d2l);
29845773Sdcs	switch (*ana->outbuf) {
2991541Srgrimes	default:
3001541Srgrimes		break;
3011541Srgrimes	case 1:
3021541Srgrimes		*ana->outlen = 2;
303102412Scharnier		/* FALLTHROUGH */
30445773Sdcs	case 0:
30545773Sdcs		/* outlen is 1 already */
30645773Sdcs		strcpy(ana->outbuf,"..");
3071541Srgrimes		break;
3081541Srgrimes	}
3091541Srgrimes}
3101541Srgrimes
3111541Srgrimes/*
3121541Srgrimes * Parent or Child Link
3131541Srgrimes */
3141541Srgrimesstatic int
3151541Srgrimescd9660_rrip_pclink(p,ana)
3165651Sjoerg	ISO_RRIP_CLINK	*p;
3171541Srgrimes	ISO_RRIP_ANALYZE *ana;
3181541Srgrimes{
3191541Srgrimes	*ana->inump = isonum_733(p->dir_loc) << ana->imp->im_bshift;
3201541Srgrimes	ana->fields &= ~(ISO_SUSP_CLINK|ISO_SUSP_PLINK);
3211541Srgrimes	return *p->h.type == 'C' ? ISO_SUSP_CLINK : ISO_SUSP_PLINK;
3221541Srgrimes}
3231541Srgrimes
3241541Srgrimes/*
3251541Srgrimes * Relocated directory
3261541Srgrimes */
3271541Srgrimesstatic int
3281541Srgrimescd9660_rrip_reldir(p,ana)
3295651Sjoerg	ISO_RRIP_RELDIR	 *p;
3301541Srgrimes	ISO_RRIP_ANALYZE *ana;
3311541Srgrimes{
3321541Srgrimes	/* special hack to make caller aware of RE field */
3331541Srgrimes	*ana->outlen = 0;
3341541Srgrimes	ana->fields = 0;
3351541Srgrimes	return ISO_SUSP_RELDIR|ISO_SUSP_ALTNAME|ISO_SUSP_CLINK|ISO_SUSP_PLINK;
3361541Srgrimes}
3371541Srgrimes
3381541Srgrimesstatic int
3391541Srgrimescd9660_rrip_tstamp(p,ana)
3401541Srgrimes	ISO_RRIP_TSTAMP *p;
3411541Srgrimes	ISO_RRIP_ANALYZE *ana;
3421541Srgrimes{
34322521Sdyson	u_char *ptime;
344131526Sphk
3451541Srgrimes	ptime = p->time;
3468876Srgrimes
3471541Srgrimes	/* Check a format of time stamp (7bytes/17bytes) */
3481541Srgrimes	if (!(*p->flags&ISO_SUSP_TSTAMP_FORM17)) {
3491541Srgrimes		if (*p->flags&ISO_SUSP_TSTAMP_CREAT)
3501541Srgrimes			ptime += 7;
3518876Srgrimes
3521541Srgrimes		if (*p->flags&ISO_SUSP_TSTAMP_MODIFY) {
3535651Sjoerg			cd9660_tstamp_conv7(ptime,&ana->inop->inode.iso_mtime,
3545651Sjoerg					    ISO_FTYPE_RRIP);
3551541Srgrimes			ptime += 7;
3561541Srgrimes		} else
3572806Sbde			bzero(&ana->inop->inode.iso_mtime,sizeof(struct timespec));
358131526Sphk
3591541Srgrimes		if (*p->flags&ISO_SUSP_TSTAMP_ACCESS) {
3605651Sjoerg			cd9660_tstamp_conv7(ptime,&ana->inop->inode.iso_atime,
3615651Sjoerg					    ISO_FTYPE_RRIP);
3621541Srgrimes			ptime += 7;
3631541Srgrimes		} else
3641541Srgrimes			ana->inop->inode.iso_atime = ana->inop->inode.iso_mtime;
3658876Srgrimes
3661541Srgrimes		if (*p->flags&ISO_SUSP_TSTAMP_ATTR)
3675651Sjoerg			cd9660_tstamp_conv7(ptime,&ana->inop->inode.iso_ctime,
3685651Sjoerg					    ISO_FTYPE_RRIP);
3691541Srgrimes		else
3701541Srgrimes			ana->inop->inode.iso_ctime = ana->inop->inode.iso_mtime;
3718876Srgrimes
3721541Srgrimes	} else {
3731541Srgrimes		if (*p->flags&ISO_SUSP_TSTAMP_CREAT)
3741541Srgrimes			ptime += 17;
3758876Srgrimes
3761541Srgrimes		if (*p->flags&ISO_SUSP_TSTAMP_MODIFY) {
3771541Srgrimes			cd9660_tstamp_conv17(ptime,&ana->inop->inode.iso_mtime);
3781541Srgrimes			ptime += 17;
3791541Srgrimes		} else
3802806Sbde			bzero(&ana->inop->inode.iso_mtime,sizeof(struct timespec));
381131526Sphk
3821541Srgrimes		if (*p->flags&ISO_SUSP_TSTAMP_ACCESS) {
3831541Srgrimes			cd9660_tstamp_conv17(ptime,&ana->inop->inode.iso_atime);
3841541Srgrimes			ptime += 17;
3851541Srgrimes		} else
3861541Srgrimes			ana->inop->inode.iso_atime = ana->inop->inode.iso_mtime;
3878876Srgrimes
3881541Srgrimes		if (*p->flags&ISO_SUSP_TSTAMP_ATTR)
3891541Srgrimes			cd9660_tstamp_conv17(ptime,&ana->inop->inode.iso_ctime);
3901541Srgrimes		else
3911541Srgrimes			ana->inop->inode.iso_ctime = ana->inop->inode.iso_mtime;
3928876Srgrimes
3931541Srgrimes	}
3941541Srgrimes	ana->fields &= ~ISO_SUSP_TSTAMP;
3951541Srgrimes	return ISO_SUSP_TSTAMP;
3961541Srgrimes}
3971541Srgrimes
3981541Srgrimesstatic void
3991541Srgrimescd9660_rrip_deftstamp(isodir,ana)
4001541Srgrimes	struct iso_directory_record  *isodir;
4011541Srgrimes	ISO_RRIP_ANALYZE *ana;
4021541Srgrimes{
4035651Sjoerg	cd9660_deftstamp(isodir,ana->inop,NULL,ISO_FTYPE_RRIP);
4041541Srgrimes}
4051541Srgrimes
4061541Srgrimes/*
4071541Srgrimes * POSIX device modes
4081541Srgrimes */
4091541Srgrimesstatic int
4101541Srgrimescd9660_rrip_device(p,ana)
4111541Srgrimes	ISO_RRIP_DEVICE *p;
4121541Srgrimes	ISO_RRIP_ANALYZE *ana;
4131541Srgrimes{
41422521Sdyson	u_int high, low;
415131526Sphk
41622521Sdyson	high = isonum_733(p->dev_t_high);
41722521Sdyson	low  = isonum_733(p->dev_t_low);
418131526Sphk
41922521Sdyson	if (high == 0)
420187830Sed		ana->inop->inode.iso_rdev = makedev(major(low), minor(low));
42122521Sdyson	else
422187830Sed		ana->inop->inode.iso_rdev = makedev(high, minor(low));
4231541Srgrimes	ana->fields &= ~ISO_SUSP_DEVICE;
4241541Srgrimes	return ISO_SUSP_DEVICE;
4251541Srgrimes}
4261541Srgrimes
4271541Srgrimes/*
4281541Srgrimes * Flag indicating
4291541Srgrimes */
4301541Srgrimesstatic int
4311541Srgrimescd9660_rrip_idflag(p,ana)
4321541Srgrimes	ISO_RRIP_IDFLAG *p;
4331541Srgrimes	ISO_RRIP_ANALYZE *ana;
4341541Srgrimes{
4351541Srgrimes	ana->fields &= isonum_711(p->flags)|~0xff; /* don't touch high bits */
4361541Srgrimes	/* special handling of RE field */
4371541Srgrimes	if (ana->fields&ISO_SUSP_RELDIR)
43812597Sbde		return cd9660_rrip_reldir(/* XXX */ (ISO_RRIP_RELDIR *)p,ana);
4398876Srgrimes
4401541Srgrimes	return ISO_SUSP_IDFLAG;
4411541Srgrimes}
4421541Srgrimes
4431541Srgrimes/*
4441541Srgrimes * Continuation pointer
4451541Srgrimes */
4461541Srgrimesstatic int
4471541Srgrimescd9660_rrip_cont(p,ana)
4481541Srgrimes	ISO_RRIP_CONT *p;
4491541Srgrimes	ISO_RRIP_ANALYZE *ana;
4501541Srgrimes{
4511541Srgrimes	ana->iso_ce_blk = isonum_733(p->location);
4521541Srgrimes	ana->iso_ce_off = isonum_733(p->offset);
4531541Srgrimes	ana->iso_ce_len = isonum_733(p->length);
4541541Srgrimes	return ISO_SUSP_CONT;
4551541Srgrimes}
4561541Srgrimes
4571541Srgrimes/*
4581541Srgrimes * System Use end
4591541Srgrimes */
4601541Srgrimesstatic int
4611541Srgrimescd9660_rrip_stop(p,ana)
4621541Srgrimes	ISO_SUSP_HEADER *p;
4631541Srgrimes	ISO_RRIP_ANALYZE *ana;
4641541Srgrimes{
4651541Srgrimes	return ISO_SUSP_STOP;
4661541Srgrimes}
4671541Srgrimes
4681541Srgrimes/*
4691541Srgrimes * Extension reference
4701541Srgrimes */
4711541Srgrimesstatic int
4721541Srgrimescd9660_rrip_extref(p,ana)
4731541Srgrimes	ISO_RRIP_EXTREF *p;
4741541Srgrimes	ISO_RRIP_ANALYZE *ana;
4751541Srgrimes{
476185334Slulf	if ( ! ((isonum_711(p->len_id) == 10
477185334Slulf	      && bcmp((char *)p + 8,"RRIP_1991A",10) == 0)
478185334Slulf	    || (isonum_711(p->len_id) == 10
479185334Slulf	      && bcmp((char *)p + 8,"IEEE_P1282",10) == 0)
480185334Slulf	    || (isonum_711(p->len_id) ==  9
481185334Slulf	      && bcmp((char *)p + 8,"IEEE_1282",  9) == 0))
4821541Srgrimes	    || isonum_711(p->version) != 1)
4831541Srgrimes		return 0;
4841541Srgrimes	ana->fields &= ~ISO_SUSP_EXTREF;
4851541Srgrimes	return ISO_SUSP_EXTREF;
4861541Srgrimes}
4871541Srgrimes
4881541Srgrimesstatic int
4891541Srgrimescd9660_rrip_loop(isodir,ana,table)
4901541Srgrimes	struct iso_directory_record *isodir;
4911541Srgrimes	ISO_RRIP_ANALYZE *ana;
4921541Srgrimes	RRIP_TABLE *table;
4931541Srgrimes{
494131526Sphk	RRIP_TABLE *ptable;
495131526Sphk	ISO_SUSP_HEADER *phead;
496131526Sphk	ISO_SUSP_HEADER *pend;
4971541Srgrimes	struct buf *bp = NULL;
4981541Srgrimes	char *pwhead;
499120492Sfjoe	u_short c;
5001541Srgrimes	int result;
5018876Srgrimes
5021541Srgrimes	/*
5031541Srgrimes	 * Note: If name length is odd,
5045651Sjoerg	 *	 it will be padding 1 byte after the name
5051541Srgrimes	 */
5061541Srgrimes	pwhead = isodir->name + isonum_711(isodir->name_len);
5071541Srgrimes	if (!(isonum_711(isodir->name_len)&1))
5081541Srgrimes		pwhead++;
509120492Sfjoe	isochar(isodir->name, pwhead, ana->imp->joliet_level, &c, NULL,
510120492Sfjoe		ana->imp->im_flags, ana->imp->im_d2l);
5118876Srgrimes
5121541Srgrimes	/* If it's not the '.' entry of the root dir obey SP field */
51345773Sdcs	if (c != 0 || isonum_733(isodir->extent) != ana->imp->root_extent)
5141541Srgrimes		pwhead += ana->imp->rr_skip;
5151541Srgrimes	else
5161541Srgrimes		pwhead += ana->imp->rr_skip0;
5178876Srgrimes
5181541Srgrimes	phead = (ISO_SUSP_HEADER *)pwhead;
5191541Srgrimes	pend = (ISO_SUSP_HEADER *)((char *)isodir + isonum_711(isodir->length));
5208876Srgrimes
5211541Srgrimes	result = 0;
5221541Srgrimes	while (1) {
5231541Srgrimes		ana->iso_ce_len = 0;
5241541Srgrimes		/*
5251541Srgrimes		 * Note: "pend" should be more than one SUSP header
5268876Srgrimes		 */
5271541Srgrimes		while (pend >= phead + 1) {
5281541Srgrimes			if (isonum_711(phead->version) == 1) {
5291541Srgrimes				for (ptable = table; ptable->func; ptable++) {
5301541Srgrimes					if (*phead->type == *ptable->type
5311541Srgrimes					    && phead->type[1] == ptable->type[1]) {
5321541Srgrimes						result |= ptable->func(phead,ana);
5331541Srgrimes						break;
5341541Srgrimes					}
5351541Srgrimes				}
5361541Srgrimes				if (!ana->fields)
5371541Srgrimes					break;
5381541Srgrimes			}
53922521Sdyson			if (result&ISO_SUSP_STOP) {
54022521Sdyson				result &= ~ISO_SUSP_STOP;
54122521Sdyson				break;
54222521Sdyson			}
54322521Sdyson			/* plausibility check */
54422521Sdyson			if (isonum_711(phead->length) < sizeof(*phead))
54522521Sdyson				break;
5461541Srgrimes			/*
5471541Srgrimes			 * move to next SUSP
5481541Srgrimes			 * Hopefully this works with newer versions, too
5491541Srgrimes			 */
5501541Srgrimes			phead = (ISO_SUSP_HEADER *)((char *)phead + isonum_711(phead->length));
5511541Srgrimes		}
552131526Sphk
55322521Sdyson		if (ana->fields && ana->iso_ce_len) {
5541541Srgrimes			if (ana->iso_ce_blk >= ana->imp->volume_space_size
5551541Srgrimes			    || ana->iso_ce_off + ana->iso_ce_len > ana->imp->logical_block_size
5561541Srgrimes			    || bread(ana->imp->im_devvp,
55722521Sdyson				     ana->iso_ce_blk <<
55822521Sdyson				     (ana->imp->im_bshift - DEV_BSHIFT),
55922521Sdyson				     ana->imp->logical_block_size, NOCRED, &bp))
5601541Srgrimes				/* what to do now? */
5611541Srgrimes				break;
56222521Sdyson			phead = (ISO_SUSP_HEADER *)(bp->b_data + ana->iso_ce_off);
5631541Srgrimes			pend = (ISO_SUSP_HEADER *) ((char *)phead + ana->iso_ce_len);
5641541Srgrimes		} else
5651541Srgrimes			break;
5661541Srgrimes	}
5671541Srgrimes	if (bp)
5681541Srgrimes		brelse(bp);
5691541Srgrimes	/*
5701541Srgrimes	 * If we don't find the Basic SUSP stuffs, just set default value
57122521Sdyson	 *   (attribute/time stamp)
5721541Srgrimes	 */
5731541Srgrimes	for (ptable = table; ptable->func2; ptable++)
5741541Srgrimes		if (!(ptable->result&result))
5751541Srgrimes			ptable->func2(isodir,ana);
5768876Srgrimes
5771541Srgrimes	return result;
5781541Srgrimes}
5791541Srgrimes
58012597Sbde/*
58122521Sdyson * Get Attributes.
58222521Sdyson */
58322521Sdyson/*
58412597Sbde * XXX the casts are bogus but will do for now.
58512597Sbde */
58612597Sbde#define	BC	(rrt_func_t *)
5871541Srgrimesstatic RRIP_TABLE rrip_table_analyze[] = {
58812597Sbde	{ "PX", BC cd9660_rrip_attr,	cd9660_rrip_defattr,	ISO_SUSP_ATTR },
58912597Sbde	{ "TF", BC cd9660_rrip_tstamp,	cd9660_rrip_deftstamp,	ISO_SUSP_TSTAMP },
59012597Sbde	{ "PN", BC cd9660_rrip_device,	0,			ISO_SUSP_DEVICE },
59112597Sbde	{ "RR", BC cd9660_rrip_idflag,	0,			ISO_SUSP_IDFLAG },
59212597Sbde	{ "CE", BC cd9660_rrip_cont,	0,			ISO_SUSP_CONT },
59312597Sbde	{ "ST", BC cd9660_rrip_stop,	0,			ISO_SUSP_STOP },
5941541Srgrimes	{ "",	0,			0,			0 }
5951541Srgrimes};
5961541Srgrimes
5971541Srgrimesint
5981541Srgrimescd9660_rrip_analyze(isodir,inop,imp)
5991541Srgrimes	struct iso_directory_record *isodir;
6001541Srgrimes	struct iso_node *inop;
6011541Srgrimes	struct iso_mnt *imp;
6021541Srgrimes{
6031541Srgrimes	ISO_RRIP_ANALYZE analyze;
6048876Srgrimes
6051541Srgrimes	analyze.inop = inop;
6061541Srgrimes	analyze.imp = imp;
6071541Srgrimes	analyze.fields = ISO_SUSP_ATTR|ISO_SUSP_TSTAMP|ISO_SUSP_DEVICE;
6088876Srgrimes
6091541Srgrimes	return cd9660_rrip_loop(isodir,&analyze,rrip_table_analyze);
6101541Srgrimes}
6111541Srgrimes
612131526Sphk/*
61322521Sdyson * Get Alternate Name.
6141541Srgrimes */
6151541Srgrimesstatic RRIP_TABLE rrip_table_getname[] = {
61612597Sbde	{ "NM", BC cd9660_rrip_altname,	cd9660_rrip_defname,	ISO_SUSP_ALTNAME },
61712597Sbde	{ "CL", BC cd9660_rrip_pclink,	0,			ISO_SUSP_CLINK|ISO_SUSP_PLINK },
61812597Sbde	{ "PL", BC cd9660_rrip_pclink,	0,			ISO_SUSP_CLINK|ISO_SUSP_PLINK },
61912597Sbde	{ "RE", BC cd9660_rrip_reldir,	0,			ISO_SUSP_RELDIR },
62012597Sbde	{ "RR", BC cd9660_rrip_idflag,	0,			ISO_SUSP_IDFLAG },
62112597Sbde	{ "CE", BC cd9660_rrip_cont,	0,			ISO_SUSP_CONT },
62212597Sbde	{ "ST", BC cd9660_rrip_stop,	0,			ISO_SUSP_STOP },
6231541Srgrimes	{ "",	0,			0,			0 }
6241541Srgrimes};
6251541Srgrimes
6261541Srgrimesint
6271541Srgrimescd9660_rrip_getname(isodir,outbuf,outlen,inump,imp)
6281541Srgrimes	struct iso_directory_record *isodir;
6291541Srgrimes	char *outbuf;
6301541Srgrimes	u_short *outlen;
6311541Srgrimes	ino_t *inump;
6321541Srgrimes	struct iso_mnt *imp;
6331541Srgrimes{
6341541Srgrimes	ISO_RRIP_ANALYZE analyze;
6351541Srgrimes	RRIP_TABLE *tab;
636120492Sfjoe	u_short c;
6378876Srgrimes
6381541Srgrimes	analyze.outbuf = outbuf;
6391541Srgrimes	analyze.outlen = outlen;
6401541Srgrimes	analyze.maxlen = NAME_MAX;
6411541Srgrimes	analyze.inump = inump;
6421541Srgrimes	analyze.imp = imp;
6431541Srgrimes	analyze.fields = ISO_SUSP_ALTNAME|ISO_SUSP_RELDIR|ISO_SUSP_CLINK|ISO_SUSP_PLINK;
6441541Srgrimes	*outlen = 0;
6458876Srgrimes
64645773Sdcs	isochar(isodir->name, isodir->name + isonum_711(isodir->name_len),
647120492Sfjoe		imp->joliet_level, &c, NULL, imp->im_flags, imp->im_d2l);
6481541Srgrimes	tab = rrip_table_getname;
64945773Sdcs	if (c == 0 || c == 1) {
6501541Srgrimes		cd9660_rrip_defname(isodir,&analyze);
6518876Srgrimes
6521541Srgrimes		analyze.fields &= ~ISO_SUSP_ALTNAME;
6531541Srgrimes		tab++;
6541541Srgrimes	}
6558876Srgrimes
6561541Srgrimes	return cd9660_rrip_loop(isodir,&analyze,tab);
6571541Srgrimes}
6581541Srgrimes
659131526Sphk/*
66022521Sdyson * Get Symbolic Link.
6611541Srgrimes */
6621541Srgrimesstatic RRIP_TABLE rrip_table_getsymname[] = {
66312597Sbde	{ "SL", BC cd9660_rrip_slink,	0,			ISO_SUSP_SLINK },
66412597Sbde	{ "RR", BC cd9660_rrip_idflag,	0,			ISO_SUSP_IDFLAG },
66512597Sbde	{ "CE", BC cd9660_rrip_cont,	0,			ISO_SUSP_CONT },
66612597Sbde	{ "ST", BC cd9660_rrip_stop,	0,			ISO_SUSP_STOP },
6671541Srgrimes	{ "",	0,			0,			0 }
6681541Srgrimes};
6691541Srgrimes
6701541Srgrimesint
6711541Srgrimescd9660_rrip_getsymname(isodir,outbuf,outlen,imp)
6721541Srgrimes	struct iso_directory_record *isodir;
6731541Srgrimes	char *outbuf;
6741541Srgrimes	u_short *outlen;
6751541Srgrimes	struct iso_mnt *imp;
6761541Srgrimes{
6771541Srgrimes	ISO_RRIP_ANALYZE analyze;
6788876Srgrimes
6791541Srgrimes	analyze.outbuf = outbuf;
6801541Srgrimes	analyze.outlen = outlen;
6811541Srgrimes	*outlen = 0;
6821541Srgrimes	analyze.maxlen = MAXPATHLEN;
6831541Srgrimes	analyze.cont = 1;		/* don't start with a slash */
6841541Srgrimes	analyze.imp = imp;
6851541Srgrimes	analyze.fields = ISO_SUSP_SLINK;
6868876Srgrimes
6871541Srgrimes	return (cd9660_rrip_loop(isodir,&analyze,rrip_table_getsymname)&ISO_SUSP_SLINK);
6881541Srgrimes}
6891541Srgrimes
6901541Srgrimesstatic RRIP_TABLE rrip_table_extref[] = {
69112597Sbde	{ "ER", BC cd9660_rrip_extref,	0,			ISO_SUSP_EXTREF },
69212597Sbde	{ "CE", BC cd9660_rrip_cont,	0,			ISO_SUSP_CONT },
69312597Sbde	{ "ST", BC cd9660_rrip_stop,	0,			ISO_SUSP_STOP },
6941541Srgrimes	{ "",	0,			0,			0 }
6951541Srgrimes};
6961541Srgrimes
6971541Srgrimes/*
6981541Srgrimes * Check for Rock Ridge Extension and return offset of its fields.
69922521Sdyson * Note: We insist on the ER field.
7001541Srgrimes */
7011541Srgrimesint
7021541Srgrimescd9660_rrip_offset(isodir,imp)
7031541Srgrimes	struct iso_directory_record *isodir;
7041541Srgrimes	struct iso_mnt *imp;
7051541Srgrimes{
7061541Srgrimes	ISO_RRIP_OFFSET *p;
7071541Srgrimes	ISO_RRIP_ANALYZE analyze;
7088876Srgrimes
7091541Srgrimes	imp->rr_skip0 = 0;
7101541Srgrimes	p = (ISO_RRIP_OFFSET *)(isodir->name + 1);
7111541Srgrimes	if (bcmp(p,"SP\7\1\276\357",6)) {
7121541Srgrimes		/* Maybe, it's a CDROM XA disc? */
7131541Srgrimes		imp->rr_skip0 = 15;
7141541Srgrimes		p = (ISO_RRIP_OFFSET *)((char *)p + 15);
7151541Srgrimes		if (bcmp(p,"SP\7\1\276\357",6))
7161541Srgrimes			return -1;
7171541Srgrimes	}
7188876Srgrimes
7191541Srgrimes	analyze.imp = imp;
7201541Srgrimes	analyze.fields = ISO_SUSP_EXTREF;
7211541Srgrimes	if (!(cd9660_rrip_loop(isodir,&analyze,rrip_table_extref)&ISO_SUSP_EXTREF))
7221541Srgrimes		return -1;
7238876Srgrimes
7241541Srgrimes	return isonum_711(p->skip);
7251541Srgrimes}
726