cd9660.c revision 1.32 1 1.32 christos /* $NetBSD: cd9660.c,v 1.32 2019/03/31 20:08:45 christos 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.32 christos #include <ctype.h>
45 1.10 thorpej #include <string.h>
46 1.10 thorpej #endif
47 1.13 veego #include <fs/cd9660/iso.h>
48 1.1 ws
49 1.2 cgd #include "stand.h"
50 1.2 cgd #include "cd9660.h"
51 1.1 ws
52 1.8 cgd /*
53 1.8 cgd * XXX Does not currently implement:
54 1.8 cgd * XXX
55 1.8 cgd * XXX LIBSA_NO_FS_SYMLINK (does this even make sense?)
56 1.8 cgd * XXX LIBSA_FS_SINGLECOMPONENT
57 1.8 cgd */
58 1.8 cgd
59 1.1 ws struct file {
60 1.1 ws off_t off; /* Current offset within file */
61 1.1 ws daddr_t bno; /* Starting block number */
62 1.1 ws off_t size; /* Size of file */
63 1.1 ws };
64 1.1 ws
65 1.1 ws struct ptable_ent {
66 1.1 ws char namlen [ISODCL( 1, 1)]; /* 711 */
67 1.1 ws char extlen [ISODCL( 2, 2)]; /* 711 */
68 1.1 ws char block [ISODCL( 3, 6)]; /* 732 */
69 1.1 ws char parent [ISODCL( 7, 8)]; /* 722 */
70 1.1 ws char name [1];
71 1.1 ws };
72 1.1 ws #define PTFIXSZ 8
73 1.1 ws #define PTSIZE(pp) roundup(PTFIXSZ + isonum_711((pp)->namlen), 2)
74 1.1 ws
75 1.31 nonaka #ifndef cdb2devb
76 1.1 ws #define cdb2devb(bno) ((bno) * ISO_DEFAULT_BLOCK_SIZE / DEV_BSIZE)
77 1.31 nonaka #endif
78 1.6 pk
79 1.17 junyoung static int pnmatch(const char *, struct ptable_ent *);
80 1.17 junyoung static int dirmatch(const char *, struct iso_directory_record *);
81 1.1 ws
82 1.1 ws static int
83 1.17 junyoung pnmatch(const char *path, struct ptable_ent *pp)
84 1.1 ws {
85 1.1 ws char *cp;
86 1.1 ws int i;
87 1.16 perry
88 1.1 ws cp = pp->name;
89 1.1 ws for (i = isonum_711(pp->namlen); --i >= 0; path++, cp++) {
90 1.32 christos if (toupper((unsigned char)*path) == *cp)
91 1.1 ws continue;
92 1.1 ws return 0;
93 1.1 ws }
94 1.1 ws if (*path != '/')
95 1.1 ws return 0;
96 1.1 ws return 1;
97 1.1 ws }
98 1.1 ws
99 1.1 ws static int
100 1.17 junyoung dirmatch(const char *path, struct iso_directory_record *dp)
101 1.1 ws {
102 1.1 ws char *cp;
103 1.1 ws int i;
104 1.1 ws
105 1.1 ws /* This needs to be a regular file */
106 1.1 ws if (dp->flags[0] & 6)
107 1.1 ws return 0;
108 1.1 ws
109 1.1 ws cp = dp->name;
110 1.1 ws for (i = isonum_711(dp->name_len); --i >= 0; path++, cp++) {
111 1.1 ws if (!*path)
112 1.1 ws break;
113 1.32 christos if (toupper((unsigned char)*path) == *cp)
114 1.1 ws continue;
115 1.1 ws return 0;
116 1.1 ws }
117 1.1 ws if (*path)
118 1.1 ws return 0;
119 1.1 ws /*
120 1.1 ws * Allow stripping of trailing dots and the version number.
121 1.1 ws * Note that this will find the first instead of the last version
122 1.1 ws * of a file.
123 1.1 ws */
124 1.1 ws if (i >= 0 && (*cp == ';' || *cp == '.')) {
125 1.1 ws /* This is to prevent matching of numeric extensions */
126 1.1 ws if (*cp == '.' && cp[1] != ';')
127 1.1 ws return 0;
128 1.1 ws while (--i >= 0)
129 1.1 ws if (*++cp != ';' && (*cp < '0' || *cp > '9'))
130 1.1 ws return 0;
131 1.1 ws }
132 1.1 ws return 1;
133 1.1 ws }
134 1.1 ws
135 1.27 joerg __compactcall int
136 1.17 junyoung cd9660_open(const char *path, struct open_file *f)
137 1.1 ws {
138 1.1 ws struct file *fp = 0;
139 1.1 ws void *buf;
140 1.1 ws struct iso_primary_descriptor *vd;
141 1.15 itohy size_t buf_size, nread, psize, dsize;
142 1.1 ws daddr_t bno;
143 1.1 ws int parent, ent;
144 1.1 ws struct ptable_ent *pp;
145 1.4 drochner struct iso_directory_record *dp = 0;
146 1.1 ws int rc;
147 1.16 perry
148 1.1 ws /* First find the volume descriptor */
149 1.18 junyoung buf_size = ISO_DEFAULT_BLOCK_SIZE;
150 1.17 junyoung buf = alloc(buf_size);
151 1.1 ws vd = buf;
152 1.1 ws for (bno = 16;; bno++) {
153 1.8 cgd #if !defined(LIBSA_NO_TWIDDLE)
154 1.1 ws twiddle();
155 1.8 cgd #endif
156 1.8 cgd rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ, cdb2devb(bno),
157 1.15 itohy ISO_DEFAULT_BLOCK_SIZE, buf, &nread);
158 1.1 ws if (rc)
159 1.1 ws goto out;
160 1.15 itohy if (nread != ISO_DEFAULT_BLOCK_SIZE) {
161 1.1 ws rc = EIO;
162 1.1 ws goto out;
163 1.1 ws }
164 1.1 ws rc = EINVAL;
165 1.17 junyoung if (memcmp(vd->id, ISO_STANDARD_ID, sizeof vd->id) != 0)
166 1.1 ws goto out;
167 1.1 ws if (isonum_711(vd->type) == ISO_VD_END)
168 1.1 ws goto out;
169 1.1 ws if (isonum_711(vd->type) == ISO_VD_PRIMARY)
170 1.1 ws break;
171 1.1 ws }
172 1.1 ws if (isonum_723(vd->logical_block_size) != ISO_DEFAULT_BLOCK_SIZE)
173 1.1 ws goto out;
174 1.16 perry
175 1.1 ws /* Now get the path table and lookup the directory of the file */
176 1.1 ws bno = isonum_732(vd->type_m_path_table);
177 1.1 ws psize = isonum_733(vd->path_table_size);
178 1.16 perry
179 1.1 ws if (psize > ISO_DEFAULT_BLOCK_SIZE) {
180 1.20 christos dealloc(buf, ISO_DEFAULT_BLOCK_SIZE);
181 1.1 ws buf = alloc(buf_size = roundup(psize, ISO_DEFAULT_BLOCK_SIZE));
182 1.1 ws }
183 1.1 ws
184 1.8 cgd #if !defined(LIBSA_NO_TWIDDLE)
185 1.1 ws twiddle();
186 1.8 cgd #endif
187 1.8 cgd rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ, cdb2devb(bno),
188 1.23 isaki buf_size, buf, &nread);
189 1.1 ws if (rc)
190 1.1 ws goto out;
191 1.15 itohy if (nread != buf_size) {
192 1.1 ws rc = EIO;
193 1.1 ws goto out;
194 1.1 ws }
195 1.16 perry
196 1.1 ws parent = 1;
197 1.1 ws pp = (struct ptable_ent *)buf;
198 1.1 ws ent = 1;
199 1.32 christos bno = (daddr_t)isonum_732(pp->block) + isonum_711(pp->extlen);
200 1.16 perry
201 1.1 ws rc = ENOENT;
202 1.11 simonb
203 1.1 ws while (*path) {
204 1.26 ws /*
205 1.26 ws * Remove extra separators
206 1.26 ws */
207 1.26 ws while (*path == '/')
208 1.26 ws path++;
209 1.26 ws
210 1.22 he if ((char *)pp >= (char *)buf + psize)
211 1.1 ws break;
212 1.1 ws if (isonum_722(pp->parent) != parent)
213 1.1 ws break;
214 1.1 ws if (!pnmatch(path, pp)) {
215 1.22 he pp = (struct ptable_ent *)((char *)pp + PTSIZE(pp));
216 1.1 ws ent++;
217 1.1 ws continue;
218 1.1 ws }
219 1.1 ws path += isonum_711(pp->namlen) + 1;
220 1.1 ws parent = ent;
221 1.32 christos bno = (daddr_t)isonum_732(pp->block) + isonum_711(pp->extlen);
222 1.22 he while ((char *)pp < (char *)buf + psize) {
223 1.1 ws if (isonum_722(pp->parent) == parent)
224 1.1 ws break;
225 1.22 he pp = (struct ptable_ent *)((char *)pp + PTSIZE(pp));
226 1.1 ws ent++;
227 1.1 ws }
228 1.1 ws }
229 1.1 ws
230 1.23 isaki /*
231 1.23 isaki * Now bno has the start of the directory that supposedly
232 1.23 isaki * contains the file
233 1.23 isaki */
234 1.1 ws bno--;
235 1.23 isaki dsize = 1; /* Something stupid, but > 0 XXX */
236 1.1 ws for (psize = 0; psize < dsize;) {
237 1.1 ws if (!(psize % ISO_DEFAULT_BLOCK_SIZE)) {
238 1.1 ws bno++;
239 1.8 cgd #if !defined(LIBSA_NO_TWIDDLE)
240 1.1 ws twiddle();
241 1.8 cgd #endif
242 1.8 cgd rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ,
243 1.23 isaki cdb2devb(bno),
244 1.23 isaki ISO_DEFAULT_BLOCK_SIZE,
245 1.23 isaki buf, &nread);
246 1.1 ws if (rc)
247 1.1 ws goto out;
248 1.15 itohy if (nread != ISO_DEFAULT_BLOCK_SIZE) {
249 1.1 ws rc = EIO;
250 1.1 ws goto out;
251 1.1 ws }
252 1.1 ws dp = (struct iso_directory_record *)buf;
253 1.1 ws }
254 1.1 ws if (!isonum_711(dp->length)) {
255 1.1 ws if ((void *)dp == buf)
256 1.1 ws psize += ISO_DEFAULT_BLOCK_SIZE;
257 1.1 ws else
258 1.1 ws psize = roundup(psize, ISO_DEFAULT_BLOCK_SIZE);
259 1.1 ws continue;
260 1.1 ws }
261 1.1 ws if (dsize == 1)
262 1.1 ws dsize = isonum_733(dp->size);
263 1.1 ws if (dirmatch(path, dp))
264 1.1 ws break;
265 1.32 christos psize += (size_t)isonum_711(dp->length);
266 1.23 isaki dp = (struct iso_directory_record *)
267 1.23 isaki ((char *)dp + isonum_711(dp->length));
268 1.1 ws }
269 1.1 ws
270 1.1 ws if (psize >= dsize) {
271 1.1 ws rc = ENOENT;
272 1.1 ws goto out;
273 1.1 ws }
274 1.16 perry
275 1.1 ws /* allocate file system specific data structure */
276 1.1 ws fp = alloc(sizeof(struct file));
277 1.17 junyoung memset(fp, 0, sizeof(struct file));
278 1.1 ws f->f_fsdata = (void *)fp;
279 1.1 ws
280 1.1 ws fp->off = 0;
281 1.1 ws fp->bno = isonum_733(dp->extent);
282 1.1 ws fp->size = isonum_733(dp->size);
283 1.20 christos dealloc(buf, buf_size);
284 1.24 ad fsmod = "cd9660";
285 1.16 perry
286 1.1 ws return 0;
287 1.16 perry
288 1.1 ws out:
289 1.1 ws if (fp)
290 1.20 christos dealloc(fp, sizeof(struct file));
291 1.20 christos dealloc(buf, buf_size);
292 1.16 perry
293 1.1 ws return rc;
294 1.1 ws }
295 1.1 ws
296 1.8 cgd #if !defined(LIBSA_NO_FS_CLOSE)
297 1.27 joerg __compactcall int
298 1.17 junyoung cd9660_close(struct open_file *f)
299 1.1 ws {
300 1.1 ws struct file *fp = (struct file *)f->f_fsdata;
301 1.16 perry
302 1.1 ws f->f_fsdata = 0;
303 1.20 christos dealloc(fp, sizeof *fp);
304 1.16 perry
305 1.1 ws return 0;
306 1.1 ws }
307 1.8 cgd #endif /* !defined(LIBSA_NO_FS_CLOSE) */
308 1.1 ws
309 1.27 joerg __compactcall int
310 1.17 junyoung cd9660_read(struct open_file *f, void *start, size_t size, size_t *resid)
311 1.1 ws {
312 1.1 ws struct file *fp = (struct file *)f->f_fsdata;
313 1.1 ws int rc = 0;
314 1.1 ws daddr_t bno;
315 1.1 ws char buf[ISO_DEFAULT_BLOCK_SIZE];
316 1.1 ws char *dp;
317 1.15 itohy size_t nread, off;
318 1.16 perry
319 1.1 ws while (size) {
320 1.1 ws if (fp->off < 0 || fp->off >= fp->size)
321 1.1 ws break;
322 1.1 ws bno = fp->off / ISO_DEFAULT_BLOCK_SIZE + fp->bno;
323 1.1 ws if (fp->off & (ISO_DEFAULT_BLOCK_SIZE - 1)
324 1.29 mhitch || (fp->off + ISO_DEFAULT_BLOCK_SIZE) > fp->size
325 1.1 ws || size < ISO_DEFAULT_BLOCK_SIZE)
326 1.1 ws dp = buf;
327 1.1 ws else
328 1.1 ws dp = start;
329 1.8 cgd #if !defined(LIBSA_NO_TWIDDLE)
330 1.16 perry twiddle();
331 1.8 cgd #endif
332 1.8 cgd rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ, cdb2devb(bno),
333 1.23 isaki ISO_DEFAULT_BLOCK_SIZE, dp, &nread);
334 1.1 ws if (rc)
335 1.1 ws return rc;
336 1.15 itohy if (nread != ISO_DEFAULT_BLOCK_SIZE)
337 1.1 ws return EIO;
338 1.1 ws if (dp == buf) {
339 1.32 christos size_t nr;
340 1.1 ws off = fp->off & (ISO_DEFAULT_BLOCK_SIZE - 1);
341 1.15 itohy if (nread > off + size)
342 1.15 itohy nread = off + size;
343 1.15 itohy nread -= off;
344 1.32 christos nr = (size_t)(fp->size - fp->off);
345 1.32 christos if (nread > nr)
346 1.32 christos nread = nr;
347 1.17 junyoung memcpy(start, buf + off, nread);
348 1.22 he start = (char *)start + nread;
349 1.32 christos fp->off += (off_t)nread;
350 1.15 itohy size -= nread;
351 1.1 ws } else {
352 1.22 he start = (char *)start + ISO_DEFAULT_BLOCK_SIZE;
353 1.1 ws fp->off += ISO_DEFAULT_BLOCK_SIZE;
354 1.1 ws size -= ISO_DEFAULT_BLOCK_SIZE;
355 1.1 ws }
356 1.1 ws }
357 1.25 jakllsch if(fp->off > fp->size)
358 1.32 christos size += (size_t)(fp->off - fp->size);
359 1.1 ws if (resid)
360 1.1 ws *resid = size;
361 1.1 ws return rc;
362 1.1 ws }
363 1.1 ws
364 1.8 cgd #if !defined(LIBSA_NO_FS_WRITE)
365 1.27 joerg __compactcall int
366 1.17 junyoung cd9660_write(struct open_file *f, void *start, size_t size, size_t *resid)
367 1.1 ws {
368 1.23 isaki
369 1.1 ws return EROFS;
370 1.1 ws }
371 1.8 cgd #endif /* !defined(LIBSA_NO_FS_WRITE) */
372 1.1 ws
373 1.8 cgd #if !defined(LIBSA_NO_FS_SEEK)
374 1.27 joerg __compactcall off_t
375 1.17 junyoung cd9660_seek(struct open_file *f, off_t offset, int where)
376 1.1 ws {
377 1.1 ws struct file *fp = (struct file *)f->f_fsdata;
378 1.16 perry
379 1.1 ws switch (where) {
380 1.1 ws case SEEK_SET:
381 1.1 ws fp->off = offset;
382 1.1 ws break;
383 1.1 ws case SEEK_CUR:
384 1.1 ws fp->off += offset;
385 1.1 ws break;
386 1.1 ws case SEEK_END:
387 1.1 ws fp->off = fp->size - offset;
388 1.1 ws break;
389 1.1 ws default:
390 1.1 ws return -1;
391 1.1 ws }
392 1.1 ws return fp->off;
393 1.1 ws }
394 1.8 cgd #endif /* !defined(LIBSA_NO_FS_SEEK) */
395 1.1 ws
396 1.27 joerg __compactcall int
397 1.17 junyoung cd9660_stat(struct open_file *f, struct stat *sb)
398 1.1 ws {
399 1.1 ws struct file *fp = (struct file *)f->f_fsdata;
400 1.16 perry
401 1.1 ws /* only importatn stuff */
402 1.1 ws sb->st_mode = S_IFREG | S_IRUSR | S_IRGRP | S_IROTH;
403 1.1 ws sb->st_uid = sb->st_gid = 0;
404 1.1 ws sb->st_size = fp->size;
405 1.1 ws return 0;
406 1.1 ws }
407 1.28 tsutsui
408 1.28 tsutsui #if defined(LIBSA_ENABLE_LS_OP)
409 1.30 christos #include "ls.h"
410 1.28 tsutsui __compactcall void
411 1.28 tsutsui cd9660_ls(struct open_file *f, const char *pattern)
412 1.28 tsutsui {
413 1.30 christos lsunsup("cd9660");
414 1.28 tsutsui }
415 1.28 tsutsui #endif
416