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