2 * Copyright (c) 2003-2007 Tim Kientzle
3 * Copyright (c) 2009 Andreas Henriksson <andreas@fatal.se>
4 * Copyright (c) 2009-2011 Michihiro NAKAJIMA
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "archive_platform.h"
29 __FBSDID("$FreeBSD$");
34 /* #include <stdint.h> */ /* See archive_platform.h */
48 #include "archive_endian.h"
49 #include "archive_entry.h"
50 #include "archive_private.h"
51 #include "archive_read_private.h"
52 #include "archive_string.h"
55 * An overview of ISO 9660 format:
57 * Each disk is laid out as follows:
58 * * 32k reserved for private use
59 * * Volume descriptor table. Each volume descriptor
60 * is 2k and specifies basic format information.
61 * The "Primary Volume Descriptor" (PVD) is defined by the
62 * standard and should always be present; other volume
63 * descriptors include various vendor-specific extensions.
64 * * Files and directories. Each file/dir is specified by
65 * an "extent" (starting sector and length in bytes).
66 * Dirs are just files with directory records packed one
67 * after another. The PVD contains a single dir entry
68 * specifying the location of the root directory. Everything
69 * else follows from there.
71 * This module works by first reading the volume descriptors, then
72 * building a list of directory entries, sorted by starting
73 * sector. At each step, I look for the earliest dir entry that
74 * hasn't yet been read, seek forward to that location and read
75 * that entry. If it's a dir, I slurp in the new dir entries and
76 * add them to the heap; if it's a regular file, I return the
77 * corresponding archive_entry and wait for the client to request
78 * the file body. This strategy allows us to read most compliant
79 * CDs with a single pass through the data, as required by libarchive.
81 #define LOGICAL_BLOCK_SIZE 2048
82 #define SYSTEM_AREA_BLOCK 16
84 /* Structure of on-disk primary volume descriptor. */
85 #define PVD_type_offset 0
86 #define PVD_type_size 1
87 #define PVD_id_offset (PVD_type_offset + PVD_type_size)
89 #define PVD_version_offset (PVD_id_offset + PVD_id_size)
90 #define PVD_version_size 1
91 #define PVD_reserved1_offset (PVD_version_offset + PVD_version_size)
92 #define PVD_reserved1_size 1
93 #define PVD_system_id_offset (PVD_reserved1_offset + PVD_reserved1_size)
94 #define PVD_system_id_size 32
95 #define PVD_volume_id_offset (PVD_system_id_offset + PVD_system_id_size)
96 #define PVD_volume_id_size 32
97 #define PVD_reserved2_offset (PVD_volume_id_offset + PVD_volume_id_size)
98 #define PVD_reserved2_size 8
99 #define PVD_volume_space_size_offset (PVD_reserved2_offset + PVD_reserved2_size)
100 #define PVD_volume_space_size_size 8
101 #define PVD_reserved3_offset (PVD_volume_space_size_offset + PVD_volume_space_size_size)
102 #define PVD_reserved3_size 32
103 #define PVD_volume_set_size_offset (PVD_reserved3_offset + PVD_reserved3_size)
104 #define PVD_volume_set_size_size 4
105 #define PVD_volume_sequence_number_offset (PVD_volume_set_size_offset + PVD_volume_set_size_size)
106 #define PVD_volume_sequence_number_size 4
107 #define PVD_logical_block_size_offset (PVD_volume_sequence_number_offset + PVD_volume_sequence_number_size)
108 #define PVD_logical_block_size_size 4
109 #define PVD_path_table_size_offset (PVD_logical_block_size_offset + PVD_logical_block_size_size)
110 #define PVD_path_table_size_size 8
111 #define PVD_type_1_path_table_offset (PVD_path_table_size_offset + PVD_path_table_size_size)
112 #define PVD_type_1_path_table_size 4
113 #define PVD_opt_type_1_path_table_offset (PVD_type_1_path_table_offset + PVD_type_1_path_table_size)
114 #define PVD_opt_type_1_path_table_size 4
115 #define PVD_type_m_path_table_offset (PVD_opt_type_1_path_table_offset + PVD_opt_type_1_path_table_size)
116 #define PVD_type_m_path_table_size 4
117 #define PVD_opt_type_m_path_table_offset (PVD_type_m_path_table_offset + PVD_type_m_path_table_size)
118 #define PVD_opt_type_m_path_table_size 4
119 #define PVD_root_directory_record_offset (PVD_opt_type_m_path_table_offset + PVD_opt_type_m_path_table_size)
120 #define PVD_root_directory_record_size 34
121 #define PVD_volume_set_id_offset (PVD_root_directory_record_offset + PVD_root_directory_record_size)
122 #define PVD_volume_set_id_size 128
123 #define PVD_publisher_id_offset (PVD_volume_set_id_offset + PVD_volume_set_id_size)
124 #define PVD_publisher_id_size 128
125 #define PVD_preparer_id_offset (PVD_publisher_id_offset + PVD_publisher_id_size)
126 #define PVD_preparer_id_size 128
127 #define PVD_application_id_offset (PVD_preparer_id_offset + PVD_preparer_id_size)
128 #define PVD_application_id_size 128
129 #define PVD_copyright_file_id_offset (PVD_application_id_offset + PVD_application_id_size)
130 #define PVD_copyright_file_id_size 37
131 #define PVD_abstract_file_id_offset (PVD_copyright_file_id_offset + PVD_copyright_file_id_size)
132 #define PVD_abstract_file_id_size 37
133 #define PVD_bibliographic_file_id_offset (PVD_abstract_file_id_offset + PVD_abstract_file_id_size)
134 #define PVD_bibliographic_file_id_size 37
135 #define PVD_creation_date_offset (PVD_bibliographic_file_id_offset + PVD_bibliographic_file_id_size)
136 #define PVD_creation_date_size 17
137 #define PVD_modification_date_offset (PVD_creation_date_offset + PVD_creation_date_size)
138 #define PVD_modification_date_size 17
139 #define PVD_expiration_date_offset (PVD_modification_date_offset + PVD_modification_date_size)
140 #define PVD_expiration_date_size 17
141 #define PVD_effective_date_offset (PVD_expiration_date_offset + PVD_expiration_date_size)
142 #define PVD_effective_date_size 17
143 #define PVD_file_structure_version_offset (PVD_effective_date_offset + PVD_effective_date_size)
144 #define PVD_file_structure_version_size 1
145 #define PVD_reserved4_offset (PVD_file_structure_version_offset + PVD_file_structure_version_size)
146 #define PVD_reserved4_size 1
147 #define PVD_application_data_offset (PVD_reserved4_offset + PVD_reserved4_size)
148 #define PVD_application_data_size 512
149 #define PVD_reserved5_offset (PVD_application_data_offset + PVD_application_data_size)
150 #define PVD_reserved5_size (2048 - PVD_reserved5_offset)
152 /* TODO: It would make future maintenance easier to just hardcode the
153 * above values. In particular, ECMA119 states the offsets as part of
154 * the standard. That would eliminate the need for the following check.*/
155 #if PVD_reserved5_offset != 1395
156 #error PVD offset and size definitions are wrong.
160 /* Structure of optional on-disk supplementary volume descriptor. */
161 #define SVD_type_offset 0
162 #define SVD_type_size 1
163 #define SVD_id_offset (SVD_type_offset + SVD_type_size)
164 #define SVD_id_size 5
165 #define SVD_version_offset (SVD_id_offset + SVD_id_size)
166 #define SVD_version_size 1
168 #define SVD_reserved1_offset 72
169 #define SVD_reserved1_size 8
170 #define SVD_volume_space_size_offset 80
171 #define SVD_volume_space_size_size 8
172 #define SVD_escape_sequences_offset (SVD_volume_space_size_offset + SVD_volume_space_size_size)
173 #define SVD_escape_sequences_size 32
175 #define SVD_logical_block_size_offset 128
176 #define SVD_logical_block_size_size 4
177 #define SVD_type_L_path_table_offset 140
178 #define SVD_type_M_path_table_offset 148
180 #define SVD_root_directory_record_offset 156
181 #define SVD_root_directory_record_size 34
182 #define SVD_file_structure_version_offset 881
183 #define SVD_reserved2_offset 882
184 #define SVD_reserved2_size 1
185 #define SVD_reserved3_offset 1395
186 #define SVD_reserved3_size 653
188 /* FIXME: validate correctness of last SVD entry offset. */
190 /* Structure of an on-disk directory record. */
191 /* Note: ISO9660 stores each multi-byte integer twice, once in
192 * each byte order. The sizes here are the size of just one
193 * of the two integers. (This is why the offset of a field isn't
194 * the same as the offset+size of the previous field.) */
195 #define DR_length_offset 0
196 #define DR_length_size 1
197 #define DR_ext_attr_length_offset 1
198 #define DR_ext_attr_length_size 1
199 #define DR_extent_offset 2
200 #define DR_extent_size 4
201 #define DR_size_offset 10
202 #define DR_size_size 4
203 #define DR_date_offset 18
204 #define DR_date_size 7
205 #define DR_flags_offset 25
206 #define DR_flags_size 1
207 #define DR_file_unit_size_offset 26
208 #define DR_file_unit_size_size 1
209 #define DR_interleave_offset 27
210 #define DR_interleave_size 1
211 #define DR_volume_sequence_number_offset 28
212 #define DR_volume_sequence_number_size 2
213 #define DR_name_len_offset 32
214 #define DR_name_len_size 1
215 #define DR_name_offset 33
218 static const unsigned char zisofs_magic[8] = {
219 0x37, 0xE4, 0x53, 0x96, 0xC9, 0xDB, 0xD6, 0x07
223 /* Set 1 if this file compressed by paged zlib */
225 int pz_log2_bs; /* Log2 of block size */
226 uint64_t pz_uncompressed_size;
229 unsigned char *uncompressed_buffer;
230 size_t uncompressed_buffer_size;
233 unsigned char header[16];
236 unsigned char *block_pointers;
237 size_t block_pointers_alloc;
238 size_t block_pointers_size;
239 size_t block_pointers_avail;
241 uint32_t block_avail;
248 /* Set 1 if this file compressed by paged zlib */
254 uint64_t offset;/* Offset on disk. */
255 uint64_t size; /* File size in bytes. */
256 struct content *next;
259 /* In-memory storage for a directory record. */
261 struct file_info *use_next;
262 struct file_info *parent;
263 struct file_info *next;
264 struct file_info *re_next;
266 uint64_t key; /* Heap Key. */
267 uint64_t offset; /* Offset on disk. */
268 uint64_t size; /* File size in bytes. */
269 uint32_t ce_offset; /* Offset of CE. */
270 uint32_t ce_size; /* Size of CE. */
271 char rr_moved; /* Flag to rr_moved. */
272 char rr_moved_has_re_only;
273 char re; /* Having RRIP "RE" extension. */
275 uint64_t cl_offset; /* Having RRIP "CL" extension. */
276 int birthtime_is_set;
277 time_t birthtime; /* File created time. */
278 time_t mtime; /* File last modified time. */
279 time_t atime; /* File last accessed time. */
280 time_t ctime; /* File attribute change time. */
281 uint64_t rdev; /* Device number. */
287 struct archive_string name; /* Pathname */
288 char name_continues; /* Non-zero if name continues */
289 struct archive_string symlink;
290 char symlink_continues; /* Non-zero if link continues */
291 /* Set 1 if this file compressed by paged zlib(zisofs) */
293 int pz_log2_bs; /* Log2 of block size */
294 uint64_t pz_uncompressed_size;
295 /* Set 1 if this file is multi extent. */
298 struct content *first;
299 struct content **last;
302 struct file_info *first;
303 struct file_info **last;
308 struct file_info **files;
315 #define ISO9660_MAGIC 0x96609660
317 int opt_support_joliet;
318 int opt_support_rockridge;
320 struct archive_string pathname;
321 char seenRockridge; /* Set true if RR extensions are used. */
322 char seenSUSP; /* Set true if SUSP is beging used. */
325 unsigned char suspOffset;
326 struct file_info *rr_moved;
327 struct read_ce_queue {
329 uint64_t offset;/* Offset of CE on disk. */
330 struct file_info *file;
336 int64_t previous_number;
337 struct archive_string previous_pathname;
339 struct file_info *use_files;
340 struct heap_queue pending_files;
342 struct file_info *first;
343 struct file_info **last;
346 struct file_info *first;
347 struct file_info **last;
350 uint64_t current_position;
351 ssize_t logical_block_size;
352 uint64_t volume_size; /* Total size of volume in bytes. */
353 int32_t volume_block;/* Total size of volume in logical blocks. */
356 int location; /* Location of Extent. */
360 off_t entry_sparse_offset;
361 int64_t entry_bytes_remaining;
362 struct zisofs entry_zisofs;
363 struct content *entry_content;
366 static int archive_read_format_iso9660_bid(struct archive_read *);
367 static int archive_read_format_iso9660_options(struct archive_read *,
368 const char *, const char *);
369 static int archive_read_format_iso9660_cleanup(struct archive_read *);
370 static int archive_read_format_iso9660_read_data(struct archive_read *,
371 const void **, size_t *, off_t *);
372 static int archive_read_format_iso9660_read_data_skip(struct archive_read *);
373 static int archive_read_format_iso9660_read_header(struct archive_read *,
374 struct archive_entry *);
375 static const char *build_pathname(struct archive_string *, struct file_info *);
377 static void dump_isodirrec(FILE *, const unsigned char *isodirrec);
379 static time_t time_from_tm(struct tm *);
380 static time_t isodate17(const unsigned char *);
381 static time_t isodate7(const unsigned char *);
382 static int isBootRecord(struct iso9660 *, const unsigned char *);
383 static int isVolumePartition(struct iso9660 *, const unsigned char *);
384 static int isVDSetTerminator(struct iso9660 *, const unsigned char *);
385 static int isJolietSVD(struct iso9660 *, const unsigned char *);
386 static int isSVD(struct iso9660 *, const unsigned char *);
387 static int isEVD(struct iso9660 *, const unsigned char *);
388 static int isPVD(struct iso9660 *, const unsigned char *);
389 static int next_cache_entry(struct archive_read *, struct iso9660 *,
390 struct file_info **);
391 static int next_entry_seek(struct archive_read *a, struct iso9660 *iso9660,
392 struct file_info **pfile);
393 static struct file_info *
394 parse_file_info(struct archive_read *a,
395 struct file_info *parent, const unsigned char *isodirrec);
396 static int parse_rockridge(struct archive_read *a,
397 struct file_info *file, const unsigned char *start,
398 const unsigned char *end);
399 static int register_CE(struct archive_read *a, int32_t location,
400 struct file_info *file);
401 static int read_CE(struct archive_read *a, struct iso9660 *iso9660);
402 static void parse_rockridge_NM1(struct file_info *,
403 const unsigned char *, int);
404 static void parse_rockridge_SL1(struct file_info *,
405 const unsigned char *, int);
406 static void parse_rockridge_TF1(struct file_info *,
407 const unsigned char *, int);
408 static void parse_rockridge_ZF1(struct file_info *,
409 const unsigned char *, int);
410 static void register_file(struct iso9660 *, struct file_info *);
411 static void release_files(struct iso9660 *);
412 static unsigned toi(const void *p, int n);
413 static inline void re_add_entry(struct iso9660 *, struct file_info *);
414 static inline struct file_info * re_get_entry(struct iso9660 *);
415 static inline int rede_add_entry(struct file_info *);
416 static inline struct file_info * rede_get_entry(struct file_info *);
417 static inline void cache_add_entry(struct iso9660 *iso9660,
418 struct file_info *file);
419 static inline struct file_info *cache_get_entry(struct iso9660 *iso9660);
420 static void heap_add_entry(struct heap_queue *heap,
421 struct file_info *file, uint64_t key);
422 static struct file_info *heap_get_entry(struct heap_queue *heap);
424 #define add_entry(iso9660, file) \
425 heap_add_entry(&((iso9660)->pending_files), file, file->offset)
426 #define next_entry(iso9660) \
427 heap_get_entry(&((iso9660)->pending_files))
430 archive_read_support_format_iso9660(struct archive *_a)
432 struct archive_read *a = (struct archive_read *)_a;
433 struct iso9660 *iso9660;
436 iso9660 = (struct iso9660 *)malloc(sizeof(*iso9660));
437 if (iso9660 == NULL) {
438 archive_set_error(&a->archive, ENOMEM, "Can't allocate iso9660 data");
439 return (ARCHIVE_FATAL);
441 memset(iso9660, 0, sizeof(*iso9660));
442 iso9660->magic = ISO9660_MAGIC;
443 iso9660->cache_files.first = NULL;
444 iso9660->cache_files.last = &(iso9660->cache_files.first);
445 iso9660->re_files.first = NULL;
446 iso9660->re_files.last = &(iso9660->re_files.first);
447 /* Enable to support Joliet extensions by default. */
448 iso9660->opt_support_joliet = 1;
449 /* Enable to support Rock Ridge extensions by default. */
450 iso9660->opt_support_rockridge = 1;
452 r = __archive_read_register_format(a,
455 archive_read_format_iso9660_bid,
456 archive_read_format_iso9660_options,
457 archive_read_format_iso9660_read_header,
458 archive_read_format_iso9660_read_data,
459 archive_read_format_iso9660_read_data_skip,
460 archive_read_format_iso9660_cleanup);
462 if (r != ARCHIVE_OK) {
471 archive_read_format_iso9660_bid(struct archive_read *a)
473 struct iso9660 *iso9660;
476 const unsigned char *p;
479 iso9660 = (struct iso9660 *)(a->format->data);
482 * Skip the first 32k (reserved area) and get the first
483 * 8 sectors of the volume descriptor table. Of course,
484 * if the I/O layer gives us more, we'll take it.
486 #define RESERVED_AREA (SYSTEM_AREA_BLOCK * LOGICAL_BLOCK_SIZE)
487 h = __archive_read_ahead(a,
488 RESERVED_AREA + 8 * LOGICAL_BLOCK_SIZE,
492 p = (const unsigned char *)h;
494 /* Skip the reserved area. */
495 bytes_read -= RESERVED_AREA;
498 /* Check each volume descriptor. */
500 for (; bytes_read > LOGICAL_BLOCK_SIZE;
501 bytes_read -= LOGICAL_BLOCK_SIZE, p += LOGICAL_BLOCK_SIZE) {
502 /* Do not handle undefined Volume Descriptor Type. */
503 if (p[0] >= 4 && p[0] <= 254)
505 /* Standard Identifier must be "CD001" */
506 if (memcmp(p + 1, "CD001", 5) != 0)
508 if (!iso9660->primary.location) {
509 if (isPVD(iso9660, p))
512 if (!iso9660->joliet.location) {
513 if (isJolietSVD(iso9660, p))
516 if (isBootRecord(iso9660, p))
518 if (isEVD(iso9660, p))
520 if (isSVD(iso9660, p))
522 if (isVolumePartition(iso9660, p))
524 if (isVDSetTerminator(iso9660, p)) {
531 * ISO 9660 format must have Primary Volume Descriptor and
532 * Volume Descriptor Set Terminator.
534 if (seenTerminator && iso9660->primary.location > 16)
537 /* We didn't find a valid PVD; return a bid of zero. */
542 archive_read_format_iso9660_options(struct archive_read *a,
543 const char *key, const char *val)
545 struct iso9660 *iso9660;
547 iso9660 = (struct iso9660 *)(a->format->data);
549 if (strcmp(key, "joliet") == 0) {
550 if (val == NULL || strcmp(val, "off") == 0 ||
551 strcmp(val, "ignore") == 0 ||
552 strcmp(val, "disable") == 0 ||
553 strcmp(val, "0") == 0)
554 iso9660->opt_support_joliet = 0;
556 iso9660->opt_support_joliet = 1;
559 if (strcmp(key, "rockridge") == 0 ||
560 strcmp(key, "Rockridge") == 0) {
561 iso9660->opt_support_rockridge = val != NULL;
565 /* Note: The "warn" return is just to inform the options
566 * supervisor that we didn't handle it. It will generate
567 * a suitable error if noone used this option. */
568 return (ARCHIVE_WARN);
572 isBootRecord(struct iso9660 *iso9660, const unsigned char *h)
574 (void)iso9660; /* UNUSED */
576 /* Type of the Volume Descriptor Boot Record must be 0. */
580 /* Volume Descriptor Version must be 1. */
588 isVolumePartition(struct iso9660 *iso9660, const unsigned char *h)
592 /* Type of the Volume Partition Descriptor must be 3. */
596 /* Volume Descriptor Version must be 1. */
603 location = archive_le32dec(h + 72);
604 if (location <= SYSTEM_AREA_BLOCK ||
605 location >= iso9660->volume_block)
607 if ((uint32_t)location != archive_be32dec(h + 76))
614 isVDSetTerminator(struct iso9660 *iso9660, const unsigned char *h)
618 (void)iso9660; /* UNUSED */
620 /* Type of the Volume Descriptor Set Terminator must be 255. */
624 /* Volume Descriptor Version must be 1. */
628 /* Reserved field must be 0. */
629 for (i = 7; i < 2048; ++i)
637 isJolietSVD(struct iso9660 *iso9660, const unsigned char *h)
639 const unsigned char *p;
640 ssize_t logical_block_size;
641 int32_t volume_block;
643 /* Check if current sector is a kind of Supplementary Volume
645 if (!isSVD(iso9660, h))
648 /* FIXME: do more validations according to joliet spec. */
650 /* check if this SVD contains joliet extension! */
651 p = h + SVD_escape_sequences_offset;
652 /* N.B. Joliet spec says p[1] == '\\', but.... */
653 if (p[0] == '%' && p[1] == '/') {
658 else if (p[2] == 'C')
660 else if (p[2] == 'E')
662 else /* not joliet */
665 iso9660->seenJoliet = level;
667 } else /* not joliet */
671 archive_le16dec(h + SVD_logical_block_size_offset);
672 volume_block = archive_le32dec(h + SVD_volume_space_size_offset);
674 iso9660->logical_block_size = logical_block_size;
675 iso9660->volume_block = volume_block;
676 iso9660->volume_size = logical_block_size * (uint64_t)volume_block;
677 /* Read Root Directory Record in Volume Descriptor. */
678 p = h + SVD_root_directory_record_offset;
679 iso9660->joliet.location = archive_le32dec(p + DR_extent_offset);
680 iso9660->joliet.size = archive_le32dec(p + DR_size_offset);
686 isSVD(struct iso9660 *iso9660, const unsigned char *h)
688 const unsigned char *p;
689 ssize_t logical_block_size;
690 int32_t volume_block;
694 (void)iso9660; /* UNUSED */
696 /* Type 2 means it's a SVD. */
697 if (h[SVD_type_offset] != 2)
700 /* Reserved field must be 0. */
701 for (i = 0; i < SVD_reserved1_size; ++i)
702 if (h[SVD_reserved1_offset + i] != 0)
704 for (i = 0; i < SVD_reserved2_size; ++i)
705 if (h[SVD_reserved2_offset + i] != 0)
707 for (i = 0; i < SVD_reserved3_size; ++i)
708 if (h[SVD_reserved3_offset + i] != 0)
711 /* File structure version must be 1 for ISO9660/ECMA119. */
712 if (h[SVD_file_structure_version_offset] != 1)
716 archive_le16dec(h + SVD_logical_block_size_offset);
717 if (logical_block_size <= 0)
720 volume_block = archive_le32dec(h + SVD_volume_space_size_offset);
721 if (volume_block <= SYSTEM_AREA_BLOCK+4)
724 /* Location of Occurrence of Type L Path Table must be
725 * available location,
726 * >= SYSTEM_AREA_BLOCK(16) + 2 and < Volume Space Size. */
727 location = archive_le32dec(h+SVD_type_L_path_table_offset);
728 if (location < SYSTEM_AREA_BLOCK+2 || location >= volume_block)
731 /* The Type M Path Table must be at a valid location (WinISO
732 * and probably other programs omit this, so we allow zero)
734 * >= SYSTEM_AREA_BLOCK(16) + 2 and < Volume Space Size. */
735 location = archive_be32dec(h+SVD_type_M_path_table_offset);
736 if ((location > 0 && location < SYSTEM_AREA_BLOCK+2)
737 || location >= volume_block)
740 /* Read Root Directory Record in Volume Descriptor. */
741 p = h + SVD_root_directory_record_offset;
742 if (p[DR_length_offset] != 34)
749 isEVD(struct iso9660 *iso9660, const unsigned char *h)
751 const unsigned char *p;
752 ssize_t logical_block_size;
753 int32_t volume_block;
757 (void)iso9660; /* UNUSED */
759 /* Type of the Enhanced Volume Descriptor must be 2. */
760 if (h[PVD_type_offset] != 2)
763 /* EVD version must be 2. */
764 if (h[PVD_version_offset] != 2)
767 /* Reserved field must be 0. */
768 if (h[PVD_reserved1_offset] != 0)
771 /* Reserved field must be 0. */
772 for (i = 0; i < PVD_reserved2_size; ++i)
773 if (h[PVD_reserved2_offset + i] != 0)
776 /* Reserved field must be 0. */
777 for (i = 0; i < PVD_reserved3_size; ++i)
778 if (h[PVD_reserved3_offset + i] != 0)
781 /* Logical block size must be > 0. */
782 /* I've looked at Ecma 119 and can't find any stronger
783 * restriction on this field. */
785 archive_le16dec(h + PVD_logical_block_size_offset);
786 if (logical_block_size <= 0)
790 archive_le32dec(h + PVD_volume_space_size_offset);
791 if (volume_block <= SYSTEM_AREA_BLOCK+4)
794 /* File structure version must be 2 for ISO9660:1999. */
795 if (h[PVD_file_structure_version_offset] != 2)
798 /* Location of Occurrence of Type L Path Table must be
799 * available location,
800 * >= SYSTEM_AREA_BLOCK(16) + 2 and < Volume Space Size. */
801 location = archive_le32dec(h+PVD_type_1_path_table_offset);
802 if (location < SYSTEM_AREA_BLOCK+2 || location >= volume_block)
805 /* Location of Occurrence of Type M Path Table must be
806 * available location,
807 * >= SYSTEM_AREA_BLOCK(16) + 2 and < Volume Space Size. */
808 location = archive_be32dec(h+PVD_type_m_path_table_offset);
809 if ((location > 0 && location < SYSTEM_AREA_BLOCK+2)
810 || location >= volume_block)
813 /* Reserved field must be 0. */
814 for (i = 0; i < PVD_reserved4_size; ++i)
815 if (h[PVD_reserved4_offset + i] != 0)
818 /* Reserved field must be 0. */
819 for (i = 0; i < PVD_reserved5_size; ++i)
820 if (h[PVD_reserved5_offset + i] != 0)
823 /* Read Root Directory Record in Volume Descriptor. */
824 p = h + PVD_root_directory_record_offset;
825 if (p[DR_length_offset] != 34)
832 isPVD(struct iso9660 *iso9660, const unsigned char *h)
834 const unsigned char *p;
835 ssize_t logical_block_size;
836 int32_t volume_block;
840 /* Type of the Primary Volume Descriptor must be 1. */
841 if (h[PVD_type_offset] != 1)
844 /* PVD version must be 1. */
845 if (h[PVD_version_offset] != 1)
848 /* Reserved field must be 0. */
849 if (h[PVD_reserved1_offset] != 0)
852 /* Reserved field must be 0. */
853 for (i = 0; i < PVD_reserved2_size; ++i)
854 if (h[PVD_reserved2_offset + i] != 0)
857 /* Reserved field must be 0. */
858 for (i = 0; i < PVD_reserved3_size; ++i)
859 if (h[PVD_reserved3_offset + i] != 0)
862 /* Logical block size must be > 0. */
863 /* I've looked at Ecma 119 and can't find any stronger
864 * restriction on this field. */
866 archive_le16dec(h + PVD_logical_block_size_offset);
867 if (logical_block_size <= 0)
870 volume_block = archive_le32dec(h + PVD_volume_space_size_offset);
871 if (volume_block <= SYSTEM_AREA_BLOCK+4)
874 /* File structure version must be 1 for ISO9660/ECMA119. */
875 if (h[PVD_file_structure_version_offset] != 1)
878 /* Location of Occurrence of Type L Path Table must be
879 * available location,
880 * > SYSTEM_AREA_BLOCK(16) + 2 and < Volume Space Size. */
881 location = archive_le32dec(h+PVD_type_1_path_table_offset);
882 if (location < SYSTEM_AREA_BLOCK+2 || location >= volume_block)
885 /* The Type M Path Table must also be at a valid location
886 * (although ECMA 119 requires a Type M Path Table, WinISO and
887 * probably other programs omit it, so we permit a zero here)
889 * >= SYSTEM_AREA_BLOCK(16) + 2 and < Volume Space Size. */
890 location = archive_be32dec(h+PVD_type_m_path_table_offset);
891 if ((location > 0 && location < SYSTEM_AREA_BLOCK+2)
892 || location >= volume_block)
895 /* Reserved field must be 0. */
896 /* FreeBSD: makefs erroneously created images with 0x20 */
897 for (i = 0; i < PVD_reserved4_size; ++i)
898 if (h[PVD_reserved4_offset + i] != 0 &&
899 h[PVD_reserved4_offset + i] != 32)
902 /* Reserved field must be 0. */
903 for (i = 0; i < PVD_reserved5_size; ++i)
904 if (h[PVD_reserved5_offset + i] != 0)
907 /* XXX TODO: Check other values for sanity; reject more
908 * malformed PVDs. XXX */
910 /* Read Root Directory Record in Volume Descriptor. */
911 p = h + PVD_root_directory_record_offset;
912 if (p[DR_length_offset] != 34)
915 iso9660->logical_block_size = logical_block_size;
916 iso9660->volume_block = volume_block;
917 iso9660->volume_size = logical_block_size * (uint64_t)volume_block;
918 iso9660->primary.location = archive_le32dec(p + DR_extent_offset);
919 iso9660->primary.size = archive_le32dec(p + DR_size_offset);
925 read_children(struct archive_read *a, struct file_info *parent)
927 struct iso9660 *iso9660;
928 const unsigned char *b, *p;
929 struct file_info *multi;
932 iso9660 = (struct iso9660 *)(a->format->data);
933 if (iso9660->current_position > parent->offset) {
934 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
935 "Ignoring out-of-order directory (%s) %jd > %jd",
937 (intmax_t)iso9660->current_position,
938 (intmax_t)parent->offset);
939 return (ARCHIVE_WARN);
941 if (parent->offset + parent->size > iso9660->volume_size) {
942 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
943 "Directory is beyond end-of-media: %s",
945 return (ARCHIVE_WARN);
947 if (iso9660->current_position < parent->offset) {
950 skipsize = parent->offset - iso9660->current_position;
951 skipsize = __archive_read_skip(a, skipsize);
953 return ((int)skipsize);
954 iso9660->current_position = parent->offset;
957 step = ((parent->size + iso9660->logical_block_size -1) /
958 iso9660->logical_block_size) * iso9660->logical_block_size;
959 b = __archive_read_ahead(a, step, NULL);
961 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
962 "Failed to read full block when scanning "
963 "ISO9660 directory list");
964 return (ARCHIVE_FATAL);
966 __archive_read_consume(a, step);
967 iso9660->current_position += step;
971 b += iso9660->logical_block_size;
972 step -= iso9660->logical_block_size;
973 for (; *p != 0 && p < b && p + *p <= b; p += *p) {
974 struct file_info *child;
976 /* N.B.: these special directory identifiers
977 * are 8 bit "values" even on a
978 * Joliet CD with UCS-2 (16bit) encoding.
981 /* Skip '.' entry. */
982 if (*(p + DR_name_len_offset) == 1
983 && *(p + DR_name_offset) == '\0')
985 /* Skip '..' entry. */
986 if (*(p + DR_name_len_offset) == 1
987 && *(p + DR_name_offset) == '\001')
989 child = parse_file_info(a, parent, p);
991 return (ARCHIVE_FATAL);
992 if (child->cl_offset == 0 &&
993 (child->multi_extent || multi != NULL)) {
998 multi->contents.first = NULL;
999 multi->contents.last =
1000 &(multi->contents.first);
1002 con = malloc(sizeof(struct content));
1005 &a->archive, ENOMEM,
1008 return (ARCHIVE_FATAL);
1010 con->offset = child->offset;
1011 con->size = child->size;
1013 *multi->contents.last = con;
1014 multi->contents.last = &(con->next);
1016 add_entry(iso9660, child);
1018 multi->size += child->size;
1019 if (!child->multi_extent)
1023 add_entry(iso9660, child);
1027 /* Read data which recorded by RRIP "CE" extension. */
1028 if (read_CE(a, iso9660) != ARCHIVE_OK)
1029 return (ARCHIVE_FATAL);
1031 return (ARCHIVE_OK);
1035 archive_read_format_iso9660_read_header(struct archive_read *a,
1036 struct archive_entry *entry)
1038 struct iso9660 *iso9660;
1039 struct file_info *file;
1040 int r, rd_r = ARCHIVE_OK;
1042 iso9660 = (struct iso9660 *)(a->format->data);
1044 if (!a->archive.archive_format) {
1045 a->archive.archive_format = ARCHIVE_FORMAT_ISO9660;
1046 a->archive.archive_format_name = "ISO9660";
1049 if (iso9660->current_position == 0) {
1055 vd = &(iso9660->primary);
1056 if (!iso9660->opt_support_joliet)
1057 iso9660->seenJoliet = 0;
1058 if (iso9660->seenJoliet &&
1059 vd->location > iso9660->joliet.location)
1060 /* This condition is unlikely; by way of caution. */
1061 vd = &(iso9660->joliet);
1063 skipsize = LOGICAL_BLOCK_SIZE * vd->location;
1064 skipsize = __archive_read_skip(a, skipsize);
1066 return ((int)skipsize);
1067 iso9660->current_position = skipsize;
1069 block = __archive_read_ahead(a, vd->size, NULL);
1070 if (block == NULL) {
1071 archive_set_error(&a->archive,
1073 "Failed to read full block when scanning "
1074 "ISO9660 directory list");
1075 return (ARCHIVE_FATAL);
1079 * While reading Root Directory, flag seenJoliet
1080 * must be zero to avoid converting special name
1081 * 0x00(Current Directory) and next byte to UCS2.
1083 seenJoliet = iso9660->seenJoliet;/* Save flag. */
1084 iso9660->seenJoliet = 0;
1085 file = parse_file_info(a, NULL, block);
1087 return (ARCHIVE_FATAL);
1088 iso9660->seenJoliet = seenJoliet;
1089 if (vd == &(iso9660->primary) && iso9660->seenRockridge
1090 && iso9660->seenJoliet)
1092 * If iso image has RockRidge and Joliet,
1093 * we use RockRidge Extensions.
1095 iso9660->seenJoliet = 0;
1096 if (vd == &(iso9660->primary) && !iso9660->seenRockridge
1097 && iso9660->seenJoliet) {
1098 /* Switch reading data from primary to joliet. */
1099 vd = &(iso9660->joliet);
1100 skipsize = LOGICAL_BLOCK_SIZE * vd->location;
1101 skipsize -= iso9660->current_position;
1102 skipsize = __archive_read_skip(a, skipsize);
1104 return ((int)skipsize);
1105 iso9660->current_position += skipsize;
1107 block = __archive_read_ahead(a, vd->size, NULL);
1108 if (block == NULL) {
1109 archive_set_error(&a->archive,
1111 "Failed to read full block when scanning "
1112 "ISO9660 directory list");
1113 return (ARCHIVE_FATAL);
1115 seenJoliet = iso9660->seenJoliet;/* Save flag. */
1116 iso9660->seenJoliet = 0;
1117 file = parse_file_info(a, NULL, block);
1119 return (ARCHIVE_FATAL);
1120 iso9660->seenJoliet = seenJoliet;
1122 /* Store the root directory in the pending list. */
1123 add_entry(iso9660, file);
1124 if (iso9660->seenRockridge) {
1125 a->archive.archive_format =
1126 ARCHIVE_FORMAT_ISO9660_ROCKRIDGE;
1127 a->archive.archive_format_name =
1128 "ISO9660 with Rockridge extensions";
1132 /* Get the next entry that appears after the current offset. */
1133 r = next_entry_seek(a, iso9660, &file);
1134 if (r != ARCHIVE_OK)
1137 iso9660->entry_bytes_remaining = file->size;
1138 iso9660->entry_sparse_offset = 0; /* Offset for sparse-file-aware clients. */
1140 if (file->offset + file->size > iso9660->volume_size) {
1141 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1142 "File is beyond end-of-media: %s", file->name.s);
1143 iso9660->entry_bytes_remaining = 0;
1144 iso9660->entry_sparse_offset = 0;
1145 return (ARCHIVE_WARN);
1148 /* Set up the entry structure with information about this entry. */
1149 archive_entry_set_mode(entry, file->mode);
1150 archive_entry_set_uid(entry, file->uid);
1151 archive_entry_set_gid(entry, file->gid);
1152 archive_entry_set_nlink(entry, file->nlinks);
1153 if (file->birthtime_is_set)
1154 archive_entry_set_birthtime(entry, file->birthtime, 0);
1156 archive_entry_unset_birthtime(entry);
1157 archive_entry_set_mtime(entry, file->mtime, 0);
1158 archive_entry_set_ctime(entry, file->ctime, 0);
1159 archive_entry_set_atime(entry, file->atime, 0);
1160 /* N.B.: Rock Ridge supports 64-bit device numbers. */
1161 archive_entry_set_rdev(entry, (dev_t)file->rdev);
1162 archive_entry_set_size(entry, iso9660->entry_bytes_remaining);
1163 archive_string_empty(&iso9660->pathname);
1164 archive_entry_set_pathname(entry,
1165 build_pathname(&iso9660->pathname, file));
1166 if (file->symlink.s != NULL)
1167 archive_entry_copy_symlink(entry, file->symlink.s);
1169 /* Note: If the input isn't seekable, we can't rewind to
1170 * return the same body again, so if the next entry refers to
1171 * the same data, we have to return it as a hardlink to the
1172 * original entry. */
1173 if (file->number != -1 &&
1174 file->number == iso9660->previous_number) {
1175 archive_entry_set_hardlink(entry,
1176 iso9660->previous_pathname.s);
1177 archive_entry_unset_size(entry);
1178 iso9660->entry_bytes_remaining = 0;
1179 iso9660->entry_sparse_offset = 0;
1180 return (ARCHIVE_OK);
1183 /* Except for the hardlink case above, if the offset of the
1184 * next entry is before our current position, we can't seek
1185 * backwards to extract it, so issue a warning. Note that
1186 * this can only happen if this entry was added to the heap
1187 * after we passed this offset, that is, only if the directory
1188 * mentioning this entry is later than the body of the entry.
1189 * Such layouts are very unusual; most ISO9660 writers lay out
1190 * and record all directory information first, then store
1191 * all file bodies. */
1192 /* TODO: Someday, libarchive's I/O core will support optional
1193 * seeking. When that day comes, this code should attempt to
1194 * seek and only return the error if the seek fails. That
1195 * will give us support for whacky ISO images that require
1196 * seeking while retaining the ability to read almost all ISO
1197 * images in a streaming fashion. */
1198 if ((file->mode & AE_IFMT) != AE_IFDIR &&
1199 file->offset < iso9660->current_position) {
1200 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1201 "Ignoring out-of-order file (%s) %jd < %jd",
1202 iso9660->pathname.s,
1203 (intmax_t)file->offset,
1204 (intmax_t)iso9660->current_position);
1205 iso9660->entry_bytes_remaining = 0;
1206 iso9660->entry_sparse_offset = 0;
1207 return (ARCHIVE_WARN);
1210 /* Initialize zisofs variables. */
1211 iso9660->entry_zisofs.pz = file->pz;
1214 struct zisofs *zisofs;
1216 zisofs = &iso9660->entry_zisofs;
1217 zisofs->initialized = 0;
1218 zisofs->pz_log2_bs = file->pz_log2_bs;
1219 zisofs->pz_uncompressed_size = file->pz_uncompressed_size;
1220 zisofs->pz_offset = 0;
1221 zisofs->header_avail = 0;
1222 zisofs->header_passed = 0;
1223 zisofs->block_pointers_avail = 0;
1225 archive_entry_set_size(entry, file->pz_uncompressed_size);
1228 iso9660->previous_number = file->number;
1229 archive_strcpy(&iso9660->previous_pathname, iso9660->pathname.s);
1231 /* Reset entry_bytes_remaining if the file is multi extent. */
1232 iso9660->entry_content = file->contents.first;
1233 if (iso9660->entry_content != NULL)
1234 iso9660->entry_bytes_remaining = iso9660->entry_content->size;
1236 if (archive_entry_filetype(entry) == AE_IFDIR) {
1237 /* Overwrite nlinks by proper link number which is
1238 * calculated from number of sub directories. */
1239 archive_entry_set_nlink(entry, 2 + file->subdirs);
1240 /* Directory data has been read completely. */
1241 iso9660->entry_bytes_remaining = 0;
1242 iso9660->entry_sparse_offset = 0;
1245 if (rd_r != ARCHIVE_OK)
1247 return (ARCHIVE_OK);
1251 archive_read_format_iso9660_read_data_skip(struct archive_read *a)
1253 /* Because read_next_header always does an explicit skip
1254 * to the next entry, we don't need to do anything here. */
1255 (void)a; /* UNUSED */
1256 return (ARCHIVE_OK);
1262 zisofs_read_data(struct archive_read *a,
1263 const void **buff, size_t *size, off_t *offset)
1265 struct iso9660 *iso9660;
1266 struct zisofs *zisofs;
1267 const unsigned char *p;
1270 size_t uncompressed_size;
1273 iso9660 = (struct iso9660 *)(a->format->data);
1274 zisofs = &iso9660->entry_zisofs;
1276 p = __archive_read_ahead(a, 1, &bytes_read);
1277 if (bytes_read <= 0) {
1278 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1279 "Truncated zisofs file body");
1280 return (ARCHIVE_FATAL);
1282 if (bytes_read > iso9660->entry_bytes_remaining)
1283 bytes_read = iso9660->entry_bytes_remaining;
1285 uncompressed_size = 0;
1287 if (!zisofs->initialized) {
1290 /* Allocate block pointers buffer. */
1291 ceil = (zisofs->pz_uncompressed_size +
1292 (1LL << zisofs->pz_log2_bs) - 1)
1293 >> zisofs->pz_log2_bs;
1294 xsize = (ceil + 1) * 4;
1295 if (zisofs->block_pointers_alloc < xsize) {
1298 if (zisofs->block_pointers != NULL)
1299 free(zisofs->block_pointers);
1300 alloc = ((xsize >> 10) + 1) << 10;
1301 zisofs->block_pointers = malloc(alloc);
1302 if (zisofs->block_pointers == NULL) {
1303 archive_set_error(&a->archive, ENOMEM,
1304 "No memory for zisofs decompression");
1305 return (ARCHIVE_FATAL);
1307 zisofs->block_pointers_alloc = alloc;
1309 zisofs->block_pointers_size = xsize;
1311 /* Allocate uncompressed data buffer. */
1312 xsize = 1UL << zisofs->pz_log2_bs;
1313 if (zisofs->uncompressed_buffer_size < xsize) {
1314 if (zisofs->uncompressed_buffer != NULL)
1315 free(zisofs->uncompressed_buffer);
1316 zisofs->uncompressed_buffer = malloc(xsize);
1317 if (zisofs->uncompressed_buffer == NULL) {
1318 archive_set_error(&a->archive, ENOMEM,
1319 "No memory for zisofs decompression");
1320 return (ARCHIVE_FATAL);
1323 zisofs->uncompressed_buffer_size = xsize;
1326 * Read the file header, and check the magic code of zisofs.
1328 if (zisofs->header_avail < sizeof(zisofs->header)) {
1329 xsize = sizeof(zisofs->header) - zisofs->header_avail;
1332 memcpy(zisofs->header + zisofs->header_avail, p, xsize);
1333 zisofs->header_avail += xsize;
1337 if (!zisofs->header_passed &&
1338 zisofs->header_avail == sizeof(zisofs->header)) {
1341 if (memcmp(zisofs->header, zisofs_magic,
1342 sizeof(zisofs_magic)) != 0)
1344 if (archive_le32dec(zisofs->header + 8)
1345 != zisofs->pz_uncompressed_size)
1347 if (zisofs->header[12] != 4)
1349 if (zisofs->header[13] != zisofs->pz_log2_bs)
1352 archive_set_error(&a->archive,
1353 ARCHIVE_ERRNO_FILE_FORMAT,
1354 "Illegal zisofs file body");
1355 return (ARCHIVE_FATAL);
1357 zisofs->header_passed = 1;
1360 * Read block pointers.
1362 if (zisofs->header_passed &&
1363 zisofs->block_pointers_avail < zisofs->block_pointers_size) {
1364 xsize = zisofs->block_pointers_size
1365 - zisofs->block_pointers_avail;
1368 memcpy(zisofs->block_pointers
1369 + zisofs->block_pointers_avail, p, xsize);
1370 zisofs->block_pointers_avail += xsize;
1373 if (zisofs->block_pointers_avail
1374 == zisofs->block_pointers_size) {
1375 /* We've got all block pointers and initialize
1376 * related variables. */
1377 zisofs->block_off = 0;
1378 zisofs->block_avail = 0;
1379 /* Complete a initialization */
1380 zisofs->initialized = 1;
1384 if (!zisofs->initialized)
1385 goto next_data; /* We need more datas. */
1389 * Get block offsets from block pointers.
1391 if (zisofs->block_avail == 0) {
1394 if (zisofs->block_off + 4 >= zisofs->block_pointers_size) {
1395 /* There isn't a pair of offsets. */
1396 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1397 "Illegal zisofs block pointers");
1398 return (ARCHIVE_FATAL);
1400 bst = archive_le32dec(zisofs->block_pointers + zisofs->block_off);
1401 if (bst != zisofs->pz_offset + (bytes_read - avail)) {
1402 /* TODO: Should we seek offset of current file by bst ? */
1403 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1404 "Illegal zisofs block pointers(cannot seek)");
1405 return (ARCHIVE_FATAL);
1407 bed = archive_le32dec(
1408 zisofs->block_pointers + zisofs->block_off + 4);
1410 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1411 "Illegal zisofs block pointers");
1412 return (ARCHIVE_FATAL);
1414 zisofs->block_avail = bed - bst;
1415 zisofs->block_off += 4;
1417 /* Initialize compression library for new block. */
1418 if (zisofs->stream_valid)
1419 r = inflateReset(&zisofs->stream);
1421 r = inflateInit(&zisofs->stream);
1423 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1424 "Can't initialize zisofs decompression.");
1425 return (ARCHIVE_FATAL);
1427 zisofs->stream_valid = 1;
1428 zisofs->stream.total_in = 0;
1429 zisofs->stream.total_out = 0;
1433 * Make uncompressed datas.
1435 if (zisofs->block_avail == 0) {
1436 memset(zisofs->uncompressed_buffer, 0,
1437 zisofs->uncompressed_buffer_size);
1438 uncompressed_size = zisofs->uncompressed_buffer_size;
1440 zisofs->stream.next_in = (Bytef *)(uintptr_t)(const void *)p;
1441 if (avail > zisofs->block_avail)
1442 zisofs->stream.avail_in = zisofs->block_avail;
1444 zisofs->stream.avail_in = avail;
1445 zisofs->stream.next_out = zisofs->uncompressed_buffer;
1446 zisofs->stream.avail_out = zisofs->uncompressed_buffer_size;
1448 r = inflate(&zisofs->stream, 0);
1450 case Z_OK: /* Decompressor made some progress.*/
1451 case Z_STREAM_END: /* Found end of stream. */
1454 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1455 "zisofs decompression failed (%d)", r);
1456 return (ARCHIVE_FATAL);
1459 zisofs->uncompressed_buffer_size - zisofs->stream.avail_out;
1460 avail -= zisofs->stream.next_in - p;
1461 zisofs->block_avail -= zisofs->stream.next_in - p;
1464 bytes_read -= avail;
1465 *buff = zisofs->uncompressed_buffer;
1466 *size = uncompressed_size;
1467 *offset = iso9660->entry_sparse_offset;
1468 iso9660->entry_sparse_offset += uncompressed_size;
1469 iso9660->entry_bytes_remaining -= bytes_read;
1470 iso9660->current_position += bytes_read;
1471 zisofs->pz_offset += bytes_read;
1472 __archive_read_consume(a, bytes_read);
1474 return (ARCHIVE_OK);
1477 #else /* HAVE_ZLIB_H */
1480 zisofs_read_data(struct archive_read *a,
1481 const void **buff, size_t *size, off_t *offset)
1484 (void)buff;/* UNUSED */
1485 (void)size;/* UNUSED */
1486 (void)offset;/* UNUSED */
1487 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1488 "zisofs is not supported on this platform.");
1489 return (ARCHIVE_FAILED);
1492 #endif /* HAVE_ZLIB_H */
1495 archive_read_format_iso9660_read_data(struct archive_read *a,
1496 const void **buff, size_t *size, off_t *offset)
1499 struct iso9660 *iso9660;
1501 iso9660 = (struct iso9660 *)(a->format->data);
1502 if (iso9660->entry_bytes_remaining <= 0) {
1503 if (iso9660->entry_content != NULL)
1504 iso9660->entry_content = iso9660->entry_content->next;
1505 if (iso9660->entry_content == NULL) {
1508 *offset = iso9660->entry_sparse_offset;
1509 return (ARCHIVE_EOF);
1511 /* Seek forward to the start of the entry. */
1512 if (iso9660->current_position < iso9660->entry_content->offset) {
1515 step = iso9660->entry_content->offset -
1516 iso9660->current_position;
1517 step = __archive_read_skip(a, step);
1520 iso9660->current_position =
1521 iso9660->entry_content->offset;
1523 if (iso9660->entry_content->offset < iso9660->current_position) {
1524 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1525 "Ignoring out-of-order file (%s) %jd < %jd",
1526 iso9660->pathname.s,
1527 (intmax_t)iso9660->entry_content->offset,
1528 (intmax_t)iso9660->current_position);
1531 *offset = iso9660->entry_sparse_offset;
1532 return (ARCHIVE_WARN);
1534 iso9660->entry_bytes_remaining = iso9660->entry_content->size;
1536 if (iso9660->entry_zisofs.pz)
1537 return (zisofs_read_data(a, buff, size, offset));
1539 *buff = __archive_read_ahead(a, 1, &bytes_read);
1540 if (bytes_read == 0)
1541 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1542 "Truncated input file");
1544 return (ARCHIVE_FATAL);
1545 if (bytes_read > iso9660->entry_bytes_remaining)
1546 bytes_read = iso9660->entry_bytes_remaining;
1548 *offset = iso9660->entry_sparse_offset;
1549 iso9660->entry_sparse_offset += bytes_read;
1550 iso9660->entry_bytes_remaining -= bytes_read;
1551 iso9660->current_position += bytes_read;
1552 __archive_read_consume(a, bytes_read);
1553 return (ARCHIVE_OK);
1557 archive_read_format_iso9660_cleanup(struct archive_read *a)
1559 struct iso9660 *iso9660;
1562 iso9660 = (struct iso9660 *)(a->format->data);
1563 release_files(iso9660);
1564 free(iso9660->read_ce_req.reqs);
1565 archive_string_free(&iso9660->pathname);
1566 archive_string_free(&iso9660->previous_pathname);
1567 if (iso9660->pending_files.files)
1568 free(iso9660->pending_files.files);
1570 free(iso9660->entry_zisofs.uncompressed_buffer);
1571 free(iso9660->entry_zisofs.block_pointers);
1572 if (iso9660->entry_zisofs.stream_valid) {
1573 if (inflateEnd(&iso9660->entry_zisofs.stream) != Z_OK) {
1574 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1575 "Failed to clean up zlib decompressor");
1581 (a->format->data) = NULL;
1586 * This routine parses a single ISO directory record, makes sense
1587 * of any extensions, and stores the result in memory.
1589 static struct file_info *
1590 parse_file_info(struct archive_read *a, struct file_info *parent,
1591 const unsigned char *isodirrec)
1593 struct iso9660 *iso9660;
1594 struct file_info *file;
1596 const unsigned char *rr_start, *rr_end;
1597 const unsigned char *p;
1603 iso9660 = (struct iso9660 *)(a->format->data);
1605 dr_len = (size_t)isodirrec[DR_length_offset];
1606 name_len = (size_t)isodirrec[DR_name_len_offset];
1607 location = archive_le32dec(isodirrec + DR_extent_offset);
1608 fsize = toi(isodirrec + DR_size_offset, DR_size_size);
1609 /* Sanity check that dr_len needs at least 34. */
1611 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1612 "Invalid length of directory record");
1615 /* Sanity check that name_len doesn't exceed dr_len. */
1616 if (dr_len - 33 < name_len || name_len == 0) {
1617 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1618 "Invalid length of file identifier");
1621 /* Sanity check that location doesn't exceed volume block.
1622 * Don't check lower limit of location; it's possibility
1623 * the location has negative value when file type is symbolic
1624 * link or file size is zero. As far as I know latest mkisofs
1628 (location + ((fsize + iso9660->logical_block_size -1)
1629 / iso9660->logical_block_size))
1630 > (uint32_t)iso9660->volume_block) {
1631 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1632 "Invalid location of extent of file");
1636 /* Create a new file entry and copy data from the ISO dir record. */
1637 file = (struct file_info *)malloc(sizeof(*file));
1639 archive_set_error(&a->archive, ENOMEM,
1640 "No memory for file entry");
1643 memset(file, 0, sizeof(*file));
1644 file->parent = parent;
1645 file->offset = iso9660->logical_block_size * (uint64_t)location;
1647 file->mtime = isodate7(isodirrec + DR_date_offset);
1648 file->ctime = file->atime = file->mtime;
1649 file->rede_files.first = NULL;
1650 file->rede_files.last = &(file->rede_files.first);
1652 p = isodirrec + DR_name_offset;
1653 /* Rockridge extensions (if any) follow name. Compute this
1654 * before fidgeting the name_len below. */
1655 rr_start = p + name_len + (name_len & 1 ? 0 : 1);
1656 rr_end = isodirrec + dr_len;
1658 if (iso9660->seenJoliet) {
1659 /* Joliet names are max 64 chars (128 bytes) according to spec,
1660 * but genisoimage/mkisofs allows recording longer Joliet
1661 * names which are 103 UCS2 characters(206 bytes) by their
1662 * option '-joliet-long'.
1664 wchar_t wbuff[103+1], *wp;
1665 const unsigned char *c;
1669 /* convert BE UTF-16 to wchar_t */
1670 for (c = p, wp = wbuff;
1671 c < (p + name_len) &&
1672 wp < (wbuff + sizeof(wbuff)/sizeof(*wbuff) - 1);
1674 *wp++ = (((255 & (int)c[0]) << 8) | (255 & (int)c[1]));
1678 #if 0 /* untested code, is it at all useful on Joliet? */
1679 /* trim trailing first version and dot from filename.
1681 * Remember we where in UTF-16BE land!
1682 * SEPARATOR 1 (.) and SEPARATOR 2 (;) are both
1683 * 16 bits big endian characters on Joliet.
1685 * TODO: sanitize filename?
1686 * Joliet allows any UCS-2 char except:
1687 * *, /, :, ;, ? and \.
1689 /* Chop off trailing ';1' from files. */
1690 if (*(wp-2) == ';' && *(wp-1) == '1') {
1695 /* Chop off trailing '.' from filenames. */
1700 /* store the result in the file name field. */
1701 archive_strappend_w_utf8(&file->name, wbuff);
1703 /* Chop off trailing ';1' from files. */
1704 if (name_len > 2 && p[name_len - 2] == ';' &&
1705 p[name_len - 1] == '1')
1707 /* Chop off trailing '.' from filenames. */
1708 if (name_len > 1 && p[name_len - 1] == '.')
1711 archive_strncpy(&file->name, (const char *)p, name_len);
1714 flags = isodirrec[DR_flags_offset];
1716 file->mode = AE_IFDIR | 0700;
1718 file->mode = AE_IFREG | 0400;
1720 file->multi_extent = 1;
1722 file->multi_extent = 0;
1724 * Use location for file number.
1725 * File number is treated as inode number to find out harlink
1726 * target. If Rockridge extensions is being used, file number
1727 * will be overwritten by FILE SERIAL NUMBER of RRIP "PX"
1729 * NOTE: Old mkisofs did not record that FILE SERIAL NUMBER
1732 if (file->size == 0 && location >= 0)
1733 /* If file->size is zero, its location points wrong place.
1734 * Dot not use it for file number.
1735 * When location has negative value, it can be used
1740 file->number = (int64_t)(uint32_t)location;
1742 /* Rockridge extensions overwrite information from above. */
1743 if (iso9660->opt_support_rockridge) {
1744 if (parent == NULL && rr_end - rr_start >= 7) {
1746 if (p[0] == 'S' && p[1] == 'P'
1747 && p[2] == 7 && p[3] == 1
1748 && p[4] == 0xBE && p[5] == 0xEF) {
1750 * SP extension stores the suspOffset
1751 * (Number of bytes to skip between
1752 * filename and SUSP records.)
1753 * It is mandatory by the SUSP standard
1756 * It allows SUSP to coexist with
1757 * non-SUSP uses of the System
1758 * Use Area by placing non-SUSP data
1761 * SP extension must be in the root
1762 * directory entry, disable all SUSP
1763 * processing if not found.
1765 iso9660->suspOffset = p[6];
1766 iso9660->seenSUSP = 1;
1770 if (iso9660->seenSUSP) {
1773 file->name_continues = 0;
1774 file->symlink_continues = 0;
1775 rr_start += iso9660->suspOffset;
1776 r = parse_rockridge(a, file, rr_start, rr_end);
1777 if (r != ARCHIVE_OK) {
1782 /* If there isn't SUSP, disable parsing
1783 * rock ridge extensions. */
1784 iso9660->opt_support_rockridge = 0;
1787 file->nlinks = 1;/* Reset nlink. we'll calculate it later. */
1788 /* Tell file's parent how many children that parent has. */
1789 if (parent != NULL && (flags & 0x02))
1792 if (iso9660->seenRockridge) {
1793 if (parent != NULL && parent->parent == NULL &&
1794 (flags & 0x02) && iso9660->rr_moved == NULL &&
1795 (strcmp(file->name.s, "rr_moved") == 0 ||
1796 strcmp(file->name.s, ".rr_moved") == 0)) {
1797 iso9660->rr_moved = file;
1799 file->rr_moved_has_re_only = 1;
1802 } else if (file->re) {
1804 * Sanity check: file's parent is rr_moved.
1806 if (parent == NULL || parent->rr_moved == 0) {
1807 archive_set_error(&a->archive,
1809 "Invalid Rockridge RE");
1813 * Sanity check: file does not have "CL" extension.
1815 if (file->cl_offset) {
1816 archive_set_error(&a->archive,
1818 "Invalid Rockridge RE and CL");
1822 * Sanity check: The file type must be a directory.
1824 if ((flags & 0x02) == 0) {
1825 archive_set_error(&a->archive,
1827 "Invalid Rockridge RE");
1830 } else if (parent != NULL && parent->rr_moved)
1831 file->rr_moved_has_re_only = 0;
1832 else if (parent != NULL && (flags & 0x02) &&
1833 (parent->re || parent->re_descendant))
1834 file->re_descendant = 1;
1835 if (file->cl_offset) {
1836 struct file_info *r;
1838 if (parent == NULL || parent->parent == NULL) {
1839 archive_set_error(&a->archive,
1841 "Invalid Rockridge CL");
1845 * Sanity check: The file type must be a regular file.
1847 if ((flags & 0x02) != 0) {
1848 archive_set_error(&a->archive,
1850 "Invalid Rockridge CL");
1854 /* Overwrite an offset and a number of this "CL" entry
1855 * to appear before other dirs. "+1" to those is to
1856 * make sure to appear after "RE" entry which this
1857 * "CL" entry should be connected with. */
1858 file->offset = file->number = file->cl_offset + 1;
1861 * Sanity check: cl_offset does not point at its
1862 * the parents or itself.
1864 for (r = parent; r; r = r->parent) {
1865 if (r->offset == file->cl_offset) {
1866 archive_set_error(&a->archive,
1868 "Invalid Rockridge CL");
1872 if (file->cl_offset == file->offset ||
1874 archive_set_error(&a->archive,
1876 "Invalid Rockridge CL");
1883 /* DEBUGGING: Warn about attributes I don't yet fully support. */
1884 if ((flags & ~0x02) != 0) {
1885 fprintf(stderr, "\n ** Unrecognized flag: ");
1886 dump_isodirrec(stderr, isodirrec);
1887 fprintf(stderr, "\n");
1888 } else if (toi(isodirrec + DR_volume_sequence_number_offset, 2) != 1) {
1889 fprintf(stderr, "\n ** Unrecognized sequence number: ");
1890 dump_isodirrec(stderr, isodirrec);
1891 fprintf(stderr, "\n");
1892 } else if (*(isodirrec + DR_file_unit_size_offset) != 0) {
1893 fprintf(stderr, "\n ** Unexpected file unit size: ");
1894 dump_isodirrec(stderr, isodirrec);
1895 fprintf(stderr, "\n");
1896 } else if (*(isodirrec + DR_interleave_offset) != 0) {
1897 fprintf(stderr, "\n ** Unexpected interleave: ");
1898 dump_isodirrec(stderr, isodirrec);
1899 fprintf(stderr, "\n");
1900 } else if (*(isodirrec + DR_ext_attr_length_offset) != 0) {
1901 fprintf(stderr, "\n ** Unexpected extended attribute length: ");
1902 dump_isodirrec(stderr, isodirrec);
1903 fprintf(stderr, "\n");
1906 register_file(iso9660, file);
1911 parse_rockridge(struct archive_read *a, struct file_info *file,
1912 const unsigned char *p, const unsigned char *end)
1914 struct iso9660 *iso9660;
1916 iso9660 = (struct iso9660 *)(a->format->data);
1918 while (p + 4 <= end /* Enough space for another entry. */
1919 && p[0] >= 'A' && p[0] <= 'Z' /* Sanity-check 1st char of name. */
1920 && p[1] >= 'A' && p[1] <= 'Z' /* Sanity-check 2nd char of name. */
1921 && p[2] >= 4 /* Sanity-check length. */
1922 && p + p[2] <= end) { /* Sanity-check length. */
1923 const unsigned char *data = p + 4;
1924 int data_length = p[2] - 4;
1928 * Yes, each 'if' here does test p[0] again.
1929 * Otherwise, the fall-through handling to catch
1930 * unsupported extensions doesn't work.
1934 if (p[0] == 'C' && p[1] == 'E') {
1935 if (version == 1 && data_length == 24) {
1937 * CE extension comprises:
1938 * 8 byte sector containing extension
1939 * 8 byte offset w/in above sector
1940 * 8 byte length of continuation
1943 archive_le32dec(data);
1945 archive_le32dec(data+8);
1947 archive_le32dec(data+16);
1948 if (register_CE(a, location, file)
1950 return (ARCHIVE_FATAL);
1954 if (p[0] == 'C' && p[1] == 'L') {
1955 if (version == 1 && data_length == 8) {
1956 file->cl_offset = (uint64_t)
1957 iso9660->logical_block_size *
1958 (uint64_t)archive_le32dec(data);
1959 iso9660->seenRockridge = 1;
1965 if (p[0] == 'N' && p[1] == 'M') {
1967 parse_rockridge_NM1(file,
1969 iso9660->seenRockridge = 1;
1975 if (p[0] == 'P' && p[1] == 'D') {
1977 * PD extension is padding;
1978 * contents are always ignored.
1982 if (p[0] == 'P' && p[1] == 'L') {
1984 * PL extension won't appear;
1985 * contents are always ignored.
1989 if (p[0] == 'P' && p[1] == 'N') {
1990 if (version == 1 && data_length == 16) {
1991 file->rdev = toi(data,4);
1993 file->rdev |= toi(data + 8, 4);
1994 iso9660->seenRockridge = 1;
1998 if (p[0] == 'P' && p[1] == 'X') {
2000 * PX extension comprises:
2002 * 8 bytes for nlinks,
2005 * 8 bytes for inode.
2008 if (data_length >= 8)
2011 if (data_length >= 16)
2014 if (data_length >= 24)
2016 = toi(data + 16, 4);
2017 if (data_length >= 32)
2019 = toi(data + 24, 4);
2020 if (data_length >= 40)
2022 = toi(data + 32, 4);
2023 iso9660->seenRockridge = 1;
2029 if (p[0] == 'R' && p[1] == 'E' && version == 1) {
2031 iso9660->seenRockridge = 1;
2034 if (p[0] == 'R' && p[1] == 'R' && version == 1) {
2036 * RR extension comprises:
2037 * one byte flag value
2038 * This extension is obsolete,
2039 * so contents are always ignored.
2045 if (p[0] == 'S' && p[1] == 'L') {
2047 parse_rockridge_SL1(file,
2049 iso9660->seenRockridge = 1;
2053 if (p[0] == 'S' && p[1] == 'T'
2054 && data_length == 0 && version == 1) {
2056 * ST extension marks end of this
2057 * block of SUSP entries.
2059 * It allows SUSP to coexist with
2060 * non-SUSP uses of the System
2061 * Use Area by placing non-SUSP data
2064 iso9660->seenSUSP = 0;
2065 iso9660->seenRockridge = 0;
2066 return (ARCHIVE_OK);
2069 if (p[0] == 'T' && p[1] == 'F') {
2071 parse_rockridge_TF1(file,
2073 iso9660->seenRockridge = 1;
2079 if (p[0] == 'Z' && p[1] == 'F') {
2081 parse_rockridge_ZF1(file,
2087 /* The FALLTHROUGHs above leave us here for
2088 * any unsupported extension. */
2096 return (ARCHIVE_OK);
2100 register_CE(struct archive_read *a, int32_t location,
2101 struct file_info *file)
2103 struct iso9660 *iso9660;
2104 struct read_ce_queue *heap;
2105 struct read_ce_req *p;
2106 uint64_t offset, parent_offset;
2109 iso9660 = (struct iso9660 *)(a->format->data);
2110 offset = ((uint64_t)location) * (uint64_t)iso9660->logical_block_size;
2111 if (((file->mode & AE_IFMT) == AE_IFREG &&
2112 offset >= file->offset) ||
2113 offset < iso9660->current_position) {
2114 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
2115 "Invalid location in SUSP \"CE\" extension");
2116 return (ARCHIVE_FATAL);
2119 /* Expand our CE list as necessary. */
2120 heap = &(iso9660->read_ce_req);
2121 if (heap->cnt >= heap->allocated) {
2124 if (heap->allocated < 16)
2127 new_size = heap->allocated * 2;
2128 /* Overflow might keep us from growing the list. */
2129 if (new_size <= heap->allocated)
2130 __archive_errx(1, "Out of memory");
2131 p = malloc(new_size * sizeof(p[0]));
2133 __archive_errx(1, "Out of memory");
2134 if (heap->reqs != NULL) {
2135 memcpy(p, heap->reqs, heap->cnt * sizeof(*p));
2139 heap->allocated = new_size;
2143 * Start with hole at end, walk it up tree to find insertion point.
2147 parent = (hole - 1)/2;
2148 parent_offset = heap->reqs[parent].offset;
2149 if (offset >= parent_offset) {
2150 heap->reqs[hole].offset = offset;
2151 heap->reqs[hole].file = file;
2152 return (ARCHIVE_OK);
2154 // Move parent into hole <==> move hole up tree.
2155 heap->reqs[hole] = heap->reqs[parent];
2158 heap->reqs[0].offset = offset;
2159 heap->reqs[0].file = file;
2160 return (ARCHIVE_OK);
2164 next_CE(struct read_ce_queue *heap)
2166 uint64_t a_offset, b_offset, c_offset;
2168 struct read_ce_req tmp;
2174 * Move the last item in the heap to the root of the tree
2176 heap->reqs[0] = heap->reqs[--(heap->cnt)];
2179 * Rebalance the heap.
2181 a = 0; // Starting element and its offset
2182 a_offset = heap->reqs[a].offset;
2184 b = a + a + 1; // First child
2187 b_offset = heap->reqs[b].offset;
2188 c = b + 1; // Use second child if it is smaller.
2189 if (c < heap->cnt) {
2190 c_offset = heap->reqs[c].offset;
2191 if (c_offset < b_offset) {
2193 b_offset = c_offset;
2196 if (a_offset <= b_offset)
2198 tmp = heap->reqs[a];
2199 heap->reqs[a] = heap->reqs[b];
2200 heap->reqs[b] = tmp;
2207 read_CE(struct archive_read *a, struct iso9660 *iso9660)
2209 struct read_ce_queue *heap;
2210 const unsigned char *b, *p, *end;
2211 struct file_info *file;
2215 /* Read data which RRIP "CE" extension points. */
2216 heap = &(iso9660->read_ce_req);
2217 step = iso9660->logical_block_size;
2219 heap->reqs[0].offset == iso9660->current_position) {
2220 b = __archive_read_ahead(a, step, NULL);
2222 archive_set_error(&a->archive,
2224 "Failed to read full block when scanning "
2225 "ISO9660 directory list");
2226 return (ARCHIVE_FATAL);
2229 file = heap->reqs[0].file;
2230 p = b + file->ce_offset;
2231 end = p + file->ce_size;
2233 r = parse_rockridge(a, file, p, end);
2234 if (r != ARCHIVE_OK)
2235 return (ARCHIVE_FATAL);
2236 } while (heap->cnt &&
2237 heap->reqs[0].offset == iso9660->current_position);
2238 /* NOTE: Do not move this consume's code to fron of
2239 * do-while loop. Registration of nested CE extension
2240 * might cause error because of current position. */
2241 __archive_read_consume(a, step);
2242 iso9660->current_position += step;
2244 return (ARCHIVE_OK);
2248 parse_rockridge_NM1(struct file_info *file,
2249 const unsigned char *data, int data_length)
2251 if (!file->name_continues)
2252 archive_string_empty(&file->name);
2253 file->name_continues = 0;
2254 if (data_length < 1)
2257 * NM version 1 extension comprises:
2258 * 1 byte flag, value is one of:
2259 * = 0: remainder is name
2260 * = 1: remainder is name, next NM entry continues name
2263 * = 32: Implementation specific
2264 * All other values are reserved.
2268 if (data_length < 2)
2270 archive_strncat(&file->name, (const char *)data + 1, data_length - 1);
2273 if (data_length < 2)
2275 archive_strncat(&file->name, (const char *)data + 1, data_length - 1);
2276 file->name_continues = 1;
2279 archive_strcat(&file->name, ".");
2282 archive_strcat(&file->name, "..");
2291 parse_rockridge_TF1(struct file_info *file, const unsigned char *data,
2296 * TF extension comprises:
2298 * create time (optional)
2299 * modify time (optional)
2300 * access time (optional)
2301 * attribute time (optional)
2302 * Time format and presence of fields
2303 * is controlled by flag bits.
2305 if (data_length < 1)
2311 /* Use 17-byte time format. */
2312 if ((flag & 1) && data_length >= 17) {
2314 file->birthtime_is_set = 1;
2315 file->birthtime = isodate17(data);
2319 if ((flag & 2) && data_length >= 17) {
2321 file->mtime = isodate17(data);
2325 if ((flag & 4) && data_length >= 17) {
2327 file->atime = isodate17(data);
2331 if ((flag & 8) && data_length >= 17) {
2332 /* Attribute change time. */
2333 file->ctime = isodate17(data);
2336 /* Use 7-byte time format. */
2337 if ((flag & 1) && data_length >= 7) {
2339 file->birthtime_is_set = 1;
2340 file->birthtime = isodate7(data);
2344 if ((flag & 2) && data_length >= 7) {
2346 file->mtime = isodate7(data);
2350 if ((flag & 4) && data_length >= 7) {
2352 file->atime = isodate7(data);
2356 if ((flag & 8) && data_length >= 7) {
2357 /* Attribute change time. */
2358 file->ctime = isodate7(data);
2364 parse_rockridge_SL1(struct file_info *file, const unsigned char *data,
2367 const char *separator = "";
2369 if (!file->symlink_continues || file->symlink.length < 1)
2370 archive_string_empty(&file->symlink);
2371 else if (!file->symlink_continues &&
2372 file->symlink.s[file->symlink.length - 1] != '/')
2374 file->symlink_continues = 0;
2377 * Defined flag values:
2378 * 0: This is the last SL record for this symbolic link
2379 * 1: this symbolic link field continues in next SL entry
2380 * All other values are reserved.
2382 if (data_length < 1)
2388 file->symlink_continues = 1;
2393 ++data; /* Skip flag byte. */
2397 * SL extension body stores "components".
2398 * Basically, this is a complicated way of storing
2399 * a POSIX path. It also interferes with using
2400 * symlinks for storing non-path data. <sigh>
2402 * Each component is 2 bytes (flag and length)
2403 * possibly followed by name data.
2405 while (data_length >= 2) {
2406 unsigned char flag = *data++;
2407 unsigned char nlen = *data++;
2410 archive_strcat(&file->symlink, separator);
2414 case 0: /* Usual case, this is text. */
2415 if (data_length < nlen)
2417 archive_strncat(&file->symlink,
2418 (const char *)data, nlen);
2420 case 0x01: /* Text continues in next component. */
2421 if (data_length < nlen)
2423 archive_strncat(&file->symlink,
2424 (const char *)data, nlen);
2427 case 0x02: /* Current dir. */
2428 archive_strcat(&file->symlink, ".");
2430 case 0x04: /* Parent dir. */
2431 archive_strcat(&file->symlink, "..");
2433 case 0x08: /* Root of filesystem. */
2434 archive_strcat(&file->symlink, "/");
2437 case 0x10: /* Undefined (historically "volume root" */
2438 archive_string_empty(&file->symlink);
2439 archive_strcat(&file->symlink, "ROOT");
2441 case 0x20: /* Undefined (historically "hostname") */
2442 archive_strcat(&file->symlink, "hostname");
2445 /* TODO: issue a warning ? */
2449 data_length -= nlen;
2454 parse_rockridge_ZF1(struct file_info *file, const unsigned char *data,
2458 if (data[0] == 0x70 && data[1] == 0x7a && data_length == 12) {
2461 file->pz_log2_bs = data[3];
2462 file->pz_uncompressed_size = archive_le32dec(&data[4]);
2467 register_file(struct iso9660 *iso9660, struct file_info *file)
2470 file->use_next = iso9660->use_files;
2471 iso9660->use_files = file;
2475 release_files(struct iso9660 *iso9660)
2477 struct content *con, *connext;
2478 struct file_info *file;
2480 file = iso9660->use_files;
2481 while (file != NULL) {
2482 struct file_info *next = file->use_next;
2484 archive_string_free(&file->name);
2485 archive_string_free(&file->symlink);
2486 con = file->contents.first;
2487 while (con != NULL) {
2488 connext = con->next;
2498 next_entry_seek(struct archive_read *a, struct iso9660 *iso9660,
2499 struct file_info **pfile)
2501 struct file_info *file;
2504 r = next_cache_entry(a, iso9660, pfile);
2505 if (r != ARCHIVE_OK)
2509 /* Don't waste time seeking for zero-length bodies. */
2510 if (file->size == 0)
2511 file->offset = iso9660->current_position;
2513 /* Seek forward to the start of the entry. */
2514 if (iso9660->current_position < file->offset) {
2517 step = file->offset - iso9660->current_position;
2518 step = __archive_read_skip(a, step);
2521 iso9660->current_position = file->offset;
2524 /* We found body of file; handle it now. */
2525 return (ARCHIVE_OK);
2529 next_cache_entry(struct archive_read *a, struct iso9660 *iso9660,
2530 struct file_info **pfile)
2532 struct file_info *file;
2534 struct file_info *first;
2535 struct file_info **last;
2540 file = cache_get_entry(iso9660);
2543 return (ARCHIVE_OK);
2547 struct file_info *re, *d;
2549 *pfile = file = next_entry(iso9660);
2552 * If directory entries all which are descendant of
2553 * rr_moved are stil remaning, expose their.
2555 if (iso9660->re_files.first != NULL &&
2556 iso9660->rr_moved != NULL &&
2557 iso9660->rr_moved->rr_moved_has_re_only)
2558 /* Expose "rr_moved" entry. */
2559 cache_add_entry(iso9660, iso9660->rr_moved);
2560 while ((re = re_get_entry(iso9660)) != NULL) {
2561 /* Expose its descendant dirs. */
2562 while ((d = rede_get_entry(re)) != NULL)
2563 cache_add_entry(iso9660, d);
2565 if (iso9660->cache_files.first != NULL)
2566 return (next_cache_entry(a, iso9660, pfile));
2567 return (ARCHIVE_EOF);
2570 if (file->cl_offset) {
2571 struct file_info *first_re = NULL;
2575 * Find "RE" dir for the current file, which
2578 while ((re = re_get_entry(iso9660))
2580 if (first_re == NULL)
2582 if (re->offset == file->cl_offset) {
2583 re->parent->subdirs--;
2584 re->parent = file->parent;
2586 if (re->parent->re_descendant) {
2588 re->re_descendant = 1;
2589 if (rede_add_entry(re) < 0)
2591 /* Move a list of descendants
2592 * to a new ancestor. */
2593 while ((d = rede_get_entry(
2595 if (rede_add_entry(d)
2600 /* Replace the current file
2603 /* Expose its descendant */
2604 while ((d = rede_get_entry(
2610 re_add_entry(iso9660, re);
2614 * Do not expose this at this time
2615 * because we have not gotten its full-path
2620 } else if ((file->mode & AE_IFMT) == AE_IFDIR) {
2623 /* Read file entries in this dir. */
2624 r = read_children(a, file);
2625 if (r != ARCHIVE_OK)
2629 * Handle a special dir of Rockridge extensions,
2632 if (file->rr_moved) {
2634 * If this has only the subdirectories which
2635 * have "RE" flags, do not expose at this time.
2637 if (file->rr_moved_has_re_only)
2639 /* Otherwise expose "rr_moved" entry. */
2640 } else if (file->re) {
2642 * Do not expose this at this time
2643 * because we have not gotten its full-path
2646 re_add_entry(iso9660, file);
2648 } else if (file->re_descendant) {
2650 * If the top level "RE" entry of this entry
2651 * is not exposed, we, accordingly, should not
2652 * expose this entry at this time because
2653 * we cannot make its proper full-path name.
2655 if (rede_add_entry(file) == 0)
2657 /* Otherwise we can expose this entry because
2658 * it seems its top level "RE" has already been
2665 if ((file->mode & AE_IFMT) != AE_IFREG || file->number == -1)
2666 return (ARCHIVE_OK);
2669 number = file->number;
2670 iso9660->cache_files.first = NULL;
2671 iso9660->cache_files.last = &(iso9660->cache_files.first);
2672 empty_files.first = NULL;
2673 empty_files.last = &empty_files.first;
2674 /* Collect files which has the same file serial number.
2675 * Peek pending_files so that file which number is different
2676 * is not put bak. */
2677 while (iso9660->pending_files.used > 0 &&
2678 (iso9660->pending_files.files[0]->number == -1 ||
2679 iso9660->pending_files.files[0]->number == number)) {
2680 if (file->number == -1) {
2681 /* This file has the same offset
2682 * but it's wrong offset which empty files
2683 * and symlink files have.
2684 * NOTE: This wrong offse was recorded by
2685 * old mkisofs utility. If ISO images is
2686 * created by latest mkisofs, this does not
2690 *empty_files.last = file;
2691 empty_files.last = &(file->next);
2694 cache_add_entry(iso9660, file);
2696 file = next_entry(iso9660);
2701 return ((file == NULL)?ARCHIVE_EOF:ARCHIVE_OK);
2703 if (file->number == -1) {
2705 *empty_files.last = file;
2706 empty_files.last = &(file->next);
2709 cache_add_entry(iso9660, file);
2713 /* The count is the same as number of hardlink,
2714 * so much so that each nlinks of files in cache_file
2715 * is overwritten by value of the count.
2717 for (file = iso9660->cache_files.first;
2718 file != NULL; file = file->next)
2719 file->nlinks = count;
2721 /* If there are empty files, that files are added
2722 * to the tail of the cache_files. */
2723 if (empty_files.first != NULL) {
2724 *iso9660->cache_files.last = empty_files.first;
2725 iso9660->cache_files.last = empty_files.last;
2727 *pfile = cache_get_entry(iso9660);
2728 return ((*pfile == NULL)?ARCHIVE_EOF:ARCHIVE_OK);
2731 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
2732 "Failed to connect 'CL' pointer to 'RE' rr_moved pointer of"
2733 "Rockridge extensions");
2734 return (ARCHIVE_FATAL);
2738 re_add_entry(struct iso9660 *iso9660, struct file_info *file)
2740 file->re_next = NULL;
2741 *iso9660->re_files.last = file;
2742 iso9660->re_files.last = &(file->re_next);
2745 static inline struct file_info *
2746 re_get_entry(struct iso9660 *iso9660)
2748 struct file_info *file;
2750 if ((file = iso9660->re_files.first) != NULL) {
2751 iso9660->re_files.first = file->re_next;
2752 if (iso9660->re_files.first == NULL)
2753 iso9660->re_files.last =
2754 &(iso9660->re_files.first);
2760 rede_add_entry(struct file_info *file)
2762 struct file_info *re;
2768 while (re != NULL && !re->re)
2773 file->re_next = NULL;
2774 *re->rede_files.last = file;
2775 re->rede_files.last = &(file->re_next);
2779 static inline struct file_info *
2780 rede_get_entry(struct file_info *re)
2782 struct file_info *file;
2784 if ((file = re->rede_files.first) != NULL) {
2785 re->rede_files.first = file->re_next;
2786 if (re->rede_files.first == NULL)
2787 re->rede_files.last =
2788 &(re->rede_files.first);
2794 cache_add_entry(struct iso9660 *iso9660, struct file_info *file)
2797 *iso9660->cache_files.last = file;
2798 iso9660->cache_files.last = &(file->next);
2801 static inline struct file_info *
2802 cache_get_entry(struct iso9660 *iso9660)
2804 struct file_info *file;
2806 if ((file = iso9660->cache_files.first) != NULL) {
2807 iso9660->cache_files.first = file->next;
2808 if (iso9660->cache_files.first == NULL)
2809 iso9660->cache_files.last = &(iso9660->cache_files.first);
2815 heap_add_entry(struct heap_queue *heap, struct file_info *file, uint64_t key)
2817 uint64_t file_key, parent_key;
2820 /* Expand our pending files list as necessary. */
2821 if (heap->used >= heap->allocated) {
2822 struct file_info **new_pending_files;
2823 int new_size = heap->allocated * 2;
2825 if (heap->allocated < 1024)
2827 /* Overflow might keep us from growing the list. */
2828 if (new_size <= heap->allocated)
2829 __archive_errx(1, "Out of memory");
2830 new_pending_files = (struct file_info **)
2831 malloc(new_size * sizeof(new_pending_files[0]));
2832 if (new_pending_files == NULL)
2833 __archive_errx(1, "Out of memory");
2834 memcpy(new_pending_files, heap->files,
2835 heap->allocated * sizeof(new_pending_files[0]));
2836 if (heap->files != NULL)
2838 heap->files = new_pending_files;
2839 heap->allocated = new_size;
2842 file_key = file->key = key;
2845 * Start with hole at end, walk it up tree to find insertion point.
2847 hole = heap->used++;
2849 parent = (hole - 1)/2;
2850 parent_key = heap->files[parent]->key;
2851 if (file_key >= parent_key) {
2852 heap->files[hole] = file;
2855 // Move parent into hole <==> move hole up tree.
2856 heap->files[hole] = heap->files[parent];
2859 heap->files[0] = file;
2862 static struct file_info *
2863 heap_get_entry(struct heap_queue *heap)
2865 uint64_t a_key, b_key, c_key;
2867 struct file_info *r, *tmp;
2873 * The first file in the list is the earliest; we'll return this.
2878 * Move the last item in the heap to the root of the tree
2880 heap->files[0] = heap->files[--(heap->used)];
2883 * Rebalance the heap.
2885 a = 0; // Starting element and its heap key
2886 a_key = heap->files[a]->key;
2888 b = a + a + 1; // First child
2889 if (b >= heap->used)
2891 b_key = heap->files[b]->key;
2892 c = b + 1; // Use second child if it is smaller.
2893 if (c < heap->used) {
2894 c_key = heap->files[c]->key;
2895 if (c_key < b_key) {
2902 tmp = heap->files[a];
2903 heap->files[a] = heap->files[b];
2904 heap->files[b] = tmp;
2910 toi(const void *p, int n)
2912 const unsigned char *v = (const unsigned char *)p;
2914 return v[0] + 256 * toi(v + 1, n - 1);
2921 isodate7(const unsigned char *v)
2925 memset(&tm, 0, sizeof(tm));
2927 tm.tm_mon = v[1] - 1;
2932 /* v[6] is the signed timezone offset, in 1/4-hour increments. */
2933 offset = ((const signed char *)v)[6];
2934 if (offset > -48 && offset < 52) {
2935 tm.tm_hour -= offset / 4;
2936 tm.tm_min -= (offset % 4) * 15;
2938 return (time_from_tm(&tm));
2942 isodate17(const unsigned char *v)
2946 memset(&tm, 0, sizeof(tm));
2947 tm.tm_year = (v[0] - '0') * 1000 + (v[1] - '0') * 100
2948 + (v[2] - '0') * 10 + (v[3] - '0')
2950 tm.tm_mon = (v[4] - '0') * 10 + (v[5] - '0');
2951 tm.tm_mday = (v[6] - '0') * 10 + (v[7] - '0');
2952 tm.tm_hour = (v[8] - '0') * 10 + (v[9] - '0');
2953 tm.tm_min = (v[10] - '0') * 10 + (v[11] - '0');
2954 tm.tm_sec = (v[12] - '0') * 10 + (v[13] - '0');
2955 /* v[16] is the signed timezone offset, in 1/4-hour increments. */
2956 offset = ((const signed char *)v)[16];
2957 if (offset > -48 && offset < 52) {
2958 tm.tm_hour -= offset / 4;
2959 tm.tm_min -= (offset % 4) * 15;
2961 return (time_from_tm(&tm));
2965 time_from_tm(struct tm *t)
2968 /* Use platform timegm() if available. */
2971 /* Else use direct calculation using POSIX assumptions. */
2972 /* First, fix up tm_yday based on the year/month/day. */
2974 /* Then we can compute timegm() from first principles. */
2975 return (t->tm_sec + t->tm_min * 60 + t->tm_hour * 3600
2976 + t->tm_yday * 86400 + (t->tm_year - 70) * 31536000
2977 + ((t->tm_year - 69) / 4) * 86400 -
2978 ((t->tm_year - 1) / 100) * 86400
2979 + ((t->tm_year + 299) / 400) * 86400);
2984 build_pathname(struct archive_string *as, struct file_info *file)
2986 if (file->parent != NULL && archive_strlen(&file->parent->name) > 0) {
2987 build_pathname(as, file->parent);
2988 archive_strcat(as, "/");
2990 if (archive_strlen(&file->name) == 0)
2991 archive_strcat(as, ".");
2993 archive_string_concat(as, &file->name);
2999 dump_isodirrec(FILE *out, const unsigned char *isodirrec)
3001 fprintf(out, " l %d,",
3002 toi(isodirrec + DR_length_offset, DR_length_size));
3003 fprintf(out, " a %d,",
3004 toi(isodirrec + DR_ext_attr_length_offset, DR_ext_attr_length_size));
3005 fprintf(out, " ext 0x%x,",
3006 toi(isodirrec + DR_extent_offset, DR_extent_size));
3007 fprintf(out, " s %d,",
3008 toi(isodirrec + DR_size_offset, DR_extent_size));
3009 fprintf(out, " f 0x%02x,",
3010 toi(isodirrec + DR_flags_offset, DR_flags_size));
3011 fprintf(out, " u %d,",
3012 toi(isodirrec + DR_file_unit_size_offset, DR_file_unit_size_size));
3013 fprintf(out, " ilv %d,",
3014 toi(isodirrec + DR_interleave_offset, DR_interleave_size));
3015 fprintf(out, " seq %d,",
3016 toi(isodirrec + DR_volume_sequence_number_offset, DR_volume_sequence_number_size));
3017 fprintf(out, " nl %d:",
3018 toi(isodirrec + DR_name_len_offset, DR_name_len_size));
3019 fprintf(out, " `%.*s'",
3020 toi(isodirrec + DR_name_len_offset, DR_name_len_size), isodirrec + DR_name_offset);