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