]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libarchive/archive_read_support_format_iso9660.c
Merge from head up to r188941 (last revision before the USB stack switch)
[FreeBSD/FreeBSD.git] / lib / 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$");
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 #define PVD_reserved5_offset (PVD_application_data_offset + PVD_application_data_size)
142 #define PVD_reserved5_size (2048 - PVD_reserved5_offset)
143
144 /* TODO: It would make future maintenance easier to just hardcode the
145  * above values.  In particular, ECMA119 states the offsets as part of
146  * the standard.  That would eliminate the need for the following check.*/
147 #if PVD_reserved5_offset != 1395
148 #error PVD offset and size definitions are wrong.
149 #endif
150
151 /* Structure of an on-disk directory record. */
152 /* Note:  ISO9660 stores each multi-byte integer twice, once in
153  * each byte order.  The sizes here are the size of just one
154  * of the two integers.  (This is why the offset of a field isn't
155  * the same as the offset+size of the previous field.) */
156 #define DR_length_offset 0
157 #define DR_length_size 1
158 #define DR_ext_attr_length_offset 1
159 #define DR_ext_attr_length_size 1
160 #define DR_extent_offset 2
161 #define DR_extent_size 4
162 #define DR_size_offset 10
163 #define DR_size_size 4
164 #define DR_date_offset 18
165 #define DR_date_size 7
166 #define DR_flags_offset 25
167 #define DR_flags_size 1
168 #define DR_file_unit_size_offset 26
169 #define DR_file_unit_size_size 1
170 #define DR_interleave_offset 27
171 #define DR_interleave_size 1
172 #define DR_volume_sequence_number_offset 28
173 #define DR_volume_sequence_number_size 2
174 #define DR_name_len_offset 32
175 #define DR_name_len_size 1
176 #define DR_name_offset 33
177
178 /*
179  * Our private data.
180  */
181
182 /* In-memory storage for a directory record. */
183 struct file_info {
184         struct file_info        *parent;
185         int              refcount;
186         uint64_t         offset;  /* Offset on disk. */
187         uint64_t         size;  /* File size in bytes. */
188         uint64_t         ce_offset; /* Offset of CE */
189         uint64_t         ce_size; /* Size of CE */
190         time_t           birthtime; /* File created time. */
191         time_t           mtime; /* File last modified time. */
192         time_t           atime; /* File last accessed time. */
193         time_t           ctime; /* File attribute change time. */
194         uint64_t         rdev; /* Device number */
195         mode_t           mode;
196         uid_t            uid;
197         gid_t            gid;
198         ino_t            inode;
199         int              nlinks;
200         struct archive_string name; /* Pathname */
201         char             name_continues; /* Non-zero if name continues */
202         struct archive_string symlink;
203         char             symlink_continues; /* Non-zero if link continues */
204 };
205
206
207 struct iso9660 {
208         int     magic;
209 #define ISO9660_MAGIC   0x96609660
210         struct archive_string pathname;
211         char    seenRockridge; /* Set true if RR extensions are used. */
212         unsigned char   suspOffset;
213
214         uint64_t        previous_offset;
215         uint64_t        previous_size;
216         struct archive_string previous_pathname;
217
218         /* TODO: Make this a heap for fast inserts and deletions. */
219         struct file_info **pending_files;
220         int     pending_files_allocated;
221         int     pending_files_used;
222
223         uint64_t current_position;
224         ssize_t logical_block_size;
225         uint64_t volume_size; /* Total size of volume in bytes. */
226
227         off_t   entry_sparse_offset;
228         int64_t entry_bytes_remaining;
229 };
230
231 static void     add_entry(struct iso9660 *iso9660, struct file_info *file);
232 static int      archive_read_format_iso9660_bid(struct archive_read *);
233 static int      archive_read_format_iso9660_cleanup(struct archive_read *);
234 static int      archive_read_format_iso9660_read_data(struct archive_read *,
235                     const void **, size_t *, off_t *);
236 static int      archive_read_format_iso9660_read_data_skip(struct archive_read *);
237 static int      archive_read_format_iso9660_read_header(struct archive_read *,
238                     struct archive_entry *);
239 static const char *build_pathname(struct archive_string *, struct file_info *);
240 #if DEBUG
241 static void     dump_isodirrec(FILE *, const unsigned char *isodirrec);
242 #endif
243 static time_t   time_from_tm(struct tm *);
244 static time_t   isodate17(const unsigned char *);
245 static time_t   isodate7(const unsigned char *);
246 static int      isPVD(struct iso9660 *, const unsigned char *);
247 static struct file_info *next_entry(struct iso9660 *);
248 static int      next_entry_seek(struct archive_read *a, struct iso9660 *iso9660,
249                     struct file_info **pfile);
250 static struct file_info *
251                 parse_file_info(struct iso9660 *iso9660,
252                     struct file_info *parent, const unsigned char *isodirrec);
253 static void     parse_rockridge(struct iso9660 *iso9660,
254                     struct file_info *file, const unsigned char *start,
255                     const unsigned char *end);
256 static void     parse_rockridge_NM1(struct file_info *,
257                     const unsigned char *, int);
258 static void     parse_rockridge_SL1(struct file_info *,
259                     const unsigned char *, int);
260 static void     parse_rockridge_TF1(struct file_info *,
261                     const unsigned char *, int);
262 static void     release_file(struct iso9660 *, struct file_info *);
263 static unsigned toi(const void *p, int n);
264
265 int
266 archive_read_support_format_iso9660(struct archive *_a)
267 {
268         struct archive_read *a = (struct archive_read *)_a;
269         struct iso9660 *iso9660;
270         int r;
271
272         iso9660 = (struct iso9660 *)malloc(sizeof(*iso9660));
273         if (iso9660 == NULL) {
274                 archive_set_error(&a->archive, ENOMEM, "Can't allocate iso9660 data");
275                 return (ARCHIVE_FATAL);
276         }
277         memset(iso9660, 0, sizeof(*iso9660));
278         iso9660->magic = ISO9660_MAGIC;
279
280         r = __archive_read_register_format(a,
281             iso9660,
282             archive_read_format_iso9660_bid,
283             archive_read_format_iso9660_read_header,
284             archive_read_format_iso9660_read_data,
285             archive_read_format_iso9660_read_data_skip,
286             archive_read_format_iso9660_cleanup);
287
288         if (r != ARCHIVE_OK) {
289                 free(iso9660);
290                 return (r);
291         }
292         return (ARCHIVE_OK);
293 }
294
295
296 static int
297 archive_read_format_iso9660_bid(struct archive_read *a)
298 {
299         struct iso9660 *iso9660;
300         ssize_t bytes_read;
301         const void *h;
302         const unsigned char *p;
303         int bid;
304
305         iso9660 = (struct iso9660 *)(a->format->data);
306
307         /*
308          * Skip the first 32k (reserved area) and get the first
309          * 8 sectors of the volume descriptor table.  Of course,
310          * if the I/O layer gives us more, we'll take it.
311          */
312         h = __archive_read_ahead(a, 32768 + 8*2048, &bytes_read);
313         if (h == NULL)
314             return (-1);
315         p = (const unsigned char *)h;
316
317         /* Skip the reserved area. */
318         bytes_read -= 32768;
319         p += 32768;
320
321         /* Check each volume descriptor to locate the PVD. */
322         for (; bytes_read > 2048; bytes_read -= 2048, p += 2048) {
323                 bid = isPVD(iso9660, p);
324                 if (bid > 0)
325                         return (bid);
326                 if (*p == '\177') /* End-of-volume-descriptor marker. */
327                         break;
328         }
329
330         /* We didn't find a valid PVD; return a bid of zero. */
331         return (0);
332 }
333
334 static int
335 isPVD(struct iso9660 *iso9660, const unsigned char *h)
336 {
337         struct file_info *file;
338         int i;
339
340         /* Type of the Primary Volume Descriptor must be 1. */
341         if (h[PVD_type_offset] != 1)
342                 return (0);
343
344         /* ID must be "CD001" */
345         if (memcmp(h + PVD_id_offset, "CD001", 5) != 0)
346                 return (0);
347
348         /* PVD version must be 1. */
349         if (h[PVD_version_offset] != 1)
350                 return (0);
351
352         /* Reserved field must be 0. */
353         if (h[PVD_reserved1_offset] != 0)
354                 return (0);
355
356         /* Reserved field must be 0. */
357         for (i = 0; i < PVD_reserved2_size; ++i)
358                 if (h[PVD_reserved2_offset + i] != 0)
359                         return (0);
360
361         /* Reserved field must be 0. */
362         for (i = 0; i < PVD_reserved3_size; ++i)
363                 if (h[PVD_reserved3_offset + i] != 0)
364                         return (0);
365
366         /* Logical block size must be > 0. */
367         /* I've looked at Ecma 119 and can't find any stronger
368          * restriction on this field. */
369         iso9660->logical_block_size = toi(h + PVD_logical_block_size_offset, 2);
370         if (iso9660->logical_block_size <= 0)
371                 return (0);
372
373         iso9660->volume_size = iso9660->logical_block_size
374             * (uint64_t)toi(h + PVD_volume_space_size_offset, 4);
375
376         /* File structure version must be 1 for ISO9660/ECMA119. */
377         if (h[PVD_file_structure_version_offset] != 1)
378                 return (0);
379
380
381         /* Reserved field must be 0. */
382         for (i = 0; i < PVD_reserved4_size; ++i)
383                 if (h[PVD_reserved4_offset + i] != 0)
384                         return (0);
385
386         /* Reserved field must be 0. */
387         for (i = 0; i < PVD_reserved5_size; ++i)
388                 if (h[PVD_reserved5_offset + i] != 0)
389                         return (0);
390
391         /* XXX TODO: Check other values for sanity; reject more
392          * malformed PVDs. XXX */
393
394         /* Store the root directory in the pending list. */
395         file = parse_file_info(iso9660, NULL, h + PVD_root_directory_record_offset);
396         add_entry(iso9660, file);
397         return (48);
398 }
399
400 static int
401 archive_read_format_iso9660_read_header(struct archive_read *a,
402     struct archive_entry *entry)
403 {
404         struct iso9660 *iso9660;
405         struct file_info *file;
406         int r;
407
408         iso9660 = (struct iso9660 *)(a->format->data);
409
410         if (!a->archive.archive_format) {
411                 a->archive.archive_format = ARCHIVE_FORMAT_ISO9660;
412                 a->archive.archive_format_name = "ISO9660";
413         }
414
415         /* Get the next entry that appears after the current offset. */
416         r = next_entry_seek(a, iso9660, &file);
417         if (r != ARCHIVE_OK)
418                 return (r);
419
420         iso9660->entry_bytes_remaining = file->size;
421         iso9660->entry_sparse_offset = 0; /* Offset for sparse-file-aware clients. */
422
423         if (file->offset + file->size > iso9660->volume_size) {
424                 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
425                     "File is beyond end-of-media: %s", file->name);
426                 iso9660->entry_bytes_remaining = 0;
427                 iso9660->entry_sparse_offset = 0;
428                 release_file(iso9660, file);
429                 return (ARCHIVE_WARN);
430         }
431
432         /* Set up the entry structure with information about this entry. */
433         archive_entry_set_mode(entry, file->mode);
434         archive_entry_set_uid(entry, file->uid);
435         archive_entry_set_gid(entry, file->gid);
436         archive_entry_set_nlink(entry, file->nlinks);
437         archive_entry_set_ino(entry, file->inode);
438         archive_entry_set_birthtime(entry, file->birthtime, 0);
439         archive_entry_set_mtime(entry, file->mtime, 0);
440         archive_entry_set_ctime(entry, file->ctime, 0);
441         archive_entry_set_atime(entry, file->atime, 0);
442         /* N.B.: Rock Ridge supports 64-bit device numbers. */
443         archive_entry_set_rdev(entry, (dev_t)file->rdev);
444         archive_entry_set_size(entry, iso9660->entry_bytes_remaining);
445         archive_string_empty(&iso9660->pathname);
446         archive_entry_set_pathname(entry,
447             build_pathname(&iso9660->pathname, file));
448         if (file->symlink.s != NULL)
449                 archive_entry_copy_symlink(entry, file->symlink.s);
450
451         /* If this entry points to the same data as the previous
452          * entry, convert this into a hardlink to that entry.
453          * But don't bother for zero-length files. */
454         if (file->offset == iso9660->previous_offset
455             && file->size == iso9660->previous_size
456             && file->size > 0) {
457                 archive_entry_set_hardlink(entry,
458                     iso9660->previous_pathname.s);
459                 iso9660->entry_bytes_remaining = 0;
460                 iso9660->entry_sparse_offset = 0;
461                 release_file(iso9660, file);
462                 return (ARCHIVE_OK);
463         }
464
465         /* If the offset is before our current position, we can't
466          * seek backwards to extract it, so issue a warning. */
467         if (file->offset < iso9660->current_position) {
468                 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
469                     "Ignoring out-of-order file @%x (%s) %jd < %jd",
470                     file,
471                     iso9660->pathname.s,
472                     file->offset, iso9660->current_position);
473                 iso9660->entry_bytes_remaining = 0;
474                 iso9660->entry_sparse_offset = 0;
475                 release_file(iso9660, file);
476                 return (ARCHIVE_WARN);
477         }
478
479         iso9660->previous_size = file->size;
480         iso9660->previous_offset = file->offset;
481         archive_strcpy(&iso9660->previous_pathname, iso9660->pathname.s);
482
483         /* If this is a directory, read in all of the entries right now. */
484         if (archive_entry_filetype(entry) == AE_IFDIR) {
485                 while (iso9660->entry_bytes_remaining > 0) {
486                         const void *block;
487                         const unsigned char *p;
488                         ssize_t step = iso9660->logical_block_size;
489                         if (step > iso9660->entry_bytes_remaining)
490                                 step = iso9660->entry_bytes_remaining;
491                         block = __archive_read_ahead(a, step, NULL);
492                         if (block == NULL) {
493                                 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
494             "Failed to read full block when scanning ISO9660 directory list");
495                                 release_file(iso9660, file);
496                                 return (ARCHIVE_FATAL);
497                         }
498                         __archive_read_consume(a, step);
499                         iso9660->current_position += step;
500                         iso9660->entry_bytes_remaining -= step;
501                         for (p = (const unsigned char *)block;
502                              *p != 0 && p < (const unsigned char *)block + step;
503                              p += *p) {
504                                 struct file_info *child;
505
506                                 /* Skip '.' entry. */
507                                 if (*(p + DR_name_len_offset) == 1
508                                     && *(p + DR_name_offset) == '\0')
509                                         continue;
510                                 /* Skip '..' entry. */
511                                 if (*(p + DR_name_len_offset) == 1
512                                     && *(p + DR_name_offset) == '\001')
513                                         continue;
514                                 child = parse_file_info(iso9660, file, p);
515                                 add_entry(iso9660, child);
516                                 if (iso9660->seenRockridge) {
517                                         a->archive.archive_format =
518                                             ARCHIVE_FORMAT_ISO9660_ROCKRIDGE;
519                                         a->archive.archive_format_name =
520                                             "ISO9660 with Rockridge extensions";
521                                 }
522                         }
523                 }
524         }
525
526         release_file(iso9660, file);
527         return (ARCHIVE_OK);
528 }
529
530 static int
531 archive_read_format_iso9660_read_data_skip(struct archive_read *a)
532 {
533         /* Because read_next_header always does an explicit skip
534          * to the next entry, we don't need to do anything here. */
535         (void)a; /* UNUSED */
536         return (ARCHIVE_OK);
537 }
538
539 static int
540 archive_read_format_iso9660_read_data(struct archive_read *a,
541     const void **buff, size_t *size, off_t *offset)
542 {
543         ssize_t bytes_read;
544         struct iso9660 *iso9660;
545
546         iso9660 = (struct iso9660 *)(a->format->data);
547         if (iso9660->entry_bytes_remaining <= 0) {
548                 *buff = NULL;
549                 *size = 0;
550                 *offset = iso9660->entry_sparse_offset;
551                 return (ARCHIVE_EOF);
552         }
553
554         *buff = __archive_read_ahead(a, 1, &bytes_read);
555         if (bytes_read == 0)
556                 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
557                     "Truncated input file");
558         if (buff == NULL)
559                 return (ARCHIVE_FATAL);
560         if (bytes_read > iso9660->entry_bytes_remaining)
561                 bytes_read = iso9660->entry_bytes_remaining;
562         *size = bytes_read;
563         *offset = iso9660->entry_sparse_offset;
564         iso9660->entry_sparse_offset += bytes_read;
565         iso9660->entry_bytes_remaining -= bytes_read;
566         iso9660->current_position += bytes_read;
567         __archive_read_consume(a, bytes_read);
568         return (ARCHIVE_OK);
569 }
570
571 static int
572 archive_read_format_iso9660_cleanup(struct archive_read *a)
573 {
574         struct iso9660 *iso9660;
575         struct file_info *file;
576
577         iso9660 = (struct iso9660 *)(a->format->data);
578         while ((file = next_entry(iso9660)) != NULL)
579                 release_file(iso9660, file);
580         archive_string_free(&iso9660->pathname);
581         archive_string_free(&iso9660->previous_pathname);
582         if (iso9660->pending_files)
583                 free(iso9660->pending_files);
584         free(iso9660);
585         (a->format->data) = NULL;
586         return (ARCHIVE_OK);
587 }
588
589 /*
590  * This routine parses a single ISO directory record, makes sense
591  * of any extensions, and stores the result in memory.
592  */
593 static struct file_info *
594 parse_file_info(struct iso9660 *iso9660, struct file_info *parent,
595     const unsigned char *isodirrec)
596 {
597         struct file_info *file;
598         size_t name_len;
599         const unsigned char *rr_start, *rr_end;
600         const char *p;
601         int flags;
602
603         /* TODO: Sanity check that name_len doesn't exceed length, etc. */
604
605         /* Create a new file entry and copy data from the ISO dir record. */
606         file = (struct file_info *)malloc(sizeof(*file));
607         if (file == NULL)
608                 return (NULL);
609         memset(file, 0, sizeof(*file));
610         file->parent = parent;
611         if (parent != NULL)
612                 parent->refcount++;
613         file->offset = (uint64_t)toi(isodirrec + DR_extent_offset, DR_extent_size)
614             * iso9660->logical_block_size;
615         file->size = toi(isodirrec + DR_size_offset, DR_size_size);
616         file->mtime = isodate7(isodirrec + DR_date_offset);
617         file->ctime = file->atime = file->mtime;
618
619         name_len = (size_t)*(const unsigned char *)(isodirrec + DR_name_len_offset);
620         p = isodirrec + DR_name_offset;
621         /* Rockridge extensions (if any) follow name.  Compute this
622          * before fidgeting the name_len below. */
623         rr_start = p + name_len + (name_len & 1 ? 0 : 1) + iso9660->suspOffset;
624         rr_end = (const unsigned char *)isodirrec
625             + *(isodirrec + DR_length_offset);
626
627         /* Chop off trailing ';1' from files. */
628         if (name_len > 2 && p[name_len - 1] == '1' && p[name_len - 2] == ';')
629                 name_len -= 2;
630         /* Chop off trailing '.' from filenames. */
631         if (name_len > 1 && p[name_len - 1] == '.')
632                 --name_len;
633         archive_strncpy(&file->name, p, name_len);
634
635         flags = *(isodirrec + DR_flags_offset);
636         if (flags & 0x02)
637                 file->mode = AE_IFDIR | 0700;
638         else
639                 file->mode = AE_IFREG | 0400;
640
641         /* Rockridge extensions overwrite information from above. */
642         parse_rockridge(iso9660, file, rr_start, rr_end);
643
644 #if DEBUG
645         /* DEBUGGING: Warn about attributes I don't yet fully support. */
646         if ((flags & ~0x02) != 0) {
647                 fprintf(stderr, "\n ** Unrecognized flag: ");
648                 dump_isodirrec(stderr, isodirrec);
649                 fprintf(stderr, "\n");
650         } else if (toi(isodirrec + DR_volume_sequence_number_offset, 2) != 1) {
651                 fprintf(stderr, "\n ** Unrecognized sequence number: ");
652                 dump_isodirrec(stderr, isodirrec);
653                 fprintf(stderr, "\n");
654         } else if (*(isodirrec + DR_file_unit_size_offset) != 0) {
655                 fprintf(stderr, "\n ** Unexpected file unit size: ");
656                 dump_isodirrec(stderr, isodirrec);
657                 fprintf(stderr, "\n");
658         } else if (*(isodirrec + DR_interleave_offset) != 0) {
659                 fprintf(stderr, "\n ** Unexpected interleave: ");
660                 dump_isodirrec(stderr, isodirrec);
661                 fprintf(stderr, "\n");
662         } else if (*(isodirrec + DR_ext_attr_length_offset) != 0) {
663                 fprintf(stderr, "\n ** Unexpected extended attribute length: ");
664                 dump_isodirrec(stderr, isodirrec);
665                 fprintf(stderr, "\n");
666         }
667 #endif
668         return (file);
669 }
670
671 static void
672 add_entry(struct iso9660 *iso9660, struct file_info *file)
673 {
674         /* Expand our pending files list as necessary. */
675         if (iso9660->pending_files_used >= iso9660->pending_files_allocated) {
676                 struct file_info **new_pending_files;
677                 int new_size = iso9660->pending_files_allocated * 2;
678
679                 if (iso9660->pending_files_allocated < 1024)
680                         new_size = 1024;
681                 /* Overflow might keep us from growing the list. */
682                 if (new_size <= iso9660->pending_files_allocated)
683                         __archive_errx(1, "Out of memory");
684                 new_pending_files = (struct file_info **)malloc(new_size * sizeof(new_pending_files[0]));
685                 if (new_pending_files == NULL)
686                         __archive_errx(1, "Out of memory");
687                 memcpy(new_pending_files, iso9660->pending_files,
688                     iso9660->pending_files_allocated * sizeof(new_pending_files[0]));
689                 if (iso9660->pending_files != NULL)
690                         free(iso9660->pending_files);
691                 iso9660->pending_files = new_pending_files;
692                 iso9660->pending_files_allocated = new_size;
693         }
694
695         iso9660->pending_files[iso9660->pending_files_used++] = file;
696 }
697
698 static void
699 parse_rockridge(struct iso9660 *iso9660, struct file_info *file,
700     const unsigned char *p, const unsigned char *end)
701 {
702         (void)iso9660; /* UNUSED */
703         file->name_continues = 0;
704         file->symlink_continues = 0;
705
706         while (p + 4 < end  /* Enough space for another entry. */
707             && p[0] >= 'A' && p[0] <= 'Z' /* Sanity-check 1st char of name. */
708             && p[1] >= 'A' && p[1] <= 'Z' /* Sanity-check 2nd char of name. */
709             && p[2] >= 4 /* Sanity-check length. */
710             && p + p[2] <= end) { /* Sanity-check length. */
711                 const unsigned char *data = p + 4;
712                 int data_length = p[2] - 4;
713                 int version = p[3];
714
715                 /*
716                  * Yes, each 'if' here does test p[0] again.
717                  * Otherwise, the fall-through handling to catch
718                  * unsupported extensions doesn't work.
719                  */
720                 switch(p[0]) {
721                 case 'C':
722                         if (p[0] == 'C' && p[1] == 'E') {
723                                 if (version == 1 && data_length == 24) {
724                                         /*
725                                          * CE extension comprises:
726                                          *   8 byte sector containing extension
727                                          *   8 byte offset w/in above sector
728                                          *   8 byte length of continuation
729                                          */
730                                         file->ce_offset = (uint64_t)toi(data, 4)
731                                             * iso9660->logical_block_size
732                                             + toi(data + 8, 4);
733                                         file->ce_size = toi(data + 16, 4);
734                                         /* If the result is rediculous,
735                                          * ignore it. */
736                                         if (file->ce_offset + file->ce_size
737                                             > iso9660->volume_size) {
738                                                 file->ce_offset = 0;
739                                                 file->ce_size = 0;
740                                         }
741                                 }
742                                 break;
743                         }
744                         /* FALLTHROUGH */
745                 case 'N':
746                         if (p[0] == 'N' && p[1] == 'M') {
747                                 if (version == 1)
748                                         parse_rockridge_NM1(file,
749                                             data, data_length);
750                                 break;
751                         }
752                         /* FALLTHROUGH */
753                 case 'P':
754                         if (p[0] == 'P' && p[1] == 'D') {
755                                 /*
756                                  * PD extension is padding;
757                                  * contents are always ignored.
758                                  */
759                                 break;
760                         }
761                         if (p[0] == 'P' && p[1] == 'N') {
762                                 if (version == 1 && data_length == 16) {
763                                         file->rdev = toi(data,4);
764                                         file->rdev <<= 32;
765                                         file->rdev |= toi(data + 8, 4);
766                                 }
767                                 break;
768                         }
769                         if (p[0] == 'P' && p[1] == 'X') {
770                                 /*
771                                  * PX extension comprises:
772                                  *   8 bytes for mode,
773                                  *   8 bytes for nlinks,
774                                  *   8 bytes for uid,
775                                  *   8 bytes for gid,
776                                  *   8 bytes for inode.
777                                  */
778                                 if (version == 1) {
779                                         if (data_length >= 8)
780                                                 file->mode
781                                                     = toi(data, 4);
782                                         if (data_length >= 16)
783                                                 file->nlinks
784                                                     = toi(data + 8, 4);
785                                         if (data_length >= 24)
786                                                 file->uid
787                                                     = toi(data + 16, 4);
788                                         if (data_length >= 32)
789                                                 file->gid
790                                                     = toi(data + 24, 4);
791                                         if (data_length >= 40)
792                                                 file->inode
793                                                     = toi(data + 32, 4);
794                                 }
795                                 break;
796                         }
797                         /* FALLTHROUGH */
798                 case 'R':
799                         if (p[0] == 'R' && p[1] == 'R' && version == 1) {
800                                 iso9660->seenRockridge = 1;
801                                 /*
802                                  * RR extension comprises:
803                                  *    one byte flag value
804                                  */
805                                 /* TODO: Handle RR extension. */
806                                 break;
807                         }
808                         /* FALLTHROUGH */
809                 case 'S':
810                         if (p[0] == 'S' && p[1] == 'L') {
811                                 if (version == 1)
812                                         parse_rockridge_SL1(file,
813                                             data, data_length);
814                                 break;
815                         }
816                         if (p[0] == 'S' && p[1] == 'P'
817                             && version == 1 && data_length == 3
818                             && data[0] == (unsigned char)'\xbe'
819                             && data[1] == (unsigned char)'\xef') {
820                                 /*
821                                  * SP extension stores the suspOffset
822                                  * (Number of bytes to skip between
823                                  * filename and SUSP records.)
824                                  * It is mandatory by the SUSP standard
825                                  * (IEEE 1281).
826                                  *
827                                  * It allows SUSP to coexist with
828                                  * non-SUSP uses of the System
829                                  * Use Area by placing non-SUSP data
830                                  * before SUSP data.
831                                  *
832                                  * TODO: Add a check for 'SP' in
833                                  * first directory entry, disable all SUSP
834                                  * processing if not found.
835                                  */
836                                 iso9660->suspOffset = data[2];
837                                 break;
838                         }
839                         if (p[0] == 'S' && p[1] == 'T'
840                             && data_length == 0 && version == 1) {
841                                 /*
842                                  * ST extension marks end of this
843                                  * block of SUSP entries.
844                                  *
845                                  * It allows SUSP to coexist with
846                                  * non-SUSP uses of the System
847                                  * Use Area by placing non-SUSP data
848                                  * after SUSP data.
849                                  */
850                                 return;
851                         }
852                 case 'T':
853                         if (p[0] == 'T' && p[1] == 'F') {
854                                 if (version == 1)
855                                         parse_rockridge_TF1(file,
856                                             data, data_length);
857                                 break;
858                         }
859                         /* FALLTHROUGH */
860                 default:
861                         /* The FALLTHROUGHs above leave us here for
862                          * any unsupported extension. */
863 #if DEBUG
864                         {
865                                 const unsigned char *t;
866                                 fprintf(stderr, "\nUnsupported RRIP extension for %s\n", file->name.s);
867                                 fprintf(stderr, " %c%c(%d):", p[0], p[1], data_length);
868                                 for (t = data; t < data + data_length && t < data + 16; t++)
869                                         fprintf(stderr, " %02x", *t);
870                                 fprintf(stderr, "\n");
871                         }
872 #endif
873                         break;
874                 }
875
876
877
878                 p += p[2];
879         }
880 }
881
882 static void
883 parse_rockridge_NM1(struct file_info *file, const unsigned char *data,
884     int data_length)
885 {
886         if (!file->name_continues)
887                 archive_string_empty(&file->name);
888         file->name_continues = 0;
889         if (data_length < 1)
890                 return;
891         /*
892          * NM version 1 extension comprises:
893          *   1 byte flag, value is one of:
894          *     = 0: remainder is name
895          *     = 1: remainder is name, next NM entry continues name
896          *     = 2: "."
897          *     = 4: ".."
898          *     = 32: Implementation specific
899          *     All other values are reserved.
900          */
901         switch(data[0]) {
902         case 0:
903                 if (data_length < 2)
904                         return;
905                 archive_strncat(&file->name, data + 1, data_length - 1);
906                 break;
907         case 1:
908                 if (data_length < 2)
909                         return;
910                 archive_strncat(&file->name, data + 1, data_length - 1);
911                 file->name_continues = 1;
912                 break;
913         case 2:
914                 archive_strcat(&file->name, ".");
915                 break;
916         case 4:
917                 archive_strcat(&file->name, "..");
918                 break;
919         default:
920                 return;
921         }
922
923 }
924
925 static void
926 parse_rockridge_TF1(struct file_info *file, const unsigned char *data,
927     int data_length)
928 {
929         char flag;
930         /*
931          * TF extension comprises:
932          *   one byte flag
933          *   create time (optional)
934          *   modify time (optional)
935          *   access time (optional)
936          *   attribute time (optional)
937          *  Time format and presence of fields
938          *  is controlled by flag bits.
939          */
940         if (data_length < 1)
941                 return;
942         flag = data[0];
943         ++data;
944         --data_length;
945         if (flag & 0x80) {
946                 /* Use 17-byte time format. */
947                 if ((flag & 1) && data_length >= 17) {
948                         /* Create time. */
949                         file->birthtime = isodate17(data);
950                         data += 17;
951                         data_length -= 17;
952                 }
953                 if ((flag & 2) && data_length >= 17) {
954                         /* Modify time. */
955                         file->mtime = isodate17(data);
956                         data += 17;
957                         data_length -= 17;
958                 }
959                 if ((flag & 4) && data_length >= 17) {
960                         /* Access time. */
961                         file->atime = isodate17(data);
962                         data += 17;
963                         data_length -= 17;
964                 }
965                 if ((flag & 8) && data_length >= 17) {
966                         /* Attribute change time. */
967                         file->ctime = isodate17(data);
968                         data += 17;
969                         data_length -= 17;
970                 }
971         } else {
972                 /* Use 7-byte time format. */
973                 if ((flag & 1) && data_length >= 7) {
974                         /* Create time. */
975                         file->birthtime = isodate17(data);
976                         data += 7;
977                         data_length -= 7;
978                 }
979                 if ((flag & 2) && data_length >= 7) {
980                         /* Modify time. */
981                         file->mtime = isodate7(data);
982                         data += 7;
983                         data_length -= 7;
984                 }
985                 if ((flag & 4) && data_length >= 7) {
986                         /* Access time. */
987                         file->atime = isodate7(data);
988                         data += 7;
989                         data_length -= 7;
990                 }
991                 if ((flag & 8) && data_length >= 7) {
992                         /* Attribute change time. */
993                         file->ctime = isodate7(data);
994                         data += 7;
995                         data_length -= 7;
996                 }
997         }
998 }
999
1000 static void
1001 parse_rockridge_SL1(struct file_info *file, const unsigned char *data,
1002     int data_length)
1003 {
1004         int component_continues = 1;
1005
1006         if (!file->symlink_continues)
1007                 archive_string_empty(&file->symlink);
1008         else
1009                 archive_strcat(&file->symlink, "/");
1010         file->symlink_continues = 0;
1011
1012         /*
1013          * Defined flag values:
1014          *  0: This is the last SL record for this symbolic link
1015          *  1: this symbolic link field continues in next SL entry
1016          *  All other values are reserved.
1017          */
1018         if (data_length < 1)
1019                 return;
1020         switch(*data) {
1021         case 0:
1022                 break;
1023         case 1:
1024                 file->symlink_continues = 1;
1025                 break;
1026         default:
1027                 return;
1028         }
1029         ++data;  /* Skip flag byte. */
1030         --data_length;
1031
1032         /*
1033          * SL extension body stores "components".
1034          * Basically, this is a complicated way of storing
1035          * a POSIX path.  It also interferes with using
1036          * symlinks for storing non-path data. <sigh>
1037          *
1038          * Each component is 2 bytes (flag and length)
1039          * possibly followed by name data.
1040          */
1041         while (data_length >= 2) {
1042                 unsigned char flag = *data++;
1043                 unsigned char nlen = *data++;
1044                 data_length -= 2;
1045
1046                 if (!component_continues)
1047                         archive_strcat(&file->symlink, "/");
1048                 component_continues = 0;
1049
1050                 switch(flag) {
1051                 case 0: /* Usual case, this is text. */
1052                         if (data_length < nlen)
1053                                 return;
1054                         archive_strncat(&file->symlink,
1055                             (const char *)data, nlen);
1056                         break;
1057                 case 0x01: /* Text continues in next component. */
1058                         if (data_length < nlen)
1059                                 return;
1060                         archive_strncat(&file->symlink,
1061                             (const char *)data, nlen);
1062                         component_continues = 1;
1063                         break;
1064                 case 0x02: /* Current dir. */
1065                         archive_strcat(&file->symlink, ".");
1066                         break;
1067                 case 0x04: /* Parent dir. */
1068                         archive_strcat(&file->symlink, "..");
1069                         break;
1070                 case 0x08: /* Root of filesystem. */
1071                         archive_string_empty(&file->symlink);
1072                         archive_strcat(&file->symlink, "/");
1073                         break;
1074                 case 0x10: /* Undefined (historically "volume root" */
1075                         archive_string_empty(&file->symlink);
1076                         archive_strcat(&file->symlink, "ROOT");
1077                         break;
1078                 case 0x20: /* Undefined (historically "hostname") */
1079                         archive_strcat(&file->symlink, "hostname");
1080                         break;
1081                 default:
1082                         /* TODO: issue a warning ? */
1083                         return;
1084                 }
1085                 data += nlen;
1086                 data_length -= nlen;
1087         }
1088 }
1089
1090
1091 static void
1092 release_file(struct iso9660 *iso9660, struct file_info *file)
1093 {
1094         struct file_info *parent;
1095
1096         if (file->refcount == 0) {
1097                 parent = file->parent;
1098                 archive_string_free(&file->name);
1099                 archive_string_free(&file->symlink);
1100                 free(file);
1101                 if (parent != NULL) {
1102                         parent->refcount--;
1103                         release_file(iso9660, parent);
1104                 }
1105         }
1106 }
1107
1108 static int
1109 next_entry_seek(struct archive_read *a, struct iso9660 *iso9660,
1110     struct file_info **pfile)
1111 {
1112         struct file_info *file;
1113         uint64_t offset;
1114
1115         *pfile = NULL;
1116         for (;;) {
1117                 *pfile = file = next_entry(iso9660);
1118                 if (file == NULL)
1119                         return (ARCHIVE_EOF);
1120
1121                 /* CE area precedes actual file data? Ignore it. */
1122                 if (file->ce_offset > file->offset) {
1123                         /* fprintf(stderr, " *** Discarding CE data.\n"); */
1124                         file->ce_offset = 0;
1125                         file->ce_size = 0;
1126                 }
1127
1128                 /* Don't waste time seeking for zero-length bodies. */
1129                 if (file->size == 0) {
1130                         file->offset = iso9660->current_position;
1131                 }
1132
1133                 /* If CE exists, find and read it now. */
1134                 if (file->ce_offset > 0)
1135                         offset = file->ce_offset;
1136                 else
1137                         offset = file->offset;
1138
1139                 /* Seek forward to the start of the entry. */
1140                 if (iso9660->current_position < offset) {
1141                         off_t step = offset - iso9660->current_position;
1142                         off_t bytes_read;
1143                         bytes_read = __archive_read_skip(a, step);
1144                         if (bytes_read < 0)
1145                                 return (bytes_read);
1146                         iso9660->current_position = offset;
1147                 }
1148
1149                 /* We found body of file; handle it now. */
1150                 if (offset == file->offset)
1151                         return (ARCHIVE_OK);
1152
1153                 /* Found CE?  Process it and push the file back onto list. */
1154                 if (offset == file->ce_offset) {
1155                         const void *p;
1156                         ssize_t size = file->ce_size;
1157                         const unsigned char *rr_start;
1158
1159                         file->ce_offset = 0;
1160                         file->ce_size = 0;
1161                         p = __archive_read_ahead(a, size, NULL);
1162                         if (p == NULL)
1163                                 return (ARCHIVE_FATAL);
1164                         rr_start = (const unsigned char *)p;
1165                         parse_rockridge(iso9660, file, rr_start,
1166                             rr_start + size);
1167                         __archive_read_consume(a, size);
1168                         iso9660->current_position += size;
1169                         add_entry(iso9660, file);
1170                 }
1171         }
1172 }
1173
1174 static struct file_info *
1175 next_entry(struct iso9660 *iso9660)
1176 {
1177         int least_index;
1178         uint64_t least_end_offset;
1179         int i;
1180         struct file_info *r;
1181
1182         if (iso9660->pending_files_used < 1)
1183                 return (NULL);
1184
1185         /* Assume the first file in the list is the earliest on disk. */
1186         least_index = 0;
1187         least_end_offset = iso9660->pending_files[0]->offset
1188             + iso9660->pending_files[0]->size;
1189
1190         /* Now, try to find an earlier one. */
1191         for (i = 0; i < iso9660->pending_files_used; i++) {
1192                 /* Use the position of the file *end* as our comparison. */
1193                 uint64_t end_offset = iso9660->pending_files[i]->offset
1194                     + iso9660->pending_files[i]->size;
1195                 if (iso9660->pending_files[i]->ce_offset > 0
1196                     && iso9660->pending_files[i]->ce_offset < iso9660->pending_files[i]->offset)
1197                         end_offset = iso9660->pending_files[i]->ce_offset
1198                     + iso9660->pending_files[i]->ce_size;
1199                 if (least_end_offset > end_offset) {
1200                         least_index = i;
1201                         least_end_offset = end_offset;
1202                 }
1203         }
1204         r = iso9660->pending_files[least_index];
1205         iso9660->pending_files[least_index]
1206             = iso9660->pending_files[--iso9660->pending_files_used];
1207         return (r);
1208 }
1209
1210 static unsigned int
1211 toi(const void *p, int n)
1212 {
1213         const unsigned char *v = (const unsigned char *)p;
1214         if (n > 1)
1215                 return v[0] + 256 * toi(v + 1, n - 1);
1216         if (n == 1)
1217                 return v[0];
1218         return (0);
1219 }
1220
1221 static time_t
1222 isodate7(const unsigned char *v)
1223 {
1224         struct tm tm;
1225         int offset;
1226         memset(&tm, 0, sizeof(tm));
1227         tm.tm_year = v[0];
1228         tm.tm_mon = v[1] - 1;
1229         tm.tm_mday = v[2];
1230         tm.tm_hour = v[3];
1231         tm.tm_min = v[4];
1232         tm.tm_sec = v[5];
1233         /* v[6] is the signed timezone offset, in 1/4-hour increments. */
1234         offset = ((const signed char *)v)[6];
1235         if (offset > -48 && offset < 52) {
1236                 tm.tm_hour -= offset / 4;
1237                 tm.tm_min -= (offset % 4) * 15;
1238         }
1239         return (time_from_tm(&tm));
1240 }
1241
1242 static time_t
1243 isodate17(const unsigned char *v)
1244 {
1245         struct tm tm;
1246         int offset;
1247         memset(&tm, 0, sizeof(tm));
1248         tm.tm_year = (v[0] - '0') * 1000 + (v[1] - '0') * 100
1249             + (v[2] - '0') * 10 + (v[3] - '0')
1250             - 1900;
1251         tm.tm_mon = (v[4] - '0') * 10 + (v[5] - '0');
1252         tm.tm_mday = (v[6] - '0') * 10 + (v[7] - '0');
1253         tm.tm_hour = (v[8] - '0') * 10 + (v[9] - '0');
1254         tm.tm_min = (v[10] - '0') * 10 + (v[11] - '0');
1255         tm.tm_sec = (v[12] - '0') * 10 + (v[13] - '0');
1256         /* v[16] is the signed timezone offset, in 1/4-hour increments. */
1257         offset = ((const signed char *)v)[16];
1258         if (offset > -48 && offset < 52) {
1259                 tm.tm_hour -= offset / 4;
1260                 tm.tm_min -= (offset % 4) * 15;
1261         }
1262         return (time_from_tm(&tm));
1263 }
1264
1265 static time_t
1266 time_from_tm(struct tm *t)
1267 {
1268 #if HAVE_TIMEGM
1269         /* Use platform timegm() if available. */
1270         return (timegm(t));
1271 #else
1272         /* Else use direct calculation using POSIX assumptions. */
1273         /* First, fix up tm_yday based on the year/month/day. */
1274         mktime(t);
1275         /* Then we can compute timegm() from first principles. */
1276         return (t->tm_sec + t->tm_min * 60 + t->tm_hour * 3600
1277             + t->tm_yday * 86400 + (t->tm_year - 70) * 31536000
1278             + ((t->tm_year - 69) / 4) * 86400 -
1279             ((t->tm_year - 1) / 100) * 86400
1280             + ((t->tm_year + 299) / 400) * 86400);
1281 #endif
1282 }
1283
1284 static const char *
1285 build_pathname(struct archive_string *as, struct file_info *file)
1286 {
1287         if (file->parent != NULL && archive_strlen(&file->parent->name) > 0) {
1288                 build_pathname(as, file->parent);
1289                 archive_strcat(as, "/");
1290         }
1291         if (archive_strlen(&file->name) == 0)
1292                 archive_strcat(as, ".");
1293         else
1294                 archive_string_concat(as, &file->name);
1295         return (as->s);
1296 }
1297
1298 #if DEBUG
1299 static void
1300 dump_isodirrec(FILE *out, const unsigned char *isodirrec)
1301 {
1302         fprintf(out, " l %d,",
1303             toi(isodirrec + DR_length_offset, DR_length_size));
1304         fprintf(out, " a %d,",
1305             toi(isodirrec + DR_ext_attr_length_offset, DR_ext_attr_length_size));
1306         fprintf(out, " ext 0x%x,",
1307             toi(isodirrec + DR_extent_offset, DR_extent_size));
1308         fprintf(out, " s %d,",
1309             toi(isodirrec + DR_size_offset, DR_extent_size));
1310         fprintf(out, " f 0x%02x,",
1311             toi(isodirrec + DR_flags_offset, DR_flags_size));
1312         fprintf(out, " u %d,",
1313             toi(isodirrec + DR_file_unit_size_offset, DR_file_unit_size_size));
1314         fprintf(out, " ilv %d,",
1315             toi(isodirrec + DR_interleave_offset, DR_interleave_size));
1316         fprintf(out, " seq %d,",
1317             toi(isodirrec + DR_volume_sequence_number_offset, DR_volume_sequence_number_size));
1318         fprintf(out, " nl %d:",
1319             toi(isodirrec + DR_name_len_offset, DR_name_len_size));
1320         fprintf(out, " `%.*s'",
1321             toi(isodirrec + DR_name_len_offset, DR_name_len_size), isodirrec + DR_name_offset);
1322 }
1323 #endif