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