Kernel tree reorganization stage 2: Major cvs repository work.
[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.3 2003/08/08 04:18:34 dillon 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(p)
123         u_char *p;
124 {
125         return (*p << 8)|p[1];
126 }
127
128 static __inline int
129 isonum_732(p)
130         u_char *p;
131 {
132         return (*p << 24)|(p[1] << 16)|(p[2] << 8)|p[3];
133 }
134
135 static ISO_SUSP_HEADER *
136 susp_lookup_record(struct open_file *f, const char *identifier,
137     struct iso_directory_record *dp, int lenskip)
138 {
139         static char susp_buffer[ISO_DEFAULT_BLOCK_SIZE];
140         ISO_SUSP_HEADER *sh;
141         ISO_RRIP_CONT *shc;
142         char *p, *end;
143         int error;
144         size_t read;
145
146         p = dp->name + isonum_711(dp->name_len) + lenskip;
147         /* Names of even length have a padding byte after the name. */
148         if ((isonum_711(dp->name_len) & 1) == 0)
149                 p++;
150         end = (char *)dp + isonum_711(dp->length);
151         while (p + 3 < end) {
152                 sh = (ISO_SUSP_HEADER *)p;
153                 if (bcmp(sh->type, identifier, 2) == 0)
154                         return (sh);
155                 if (bcmp(sh->type, SUSP_STOP, 2) == 0)
156                         return (NULL);
157                 if (bcmp(sh->type, SUSP_CONTINUATION, 2) == 0) {
158                         shc = (ISO_RRIP_CONT *)sh;
159                         error = f->f_dev->dv_strategy(f->f_devdata, F_READ,
160                             cdb2devb(isonum_733(shc->location)),
161                             ISO_DEFAULT_BLOCK_SIZE, susp_buffer, &read);
162
163                         /* Bail if it fails. */
164                         if (error != 0 || read != ISO_DEFAULT_BLOCK_SIZE)
165                                 return (NULL);
166                         p = susp_buffer + isonum_733(shc->offset);
167                         end = p + isonum_733(shc->length);
168                 } else
169                         /* Ignore this record and skip to the next. */
170                         p += isonum_711(sh->length);
171         }
172         return (NULL);
173 }
174
175 static char *
176 rrip_lookup_name(struct open_file *f, struct iso_directory_record *dp,
177     int lenskip, size_t *len)
178 {
179         ISO_RRIP_ALTNAME *p;
180
181         if (len == NULL)
182                 return (NULL);
183
184         p = (ISO_RRIP_ALTNAME *)susp_lookup_record(f, RRIP_NAME, dp, lenskip);
185         if (p == NULL)
186                 return (NULL);
187         switch (*p->flags) {
188         case ISO_SUSP_CFLAG_CURRENT:
189                 *len = 1;
190                 return (".");
191         case ISO_SUSP_CFLAG_PARENT:
192                 *len = 2;
193                 return ("..");
194         case 0:
195                 *len = isonum_711(p->h.length) - 5;
196                 return ((char *)p + 5);
197         default:
198                 /*
199                  * We don't handle hostnames or continued names as they are
200                  * too hard, so just bail and use the default name.
201                  */
202                 return (NULL);
203         }
204 }
205
206 static int
207 rrip_check(struct open_file *f, struct iso_directory_record *dp, int *lenskip)
208 {
209         ISO_SUSP_PRESENT *sp;
210         ISO_RRIP_EXTREF *er;
211         char *p;
212
213         /* First, see if we can find a SP field. */
214         p = dp->name + isonum_711(dp->name_len);
215         if (p > (char *)dp + isonum_711(dp->length))
216                 return (0);
217         sp = (ISO_SUSP_PRESENT *)p;
218         if (bcmp(sp->h.type, SUSP_PRESENT, 2) != 0)
219                 return (0);
220         if (isonum_711(sp->h.length) != sizeof(ISO_SUSP_PRESENT))
221                 return (0);
222         if (sp->signature[0] != 0xbe || sp->signature[1] != 0xef)
223                 return (0);
224         *lenskip = isonum_711(sp->len_skp);
225
226         /*
227          * Now look for an ER field.  If RRIP is present, then there must
228          * be at least one of these.  It would be more pedantic to walk
229          * through the list of fields looking for a Rock Ridge ER field.
230          */
231         er = (ISO_RRIP_EXTREF *)susp_lookup_record(f, SUSP_EXTREF, dp, 0);
232         if (er == NULL)
233                 return (0);
234         return (1);
235 }
236
237 static int
238 dirmatch(struct open_file *f, const char *path, struct iso_directory_record *dp,
239     int use_rrip, int lenskip)
240 {
241         size_t len;
242         char *cp;
243         int i, icase;
244
245         if (use_rrip)
246                 cp = rrip_lookup_name(f, dp, lenskip, &len);
247         else
248                 cp = NULL;
249         if (cp == NULL) {
250                 len = isonum_711(dp->name_len);
251                 cp = dp->name;
252                 icase = 1;
253         } else
254                 icase = 0;
255         for (i = len; --i >= 0; path++, cp++) {
256                 if (!*path || *path == '/')
257                         break;
258                 if (*path == *cp)
259                         continue;
260                 if (!icase && toupper(*path) == *cp)
261                         continue;
262                 return 0;
263         }
264         if (*path && *path != '/')
265                 return 0;
266         /*
267          * Allow stripping of trailing dots and the version number.
268          * Note that this will find the first instead of the last version
269          * of a file.
270          */
271         if (i >= 0 && (*cp == ';' || *cp == '.')) {
272                 /* This is to prevent matching of numeric extensions */
273                 if (*cp == '.' && cp[1] != ';')
274                         return 0;
275                 while (--i >= 0)
276                         if (*++cp != ';' && (*cp < '0' || *cp > '9'))
277                                 return 0;
278         }
279         return 1;
280 }
281
282 static int
283 cd9660_open(const char *path, struct open_file *f)
284 {
285         struct file *fp = 0;
286         void *buf;
287         struct iso_primary_descriptor *vd;
288         size_t buf_size, read, dsize, off;
289         daddr_t bno, boff;
290         struct iso_directory_record rec;
291         struct iso_directory_record *dp = 0;
292         int rc, first, use_rrip, lenskip;
293
294         /* First find the volume descriptor */
295         buf = malloc(buf_size = ISO_DEFAULT_BLOCK_SIZE);
296         vd = buf;
297         for (bno = 16;; bno++) {
298                 twiddle();
299                 rc = f->f_dev->dv_strategy(f->f_devdata, F_READ, cdb2devb(bno),
300                                            ISO_DEFAULT_BLOCK_SIZE, buf, &read);
301                 if (rc)
302                         goto out;
303                 if (read != ISO_DEFAULT_BLOCK_SIZE) {
304                         rc = EIO;
305                         goto out;
306                 }
307                 rc = EINVAL;
308                 if (bcmp(vd->id, ISO_STANDARD_ID, sizeof vd->id) != 0)
309                         goto out;
310                 if (isonum_711(vd->type) == ISO_VD_END)
311                         goto out;
312                 if (isonum_711(vd->type) == ISO_VD_PRIMARY)
313                         break;
314         }
315         if (isonum_723(vd->logical_block_size) != ISO_DEFAULT_BLOCK_SIZE)
316                 goto out;
317
318         rec = *(struct iso_directory_record *) vd->root_directory_record;
319         if (*path == '/') path++; /* eat leading '/' */
320
321         first = 1;
322         use_rrip = 0;
323         while (*path) {
324                 bno = isonum_733(rec.extent) + isonum_711(rec.ext_attr_length);
325                 dsize = isonum_733(rec.size);
326                 off = 0;
327                 boff = 0;
328
329                 while (off < dsize) {
330                         if ((off % ISO_DEFAULT_BLOCK_SIZE) == 0) {
331                                 twiddle();
332                                 rc = f->f_dev->dv_strategy
333                                         (f->f_devdata, F_READ,
334                                          cdb2devb(bno + boff),
335                                          ISO_DEFAULT_BLOCK_SIZE,
336                                          buf, &read);
337                                 if (rc)
338                                         goto out;
339                                 if (read != ISO_DEFAULT_BLOCK_SIZE) {
340                                         rc = EIO;
341                                         goto out;
342                                 }
343                                 boff++;
344                                 dp = (struct iso_directory_record *) buf;
345                         }
346                         if (isonum_711(dp->length) == 0) {
347                             /* skip to next block, if any */
348                             off = boff * ISO_DEFAULT_BLOCK_SIZE;
349                             continue;
350                         }
351
352                         /* See if RRIP is in use. */
353                         if (first)
354                                 use_rrip = rrip_check(f, dp, &lenskip);
355
356                         if (dirmatch(f, path, dp, use_rrip,
357                                 first ? 0 : lenskip)) {
358                                 first = 0;
359                                 break;
360                         } else
361                                 first = 0;
362
363                         dp = (struct iso_directory_record *)
364                                 ((char *) dp + isonum_711(dp->length));
365                         off += isonum_711(dp->length);
366                 }
367                 if (off >= dsize) {
368                         rc = ENOENT;
369                         goto out;
370                 }
371
372                 rec = *dp;
373                 while (*path && *path != '/') /* look for next component */
374                         path++;
375                 if (*path) path++; /* skip '/' */
376         }
377
378         /* allocate file system specific data structure */
379         fp = malloc(sizeof(struct file));
380         bzero(fp, sizeof(struct file));
381         f->f_fsdata = (void *)fp;
382
383         if ((isonum_711(rec.flags) & 2) != 0) {
384                 fp->f_flags = F_ISDIR;
385         }
386         if (first) {
387                 fp->f_flags |= F_ROOTDIR;
388
389                 /* Check for Rock Ridge since we didn't in the loop above. */
390                 bno = isonum_733(rec.extent) + isonum_711(rec.ext_attr_length);
391                 twiddle();
392                 rc = f->f_dev->dv_strategy(f->f_devdata, F_READ, cdb2devb(bno),
393                     ISO_DEFAULT_BLOCK_SIZE, buf, &read);
394                 if (rc)
395                         goto out;
396                 if (read != ISO_DEFAULT_BLOCK_SIZE) {
397                         rc = EIO;
398                         goto out;
399                 }
400                 dp = (struct iso_directory_record *)buf;
401                 use_rrip = rrip_check(f, dp, &lenskip);
402         }
403         if (use_rrip) {
404                 fp->f_flags |= F_RR;
405                 fp->f_susp_skip = lenskip;
406         }
407         fp->f_off = 0;
408         fp->f_bno = isonum_733(rec.extent) + isonum_711(rec.ext_attr_length);
409         fp->f_size = isonum_733(rec.size);
410         free(buf);
411
412         return 0;
413
414 out:
415         if (fp)
416                 free(fp);
417         free(buf);
418
419         return rc;
420 }
421
422 static int
423 cd9660_close(struct open_file *f)
424 {
425         struct file *fp = (struct file *)f->f_fsdata;
426
427         f->f_fsdata = 0;
428         free(fp);
429
430         return 0;
431 }
432
433 static int
434 buf_read_file(struct open_file *f, char **buf_p, size_t *size_p)
435 {
436         struct file *fp = (struct file *)f->f_fsdata;
437         daddr_t blkno, blkoff;
438         int rc = 0;
439         size_t read;
440
441         blkno = fp->f_off / ISO_DEFAULT_BLOCK_SIZE + fp->f_bno;
442         blkoff = fp->f_off % ISO_DEFAULT_BLOCK_SIZE;
443
444         if (blkno != fp->f_buf_blkno) {
445                 if (fp->f_buf == (char *)0)
446                         fp->f_buf = malloc(ISO_DEFAULT_BLOCK_SIZE);
447
448                 twiddle();
449                 rc = f->f_dev->dv_strategy(f->f_devdata, F_READ,
450                     cdb2devb(blkno), ISO_DEFAULT_BLOCK_SIZE, fp->f_buf, &read);
451                 if (rc)
452                         return (rc);
453                 if (read != ISO_DEFAULT_BLOCK_SIZE)
454                         return (EIO);
455
456                 fp->f_buf_blkno = blkno;
457         }
458
459         *buf_p = fp->f_buf + blkoff;
460         *size_p = ISO_DEFAULT_BLOCK_SIZE - blkoff;
461
462         if (*size_p > fp->f_size - fp->f_off)
463                 *size_p = fp->f_size - fp->f_off;
464         return (rc);
465 }
466
467 static int
468 cd9660_read(struct open_file *f, void *start, size_t size, size_t *resid)
469 {
470         struct file *fp = (struct file *)f->f_fsdata;
471         char *buf, *addr;
472         size_t buf_size, csize;
473         int rc = 0;
474
475         addr = start;
476         while (size) {
477                 if (fp->f_off < 0 || fp->f_off >= fp->f_size)
478                         break;
479
480                 rc = buf_read_file(f, &buf, &buf_size);
481                 if (rc)
482                         break;
483
484                 csize = size > buf_size ? buf_size : size;
485                 bcopy(buf, addr, csize);
486
487                 fp->f_off += csize;
488                 addr += csize;
489                 size -= csize;
490         }
491         if (resid)
492                 *resid = size;
493         return (rc);
494 }
495
496 static int
497 cd9660_readdir(struct open_file *f, struct dirent *d)
498 {
499         struct file *fp = (struct file *)f->f_fsdata;
500         struct iso_directory_record *ep;
501         size_t buf_size, reclen, namelen;
502         int error = 0;
503         int lenskip;
504         char *buf, *name;
505
506 again:
507         if (fp->f_off >= fp->f_size)
508                 return (ENOENT);
509         error = buf_read_file(f, &buf, &buf_size);
510         if (error)
511                 return (error);
512         ep = (struct iso_directory_record *)buf;
513
514         if (isonum_711(ep->length) == 0) {
515                 daddr_t blkno;
516                 
517                 /* skip to next block, if any */
518                 blkno = fp->f_off / ISO_DEFAULT_BLOCK_SIZE;
519                 fp->f_off = (blkno + 1) * ISO_DEFAULT_BLOCK_SIZE;
520                 goto again;
521         }
522
523         if (fp->f_flags & F_RR) {
524                 if (fp->f_flags & F_ROOTDIR && fp->f_off == 0)
525                         lenskip = 0;
526                 else
527                         lenskip = fp->f_susp_skip;
528                 name = rrip_lookup_name(f, ep, lenskip, &namelen);
529         } else
530                 name = NULL;
531         if (name == NULL) {
532                 namelen = isonum_711(ep->name_len);
533                 name = ep->name;
534                 if (namelen == 1) {
535                         if (ep->name[0] == 0)
536                                 name = ".";
537                         else if (ep->name[0] == 1) {
538                                 namelen = 2;
539                                 name = "..";
540                         }
541                 }
542         }
543         reclen = sizeof(struct dirent) - (MAXNAMLEN+1) + namelen + 1;
544         reclen = (reclen + 3) & ~3;
545
546         d->d_fileno = isonum_733(ep->extent);
547         d->d_reclen = reclen;
548         if (isonum_711(ep->flags) & 2)
549                 d->d_type = DT_DIR;
550         else
551                 d->d_type = DT_REG;
552         d->d_namlen = namelen;
553
554         bcopy(name, d->d_name, d->d_namlen);
555         d->d_name[d->d_namlen] = 0;
556
557         fp->f_off += isonum_711(ep->length);
558         return (0);
559 }
560
561 static int
562 cd9660_write(struct open_file *f, void *start, size_t size, size_t *resid)
563 {
564         return EROFS;
565 }
566
567 static off_t
568 cd9660_seek(struct open_file *f, off_t offset, int where)
569 {
570         struct file *fp = (struct file *)f->f_fsdata;
571
572         switch (where) {
573         case SEEK_SET:
574                 fp->f_off = offset;
575                 break;
576         case SEEK_CUR:
577                 fp->f_off += offset;
578                 break;
579         case SEEK_END:
580                 fp->f_off = fp->f_size - offset;
581                 break;
582         default:
583                 return -1;
584         }
585         return fp->f_off;
586 }
587
588 static int
589 cd9660_stat(struct open_file *f, struct stat *sb)
590 {
591         struct file *fp = (struct file *)f->f_fsdata;
592
593         /* only important stuff */
594         sb->st_mode = S_IRUSR | S_IRGRP | S_IROTH;
595         if (fp->f_flags & F_ISDIR)
596                 sb->st_mode |= S_IFDIR;
597         else
598                 sb->st_mode |= S_IFREG;
599         sb->st_uid = sb->st_gid = 0;
600         sb->st_size = fp->f_size;
601         return 0;
602 }