Actually hook powernow.4 into the build.
[dragonfly.git] / contrib / libarchive / libarchive / archive_read_support_format_iso9660.c
blob1921c580953920ca2ed28008a70bb88b6e695df1
1 /*-
2 * Copyright (c) 2003-2007 Tim Kientzle
3 * Copyright (c) 2009 Andreas Henriksson <andreas@fatal.se>
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "archive_platform.h"
28 __FBSDID("$FreeBSD: src/lib/libarchive/archive_read_support_format_iso9660.c,v 1.30 2008/12/06 06:57:45 kientzle Exp $");
30 #ifdef HAVE_ERRNO_H
31 #include <errno.h>
32 #endif
33 /* #include <stdint.h> */ /* See archive_platform.h */
34 #include <stdio.h>
35 #ifdef HAVE_STDLIB_H
36 #include <stdlib.h>
37 #endif
38 #ifdef HAVE_STRING_H
39 #include <string.h>
40 #endif
41 #include <time.h>
43 #include "archive.h"
44 #include "archive_entry.h"
45 #include "archive_private.h"
46 #include "archive_read_private.h"
47 #include "archive_string.h"
50 * An overview of ISO 9660 format:
52 * Each disk is laid out as follows:
53 * * 32k reserved for private use
54 * * Volume descriptor table. Each volume descriptor
55 * is 2k and specifies basic format information.
56 * The "Primary Volume Descriptor" (PVD) is defined by the
57 * standard and should always be present; other volume
58 * descriptors include various vendor-specific extensions.
59 * * Files and directories. Each file/dir is specified by
60 * an "extent" (starting sector and length in bytes).
61 * Dirs are just files with directory records packed one
62 * after another. The PVD contains a single dir entry
63 * specifying the location of the root directory. Everything
64 * else follows from there.
66 * This module works by first reading the volume descriptors, then
67 * building a list of directory entries, sorted by starting
68 * sector. At each step, I look for the earliest dir entry that
69 * hasn't yet been read, seek forward to that location and read
70 * that entry. If it's a dir, I slurp in the new dir entries and
71 * add them to the heap; if it's a regular file, I return the
72 * corresponding archive_entry and wait for the client to request
73 * the file body. This strategy allows us to read most compliant
74 * CDs with a single pass through the data, as required by libarchive.
77 /* Structure of on-disk primary volume descriptor. */
78 #define PVD_type_offset 0
79 #define PVD_type_size 1
80 #define PVD_id_offset (PVD_type_offset + PVD_type_size)
81 #define PVD_id_size 5
82 #define PVD_version_offset (PVD_id_offset + PVD_id_size)
83 #define PVD_version_size 1
84 #define PVD_reserved1_offset (PVD_version_offset + PVD_version_size)
85 #define PVD_reserved1_size 1
86 #define PVD_system_id_offset (PVD_reserved1_offset + PVD_reserved1_size)
87 #define PVD_system_id_size 32
88 #define PVD_volume_id_offset (PVD_system_id_offset + PVD_system_id_size)
89 #define PVD_volume_id_size 32
90 #define PVD_reserved2_offset (PVD_volume_id_offset + PVD_volume_id_size)
91 #define PVD_reserved2_size 8
92 #define PVD_volume_space_size_offset (PVD_reserved2_offset + PVD_reserved2_size)
93 #define PVD_volume_space_size_size 8
94 #define PVD_reserved3_offset (PVD_volume_space_size_offset + PVD_volume_space_size_size)
95 #define PVD_reserved3_size 32
96 #define PVD_volume_set_size_offset (PVD_reserved3_offset + PVD_reserved3_size)
97 #define PVD_volume_set_size_size 4
98 #define PVD_volume_sequence_number_offset (PVD_volume_set_size_offset + PVD_volume_set_size_size)
99 #define PVD_volume_sequence_number_size 4
100 #define PVD_logical_block_size_offset (PVD_volume_sequence_number_offset + PVD_volume_sequence_number_size)
101 #define PVD_logical_block_size_size 4
102 #define PVD_path_table_size_offset (PVD_logical_block_size_offset + PVD_logical_block_size_size)
103 #define PVD_path_table_size_size 8
104 #define PVD_type_1_path_table_offset (PVD_path_table_size_offset + PVD_path_table_size_size)
105 #define PVD_type_1_path_table_size 4
106 #define PVD_opt_type_1_path_table_offset (PVD_type_1_path_table_offset + PVD_type_1_path_table_size)
107 #define PVD_opt_type_1_path_table_size 4
108 #define PVD_type_m_path_table_offset (PVD_opt_type_1_path_table_offset + PVD_opt_type_1_path_table_size)
109 #define PVD_type_m_path_table_size 4
110 #define PVD_opt_type_m_path_table_offset (PVD_type_m_path_table_offset + PVD_type_m_path_table_size)
111 #define PVD_opt_type_m_path_table_size 4
112 #define PVD_root_directory_record_offset (PVD_opt_type_m_path_table_offset + PVD_opt_type_m_path_table_size)
113 #define PVD_root_directory_record_size 34
114 #define PVD_volume_set_id_offset (PVD_root_directory_record_offset + PVD_root_directory_record_size)
115 #define PVD_volume_set_id_size 128
116 #define PVD_publisher_id_offset (PVD_volume_set_id_offset + PVD_volume_set_id_size)
117 #define PVD_publisher_id_size 128
118 #define PVD_preparer_id_offset (PVD_publisher_id_offset + PVD_publisher_id_size)
119 #define PVD_preparer_id_size 128
120 #define PVD_application_id_offset (PVD_preparer_id_offset + PVD_preparer_id_size)
121 #define PVD_application_id_size 128
122 #define PVD_copyright_file_id_offset (PVD_application_id_offset + PVD_application_id_size)
123 #define PVD_copyright_file_id_size 37
124 #define PVD_abstract_file_id_offset (PVD_copyright_file_id_offset + PVD_copyright_file_id_size)
125 #define PVD_abstract_file_id_size 37
126 #define PVD_bibliographic_file_id_offset (PVD_abstract_file_id_offset + PVD_abstract_file_id_size)
127 #define PVD_bibliographic_file_id_size 37
128 #define PVD_creation_date_offset (PVD_bibliographic_file_id_offset + PVD_bibliographic_file_id_size)
129 #define PVD_creation_date_size 17
130 #define PVD_modification_date_offset (PVD_creation_date_offset + PVD_creation_date_size)
131 #define PVD_modification_date_size 17
132 #define PVD_expiration_date_offset (PVD_modification_date_offset + PVD_modification_date_size)
133 #define PVD_expiration_date_size 17
134 #define PVD_effective_date_offset (PVD_expiration_date_offset + PVD_expiration_date_size)
135 #define PVD_effective_date_size 17
136 #define PVD_file_structure_version_offset (PVD_effective_date_offset + PVD_effective_date_size)
137 #define PVD_file_structure_version_size 1
138 #define PVD_reserved4_offset (PVD_file_structure_version_offset + PVD_file_structure_version_size)
139 #define PVD_reserved4_size 1
140 #define PVD_application_data_offset (PVD_reserved4_offset + PVD_reserved4_size)
141 #define PVD_application_data_size 512
142 #define PVD_reserved5_offset (PVD_application_data_offset + PVD_application_data_size)
143 #define PVD_reserved5_size (2048 - PVD_reserved5_offset)
145 /* TODO: It would make future maintenance easier to just hardcode the
146 * above values. In particular, ECMA119 states the offsets as part of
147 * the standard. That would eliminate the need for the following check.*/
148 #if PVD_reserved5_offset != 1395
149 #error PVD offset and size definitions are wrong.
150 #endif
153 /* Structure of optional on-disk supplementary volume descriptor. */
154 #define SVD_type_offset 0
155 #define SVD_type_size 1
156 #define SVD_id_offset (SVD_type_offset + SVD_type_size)
157 #define SVD_id_size 5
158 #define SVD_version_offset (SVD_id_offset + SVD_id_size)
159 #define SVD_version_size 1
160 /* ... */
161 #define SVD_volume_space_size_offset 80
162 #define SVD_volume_space_size_size 8
163 #define SVD_escape_sequences_offset (SVD_volume_space_size_offset + SVD_volume_space_size_size)
164 #define SVD_escape_sequences_size 32
165 /* ... */
166 #define SVD_logical_block_size_offset 128
167 #define SVD_logical_block_size_size 4
168 /* ... */
169 #define SVD_root_directory_record_offset 156
170 #define SVD_root_directory_record_size 34
171 /* ... */
172 /* FIXME: validate correctness of last SVD entry offset. */
174 /* Structure of an on-disk directory record. */
175 /* Note: ISO9660 stores each multi-byte integer twice, once in
176 * each byte order. The sizes here are the size of just one
177 * of the two integers. (This is why the offset of a field isn't
178 * the same as the offset+size of the previous field.) */
179 #define DR_length_offset 0
180 #define DR_length_size 1
181 #define DR_ext_attr_length_offset 1
182 #define DR_ext_attr_length_size 1
183 #define DR_extent_offset 2
184 #define DR_extent_size 4
185 #define DR_size_offset 10
186 #define DR_size_size 4
187 #define DR_date_offset 18
188 #define DR_date_size 7
189 #define DR_flags_offset 25
190 #define DR_flags_size 1
191 #define DR_file_unit_size_offset 26
192 #define DR_file_unit_size_size 1
193 #define DR_interleave_offset 27
194 #define DR_interleave_size 1
195 #define DR_volume_sequence_number_offset 28
196 #define DR_volume_sequence_number_size 2
197 #define DR_name_len_offset 32
198 #define DR_name_len_size 1
199 #define DR_name_offset 33
201 /* In-memory storage for a directory record. */
202 struct file_info {
203 struct file_info *parent;
204 int refcount;
205 uint64_t offset; /* Offset on disk. */
206 uint64_t size; /* File size in bytes. */
207 uint64_t ce_offset; /* Offset of CE */
208 uint64_t ce_size; /* Size of CE */
209 time_t birthtime; /* File created time. */
210 time_t mtime; /* File last modified time. */
211 time_t atime; /* File last accessed time. */
212 time_t ctime; /* File attribute change time. */
213 uint64_t rdev; /* Device number */
214 mode_t mode;
215 uid_t uid;
216 gid_t gid;
217 ino_t inode;
218 int nlinks;
219 struct archive_string name; /* Pathname */
220 char name_continues; /* Non-zero if name continues */
221 struct archive_string symlink;
222 char symlink_continues; /* Non-zero if link continues */
226 struct iso9660 {
227 int magic;
228 #define ISO9660_MAGIC 0x96609660
230 int option_ignore_joliet;
232 struct archive_string pathname;
233 char seenRockridge; /* Set true if RR extensions are used. */
234 unsigned char suspOffset;
235 char seenJoliet;
237 uint64_t previous_offset;
238 uint64_t previous_size;
239 struct archive_string previous_pathname;
241 /* TODO: Make this a heap for fast inserts and deletions. */
242 struct file_info **pending_files;
243 int pending_files_allocated;
244 int pending_files_used;
246 uint64_t current_position;
247 ssize_t logical_block_size;
248 uint64_t volume_size; /* Total size of volume in bytes. */
250 off_t entry_sparse_offset;
251 int64_t entry_bytes_remaining;
254 static void add_entry(struct iso9660 *iso9660, struct file_info *file);
255 static int archive_read_format_iso9660_bid(struct archive_read *);
256 static int archive_read_format_iso9660_options(struct archive_read *,
257 const char *, const char *);
258 static int archive_read_format_iso9660_cleanup(struct archive_read *);
259 static int archive_read_format_iso9660_read_data(struct archive_read *,
260 const void **, size_t *, off_t *);
261 static int archive_read_format_iso9660_read_data_skip(struct archive_read *);
262 static int archive_read_format_iso9660_read_header(struct archive_read *,
263 struct archive_entry *);
264 static const char *build_pathname(struct archive_string *, struct file_info *);
265 #if DEBUG
266 static void dump_isodirrec(FILE *, const unsigned char *isodirrec);
267 #endif
268 static time_t time_from_tm(struct tm *);
269 static time_t isodate17(const unsigned char *);
270 static time_t isodate7(const unsigned char *);
271 static int isJolietSVD(struct iso9660 *, const unsigned char *);
272 static int isPVD(struct iso9660 *, const unsigned char *);
273 static struct file_info *next_entry(struct iso9660 *);
274 static int next_entry_seek(struct archive_read *a, struct iso9660 *iso9660,
275 struct file_info **pfile);
276 static struct file_info *
277 parse_file_info(struct iso9660 *iso9660,
278 struct file_info *parent, const unsigned char *isodirrec);
279 static void parse_rockridge(struct iso9660 *iso9660,
280 struct file_info *file, const unsigned char *start,
281 const unsigned char *end);
282 static void parse_rockridge_NM1(struct file_info *,
283 const unsigned char *, int);
284 static void parse_rockridge_SL1(struct file_info *,
285 const unsigned char *, int);
286 static void parse_rockridge_TF1(struct file_info *,
287 const unsigned char *, int);
288 static void release_file(struct iso9660 *, struct file_info *);
289 static unsigned toi(const void *p, int n);
292 archive_read_support_format_iso9660(struct archive *_a)
294 struct archive_read *a = (struct archive_read *)_a;
295 struct iso9660 *iso9660;
296 int r;
298 iso9660 = (struct iso9660 *)malloc(sizeof(*iso9660));
299 if (iso9660 == NULL) {
300 archive_set_error(&a->archive, ENOMEM, "Can't allocate iso9660 data");
301 return (ARCHIVE_FATAL);
303 memset(iso9660, 0, sizeof(*iso9660));
304 iso9660->magic = ISO9660_MAGIC;
306 r = __archive_read_register_format(a,
307 iso9660,
308 "iso9660",
309 archive_read_format_iso9660_bid,
310 archive_read_format_iso9660_options,
311 archive_read_format_iso9660_read_header,
312 archive_read_format_iso9660_read_data,
313 archive_read_format_iso9660_read_data_skip,
314 archive_read_format_iso9660_cleanup);
316 if (r != ARCHIVE_OK) {
317 free(iso9660);
318 return (r);
320 return (ARCHIVE_OK);
324 static int
325 archive_read_format_iso9660_bid(struct archive_read *a)
327 struct iso9660 *iso9660;
328 ssize_t bytes_read, brsvd;
329 const void *h;
330 const unsigned char *p, *psvd;
331 int bid;
333 iso9660 = (struct iso9660 *)(a->format->data);
336 * Skip the first 32k (reserved area) and get the first
337 * 8 sectors of the volume descriptor table. Of course,
338 * if the I/O layer gives us more, we'll take it.
340 h = __archive_read_ahead(a, 32768 + 8*2048, &bytes_read);
341 if (h == NULL)
342 return (-1);
343 p = (const unsigned char *)h;
345 /* Skip the reserved area. */
346 bytes_read -= 32768;
347 p += 32768;
349 /* Check each volume descriptor to locate possible SVD with Joliet. */
350 for (brsvd = bytes_read, psvd = p;
351 !iso9660->option_ignore_joliet && brsvd > 2048;
352 brsvd -= 2048, psvd += 2048) {
353 bid = isJolietSVD(iso9660, psvd);
354 if (bid > 0)
355 return (bid);
356 if (*p == '\177') /* End-of-volume-descriptor marker. */
357 break;
360 /* Check each volume descriptor to locate the PVD. */
361 for (; bytes_read > 2048; bytes_read -= 2048, p += 2048) {
362 bid = isPVD(iso9660, p);
363 if (bid > 0)
364 return (bid);
365 if (*p == '\177') /* End-of-volume-descriptor marker. */
366 break;
369 /* We didn't find a valid PVD; return a bid of zero. */
370 return (0);
373 static int
374 archive_read_format_iso9660_options(struct archive_read *a,
375 const char *key, const char *val)
377 struct iso9660 *iso9660;
379 iso9660 = (struct iso9660 *)(a->format->data);
381 if (strcmp(key, "joliet") == 0) {
382 if (val == NULL || strcmp(val, "off") == 0 ||
383 strcmp(val, "ignore") == 0 ||
384 strcmp(val, "disable") == 0 ||
385 strcmp(val, "0") == 0)
386 iso9660->option_ignore_joliet = 1;
387 else
388 iso9660->option_ignore_joliet = 0;
389 return (ARCHIVE_OK);
392 /* Note: The "warn" return is just to inform the options
393 * supervisor that we didn't handle it. It will generate
394 * a suitable error if noone used this option. */
395 return (ARCHIVE_WARN);
398 static int
399 isJolietSVD(struct iso9660 *iso9660, const unsigned char *h)
401 struct file_info *file;
402 const unsigned char *p;
404 /* Type 2 means it's a SVD. */
405 if (h[SVD_type_offset] != 2)
406 return (0);
408 /* ID must be "CD001" */
409 if (memcmp(h + SVD_id_offset, "CD001", 5) != 0)
410 return (0);
412 /* FIXME: do more validations according to joliet spec. */
414 /* check if this SVD contains joliet extension! */
415 p = h + SVD_escape_sequences_offset;
416 /* N.B. Joliet spec says p[1] == '\\', but.... */
417 if (p[0] == '%' && p[1] == '/') {
418 int level = 0;
420 if (p[2] == '@')
421 level = 1;
422 else if (p[2] == 'C')
423 level = 2;
424 else if (p[2] == 'E')
425 level = 3;
426 else /* not joliet */
427 return (0);
429 iso9660->seenJoliet = level;
431 } else /* not joliet */
432 return (0);
434 iso9660->logical_block_size = toi(h + SVD_logical_block_size_offset, 2);
435 if (iso9660->logical_block_size <= 0)
436 return (0);
438 iso9660->volume_size = iso9660->logical_block_size
439 * (uint64_t)toi(h + SVD_volume_space_size_offset, 4);
441 #if DEBUG
442 fprintf(stderr, "Joliet UCS-2 level %d with "
443 "logical block size:%d, volume size:%d\n",
444 iso9660->seenJoliet,
445 iso9660->logical_block_size, iso9660->volume_size);
446 #endif
448 /* Store the root directory in the pending list. */
449 file = parse_file_info(iso9660, NULL, h + SVD_root_directory_record_offset);
450 add_entry(iso9660, file);
452 return (48);
455 static int
456 isPVD(struct iso9660 *iso9660, const unsigned char *h)
458 struct file_info *file;
459 int i;
461 /* Type of the Primary Volume Descriptor must be 1. */
462 if (h[PVD_type_offset] != 1)
463 return (0);
465 /* ID must be "CD001" */
466 if (memcmp(h + PVD_id_offset, "CD001", 5) != 0)
467 return (0);
469 /* PVD version must be 1. */
470 if (h[PVD_version_offset] != 1)
471 return (0);
473 /* Reserved field must be 0. */
474 if (h[PVD_reserved1_offset] != 0)
475 return (0);
477 /* Reserved field must be 0. */
478 for (i = 0; i < PVD_reserved2_size; ++i)
479 if (h[PVD_reserved2_offset + i] != 0)
480 return (0);
482 /* Reserved field must be 0. */
483 for (i = 0; i < PVD_reserved3_size; ++i)
484 if (h[PVD_reserved3_offset + i] != 0)
485 return (0);
487 /* Logical block size must be > 0. */
488 /* I've looked at Ecma 119 and can't find any stronger
489 * restriction on this field. */
490 iso9660->logical_block_size = toi(h + PVD_logical_block_size_offset, 2);
491 if (iso9660->logical_block_size <= 0)
492 return (0);
494 iso9660->volume_size = iso9660->logical_block_size
495 * (uint64_t)toi(h + PVD_volume_space_size_offset, 4);
497 /* File structure version must be 1 for ISO9660/ECMA119. */
498 if (h[PVD_file_structure_version_offset] != 1)
499 return (0);
502 /* Reserved field must be 0. */
503 for (i = 0; i < PVD_reserved4_size; ++i)
504 if (h[PVD_reserved4_offset + i] != 0)
505 return (0);
507 /* Reserved field must be 0. */
508 for (i = 0; i < PVD_reserved5_size; ++i)
509 if (h[PVD_reserved5_offset + i] != 0)
510 return (0);
512 /* XXX TODO: Check other values for sanity; reject more
513 * malformed PVDs. XXX */
515 /* Store the root directory in the pending list. */
516 file = parse_file_info(iso9660, NULL, h + PVD_root_directory_record_offset);
517 add_entry(iso9660, file);
518 return (48);
521 static int
522 archive_read_format_iso9660_read_header(struct archive_read *a,
523 struct archive_entry *entry)
525 struct iso9660 *iso9660;
526 struct file_info *file;
527 int r;
529 iso9660 = (struct iso9660 *)(a->format->data);
531 if (!a->archive.archive_format) {
532 a->archive.archive_format = ARCHIVE_FORMAT_ISO9660;
533 a->archive.archive_format_name = "ISO9660";
536 /* Get the next entry that appears after the current offset. */
537 r = next_entry_seek(a, iso9660, &file);
538 if (r != ARCHIVE_OK) {
539 release_file(iso9660, file);
540 return (r);
543 iso9660->entry_bytes_remaining = file->size;
544 iso9660->entry_sparse_offset = 0; /* Offset for sparse-file-aware clients. */
546 if (file->offset + file->size > iso9660->volume_size) {
547 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
548 "File is beyond end-of-media: %s", file->name);
549 iso9660->entry_bytes_remaining = 0;
550 iso9660->entry_sparse_offset = 0;
551 release_file(iso9660, file);
552 return (ARCHIVE_WARN);
555 /* Set up the entry structure with information about this entry. */
556 archive_entry_set_mode(entry, file->mode);
557 archive_entry_set_uid(entry, file->uid);
558 archive_entry_set_gid(entry, file->gid);
559 archive_entry_set_nlink(entry, file->nlinks);
560 archive_entry_set_ino(entry, file->inode);
561 archive_entry_set_birthtime(entry, file->birthtime, 0);
562 archive_entry_set_mtime(entry, file->mtime, 0);
563 archive_entry_set_ctime(entry, file->ctime, 0);
564 archive_entry_set_atime(entry, file->atime, 0);
565 /* N.B.: Rock Ridge supports 64-bit device numbers. */
566 archive_entry_set_rdev(entry, (dev_t)file->rdev);
567 archive_entry_set_size(entry, iso9660->entry_bytes_remaining);
568 archive_string_empty(&iso9660->pathname);
569 archive_entry_set_pathname(entry,
570 build_pathname(&iso9660->pathname, file));
571 if (file->symlink.s != NULL)
572 archive_entry_copy_symlink(entry, file->symlink.s);
574 /* If this entry points to the same data as the previous
575 * entry, convert this into a hardlink to that entry.
576 * But don't bother for zero-length files. */
577 if (file->offset == iso9660->previous_offset
578 && file->size == iso9660->previous_size
579 && file->size > 0) {
580 archive_entry_set_hardlink(entry,
581 iso9660->previous_pathname.s);
582 iso9660->entry_bytes_remaining = 0;
583 iso9660->entry_sparse_offset = 0;
584 release_file(iso9660, file);
585 return (ARCHIVE_OK);
588 /* If the offset is before our current position, we can't
589 * seek backwards to extract it, so issue a warning. */
590 if (file->offset < iso9660->current_position) {
591 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
592 "Ignoring out-of-order file @%x (%s) %jd < %jd",
593 file,
594 iso9660->pathname.s,
595 file->offset, iso9660->current_position);
596 iso9660->entry_bytes_remaining = 0;
597 iso9660->entry_sparse_offset = 0;
598 release_file(iso9660, file);
599 return (ARCHIVE_WARN);
602 iso9660->previous_size = file->size;
603 iso9660->previous_offset = file->offset;
604 archive_strcpy(&iso9660->previous_pathname, iso9660->pathname.s);
606 /* If this is a directory, read in all of the entries right now. */
607 if (archive_entry_filetype(entry) == AE_IFDIR) {
608 while (iso9660->entry_bytes_remaining > 0) {
609 const void *block;
610 const unsigned char *p;
611 ssize_t step = iso9660->logical_block_size;
612 if (step > iso9660->entry_bytes_remaining)
613 step = iso9660->entry_bytes_remaining;
614 block = __archive_read_ahead(a, step, NULL);
615 if (block == NULL) {
616 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
617 "Failed to read full block when scanning ISO9660 directory list");
618 release_file(iso9660, file);
619 return (ARCHIVE_FATAL);
621 __archive_read_consume(a, step);
622 iso9660->current_position += step;
623 iso9660->entry_bytes_remaining -= step;
624 for (p = (const unsigned char *)block;
625 *p != 0 && p < (const unsigned char *)block + step;
626 p += *p) {
627 struct file_info *child;
629 /* N.B.: these special directory identifiers
630 * are 8 bit "values" even on a
631 * Joliet CD with UCS-2 (16bit) encoding.
634 /* Skip '.' entry. */
635 if (*(p + DR_name_len_offset) == 1
636 && *(p + DR_name_offset) == '\0')
637 continue;
638 /* Skip '..' entry. */
639 if (*(p + DR_name_len_offset) == 1
640 && *(p + DR_name_offset) == '\001')
641 continue;
642 child = parse_file_info(iso9660, file, p);
643 add_entry(iso9660, child);
644 if (iso9660->seenRockridge) {
645 a->archive.archive_format =
646 ARCHIVE_FORMAT_ISO9660_ROCKRIDGE;
647 a->archive.archive_format_name =
648 "ISO9660 with Rockridge extensions";
654 release_file(iso9660, file);
655 return (ARCHIVE_OK);
658 static int
659 archive_read_format_iso9660_read_data_skip(struct archive_read *a)
661 /* Because read_next_header always does an explicit skip
662 * to the next entry, we don't need to do anything here. */
663 (void)a; /* UNUSED */
664 return (ARCHIVE_OK);
667 static int
668 archive_read_format_iso9660_read_data(struct archive_read *a,
669 const void **buff, size_t *size, off_t *offset)
671 ssize_t bytes_read;
672 struct iso9660 *iso9660;
674 iso9660 = (struct iso9660 *)(a->format->data);
675 if (iso9660->entry_bytes_remaining <= 0) {
676 *buff = NULL;
677 *size = 0;
678 *offset = iso9660->entry_sparse_offset;
679 return (ARCHIVE_EOF);
682 *buff = __archive_read_ahead(a, 1, &bytes_read);
683 if (bytes_read == 0)
684 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
685 "Truncated input file");
686 if (*buff == NULL)
687 return (ARCHIVE_FATAL);
688 if (bytes_read > iso9660->entry_bytes_remaining)
689 bytes_read = iso9660->entry_bytes_remaining;
690 *size = bytes_read;
691 *offset = iso9660->entry_sparse_offset;
692 iso9660->entry_sparse_offset += bytes_read;
693 iso9660->entry_bytes_remaining -= bytes_read;
694 iso9660->current_position += bytes_read;
695 __archive_read_consume(a, bytes_read);
696 return (ARCHIVE_OK);
699 static int
700 archive_read_format_iso9660_cleanup(struct archive_read *a)
702 struct iso9660 *iso9660;
703 struct file_info *file;
705 iso9660 = (struct iso9660 *)(a->format->data);
706 while ((file = next_entry(iso9660)) != NULL)
707 release_file(iso9660, file);
708 archive_string_free(&iso9660->pathname);
709 archive_string_free(&iso9660->previous_pathname);
710 if (iso9660->pending_files)
711 free(iso9660->pending_files);
712 free(iso9660);
713 (a->format->data) = NULL;
714 return (ARCHIVE_OK);
718 * This routine parses a single ISO directory record, makes sense
719 * of any extensions, and stores the result in memory.
721 static struct file_info *
722 parse_file_info(struct iso9660 *iso9660, struct file_info *parent,
723 const unsigned char *isodirrec)
725 struct file_info *file;
726 size_t name_len;
727 const unsigned char *rr_start, *rr_end;
728 const unsigned char *p;
729 int flags;
731 /* TODO: Sanity check that name_len doesn't exceed length, etc. */
733 /* Create a new file entry and copy data from the ISO dir record. */
734 file = (struct file_info *)malloc(sizeof(*file));
735 if (file == NULL)
736 return (NULL);
737 memset(file, 0, sizeof(*file));
738 file->parent = parent;
739 if (parent != NULL)
740 parent->refcount++;
741 file->offset = (uint64_t)toi(isodirrec + DR_extent_offset, DR_extent_size)
742 * iso9660->logical_block_size;
743 file->size = toi(isodirrec + DR_size_offset, DR_size_size);
744 file->mtime = isodate7(isodirrec + DR_date_offset);
745 file->ctime = file->atime = file->mtime;
747 name_len = (size_t)isodirrec[DR_name_len_offset];
748 p = isodirrec + DR_name_offset;
749 /* Rockridge extensions (if any) follow name. Compute this
750 * before fidgeting the name_len below. */
751 rr_start = p + name_len + (name_len & 1 ? 0 : 1) + iso9660->suspOffset;
752 rr_end = isodirrec + isodirrec[DR_length_offset];
754 if (iso9660->seenJoliet) {
755 /* Joliet names are max 64 chars (128 bytes) according to spec,
756 * but genisoimage (and others?) will allow you to have more.
758 wchar_t wbuff[64+1], *wp;
759 const unsigned char *c;
761 /* TODO: warn when name_len > 128 ? */
763 /* convert BE UTF-16 to wchar_t */
764 for (c = p, wp = wbuff;
765 c < (p + name_len) &&
766 wp < (wbuff + sizeof(wbuff)/sizeof(*wbuff) - 1);
767 c += 2) {
768 *wp++ = (((255 & (int)c[0]) << 8) | (255 & (int)c[1]));
770 *wp = L'\0';
772 #if 0 /* untested code, is it at all useful on Joliet? */
773 /* trim trailing first version and dot from filename.
775 * Remember we where in UTF-16BE land!
776 * SEPARATOR 1 (.) and SEPARATOR 2 (;) are both
777 * 16 bits big endian characters on Joliet.
779 * TODO: sanitize filename?
780 * Joliet allows any UCS-2 char except:
781 * *, /, :, ;, ? and \.
783 /* Chop off trailing ';1' from files. */
784 if (*(wp-2) == ';' && *(wp-1) == '1') {
785 wp-=2;
786 *wp = L'\0';
789 /* Chop off trailing '.' from filenames. */
790 if (*(wp-1) == '.')
791 *(--wp) = L'\0';
792 #endif
794 /* store the result in the file name field. */
795 archive_strappend_w_utf8(&file->name, wbuff);
796 } else {
797 /* Chop off trailing ';1' from files. */
798 if (name_len > 2 && p[name_len - 2] == ';' &&
799 p[name_len - 1] == '1')
800 name_len -= 2;
801 /* Chop off trailing '.' from filenames. */
802 if (name_len > 1 && p[name_len - 1] == '.')
803 --name_len;
805 archive_strncpy(&file->name, (const char *)p, name_len);
808 flags = isodirrec[DR_flags_offset];
809 if (flags & 0x02)
810 file->mode = AE_IFDIR | 0700;
811 else
812 file->mode = AE_IFREG | 0400;
814 /* Rockridge extensions overwrite information from above. */
815 parse_rockridge(iso9660, file, rr_start, rr_end);
817 #if DEBUG
818 /* DEBUGGING: Warn about attributes I don't yet fully support. */
819 if ((flags & ~0x02) != 0) {
820 fprintf(stderr, "\n ** Unrecognized flag: ");
821 dump_isodirrec(stderr, isodirrec);
822 fprintf(stderr, "\n");
823 } else if (toi(isodirrec + DR_volume_sequence_number_offset, 2) != 1) {
824 fprintf(stderr, "\n ** Unrecognized sequence number: ");
825 dump_isodirrec(stderr, isodirrec);
826 fprintf(stderr, "\n");
827 } else if (*(isodirrec + DR_file_unit_size_offset) != 0) {
828 fprintf(stderr, "\n ** Unexpected file unit size: ");
829 dump_isodirrec(stderr, isodirrec);
830 fprintf(stderr, "\n");
831 } else if (*(isodirrec + DR_interleave_offset) != 0) {
832 fprintf(stderr, "\n ** Unexpected interleave: ");
833 dump_isodirrec(stderr, isodirrec);
834 fprintf(stderr, "\n");
835 } else if (*(isodirrec + DR_ext_attr_length_offset) != 0) {
836 fprintf(stderr, "\n ** Unexpected extended attribute length: ");
837 dump_isodirrec(stderr, isodirrec);
838 fprintf(stderr, "\n");
840 #endif
841 return (file);
844 static void
845 add_entry(struct iso9660 *iso9660, struct file_info *file)
847 /* Expand our pending files list as necessary. */
848 if (iso9660->pending_files_used >= iso9660->pending_files_allocated) {
849 struct file_info **new_pending_files;
850 int new_size = iso9660->pending_files_allocated * 2;
852 if (iso9660->pending_files_allocated < 1024)
853 new_size = 1024;
854 /* Overflow might keep us from growing the list. */
855 if (new_size <= iso9660->pending_files_allocated)
856 __archive_errx(1, "Out of memory");
857 new_pending_files = (struct file_info **)malloc(new_size * sizeof(new_pending_files[0]));
858 if (new_pending_files == NULL)
859 __archive_errx(1, "Out of memory");
860 memcpy(new_pending_files, iso9660->pending_files,
861 iso9660->pending_files_allocated * sizeof(new_pending_files[0]));
862 if (iso9660->pending_files != NULL)
863 free(iso9660->pending_files);
864 iso9660->pending_files = new_pending_files;
865 iso9660->pending_files_allocated = new_size;
868 iso9660->pending_files[iso9660->pending_files_used++] = file;
871 static void
872 parse_rockridge(struct iso9660 *iso9660, struct file_info *file,
873 const unsigned char *p, const unsigned char *end)
875 (void)iso9660; /* UNUSED */
876 file->name_continues = 0;
877 file->symlink_continues = 0;
879 while (p + 4 < end /* Enough space for another entry. */
880 && p[0] >= 'A' && p[0] <= 'Z' /* Sanity-check 1st char of name. */
881 && p[1] >= 'A' && p[1] <= 'Z' /* Sanity-check 2nd char of name. */
882 && p[2] >= 4 /* Sanity-check length. */
883 && p + p[2] <= end) { /* Sanity-check length. */
884 const unsigned char *data = p + 4;
885 int data_length = p[2] - 4;
886 int version = p[3];
889 * Yes, each 'if' here does test p[0] again.
890 * Otherwise, the fall-through handling to catch
891 * unsupported extensions doesn't work.
893 switch(p[0]) {
894 case 'C':
895 if (p[0] == 'C' && p[1] == 'E') {
896 if (version == 1 && data_length == 24) {
898 * CE extension comprises:
899 * 8 byte sector containing extension
900 * 8 byte offset w/in above sector
901 * 8 byte length of continuation
903 file->ce_offset = (uint64_t)toi(data, 4)
904 * iso9660->logical_block_size
905 + toi(data + 8, 4);
906 file->ce_size = toi(data + 16, 4);
907 /* If the result is rediculous,
908 * ignore it. */
909 if (file->ce_offset + file->ce_size
910 > iso9660->volume_size) {
911 file->ce_offset = 0;
912 file->ce_size = 0;
915 break;
917 /* FALLTHROUGH */
918 case 'N':
919 if (p[0] == 'N' && p[1] == 'M') {
920 if (version == 1)
921 parse_rockridge_NM1(file,
922 data, data_length);
923 break;
925 /* FALLTHROUGH */
926 case 'P':
927 if (p[0] == 'P' && p[1] == 'D') {
929 * PD extension is padding;
930 * contents are always ignored.
932 break;
934 if (p[0] == 'P' && p[1] == 'N') {
935 if (version == 1 && data_length == 16) {
936 file->rdev = toi(data,4);
937 file->rdev <<= 32;
938 file->rdev |= toi(data + 8, 4);
940 break;
942 if (p[0] == 'P' && p[1] == 'X') {
944 * PX extension comprises:
945 * 8 bytes for mode,
946 * 8 bytes for nlinks,
947 * 8 bytes for uid,
948 * 8 bytes for gid,
949 * 8 bytes for inode.
951 if (version == 1) {
952 if (data_length >= 8)
953 file->mode
954 = toi(data, 4);
955 if (data_length >= 16)
956 file->nlinks
957 = toi(data + 8, 4);
958 if (data_length >= 24)
959 file->uid
960 = toi(data + 16, 4);
961 if (data_length >= 32)
962 file->gid
963 = toi(data + 24, 4);
964 if (data_length >= 40)
965 file->inode
966 = toi(data + 32, 4);
968 break;
970 /* FALLTHROUGH */
971 case 'R':
972 if (p[0] == 'R' && p[1] == 'R' && version == 1) {
973 iso9660->seenRockridge = 1;
975 * RR extension comprises:
976 * one byte flag value
978 /* TODO: Handle RR extension. */
979 break;
981 /* FALLTHROUGH */
982 case 'S':
983 if (p[0] == 'S' && p[1] == 'L') {
984 if (version == 1)
985 parse_rockridge_SL1(file,
986 data, data_length);
987 break;
989 if (p[0] == 'S' && p[1] == 'P'
990 && version == 1 && data_length == 3
991 && data[0] == (unsigned char)'\xbe'
992 && data[1] == (unsigned char)'\xef') {
994 * SP extension stores the suspOffset
995 * (Number of bytes to skip between
996 * filename and SUSP records.)
997 * It is mandatory by the SUSP standard
998 * (IEEE 1281).
1000 * It allows SUSP to coexist with
1001 * non-SUSP uses of the System
1002 * Use Area by placing non-SUSP data
1003 * before SUSP data.
1005 * TODO: Add a check for 'SP' in
1006 * first directory entry, disable all SUSP
1007 * processing if not found.
1009 iso9660->suspOffset = data[2];
1010 break;
1012 if (p[0] == 'S' && p[1] == 'T'
1013 && data_length == 0 && version == 1) {
1015 * ST extension marks end of this
1016 * block of SUSP entries.
1018 * It allows SUSP to coexist with
1019 * non-SUSP uses of the System
1020 * Use Area by placing non-SUSP data
1021 * after SUSP data.
1023 return;
1025 case 'T':
1026 if (p[0] == 'T' && p[1] == 'F') {
1027 if (version == 1)
1028 parse_rockridge_TF1(file,
1029 data, data_length);
1030 break;
1032 /* FALLTHROUGH */
1033 default:
1034 /* The FALLTHROUGHs above leave us here for
1035 * any unsupported extension. */
1036 #if DEBUG
1038 const unsigned char *t;
1039 fprintf(stderr, "\nUnsupported RRIP extension for %s\n", file->name.s);
1040 fprintf(stderr, " %c%c(%d):", p[0], p[1], data_length);
1041 for (t = data; t < data + data_length && t < data + 16; t++)
1042 fprintf(stderr, " %02x", *t);
1043 fprintf(stderr, "\n");
1045 #endif
1046 break;
1051 p += p[2];
1055 static void
1056 parse_rockridge_NM1(struct file_info *file,
1057 const unsigned char *data, int data_length)
1059 if (!file->name_continues)
1060 archive_string_empty(&file->name);
1061 file->name_continues = 0;
1062 if (data_length < 1)
1063 return;
1065 * NM version 1 extension comprises:
1066 * 1 byte flag, value is one of:
1067 * = 0: remainder is name
1068 * = 1: remainder is name, next NM entry continues name
1069 * = 2: "."
1070 * = 4: ".."
1071 * = 32: Implementation specific
1072 * All other values are reserved.
1074 switch(data[0]) {
1075 case 0:
1076 if (data_length < 2)
1077 return;
1078 archive_strncat(&file->name, (const char *)data + 1, data_length - 1);
1079 break;
1080 case 1:
1081 if (data_length < 2)
1082 return;
1083 archive_strncat(&file->name, (const char *)data + 1, data_length - 1);
1084 file->name_continues = 1;
1085 break;
1086 case 2:
1087 archive_strcat(&file->name, ".");
1088 break;
1089 case 4:
1090 archive_strcat(&file->name, "..");
1091 break;
1092 default:
1093 return;
1098 static void
1099 parse_rockridge_TF1(struct file_info *file, const unsigned char *data,
1100 int data_length)
1102 char flag;
1104 * TF extension comprises:
1105 * one byte flag
1106 * create time (optional)
1107 * modify time (optional)
1108 * access time (optional)
1109 * attribute time (optional)
1110 * Time format and presence of fields
1111 * is controlled by flag bits.
1113 if (data_length < 1)
1114 return;
1115 flag = data[0];
1116 ++data;
1117 --data_length;
1118 if (flag & 0x80) {
1119 /* Use 17-byte time format. */
1120 if ((flag & 1) && data_length >= 17) {
1121 /* Create time. */
1122 file->birthtime = isodate17(data);
1123 data += 17;
1124 data_length -= 17;
1126 if ((flag & 2) && data_length >= 17) {
1127 /* Modify time. */
1128 file->mtime = isodate17(data);
1129 data += 17;
1130 data_length -= 17;
1132 if ((flag & 4) && data_length >= 17) {
1133 /* Access time. */
1134 file->atime = isodate17(data);
1135 data += 17;
1136 data_length -= 17;
1138 if ((flag & 8) && data_length >= 17) {
1139 /* Attribute change time. */
1140 file->ctime = isodate17(data);
1141 data += 17;
1142 data_length -= 17;
1144 } else {
1145 /* Use 7-byte time format. */
1146 if ((flag & 1) && data_length >= 7) {
1147 /* Create time. */
1148 file->birthtime = isodate17(data);
1149 data += 7;
1150 data_length -= 7;
1152 if ((flag & 2) && data_length >= 7) {
1153 /* Modify time. */
1154 file->mtime = isodate7(data);
1155 data += 7;
1156 data_length -= 7;
1158 if ((flag & 4) && data_length >= 7) {
1159 /* Access time. */
1160 file->atime = isodate7(data);
1161 data += 7;
1162 data_length -= 7;
1164 if ((flag & 8) && data_length >= 7) {
1165 /* Attribute change time. */
1166 file->ctime = isodate7(data);
1167 data += 7;
1168 data_length -= 7;
1173 static void
1174 parse_rockridge_SL1(struct file_info *file, const unsigned char *data,
1175 int data_length)
1177 int component_continues = 1;
1179 if (!file->symlink_continues)
1180 archive_string_empty(&file->symlink);
1181 else
1182 archive_strcat(&file->symlink, "/");
1183 file->symlink_continues = 0;
1186 * Defined flag values:
1187 * 0: This is the last SL record for this symbolic link
1188 * 1: this symbolic link field continues in next SL entry
1189 * All other values are reserved.
1191 if (data_length < 1)
1192 return;
1193 switch(*data) {
1194 case 0:
1195 break;
1196 case 1:
1197 file->symlink_continues = 1;
1198 break;
1199 default:
1200 return;
1202 ++data; /* Skip flag byte. */
1203 --data_length;
1206 * SL extension body stores "components".
1207 * Basically, this is a complicated way of storing
1208 * a POSIX path. It also interferes with using
1209 * symlinks for storing non-path data. <sigh>
1211 * Each component is 2 bytes (flag and length)
1212 * possibly followed by name data.
1214 while (data_length >= 2) {
1215 unsigned char flag = *data++;
1216 unsigned char nlen = *data++;
1217 data_length -= 2;
1219 if (!component_continues)
1220 archive_strcat(&file->symlink, "/");
1221 component_continues = 0;
1223 switch(flag) {
1224 case 0: /* Usual case, this is text. */
1225 if (data_length < nlen)
1226 return;
1227 archive_strncat(&file->symlink,
1228 (const char *)data, nlen);
1229 break;
1230 case 0x01: /* Text continues in next component. */
1231 if (data_length < nlen)
1232 return;
1233 archive_strncat(&file->symlink,
1234 (const char *)data, nlen);
1235 component_continues = 1;
1236 break;
1237 case 0x02: /* Current dir. */
1238 archive_strcat(&file->symlink, ".");
1239 break;
1240 case 0x04: /* Parent dir. */
1241 archive_strcat(&file->symlink, "..");
1242 break;
1243 case 0x08: /* Root of filesystem. */
1244 archive_string_empty(&file->symlink);
1245 archive_strcat(&file->symlink, "/");
1246 break;
1247 case 0x10: /* Undefined (historically "volume root" */
1248 archive_string_empty(&file->symlink);
1249 archive_strcat(&file->symlink, "ROOT");
1250 break;
1251 case 0x20: /* Undefined (historically "hostname") */
1252 archive_strcat(&file->symlink, "hostname");
1253 break;
1254 default:
1255 /* TODO: issue a warning ? */
1256 return;
1258 data += nlen;
1259 data_length -= nlen;
1264 static void
1265 release_file(struct iso9660 *iso9660, struct file_info *file)
1267 struct file_info *parent;
1269 if (file == NULL)
1270 return;
1272 if (file->refcount == 0) {
1273 parent = file->parent;
1274 archive_string_free(&file->name);
1275 archive_string_free(&file->symlink);
1276 free(file);
1277 if (parent != NULL) {
1278 parent->refcount--;
1279 release_file(iso9660, parent);
1284 static int
1285 next_entry_seek(struct archive_read *a, struct iso9660 *iso9660,
1286 struct file_info **pfile)
1288 struct file_info *file;
1289 uint64_t offset;
1291 *pfile = NULL;
1292 for (;;) {
1293 *pfile = file = next_entry(iso9660);
1294 if (file == NULL)
1295 return (ARCHIVE_EOF);
1297 /* CE area precedes actual file data? Ignore it. */
1298 if (file->ce_offset > file->offset) {
1299 /* fprintf(stderr, " *** Discarding CE data.\n"); */
1300 file->ce_offset = 0;
1301 file->ce_size = 0;
1304 /* Don't waste time seeking for zero-length bodies. */
1305 if (file->size == 0) {
1306 file->offset = iso9660->current_position;
1309 /* If CE exists, find and read it now. */
1310 if (file->ce_offset > 0)
1311 offset = file->ce_offset;
1312 else
1313 offset = file->offset;
1315 /* Seek forward to the start of the entry. */
1316 if (iso9660->current_position < offset) {
1317 off_t step = offset - iso9660->current_position;
1318 off_t bytes_read;
1319 bytes_read = __archive_read_skip(a, step);
1320 if (bytes_read < 0)
1321 return (bytes_read);
1322 iso9660->current_position = offset;
1325 /* We found body of file; handle it now. */
1326 if (offset == file->offset)
1327 return (ARCHIVE_OK);
1329 /* Found CE? Process it and push the file back onto list. */
1330 if (offset == file->ce_offset) {
1331 const void *p;
1332 ssize_t size = file->ce_size;
1333 const unsigned char *rr_start;
1335 file->ce_offset = 0;
1336 file->ce_size = 0;
1337 p = __archive_read_ahead(a, size, NULL);
1338 if (p == NULL)
1339 return (ARCHIVE_FATAL);
1340 rr_start = (const unsigned char *)p;
1341 parse_rockridge(iso9660, file, rr_start,
1342 rr_start + size);
1343 __archive_read_consume(a, size);
1344 iso9660->current_position += size;
1345 add_entry(iso9660, file);
1350 static struct file_info *
1351 next_entry(struct iso9660 *iso9660)
1353 int least_index;
1354 uint64_t least_end_offset;
1355 int i;
1356 struct file_info *r;
1358 if (iso9660->pending_files_used < 1)
1359 return (NULL);
1361 /* Assume the first file in the list is the earliest on disk. */
1362 least_index = 0;
1363 least_end_offset = iso9660->pending_files[0]->offset
1364 + iso9660->pending_files[0]->size;
1366 /* Now, try to find an earlier one. */
1367 for (i = 0; i < iso9660->pending_files_used; i++) {
1368 /* Use the position of the file *end* as our comparison. */
1369 uint64_t end_offset = iso9660->pending_files[i]->offset
1370 + iso9660->pending_files[i]->size;
1371 if (iso9660->pending_files[i]->ce_offset > 0
1372 && iso9660->pending_files[i]->ce_offset < iso9660->pending_files[i]->offset)
1373 end_offset = iso9660->pending_files[i]->ce_offset
1374 + iso9660->pending_files[i]->ce_size;
1375 if (least_end_offset > end_offset) {
1376 least_index = i;
1377 least_end_offset = end_offset;
1380 r = iso9660->pending_files[least_index];
1381 iso9660->pending_files[least_index]
1382 = iso9660->pending_files[--iso9660->pending_files_used];
1383 return (r);
1386 static unsigned int
1387 toi(const void *p, int n)
1389 const unsigned char *v = (const unsigned char *)p;
1390 if (n > 1)
1391 return v[0] + 256 * toi(v + 1, n - 1);
1392 if (n == 1)
1393 return v[0];
1394 return (0);
1397 static time_t
1398 isodate7(const unsigned char *v)
1400 struct tm tm;
1401 int offset;
1402 memset(&tm, 0, sizeof(tm));
1403 tm.tm_year = v[0];
1404 tm.tm_mon = v[1] - 1;
1405 tm.tm_mday = v[2];
1406 tm.tm_hour = v[3];
1407 tm.tm_min = v[4];
1408 tm.tm_sec = v[5];
1409 /* v[6] is the signed timezone offset, in 1/4-hour increments. */
1410 offset = ((const signed char *)v)[6];
1411 if (offset > -48 && offset < 52) {
1412 tm.tm_hour -= offset / 4;
1413 tm.tm_min -= (offset % 4) * 15;
1415 return (time_from_tm(&tm));
1418 static time_t
1419 isodate17(const unsigned char *v)
1421 struct tm tm;
1422 int offset;
1423 memset(&tm, 0, sizeof(tm));
1424 tm.tm_year = (v[0] - '0') * 1000 + (v[1] - '0') * 100
1425 + (v[2] - '0') * 10 + (v[3] - '0')
1426 - 1900;
1427 tm.tm_mon = (v[4] - '0') * 10 + (v[5] - '0');
1428 tm.tm_mday = (v[6] - '0') * 10 + (v[7] - '0');
1429 tm.tm_hour = (v[8] - '0') * 10 + (v[9] - '0');
1430 tm.tm_min = (v[10] - '0') * 10 + (v[11] - '0');
1431 tm.tm_sec = (v[12] - '0') * 10 + (v[13] - '0');
1432 /* v[16] is the signed timezone offset, in 1/4-hour increments. */
1433 offset = ((const signed char *)v)[16];
1434 if (offset > -48 && offset < 52) {
1435 tm.tm_hour -= offset / 4;
1436 tm.tm_min -= (offset % 4) * 15;
1438 return (time_from_tm(&tm));
1441 static time_t
1442 time_from_tm(struct tm *t)
1444 #if HAVE_TIMEGM
1445 /* Use platform timegm() if available. */
1446 return (timegm(t));
1447 #else
1448 /* Else use direct calculation using POSIX assumptions. */
1449 /* First, fix up tm_yday based on the year/month/day. */
1450 mktime(t);
1451 /* Then we can compute timegm() from first principles. */
1452 return (t->tm_sec + t->tm_min * 60 + t->tm_hour * 3600
1453 + t->tm_yday * 86400 + (t->tm_year - 70) * 31536000
1454 + ((t->tm_year - 69) / 4) * 86400 -
1455 ((t->tm_year - 1) / 100) * 86400
1456 + ((t->tm_year + 299) / 400) * 86400);
1457 #endif
1460 static const char *
1461 build_pathname(struct archive_string *as, struct file_info *file)
1463 if (file->parent != NULL && archive_strlen(&file->parent->name) > 0) {
1464 build_pathname(as, file->parent);
1465 archive_strcat(as, "/");
1467 if (archive_strlen(&file->name) == 0)
1468 archive_strcat(as, ".");
1469 else
1470 archive_string_concat(as, &file->name);
1471 return (as->s);
1474 #if DEBUG
1475 static void
1476 dump_isodirrec(FILE *out, const unsigned char *isodirrec)
1478 fprintf(out, " l %d,",
1479 toi(isodirrec + DR_length_offset, DR_length_size));
1480 fprintf(out, " a %d,",
1481 toi(isodirrec + DR_ext_attr_length_offset, DR_ext_attr_length_size));
1482 fprintf(out, " ext 0x%x,",
1483 toi(isodirrec + DR_extent_offset, DR_extent_size));
1484 fprintf(out, " s %d,",
1485 toi(isodirrec + DR_size_offset, DR_extent_size));
1486 fprintf(out, " f 0x%02x,",
1487 toi(isodirrec + DR_flags_offset, DR_flags_size));
1488 fprintf(out, " u %d,",
1489 toi(isodirrec + DR_file_unit_size_offset, DR_file_unit_size_size));
1490 fprintf(out, " ilv %d,",
1491 toi(isodirrec + DR_interleave_offset, DR_interleave_size));
1492 fprintf(out, " seq %d,",
1493 toi(isodirrec + DR_volume_sequence_number_offset, DR_volume_sequence_number_size));
1494 fprintf(out, " nl %d:",
1495 toi(isodirrec + DR_name_len_offset, DR_name_len_size));
1496 fprintf(out, " `%.*s'",
1497 toi(isodirrec + DR_name_len_offset, DR_name_len_size), isodirrec + DR_name_offset);
1499 #endif