Document the recently added WITHOUT_SRCS variable.
[dragonfly.git] / contrib / libarchive / archive_read_support_format_iso9660.c
1 /*-
2  * Copyright (c) 2003-2004 Tim Kientzle
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer
10  *    in this position and unchanged.
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  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include "archive_platform.h"
28 __FBSDID("$FreeBSD: src/lib/libarchive/archive_read_support_format_iso9660.c,v 1.8 2005/04/06 04:19:30 kientzle Exp $");
29
30 #include <sys/stat.h>
31
32 #include <errno.h>
33 /* #include <stdint.h> */ /* See archive_platform.h */
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <time.h>
38 #include <unistd.h>
39
40 #include "archive.h"
41 #include "archive_entry.h"
42 #include "archive_private.h"
43 #include "archive_string.h"
44
45 /*
46  * An overview of ISO 9660 format:
47  *
48  * Each disk is laid out as follows:
49  *   * 32k reserved for private use
50  *   * Volume descriptor table.  Each volume descriptor
51  *     is 2k and specifies basic format information.
52  *     The "Primary Volume Descriptor" (PVD) is defined by the
53  *     standard and should always be present; other volume
54  *     descriptors include various vendor-specific extensions.
55  *   * Files and directories.  Each file/dir is specified by
56  *     an "extent" (starting sector and length in bytes).
57  *     Dirs are just files with directory records packed one
58  *     after another.  The PVD contains a single dir entry
59  *     specifying the location of the root directory.  Everything
60  *     else follows from there.
61  *
62  * This module works by first reading the volume descriptors, then
63  * building a list of directory entries, sorted by starting
64  * sector.  At each step, I look for the earliest dir entry that
65  * hasn't yet been read, seek forward to that location and read
66  * that entry.  If it's a dir, I slurp in the new dir entries and
67  * add them to the heap; if it's a regular file, I return the
68  * corresponding archive_entry and wait for the client to request
69  * the file body.  This strategy allows us to read most compliant
70  * CDs with a single pass through the data, as required by libarchive.
71  */
72
73 /* Structure of on-disk PVD. */
74 struct iso9660_primary_volume_descriptor {
75         unsigned char   type[1];
76         char    id[5];
77         unsigned char   version[1];
78         char    reserved1[1];
79         char    system_id[32];
80         char    volume_id[32];
81         char    reserved2[8];
82         char    volume_space_size[8];
83         char    reserved3[32];
84         char    volume_set_size[4];
85         char    volume_sequence_number[4];
86         char    logical_block_size[4];
87         char    path_table_size[8];
88         char    type_1_path_table[4];
89         char    opt_type_1_path_table[4];
90         char    type_m_path_table[4];
91         char    opt_type_m_path_table[4];
92         char    root_directory_record[34];
93         char    volume_set_id[128];
94         char    publisher_id[128];
95         char    preparer_id[128];
96         char    application_id[128];
97         char    copyright_file_id[37];
98         char    abstract_file_id[37];
99         char    bibliographic_file_id[37];
100         char    creation_date[17];
101         char    modification_date[17];
102         char    expiration_date[17];
103         char    effective_date[17];
104         char    file_structure_version[1];
105         char    reserved4[1];
106         char    application_data[512];
107 };
108
109 /* Structure of an on-disk directory record. */
110 struct iso9660_directory_record {
111         unsigned char length[1];
112         unsigned char ext_attr_length[1];
113         unsigned char extent[8];
114         unsigned char size[8];
115         char date[7];
116         unsigned char flags[1];
117         unsigned char file_unit_size[1];
118         unsigned char interleave[1];
119         unsigned char volume_sequence_number[4];
120         unsigned char name_len[1];
121         char name[1];
122 };
123
124
125 /*
126  * Our private data.
127  */
128
129 /* In-memory storage for a directory record. */
130 struct file_info {
131         struct file_info        *parent;
132         int              refcount;
133         uint64_t         offset;  /* Offset on disk. */
134         uint64_t         size;  /* File size in bytes. */
135         uint64_t         ce_offset; /* Offset of CE */
136         uint64_t         ce_size; /* Size of CE */
137         time_t           mtime; /* File last modified time. */
138         time_t           atime; /* File last accessed time. */
139         time_t           ctime; /* File creation time. */
140         mode_t           mode;
141         uid_t            uid;
142         gid_t            gid;
143         ino_t            inode;
144         int              nlinks;
145         char            *name; /* Null-terminated filename. */
146         struct archive_string symlink;
147 };
148
149
150 struct iso9660 {
151         int     magic;
152 #define ISO9660_MAGIC   0x96609660
153         int     bid; /* If non-zero, return this as our bid. */
154         struct archive_string pathname;
155         char    seenRockridge; /* Set true if RR extensions are used. */
156         unsigned char   suspOffset;
157
158         uint64_t        previous_offset;
159         uint64_t        previous_size;
160         struct archive_string previous_pathname;
161
162         /* TODO: Make this a heap for fast inserts and deletions. */
163         struct file_info **pending_files;
164         int     pending_files_allocated;
165         int     pending_files_used;
166
167         uint64_t current_position;
168         ssize_t logical_block_size;
169
170         off_t   entry_sparse_offset;
171         ssize_t entry_bytes_remaining;
172 };
173
174 static void     add_entry(struct iso9660 *iso9660, struct file_info *file);
175 static int      archive_read_format_iso9660_bid(struct archive *);
176 static int      archive_read_format_iso9660_cleanup(struct archive *);
177 static int      archive_read_format_iso9660_read_data(struct archive *,
178                     const void **, size_t *, off_t *);
179 static int      archive_read_format_iso9660_read_header(struct archive *,
180                     struct archive_entry *);
181 static const char *build_pathname(struct archive_string *, struct file_info *);
182 static void     dump_isodirrec(FILE *, const struct iso9660_directory_record *);
183 static time_t   isodate17(const void *);
184 static time_t   isodate7(const void *);
185 static int      isPVD(struct iso9660 *, const char *);
186 static struct file_info *next_entry(struct iso9660 *);
187 static int      next_entry_seek(struct archive *a, struct iso9660 *iso9660,
188                     struct file_info **pfile);
189 static struct file_info *
190                 parse_file_info(struct iso9660 *iso9660,
191                     struct file_info *parent,
192                     const struct iso9660_directory_record *isodirrec);
193 static void     parse_rockridge(struct iso9660 *iso9660,
194                     struct file_info *file, const unsigned char *start,
195                     const unsigned char *end);
196 static void     release_file(struct iso9660 *, struct file_info *);
197 static int      toi(const void *p, int n);
198
199 int
200 archive_read_support_format_iso9660(struct archive *a)
201 {
202         struct iso9660 *iso9660;
203         int r;
204
205         iso9660 = malloc(sizeof(*iso9660));
206         memset(iso9660, 0, sizeof(*iso9660));
207         iso9660->magic = ISO9660_MAGIC;
208         iso9660->bid = -1; /* We haven't yet bid. */
209
210         r = __archive_read_register_format(a,
211             iso9660,
212             archive_read_format_iso9660_bid,
213             archive_read_format_iso9660_read_header,
214             archive_read_format_iso9660_read_data,
215             NULL,
216             archive_read_format_iso9660_cleanup);
217
218         if (r != ARCHIVE_OK) {
219                 free(iso9660);
220                 return (r);
221         }
222         return (ARCHIVE_OK);
223 }
224
225
226 static int
227 archive_read_format_iso9660_bid(struct archive *a)
228 {
229         struct iso9660 *iso9660;
230         ssize_t bytes_read;
231         const void *h;
232         const char *p;
233
234         iso9660 = *(a->pformat_data);
235
236         if (iso9660->bid >= 0)
237                 return (iso9660->bid);
238
239         /*
240          * Skip the first 32k (reserved area) and get the first
241          * 8 sectors of the volume descriptor table.  Of course,
242          * if the I/O layer gives us more, we'll take it.
243          */
244         bytes_read = (a->compression_read_ahead)(a, &h, 32768 + 8*2048);
245         if (bytes_read < 32768 + 8*2048)
246             return (iso9660->bid = -1);
247         p = (const char *)h;
248
249         /* Skip the reserved area. */
250         bytes_read -= 32768;
251         p += 32768;
252
253         /* Check each volume descriptor to locate the PVD. */
254         for (; bytes_read > 2048; bytes_read -= 2048, p += 2048) {
255                 iso9660->bid = isPVD(iso9660, p);
256                 if (iso9660->bid > 0)
257                         return (iso9660->bid);
258                 if (*p == '\xff') /* End-of-volume-descriptor marker. */
259                         break;
260         }
261
262         /* We didn't find a valid PVD; return a bid of zero. */
263         iso9660->bid = 0;
264         return (iso9660->bid);
265 }
266
267 static int
268 isPVD(struct iso9660 *iso9660, const char *h)
269 {
270         const struct iso9660_primary_volume_descriptor *voldesc;
271         struct file_info *file;
272
273         if (h[0] != 1)
274                 return (0);
275         if (memcmp(h+1, "CD001", 5) != 0)
276                 return (0);
277
278
279         voldesc = (const struct iso9660_primary_volume_descriptor *)h;
280         iso9660->logical_block_size = toi(&voldesc->logical_block_size, 2);
281
282         /* Store the root directory in the pending list. */
283         file = parse_file_info(iso9660, NULL,
284             (struct iso9660_directory_record *)&voldesc->root_directory_record);
285         add_entry(iso9660, file);
286         return (48);
287 }
288
289 static int
290 archive_read_format_iso9660_read_header(struct archive *a,
291     struct archive_entry *entry)
292 {
293         struct stat st;
294         struct iso9660 *iso9660;
295         struct file_info *file;
296         ssize_t bytes_read;
297         int r;
298
299         iso9660 = *(a->pformat_data);
300
301         if (iso9660->seenRockridge) {
302                 a->archive_format = ARCHIVE_FORMAT_ISO9660_ROCKRIDGE;
303                 a->archive_format_name = "ISO9660 with Rockridge extensions";
304         } else {
305                 a->archive_format = ARCHIVE_FORMAT_ISO9660;
306                 a->archive_format_name = "ISO9660";
307         }
308
309         /* Get the next entry that appears after the current offset. */
310         r = next_entry_seek(a, iso9660, &file);
311         if (r != ARCHIVE_OK)
312                 return (r);
313
314         iso9660->entry_bytes_remaining = file->size;
315         iso9660->entry_sparse_offset = 0; /* Offset for sparse-file-aware clients. */
316
317         /* Set up the entry structure with information about this entry. */
318         memset(&st, 0, sizeof(st));
319         st.st_mode = file->mode;
320         st.st_uid = file->uid;
321         st.st_gid = file->gid;
322         st.st_nlink = file->nlinks;
323         st.st_ino = file->inode;
324         st.st_mtime = file->mtime;
325         st.st_ctime = file->ctime;
326         st.st_atime = file->atime;
327         st.st_size = iso9660->entry_bytes_remaining;
328         archive_entry_copy_stat(entry, &st);
329         archive_string_empty(&iso9660->pathname);
330         archive_entry_set_pathname(entry,
331             build_pathname(&iso9660->pathname, file));
332         if (file->symlink.s != NULL)
333                 archive_entry_set_symlink(entry, file->symlink.s);
334
335         /* If this entry points to the same data as the previous
336          * entry, convert this into a hardlink to that entry.
337          * But don't bother for zero-length files. */
338         if (file->offset == iso9660->previous_offset
339             && file->size == iso9660->previous_size
340             && file->size > 0) {
341                 archive_entry_set_hardlink(entry,
342                     iso9660->previous_pathname.s);
343                 iso9660->entry_bytes_remaining = 0;
344                 iso9660->entry_sparse_offset = 0;
345                 release_file(iso9660, file);
346                 return (ARCHIVE_OK);
347         }
348
349         /* If the offset is before our current position, we can't
350          * seek backwards to extract it, so issue a warning. */
351         if (file->offset < iso9660->current_position) {
352                 archive_set_error(a, ARCHIVE_ERRNO_MISC,
353                     "Ignoring out-of-order file");
354                 iso9660->entry_bytes_remaining = 0;
355                 iso9660->entry_sparse_offset = 0;
356                 release_file(iso9660, file);
357                 return (ARCHIVE_WARN);
358         }
359
360         iso9660->previous_size = file->size;
361         iso9660->previous_offset = file->offset;
362         archive_strcpy(&iso9660->previous_pathname, iso9660->pathname.s);
363
364         /* If this is a directory, read in all of the entries right now. */
365         if (S_ISDIR(st.st_mode)) {
366                 while(iso9660->entry_bytes_remaining > 0) {
367                         const void *block;
368                         const unsigned char *p;
369                         ssize_t step = iso9660->logical_block_size;
370                         if (step > iso9660->entry_bytes_remaining)
371                                 step = iso9660->entry_bytes_remaining;
372                         bytes_read = (a->compression_read_ahead)(a, &block, step);
373                         if (bytes_read < step) {
374                                 archive_set_error(a, ARCHIVE_ERRNO_MISC,
375             "Failed to read full block when scanning ISO9660 directory list");
376                                 release_file(iso9660, file);
377                                 return (ARCHIVE_FATAL);
378                         }
379                         if (bytes_read > step)
380                                 bytes_read = step;
381                         (a->compression_read_consume)(a, bytes_read);
382                         iso9660->current_position += bytes_read;
383                         iso9660->entry_bytes_remaining -= bytes_read;
384                         for (p = block;
385                              *p != 0 && p < (const unsigned char *)block + bytes_read;
386                              p += *p) {
387                                 const struct iso9660_directory_record *dr
388                                     = (const struct iso9660_directory_record *)p;
389                                 struct file_info *child;
390
391                                 /* Skip '.' entry. */
392                                 if (dr->name_len[0] == 1
393                                     && dr->name[0] == '\0')
394                                         continue;
395                                 /* Skip '..' entry. */
396                                 if (dr->name_len[0] == 1
397                                     && dr->name[0] == '\001')
398                                         continue;
399                                 child = parse_file_info(iso9660, file, dr);
400                                 add_entry(iso9660, child);
401                         }
402                 }
403         }
404
405         release_file(iso9660, file);
406         return (ARCHIVE_OK);
407 }
408
409 static int
410 archive_read_format_iso9660_read_data(struct archive *a,
411     const void **buff, size_t *size, off_t *offset)
412 {
413         ssize_t bytes_read;
414         struct iso9660 *iso9660;
415
416         iso9660 = *(a->pformat_data);
417         if (iso9660->entry_bytes_remaining <= 0) {
418                 *buff = NULL;
419                 *size = 0;
420                 *offset = iso9660->entry_sparse_offset;
421                 return (ARCHIVE_EOF);
422         }
423
424         bytes_read = (a->compression_read_ahead)(a, buff, 1);
425         if (bytes_read <= 0)
426                 return (ARCHIVE_FATAL);
427         if (bytes_read > iso9660->entry_bytes_remaining)
428                 bytes_read = iso9660->entry_bytes_remaining;
429         *size = bytes_read;
430         *offset = iso9660->entry_sparse_offset;
431         iso9660->entry_sparse_offset += bytes_read;
432         iso9660->entry_bytes_remaining -= bytes_read;
433         iso9660->current_position += bytes_read;
434         (a->compression_read_consume)(a, bytes_read);
435         return (ARCHIVE_OK);
436 }
437
438 static int
439 archive_read_format_iso9660_cleanup(struct archive *a)
440 {
441         struct iso9660 *iso9660;
442         struct file_info *file;
443
444         iso9660 = *(a->pformat_data);
445         while ((file = next_entry(iso9660)) != NULL)
446                 release_file(iso9660, file);
447         archive_string_free(&iso9660->pathname);
448         archive_string_free(&iso9660->previous_pathname);
449         free(iso9660);
450         *(a->pformat_data) = NULL;
451         return (ARCHIVE_OK);
452 }
453
454 /*
455  * This routine parses a single ISO directory record, makes sense
456  * of any extensions, and stores the result in memory.
457  */
458 static struct file_info *
459 parse_file_info(struct iso9660 *iso9660, struct file_info *parent,
460     const struct iso9660_directory_record *isodirrec)
461 {
462         struct file_info *file;
463
464         /* TODO: Sanity check that name_len doesn't exceed length, etc. */
465
466         /* Create a new file entry and copy data from the ISO dir record. */
467         file = malloc(sizeof(*file));
468         memset(file, 0, sizeof(*file));
469         file->parent = parent;
470         if (parent != NULL)
471                 parent->refcount++;
472         file->offset = toi(isodirrec->extent, 4)
473             * iso9660->logical_block_size;
474         file->size = toi(isodirrec->size, 4);
475         file->mtime = isodate7(isodirrec->date);
476         file->ctime = file->atime = file->mtime;
477         file->name = malloc(isodirrec->name_len[0] + 1);
478         memcpy(file->name, isodirrec->name, isodirrec->name_len[0]);
479         file->name[(int)isodirrec->name_len[0]] = '\0';
480         if (isodirrec->flags[0] & 0x02)
481                 file->mode = S_IFDIR | 0700;
482         else
483                 file->mode = S_IFREG | 0400;
484
485         /* Rockridge extensions overwrite information from above. */
486         {
487                 const unsigned char *rr_start, *rr_end;
488                 rr_end = (const unsigned char *)isodirrec
489                     + isodirrec->length[0];
490                 rr_start = isodirrec->name + isodirrec->name_len[0];
491                 if ((isodirrec->name_len[0] & 1) == 0)
492                         rr_start++;
493                 rr_start += iso9660->suspOffset;
494                 parse_rockridge(iso9660, file, rr_start, rr_end);
495         }
496
497         /* DEBUGGING: Warn about attributes I don't yet fully support. */
498         if ((isodirrec->flags[0] & ~0x02) != 0) {
499                 fprintf(stderr, "\n ** Unrecognized flag: ");
500                 dump_isodirrec(stderr, isodirrec);
501                 fprintf(stderr, "\n");
502         } else if (toi(isodirrec->volume_sequence_number, 2) != 1) {
503                 fprintf(stderr, "\n ** Unrecognized sequence number: ");
504                 dump_isodirrec(stderr, isodirrec);
505                 fprintf(stderr, "\n");
506         } else if (isodirrec->file_unit_size[0] != 0) {
507                 fprintf(stderr, "\n ** Unexpected file unit size: ");
508                 dump_isodirrec(stderr, isodirrec);
509                 fprintf(stderr, "\n");
510         } else if (isodirrec->interleave[0] != 0) {
511                 fprintf(stderr, "\n ** Unexpected interleave: ");
512                 dump_isodirrec(stderr, isodirrec);
513                 fprintf(stderr, "\n");
514         } else if (isodirrec->ext_attr_length[0] != 0) {
515                 fprintf(stderr, "\n ** Unexpected extended attribute length: ");
516                 dump_isodirrec(stderr, isodirrec);
517                 fprintf(stderr, "\n");
518         }
519
520         return (file);
521 }
522
523 static void
524 add_entry(struct iso9660 *iso9660, struct file_info *file)
525 {
526         /* Expand our pending files list as necessary. */
527         if (iso9660->pending_files_used >= iso9660->pending_files_allocated) {
528                 struct file_info **new_pending_files;
529                 int new_size = iso9660->pending_files_allocated * 2;
530
531                 if (new_size < 1024)
532                         new_size = 1024;
533                 new_pending_files = malloc(new_size * sizeof(new_pending_files[0]));
534                 memcpy(new_pending_files, iso9660->pending_files,
535                     iso9660->pending_files_allocated * sizeof(new_pending_files[0]));
536                 if (iso9660->pending_files != NULL)
537                         free(iso9660->pending_files);
538                 iso9660->pending_files = new_pending_files;
539                 iso9660->pending_files_allocated = new_size;
540         }
541
542         iso9660->pending_files[iso9660->pending_files_used++] = file;
543 }
544
545 static void
546 parse_rockridge(struct iso9660 *iso9660, struct file_info *file,
547     const unsigned char *p, const unsigned char *end)
548 {
549         (void)iso9660; /* UNUSED */
550
551         while (p + 4 < end  /* Enough space for another entry. */
552             && p[0] >= 'A' && p[0] <= 'Z' /* Sanity-check 1st char of name. */
553             && p[1] >= 'A' && p[1] <= 'Z' /* Sanity-check 2nd char of name. */
554             && p + p[2] <= end) { /* Sanity-check length. */
555                 const unsigned char *data = p + 4;
556                 int data_length = p[2] - 4;
557                 int version = p[3];
558
559                 /*
560                  * Yes, each 'if' here does test p[0] again.
561                  * Otherwise, the fall-through handling to catch
562                  * unsupported extensions doesn't work.
563                  */
564                 switch(p[0]) {
565                 case 'C':
566                         if (p[0] == 'C' && p[1] == 'E' && version == 1) {
567                                 /*
568                                  * CE extension comprises:
569                                  *   8 byte sector containing extension
570                                  *   8 byte offset w/in above sector
571                                  *   8 byte length of continuation
572                                  */
573                                 file->ce_offset = toi(data, 4)
574                                     * iso9660->logical_block_size
575                                     + toi(data + 8, 4);
576                                 file->ce_size = toi(data + 16, 4);
577                                 break;
578                         }
579                         /* FALLTHROUGH */
580                 case 'N':
581                         if (p[0] == 'N' && p[1] == 'M' && version == 1
582                                 && *data == 0) {
583                                 /* NM extension with flag byte == 0 */
584                                 /*
585                                  * NM extension comprises:
586                                  *   one byte flag
587                                  *   rest is long name
588                                  */
589                                 /* TODO: Obey flags. */
590                                 char *old_name = file->name;
591
592                                 data++;  /* Skip flag byte. */
593                                 data_length--;
594                                 file->name = malloc(data_length + 1);
595                                 if (file->name != NULL) {
596                                         free(old_name);
597                                         memcpy(file->name, data, data_length);
598                                         file->name[data_length] = '\0';
599                                 } else
600                                         file->name = old_name;
601                                 break;
602                         }
603                         /* FALLTHROUGH */
604                 case 'P':
605                         if (p[0] == 'P' && p[1] == 'D' && version == 1) {
606                                 /*
607                                  * PD extension is padding;
608                                  * contents are always ignored.
609                                  */
610                                 break;
611                         }
612                         if (p[0] == 'P' && p[1] == 'X' && version == 1) {
613                                 /*
614                                  * PX extension comprises:
615                                  *   8 bytes for mode,
616                                  *   8 bytes for nlinks,
617                                  *   8 bytes for uid,
618                                  *   8 bytes for gid,
619                                  *   8 bytes for inode.
620                                  */
621                                 if (data_length == 32) {
622                                         file->mode = toi(data, 4);
623                                         file->nlinks = toi(data + 8, 4);
624                                         file->uid = toi(data + 16, 4);
625                                         file->gid = toi(data + 24, 4);
626                                         file->inode = toi(data + 32, 4);
627                                 }
628                                 break;
629                         }
630                         /* FALLTHROUGH */
631                 case 'R':
632                         if (p[0] == 'R' && p[1] == 'R' && version == 1) {
633                                 iso9660->seenRockridge = 1;
634                                 /*
635                                  * RR extension comprises:
636                                  *    one byte flag value
637                                  */
638                                 /* TODO: Handle RR extension. */
639                                 break;
640                         }
641                         /* FALLTHROUGH */
642                 case 'S':
643                         if (p[0] == 'S' && p[1] == 'L' && version == 1
644                             && *data == 0) {
645                                 int cont = 1;
646                                 /* SL extension with flags == 0 */
647                                 /* TODO: handle non-zero flag values. */
648                                 data++;  /* Skip flag byte. */
649                                 data_length--;
650                                 while (data_length > 0) {
651                                         unsigned char flag = *data++;
652                                         unsigned char nlen = *data++;
653                                         data_length -= 2;
654
655                                         if (cont == 0)
656                                                 archive_strcat(&file->symlink, "/");
657                                         cont = 0;
658
659                                         switch(flag) {
660                                         case 0x01: /* Continue */
661                                                 archive_strncat(&file->symlink, data, nlen);
662                                                 cont = 1;
663                                                 break;
664                                         case 0x02: /* Current */
665                                                 archive_strcat(&file->symlink, ".");
666                                                 break;
667                                         case 0x04: /* Parent */
668                                                 archive_strcat(&file->symlink, "..");
669                                                 break;
670                                         case 0x08: /* Root */
671                                         case 0x10: /* Volume root */
672                                                 archive_string_empty(&file->symlink);
673                                                 break;
674                                         case 0x20: /* Hostname */
675                                                 archive_strcat(&file->symlink, "hostname");
676                                                 break;
677                                         case 0:
678                                                 archive_strncat(&file->symlink, data, nlen);
679                                                 break;
680                                         default:
681                                                 /* TODO: issue a warning ? */
682                                                 break;
683                                         }
684                                         data += nlen;
685                                         data_length -= nlen;
686                                 }
687                                 break;
688                         }
689                         if (p[0] == 'S' && p[1] == 'P'
690                             && version == 1 && data_length == 7
691                             && data[0] == (unsigned char)'\xbe'
692                             && data[1] == (unsigned char)'\xef') {
693                                 /*
694                                  * SP extension stores the suspOffset
695                                  * (Number of bytes to skip between
696                                  * filename and SUSP records.)
697                                  * It is mandatory by the SUSP standard
698                                  * (IEEE 1281).
699                                  *
700                                  * It allows SUSP to coexist with
701                                  * non-SUSP uses of the System
702                                  * Use Area by placing non-SUSP data
703                                  * before SUSP data.
704                                  *
705                                  * TODO: Add a check for 'SP' in
706                                  * first directory entry, disable all SUSP
707                                  * processing if not found.
708                                  */
709                                 iso9660->suspOffset = data[2];
710                                 break;
711                         }
712                         if (p[0] == 'S' && p[1] == 'T'
713                             && data_length == 0 && version == 1) {
714                                 /*
715                                  * ST extension marks end of this
716                                  * block of SUSP entries.
717                                  *
718                                  * It allows SUSP to coexist with
719                                  * non-SUSP uses of the System
720                                  * Use Area by placing non-SUSP data
721                                  * after SUSP data.
722                                  */
723                                 return;
724                         }
725                 case 'T':
726                         if (p[0] == 'T' && p[1] == 'F' && version == 1) {
727                                 char flag = data[0];
728                                 /*
729                                  * TF extension comprises:
730                                  *   one byte flag
731                                  *   create time (optional)
732                                  *   modify time (optional)
733                                  *   access time (optional)
734                                  *   attribute time (optional)
735                                  *  Time format and presence of fields
736                                  *  is controlled by flag bits.
737                                  */
738                                 data++;
739                                 if (flag & 0x80) {
740                                         /* Use 17-byte time format. */
741                                         if (flag & 1) /* Create time. */
742                                                 data += 17;
743                                         if (flag & 2) { /* Modify time. */
744                                                 file->mtime = isodate17(data);
745                                                 data += 17;
746                                         }
747                                         if (flag & 4) { /* Access time. */
748                                                 file->atime = isodate17(data);
749                                                 data += 17;
750                                         }
751                                         if (flag & 8) { /* Attribute time. */
752                                                 file->ctime = isodate17(data);
753                                                 data += 17;
754                                         }
755                                 } else {
756                                         /* Use 7-byte time format. */
757                                         if (flag & 1) /* Create time. */
758                                                 data += 7;
759                                         if (flag & 2) { /* Modify time. */
760                                                 file->mtime = isodate7(data);
761                                                 data += 7;
762                                         }
763                                         if (flag & 4) { /* Access time. */
764                                                 file->atime = isodate7(data);
765                                                 data += 7;
766                                         }
767                                         if (flag & 8) { /* Attribute time. */
768                                                 file->ctime = isodate7(data);
769                                                 data += 7;
770                                         }
771                                 }
772                                 break;
773                         }
774                         /* FALLTHROUGH */
775                 default:
776                         /* The FALLTHROUGHs above leave us here for
777                          * any unsupported extension. */
778                         {
779                                 const unsigned char *t;
780                                 fprintf(stderr, "\nUnsupported RRIP extension for %s\n", file->name);
781                                 fprintf(stderr, " %c%c(%d):", p[0], p[1], data_length);
782                                 for (t = data; t < data + data_length && t < data + 16; t++)
783                                         fprintf(stderr, " %02x", *t);
784                                 fprintf(stderr, "\n");
785                         }
786                 }
787
788
789
790                 p += p[2];
791         }
792 }
793
794 static void
795 release_file(struct iso9660 *iso9660, struct file_info *file)
796 {
797         struct file_info *parent;
798
799         if (file->refcount == 0) {
800                 parent = file->parent;
801                 if (file->name)
802                         free(file->name);
803                 archive_string_free(&file->symlink);
804                 free(file);
805                 if (parent != NULL) {
806                         parent->refcount--;
807                         release_file(iso9660, parent);
808                 }
809         }
810 }
811
812 static int
813 next_entry_seek(struct archive *a, struct iso9660 *iso9660,
814     struct file_info **pfile)
815 {
816         struct file_info *file;
817         uint64_t offset;
818
819         *pfile = NULL;
820         for (;;) {
821                 *pfile = file = next_entry(iso9660);
822                 if (file == NULL)
823                         return (ARCHIVE_EOF);
824
825                 /* CE area precedes actual file data? Ignore it. */
826                 if (file->ce_offset > file->offset) {
827 fprintf(stderr, " *** Discarding CE data.\n");
828                         file->ce_offset = 0;
829                         file->ce_size = 0;
830                 }
831
832                 /* If CE exists, find and read it now. */
833                 if (file->ce_offset > 0)
834                         offset = file->ce_offset;
835                 else
836                         offset = file->offset;
837
838                 /* Seek forward to the start of the entry. */
839                 while (iso9660->current_position < offset) {
840                         ssize_t step = offset - iso9660->current_position;
841                         ssize_t bytes_read;
842                         const void *buff;
843
844                         if (step > iso9660->logical_block_size)
845                                 step = iso9660->logical_block_size;
846                         bytes_read = (a->compression_read_ahead)(a, &buff, step);
847                         if (bytes_read <= 0) {
848                                 release_file(iso9660, file);
849                                 return (ARCHIVE_FATAL);
850                         }
851                         if (bytes_read > step)
852                                 bytes_read = step;
853                         iso9660->current_position += bytes_read;
854                         (a->compression_read_consume)(a, bytes_read);
855                 }
856
857                 /* We found body of file; handle it now. */
858                 if (offset == file->offset)
859                         return (ARCHIVE_OK);
860
861                 /* Found CE?  Process it and push the file back onto list. */
862                 if (offset == file->ce_offset) {
863                         const void *p;
864                         ssize_t size = file->ce_size;
865                         ssize_t bytes_read;
866                         const unsigned char *rr_start;
867
868                         file->ce_offset = 0;
869                         file->ce_size = 0;
870                         bytes_read = (a->compression_read_ahead)(a, &p, size);
871                         if (bytes_read > size)
872                                 bytes_read = size;
873                         rr_start = (const unsigned char *)p;
874                         parse_rockridge(iso9660, file, rr_start,
875                             rr_start + bytes_read);
876                         (a->compression_read_consume)(a, bytes_read);
877                         iso9660->current_position += bytes_read;
878                         add_entry(iso9660, file);
879                 }
880         }
881 }
882
883 static struct file_info *
884 next_entry(struct iso9660 *iso9660)
885 {
886         int least_index;
887         uint64_t least_end_offset;
888         int i;
889         struct file_info *r;
890
891         if (iso9660->pending_files_used < 1)
892                 return (NULL);
893
894         /* Assume the first file in the list is the earliest on disk. */
895         least_index = 0;
896         least_end_offset = iso9660->pending_files[0]->offset
897             + iso9660->pending_files[0]->size;
898
899         /* Now, try to find an earlier one. */
900         for(i = 0; i < iso9660->pending_files_used; i++) {
901                 /* Use the position of the file *end* as our comparison. */
902                 uint64_t end_offset = iso9660->pending_files[i]->offset
903                     + iso9660->pending_files[i]->size;
904                 if (iso9660->pending_files[i]->ce_offset > 0
905                     && iso9660->pending_files[i]->ce_offset < iso9660->pending_files[i]->offset)
906                         end_offset = iso9660->pending_files[i]->ce_offset
907                     + iso9660->pending_files[i]->ce_size;
908                 if (least_end_offset > end_offset) {
909                         least_index = i;
910                         least_end_offset = end_offset;
911                 }
912         }
913         r = iso9660->pending_files[least_index];
914         iso9660->pending_files[least_index]
915             = iso9660->pending_files[--iso9660->pending_files_used];
916         return (r);
917 }
918
919 static int
920 toi(const void *p, int n)
921 {
922         const unsigned char *v = (const unsigned char *)p;
923         if (n > 1)
924                 return v[0] + 256 * toi(v + 1, n - 1);
925         if (n == 1)
926                 return v[0];
927         return (0);
928 }
929
930 static time_t
931 isodate7(const void *p)
932 {
933         struct tm tm;
934         const unsigned char *v = (const unsigned char *)p;
935         int offset;
936         tm.tm_year = v[0];
937         tm.tm_mon = v[1] - 1;
938         tm.tm_mday = v[2];
939         tm.tm_hour = v[3];
940         tm.tm_min = v[4];
941         tm.tm_sec = v[5];
942         /* v[6] is the timezone offset, in 1/4-hour increments. */
943         offset = ((const signed char *)p)[6];
944         if (offset > -48 && offset < 52) {
945                 tm.tm_hour -= offset / 4;
946                 tm.tm_min -= (offset % 4) * 15;
947         }
948         return (timegm(&tm));
949 }
950
951 static time_t
952 isodate17(const void *p)
953 {
954         struct tm tm;
955         const unsigned char *v = (const unsigned char *)p;
956         int offset;
957         tm.tm_year = (v[0] - '0') * 1000 + (v[1] - '0') * 100
958             + (v[2] - '0') * 10 + (v[3] - '0')
959             - 1900;
960         tm.tm_mon = (v[4] - '0') * 10 + (v[5] - '0');
961         tm.tm_mday = (v[6] - '0') * 10 + (v[7] - '0');
962         tm.tm_hour = (v[8] - '0') * 10 + (v[9] - '0');
963         tm.tm_min = (v[10] - '0') * 10 + (v[11] - '0');
964         tm.tm_sec = (v[12] - '0') * 10 + (v[13] - '0');
965         /* v[16] is the timezone offset, in 1/4-hour increments. */
966         offset = ((const signed char *)p)[16];
967         if (offset > -48 && offset < 52) {
968                 tm.tm_hour -= offset / 4;
969                 tm.tm_min -= (offset % 4) * 15;
970         }
971         return (timegm(&tm));
972 }
973
974 static const char *
975 build_pathname(struct archive_string *as, struct file_info *file)
976 {
977         if (file->parent != NULL && file->parent->name[0] != '\0') {
978                 build_pathname(as, file->parent);
979                 archive_strcat(as, "/");
980         }
981         if (file->name[0] == '\0')
982                 archive_strcat(as, ".");
983         else
984                 archive_strcat(as, file->name);
985         return (as->s);
986 }
987
988 static void
989 dump_isodirrec(FILE *out, const struct iso9660_directory_record *isodirrec)
990 {
991         fprintf(out, " l %d,", isodirrec->length[0]);
992         fprintf(out, " a %d,", isodirrec->ext_attr_length[0]);
993         fprintf(out, " ext 0x%x,", toi(isodirrec->extent, 4));
994         fprintf(out, " s %d,", toi(isodirrec->size, 4));
995         fprintf(out, " f 0x%02x,", isodirrec->flags[0]);
996         fprintf(out, " u %d,", isodirrec->file_unit_size[0]);
997         fprintf(out, " ilv %d,", isodirrec->interleave[0]);
998         fprintf(out, " seq %d,", toi(isodirrec->volume_sequence_number,2));
999         fprintf(out, " nl %d:", isodirrec->name_len[0]);
1000         fprintf(out, " `%.*s'", isodirrec->name_len[0], isodirrec->name);
1001 }