cd9660.c revision 1.11 1 1.11 simonb /* $NetBSD: cd9660.c,v 1.11 1999/11/23 12:20:53 simonb Exp $ */
2 1.1 ws
3 1.1 ws /*
4 1.1 ws * Copyright (C) 1996 Wolfgang Solfrank.
5 1.1 ws * Copyright (C) 1996 TooLs GmbH.
6 1.1 ws * All rights reserved.
7 1.1 ws *
8 1.1 ws * Redistribution and use in source and binary forms, with or without
9 1.1 ws * modification, are permitted provided that the following conditions
10 1.1 ws * are met:
11 1.1 ws * 1. Redistributions of source code must retain the above copyright
12 1.1 ws * notice, this list of conditions and the following disclaimer.
13 1.1 ws * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 ws * notice, this list of conditions and the following disclaimer in the
15 1.1 ws * documentation and/or other materials provided with the distribution.
16 1.1 ws * 3. All advertising materials mentioning features or use of this software
17 1.1 ws * must display the following acknowledgement:
18 1.1 ws * This product includes software developed by TooLs GmbH.
19 1.1 ws * 4. The name of TooLs GmbH may not be used to endorse or promote products
20 1.1 ws * derived from this software without specific prior written permission.
21 1.1 ws *
22 1.1 ws * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
23 1.1 ws * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 1.1 ws * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 1.1 ws * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 1.1 ws * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 1.1 ws * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28 1.1 ws * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 1.1 ws * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 1.1 ws * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 1.1 ws * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 1.1 ws */
33 1.1 ws
34 1.1 ws /*
35 1.1 ws * Stand-alone ISO9660 file reading package.
36 1.1 ws *
37 1.1 ws * Note: This doesn't support Rock Ridge extensions, extended attributes,
38 1.1 ws * blocksizes other than 2048 bytes, multi-extent files, etc.
39 1.1 ws */
40 1.1 ws #include <sys/param.h>
41 1.10 thorpej #ifdef _STANDALONE
42 1.10 thorpej #include <lib/libkern/libkern.h>
43 1.10 thorpej #else
44 1.10 thorpej #include <string.h>
45 1.10 thorpej #endif
46 1.2 cgd #include <isofs/cd9660/iso.h>
47 1.1 ws
48 1.2 cgd #include "stand.h"
49 1.2 cgd #include "cd9660.h"
50 1.1 ws
51 1.8 cgd /*
52 1.8 cgd * XXX Does not currently implement:
53 1.8 cgd * XXX
54 1.8 cgd * XXX LIBSA_NO_FS_SYMLINK (does this even make sense?)
55 1.8 cgd * XXX LIBSA_FS_SINGLECOMPONENT
56 1.8 cgd */
57 1.8 cgd
58 1.1 ws struct file {
59 1.1 ws off_t off; /* Current offset within file */
60 1.1 ws daddr_t bno; /* Starting block number */
61 1.1 ws off_t size; /* Size of file */
62 1.1 ws };
63 1.1 ws
64 1.1 ws struct ptable_ent {
65 1.1 ws char namlen [ISODCL( 1, 1)]; /* 711 */
66 1.1 ws char extlen [ISODCL( 2, 2)]; /* 711 */
67 1.1 ws char block [ISODCL( 3, 6)]; /* 732 */
68 1.1 ws char parent [ISODCL( 7, 8)]; /* 722 */
69 1.1 ws char name [1];
70 1.1 ws };
71 1.1 ws #define PTFIXSZ 8
72 1.1 ws #define PTSIZE(pp) roundup(PTFIXSZ + isonum_711((pp)->namlen), 2)
73 1.1 ws
74 1.1 ws #define cdb2devb(bno) ((bno) * ISO_DEFAULT_BLOCK_SIZE / DEV_BSIZE)
75 1.6 pk
76 1.10 thorpej static int toupper __P((int));
77 1.6 pk static int pnmatch __P((char *, struct ptable_ent *));
78 1.6 pk static int dirmatch __P((char *, struct iso_directory_record *));
79 1.10 thorpej
80 1.10 thorpej static int
81 1.10 thorpej toupper(c)
82 1.10 thorpej int c;
83 1.10 thorpej {
84 1.10 thorpej return c >= 'a' && c <= 'z' ? c - 'a' + 'A' : c;
85 1.10 thorpej }
86 1.1 ws
87 1.1 ws static int
88 1.1 ws pnmatch(path, pp)
89 1.1 ws char *path;
90 1.1 ws struct ptable_ent *pp;
91 1.1 ws {
92 1.1 ws char *cp;
93 1.1 ws int i;
94 1.1 ws
95 1.1 ws cp = pp->name;
96 1.1 ws for (i = isonum_711(pp->namlen); --i >= 0; path++, cp++) {
97 1.1 ws if (toupper(*path) == *cp)
98 1.1 ws continue;
99 1.1 ws return 0;
100 1.1 ws }
101 1.1 ws if (*path != '/')
102 1.1 ws return 0;
103 1.1 ws return 1;
104 1.1 ws }
105 1.1 ws
106 1.1 ws static int
107 1.1 ws dirmatch(path, dp)
108 1.1 ws char *path;
109 1.1 ws struct iso_directory_record *dp;
110 1.1 ws {
111 1.1 ws char *cp;
112 1.1 ws int i;
113 1.1 ws
114 1.1 ws /* This needs to be a regular file */
115 1.1 ws if (dp->flags[0] & 6)
116 1.1 ws return 0;
117 1.1 ws
118 1.1 ws cp = dp->name;
119 1.1 ws for (i = isonum_711(dp->name_len); --i >= 0; path++, cp++) {
120 1.1 ws if (!*path)
121 1.1 ws break;
122 1.1 ws if (toupper(*path) == *cp)
123 1.1 ws continue;
124 1.1 ws return 0;
125 1.1 ws }
126 1.1 ws if (*path)
127 1.1 ws return 0;
128 1.1 ws /*
129 1.1 ws * Allow stripping of trailing dots and the version number.
130 1.1 ws * Note that this will find the first instead of the last version
131 1.1 ws * of a file.
132 1.1 ws */
133 1.1 ws if (i >= 0 && (*cp == ';' || *cp == '.')) {
134 1.1 ws /* This is to prevent matching of numeric extensions */
135 1.1 ws if (*cp == '.' && cp[1] != ';')
136 1.1 ws return 0;
137 1.1 ws while (--i >= 0)
138 1.1 ws if (*++cp != ';' && (*cp < '0' || *cp > '9'))
139 1.1 ws return 0;
140 1.1 ws }
141 1.1 ws return 1;
142 1.1 ws }
143 1.1 ws
144 1.1 ws int
145 1.1 ws cd9660_open(path, f)
146 1.1 ws char *path;
147 1.1 ws struct open_file *f;
148 1.1 ws {
149 1.1 ws struct file *fp = 0;
150 1.1 ws void *buf;
151 1.1 ws struct iso_primary_descriptor *vd;
152 1.1 ws size_t buf_size, read, psize, dsize;
153 1.1 ws daddr_t bno;
154 1.1 ws int parent, ent;
155 1.1 ws struct ptable_ent *pp;
156 1.4 drochner struct iso_directory_record *dp = 0;
157 1.1 ws int rc;
158 1.1 ws
159 1.1 ws /* First find the volume descriptor */
160 1.1 ws buf = alloc(buf_size = ISO_DEFAULT_BLOCK_SIZE);
161 1.1 ws vd = buf;
162 1.1 ws for (bno = 16;; bno++) {
163 1.8 cgd #if !defined(LIBSA_NO_TWIDDLE)
164 1.1 ws twiddle();
165 1.8 cgd #endif
166 1.8 cgd rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ, cdb2devb(bno),
167 1.1 ws ISO_DEFAULT_BLOCK_SIZE, buf, &read);
168 1.1 ws if (rc)
169 1.1 ws goto out;
170 1.1 ws if (read != ISO_DEFAULT_BLOCK_SIZE) {
171 1.1 ws rc = EIO;
172 1.1 ws goto out;
173 1.1 ws }
174 1.1 ws rc = EINVAL;
175 1.1 ws if (bcmp(vd->id, ISO_STANDARD_ID, sizeof vd->id) != 0)
176 1.1 ws goto out;
177 1.1 ws if (isonum_711(vd->type) == ISO_VD_END)
178 1.1 ws goto out;
179 1.1 ws if (isonum_711(vd->type) == ISO_VD_PRIMARY)
180 1.1 ws break;
181 1.1 ws }
182 1.1 ws if (isonum_723(vd->logical_block_size) != ISO_DEFAULT_BLOCK_SIZE)
183 1.1 ws goto out;
184 1.1 ws
185 1.1 ws /* Now get the path table and lookup the directory of the file */
186 1.1 ws bno = isonum_732(vd->type_m_path_table);
187 1.1 ws psize = isonum_733(vd->path_table_size);
188 1.1 ws
189 1.1 ws if (psize > ISO_DEFAULT_BLOCK_SIZE) {
190 1.1 ws free(buf, ISO_DEFAULT_BLOCK_SIZE);
191 1.1 ws buf = alloc(buf_size = roundup(psize, ISO_DEFAULT_BLOCK_SIZE));
192 1.1 ws }
193 1.1 ws
194 1.8 cgd #if !defined(LIBSA_NO_TWIDDLE)
195 1.1 ws twiddle();
196 1.8 cgd #endif
197 1.8 cgd rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ, cdb2devb(bno),
198 1.1 ws buf_size, buf, &read);
199 1.1 ws if (rc)
200 1.1 ws goto out;
201 1.1 ws if (read != buf_size) {
202 1.1 ws rc = EIO;
203 1.1 ws goto out;
204 1.1 ws }
205 1.1 ws
206 1.1 ws parent = 1;
207 1.1 ws pp = (struct ptable_ent *)buf;
208 1.1 ws ent = 1;
209 1.1 ws bno = isonum_732(pp->block) + isonum_711(pp->extlen);
210 1.1 ws
211 1.1 ws rc = ENOENT;
212 1.11 simonb /*
213 1.11 simonb * Remove extra separators
214 1.11 simonb */
215 1.11 simonb while (*path == '/')
216 1.11 simonb path++;
217 1.11 simonb
218 1.1 ws while (*path) {
219 1.7 dbj if ((caddr_t)pp >= (caddr_t)buf + psize)
220 1.1 ws break;
221 1.1 ws if (isonum_722(pp->parent) != parent)
222 1.1 ws break;
223 1.1 ws if (!pnmatch(path, pp)) {
224 1.7 dbj pp = (struct ptable_ent *)((caddr_t)pp + PTSIZE(pp));
225 1.1 ws ent++;
226 1.1 ws continue;
227 1.1 ws }
228 1.1 ws path += isonum_711(pp->namlen) + 1;
229 1.1 ws parent = ent;
230 1.1 ws bno = isonum_732(pp->block) + isonum_711(pp->extlen);
231 1.7 dbj while ((caddr_t)pp < (caddr_t)buf + psize) {
232 1.1 ws if (isonum_722(pp->parent) == parent)
233 1.1 ws break;
234 1.7 dbj pp = (struct ptable_ent *)((caddr_t)pp + PTSIZE(pp));
235 1.1 ws ent++;
236 1.1 ws }
237 1.1 ws }
238 1.1 ws
239 1.1 ws /* Now bno has the start of the directory that supposedly contains the file */
240 1.1 ws bno--;
241 1.1 ws dsize = 1; /* Something stupid, but > 0 XXX */
242 1.1 ws for (psize = 0; psize < dsize;) {
243 1.1 ws if (!(psize % ISO_DEFAULT_BLOCK_SIZE)) {
244 1.1 ws bno++;
245 1.8 cgd #if !defined(LIBSA_NO_TWIDDLE)
246 1.1 ws twiddle();
247 1.8 cgd #endif
248 1.8 cgd rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ,
249 1.1 ws cdb2devb(bno),
250 1.1 ws ISO_DEFAULT_BLOCK_SIZE,
251 1.1 ws buf, &read);
252 1.1 ws if (rc)
253 1.1 ws goto out;
254 1.1 ws if (read != ISO_DEFAULT_BLOCK_SIZE) {
255 1.1 ws rc = EIO;
256 1.1 ws goto out;
257 1.1 ws }
258 1.1 ws dp = (struct iso_directory_record *)buf;
259 1.1 ws }
260 1.1 ws if (!isonum_711(dp->length)) {
261 1.1 ws if ((void *)dp == buf)
262 1.1 ws psize += ISO_DEFAULT_BLOCK_SIZE;
263 1.1 ws else
264 1.1 ws psize = roundup(psize, ISO_DEFAULT_BLOCK_SIZE);
265 1.1 ws continue;
266 1.1 ws }
267 1.1 ws if (dsize == 1)
268 1.1 ws dsize = isonum_733(dp->size);
269 1.1 ws if (dirmatch(path, dp))
270 1.1 ws break;
271 1.1 ws psize += isonum_711(dp->length);
272 1.7 dbj dp = (struct iso_directory_record *)((caddr_t)dp + isonum_711(dp->length));
273 1.1 ws }
274 1.1 ws
275 1.1 ws if (psize >= dsize) {
276 1.1 ws rc = ENOENT;
277 1.1 ws goto out;
278 1.1 ws }
279 1.1 ws
280 1.1 ws /* allocate file system specific data structure */
281 1.1 ws fp = alloc(sizeof(struct file));
282 1.1 ws bzero(fp, sizeof(struct file));
283 1.1 ws f->f_fsdata = (void *)fp;
284 1.1 ws
285 1.1 ws fp->off = 0;
286 1.1 ws fp->bno = isonum_733(dp->extent);
287 1.1 ws fp->size = isonum_733(dp->size);
288 1.1 ws free(buf, buf_size);
289 1.1 ws
290 1.1 ws return 0;
291 1.1 ws
292 1.1 ws out:
293 1.1 ws if (fp)
294 1.1 ws free(fp, sizeof(struct file));
295 1.1 ws free(buf, buf_size);
296 1.1 ws
297 1.1 ws return rc;
298 1.1 ws }
299 1.1 ws
300 1.8 cgd #if !defined(LIBSA_NO_FS_CLOSE)
301 1.1 ws int
302 1.1 ws cd9660_close(f)
303 1.1 ws struct open_file *f;
304 1.1 ws {
305 1.1 ws struct file *fp = (struct file *)f->f_fsdata;
306 1.1 ws
307 1.1 ws f->f_fsdata = 0;
308 1.1 ws free(fp, sizeof *fp);
309 1.1 ws
310 1.1 ws return 0;
311 1.1 ws }
312 1.8 cgd #endif /* !defined(LIBSA_NO_FS_CLOSE) */
313 1.1 ws
314 1.1 ws int
315 1.1 ws cd9660_read(f, start, size, resid)
316 1.1 ws struct open_file *f;
317 1.1 ws void *start;
318 1.1 ws size_t size;
319 1.1 ws size_t *resid;
320 1.1 ws {
321 1.1 ws struct file *fp = (struct file *)f->f_fsdata;
322 1.1 ws int rc = 0;
323 1.1 ws daddr_t bno;
324 1.1 ws char buf[ISO_DEFAULT_BLOCK_SIZE];
325 1.1 ws char *dp;
326 1.1 ws size_t read, off;
327 1.1 ws
328 1.1 ws while (size) {
329 1.1 ws if (fp->off < 0 || fp->off >= fp->size)
330 1.1 ws break;
331 1.1 ws bno = fp->off / ISO_DEFAULT_BLOCK_SIZE + fp->bno;
332 1.1 ws if (fp->off & (ISO_DEFAULT_BLOCK_SIZE - 1)
333 1.1 ws || size < ISO_DEFAULT_BLOCK_SIZE)
334 1.1 ws dp = buf;
335 1.1 ws else
336 1.1 ws dp = start;
337 1.8 cgd #if !defined(LIBSA_NO_TWIDDLE)
338 1.1 ws twiddle();
339 1.8 cgd #endif
340 1.8 cgd rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ, cdb2devb(bno),
341 1.1 ws ISO_DEFAULT_BLOCK_SIZE, dp, &read);
342 1.1 ws if (rc)
343 1.1 ws return rc;
344 1.1 ws if (read != ISO_DEFAULT_BLOCK_SIZE)
345 1.1 ws return EIO;
346 1.1 ws if (dp == buf) {
347 1.1 ws off = fp->off & (ISO_DEFAULT_BLOCK_SIZE - 1);
348 1.1 ws if (read > off + size)
349 1.1 ws read = off + size;
350 1.1 ws read -= off;
351 1.1 ws bcopy(buf + off, start, read);
352 1.7 dbj start = (caddr_t)start + read;
353 1.1 ws fp->off += read;
354 1.1 ws size -= read;
355 1.1 ws } else {
356 1.7 dbj start = (caddr_t)start + ISO_DEFAULT_BLOCK_SIZE;
357 1.1 ws fp->off += ISO_DEFAULT_BLOCK_SIZE;
358 1.1 ws size -= ISO_DEFAULT_BLOCK_SIZE;
359 1.1 ws }
360 1.1 ws }
361 1.1 ws if (resid)
362 1.1 ws *resid = size;
363 1.1 ws return rc;
364 1.1 ws }
365 1.1 ws
366 1.8 cgd #if !defined(LIBSA_NO_FS_WRITE)
367 1.1 ws int
368 1.1 ws cd9660_write(f, start, size, resid)
369 1.1 ws struct open_file *f;
370 1.1 ws void *start;
371 1.1 ws size_t size;
372 1.1 ws size_t *resid;
373 1.1 ws {
374 1.1 ws return EROFS;
375 1.1 ws }
376 1.8 cgd #endif /* !defined(LIBSA_NO_FS_WRITE) */
377 1.1 ws
378 1.8 cgd #if !defined(LIBSA_NO_FS_SEEK)
379 1.1 ws off_t
380 1.1 ws cd9660_seek(f, offset, where)
381 1.1 ws struct open_file *f;
382 1.1 ws off_t offset;
383 1.1 ws int where;
384 1.1 ws {
385 1.1 ws struct file *fp = (struct file *)f->f_fsdata;
386 1.1 ws
387 1.1 ws switch (where) {
388 1.1 ws case SEEK_SET:
389 1.1 ws fp->off = offset;
390 1.1 ws break;
391 1.1 ws case SEEK_CUR:
392 1.1 ws fp->off += offset;
393 1.1 ws break;
394 1.1 ws case SEEK_END:
395 1.1 ws fp->off = fp->size - offset;
396 1.1 ws break;
397 1.1 ws default:
398 1.1 ws return -1;
399 1.1 ws }
400 1.1 ws return fp->off;
401 1.1 ws }
402 1.8 cgd #endif /* !defined(LIBSA_NO_FS_SEEK) */
403 1.1 ws
404 1.1 ws int
405 1.1 ws cd9660_stat(f, sb)
406 1.1 ws struct open_file *f;
407 1.1 ws struct stat *sb;
408 1.1 ws {
409 1.1 ws struct file *fp = (struct file *)f->f_fsdata;
410 1.1 ws
411 1.1 ws /* only importatn stuff */
412 1.1 ws sb->st_mode = S_IFREG | S_IRUSR | S_IRGRP | S_IROTH;
413 1.1 ws sb->st_uid = sb->st_gid = 0;
414 1.1 ws sb->st_size = fp->size;
415 1.1 ws return 0;
416 1.1 ws }
417