7e3716deaacfe7ad101d0d8124424dae6a4f9037
[dragonfly.git] / sys / vfs / isofs / cd9660 / cd9660_rrip.c
1 /*-
2  * Copyright (c) 1993, 1994
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley
6  * by Pace Willisson (pace@blitz.com).  The Rock Ridge Extension
7  * Support code is derived from software contributed to Berkeley
8  * by Atsushi Murai (amurai@spec.co.jp).
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *      This product includes software developed by the University of
21  *      California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *      @(#)cd9660_rrip.c       8.6 (Berkeley) 12/5/94
39  * $FreeBSD: src/sys/isofs/cd9660/cd9660_rrip.c,v 1.17 1999/08/28 00:46:06 peter Exp $
40  * $DragonFly: src/sys/vfs/isofs/cd9660/cd9660_rrip.c,v 1.8 2005/02/02 21:34:18 joerg Exp $
41  */
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/buf.h>
46 #include <sys/vnode.h>
47 #include <sys/mount.h>
48 #include <sys/kernel.h>
49
50 #include "iso.h"
51 #include "cd9660_node.h"
52 #include "cd9660_rrip.h"
53 #include "iso_rrip.h"
54
55 typedef int     rrt_func_t (void *, ISO_RRIP_ANALYZE *ana);
56
57 typedef struct {
58         char type[2];
59         rrt_func_t *func;
60         void (*func2) (struct iso_directory_record *isodir, ISO_RRIP_ANALYZE *ana);
61         int result;
62 } RRIP_TABLE;
63
64 static int      cd9660_rrip_altname (ISO_RRIP_ALTNAME *p,
65                                          ISO_RRIP_ANALYZE *ana);
66 static int      cd9660_rrip_attr (ISO_RRIP_ATTR *p, ISO_RRIP_ANALYZE *ana);
67 static int      cd9660_rrip_cont (ISO_RRIP_CONT *p, ISO_RRIP_ANALYZE *ana);
68 static void     cd9660_rrip_defattr (struct iso_directory_record *isodir,
69                                          ISO_RRIP_ANALYZE *ana);
70 static void     cd9660_rrip_defname (struct iso_directory_record *isodir,
71                                          ISO_RRIP_ANALYZE *ana);
72 static void     cd9660_rrip_deftstamp (struct iso_directory_record *isodir,
73                                            ISO_RRIP_ANALYZE *ana);
74 static int      cd9660_rrip_device (ISO_RRIP_DEVICE *p,
75                                         ISO_RRIP_ANALYZE *ana);
76 static int      cd9660_rrip_extref (ISO_RRIP_EXTREF *p,
77                                         ISO_RRIP_ANALYZE *ana);
78 static int      cd9660_rrip_idflag (ISO_RRIP_IDFLAG *p,
79                                         ISO_RRIP_ANALYZE *ana);
80 static int      cd9660_rrip_loop (struct iso_directory_record *isodir,
81                                       ISO_RRIP_ANALYZE *ana,
82                                       RRIP_TABLE *table);
83 static int      cd9660_rrip_pclink (ISO_RRIP_CLINK *p,
84                                         ISO_RRIP_ANALYZE *ana);
85 static int      cd9660_rrip_reldir (ISO_RRIP_RELDIR *p,
86                                         ISO_RRIP_ANALYZE *ana);
87 static int      cd9660_rrip_slink (ISO_RRIP_SLINK *p,
88                                        ISO_RRIP_ANALYZE *ana);
89 static int      cd9660_rrip_stop (ISO_SUSP_HEADER *p,
90                                       ISO_RRIP_ANALYZE *ana);
91 static int      cd9660_rrip_tstamp (ISO_RRIP_TSTAMP *p,
92                                         ISO_RRIP_ANALYZE *ana);
93
94 /*
95  * POSIX file attribute
96  */
97 static int
98 cd9660_rrip_attr(ISO_RRIP_ATTR *p, ISO_RRIP_ANALYZE *ana)
99 {
100         ana->inop->inode.iso_mode = isonum_733(p->mode);
101         ana->inop->inode.iso_uid = isonum_733(p->uid);
102         ana->inop->inode.iso_gid = isonum_733(p->gid);
103         ana->inop->inode.iso_links = isonum_733(p->links);
104         ana->fields &= ~ISO_SUSP_ATTR;
105         return ISO_SUSP_ATTR;
106 }
107
108 static void
109 cd9660_rrip_defattr(struct iso_directory_record *isodir, ISO_RRIP_ANALYZE *ana)
110 {
111         /* But this is a required field! */
112         printf("RRIP without PX field?\n");
113         cd9660_defattr(isodir,ana->inop,NULL,ISO_FTYPE_RRIP);
114 }
115
116 /*
117  * Symbolic Links
118  */
119 static int
120 cd9660_rrip_slink(ISO_RRIP_SLINK *p, ISO_RRIP_ANALYZE *ana)
121 {
122         ISO_RRIP_SLINK_COMPONENT *pcomp;
123         ISO_RRIP_SLINK_COMPONENT *pcompe;
124         int error, len, wlen, cont;
125         char *outbuf, *inbuf, *freebuf;
126
127         pcomp = (ISO_RRIP_SLINK_COMPONENT *)p->component;
128         pcompe = (ISO_RRIP_SLINK_COMPONENT *)((char *)p + isonum_711(p->h.length));
129         len = *ana->outlen;
130         outbuf = ana->outbuf;
131         cont = ana->cont;
132         freebuf = NULL;
133
134         /*
135          * Gathering a Symbolic name from each component with path
136          */
137         for (;
138              pcomp < pcompe;
139              pcomp = (ISO_RRIP_SLINK_COMPONENT *)((char *)pcomp + ISO_RRIP_SLSIZ
140                                                   + isonum_711(pcomp->clen))) {
141
142                 if (!cont) {
143                         if (len < ana->maxlen) {
144                                 len++;
145                                 *outbuf++ = '/';
146                         }
147                 }
148                 cont = 0;
149
150                 inbuf = "..";
151                 wlen = 0;
152
153                 switch (*pcomp->cflag) {
154
155                 case ISO_SUSP_CFLAG_CURRENT:
156                         /* Inserting Current */
157                         wlen = 1;
158                         break;
159
160                 case ISO_SUSP_CFLAG_PARENT:
161                         /* Inserting Parent */
162                         wlen = 2;
163                         break;
164
165                 case ISO_SUSP_CFLAG_ROOT:
166                         /* Inserting slash for ROOT */
167                         /* start over from beginning(?) */
168                         outbuf -= len;
169                         len = 0;
170                         break;
171
172                 case ISO_SUSP_CFLAG_VOLROOT:
173                         /* Inserting a mount point i.e. "/cdrom" */
174                         /* same as above */
175                         outbuf -= len;
176                         len = 0;
177                         error = cache_fullpath(NULL, ana->imp->im_mountp->mnt_ncp,
178                                                &inbuf, &freebuf);
179                         if (error)
180                                 goto bad;
181                         wlen = strlen(inbuf);
182                         break;
183
184                 case ISO_SUSP_CFLAG_HOST:
185                         /* Inserting hostname i.e. "kurt.tools.de" */
186                         inbuf = hostname;
187                         wlen = strlen(hostname);
188                         break;
189
190                 case ISO_SUSP_CFLAG_CONTINUE:
191                         cont = 1;
192                         /* fall thru */
193                 case 0:
194                         /* Inserting component */
195                         wlen = isonum_711(pcomp->clen);
196                         inbuf = pcomp->name;
197                         break;
198                 default:
199                         printf("RRIP with incorrect flags?");
200                         wlen = ana->maxlen + 1;
201                         break;
202                 }
203
204                 if (len + wlen > ana->maxlen)
205                         goto bad;
206
207                 bcopy(inbuf,outbuf,wlen);
208                 outbuf += wlen;
209                 len += wlen;
210
211                 if (freebuf != NULL) {
212                         free(freebuf, M_TEMP);
213                         freebuf = NULL;
214                 }
215         }
216         ana->outbuf = outbuf;
217         *ana->outlen = len;
218         ana->cont = cont;
219
220         if (!isonum_711(p->flags)) {
221                 ana->fields &= ~ISO_SUSP_SLINK;
222                 return ISO_SUSP_SLINK;
223         }
224         return 0;
225
226 bad:
227         ana->cont = 1;
228         ana->fields = 0;
229         ana->outbuf -= *ana->outlen;
230         *ana->outlen = 0;
231         return 0;
232 }
233
234 /*
235  * Alternate name
236  */
237 static int
238 cd9660_rrip_altname(ISO_RRIP_ALTNAME *p, ISO_RRIP_ANALYZE *ana)
239 {
240         char *inbuf;
241         int wlen;
242         int cont;
243
244         inbuf = "..";
245         wlen = 0;
246         cont = 0;
247
248         switch (*p->flags) {
249         case ISO_SUSP_CFLAG_CURRENT:
250                 /* Inserting Current */
251                 wlen = 1;
252                 break;
253
254         case ISO_SUSP_CFLAG_PARENT:
255                 /* Inserting Parent */
256                 wlen = 2;
257                 break;
258
259         case ISO_SUSP_CFLAG_HOST:
260                 /* Inserting hostname i.e. "kurt.tools.de" */
261                 inbuf = hostname;
262                 wlen = strlen(hostname);
263                 break;
264
265         case ISO_SUSP_CFLAG_CONTINUE:
266                 cont = 1;
267                 /* fall thru */
268         case 0:
269                 /* Inserting component */
270                 wlen = isonum_711(p->h.length) - 5;
271                 inbuf = (char *)p + 5;
272                 break;
273
274         default:
275                 printf("RRIP with incorrect NM flags?\n");
276                 wlen = ana->maxlen + 1;
277                 break;
278         }
279
280         if ((*ana->outlen += wlen) > ana->maxlen) {
281                 /* treat as no name field */
282                 ana->fields &= ~ISO_SUSP_ALTNAME;
283                 ana->outbuf -= *ana->outlen - wlen;
284                 *ana->outlen = 0;
285                 return 0;
286         }
287
288         bcopy(inbuf,ana->outbuf,wlen);
289         ana->outbuf += wlen;
290
291         if (!cont) {
292                 ana->fields &= ~ISO_SUSP_ALTNAME;
293                 return ISO_SUSP_ALTNAME;
294         }
295         return 0;
296 }
297
298 static void
299 cd9660_rrip_defname(struct iso_directory_record *isodir, ISO_RRIP_ANALYZE *ana)
300 {
301         isofntrans(isodir->name,isonum_711(isodir->name_len),
302                    ana->outbuf,ana->outlen,
303                    1,isonum_711(isodir->flags)&4, ana->imp->joliet_level);
304         switch (*ana->outbuf) {
305         default:
306                 break;
307         case 1:
308                 *ana->outlen = 2;
309                 /* FALL THROUGH */
310         case 0:
311                 /* outlen is 1 already */
312                 strcpy(ana->outbuf,"..");
313                 break;
314         }
315 }
316
317 /*
318  * Parent or Child Link
319  */
320 static int
321 cd9660_rrip_pclink(ISO_RRIP_CLINK *p, ISO_RRIP_ANALYZE *ana)
322 {
323         *ana->inump = isonum_733(p->dir_loc) << ana->imp->im_bshift;
324         ana->fields &= ~(ISO_SUSP_CLINK|ISO_SUSP_PLINK);
325         return *p->h.type == 'C' ? ISO_SUSP_CLINK : ISO_SUSP_PLINK;
326 }
327
328 /*
329  * Relocated directory
330  */
331 static int
332 cd9660_rrip_reldir(ISO_RRIP_RELDIR *p, ISO_RRIP_ANALYZE *ana)
333 {
334         /* special hack to make caller aware of RE field */
335         *ana->outlen = 0;
336         ana->fields = 0;
337         return ISO_SUSP_RELDIR|ISO_SUSP_ALTNAME|ISO_SUSP_CLINK|ISO_SUSP_PLINK;
338 }
339
340 static int
341 cd9660_rrip_tstamp(ISO_RRIP_TSTAMP *p, ISO_RRIP_ANALYZE *ana)
342 {
343         u_char *ptime;
344         
345         ptime = p->time;
346
347         /* Check a format of time stamp (7bytes/17bytes) */
348         if (!(*p->flags&ISO_SUSP_TSTAMP_FORM17)) {
349                 if (*p->flags&ISO_SUSP_TSTAMP_CREAT)
350                         ptime += 7;
351
352                 if (*p->flags&ISO_SUSP_TSTAMP_MODIFY) {
353                         cd9660_tstamp_conv7(ptime,&ana->inop->inode.iso_mtime,
354                                             ISO_FTYPE_RRIP);
355                         ptime += 7;
356                 } else
357                         bzero(&ana->inop->inode.iso_mtime,sizeof(struct timespec));
358                 
359                 if (*p->flags&ISO_SUSP_TSTAMP_ACCESS) {
360                         cd9660_tstamp_conv7(ptime,&ana->inop->inode.iso_atime,
361                                             ISO_FTYPE_RRIP);
362                         ptime += 7;
363                 } else
364                         ana->inop->inode.iso_atime = ana->inop->inode.iso_mtime;
365
366                 if (*p->flags&ISO_SUSP_TSTAMP_ATTR)
367                         cd9660_tstamp_conv7(ptime,&ana->inop->inode.iso_ctime,
368                                             ISO_FTYPE_RRIP);
369                 else
370                         ana->inop->inode.iso_ctime = ana->inop->inode.iso_mtime;
371
372         } else {
373                 if (*p->flags&ISO_SUSP_TSTAMP_CREAT)
374                         ptime += 17;
375
376                 if (*p->flags&ISO_SUSP_TSTAMP_MODIFY) {
377                         cd9660_tstamp_conv17(ptime,&ana->inop->inode.iso_mtime);
378                         ptime += 17;
379                 } else
380                         bzero(&ana->inop->inode.iso_mtime,sizeof(struct timespec));
381                 
382                 if (*p->flags&ISO_SUSP_TSTAMP_ACCESS) {
383                         cd9660_tstamp_conv17(ptime,&ana->inop->inode.iso_atime);
384                         ptime += 17;
385                 } else
386                         ana->inop->inode.iso_atime = ana->inop->inode.iso_mtime;
387
388                 if (*p->flags&ISO_SUSP_TSTAMP_ATTR)
389                         cd9660_tstamp_conv17(ptime,&ana->inop->inode.iso_ctime);
390                 else
391                         ana->inop->inode.iso_ctime = ana->inop->inode.iso_mtime;
392
393         }
394         ana->fields &= ~ISO_SUSP_TSTAMP;
395         return ISO_SUSP_TSTAMP;
396 }
397
398 static void
399 cd9660_rrip_deftstamp(struct iso_directory_record *isodir, ISO_RRIP_ANALYZE *ana)
400 {
401         cd9660_deftstamp(isodir,ana->inop,NULL,ISO_FTYPE_RRIP);
402 }
403
404 /*
405  * POSIX device modes
406  */
407 static int
408 cd9660_rrip_device(ISO_RRIP_DEVICE *p, ISO_RRIP_ANALYZE *ana)
409 {
410         u_int high, low;
411         
412         high = isonum_733(p->dev_t_high);
413         low  = isonum_733(p->dev_t_low);
414         
415         if (high == 0)
416                 ana->inop->inode.iso_rdev = makeudev(umajor(low), uminor(low));
417         else
418                 ana->inop->inode.iso_rdev = makeudev(high, uminor(low));
419         ana->fields &= ~ISO_SUSP_DEVICE;
420         return ISO_SUSP_DEVICE;
421 }
422
423 /*
424  * Flag indicating
425  */
426 static int
427 cd9660_rrip_idflag(ISO_RRIP_IDFLAG *p, ISO_RRIP_ANALYZE *ana)
428 {
429         ana->fields &= isonum_711(p->flags)|~0xff; /* don't touch high bits */
430         /* special handling of RE field */
431         if (ana->fields&ISO_SUSP_RELDIR)
432                 return cd9660_rrip_reldir(/* XXX */ (ISO_RRIP_RELDIR *)p,ana);
433
434         return ISO_SUSP_IDFLAG;
435 }
436
437 /*
438  * Continuation pointer
439  */
440 static int
441 cd9660_rrip_cont(ISO_RRIP_CONT *p, ISO_RRIP_ANALYZE *ana)
442 {
443         ana->iso_ce_blk = isonum_733(p->location);
444         ana->iso_ce_off = isonum_733(p->offset);
445         ana->iso_ce_len = isonum_733(p->length);
446         return ISO_SUSP_CONT;
447 }
448
449 /*
450  * System Use end
451  */
452 static int
453 cd9660_rrip_stop(ISO_SUSP_HEADER *p, ISO_RRIP_ANALYZE *ana)
454 {
455         return ISO_SUSP_STOP;
456 }
457
458 /*
459  * Extension reference
460  */
461 static int
462 cd9660_rrip_extref(ISO_RRIP_EXTREF *p, ISO_RRIP_ANALYZE *ana)
463 {
464         if (isonum_711(p->len_id) != 10
465             || bcmp((char *)p + 8,"RRIP_1991A",10)
466             || isonum_711(p->version) != 1)
467                 return 0;
468         ana->fields &= ~ISO_SUSP_EXTREF;
469         return ISO_SUSP_EXTREF;
470 }
471
472 static int
473 cd9660_rrip_loop(struct iso_directory_record *isodir, ISO_RRIP_ANALYZE *ana,
474                  RRIP_TABLE *table)
475 {
476         RRIP_TABLE *ptable;
477         ISO_SUSP_HEADER *phead;
478         ISO_SUSP_HEADER *pend;
479         struct buf *bp = NULL;
480         char *pwhead;
481         u_char c;
482         int result;
483
484         /*
485          * Note: If name length is odd,
486          *       it will be padding 1 byte after the name
487          */
488         pwhead = isodir->name + isonum_711(isodir->name_len);
489         if (!(isonum_711(isodir->name_len)&1))
490                 pwhead++;
491         isochar(isodir->name, pwhead, ana->imp->joliet_level, &c);
492
493         /* If it's not the '.' entry of the root dir obey SP field */
494         if (c != 0 || isonum_733(isodir->extent) != ana->imp->root_extent)
495                 pwhead += ana->imp->rr_skip;
496         else
497                 pwhead += ana->imp->rr_skip0;
498
499         phead = (ISO_SUSP_HEADER *)pwhead;
500         pend = (ISO_SUSP_HEADER *)((char *)isodir + isonum_711(isodir->length));
501
502         result = 0;
503         while (1) {
504                 ana->iso_ce_len = 0;
505                 /*
506                  * Note: "pend" should be more than one SUSP header
507                  */
508                 while (pend >= phead + 1) {
509                         if (isonum_711(phead->version) == 1) {
510                                 for (ptable = table; ptable->func; ptable++) {
511                                         if (*phead->type == *ptable->type
512                                             && phead->type[1] == ptable->type[1]) {
513                                                 result |= ptable->func(phead,ana);
514                                                 break;
515                                         }
516                                 }
517                                 if (!ana->fields)
518                                         break;
519                         }
520                         if (result&ISO_SUSP_STOP) {
521                                 result &= ~ISO_SUSP_STOP;
522                                 break;
523                         }
524                         /* plausibility check */
525                         if (isonum_711(phead->length) < sizeof(*phead))
526                                 break;
527                         /*
528                          * move to next SUSP
529                          * Hopefully this works with newer versions, too
530                          */
531                         phead = (ISO_SUSP_HEADER *)((char *)phead + isonum_711(phead->length));
532                 }
533                 
534                 if (ana->fields && ana->iso_ce_len) {
535                         if (ana->iso_ce_blk >= ana->imp->volume_space_size
536                             || ana->iso_ce_off + ana->iso_ce_len > ana->imp->logical_block_size
537                             || bread(ana->imp->im_devvp,
538                                      ana->iso_ce_blk <<
539                                      (ana->imp->im_bshift - DEV_BSHIFT),
540                                      ana->imp->logical_block_size, &bp))
541                                 /* what to do now? */
542                                 break;
543                         phead = (ISO_SUSP_HEADER *)(bp->b_data + ana->iso_ce_off);
544                         pend = (ISO_SUSP_HEADER *) ((char *)phead + ana->iso_ce_len);
545                 } else
546                         break;
547         }
548         if (bp)
549                 brelse(bp);
550         /*
551          * If we don't find the Basic SUSP stuffs, just set default value
552          *   (attribute/time stamp)
553          */
554         for (ptable = table; ptable->func2; ptable++)
555                 if (!(ptable->result&result))
556                         ptable->func2(isodir,ana);
557
558         return result;
559 }
560
561 /*
562  * Get Attributes.
563  */
564 /*
565  * XXX the casts are bogus but will do for now.
566  */
567 #define BC      (rrt_func_t *)
568 static RRIP_TABLE rrip_table_analyze[] = {
569         { "PX", BC cd9660_rrip_attr,    cd9660_rrip_defattr,    ISO_SUSP_ATTR },
570         { "TF", BC cd9660_rrip_tstamp,  cd9660_rrip_deftstamp,  ISO_SUSP_TSTAMP },
571         { "PN", BC cd9660_rrip_device,  0,                      ISO_SUSP_DEVICE },
572         { "RR", BC cd9660_rrip_idflag,  0,                      ISO_SUSP_IDFLAG },
573         { "CE", BC cd9660_rrip_cont,    0,                      ISO_SUSP_CONT },
574         { "ST", BC cd9660_rrip_stop,    0,                      ISO_SUSP_STOP },
575         { "",   0,                      0,                      0 }
576 };
577
578 int
579 cd9660_rrip_analyze(struct iso_directory_record *isodir, struct iso_node *inop,
580                     struct iso_mnt *imp)
581 {
582         ISO_RRIP_ANALYZE analyze;
583
584         analyze.inop = inop;
585         analyze.imp = imp;
586         analyze.fields = ISO_SUSP_ATTR|ISO_SUSP_TSTAMP|ISO_SUSP_DEVICE;
587
588         return cd9660_rrip_loop(isodir,&analyze,rrip_table_analyze);
589 }
590
591 /* 
592  * Get Alternate Name.
593  */
594 static RRIP_TABLE rrip_table_getname[] = {
595         { "NM", BC cd9660_rrip_altname, cd9660_rrip_defname,    ISO_SUSP_ALTNAME },
596         { "CL", BC cd9660_rrip_pclink,  0,                      ISO_SUSP_CLINK|ISO_SUSP_PLINK },
597         { "PL", BC cd9660_rrip_pclink,  0,                      ISO_SUSP_CLINK|ISO_SUSP_PLINK },
598         { "RE", BC cd9660_rrip_reldir,  0,                      ISO_SUSP_RELDIR },
599         { "RR", BC cd9660_rrip_idflag,  0,                      ISO_SUSP_IDFLAG },
600         { "CE", BC cd9660_rrip_cont,    0,                      ISO_SUSP_CONT },
601         { "ST", BC cd9660_rrip_stop,    0,                      ISO_SUSP_STOP },
602         { "",   0,                      0,                      0 }
603 };
604
605 int
606 cd9660_rrip_getname(struct iso_directory_record *isodir, char *outbuf,
607                     u_short *outlen, ino_t *inump, struct iso_mnt *imp)
608 {
609         ISO_RRIP_ANALYZE analyze;
610         RRIP_TABLE *tab;
611         u_char c;
612
613         analyze.outbuf = outbuf;
614         analyze.outlen = outlen;
615         analyze.maxlen = NAME_MAX;
616         analyze.inump = inump;
617         analyze.imp = imp;
618         analyze.fields = ISO_SUSP_ALTNAME|ISO_SUSP_RELDIR|ISO_SUSP_CLINK|ISO_SUSP_PLINK;
619         *outlen = 0;
620
621         isochar(isodir->name, isodir->name + isonum_711(isodir->name_len),
622                 imp->joliet_level, &c);
623         tab = rrip_table_getname;
624         if (c == 0 || c == 1) {
625                 cd9660_rrip_defname(isodir,&analyze);
626
627                 analyze.fields &= ~ISO_SUSP_ALTNAME;
628                 tab++;
629         }
630
631         return cd9660_rrip_loop(isodir,&analyze,tab);
632 }
633
634 /* 
635  * Get Symbolic Link.
636  */
637 static RRIP_TABLE rrip_table_getsymname[] = {
638         { "SL", BC cd9660_rrip_slink,   0,                      ISO_SUSP_SLINK },
639         { "RR", BC cd9660_rrip_idflag,  0,                      ISO_SUSP_IDFLAG },
640         { "CE", BC cd9660_rrip_cont,    0,                      ISO_SUSP_CONT },
641         { "ST", BC cd9660_rrip_stop,    0,                      ISO_SUSP_STOP },
642         { "",   0,                      0,                      0 }
643 };
644
645 int
646 cd9660_rrip_getsymname(struct iso_directory_record *isodir, char *outbuf,
647                        u_short *outlen, struct iso_mnt *imp)
648 {
649         ISO_RRIP_ANALYZE analyze;
650
651         analyze.outbuf = outbuf;
652         analyze.outlen = outlen;
653         *outlen = 0;
654         analyze.maxlen = MAXPATHLEN;
655         analyze.cont = 1;               /* don't start with a slash */
656         analyze.imp = imp;
657         analyze.fields = ISO_SUSP_SLINK;
658
659         return (cd9660_rrip_loop(isodir,&analyze,rrip_table_getsymname)&ISO_SUSP_SLINK);
660 }
661
662 static RRIP_TABLE rrip_table_extref[] = {
663         { "ER", BC cd9660_rrip_extref,  0,                      ISO_SUSP_EXTREF },
664         { "CE", BC cd9660_rrip_cont,    0,                      ISO_SUSP_CONT },
665         { "ST", BC cd9660_rrip_stop,    0,                      ISO_SUSP_STOP },
666         { "",   0,                      0,                      0 }
667 };
668
669 /*
670  * Check for Rock Ridge Extension and return offset of its fields.
671  * Note: We insist on the ER field.
672  */
673 int
674 cd9660_rrip_offset(struct iso_directory_record *isodir, struct iso_mnt *imp)
675 {
676         ISO_RRIP_OFFSET *p;
677         ISO_RRIP_ANALYZE analyze;
678
679         imp->rr_skip0 = 0;
680         p = (ISO_RRIP_OFFSET *)(isodir->name + 1);
681         if (bcmp(p,"SP\7\1\276\357",6)) {
682                 /* Maybe, it's a CDROM XA disc? */
683                 imp->rr_skip0 = 15;
684                 p = (ISO_RRIP_OFFSET *)((char *)p + 15);
685                 if (bcmp(p,"SP\7\1\276\357",6))
686                         return -1;
687         }
688
689         analyze.imp = imp;
690         analyze.fields = ISO_SUSP_EXTREF;
691         if (!(cd9660_rrip_loop(isodir,&analyze,rrip_table_extref)&ISO_SUSP_EXTREF))
692                 return -1;
693
694         return isonum_711(p->skip);
695 }