libstand - Fix CD9660 name matching code
[dragonfly.git] / lib / libstand / cd9660.c
1 /*
2  * Copyright (C) 1996 Wolfgang Solfrank.
3  * Copyright (C) 1996 TooLs GmbH.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
19  *
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.
30  *
31  * $NetBSD: cd9660.c,v 1.5 1997/06/26 19:11:33 drochner Exp $
32  * $FreeBSD: src/lib/libstand/cd9660.c,v 1.4.2.4 2001/12/21 22:17:44 jhb Exp $
33  * $DragonFly: src/lib/libstand/cd9660.c,v 1.6 2005/12/11 02:27:26 swildner Exp $
34  */
35
36 /*
37  * Stand-alone ISO9660 file reading package.
38  *
39  * Note: This doesn't support Rock Ridge extensions, extended attributes,
40  * blocksizes other than 2048 bytes, multi-extent files, etc.
41  */
42 #include <sys/param.h>
43 #include <string.h>
44 #include <sys/dirent.h>
45 #include <isofs/cd9660/iso.h>
46 #include <isofs/cd9660/cd9660_rrip.h>
47
48 #include "stand.h"
49
50 #define SUSP_CONTINUATION       "CE"
51 #define SUSP_PRESENT            "SP"
52 #define SUSP_STOP               "ST"
53 #define SUSP_EXTREF             "ER"
54 #define RRIP_NAME               "NM"
55
56 typedef struct {
57         ISO_SUSP_HEADER         h;
58         u_char signature        [ISODCL (  5,    6)];
59         u_char len_skp          [ISODCL (  7,    7)]; /* 711 */
60 } ISO_SUSP_PRESENT;
61         
62 static int      buf_read_file(struct open_file *f, char **buf_p,
63                     size_t *size_p);
64 static int      cd9660_open(const char *path, struct open_file *f);
65 static int      cd9660_close(struct open_file *f);
66 static int      cd9660_read(struct open_file *f, void *buf, size_t size,
67                     size_t *resid);
68 static int      cd9660_write(struct open_file *f, void *buf, size_t size,
69                     size_t *resid);
70 static off_t    cd9660_seek(struct open_file *f, off_t offset, int where);
71 static int      cd9660_stat(struct open_file *f, struct stat *sb);
72 static int      cd9660_readdir(struct open_file *f, struct dirent *d);
73 static int      dirmatch(struct open_file *f, const char *path,
74                     struct iso_directory_record *dp, int use_rrip, int lenskip);
75 static int      rrip_check(struct open_file *f, struct iso_directory_record *dp,
76                     int *lenskip);
77 static char     *rrip_lookup_name(struct open_file *f,
78                     struct iso_directory_record *dp, int lenskip, size_t *len);
79 static ISO_SUSP_HEADER *susp_lookup_record(struct open_file *f,
80                     const char *identifier, struct iso_directory_record *dp,
81                     int lenskip);
82
83 struct fs_ops cd9660_fsops = {
84         "cd9660",
85         cd9660_open,
86         cd9660_close,
87         cd9660_read,
88         cd9660_write,
89         cd9660_seek,
90         cd9660_stat,
91         cd9660_readdir
92 };
93
94 #define F_ISDIR         0x0001          /* Directory */
95 #define F_ROOTDIR       0x0002          /* Root directory */
96 #define F_RR            0x0004          /* Rock Ridge on this volume */
97
98 struct file {
99         int             f_flags;        /* file flags */
100         off_t           f_off;          /* Current offset within file */
101         daddr_t         f_bno;          /* Starting block number */
102         off_t           f_size;         /* Size of file */
103         daddr_t         f_buf_blkno;    /* block number of data block */        
104         char            *f_buf;         /* buffer for data block */
105         int             f_susp_skip;    /* len_skip for SUSP records */
106 };
107
108 struct ptable_ent {
109         char namlen     [ISODCL( 1, 1)];        /* 711 */
110         char extlen     [ISODCL( 2, 2)];        /* 711 */
111         char block      [ISODCL( 3, 6)];        /* 732 */
112         char parent     [ISODCL( 7, 8)];        /* 722 */
113         char name       [1];
114 };
115 #define PTFIXSZ         8
116 #define PTSIZE(pp)      roundup(PTFIXSZ + isonum_711((pp)->namlen), 2)
117
118 #define cdb2devb(bno)   ((bno) * ISO_DEFAULT_BLOCK_SIZE / DEV_BSIZE)
119
120 /* XXX these should be in the system headers */
121 static __inline int
122 isonum_722(u_char *p)
123 {
124         return (*p << 8)|p[1];
125 }
126
127 static __inline int
128 isonum_732(u_char *p)
129 {
130         return (*p << 24)|(p[1] << 16)|(p[2] << 8)|p[3];
131 }
132
133 static ISO_SUSP_HEADER *
134 susp_lookup_record(struct open_file *f, const char *identifier,
135     struct iso_directory_record *dp, int lenskip)
136 {
137         static char susp_buffer[ISO_DEFAULT_BLOCK_SIZE];
138         ISO_SUSP_HEADER *sh;
139         ISO_RRIP_CONT *shc;
140         char *p, *end;
141         int error;
142         size_t read;
143
144         p = dp->name + isonum_711(dp->name_len) + lenskip;
145         /* Names of even length have a padding byte after the name. */
146         if ((isonum_711(dp->name_len) & 1) == 0)
147                 p++;
148         end = (char *)dp + isonum_711(dp->length);
149         while (p + 3 < end) {
150                 sh = (ISO_SUSP_HEADER *)p;
151                 if (bcmp(sh->type, identifier, 2) == 0)
152                         return (sh);
153                 if (bcmp(sh->type, SUSP_STOP, 2) == 0)
154                         return (NULL);
155                 if (bcmp(sh->type, SUSP_CONTINUATION, 2) == 0) {
156                         shc = (ISO_RRIP_CONT *)sh;
157                         error = f->f_dev->dv_strategy(f->f_devdata, F_READ,
158                             cdb2devb(isonum_733(shc->location)),
159                             ISO_DEFAULT_BLOCK_SIZE, susp_buffer, &read);
160
161                         /* Bail if it fails. */
162                         if (error != 0 || read != ISO_DEFAULT_BLOCK_SIZE)
163                                 return (NULL);
164                         p = susp_buffer + isonum_733(shc->offset);
165                         end = p + isonum_733(shc->length);
166                 } else
167                         /* Ignore this record and skip to the next. */
168                         p += isonum_711(sh->length);
169         }
170         return (NULL);
171 }
172
173 static char *
174 rrip_lookup_name(struct open_file *f, struct iso_directory_record *dp,
175     int lenskip, size_t *len)
176 {
177         ISO_RRIP_ALTNAME *p;
178
179         if (len == NULL)
180                 return (NULL);
181
182         p = (ISO_RRIP_ALTNAME *)susp_lookup_record(f, RRIP_NAME, dp, lenskip);
183         if (p == NULL)
184                 return (NULL);
185         switch (*p->flags) {
186         case ISO_SUSP_CFLAG_CURRENT:
187                 *len = 1;
188                 return (".");
189         case ISO_SUSP_CFLAG_PARENT:
190                 *len = 2;
191                 return ("..");
192         case 0:
193                 *len = isonum_711(p->h.length) - 5;
194                 return ((char *)p + 5);
195         default:
196                 /*
197                  * We don't handle hostnames or continued names as they are
198                  * too hard, so just bail and use the default name.
199                  */
200                 return (NULL);
201         }
202 }
203
204 static int
205 rrip_check(struct open_file *f, struct iso_directory_record *dp, int *lenskip)
206 {
207         ISO_SUSP_PRESENT *sp;
208         ISO_RRIP_EXTREF *er;
209         char *p;
210
211         /* First, see if we can find a SP field. */
212         p = dp->name + isonum_711(dp->name_len);
213         if (p > (char *)dp + isonum_711(dp->length))
214                 return (0);
215         sp = (ISO_SUSP_PRESENT *)p;
216         if (bcmp(sp->h.type, SUSP_PRESENT, 2) != 0)
217                 return (0);
218         if (isonum_711(sp->h.length) != sizeof(ISO_SUSP_PRESENT))
219                 return (0);
220         if (sp->signature[0] != 0xbe || sp->signature[1] != 0xef)
221                 return (0);
222         *lenskip = isonum_711(sp->len_skp);
223
224         /*
225          * Now look for an ER field.  If RRIP is present, then there must
226          * be at least one of these.  It would be more pedantic to walk
227          * through the list of fields looking for a Rock Ridge ER field.
228          */
229         er = (ISO_RRIP_EXTREF *)susp_lookup_record(f, SUSP_EXTREF, dp, 0);
230         if (er == NULL)
231                 return (0);
232         return (1);
233 }
234
235 static int
236 dirmatch(struct open_file *f, const char *path, struct iso_directory_record *dp,
237     int use_rrip, int lenskip)
238 {
239         size_t len;
240         char *cp;
241         int i, icase;
242
243         if (use_rrip)
244                 cp = rrip_lookup_name(f, dp, lenskip, &len);
245         else
246                 cp = NULL;
247         if (cp == NULL) {
248                 len = isonum_711(dp->name_len);
249                 cp = dp->name;
250                 icase = 1;
251         } else
252                 icase = 0;
253         for (i = len; --i >= 0; path++, cp++) {
254                 if (!*path || *path == '/')
255                         break;
256                 if (*path == *cp)
257                         continue;
258                 if (!icase && toupper(*path) == *cp)
259                         continue;
260                 return 0;
261         }
262         if (*path && *path != '/')
263                 return 0;
264
265         /*
266          * Allow stripping of trailing dots and the version number.
267          *
268          * Note that this will find the first instead of the last version
269          * of a file unless explicitly specified.
270          *
271          * For any inexact matches we ignore everything after and including
272          * the semicolon in the directory entry name, assuming it is a
273          * version number whether it is or not.
274          */
275         if (i >= 0) {
276                 if (cp[0] == ';')
277                         return 1;       /* ignore version */
278                 if (cp[0] == '.' && i == 0)
279                         return 1;       /* ignore trailing dot */
280                 if (cp[0] == '.' && i > 0 && cp[1] == ';')
281                         return 1;       /* ignore trailing dot w/ version */
282                 return 0;               /* else mismatch */
283         }
284         return 1;
285 }
286
287 static int
288 cd9660_open(const char *path, struct open_file *f)
289 {
290         struct file *fp = 0;
291         void *buf;
292         struct iso_primary_descriptor *vd;
293         size_t buf_size, read, dsize, off;
294         daddr_t bno, boff;
295         struct iso_directory_record rec;
296         struct iso_directory_record *dp = 0;
297         int rc, first, use_rrip, lenskip;
298
299         /* First find the volume descriptor */
300         buf = malloc(buf_size = ISO_DEFAULT_BLOCK_SIZE);
301         vd = buf;
302         for (bno = 16;; bno++) {
303                 twiddle();
304                 rc = f->f_dev->dv_strategy(f->f_devdata, F_READ, cdb2devb(bno),
305                                            ISO_DEFAULT_BLOCK_SIZE, buf, &read);
306                 if (rc)
307                         goto out;
308                 if (read != ISO_DEFAULT_BLOCK_SIZE) {
309                         rc = EIO;
310                         goto out;
311                 }
312                 rc = EINVAL;
313                 if (bcmp(vd->id, ISO_STANDARD_ID, sizeof vd->id) != 0)
314                         goto out;
315                 if (isonum_711(vd->type) == ISO_VD_END)
316                         goto out;
317                 if (isonum_711(vd->type) == ISO_VD_PRIMARY)
318                         break;
319         }
320         if (isonum_723(vd->logical_block_size) != ISO_DEFAULT_BLOCK_SIZE)
321                 goto out;
322
323         rec = *(struct iso_directory_record *) vd->root_directory_record;
324         if (*path == '/') path++; /* eat leading '/' */
325
326         first = 1;
327         use_rrip = 0;
328         while (*path) {
329                 bno = isonum_733(rec.extent) + isonum_711(rec.ext_attr_length);
330                 dsize = isonum_733(rec.size);
331                 off = 0;
332                 boff = 0;
333
334                 while (off < dsize) {
335                         if ((off % ISO_DEFAULT_BLOCK_SIZE) == 0) {
336                                 twiddle();
337                                 rc = f->f_dev->dv_strategy
338                                         (f->f_devdata, F_READ,
339                                          cdb2devb(bno + boff),
340                                          ISO_DEFAULT_BLOCK_SIZE,
341                                          buf, &read);
342                                 if (rc)
343                                         goto out;
344                                 if (read != ISO_DEFAULT_BLOCK_SIZE) {
345                                         rc = EIO;
346                                         goto out;
347                                 }
348                                 boff++;
349                                 dp = (struct iso_directory_record *) buf;
350                         }
351                         if (isonum_711(dp->length) == 0) {
352                             /* skip to next block, if any */
353                             off = boff * ISO_DEFAULT_BLOCK_SIZE;
354                             continue;
355                         }
356
357                         /* See if RRIP is in use. */
358                         if (first)
359                                 use_rrip = rrip_check(f, dp, &lenskip);
360
361                         if (dirmatch(f, path, dp, use_rrip,
362                                 first ? 0 : lenskip)) {
363                                 first = 0;
364                                 break;
365                         } else
366                                 first = 0;
367
368                         dp = (struct iso_directory_record *)
369                                 ((char *) dp + isonum_711(dp->length));
370                         off += isonum_711(dp->length);
371                 }
372                 if (off >= dsize) {
373                         rc = ENOENT;
374                         goto out;
375                 }
376
377                 rec = *dp;
378                 while (*path && *path != '/') /* look for next component */
379                         path++;
380                 if (*path == '/') {     /* skip /, make sure is dir */
381                         path++;
382                         if (*path && (isonum_711(dp->flags) & 2) == 0) {
383                                 rc = ENOENT;    /* not directory */
384                                 goto out;
385                         }
386                 }
387         }
388
389         /* allocate file system specific data structure */
390         fp = malloc(sizeof(struct file));
391         bzero(fp, sizeof(struct file));
392         f->f_fsdata = (void *)fp;
393
394         if ((isonum_711(rec.flags) & 2) != 0) {
395                 fp->f_flags = F_ISDIR;
396         }
397         if (first) {
398                 fp->f_flags |= F_ROOTDIR;
399
400                 /* Check for Rock Ridge since we didn't in the loop above. */
401                 bno = isonum_733(rec.extent) + isonum_711(rec.ext_attr_length);
402                 twiddle();
403                 rc = f->f_dev->dv_strategy(f->f_devdata, F_READ, cdb2devb(bno),
404                     ISO_DEFAULT_BLOCK_SIZE, buf, &read);
405                 if (rc)
406                         goto out;
407                 if (read != ISO_DEFAULT_BLOCK_SIZE) {
408                         rc = EIO;
409                         goto out;
410                 }
411                 dp = (struct iso_directory_record *)buf;
412                 use_rrip = rrip_check(f, dp, &lenskip);
413         }
414         if (use_rrip) {
415                 fp->f_flags |= F_RR;
416                 fp->f_susp_skip = lenskip;
417         }
418         fp->f_off = 0;
419         fp->f_bno = isonum_733(rec.extent) + isonum_711(rec.ext_attr_length);
420         fp->f_size = isonum_733(rec.size);
421         free(buf);
422
423         return 0;
424
425 out:
426         f->f_fsdata = NULL;
427         if (fp)
428                 free(fp);
429         free(buf);
430
431         return rc;
432 }
433
434 static int
435 cd9660_close(struct open_file *f)
436 {
437         struct file *fp = (struct file *)f->f_fsdata;
438
439         f->f_fsdata = NULL;
440         if (fp)
441                 free(fp);
442
443         return 0;
444 }
445
446 static int
447 buf_read_file(struct open_file *f, char **buf_p, size_t *size_p)
448 {
449         struct file *fp = (struct file *)f->f_fsdata;
450         daddr_t blkno, blkoff;
451         int rc = 0;
452         size_t read;
453
454         blkno = fp->f_off / ISO_DEFAULT_BLOCK_SIZE + fp->f_bno;
455         blkoff = fp->f_off % ISO_DEFAULT_BLOCK_SIZE;
456
457         if (blkno != fp->f_buf_blkno) {
458                 if (fp->f_buf == NULL)
459                         fp->f_buf = malloc(ISO_DEFAULT_BLOCK_SIZE);
460
461                 twiddle();
462                 rc = f->f_dev->dv_strategy(f->f_devdata, F_READ,
463                     cdb2devb(blkno), ISO_DEFAULT_BLOCK_SIZE, fp->f_buf, &read);
464                 if (rc)
465                         return (rc);
466                 if (read != ISO_DEFAULT_BLOCK_SIZE)
467                         return (EIO);
468
469                 fp->f_buf_blkno = blkno;
470         }
471
472         *buf_p = fp->f_buf + blkoff;
473         *size_p = ISO_DEFAULT_BLOCK_SIZE - blkoff;
474
475         if (*size_p > fp->f_size - fp->f_off)
476                 *size_p = fp->f_size - fp->f_off;
477         return (rc);
478 }
479
480 static int
481 cd9660_read(struct open_file *f, void *start, size_t size, size_t *resid)
482 {
483         struct file *fp = (struct file *)f->f_fsdata;
484         char *buf, *addr;
485         size_t buf_size, csize;
486         int rc = 0;
487
488         addr = start;
489         while (size) {
490                 if (fp->f_off < 0 || fp->f_off >= fp->f_size)
491                         break;
492
493                 rc = buf_read_file(f, &buf, &buf_size);
494                 if (rc)
495                         break;
496
497                 csize = size > buf_size ? buf_size : size;
498                 bcopy(buf, addr, csize);
499
500                 fp->f_off += csize;
501                 addr += csize;
502                 size -= csize;
503         }
504         if (resid)
505                 *resid = size;
506         return (rc);
507 }
508
509 static int
510 cd9660_readdir(struct open_file *f, struct dirent *d)
511 {
512         struct file *fp = (struct file *)f->f_fsdata;
513         struct iso_directory_record *ep;
514         size_t buf_size, reclen, namelen;
515         int error = 0;
516         int lenskip;
517         char *buf, *name;
518
519 again:
520         if (fp->f_off >= fp->f_size)
521                 return (ENOENT);
522         error = buf_read_file(f, &buf, &buf_size);
523         if (error)
524                 return (error);
525         ep = (struct iso_directory_record *)buf;
526
527         if (isonum_711(ep->length) == 0) {
528                 daddr_t blkno;
529                 
530                 /* skip to next block, if any */
531                 blkno = fp->f_off / ISO_DEFAULT_BLOCK_SIZE;
532                 fp->f_off = (blkno + 1) * ISO_DEFAULT_BLOCK_SIZE;
533                 goto again;
534         }
535
536         if (fp->f_flags & F_RR) {
537                 if (fp->f_flags & F_ROOTDIR && fp->f_off == 0)
538                         lenskip = 0;
539                 else
540                         lenskip = fp->f_susp_skip;
541                 name = rrip_lookup_name(f, ep, lenskip, &namelen);
542         } else
543                 name = NULL;
544         if (name == NULL) {
545                 namelen = isonum_711(ep->name_len);
546                 name = ep->name;
547                 if (namelen == 1) {
548                         if (ep->name[0] == 0)
549                                 name = ".";
550                         else if (ep->name[0] == 1) {
551                                 namelen = 2;
552                                 name = "..";
553                         }
554                 }
555         }
556         reclen = _DIRENT_RECLEN(namelen);
557
558         d->d_ino = isonum_733(ep->extent);
559         if (isonum_711(ep->flags) & 2)
560                 d->d_type = DT_DIR;
561         else
562                 d->d_type = DT_REG;
563         d->d_namlen = namelen;
564
565         bcopy(name, d->d_name, d->d_namlen);
566         d->d_name[d->d_namlen] = 0;
567
568         fp->f_off += isonum_711(ep->length);
569         return (0);
570 }
571
572 static int
573 cd9660_write(struct open_file *f, void *start, size_t size, size_t *resid)
574 {
575         return EROFS;
576 }
577
578 static off_t
579 cd9660_seek(struct open_file *f, off_t offset, int where)
580 {
581         struct file *fp = (struct file *)f->f_fsdata;
582
583         switch (where) {
584         case SEEK_SET:
585                 fp->f_off = offset;
586                 break;
587         case SEEK_CUR:
588                 fp->f_off += offset;
589                 break;
590         case SEEK_END:
591                 fp->f_off = fp->f_size - offset;
592                 break;
593         default:
594                 return -1;
595         }
596         return fp->f_off;
597 }
598
599 static int
600 cd9660_stat(struct open_file *f, struct stat *sb)
601 {
602         struct file *fp = (struct file *)f->f_fsdata;
603
604         /* only important stuff */
605         sb->st_mode = S_IRUSR | S_IRGRP | S_IROTH;
606         if (fp->f_flags & F_ISDIR)
607                 sb->st_mode |= S_IFDIR;
608         else
609                 sb->st_mode |= S_IFREG;
610         sb->st_uid = sb->st_gid = 0;
611         sb->st_size = fp->f_size;
612         return 0;
613 }