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