1 /*-
2  * Copyright (c) 2003-2007 Tim Kientzle
3  * Copyright (c) 2009 Andreas Henriksson <andreas@fatal.se>
4  * Copyright (c) 2009-2012 Michihiro NAKAJIMA
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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.
26  */
27 
28 #include "archive_platform.h"
29 __FBSDID("$FreeBSD: head/lib/libarchive/archive_read_support_format_iso9660.c 201246 2009-12-30 05:30:35Z kientzle $");
30 
31 #ifdef HAVE_ERRNO_H
32 #include <errno.h>
33 #endif
34 /* #include <stdint.h> */ /* See archive_platform.h */
35 #include <stdio.h>
36 #ifdef HAVE_STDLIB_H
37 #include <stdlib.h>
38 #endif
39 #ifdef HAVE_STRING_H
40 #include <string.h>
41 #endif
42 #include <time.h>
43 #ifdef HAVE_ZLIB_H
44 #include <zlib.h>
45 #endif
46 
47 #include "archive.h"
48 #include "archive_endian.h"
49 #include "archive_entry.h"
50 #include "archive_entry_locale.h"
51 #include "archive_private.h"
52 #include "archive_read_private.h"
53 #include "archive_string.h"
54 
55 /*
56  * An overview of ISO 9660 format:
57  *
58  * Each disk is laid out as follows:
59  *   * 32k reserved for private use
60  *   * Volume descriptor table.  Each volume descriptor
61  *     is 2k and specifies basic format information.
62  *     The "Primary Volume Descriptor" (PVD) is defined by the
63  *     standard and should always be present; other volume
64  *     descriptors include various vendor-specific extensions.
65  *   * Files and directories.  Each file/dir is specified by
66  *     an "extent" (starting sector and length in bytes).
67  *     Dirs are just files with directory records packed one
68  *     after another.  The PVD contains a single dir entry
69  *     specifying the location of the root directory.  Everything
70  *     else follows from there.
71  *
72  * This module works by first reading the volume descriptors, then
73  * building a list of directory entries, sorted by starting
74  * sector.  At each step, I look for the earliest dir entry that
75  * hasn't yet been read, seek forward to that location and read
76  * that entry.  If it's a dir, I slurp in the new dir entries and
77  * add them to the heap; if it's a regular file, I return the
78  * corresponding archive_entry and wait for the client to request
79  * the file body.  This strategy allows us to read most compliant
80  * CDs with a single pass through the data, as required by libarchive.
81  */
82 #define	LOGICAL_BLOCK_SIZE	2048
83 #define	SYSTEM_AREA_BLOCK	16
84 
85 /* Structure of on-disk primary volume descriptor. */
86 #define PVD_type_offset 0
87 #define PVD_type_size 1
88 #define PVD_id_offset (PVD_type_offset + PVD_type_size)
89 #define PVD_id_size 5
90 #define PVD_version_offset (PVD_id_offset + PVD_id_size)
91 #define PVD_version_size 1
92 #define PVD_reserved1_offset (PVD_version_offset + PVD_version_size)
93 #define PVD_reserved1_size 1
94 #define PVD_system_id_offset (PVD_reserved1_offset + PVD_reserved1_size)
95 #define PVD_system_id_size 32
96 #define PVD_volume_id_offset (PVD_system_id_offset + PVD_system_id_size)
97 #define PVD_volume_id_size 32
98 #define PVD_reserved2_offset (PVD_volume_id_offset + PVD_volume_id_size)
99 #define PVD_reserved2_size 8
100 #define PVD_volume_space_size_offset (PVD_reserved2_offset + PVD_reserved2_size)
101 #define PVD_volume_space_size_size 8
102 #define PVD_reserved3_offset (PVD_volume_space_size_offset + PVD_volume_space_size_size)
103 #define PVD_reserved3_size 32
104 #define PVD_volume_set_size_offset (PVD_reserved3_offset + PVD_reserved3_size)
105 #define PVD_volume_set_size_size 4
106 #define PVD_volume_sequence_number_offset (PVD_volume_set_size_offset + PVD_volume_set_size_size)
107 #define PVD_volume_sequence_number_size 4
108 #define PVD_logical_block_size_offset (PVD_volume_sequence_number_offset + PVD_volume_sequence_number_size)
109 #define PVD_logical_block_size_size 4
110 #define PVD_path_table_size_offset (PVD_logical_block_size_offset + PVD_logical_block_size_size)
111 #define PVD_path_table_size_size 8
112 #define PVD_type_1_path_table_offset (PVD_path_table_size_offset + PVD_path_table_size_size)
113 #define PVD_type_1_path_table_size 4
114 #define PVD_opt_type_1_path_table_offset (PVD_type_1_path_table_offset + PVD_type_1_path_table_size)
115 #define PVD_opt_type_1_path_table_size 4
116 #define PVD_type_m_path_table_offset (PVD_opt_type_1_path_table_offset + PVD_opt_type_1_path_table_size)
117 #define PVD_type_m_path_table_size 4
118 #define PVD_opt_type_m_path_table_offset (PVD_type_m_path_table_offset + PVD_type_m_path_table_size)
119 #define PVD_opt_type_m_path_table_size 4
120 #define PVD_root_directory_record_offset (PVD_opt_type_m_path_table_offset + PVD_opt_type_m_path_table_size)
121 #define PVD_root_directory_record_size 34
122 #define PVD_volume_set_id_offset (PVD_root_directory_record_offset + PVD_root_directory_record_size)
123 #define PVD_volume_set_id_size 128
124 #define PVD_publisher_id_offset (PVD_volume_set_id_offset + PVD_volume_set_id_size)
125 #define PVD_publisher_id_size 128
126 #define PVD_preparer_id_offset (PVD_publisher_id_offset + PVD_publisher_id_size)
127 #define PVD_preparer_id_size 128
128 #define PVD_application_id_offset (PVD_preparer_id_offset + PVD_preparer_id_size)
129 #define PVD_application_id_size 128
130 #define PVD_copyright_file_id_offset (PVD_application_id_offset + PVD_application_id_size)
131 #define PVD_copyright_file_id_size 37
132 #define PVD_abstract_file_id_offset (PVD_copyright_file_id_offset + PVD_copyright_file_id_size)
133 #define PVD_abstract_file_id_size 37
134 #define PVD_bibliographic_file_id_offset (PVD_abstract_file_id_offset + PVD_abstract_file_id_size)
135 #define PVD_bibliographic_file_id_size 37
136 #define PVD_creation_date_offset (PVD_bibliographic_file_id_offset + PVD_bibliographic_file_id_size)
137 #define PVD_creation_date_size 17
138 #define PVD_modification_date_offset (PVD_creation_date_offset + PVD_creation_date_size)
139 #define PVD_modification_date_size 17
140 #define PVD_expiration_date_offset (PVD_modification_date_offset + PVD_modification_date_size)
141 #define PVD_expiration_date_size 17
142 #define PVD_effective_date_offset (PVD_expiration_date_offset + PVD_expiration_date_size)
143 #define PVD_effective_date_size 17
144 #define PVD_file_structure_version_offset (PVD_effective_date_offset + PVD_effective_date_size)
145 #define PVD_file_structure_version_size 1
146 #define PVD_reserved4_offset (PVD_file_structure_version_offset + PVD_file_structure_version_size)
147 #define PVD_reserved4_size 1
148 #define PVD_application_data_offset (PVD_reserved4_offset + PVD_reserved4_size)
149 #define PVD_application_data_size 512
150 #define PVD_reserved5_offset (PVD_application_data_offset + PVD_application_data_size)
151 #define PVD_reserved5_size (2048 - PVD_reserved5_offset)
152 
153 /* TODO: It would make future maintenance easier to just hardcode the
154  * above values.  In particular, ECMA119 states the offsets as part of
155  * the standard.  That would eliminate the need for the following check.*/
156 #if PVD_reserved5_offset != 1395
157 #error PVD offset and size definitions are wrong.
158 #endif
159 
160 
161 /* Structure of optional on-disk supplementary volume descriptor. */
162 #define SVD_type_offset 0
163 #define SVD_type_size 1
164 #define SVD_id_offset (SVD_type_offset + SVD_type_size)
165 #define SVD_id_size 5
166 #define SVD_version_offset (SVD_id_offset + SVD_id_size)
167 #define SVD_version_size 1
168 /* ... */
169 #define SVD_reserved1_offset	72
170 #define SVD_reserved1_size	8
171 #define SVD_volume_space_size_offset 80
172 #define SVD_volume_space_size_size 8
173 #define SVD_escape_sequences_offset (SVD_volume_space_size_offset + SVD_volume_space_size_size)
174 #define SVD_escape_sequences_size 32
175 /* ... */
176 #define SVD_logical_block_size_offset 128
177 #define SVD_logical_block_size_size 4
178 #define SVD_type_L_path_table_offset 140
179 #define SVD_type_M_path_table_offset 148
180 /* ... */
181 #define SVD_root_directory_record_offset 156
182 #define SVD_root_directory_record_size 34
183 #define SVD_file_structure_version_offset 881
184 #define SVD_reserved2_offset	882
185 #define SVD_reserved2_size	1
186 #define SVD_reserved3_offset	1395
187 #define SVD_reserved3_size	653
188 /* ... */
189 /* FIXME: validate correctness of last SVD entry offset. */
190 
191 /* Structure of an on-disk directory record. */
192 /* Note:  ISO9660 stores each multi-byte integer twice, once in
193  * each byte order.  The sizes here are the size of just one
194  * of the two integers.  (This is why the offset of a field isn't
195  * the same as the offset+size of the previous field.) */
196 #define DR_length_offset 0
197 #define DR_length_size 1
198 #define DR_ext_attr_length_offset 1
199 #define DR_ext_attr_length_size 1
200 #define DR_extent_offset 2
201 #define DR_extent_size 4
202 #define DR_size_offset 10
203 #define DR_size_size 4
204 #define DR_date_offset 18
205 #define DR_date_size 7
206 #define DR_flags_offset 25
207 #define DR_flags_size 1
208 #define DR_file_unit_size_offset 26
209 #define DR_file_unit_size_size 1
210 #define DR_interleave_offset 27
211 #define DR_interleave_size 1
212 #define DR_volume_sequence_number_offset 28
213 #define DR_volume_sequence_number_size 2
214 #define DR_name_len_offset 32
215 #define DR_name_len_size 1
216 #define DR_name_offset 33
217 
218 #ifdef HAVE_ZLIB_H
219 static const unsigned char zisofs_magic[8] = {
220 	0x37, 0xE4, 0x53, 0x96, 0xC9, 0xDB, 0xD6, 0x07
221 };
222 
223 struct zisofs {
224 	/* Set 1 if this file compressed by paged zlib */
225 	int		 pz;
226 	int		 pz_log2_bs; /* Log2 of block size */
227 	uint64_t	 pz_uncompressed_size;
228 
229 	int		 initialized;
230 	unsigned char	*uncompressed_buffer;
231 	size_t		 uncompressed_buffer_size;
232 
233 	uint32_t	 pz_offset;
234 	unsigned char	 header[16];
235 	size_t		 header_avail;
236 	int		 header_passed;
237 	unsigned char	*block_pointers;
238 	size_t		 block_pointers_alloc;
239 	size_t		 block_pointers_size;
240 	size_t		 block_pointers_avail;
241 	size_t		 block_off;
242 	uint32_t	 block_avail;
243 
244 	z_stream	 stream;
245 	int		 stream_valid;
246 };
247 #else
248 struct zisofs {
249 	/* Set 1 if this file compressed by paged zlib */
250 	int		 pz;
251 };
252 #endif
253 
254 struct content {
255 	uint64_t	 offset;/* Offset on disk.		*/
256 	uint64_t	 size;	/* File size in bytes.		*/
257 	struct content	*next;
258 };
259 
260 /* In-memory storage for a directory record. */
261 struct file_info {
262 	struct file_info	*use_next;
263 	struct file_info	*parent;
264 	struct file_info	*next;
265 	struct file_info	*re_next;
266 	int		 subdirs;
267 	uint64_t	 key;		/* Heap Key.			*/
268 	uint64_t	 offset;	/* Offset on disk.		*/
269 	uint64_t	 size;		/* File size in bytes.		*/
270 	uint32_t	 ce_offset;	/* Offset of CE.		*/
271 	uint32_t	 ce_size;	/* Size of CE.			*/
272 	char		 rr_moved;	/* Flag to rr_moved.		*/
273 	char		 rr_moved_has_re_only;
274 	char		 re;		/* Having RRIP "RE" extension.	*/
275 	char		 re_descendant;
276 	uint64_t	 cl_offset;	/* Having RRIP "CL" extension.	*/
277 	int		 birthtime_is_set;
278 	time_t		 birthtime;	/* File created time.		*/
279 	time_t		 mtime;		/* File last modified time.	*/
280 	time_t		 atime;		/* File last accessed time.	*/
281 	time_t		 ctime;		/* File attribute change time.	*/
282 	uint64_t	 rdev;		/* Device number.		*/
283 	mode_t		 mode;
284 	uid_t		 uid;
285 	gid_t		 gid;
286 	int64_t		 number;
287 	int		 nlinks;
288 	struct archive_string name; /* Pathname */
289 	unsigned char	*utf16be_name;
290 	size_t		 utf16be_bytes;
291 	char		 name_continues; /* Non-zero if name continues */
292 	struct archive_string symlink;
293 	char		 symlink_continues; /* Non-zero if link continues */
294 	/* Set 1 if this file compressed by paged zlib(zisofs) */
295 	int		 pz;
296 	int		 pz_log2_bs; /* Log2 of block size */
297 	uint64_t	 pz_uncompressed_size;
298 	/* Set 1 if this file is multi extent. */
299 	int		 multi_extent;
300 	struct {
301 		struct content	*first;
302 		struct content	**last;
303 	} contents;
304 	struct {
305 		struct file_info	*first;
306 		struct file_info	**last;
307 	} rede_files;
308 };
309 
310 struct heap_queue {
311 	struct file_info **files;
312 	int		 allocated;
313 	int		 used;
314 };
315 
316 struct iso9660 {
317 	int	magic;
318 #define ISO9660_MAGIC   0x96609660
319 
320 	int opt_support_joliet;
321 	int opt_support_rockridge;
322 
323 	struct archive_string pathname;
324 	char	seenRockridge;	/* Set true if RR extensions are used. */
325 	char	seenSUSP;	/* Set true if SUSP is beging used. */
326 	char	seenJoliet;
327 
328 	unsigned char	suspOffset;
329 	struct file_info *rr_moved;
330 	struct read_ce_queue {
331 		struct read_ce_req {
332 			uint64_t	 offset;/* Offset of CE on disk. */
333 			struct file_info *file;
334 		}		*reqs;
335 		int		 cnt;
336 		int		 allocated;
337 	}	read_ce_req;
338 
339 	int64_t		previous_number;
340 	struct archive_string previous_pathname;
341 
342 	struct file_info		*use_files;
343 	struct heap_queue		 pending_files;
344 	struct {
345 		struct file_info	*first;
346 		struct file_info	**last;
347 	}	cache_files;
348 	struct {
349 		struct file_info	*first;
350 		struct file_info	**last;
351 	}	re_files;
352 
353 	uint64_t current_position;
354 	ssize_t	logical_block_size;
355 	uint64_t volume_size; /* Total size of volume in bytes. */
356 	int32_t  volume_block;/* Total size of volume in logical blocks. */
357 
358 	struct vd {
359 		int		location;	/* Location of Extent.	*/
360 		uint32_t	size;
361 	} primary, joliet;
362 
363 	int64_t	entry_sparse_offset;
364 	int64_t	entry_bytes_remaining;
365 	size_t  entry_bytes_unconsumed;
366 	struct zisofs	 entry_zisofs;
367 	struct content	*entry_content;
368 	struct archive_string_conv *sconv_utf16be;
369 	/*
370 	 * Buffers for a full pathname in UTF-16BE in Joliet extensions.
371 	 */
372 #define UTF16_NAME_MAX	1024
373 	unsigned char *utf16be_path;
374 	size_t		 utf16be_path_len;
375 	unsigned char *utf16be_previous_path;
376 	size_t		 utf16be_previous_path_len;
377 	/* Null buufer used in bidder to improve its performance. */
378 	unsigned char	 null[2048];
379 };
380 
381 static int	archive_read_format_iso9660_bid(struct archive_read *, int);
382 static int	archive_read_format_iso9660_options(struct archive_read *,
383 		    const char *, const char *);
384 static int	archive_read_format_iso9660_cleanup(struct archive_read *);
385 static int	archive_read_format_iso9660_read_data(struct archive_read *,
386 		    const void **, size_t *, int64_t *);
387 static int	archive_read_format_iso9660_read_data_skip(struct archive_read *);
388 static int	archive_read_format_iso9660_read_header(struct archive_read *,
389 		    struct archive_entry *);
390 static const char *build_pathname(struct archive_string *, struct file_info *);
391 static int	build_pathname_utf16be(unsigned char *, size_t, size_t *,
392 		    struct file_info *);
393 #if DEBUG
394 static void	dump_isodirrec(FILE *, const unsigned char *isodirrec);
395 #endif
396 static time_t	time_from_tm(struct tm *);
397 static time_t	isodate17(const unsigned char *);
398 static time_t	isodate7(const unsigned char *);
399 static int	isBootRecord(struct iso9660 *, const unsigned char *);
400 static int	isVolumePartition(struct iso9660 *, const unsigned char *);
401 static int	isVDSetTerminator(struct iso9660 *, const unsigned char *);
402 static int	isJolietSVD(struct iso9660 *, const unsigned char *);
403 static int	isSVD(struct iso9660 *, const unsigned char *);
404 static int	isEVD(struct iso9660 *, const unsigned char *);
405 static int	isPVD(struct iso9660 *, const unsigned char *);
406 static int	next_cache_entry(struct archive_read *, struct iso9660 *,
407 		    struct file_info **);
408 static int	next_entry_seek(struct archive_read *, struct iso9660 *,
409 		    struct file_info **);
410 static struct file_info *
411 		parse_file_info(struct archive_read *a,
412 		    struct file_info *parent, const unsigned char *isodirrec);
413 static int	parse_rockridge(struct archive_read *a,
414 		    struct file_info *file, const unsigned char *start,
415 		    const unsigned char *end);
416 static int	register_CE(struct archive_read *a, int32_t location,
417 		    struct file_info *file);
418 static int	read_CE(struct archive_read *a, struct iso9660 *iso9660);
419 static void	parse_rockridge_NM1(struct file_info *,
420 		    const unsigned char *, int);
421 static void	parse_rockridge_SL1(struct file_info *,
422 		    const unsigned char *, int);
423 static void	parse_rockridge_TF1(struct file_info *,
424 		    const unsigned char *, int);
425 static void	parse_rockridge_ZF1(struct file_info *,
426 		    const unsigned char *, int);
427 static void	register_file(struct iso9660 *, struct file_info *);
428 static void	release_files(struct iso9660 *);
429 static unsigned	toi(const void *p, int n);
430 static inline void re_add_entry(struct iso9660 *, struct file_info *);
431 static inline struct file_info * re_get_entry(struct iso9660 *);
432 static inline int rede_add_entry(struct file_info *);
433 static inline struct file_info * rede_get_entry(struct file_info *);
434 static inline void cache_add_entry(struct iso9660 *iso9660,
435 		    struct file_info *file);
436 static inline struct file_info *cache_get_entry(struct iso9660 *iso9660);
437 static int	heap_add_entry(struct archive_read *a, struct heap_queue *heap,
438 		    struct file_info *file, uint64_t key);
439 static struct file_info *heap_get_entry(struct heap_queue *heap);
440 
441 #define add_entry(arch, iso9660, file)	\
442 	heap_add_entry(arch, &((iso9660)->pending_files), file, file->offset)
443 #define next_entry(iso9660)		\
444 	heap_get_entry(&((iso9660)->pending_files))
445 
446 int
447 archive_read_support_format_iso9660(struct archive *_a)
448 {
449 	struct archive_read *a = (struct archive_read *)_a;
450 	struct iso9660 *iso9660;
451 	int r;
452 
453 	archive_check_magic(_a, ARCHIVE_READ_MAGIC,
454 	    ARCHIVE_STATE_NEW, "archive_read_support_format_iso9660");
455 
456 	iso9660 = (struct iso9660 *)calloc(1, sizeof(*iso9660));
457 	if (iso9660 == NULL) {
458 		archive_set_error(&a->archive, ENOMEM,
459 		    "Can't allocate iso9660 data");
460 		return (ARCHIVE_FATAL);
461 	}
462 	iso9660->magic = ISO9660_MAGIC;
463 	iso9660->cache_files.first = NULL;
464 	iso9660->cache_files.last = &(iso9660->cache_files.first);
465 	iso9660->re_files.first = NULL;
466 	iso9660->re_files.last = &(iso9660->re_files.first);
467 	/* Enable to support Joliet extensions by default.	*/
468 	iso9660->opt_support_joliet = 1;
469 	/* Enable to support Rock Ridge extensions by default.	*/
470 	iso9660->opt_support_rockridge = 1;
471 
472 	r = __archive_read_register_format(a,
473 	    iso9660,
474 	    "iso9660",
475 	    archive_read_format_iso9660_bid,
476 	    archive_read_format_iso9660_options,
477 	    archive_read_format_iso9660_read_header,
478 	    archive_read_format_iso9660_read_data,
479 	    archive_read_format_iso9660_read_data_skip,
480 	    NULL,
481 	    archive_read_format_iso9660_cleanup);
482 
483 	if (r != ARCHIVE_OK) {
484 		free(iso9660);
485 		return (r);
486 	}
487 	return (ARCHIVE_OK);
488 }
489 
490 
491 static int
492 archive_read_format_iso9660_bid(struct archive_read *a, int best_bid)
493 {
494 	struct iso9660 *iso9660;
495 	ssize_t bytes_read;
496 	const unsigned char *p;
497 	int seenTerminator;
498 
499 	/* If there's already a better bid than we can ever
500 	   make, don't bother testing. */
501 	if (best_bid > 48)
502 		return (-1);
503 
504 	iso9660 = (struct iso9660 *)(a->format->data);
505 
506 	/*
507 	 * Skip the first 32k (reserved area) and get the first
508 	 * 8 sectors of the volume descriptor table.  Of course,
509 	 * if the I/O layer gives us more, we'll take it.
510 	 */
511 #define RESERVED_AREA	(SYSTEM_AREA_BLOCK * LOGICAL_BLOCK_SIZE)
512 	p = __archive_read_ahead(a,
513 	    RESERVED_AREA + 8 * LOGICAL_BLOCK_SIZE,
514 	    &bytes_read);
515 	if (p == NULL)
516 	    return (-1);
517 
518 	/* Skip the reserved area. */
519 	bytes_read -= RESERVED_AREA;
520 	p += RESERVED_AREA;
521 
522 	/* Check each volume descriptor. */
523 	seenTerminator = 0;
524 	for (; bytes_read > LOGICAL_BLOCK_SIZE;
525 	    bytes_read -= LOGICAL_BLOCK_SIZE, p += LOGICAL_BLOCK_SIZE) {
526 		/* Do not handle undefined Volume Descriptor Type. */
527 		if (p[0] >= 4 && p[0] <= 254)
528 			return (0);
529 		/* Standard Identifier must be "CD001" */
530 		if (memcmp(p + 1, "CD001", 5) != 0)
531 			return (0);
532 		if (isPVD(iso9660, p))
533 			continue;
534 		if (!iso9660->joliet.location) {
535 			if (isJolietSVD(iso9660, p))
536 				continue;
537 		}
538 		if (isBootRecord(iso9660, p))
539 			continue;
540 		if (isEVD(iso9660, p))
541 			continue;
542 		if (isSVD(iso9660, p))
543 			continue;
544 		if (isVolumePartition(iso9660, p))
545 			continue;
546 		if (isVDSetTerminator(iso9660, p)) {
547 			seenTerminator = 1;
548 			break;
549 		}
550 		return (0);
551 	}
552 	/*
553 	 * ISO 9660 format must have Primary Volume Descriptor and
554 	 * Volume Descriptor Set Terminator.
555 	 */
556 	if (seenTerminator && iso9660->primary.location > 16)
557 		return (48);
558 
559 	/* We didn't find a valid PVD; return a bid of zero. */
560 	return (0);
561 }
562 
563 static int
564 archive_read_format_iso9660_options(struct archive_read *a,
565 		const char *key, const char *val)
566 {
567 	struct iso9660 *iso9660;
568 
569 	iso9660 = (struct iso9660 *)(a->format->data);
570 
571 	if (strcmp(key, "joliet") == 0) {
572 		if (val == NULL || strcmp(val, "off") == 0 ||
573 				strcmp(val, "ignore") == 0 ||
574 				strcmp(val, "disable") == 0 ||
575 				strcmp(val, "0") == 0)
576 			iso9660->opt_support_joliet = 0;
577 		else
578 			iso9660->opt_support_joliet = 1;
579 		return (ARCHIVE_OK);
580 	}
581 	if (strcmp(key, "rockridge") == 0 ||
582 	    strcmp(key, "Rockridge") == 0) {
583 		iso9660->opt_support_rockridge = val != NULL;
584 		return (ARCHIVE_OK);
585 	}
586 
587 	/* Note: The "warn" return is just to inform the options
588 	 * supervisor that we didn't handle it.  It will generate
589 	 * a suitable error if no one used this option. */
590 	return (ARCHIVE_WARN);
591 }
592 
593 static int
594 isNull(struct iso9660 *iso9660, const unsigned char *h, unsigned offset,
595 unsigned bytes)
596 {
597 
598 	while (bytes >= sizeof(iso9660->null)) {
599 		if (!memcmp(iso9660->null, h + offset, sizeof(iso9660->null)))
600 			return (0);
601 		offset += sizeof(iso9660->null);
602 		bytes -= sizeof(iso9660->null);
603 	}
604 	if (bytes)
605 		return memcmp(iso9660->null, h + offset, bytes) == 0;
606 	else
607 		return (1);
608 }
609 
610 static int
611 isBootRecord(struct iso9660 *iso9660, const unsigned char *h)
612 {
613 	(void)iso9660; /* UNUSED */
614 
615 	/* Type of the Volume Descriptor Boot Record must be 0. */
616 	if (h[0] != 0)
617 		return (0);
618 
619 	/* Volume Descriptor Version must be 1. */
620 	if (h[6] != 1)
621 		return (0);
622 
623 	return (1);
624 }
625 
626 static int
627 isVolumePartition(struct iso9660 *iso9660, const unsigned char *h)
628 {
629 	int32_t location;
630 
631 	/* Type of the Volume Partition Descriptor must be 3. */
632 	if (h[0] != 3)
633 		return (0);
634 
635 	/* Volume Descriptor Version must be 1. */
636 	if (h[6] != 1)
637 		return (0);
638 	/* Unused Field */
639 	if (h[7] != 0)
640 		return (0);
641 
642 	location = archive_le32dec(h + 72);
643 	if (location <= SYSTEM_AREA_BLOCK ||
644 	    location >= iso9660->volume_block)
645 		return (0);
646 	if ((uint32_t)location != archive_be32dec(h + 76))
647 		return (0);
648 
649 	return (1);
650 }
651 
652 static int
653 isVDSetTerminator(struct iso9660 *iso9660, const unsigned char *h)
654 {
655 	(void)iso9660; /* UNUSED */
656 
657 	/* Type of the Volume Descriptor Set Terminator must be 255. */
658 	if (h[0] != 255)
659 		return (0);
660 
661 	/* Volume Descriptor Version must be 1. */
662 	if (h[6] != 1)
663 		return (0);
664 
665 	/* Reserved field must be 0. */
666 	if (!isNull(iso9660, h, 7, 2048-7))
667 		return (0);
668 
669 	return (1);
670 }
671 
672 static int
673 isJolietSVD(struct iso9660 *iso9660, const unsigned char *h)
674 {
675 	const unsigned char *p;
676 	ssize_t logical_block_size;
677 	int32_t volume_block;
678 
679 	/* Check if current sector is a kind of Supplementary Volume
680 	 * Descriptor. */
681 	if (!isSVD(iso9660, h))
682 		return (0);
683 
684 	/* FIXME: do more validations according to joliet spec. */
685 
686 	/* check if this SVD contains joliet extension! */
687 	p = h + SVD_escape_sequences_offset;
688 	/* N.B. Joliet spec says p[1] == '\\', but.... */
689 	if (p[0] == '%' && p[1] == '/') {
690 		int level = 0;
691 
692 		if (p[2] == '@')
693 			level = 1;
694 		else if (p[2] == 'C')
695 			level = 2;
696 		else if (p[2] == 'E')
697 			level = 3;
698 		else /* not joliet */
699 			return (0);
700 
701 		iso9660->seenJoliet = level;
702 
703 	} else /* not joliet */
704 		return (0);
705 
706 	logical_block_size =
707 	    archive_le16dec(h + SVD_logical_block_size_offset);
708 	volume_block = archive_le32dec(h + SVD_volume_space_size_offset);
709 
710 	iso9660->logical_block_size = logical_block_size;
711 	iso9660->volume_block = volume_block;
712 	iso9660->volume_size = logical_block_size * (uint64_t)volume_block;
713 	/* Read Root Directory Record in Volume Descriptor. */
714 	p = h + SVD_root_directory_record_offset;
715 	iso9660->joliet.location = archive_le32dec(p + DR_extent_offset);
716 	iso9660->joliet.size = archive_le32dec(p + DR_size_offset);
717 
718 	return (48);
719 }
720 
721 static int
722 isSVD(struct iso9660 *iso9660, const unsigned char *h)
723 {
724 	const unsigned char *p;
725 	ssize_t logical_block_size;
726 	int32_t volume_block;
727 	int32_t location;
728 
729 	(void)iso9660; /* UNUSED */
730 
731 	/* Type 2 means it's a SVD. */
732 	if (h[SVD_type_offset] != 2)
733 		return (0);
734 
735 	/* Reserved field must be 0. */
736 	if (!isNull(iso9660, h, SVD_reserved1_offset, SVD_reserved1_size))
737 		return (0);
738 	if (!isNull(iso9660, h, SVD_reserved2_offset, SVD_reserved2_size))
739 		return (0);
740 	if (!isNull(iso9660, h, SVD_reserved3_offset, SVD_reserved3_size))
741 		return (0);
742 
743 	/* File structure version must be 1 for ISO9660/ECMA119. */
744 	if (h[SVD_file_structure_version_offset] != 1)
745 		return (0);
746 
747 	logical_block_size =
748 	    archive_le16dec(h + SVD_logical_block_size_offset);
749 	if (logical_block_size <= 0)
750 		return (0);
751 
752 	volume_block = archive_le32dec(h + SVD_volume_space_size_offset);
753 	if (volume_block <= SYSTEM_AREA_BLOCK+4)
754 		return (0);
755 
756 	/* Location of Occurrence of Type L Path Table must be
757 	 * available location,
758 	 * >= SYSTEM_AREA_BLOCK(16) + 2 and < Volume Space Size. */
759 	location = archive_le32dec(h+SVD_type_L_path_table_offset);
760 	if (location < SYSTEM_AREA_BLOCK+2 || location >= volume_block)
761 		return (0);
762 
763 	/* The Type M Path Table must be at a valid location (WinISO
764 	 * and probably other programs omit this, so we allow zero)
765 	 *
766 	 * >= SYSTEM_AREA_BLOCK(16) + 2 and < Volume Space Size. */
767 	location = archive_be32dec(h+SVD_type_M_path_table_offset);
768 	if ((location > 0 && location < SYSTEM_AREA_BLOCK+2)
769 	    || location >= volume_block)
770 		return (0);
771 
772 	/* Read Root Directory Record in Volume Descriptor. */
773 	p = h + SVD_root_directory_record_offset;
774 	if (p[DR_length_offset] != 34)
775 		return (0);
776 
777 	return (48);
778 }
779 
780 static int
781 isEVD(struct iso9660 *iso9660, const unsigned char *h)
782 {
783 	const unsigned char *p;
784 	ssize_t logical_block_size;
785 	int32_t volume_block;
786 	int32_t location;
787 
788 	(void)iso9660; /* UNUSED */
789 
790 	/* Type of the Enhanced Volume Descriptor must be 2. */
791 	if (h[PVD_type_offset] != 2)
792 		return (0);
793 
794 	/* EVD version must be 2. */
795 	if (h[PVD_version_offset] != 2)
796 		return (0);
797 
798 	/* Reserved field must be 0. */
799 	if (h[PVD_reserved1_offset] != 0)
800 		return (0);
801 
802 	/* Reserved field must be 0. */
803 	if (!isNull(iso9660, h, PVD_reserved2_offset, PVD_reserved2_size))
804 		return (0);
805 
806 	/* Reserved field must be 0. */
807 	if (!isNull(iso9660, h, PVD_reserved3_offset, PVD_reserved3_size))
808 		return (0);
809 
810 	/* Logical block size must be > 0. */
811 	/* I've looked at Ecma 119 and can't find any stronger
812 	 * restriction on this field. */
813 	logical_block_size =
814 	    archive_le16dec(h + PVD_logical_block_size_offset);
815 	if (logical_block_size <= 0)
816 		return (0);
817 
818 	volume_block =
819 	    archive_le32dec(h + PVD_volume_space_size_offset);
820 	if (volume_block <= SYSTEM_AREA_BLOCK+4)
821 		return (0);
822 
823 	/* File structure version must be 2 for ISO9660:1999. */
824 	if (h[PVD_file_structure_version_offset] != 2)
825 		return (0);
826 
827 	/* Location of Occurrence of Type L Path Table must be
828 	 * available location,
829 	 * >= SYSTEM_AREA_BLOCK(16) + 2 and < Volume Space Size. */
830 	location = archive_le32dec(h+PVD_type_1_path_table_offset);
831 	if (location < SYSTEM_AREA_BLOCK+2 || location >= volume_block)
832 		return (0);
833 
834 	/* Location of Occurrence of Type M Path Table must be
835 	 * available location,
836 	 * >= SYSTEM_AREA_BLOCK(16) + 2 and < Volume Space Size. */
837 	location = archive_be32dec(h+PVD_type_m_path_table_offset);
838 	if ((location > 0 && location < SYSTEM_AREA_BLOCK+2)
839 	    || location >= volume_block)
840 		return (0);
841 
842 	/* Reserved field must be 0. */
843 	if (!isNull(iso9660, h, PVD_reserved4_offset, PVD_reserved4_size))
844 		return (0);
845 
846 	/* Reserved field must be 0. */
847 	if (!isNull(iso9660, h, PVD_reserved5_offset, PVD_reserved5_size))
848 		return (0);
849 
850 	/* Read Root Directory Record in Volume Descriptor. */
851 	p = h + PVD_root_directory_record_offset;
852 	if (p[DR_length_offset] != 34)
853 		return (0);
854 
855 	return (48);
856 }
857 
858 static int
859 isPVD(struct iso9660 *iso9660, const unsigned char *h)
860 {
861 	const unsigned char *p;
862 	ssize_t logical_block_size;
863 	int32_t volume_block;
864 	int32_t location;
865 	int i;
866 
867 	/* Type of the Primary Volume Descriptor must be 1. */
868 	if (h[PVD_type_offset] != 1)
869 		return (0);
870 
871 	/* PVD version must be 1. */
872 	if (h[PVD_version_offset] != 1)
873 		return (0);
874 
875 	/* Reserved field must be 0. */
876 	if (h[PVD_reserved1_offset] != 0)
877 		return (0);
878 
879 	/* Reserved field must be 0. */
880 	if (!isNull(iso9660, h, PVD_reserved2_offset, PVD_reserved2_size))
881 		return (0);
882 
883 	/* Reserved field must be 0. */
884 	if (!isNull(iso9660, h, PVD_reserved3_offset, PVD_reserved3_size))
885 		return (0);
886 
887 	/* Logical block size must be > 0. */
888 	/* I've looked at Ecma 119 and can't find any stronger
889 	 * restriction on this field. */
890 	logical_block_size =
891 	    archive_le16dec(h + PVD_logical_block_size_offset);
892 	if (logical_block_size <= 0)
893 		return (0);
894 
895 	volume_block = archive_le32dec(h + PVD_volume_space_size_offset);
896 	if (volume_block <= SYSTEM_AREA_BLOCK+4)
897 		return (0);
898 
899 	/* File structure version must be 1 for ISO9660/ECMA119. */
900 	if (h[PVD_file_structure_version_offset] != 1)
901 		return (0);
902 
903 	/* Location of Occurrence of Type L Path Table must be
904 	 * available location,
905 	 * > SYSTEM_AREA_BLOCK(16) + 2 and < Volume Space Size. */
906 	location = archive_le32dec(h+PVD_type_1_path_table_offset);
907 	if (location < SYSTEM_AREA_BLOCK+2 || location >= volume_block)
908 		return (0);
909 
910 	/* The Type M Path Table must also be at a valid location
911 	 * (although ECMA 119 requires a Type M Path Table, WinISO and
912 	 * probably other programs omit it, so we permit a zero here)
913 	 *
914 	 * >= SYSTEM_AREA_BLOCK(16) + 2 and < Volume Space Size. */
915 	location = archive_be32dec(h+PVD_type_m_path_table_offset);
916 	if ((location > 0 && location < SYSTEM_AREA_BLOCK+2)
917 	    || location >= volume_block)
918 		return (0);
919 
920 	/* Reserved field must be 0. */
921 	/* But accept NetBSD/FreeBSD "makefs" images with 0x20 here. */
922 	for (i = 0; i < PVD_reserved4_size; ++i)
923 		if (h[PVD_reserved4_offset + i] != 0
924 		    && h[PVD_reserved4_offset + i] != 0x20)
925 			return (0);
926 
927 	/* Reserved field must be 0. */
928 	if (!isNull(iso9660, h, PVD_reserved5_offset, PVD_reserved5_size))
929 		return (0);
930 
931 	/* XXX TODO: Check other values for sanity; reject more
932 	 * malformed PVDs. XXX */
933 
934 	/* Read Root Directory Record in Volume Descriptor. */
935 	p = h + PVD_root_directory_record_offset;
936 	if (p[DR_length_offset] != 34)
937 		return (0);
938 
939 	if (!iso9660->primary.location) {
940 		iso9660->logical_block_size = logical_block_size;
941 		iso9660->volume_block = volume_block;
942 		iso9660->volume_size =
943 		    logical_block_size * (uint64_t)volume_block;
944 		iso9660->primary.location =
945 		    archive_le32dec(p + DR_extent_offset);
946 		iso9660->primary.size = archive_le32dec(p + DR_size_offset);
947 	}
948 
949 	return (48);
950 }
951 
952 static int
953 read_children(struct archive_read *a, struct file_info *parent)
954 {
955 	struct iso9660 *iso9660;
956 	const unsigned char *b, *p;
957 	struct file_info *multi;
958 	size_t step, skip_size;
959 
960 	iso9660 = (struct iso9660 *)(a->format->data);
961 	/* flush any remaining bytes from the last round to ensure
962 	 * we're positioned */
963 	if (iso9660->entry_bytes_unconsumed) {
964 		__archive_read_consume(a, iso9660->entry_bytes_unconsumed);
965 		iso9660->entry_bytes_unconsumed = 0;
966 	}
967 	if (iso9660->current_position > parent->offset) {
968 		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
969 		    "Ignoring out-of-order directory (%s) %jd > %jd",
970 		    parent->name.s,
971 		    (intmax_t)iso9660->current_position,
972 		    (intmax_t)parent->offset);
973 		return (ARCHIVE_WARN);
974 	}
975 	if (parent->offset + parent->size > iso9660->volume_size) {
976 		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
977 		    "Directory is beyond end-of-media: %s",
978 		    parent->name.s);
979 		return (ARCHIVE_WARN);
980 	}
981 	if (iso9660->current_position < parent->offset) {
982 		int64_t skipsize;
983 
984 		skipsize = parent->offset - iso9660->current_position;
985 		skipsize = __archive_read_consume(a, skipsize);
986 		if (skipsize < 0)
987 			return ((int)skipsize);
988 		iso9660->current_position = parent->offset;
989 	}
990 
991 	step = (size_t)(((parent->size + iso9660->logical_block_size -1) /
992 	    iso9660->logical_block_size) * iso9660->logical_block_size);
993 	b = __archive_read_ahead(a, step, NULL);
994 	if (b == NULL) {
995 		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
996 		    "Failed to read full block when scanning "
997 		    "ISO9660 directory list");
998 		return (ARCHIVE_FATAL);
999 	}
1000 	iso9660->current_position += step;
1001 	multi = NULL;
1002 	skip_size = step;
1003 	while (step) {
1004 		p = b;
1005 		b += iso9660->logical_block_size;
1006 		step -= iso9660->logical_block_size;
1007 		for (; *p != 0 && p < b && p + *p <= b; p += *p) {
1008 			struct file_info *child;
1009 
1010 			/* N.B.: these special directory identifiers
1011 			 * are 8 bit "values" even on a
1012 			 * Joliet CD with UCS-2 (16bit) encoding.
1013 			 */
1014 
1015 			/* Skip '.' entry. */
1016 			if (*(p + DR_name_len_offset) == 1
1017 			    && *(p + DR_name_offset) == '\0')
1018 				continue;
1019 			/* Skip '..' entry. */
1020 			if (*(p + DR_name_len_offset) == 1
1021 			    && *(p + DR_name_offset) == '\001')
1022 				continue;
1023 			child = parse_file_info(a, parent, p);
1024 			if (child == NULL) {
1025 				__archive_read_consume(a, skip_size);
1026 				return (ARCHIVE_FATAL);
1027 			}
1028 			if (child->cl_offset == 0 &&
1029 			    (child->multi_extent || multi != NULL)) {
1030 				struct content *con;
1031 
1032 				if (multi == NULL) {
1033 					multi = child;
1034 					multi->contents.first = NULL;
1035 					multi->contents.last =
1036 					    &(multi->contents.first);
1037 				}
1038 				con = malloc(sizeof(struct content));
1039 				if (con == NULL) {
1040 					archive_set_error(
1041 					    &a->archive, ENOMEM,
1042 					    "No memory for multi extent");
1043 					__archive_read_consume(a, skip_size);
1044 					return (ARCHIVE_FATAL);
1045 				}
1046 				con->offset = child->offset;
1047 				con->size = child->size;
1048 				con->next = NULL;
1049 				*multi->contents.last = con;
1050 				multi->contents.last = &(con->next);
1051 				if (multi == child) {
1052 					if (add_entry(a, iso9660, child)
1053 					    != ARCHIVE_OK)
1054 						return (ARCHIVE_FATAL);
1055 				} else {
1056 					multi->size += child->size;
1057 					if (!child->multi_extent)
1058 						multi = NULL;
1059 				}
1060 			} else
1061 				if (add_entry(a, iso9660, child) != ARCHIVE_OK)
1062 					return (ARCHIVE_FATAL);
1063 		}
1064 	}
1065 
1066 	__archive_read_consume(a, skip_size);
1067 
1068 	/* Read data which recorded by RRIP "CE" extension. */
1069 	if (read_CE(a, iso9660) != ARCHIVE_OK)
1070 		return (ARCHIVE_FATAL);
1071 
1072 	return (ARCHIVE_OK);
1073 }
1074 
1075 static int
1076 choose_volume(struct archive_read *a, struct iso9660 *iso9660)
1077 {
1078 	struct file_info *file;
1079 	int64_t skipsize;
1080 	struct vd *vd;
1081 	const void *block;
1082 	char seenJoliet;
1083 
1084 	vd = &(iso9660->primary);
1085 	if (!iso9660->opt_support_joliet)
1086 		iso9660->seenJoliet = 0;
1087 	if (iso9660->seenJoliet &&
1088 		vd->location > iso9660->joliet.location)
1089 		/* This condition is unlikely; by way of caution. */
1090 		vd = &(iso9660->joliet);
1091 
1092 	skipsize = LOGICAL_BLOCK_SIZE * vd->location;
1093 	skipsize = __archive_read_consume(a, skipsize);
1094 	if (skipsize < 0)
1095 		return ((int)skipsize);
1096 	iso9660->current_position = skipsize;
1097 
1098 	block = __archive_read_ahead(a, vd->size, NULL);
1099 	if (block == NULL) {
1100 		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1101 		    "Failed to read full block when scanning "
1102 		    "ISO9660 directory list");
1103 		return (ARCHIVE_FATAL);
1104 	}
1105 
1106 	/*
1107 	 * While reading Root Directory, flag seenJoliet must be zero to
1108 	 * avoid converting special name 0x00(Current Directory) and
1109 	 * next byte to UCS2.
1110 	 */
1111 	seenJoliet = iso9660->seenJoliet;/* Save flag. */
1112 	iso9660->seenJoliet = 0;
1113 	file = parse_file_info(a, NULL, block);
1114 	if (file == NULL)
1115 		return (ARCHIVE_FATAL);
1116 	iso9660->seenJoliet = seenJoliet;
1117 
1118 	/*
1119 	 * If the iso image has both RockRidge and Joliet, we preferentially
1120 	 * use RockRidge Extensions rather than Joliet ones.
1121 	 */
1122 	if (vd == &(iso9660->primary) && iso9660->seenRockridge
1123 	    && iso9660->seenJoliet)
1124 		iso9660->seenJoliet = 0;
1125 
1126 	if (vd == &(iso9660->primary) && !iso9660->seenRockridge
1127 	    && iso9660->seenJoliet) {
1128 		/* Switch reading data from primary to joliet. */
1129 		vd = &(iso9660->joliet);
1130 		skipsize = LOGICAL_BLOCK_SIZE * vd->location;
1131 		skipsize -= iso9660->current_position;
1132 		skipsize = __archive_read_consume(a, skipsize);
1133 		if (skipsize < 0)
1134 			return ((int)skipsize);
1135 		iso9660->current_position += skipsize;
1136 
1137 		block = __archive_read_ahead(a, vd->size, NULL);
1138 		if (block == NULL) {
1139 			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1140 			    "Failed to read full block when scanning "
1141 			    "ISO9660 directory list");
1142 			return (ARCHIVE_FATAL);
1143 		}
1144 		iso9660->seenJoliet = 0;
1145 		file = parse_file_info(a, NULL, block);
1146 		if (file == NULL)
1147 			return (ARCHIVE_FATAL);
1148 		iso9660->seenJoliet = seenJoliet;
1149 	}
1150 
1151 	/* Store the root directory in the pending list. */
1152 	if (add_entry(a, iso9660, file) != ARCHIVE_OK)
1153 		return (ARCHIVE_FATAL);
1154 	if (iso9660->seenRockridge) {
1155 		a->archive.archive_format = ARCHIVE_FORMAT_ISO9660_ROCKRIDGE;
1156 		a->archive.archive_format_name =
1157 		    "ISO9660 with Rockridge extensions";
1158 	}
1159 
1160 	return (ARCHIVE_OK);
1161 }
1162 
1163 static int
1164 archive_read_format_iso9660_read_header(struct archive_read *a,
1165     struct archive_entry *entry)
1166 {
1167 	struct iso9660 *iso9660;
1168 	struct file_info *file;
1169 	int r, rd_r = ARCHIVE_OK;
1170 
1171 	iso9660 = (struct iso9660 *)(a->format->data);
1172 
1173 	if (!a->archive.archive_format) {
1174 		a->archive.archive_format = ARCHIVE_FORMAT_ISO9660;
1175 		a->archive.archive_format_name = "ISO9660";
1176 	}
1177 
1178 	if (iso9660->current_position == 0) {
1179 		r = choose_volume(a, iso9660);
1180 		if (r != ARCHIVE_OK)
1181 			return (r);
1182 	}
1183 
1184 	file = NULL;/* Eliminate a warning. */
1185 	/* Get the next entry that appears after the current offset. */
1186 	r = next_entry_seek(a, iso9660, &file);
1187 	if (r != ARCHIVE_OK)
1188 		return (r);
1189 
1190 	if (iso9660->seenJoliet) {
1191 		/*
1192 		 * Convert UTF-16BE of a filename to local locale MBS
1193 		 * and store the result into a filename field.
1194 		 */
1195 		if (iso9660->sconv_utf16be == NULL) {
1196 			iso9660->sconv_utf16be =
1197 			    archive_string_conversion_from_charset(
1198 				&(a->archive), "UTF-16BE", 1);
1199 			if (iso9660->sconv_utf16be == NULL)
1200 				/* Coundn't allocate memory */
1201 				return (ARCHIVE_FATAL);
1202 		}
1203 		if (iso9660->utf16be_path == NULL) {
1204 			iso9660->utf16be_path = malloc(UTF16_NAME_MAX);
1205 			if (iso9660->utf16be_path == NULL) {
1206 				archive_set_error(&a->archive, ENOMEM,
1207 				    "No memory");
1208 				return (ARCHIVE_FATAL);
1209 			}
1210 		}
1211 		if (iso9660->utf16be_previous_path == NULL) {
1212 			iso9660->utf16be_previous_path = malloc(UTF16_NAME_MAX);
1213 			if (iso9660->utf16be_previous_path == NULL) {
1214 				archive_set_error(&a->archive, ENOMEM,
1215 				    "No memory");
1216 				return (ARCHIVE_FATAL);
1217 			}
1218 		}
1219 
1220 		iso9660->utf16be_path_len = 0;
1221 		if (build_pathname_utf16be(iso9660->utf16be_path,
1222 		    UTF16_NAME_MAX, &(iso9660->utf16be_path_len), file) != 0) {
1223 			archive_set_error(&a->archive,
1224 			    ARCHIVE_ERRNO_FILE_FORMAT,
1225 			    "Pathname is too long");
1226 		}
1227 
1228 		r = archive_entry_copy_pathname_l(entry,
1229 		    (const char *)iso9660->utf16be_path,
1230 		    iso9660->utf16be_path_len,
1231 		    iso9660->sconv_utf16be);
1232 		if (r != 0) {
1233 			if (errno == ENOMEM) {
1234 				archive_set_error(&a->archive, ENOMEM,
1235 				    "No memory for Pathname");
1236 				return (ARCHIVE_FATAL);
1237 			}
1238 			archive_set_error(&a->archive,
1239 			    ARCHIVE_ERRNO_FILE_FORMAT,
1240 			    "Pathname cannot be converted "
1241 			    "from %s to current locale.",
1242 			    archive_string_conversion_charset_name(
1243 			      iso9660->sconv_utf16be));
1244 
1245 			rd_r = ARCHIVE_WARN;
1246 		}
1247 	} else {
1248 		archive_string_empty(&iso9660->pathname);
1249 		archive_entry_set_pathname(entry,
1250 		    build_pathname(&iso9660->pathname, file));
1251 	}
1252 
1253 	iso9660->entry_bytes_remaining = file->size;
1254 	/* Offset for sparse-file-aware clients. */
1255 	iso9660->entry_sparse_offset = 0;
1256 
1257 	if (file->offset + file->size > iso9660->volume_size) {
1258 		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1259 		    "File is beyond end-of-media: %s",
1260 		    archive_entry_pathname(entry));
1261 		iso9660->entry_bytes_remaining = 0;
1262 		return (ARCHIVE_WARN);
1263 	}
1264 
1265 	/* Set up the entry structure with information about this entry. */
1266 	archive_entry_set_mode(entry, file->mode);
1267 	archive_entry_set_uid(entry, file->uid);
1268 	archive_entry_set_gid(entry, file->gid);
1269 	archive_entry_set_nlink(entry, file->nlinks);
1270 	if (file->birthtime_is_set)
1271 		archive_entry_set_birthtime(entry, file->birthtime, 0);
1272 	else
1273 		archive_entry_unset_birthtime(entry);
1274 	archive_entry_set_mtime(entry, file->mtime, 0);
1275 	archive_entry_set_ctime(entry, file->ctime, 0);
1276 	archive_entry_set_atime(entry, file->atime, 0);
1277 	/* N.B.: Rock Ridge supports 64-bit device numbers. */
1278 	archive_entry_set_rdev(entry, (dev_t)file->rdev);
1279 	archive_entry_set_size(entry, iso9660->entry_bytes_remaining);
1280 	if (file->symlink.s != NULL)
1281 		archive_entry_copy_symlink(entry, file->symlink.s);
1282 
1283 	/* Note: If the input isn't seekable, we can't rewind to
1284 	 * return the same body again, so if the next entry refers to
1285 	 * the same data, we have to return it as a hardlink to the
1286 	 * original entry. */
1287 	if (file->number != -1 &&
1288 	    file->number == iso9660->previous_number) {
1289 		if (iso9660->seenJoliet) {
1290 			r = archive_entry_copy_hardlink_l(entry,
1291 			    (const char *)iso9660->utf16be_previous_path,
1292 			    iso9660->utf16be_previous_path_len,
1293 			    iso9660->sconv_utf16be);
1294 			if (r != 0) {
1295 				if (errno == ENOMEM) {
1296 					archive_set_error(&a->archive, ENOMEM,
1297 					    "No memory for Linkname");
1298 					return (ARCHIVE_FATAL);
1299 				}
1300 				archive_set_error(&a->archive,
1301 				    ARCHIVE_ERRNO_FILE_FORMAT,
1302 				    "Linkname cannot be converted "
1303 				    "from %s to current locale.",
1304 				    archive_string_conversion_charset_name(
1305 				      iso9660->sconv_utf16be));
1306 				rd_r = ARCHIVE_WARN;
1307 			}
1308 		} else
1309 			archive_entry_set_hardlink(entry,
1310 			    iso9660->previous_pathname.s);
1311 		archive_entry_unset_size(entry);
1312 		iso9660->entry_bytes_remaining = 0;
1313 		return (rd_r);
1314 	}
1315 
1316 	if ((file->mode & AE_IFMT) != AE_IFDIR &&
1317 	    file->offset < iso9660->current_position) {
1318 		int64_t r64;
1319 
1320 		r64 = __archive_read_seek(a, file->offset, SEEK_SET);
1321 		if (r64 != (int64_t)file->offset) {
1322 			/* We can't seek backwards to extract it, so issue
1323 			 * a warning.  Note that this can only happen if
1324 			 * this entry was added to the heap after we passed
1325 			 * this offset, that is, only if the directory
1326 			 * mentioning this entry is later than the body of
1327 			 * the entry. Such layouts are very unusual; most
1328 			 * ISO9660 writers lay out and record all directory
1329 			 * information first, then store all file bodies. */
1330 			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1331 			    "Ignoring out-of-order file @%jx (%s) %jd < %jd",
1332 			    (intmax_t)file->number,
1333 			    iso9660->pathname.s,
1334 			    (intmax_t)file->offset,
1335 			    (intmax_t)iso9660->current_position);
1336 			iso9660->entry_bytes_remaining = 0;
1337 			return (ARCHIVE_WARN);
1338 		}
1339 		iso9660->current_position = (uint64_t)r64;
1340 	}
1341 
1342 	/* Initialize zisofs variables. */
1343 	iso9660->entry_zisofs.pz = file->pz;
1344 	if (file->pz) {
1345 #ifdef HAVE_ZLIB_H
1346 		struct zisofs  *zisofs;
1347 
1348 		zisofs = &iso9660->entry_zisofs;
1349 		zisofs->initialized = 0;
1350 		zisofs->pz_log2_bs = file->pz_log2_bs;
1351 		zisofs->pz_uncompressed_size = file->pz_uncompressed_size;
1352 		zisofs->pz_offset = 0;
1353 		zisofs->header_avail = 0;
1354 		zisofs->header_passed = 0;
1355 		zisofs->block_pointers_avail = 0;
1356 #endif
1357 		archive_entry_set_size(entry, file->pz_uncompressed_size);
1358 	}
1359 
1360 	iso9660->previous_number = file->number;
1361 	if (iso9660->seenJoliet) {
1362 		memcpy(iso9660->utf16be_previous_path, iso9660->utf16be_path,
1363 		    iso9660->utf16be_path_len);
1364 		iso9660->utf16be_previous_path_len = iso9660->utf16be_path_len;
1365 	} else
1366 		archive_strcpy(
1367 		    &iso9660->previous_pathname, iso9660->pathname.s);
1368 
1369 	/* Reset entry_bytes_remaining if the file is multi extent. */
1370 	iso9660->entry_content = file->contents.first;
1371 	if (iso9660->entry_content != NULL)
1372 		iso9660->entry_bytes_remaining = iso9660->entry_content->size;
1373 
1374 	if (archive_entry_filetype(entry) == AE_IFDIR) {
1375 		/* Overwrite nlinks by proper link number which is
1376 		 * calculated from number of sub directories. */
1377 		archive_entry_set_nlink(entry, 2 + file->subdirs);
1378 		/* Directory data has been read completely. */
1379 		iso9660->entry_bytes_remaining = 0;
1380 	}
1381 
1382 	if (rd_r != ARCHIVE_OK)
1383 		return (rd_r);
1384 	return (ARCHIVE_OK);
1385 }
1386 
1387 static int
1388 archive_read_format_iso9660_read_data_skip(struct archive_read *a)
1389 {
1390 	/* Because read_next_header always does an explicit skip
1391 	 * to the next entry, we don't need to do anything here. */
1392 	(void)a; /* UNUSED */
1393 	return (ARCHIVE_OK);
1394 }
1395 
1396 #ifdef HAVE_ZLIB_H
1397 
1398 static int
1399 zisofs_read_data(struct archive_read *a,
1400     const void **buff, size_t *size, int64_t *offset)
1401 {
1402 	struct iso9660 *iso9660;
1403 	struct zisofs  *zisofs;
1404 	const unsigned char *p;
1405 	size_t avail;
1406 	ssize_t bytes_read;
1407 	size_t uncompressed_size;
1408 	int r;
1409 
1410 	iso9660 = (struct iso9660 *)(a->format->data);
1411 	zisofs = &iso9660->entry_zisofs;
1412 
1413 	p = __archive_read_ahead(a, 1, &bytes_read);
1414 	if (bytes_read <= 0) {
1415 		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1416 		    "Truncated zisofs file body");
1417 		return (ARCHIVE_FATAL);
1418 	}
1419 	if (bytes_read > iso9660->entry_bytes_remaining)
1420 		bytes_read = (ssize_t)iso9660->entry_bytes_remaining;
1421 	avail = bytes_read;
1422 	uncompressed_size = 0;
1423 
1424 	if (!zisofs->initialized) {
1425 		size_t ceil, xsize;
1426 
1427 		/* Allocate block pointers buffer. */
1428 		ceil = (size_t)((zisofs->pz_uncompressed_size +
1429 			(((int64_t)1) << zisofs->pz_log2_bs) - 1)
1430 			>> zisofs->pz_log2_bs);
1431 		xsize = (ceil + 1) * 4;
1432 		if (zisofs->block_pointers_alloc < xsize) {
1433 			size_t alloc;
1434 
1435 			if (zisofs->block_pointers != NULL)
1436 				free(zisofs->block_pointers);
1437 			alloc = ((xsize >> 10) + 1) << 10;
1438 			zisofs->block_pointers = malloc(alloc);
1439 			if (zisofs->block_pointers == NULL) {
1440 				archive_set_error(&a->archive, ENOMEM,
1441 				    "No memory for zisofs decompression");
1442 				return (ARCHIVE_FATAL);
1443 			}
1444 			zisofs->block_pointers_alloc = alloc;
1445 		}
1446 		zisofs->block_pointers_size = xsize;
1447 
1448 		/* Allocate uncompressed data buffer. */
1449 		xsize = (size_t)1UL << zisofs->pz_log2_bs;
1450 		if (zisofs->uncompressed_buffer_size < xsize) {
1451 			if (zisofs->uncompressed_buffer != NULL)
1452 				free(zisofs->uncompressed_buffer);
1453 			zisofs->uncompressed_buffer = malloc(xsize);
1454 			if (zisofs->uncompressed_buffer == NULL) {
1455 				archive_set_error(&a->archive, ENOMEM,
1456 				    "No memory for zisofs decompression");
1457 				return (ARCHIVE_FATAL);
1458 			}
1459 		}
1460 		zisofs->uncompressed_buffer_size = xsize;
1461 
1462 		/*
1463 		 * Read the file header, and check the magic code of zisofs.
1464 		 */
1465 		if (zisofs->header_avail < sizeof(zisofs->header)) {
1466 			xsize = sizeof(zisofs->header) - zisofs->header_avail;
1467 			if (avail < xsize)
1468 				xsize = avail;
1469 			memcpy(zisofs->header + zisofs->header_avail, p, xsize);
1470 			zisofs->header_avail += xsize;
1471 			avail -= xsize;
1472 			p += xsize;
1473 		}
1474 		if (!zisofs->header_passed &&
1475 		    zisofs->header_avail == sizeof(zisofs->header)) {
1476 			int err = 0;
1477 
1478 			if (memcmp(zisofs->header, zisofs_magic,
1479 			    sizeof(zisofs_magic)) != 0)
1480 				err = 1;
1481 			if (archive_le32dec(zisofs->header + 8)
1482 			    != zisofs->pz_uncompressed_size)
1483 				err = 1;
1484 			if (zisofs->header[12] != 4)
1485 				err = 1;
1486 			if (zisofs->header[13] != zisofs->pz_log2_bs)
1487 				err = 1;
1488 			if (err) {
1489 				archive_set_error(&a->archive,
1490 				    ARCHIVE_ERRNO_FILE_FORMAT,
1491 				    "Illegal zisofs file body");
1492 				return (ARCHIVE_FATAL);
1493 			}
1494 			zisofs->header_passed = 1;
1495 		}
1496 		/*
1497 		 * Read block pointers.
1498 		 */
1499 		if (zisofs->header_passed &&
1500 		    zisofs->block_pointers_avail < zisofs->block_pointers_size) {
1501 			xsize = zisofs->block_pointers_size
1502 			    - zisofs->block_pointers_avail;
1503 			if (avail < xsize)
1504 				xsize = avail;
1505 			memcpy(zisofs->block_pointers
1506 			    + zisofs->block_pointers_avail, p, xsize);
1507 			zisofs->block_pointers_avail += xsize;
1508 			avail -= xsize;
1509 			p += xsize;
1510 		    	if (zisofs->block_pointers_avail
1511 			    == zisofs->block_pointers_size) {
1512 				/* We've got all block pointers and initialize
1513 				 * related variables.	*/
1514 				zisofs->block_off = 0;
1515 				zisofs->block_avail = 0;
1516 				/* Complete a initialization */
1517 				zisofs->initialized = 1;
1518 			}
1519 		}
1520 
1521 		if (!zisofs->initialized)
1522 			goto next_data; /* We need more data. */
1523 	}
1524 
1525 	/*
1526 	 * Get block offsets from block pointers.
1527 	 */
1528 	if (zisofs->block_avail == 0) {
1529 		uint32_t bst, bed;
1530 
1531 		if (zisofs->block_off + 4 >= zisofs->block_pointers_size) {
1532 			/* There isn't a pair of offsets. */
1533 			archive_set_error(&a->archive,
1534 			    ARCHIVE_ERRNO_FILE_FORMAT,
1535 			    "Illegal zisofs block pointers");
1536 			return (ARCHIVE_FATAL);
1537 		}
1538 		bst = archive_le32dec(
1539 		    zisofs->block_pointers + zisofs->block_off);
1540 		if (bst != zisofs->pz_offset + (bytes_read - avail)) {
1541 			/* TODO: Should we seek offset of current file
1542 			 * by bst ? */
1543 			archive_set_error(&a->archive,
1544 			    ARCHIVE_ERRNO_FILE_FORMAT,
1545 			    "Illegal zisofs block pointers(cannot seek)");
1546 			return (ARCHIVE_FATAL);
1547 		}
1548 		bed = archive_le32dec(
1549 		    zisofs->block_pointers + zisofs->block_off + 4);
1550 		if (bed < bst) {
1551 			archive_set_error(&a->archive,
1552 			    ARCHIVE_ERRNO_FILE_FORMAT,
1553 			    "Illegal zisofs block pointers");
1554 			return (ARCHIVE_FATAL);
1555 		}
1556 		zisofs->block_avail = bed - bst;
1557 		zisofs->block_off += 4;
1558 
1559 		/* Initialize compression library for new block. */
1560 		if (zisofs->stream_valid)
1561 			r = inflateReset(&zisofs->stream);
1562 		else
1563 			r = inflateInit(&zisofs->stream);
1564 		if (r != Z_OK) {
1565 			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1566 			    "Can't initialize zisofs decompression.");
1567 			return (ARCHIVE_FATAL);
1568 		}
1569 		zisofs->stream_valid = 1;
1570 		zisofs->stream.total_in = 0;
1571 		zisofs->stream.total_out = 0;
1572 	}
1573 
1574 	/*
1575 	 * Make uncompressed data.
1576 	 */
1577 	if (zisofs->block_avail == 0) {
1578 		memset(zisofs->uncompressed_buffer, 0,
1579 		    zisofs->uncompressed_buffer_size);
1580 		uncompressed_size = zisofs->uncompressed_buffer_size;
1581 	} else {
1582 		zisofs->stream.next_in = (Bytef *)(uintptr_t)(const void *)p;
1583 		if (avail > zisofs->block_avail)
1584 			zisofs->stream.avail_in = zisofs->block_avail;
1585 		else
1586 			zisofs->stream.avail_in = (uInt)avail;
1587 		zisofs->stream.next_out = zisofs->uncompressed_buffer;
1588 		zisofs->stream.avail_out =
1589 		    (uInt)zisofs->uncompressed_buffer_size;
1590 
1591 		r = inflate(&zisofs->stream, 0);
1592 		switch (r) {
1593 		case Z_OK: /* Decompressor made some progress.*/
1594 		case Z_STREAM_END: /* Found end of stream. */
1595 			break;
1596 		default:
1597 			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1598 			    "zisofs decompression failed (%d)", r);
1599 			return (ARCHIVE_FATAL);
1600 		}
1601 		uncompressed_size =
1602 		    zisofs->uncompressed_buffer_size - zisofs->stream.avail_out;
1603 		avail -= zisofs->stream.next_in - p;
1604 		zisofs->block_avail -= (uint32_t)(zisofs->stream.next_in - p);
1605 	}
1606 next_data:
1607 	bytes_read -= avail;
1608 	*buff = zisofs->uncompressed_buffer;
1609 	*size = uncompressed_size;
1610 	*offset = iso9660->entry_sparse_offset;
1611 	iso9660->entry_sparse_offset += uncompressed_size;
1612 	iso9660->entry_bytes_remaining -= bytes_read;
1613 	iso9660->current_position += bytes_read;
1614 	zisofs->pz_offset += (uint32_t)bytes_read;
1615 	iso9660->entry_bytes_unconsumed += bytes_read;
1616 
1617 	return (ARCHIVE_OK);
1618 }
1619 
1620 #else /* HAVE_ZLIB_H */
1621 
1622 static int
1623 zisofs_read_data(struct archive_read *a,
1624     const void **buff, size_t *size, int64_t *offset)
1625 {
1626 
1627 	(void)buff;/* UNUSED */
1628 	(void)size;/* UNUSED */
1629 	(void)offset;/* UNUSED */
1630 	archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1631 	    "zisofs is not supported on this platform.");
1632 	return (ARCHIVE_FAILED);
1633 }
1634 
1635 #endif /* HAVE_ZLIB_H */
1636 
1637 static int
1638 archive_read_format_iso9660_read_data(struct archive_read *a,
1639     const void **buff, size_t *size, int64_t *offset)
1640 {
1641 	ssize_t bytes_read;
1642 	struct iso9660 *iso9660;
1643 
1644 	iso9660 = (struct iso9660 *)(a->format->data);
1645 
1646 	if (iso9660->entry_bytes_unconsumed) {
1647 		__archive_read_consume(a, iso9660->entry_bytes_unconsumed);
1648 		iso9660->entry_bytes_unconsumed = 0;
1649 	}
1650 
1651 	if (iso9660->entry_bytes_remaining <= 0) {
1652 		if (iso9660->entry_content != NULL)
1653 			iso9660->entry_content = iso9660->entry_content->next;
1654 		if (iso9660->entry_content == NULL) {
1655 			*buff = NULL;
1656 			*size = 0;
1657 			*offset = iso9660->entry_sparse_offset;
1658 			return (ARCHIVE_EOF);
1659 		}
1660 		/* Seek forward to the start of the entry. */
1661 		if (iso9660->current_position < iso9660->entry_content->offset) {
1662 			int64_t step;
1663 
1664 			step = iso9660->entry_content->offset -
1665 			    iso9660->current_position;
1666 			step = __archive_read_consume(a, step);
1667 			if (step < 0)
1668 				return ((int)step);
1669 			iso9660->current_position =
1670 			    iso9660->entry_content->offset;
1671 		}
1672 		if (iso9660->entry_content->offset < iso9660->current_position) {
1673 			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1674 			    "Ignoring out-of-order file (%s) %jd < %jd",
1675 			    iso9660->pathname.s,
1676 			    (intmax_t)iso9660->entry_content->offset,
1677 			    (intmax_t)iso9660->current_position);
1678 			*buff = NULL;
1679 			*size = 0;
1680 			*offset = iso9660->entry_sparse_offset;
1681 			return (ARCHIVE_WARN);
1682 		}
1683 		iso9660->entry_bytes_remaining = iso9660->entry_content->size;
1684 	}
1685 	if (iso9660->entry_zisofs.pz)
1686 		return (zisofs_read_data(a, buff, size, offset));
1687 
1688 	*buff = __archive_read_ahead(a, 1, &bytes_read);
1689 	if (bytes_read == 0)
1690 		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1691 		    "Truncated input file");
1692 	if (*buff == NULL)
1693 		return (ARCHIVE_FATAL);
1694 	if (bytes_read > iso9660->entry_bytes_remaining)
1695 		bytes_read = (ssize_t)iso9660->entry_bytes_remaining;
1696 	*size = bytes_read;
1697 	*offset = iso9660->entry_sparse_offset;
1698 	iso9660->entry_sparse_offset += bytes_read;
1699 	iso9660->entry_bytes_remaining -= bytes_read;
1700 	iso9660->entry_bytes_unconsumed = bytes_read;
1701 	iso9660->current_position += bytes_read;
1702 	return (ARCHIVE_OK);
1703 }
1704 
1705 static int
1706 archive_read_format_iso9660_cleanup(struct archive_read *a)
1707 {
1708 	struct iso9660 *iso9660;
1709 	int r = ARCHIVE_OK;
1710 
1711 	iso9660 = (struct iso9660 *)(a->format->data);
1712 	release_files(iso9660);
1713 	free(iso9660->read_ce_req.reqs);
1714 	archive_string_free(&iso9660->pathname);
1715 	archive_string_free(&iso9660->previous_pathname);
1716 	if (iso9660->pending_files.files)
1717 		free(iso9660->pending_files.files);
1718 #ifdef HAVE_ZLIB_H
1719 	free(iso9660->entry_zisofs.uncompressed_buffer);
1720 	free(iso9660->entry_zisofs.block_pointers);
1721 	if (iso9660->entry_zisofs.stream_valid) {
1722 		if (inflateEnd(&iso9660->entry_zisofs.stream) != Z_OK) {
1723 			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1724 			    "Failed to clean up zlib decompressor");
1725 			r = ARCHIVE_FATAL;
1726 		}
1727 	}
1728 #endif
1729 	free(iso9660->utf16be_path);
1730 	free(iso9660->utf16be_previous_path);
1731 	free(iso9660);
1732 	(a->format->data) = NULL;
1733 	return (r);
1734 }
1735 
1736 /*
1737  * This routine parses a single ISO directory record, makes sense
1738  * of any extensions, and stores the result in memory.
1739  */
1740 static struct file_info *
1741 parse_file_info(struct archive_read *a, struct file_info *parent,
1742     const unsigned char *isodirrec)
1743 {
1744 	struct iso9660 *iso9660;
1745 	struct file_info *file;
1746 	size_t name_len;
1747 	const unsigned char *rr_start, *rr_end;
1748 	const unsigned char *p;
1749 	size_t dr_len;
1750 	uint64_t fsize;
1751 	int32_t location;
1752 	int flags;
1753 
1754 	iso9660 = (struct iso9660 *)(a->format->data);
1755 
1756 	dr_len = (size_t)isodirrec[DR_length_offset];
1757 	name_len = (size_t)isodirrec[DR_name_len_offset];
1758 	location = archive_le32dec(isodirrec + DR_extent_offset);
1759 	fsize = toi(isodirrec + DR_size_offset, DR_size_size);
1760 	/* Sanity check that dr_len needs at least 34. */
1761 	if (dr_len < 34) {
1762 		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1763 		    "Invalid length of directory record");
1764 		return (NULL);
1765 	}
1766 	/* Sanity check that name_len doesn't exceed dr_len. */
1767 	if (dr_len - 33 < name_len || name_len == 0) {
1768 		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1769 		    "Invalid length of file identifier");
1770 		return (NULL);
1771 	}
1772 	/* Sanity check that location doesn't exceed volume block.
1773 	 * Don't check lower limit of location; it's possibility
1774 	 * the location has negative value when file type is symbolic
1775 	 * link or file size is zero. As far as I know latest mkisofs
1776 	 * do that.
1777 	 */
1778 	if (location > 0 &&
1779 	    (location + ((fsize + iso9660->logical_block_size -1)
1780 	       / iso9660->logical_block_size))
1781 			> (uint32_t)iso9660->volume_block) {
1782 		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1783 		    "Invalid location of extent of file");
1784 		return (NULL);
1785 	}
1786 	/* Sanity check that location doesn't have a negative value
1787 	 * when the file is not empty. it's too large. */
1788 	if (fsize != 0 && location < 0) {
1789 		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1790 		    "Invalid location of extent of file");
1791 		return (NULL);
1792 	}
1793 
1794 	/* Create a new file entry and copy data from the ISO dir record. */
1795 	file = (struct file_info *)calloc(1, sizeof(*file));
1796 	if (file == NULL) {
1797 		archive_set_error(&a->archive, ENOMEM,
1798 		    "No memory for file entry");
1799 		return (NULL);
1800 	}
1801 	file->parent = parent;
1802 	file->offset = iso9660->logical_block_size * (uint64_t)location;
1803 	file->size = fsize;
1804 	file->mtime = isodate7(isodirrec + DR_date_offset);
1805 	file->ctime = file->atime = file->mtime;
1806 	file->rede_files.first = NULL;
1807 	file->rede_files.last = &(file->rede_files.first);
1808 
1809 	p = isodirrec + DR_name_offset;
1810 	/* Rockridge extensions (if any) follow name.  Compute this
1811 	 * before fidgeting the name_len below. */
1812 	rr_start = p + name_len + (name_len & 1 ? 0 : 1);
1813 	rr_end = isodirrec + dr_len;
1814 
1815 	if (iso9660->seenJoliet) {
1816 		/* Joliet names are max 64 chars (128 bytes) according to spec,
1817 		 * but genisoimage/mkisofs allows recording longer Joliet
1818 		 * names which are 103 UCS2 characters(206 bytes) by their
1819 		 * option '-joliet-long'.
1820 		 */
1821 		if (name_len > 206)
1822 			name_len = 206;
1823 		name_len &= ~1;
1824 
1825 		/* trim trailing first version and dot from filename.
1826 		 *
1827 		 * Remember we were in UTF-16BE land!
1828 		 * SEPARATOR 1 (.) and SEPARATOR 2 (;) are both
1829 		 * 16 bits big endian characters on Joliet.
1830 		 *
1831 		 * TODO: sanitize filename?
1832 		 *       Joliet allows any UCS-2 char except:
1833 		 *       *, /, :, ;, ? and \.
1834 		 */
1835 		/* Chop off trailing ';1' from files. */
1836 		if (name_len > 4 && p[name_len-4] == 0 && p[name_len-3] == ';'
1837 		    && p[name_len-2] == 0 && p[name_len-1] == '1')
1838 			name_len -= 4;
1839 #if 0 /* XXX: this somehow manages to strip of single-character file extensions, like '.c'. */
1840 		/* Chop off trailing '.' from filenames. */
1841 		if (name_len > 2 && p[name_len-2] == 0 && p[name_len-1] == '.')
1842 			name_len -= 2;
1843 #endif
1844 		if ((file->utf16be_name = malloc(name_len)) == NULL) {
1845 			archive_set_error(&a->archive, ENOMEM,
1846 			    "No memory for file name");
1847 			return (NULL);
1848 		}
1849 		memcpy(file->utf16be_name, p, name_len);
1850 		file->utf16be_bytes = name_len;
1851 	} else {
1852 		/* Chop off trailing ';1' from files. */
1853 		if (name_len > 2 && p[name_len - 2] == ';' &&
1854 				p[name_len - 1] == '1')
1855 			name_len -= 2;
1856 		/* Chop off trailing '.' from filenames. */
1857 		if (name_len > 1 && p[name_len - 1] == '.')
1858 			--name_len;
1859 
1860 		archive_strncpy(&file->name, (const char *)p, name_len);
1861 	}
1862 
1863 	flags = isodirrec[DR_flags_offset];
1864 	if (flags & 0x02)
1865 		file->mode = AE_IFDIR | 0700;
1866 	else
1867 		file->mode = AE_IFREG | 0400;
1868 	if (flags & 0x80)
1869 		file->multi_extent = 1;
1870 	else
1871 		file->multi_extent = 0;
1872 	/*
1873 	 * Use a location for the file number, which is treated as an inode
1874 	 * number to find out hardlink target. If Rockridge extensions is
1875 	 * being used, the file number will be overwritten by FILE SERIAL
1876 	 * NUMBER of RRIP "PX" extension.
1877 	 * Note: Old mkisofs did not record that FILE SERIAL NUMBER
1878 	 * in ISO images.
1879 	 * Note2: xorriso set 0 to the location of a symlink file.
1880 	 */
1881 	if (file->size == 0 && location >= 0) {
1882 		/* If file->size is zero, its location points wrong place,
1883 		 * and so we should not use it for the file number.
1884 		 * When the location has negative value, it can be used
1885 		 * for the file number.
1886 		 */
1887 		file->number = -1;
1888 		/* Do not appear before any directory entries. */
1889 		file->offset = -1;
1890 	} else
1891 		file->number = (int64_t)(uint32_t)location;
1892 
1893 	/* Rockridge extensions overwrite information from above. */
1894 	if (iso9660->opt_support_rockridge) {
1895 		if (parent == NULL && rr_end - rr_start >= 7) {
1896 			p = rr_start;
1897 			if (memcmp(p, "SP\x07\x01\xbe\xef", 6) == 0) {
1898 				/*
1899 				 * SP extension stores the suspOffset
1900 				 * (Number of bytes to skip between
1901 				 * filename and SUSP records.)
1902 				 * It is mandatory by the SUSP standard
1903 				 * (IEEE 1281).
1904 				 *
1905 				 * It allows SUSP to coexist with
1906 				 * non-SUSP uses of the System
1907 				 * Use Area by placing non-SUSP data
1908 				 * before SUSP data.
1909 				 *
1910 				 * SP extension must be in the root
1911 				 * directory entry, disable all SUSP
1912 				 * processing if not found.
1913 				 */
1914 				iso9660->suspOffset = p[6];
1915 				iso9660->seenSUSP = 1;
1916 				rr_start += 7;
1917 			}
1918 		}
1919 		if (iso9660->seenSUSP) {
1920 			int r;
1921 
1922 			file->name_continues = 0;
1923 			file->symlink_continues = 0;
1924 			rr_start += iso9660->suspOffset;
1925 			r = parse_rockridge(a, file, rr_start, rr_end);
1926 			if (r != ARCHIVE_OK) {
1927 				free(file);
1928 				return (NULL);
1929 			}
1930 			/*
1931 			 * A file size of symbolic link files in ISO images
1932 			 * made by makefs is not zero and its location is
1933 			 * the same as those of next regular file. That is
1934 			 * the same as hard like file and it causes unexpected
1935 			 * error.
1936 			 */
1937 			if (file->size > 0 &&
1938 			    (file->mode & AE_IFMT) == AE_IFLNK) {
1939 				file->size = 0;
1940 				file->number = -1;
1941 				file->offset = -1;
1942 			}
1943 		} else
1944 			/* If there isn't SUSP, disable parsing
1945 			 * rock ridge extensions. */
1946 			iso9660->opt_support_rockridge = 0;
1947 	}
1948 
1949 	file->nlinks = 1;/* Reset nlink. we'll calculate it later. */
1950 	/* Tell file's parent how many children that parent has. */
1951 	if (parent != NULL && (flags & 0x02))
1952 		parent->subdirs++;
1953 
1954 	if (iso9660->seenRockridge) {
1955 		if (parent != NULL && parent->parent == NULL &&
1956 		    (flags & 0x02) && iso9660->rr_moved == NULL &&
1957 		    file->name.s &&
1958 		    (strcmp(file->name.s, "rr_moved") == 0 ||
1959 		     strcmp(file->name.s, ".rr_moved") == 0)) {
1960 			iso9660->rr_moved = file;
1961 			file->rr_moved = 1;
1962 			file->rr_moved_has_re_only = 1;
1963 			file->re = 0;
1964 			parent->subdirs--;
1965 		} else if (file->re) {
1966 			/*
1967 			 * Sanity check: file's parent is rr_moved.
1968 			 */
1969 			if (parent == NULL || parent->rr_moved == 0) {
1970 				archive_set_error(&a->archive,
1971 				    ARCHIVE_ERRNO_MISC,
1972 				    "Invalid Rockridge RE");
1973 				return (NULL);
1974 			}
1975 			/*
1976 			 * Sanity check: file does not have "CL" extension.
1977 			 */
1978 			if (file->cl_offset) {
1979 				archive_set_error(&a->archive,
1980 				    ARCHIVE_ERRNO_MISC,
1981 				    "Invalid Rockridge RE and CL");
1982 				return (NULL);
1983 			}
1984 			/*
1985 			 * Sanity check: The file type must be a directory.
1986 			 */
1987 			if ((flags & 0x02) == 0) {
1988 				archive_set_error(&a->archive,
1989 				    ARCHIVE_ERRNO_MISC,
1990 				    "Invalid Rockridge RE");
1991 				return (NULL);
1992 			}
1993 		} else if (parent != NULL && parent->rr_moved)
1994 			file->rr_moved_has_re_only = 0;
1995 		else if (parent != NULL && (flags & 0x02) &&
1996 		    (parent->re || parent->re_descendant))
1997 			file->re_descendant = 1;
1998 		if (file->cl_offset) {
1999 			struct file_info *r;
2000 
2001 			if (parent == NULL || parent->parent == NULL) {
2002 				archive_set_error(&a->archive,
2003 				    ARCHIVE_ERRNO_MISC,
2004 				    "Invalid Rockridge CL");
2005 				return (NULL);
2006 			}
2007 			/*
2008 			 * Sanity check: The file type must be a regular file.
2009 			 */
2010 			if ((flags & 0x02) != 0) {
2011 				archive_set_error(&a->archive,
2012 				    ARCHIVE_ERRNO_MISC,
2013 				    "Invalid Rockridge CL");
2014 				return (NULL);
2015 			}
2016 			parent->subdirs++;
2017 			/* Overwrite an offset and a number of this "CL" entry
2018 			 * to appear before other dirs. "+1" to those is to
2019 			 * make sure to appear after "RE" entry which this
2020 			 * "CL" entry should be connected with. */
2021 			file->offset = file->number = file->cl_offset + 1;
2022 
2023 			/*
2024 			 * Sanity check: cl_offset does not point at its
2025 			 * the parents or itself.
2026 			 */
2027 			for (r = parent; r; r = r->parent) {
2028 				if (r->offset == file->cl_offset) {
2029 					archive_set_error(&a->archive,
2030 					    ARCHIVE_ERRNO_MISC,
2031 					    "Invalid Rockridge CL");
2032 					return (NULL);
2033 				}
2034 			}
2035 			if (file->cl_offset == file->offset ||
2036 			    parent->rr_moved) {
2037 				archive_set_error(&a->archive,
2038 				    ARCHIVE_ERRNO_MISC,
2039 				    "Invalid Rockridge CL");
2040 				return (NULL);
2041 			}
2042 		}
2043 	}
2044 
2045 #if DEBUG
2046 	/* DEBUGGING: Warn about attributes I don't yet fully support. */
2047 	if ((flags & ~0x02) != 0) {
2048 		fprintf(stderr, "\n ** Unrecognized flag: ");
2049 		dump_isodirrec(stderr, isodirrec);
2050 		fprintf(stderr, "\n");
2051 	} else if (toi(isodirrec + DR_volume_sequence_number_offset, 2) != 1) {
2052 		fprintf(stderr, "\n ** Unrecognized sequence number: ");
2053 		dump_isodirrec(stderr, isodirrec);
2054 		fprintf(stderr, "\n");
2055 	} else if (*(isodirrec + DR_file_unit_size_offset) != 0) {
2056 		fprintf(stderr, "\n ** Unexpected file unit size: ");
2057 		dump_isodirrec(stderr, isodirrec);
2058 		fprintf(stderr, "\n");
2059 	} else if (*(isodirrec + DR_interleave_offset) != 0) {
2060 		fprintf(stderr, "\n ** Unexpected interleave: ");
2061 		dump_isodirrec(stderr, isodirrec);
2062 		fprintf(stderr, "\n");
2063 	} else if (*(isodirrec + DR_ext_attr_length_offset) != 0) {
2064 		fprintf(stderr, "\n ** Unexpected extended attribute length: ");
2065 		dump_isodirrec(stderr, isodirrec);
2066 		fprintf(stderr, "\n");
2067 	}
2068 #endif
2069 	register_file(iso9660, file);
2070 	return (file);
2071 }
2072 
2073 static int
2074 parse_rockridge(struct archive_read *a, struct file_info *file,
2075     const unsigned char *p, const unsigned char *end)
2076 {
2077 	struct iso9660 *iso9660;
2078 
2079 	iso9660 = (struct iso9660 *)(a->format->data);
2080 
2081 	while (p + 4 <= end  /* Enough space for another entry. */
2082 	    && p[0] >= 'A' && p[0] <= 'Z' /* Sanity-check 1st char of name. */
2083 	    && p[1] >= 'A' && p[1] <= 'Z' /* Sanity-check 2nd char of name. */
2084 	    && p[2] >= 4 /* Sanity-check length. */
2085 	    && p + p[2] <= end) { /* Sanity-check length. */
2086 		const unsigned char *data = p + 4;
2087 		int data_length = p[2] - 4;
2088 		int version = p[3];
2089 
2090 		switch(p[0]) {
2091 		case 'C':
2092 			if (p[1] == 'E') {
2093 				if (version == 1 && data_length == 24) {
2094 					/*
2095 					 * CE extension comprises:
2096 					 *   8 byte sector containing extension
2097 					 *   8 byte offset w/in above sector
2098 					 *   8 byte length of continuation
2099 					 */
2100 					int32_t location =
2101 					    archive_le32dec(data);
2102 					file->ce_offset =
2103 					    archive_le32dec(data+8);
2104 					file->ce_size =
2105 					    archive_le32dec(data+16);
2106 					if (register_CE(a, location, file)
2107 					    != ARCHIVE_OK)
2108 						return (ARCHIVE_FATAL);
2109 				}
2110 			}
2111 			else if (p[1] == 'L') {
2112 				if (version == 1 && data_length == 8) {
2113 					file->cl_offset = (uint64_t)
2114 					    iso9660->logical_block_size *
2115 					    (uint64_t)archive_le32dec(data);
2116 					iso9660->seenRockridge = 1;
2117 				}
2118 			}
2119 			break;
2120 		case 'N':
2121 			if (p[1] == 'M') {
2122 				if (version == 1) {
2123 					parse_rockridge_NM1(file,
2124 					    data, data_length);
2125 					iso9660->seenRockridge = 1;
2126 				}
2127 			}
2128 			break;
2129 		case 'P':
2130 			/*
2131 			 * PD extension is padding;
2132 			 * contents are always ignored.
2133 			 *
2134 			 * PL extension won't appear;
2135 			 * contents are always ignored.
2136 			 */
2137 			if (p[1] == 'N') {
2138 				if (version == 1 && data_length == 16) {
2139 					file->rdev = toi(data,4);
2140 					file->rdev <<= 32;
2141 					file->rdev |= toi(data + 8, 4);
2142 					iso9660->seenRockridge = 1;
2143 				}
2144 			}
2145 			else if (p[1] == 'X') {
2146 				/*
2147 				 * PX extension comprises:
2148 				 *   8 bytes for mode,
2149 				 *   8 bytes for nlinks,
2150 				 *   8 bytes for uid,
2151 				 *   8 bytes for gid,
2152 				 *   8 bytes for inode.
2153 				 */
2154 				if (version == 1) {
2155 					if (data_length >= 8)
2156 						file->mode
2157 						    = toi(data, 4);
2158 					if (data_length >= 16)
2159 						file->nlinks
2160 						    = toi(data + 8, 4);
2161 					if (data_length >= 24)
2162 						file->uid
2163 						    = toi(data + 16, 4);
2164 					if (data_length >= 32)
2165 						file->gid
2166 						    = toi(data + 24, 4);
2167 					if (data_length >= 40)
2168 						file->number
2169 						    = toi(data + 32, 4);
2170 					iso9660->seenRockridge = 1;
2171 				}
2172 			}
2173 			break;
2174 		case 'R':
2175 			if (p[1] == 'E' && version == 1) {
2176 				file->re = 1;
2177 				iso9660->seenRockridge = 1;
2178 			}
2179 			else if (p[1] == 'R' && version == 1) {
2180 				/*
2181 				 * RR extension comprises:
2182 				 *    one byte flag value
2183 				 * This extension is obsolete,
2184 				 * so contents are always ignored.
2185 				 */
2186 			}
2187 			break;
2188 		case 'S':
2189 			if (p[1] == 'L') {
2190 				if (version == 1) {
2191 					parse_rockridge_SL1(file,
2192 					    data, data_length);
2193 					iso9660->seenRockridge = 1;
2194 				}
2195 			}
2196 			else if (p[1] == 'T'
2197 			    && data_length == 0 && version == 1) {
2198 				/*
2199 				 * ST extension marks end of this
2200 				 * block of SUSP entries.
2201 				 *
2202 				 * It allows SUSP to coexist with
2203 				 * non-SUSP uses of the System
2204 				 * Use Area by placing non-SUSP data
2205 				 * after SUSP data.
2206 				 */
2207 				iso9660->seenSUSP = 0;
2208 				iso9660->seenRockridge = 0;
2209 				return (ARCHIVE_OK);
2210 			}
2211 			break;
2212 		case 'T':
2213 			if (p[1] == 'F') {
2214 				if (version == 1) {
2215 					parse_rockridge_TF1(file,
2216 					    data, data_length);
2217 					iso9660->seenRockridge = 1;
2218 				}
2219 			}
2220 			break;
2221 		case 'Z':
2222 			if (p[1] == 'F') {
2223 				if (version == 1)
2224 					parse_rockridge_ZF1(file,
2225 					    data, data_length);
2226 			}
2227 			break;
2228 		default:
2229 			break;
2230 		}
2231 
2232 		p += p[2];
2233 	}
2234 	return (ARCHIVE_OK);
2235 }
2236 
2237 static int
2238 register_CE(struct archive_read *a, int32_t location,
2239     struct file_info *file)
2240 {
2241 	struct iso9660 *iso9660;
2242 	struct read_ce_queue *heap;
2243 	struct read_ce_req *p;
2244 	uint64_t offset, parent_offset;
2245 	int hole, parent;
2246 
2247 	iso9660 = (struct iso9660 *)(a->format->data);
2248 	offset = ((uint64_t)location) * (uint64_t)iso9660->logical_block_size;
2249 	if (((file->mode & AE_IFMT) == AE_IFREG &&
2250 	    offset >= file->offset) ||
2251 	    offset < iso9660->current_position ||
2252 	    (((uint64_t)file->ce_offset) + file->ce_size)
2253 	      > (uint64_t)iso9660->logical_block_size ||
2254 	    offset + file->ce_offset + file->ce_size
2255 		  > iso9660->volume_size) {
2256 		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
2257 		    "Invalid parameter in SUSP \"CE\" extension");
2258 		return (ARCHIVE_FATAL);
2259 	}
2260 
2261 	/* Expand our CE list as necessary. */
2262 	heap = &(iso9660->read_ce_req);
2263 	if (heap->cnt >= heap->allocated) {
2264 		int new_size;
2265 
2266 		if (heap->allocated < 16)
2267 			new_size = 16;
2268 		else
2269 			new_size = heap->allocated * 2;
2270 		/* Overflow might keep us from growing the list. */
2271 		if (new_size <= heap->allocated) {
2272 			archive_set_error(&a->archive, ENOMEM, "Out of memory");
2273 			return (ARCHIVE_FATAL);
2274 		}
2275 		p = calloc(new_size, sizeof(p[0]));
2276 		if (p == NULL) {
2277 			archive_set_error(&a->archive, ENOMEM, "Out of memory");
2278 			return (ARCHIVE_FATAL);
2279 		}
2280 		if (heap->reqs != NULL) {
2281 			memcpy(p, heap->reqs, heap->cnt * sizeof(*p));
2282 			free(heap->reqs);
2283 		}
2284 		heap->reqs = p;
2285 		heap->allocated = new_size;
2286 	}
2287 
2288 	/*
2289 	 * Start with hole at end, walk it up tree to find insertion point.
2290 	 */
2291 	hole = heap->cnt++;
2292 	while (hole > 0) {
2293 		parent = (hole - 1)/2;
2294 		parent_offset = heap->reqs[parent].offset;
2295 		if (offset >= parent_offset) {
2296 			heap->reqs[hole].offset = offset;
2297 			heap->reqs[hole].file = file;
2298 			return (ARCHIVE_OK);
2299 		}
2300 		/* Move parent into hole <==> move hole up tree. */
2301 		heap->reqs[hole] = heap->reqs[parent];
2302 		hole = parent;
2303 	}
2304 	heap->reqs[0].offset = offset;
2305 	heap->reqs[0].file = file;
2306 	return (ARCHIVE_OK);
2307 }
2308 
2309 static void
2310 next_CE(struct read_ce_queue *heap)
2311 {
2312 	uint64_t a_offset, b_offset, c_offset;
2313 	int a, b, c;
2314 	struct read_ce_req tmp;
2315 
2316 	if (heap->cnt < 1)
2317 		return;
2318 
2319 	/*
2320 	 * Move the last item in the heap to the root of the tree
2321 	 */
2322 	heap->reqs[0] = heap->reqs[--(heap->cnt)];
2323 
2324 	/*
2325 	 * Rebalance the heap.
2326 	 */
2327 	a = 0; /* Starting element and its offset */
2328 	a_offset = heap->reqs[a].offset;
2329 	for (;;) {
2330 		b = a + a + 1; /* First child */
2331 		if (b >= heap->cnt)
2332 			return;
2333 		b_offset = heap->reqs[b].offset;
2334 		c = b + 1; /* Use second child if it is smaller. */
2335 		if (c < heap->cnt) {
2336 			c_offset = heap->reqs[c].offset;
2337 			if (c_offset < b_offset) {
2338 				b = c;
2339 				b_offset = c_offset;
2340 			}
2341 		}
2342 		if (a_offset <= b_offset)
2343 			return;
2344 		tmp = heap->reqs[a];
2345 		heap->reqs[a] = heap->reqs[b];
2346 		heap->reqs[b] = tmp;
2347 		a = b;
2348 	}
2349 }
2350 
2351 
2352 static int
2353 read_CE(struct archive_read *a, struct iso9660 *iso9660)
2354 {
2355 	struct read_ce_queue *heap;
2356 	const unsigned char *b, *p, *end;
2357 	struct file_info *file;
2358 	size_t step;
2359 	int r;
2360 
2361 	/* Read data which RRIP "CE" extension points. */
2362 	heap = &(iso9660->read_ce_req);
2363 	step = iso9660->logical_block_size;
2364 	while (heap->cnt &&
2365 	    heap->reqs[0].offset == iso9660->current_position) {
2366 		b = __archive_read_ahead(a, step, NULL);
2367 		if (b == NULL) {
2368 			archive_set_error(&a->archive,
2369 			    ARCHIVE_ERRNO_MISC,
2370 			    "Failed to read full block when scanning "
2371 			    "ISO9660 directory list");
2372 			return (ARCHIVE_FATAL);
2373 		}
2374 		do {
2375 			file = heap->reqs[0].file;
2376 			if (file->ce_offset + file->ce_size > step) {
2377 				archive_set_error(&a->archive,
2378 				    ARCHIVE_ERRNO_FILE_FORMAT,
2379 				    "Malformed CE information");
2380 				return (ARCHIVE_FATAL);
2381 			}
2382 			p = b + file->ce_offset;
2383 			end = p + file->ce_size;
2384 			next_CE(heap);
2385 			r = parse_rockridge(a, file, p, end);
2386 			if (r != ARCHIVE_OK)
2387 				return (ARCHIVE_FATAL);
2388 		} while (heap->cnt &&
2389 		    heap->reqs[0].offset == iso9660->current_position);
2390 		/* NOTE: Do not move this consume's code to fron of
2391 		 * do-while loop. Registration of nested CE extension
2392 		 * might cause error because of current position. */
2393 		__archive_read_consume(a, step);
2394 		iso9660->current_position += step;
2395 	}
2396 	return (ARCHIVE_OK);
2397 }
2398 
2399 static void
2400 parse_rockridge_NM1(struct file_info *file,
2401 		    const unsigned char *data, int data_length)
2402 {
2403 	if (!file->name_continues)
2404 		archive_string_empty(&file->name);
2405 	file->name_continues = 0;
2406 	if (data_length < 1)
2407 		return;
2408 	/*
2409 	 * NM version 1 extension comprises:
2410 	 *   1 byte flag, value is one of:
2411 	 *     = 0: remainder is name
2412 	 *     = 1: remainder is name, next NM entry continues name
2413 	 *     = 2: "."
2414 	 *     = 4: ".."
2415 	 *     = 32: Implementation specific
2416 	 *     All other values are reserved.
2417 	 */
2418 	switch(data[0]) {
2419 	case 0:
2420 		if (data_length < 2)
2421 			return;
2422 		archive_strncat(&file->name,
2423 		    (const char *)data + 1, data_length - 1);
2424 		break;
2425 	case 1:
2426 		if (data_length < 2)
2427 			return;
2428 		archive_strncat(&file->name,
2429 		    (const char *)data + 1, data_length - 1);
2430 		file->name_continues = 1;
2431 		break;
2432 	case 2:
2433 		archive_strcat(&file->name, ".");
2434 		break;
2435 	case 4:
2436 		archive_strcat(&file->name, "..");
2437 		break;
2438 	default:
2439 		return;
2440 	}
2441 
2442 }
2443 
2444 static void
2445 parse_rockridge_TF1(struct file_info *file, const unsigned char *data,
2446     int data_length)
2447 {
2448 	char flag;
2449 	/*
2450 	 * TF extension comprises:
2451 	 *   one byte flag
2452 	 *   create time (optional)
2453 	 *   modify time (optional)
2454 	 *   access time (optional)
2455 	 *   attribute time (optional)
2456 	 *  Time format and presence of fields
2457 	 *  is controlled by flag bits.
2458 	 */
2459 	if (data_length < 1)
2460 		return;
2461 	flag = data[0];
2462 	++data;
2463 	--data_length;
2464 	if (flag & 0x80) {
2465 		/* Use 17-byte time format. */
2466 		if ((flag & 1) && data_length >= 17) {
2467 			/* Create time. */
2468 			file->birthtime_is_set = 1;
2469 			file->birthtime = isodate17(data);
2470 			data += 17;
2471 			data_length -= 17;
2472 		}
2473 		if ((flag & 2) && data_length >= 17) {
2474 			/* Modify time. */
2475 			file->mtime = isodate17(data);
2476 			data += 17;
2477 			data_length -= 17;
2478 		}
2479 		if ((flag & 4) && data_length >= 17) {
2480 			/* Access time. */
2481 			file->atime = isodate17(data);
2482 			data += 17;
2483 			data_length -= 17;
2484 		}
2485 		if ((flag & 8) && data_length >= 17) {
2486 			/* Attribute change time. */
2487 			file->ctime = isodate17(data);
2488 		}
2489 	} else {
2490 		/* Use 7-byte time format. */
2491 		if ((flag & 1) && data_length >= 7) {
2492 			/* Create time. */
2493 			file->birthtime_is_set = 1;
2494 			file->birthtime = isodate7(data);
2495 			data += 7;
2496 			data_length -= 7;
2497 		}
2498 		if ((flag & 2) && data_length >= 7) {
2499 			/* Modify time. */
2500 			file->mtime = isodate7(data);
2501 			data += 7;
2502 			data_length -= 7;
2503 		}
2504 		if ((flag & 4) && data_length >= 7) {
2505 			/* Access time. */
2506 			file->atime = isodate7(data);
2507 			data += 7;
2508 			data_length -= 7;
2509 		}
2510 		if ((flag & 8) && data_length >= 7) {
2511 			/* Attribute change time. */
2512 			file->ctime = isodate7(data);
2513 		}
2514 	}
2515 }
2516 
2517 static void
2518 parse_rockridge_SL1(struct file_info *file, const unsigned char *data,
2519     int data_length)
2520 {
2521 	const char *separator = "";
2522 
2523 	if (!file->symlink_continues || file->symlink.length < 1)
2524 		archive_string_empty(&file->symlink);
2525 	file->symlink_continues = 0;
2526 
2527 	/*
2528 	 * Defined flag values:
2529 	 *  0: This is the last SL record for this symbolic link
2530 	 *  1: this symbolic link field continues in next SL entry
2531 	 *  All other values are reserved.
2532 	 */
2533 	if (data_length < 1)
2534 		return;
2535 	switch(*data) {
2536 	case 0:
2537 		break;
2538 	case 1:
2539 		file->symlink_continues = 1;
2540 		break;
2541 	default:
2542 		return;
2543 	}
2544 	++data;  /* Skip flag byte. */
2545 	--data_length;
2546 
2547 	/*
2548 	 * SL extension body stores "components".
2549 	 * Basically, this is a complicated way of storing
2550 	 * a POSIX path.  It also interferes with using
2551 	 * symlinks for storing non-path data. <sigh>
2552 	 *
2553 	 * Each component is 2 bytes (flag and length)
2554 	 * possibly followed by name data.
2555 	 */
2556 	while (data_length >= 2) {
2557 		unsigned char flag = *data++;
2558 		unsigned char nlen = *data++;
2559 		data_length -= 2;
2560 
2561 		archive_strcat(&file->symlink, separator);
2562 		separator = "/";
2563 
2564 		switch(flag) {
2565 		case 0: /* Usual case, this is text. */
2566 			if (data_length < nlen)
2567 				return;
2568 			archive_strncat(&file->symlink,
2569 			    (const char *)data, nlen);
2570 			break;
2571 		case 0x01: /* Text continues in next component. */
2572 			if (data_length < nlen)
2573 				return;
2574 			archive_strncat(&file->symlink,
2575 			    (const char *)data, nlen);
2576 			separator = "";
2577 			break;
2578 		case 0x02: /* Current dir. */
2579 			archive_strcat(&file->symlink, ".");
2580 			break;
2581 		case 0x04: /* Parent dir. */
2582 			archive_strcat(&file->symlink, "..");
2583 			break;
2584 		case 0x08: /* Root of filesystem. */
2585 			archive_strcat(&file->symlink, "/");
2586 			separator = "";
2587 			break;
2588 		case 0x10: /* Undefined (historically "volume root" */
2589 			archive_string_empty(&file->symlink);
2590 			archive_strcat(&file->symlink, "ROOT");
2591 			break;
2592 		case 0x20: /* Undefined (historically "hostname") */
2593 			archive_strcat(&file->symlink, "hostname");
2594 			break;
2595 		default:
2596 			/* TODO: issue a warning ? */
2597 			return;
2598 		}
2599 		data += nlen;
2600 		data_length -= nlen;
2601 	}
2602 }
2603 
2604 static void
2605 parse_rockridge_ZF1(struct file_info *file, const unsigned char *data,
2606     int data_length)
2607 {
2608 
2609 	if (data[0] == 0x70 && data[1] == 0x7a && data_length == 12) {
2610 		/* paged zlib */
2611 		file->pz = 1;
2612 		file->pz_log2_bs = data[3];
2613 		file->pz_uncompressed_size = archive_le32dec(&data[4]);
2614 	}
2615 }
2616 
2617 static void
2618 register_file(struct iso9660 *iso9660, struct file_info *file)
2619 {
2620 
2621 	file->use_next = iso9660->use_files;
2622 	iso9660->use_files = file;
2623 }
2624 
2625 static void
2626 release_files(struct iso9660 *iso9660)
2627 {
2628 	struct content *con, *connext;
2629 	struct file_info *file;
2630 
2631 	file = iso9660->use_files;
2632 	while (file != NULL) {
2633 		struct file_info *next = file->use_next;
2634 
2635 		archive_string_free(&file->name);
2636 		archive_string_free(&file->symlink);
2637 		free(file->utf16be_name);
2638 		con = file->contents.first;
2639 		while (con != NULL) {
2640 			connext = con->next;
2641 			free(con);
2642 			con = connext;
2643 		}
2644 		free(file);
2645 		file = next;
2646 	}
2647 }
2648 
2649 static int
2650 next_entry_seek(struct archive_read *a, struct iso9660 *iso9660,
2651     struct file_info **pfile)
2652 {
2653 	struct file_info *file;
2654 	int r;
2655 
2656 	r = next_cache_entry(a, iso9660, pfile);
2657 	if (r != ARCHIVE_OK)
2658 		return (r);
2659 	file = *pfile;
2660 
2661 	/* Don't waste time seeking for zero-length bodies. */
2662 	if (file->size == 0)
2663 		file->offset = iso9660->current_position;
2664 
2665 	/* flush any remaining bytes from the last round to ensure
2666 	 * we're positioned */
2667 	if (iso9660->entry_bytes_unconsumed) {
2668 		__archive_read_consume(a, iso9660->entry_bytes_unconsumed);
2669 		iso9660->entry_bytes_unconsumed = 0;
2670 	}
2671 
2672 	/* Seek forward to the start of the entry. */
2673 	if (iso9660->current_position < file->offset) {
2674 		int64_t step;
2675 
2676 		step = file->offset - iso9660->current_position;
2677 		step = __archive_read_consume(a, step);
2678 		if (step < 0)
2679 			return ((int)step);
2680 		iso9660->current_position = file->offset;
2681 	}
2682 
2683 	/* We found body of file; handle it now. */
2684 	return (ARCHIVE_OK);
2685 }
2686 
2687 static int
2688 next_cache_entry(struct archive_read *a, struct iso9660 *iso9660,
2689     struct file_info **pfile)
2690 {
2691 	struct file_info *file;
2692 	struct {
2693 		struct file_info	*first;
2694 		struct file_info	**last;
2695 	}	empty_files;
2696 	int64_t number;
2697 	int count;
2698 
2699 	file = cache_get_entry(iso9660);
2700 	if (file != NULL) {
2701 		*pfile = file;
2702 		return (ARCHIVE_OK);
2703 	}
2704 
2705 	for (;;) {
2706 		struct file_info *re, *d;
2707 
2708 		*pfile = file = next_entry(iso9660);
2709 		if (file == NULL) {
2710 			/*
2711 			 * If directory entries all which are descendant of
2712 			 * rr_moved are stil remaning, expose their.
2713 			 */
2714 			if (iso9660->re_files.first != NULL &&
2715 			    iso9660->rr_moved != NULL &&
2716 			    iso9660->rr_moved->rr_moved_has_re_only)
2717 				/* Expose "rr_moved" entry. */
2718 				cache_add_entry(iso9660, iso9660->rr_moved);
2719 			while ((re = re_get_entry(iso9660)) != NULL) {
2720 				/* Expose its descendant dirs. */
2721 				while ((d = rede_get_entry(re)) != NULL)
2722 					cache_add_entry(iso9660, d);
2723 			}
2724 			if (iso9660->cache_files.first != NULL)
2725 				return (next_cache_entry(a, iso9660, pfile));
2726 			return (ARCHIVE_EOF);
2727 		}
2728 
2729 		if (file->cl_offset) {
2730 			struct file_info *first_re = NULL;
2731 			int nexted_re = 0;
2732 
2733 			/*
2734 			 * Find "RE" dir for the current file, which
2735 			 * has "CL" flag.
2736 			 */
2737 			while ((re = re_get_entry(iso9660))
2738 			    != first_re) {
2739 				if (first_re == NULL)
2740 					first_re = re;
2741 				if (re->offset == file->cl_offset) {
2742 					re->parent->subdirs--;
2743 					re->parent = file->parent;
2744 					re->re = 0;
2745 					if (re->parent->re_descendant) {
2746 						nexted_re = 1;
2747 						re->re_descendant = 1;
2748 						if (rede_add_entry(re) < 0)
2749 							goto fatal_rr;
2750 						/* Move a list of descendants
2751 						 * to a new ancestor. */
2752 						while ((d = rede_get_entry(
2753 						    re)) != NULL)
2754 							if (rede_add_entry(d)
2755 							    < 0)
2756 								goto fatal_rr;
2757 						break;
2758 					}
2759 					/* Replace the current file
2760 					 * with "RE" dir */
2761 					*pfile = file = re;
2762 					/* Expose its descendant */
2763 					while ((d = rede_get_entry(
2764 					    file)) != NULL)
2765 						cache_add_entry(
2766 						    iso9660, d);
2767 					break;
2768 				} else
2769 					re_add_entry(iso9660, re);
2770 			}
2771 			if (nexted_re) {
2772 				/*
2773 				 * Do not expose this at this time
2774 				 * because we have not gotten its full-path
2775 				 * name yet.
2776 				 */
2777 				continue;
2778 			}
2779 		} else if ((file->mode & AE_IFMT) == AE_IFDIR) {
2780 			int r;
2781 
2782 			/* Read file entries in this dir. */
2783 			r = read_children(a, file);
2784 			if (r != ARCHIVE_OK)
2785 				return (r);
2786 
2787 			/*
2788 			 * Handle a special dir of Rockridge extensions,
2789 			 * "rr_moved".
2790 			 */
2791 			if (file->rr_moved) {
2792 				/*
2793 				 * If this has only the subdirectories which
2794 				 * have "RE" flags, do not expose at this time.
2795 				 */
2796 				if (file->rr_moved_has_re_only)
2797 					continue;
2798 				/* Otherwise expose "rr_moved" entry. */
2799 			} else if (file->re) {
2800 				/*
2801 				 * Do not expose this at this time
2802 				 * because we have not gotten its full-path
2803 				 * name yet.
2804 				 */
2805 				re_add_entry(iso9660, file);
2806 				continue;
2807 			} else if (file->re_descendant) {
2808 				/*
2809 				 * If the top level "RE" entry of this entry
2810 				 * is not exposed, we, accordingly, should not
2811 				 * expose this entry at this time because
2812 				 * we cannot make its proper full-path name.
2813 				 */
2814 				if (rede_add_entry(file) == 0)
2815 					continue;
2816 				/* Otherwise we can expose this entry because
2817 				 * it seems its top level "RE" has already been
2818 				 * exposed. */
2819 			}
2820 		}
2821 		break;
2822 	}
2823 
2824 	if ((file->mode & AE_IFMT) != AE_IFREG || file->number == -1)
2825 		return (ARCHIVE_OK);
2826 
2827 	count = 0;
2828 	number = file->number;
2829 	iso9660->cache_files.first = NULL;
2830 	iso9660->cache_files.last = &(iso9660->cache_files.first);
2831 	empty_files.first = NULL;
2832 	empty_files.last = &empty_files.first;
2833 	/* Collect files which has the same file serial number.
2834 	 * Peek pending_files so that file which number is different
2835 	 * is not put bak. */
2836 	while (iso9660->pending_files.used > 0 &&
2837 	    (iso9660->pending_files.files[0]->number == -1 ||
2838 	     iso9660->pending_files.files[0]->number == number)) {
2839 		if (file->number == -1) {
2840 			/* This file has the same offset
2841 			 * but it's wrong offset which empty files
2842 			 * and symlink files have.
2843 			 * NOTE: This wrong offse was recorded by
2844 			 * old mkisofs utility. If ISO images is
2845 			 * created by latest mkisofs, this does not
2846 			 * happen.
2847 			 */
2848 			file->next = NULL;
2849 			*empty_files.last = file;
2850 			empty_files.last = &(file->next);
2851 		} else {
2852 			count++;
2853 			cache_add_entry(iso9660, file);
2854 		}
2855 		file = next_entry(iso9660);
2856 	}
2857 
2858 	if (count == 0) {
2859 		*pfile = file;
2860 		return ((file == NULL)?ARCHIVE_EOF:ARCHIVE_OK);
2861 	}
2862 	if (file->number == -1) {
2863 		file->next = NULL;
2864 		*empty_files.last = file;
2865 		empty_files.last = &(file->next);
2866 	} else {
2867 		count++;
2868 		cache_add_entry(iso9660, file);
2869 	}
2870 
2871 	if (count > 1) {
2872 		/* The count is the same as number of hardlink,
2873 		 * so much so that each nlinks of files in cache_file
2874 		 * is overwritten by value of the count.
2875 		 */
2876 		for (file = iso9660->cache_files.first;
2877 		    file != NULL; file = file->next)
2878 			file->nlinks = count;
2879 	}
2880 	/* If there are empty files, that files are added
2881 	 * to the tail of the cache_files. */
2882 	if (empty_files.first != NULL) {
2883 		*iso9660->cache_files.last = empty_files.first;
2884 		iso9660->cache_files.last = empty_files.last;
2885 	}
2886 	*pfile = cache_get_entry(iso9660);
2887 	return ((*pfile == NULL)?ARCHIVE_EOF:ARCHIVE_OK);
2888 
2889 fatal_rr:
2890 	archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
2891 	    "Failed to connect 'CL' pointer to 'RE' rr_moved pointer of "
2892 	    "Rockridge extensions: current position = %jd, CL offset = %jd",
2893 	    (intmax_t)iso9660->current_position, (intmax_t)file->cl_offset);
2894 	return (ARCHIVE_FATAL);
2895 }
2896 
2897 static inline void
2898 re_add_entry(struct iso9660 *iso9660, struct file_info *file)
2899 {
2900 	file->re_next = NULL;
2901 	*iso9660->re_files.last = file;
2902 	iso9660->re_files.last = &(file->re_next);
2903 }
2904 
2905 static inline struct file_info *
2906 re_get_entry(struct iso9660 *iso9660)
2907 {
2908 	struct file_info *file;
2909 
2910 	if ((file = iso9660->re_files.first) != NULL) {
2911 		iso9660->re_files.first = file->re_next;
2912 		if (iso9660->re_files.first == NULL)
2913 			iso9660->re_files.last =
2914 			    &(iso9660->re_files.first);
2915 	}
2916 	return (file);
2917 }
2918 
2919 static inline int
2920 rede_add_entry(struct file_info *file)
2921 {
2922 	struct file_info *re;
2923 
2924 	/*
2925 	 * Find "RE" entry.
2926 	 */
2927 	re = file->parent;
2928 	while (re != NULL && !re->re)
2929 		re = re->parent;
2930 	if (re == NULL)
2931 		return (-1);
2932 
2933 	file->re_next = NULL;
2934 	*re->rede_files.last = file;
2935 	re->rede_files.last = &(file->re_next);
2936 	return (0);
2937 }
2938 
2939 static inline struct file_info *
2940 rede_get_entry(struct file_info *re)
2941 {
2942 	struct file_info *file;
2943 
2944 	if ((file = re->rede_files.first) != NULL) {
2945 		re->rede_files.first = file->re_next;
2946 		if (re->rede_files.first == NULL)
2947 			re->rede_files.last =
2948 			    &(re->rede_files.first);
2949 	}
2950 	return (file);
2951 }
2952 
2953 static inline void
2954 cache_add_entry(struct iso9660 *iso9660, struct file_info *file)
2955 {
2956 	file->next = NULL;
2957 	*iso9660->cache_files.last = file;
2958 	iso9660->cache_files.last = &(file->next);
2959 }
2960 
2961 static inline struct file_info *
2962 cache_get_entry(struct iso9660 *iso9660)
2963 {
2964 	struct file_info *file;
2965 
2966 	if ((file = iso9660->cache_files.first) != NULL) {
2967 		iso9660->cache_files.first = file->next;
2968 		if (iso9660->cache_files.first == NULL)
2969 			iso9660->cache_files.last =
2970 			    &(iso9660->cache_files.first);
2971 	}
2972 	return (file);
2973 }
2974 
2975 static int
2976 heap_add_entry(struct archive_read *a, struct heap_queue *heap,
2977     struct file_info *file, uint64_t key)
2978 {
2979 	uint64_t file_key, parent_key;
2980 	int hole, parent;
2981 
2982 	/* Expand our pending files list as necessary. */
2983 	if (heap->used >= heap->allocated) {
2984 		struct file_info **new_pending_files;
2985 		int new_size = heap->allocated * 2;
2986 
2987 		if (heap->allocated < 1024)
2988 			new_size = 1024;
2989 		/* Overflow might keep us from growing the list. */
2990 		if (new_size <= heap->allocated) {
2991 			archive_set_error(&a->archive,
2992 			    ENOMEM, "Out of memory");
2993 			return (ARCHIVE_FATAL);
2994 		}
2995 		new_pending_files = (struct file_info **)
2996 		    malloc(new_size * sizeof(new_pending_files[0]));
2997 		if (new_pending_files == NULL) {
2998 			archive_set_error(&a->archive,
2999 			    ENOMEM, "Out of memory");
3000 			return (ARCHIVE_FATAL);
3001 		}
3002 		memcpy(new_pending_files, heap->files,
3003 		    heap->allocated * sizeof(new_pending_files[0]));
3004 		if (heap->files != NULL)
3005 			free(heap->files);
3006 		heap->files = new_pending_files;
3007 		heap->allocated = new_size;
3008 	}
3009 
3010 	file_key = file->key = key;
3011 
3012 	/*
3013 	 * Start with hole at end, walk it up tree to find insertion point.
3014 	 */
3015 	hole = heap->used++;
3016 	while (hole > 0) {
3017 		parent = (hole - 1)/2;
3018 		parent_key = heap->files[parent]->key;
3019 		if (file_key >= parent_key) {
3020 			heap->files[hole] = file;
3021 			return (ARCHIVE_OK);
3022 		}
3023 		/* Move parent into hole <==> move hole up tree. */
3024 		heap->files[hole] = heap->files[parent];
3025 		hole = parent;
3026 	}
3027 	heap->files[0] = file;
3028 
3029 	return (ARCHIVE_OK);
3030 }
3031 
3032 static struct file_info *
3033 heap_get_entry(struct heap_queue *heap)
3034 {
3035 	uint64_t a_key, b_key, c_key;
3036 	int a, b, c;
3037 	struct file_info *r, *tmp;
3038 
3039 	if (heap->used < 1)
3040 		return (NULL);
3041 
3042 	/*
3043 	 * The first file in the list is the earliest; we'll return this.
3044 	 */
3045 	r = heap->files[0];
3046 
3047 	/*
3048 	 * Move the last item in the heap to the root of the tree
3049 	 */
3050 	heap->files[0] = heap->files[--(heap->used)];
3051 
3052 	/*
3053 	 * Rebalance the heap.
3054 	 */
3055 	a = 0; /* Starting element and its heap key */
3056 	a_key = heap->files[a]->key;
3057 	for (;;) {
3058 		b = a + a + 1; /* First child */
3059 		if (b >= heap->used)
3060 			return (r);
3061 		b_key = heap->files[b]->key;
3062 		c = b + 1; /* Use second child if it is smaller. */
3063 		if (c < heap->used) {
3064 			c_key = heap->files[c]->key;
3065 			if (c_key < b_key) {
3066 				b = c;
3067 				b_key = c_key;
3068 			}
3069 		}
3070 		if (a_key <= b_key)
3071 			return (r);
3072 		tmp = heap->files[a];
3073 		heap->files[a] = heap->files[b];
3074 		heap->files[b] = tmp;
3075 		a = b;
3076 	}
3077 }
3078 
3079 static unsigned int
3080 toi(const void *p, int n)
3081 {
3082 	const unsigned char *v = (const unsigned char *)p;
3083 	if (n > 1)
3084 		return v[0] + 256 * toi(v + 1, n - 1);
3085 	if (n == 1)
3086 		return v[0];
3087 	return (0);
3088 }
3089 
3090 static time_t
3091 isodate7(const unsigned char *v)
3092 {
3093 	struct tm tm;
3094 	int offset;
3095 	time_t t;
3096 
3097 	memset(&tm, 0, sizeof(tm));
3098 	tm.tm_year = v[0];
3099 	tm.tm_mon = v[1] - 1;
3100 	tm.tm_mday = v[2];
3101 	tm.tm_hour = v[3];
3102 	tm.tm_min = v[4];
3103 	tm.tm_sec = v[5];
3104 	/* v[6] is the signed timezone offset, in 1/4-hour increments. */
3105 	offset = ((const signed char *)v)[6];
3106 	if (offset > -48 && offset < 52) {
3107 		tm.tm_hour -= offset / 4;
3108 		tm.tm_min -= (offset % 4) * 15;
3109 	}
3110 	t = time_from_tm(&tm);
3111 	if (t == (time_t)-1)
3112 		return ((time_t)0);
3113 	return (t);
3114 }
3115 
3116 static time_t
3117 isodate17(const unsigned char *v)
3118 {
3119 	struct tm tm;
3120 	int offset;
3121 	time_t t;
3122 
3123 	memset(&tm, 0, sizeof(tm));
3124 	tm.tm_year = (v[0] - '0') * 1000 + (v[1] - '0') * 100
3125 	    + (v[2] - '0') * 10 + (v[3] - '0')
3126 	    - 1900;
3127 	tm.tm_mon = (v[4] - '0') * 10 + (v[5] - '0');
3128 	tm.tm_mday = (v[6] - '0') * 10 + (v[7] - '0');
3129 	tm.tm_hour = (v[8] - '0') * 10 + (v[9] - '0');
3130 	tm.tm_min = (v[10] - '0') * 10 + (v[11] - '0');
3131 	tm.tm_sec = (v[12] - '0') * 10 + (v[13] - '0');
3132 	/* v[16] is the signed timezone offset, in 1/4-hour increments. */
3133 	offset = ((const signed char *)v)[16];
3134 	if (offset > -48 && offset < 52) {
3135 		tm.tm_hour -= offset / 4;
3136 		tm.tm_min -= (offset % 4) * 15;
3137 	}
3138 	t = time_from_tm(&tm);
3139 	if (t == (time_t)-1)
3140 		return ((time_t)0);
3141 	return (t);
3142 }
3143 
3144 static time_t
3145 time_from_tm(struct tm *t)
3146 {
3147 #if HAVE_TIMEGM
3148 	/* Use platform timegm() if available. */
3149 	return (timegm(t));
3150 #elif HAVE__MKGMTIME64
3151 	return (_mkgmtime64(t));
3152 #else
3153 	/* Else use direct calculation using POSIX assumptions. */
3154 	/* First, fix up tm_yday based on the year/month/day. */
3155 	if (mktime(t) == (time_t)-1)
3156 		return ((time_t)-1);
3157 	/* Then we can compute timegm() from first principles. */
3158 	return (t->tm_sec + t->tm_min * 60 + t->tm_hour * 3600
3159 	    + t->tm_yday * 86400 + (t->tm_year - 70) * 31536000
3160 	    + ((t->tm_year - 69) / 4) * 86400 -
3161 	    ((t->tm_year - 1) / 100) * 86400
3162 	    + ((t->tm_year + 299) / 400) * 86400);
3163 #endif
3164 }
3165 
3166 static const char *
3167 build_pathname(struct archive_string *as, struct file_info *file)
3168 {
3169 	if (file->parent != NULL && archive_strlen(&file->parent->name) > 0) {
3170 		build_pathname(as, file->parent);
3171 		archive_strcat(as, "/");
3172 	}
3173 	if (archive_strlen(&file->name) == 0)
3174 		archive_strcat(as, ".");
3175 	else
3176 		archive_string_concat(as, &file->name);
3177 	return (as->s);
3178 }
3179 
3180 static int
3181 build_pathname_utf16be(unsigned char *p, size_t max, size_t *len,
3182     struct file_info *file)
3183 {
3184 	if (file->parent != NULL && file->parent->utf16be_bytes > 0) {
3185 		if (build_pathname_utf16be(p, max, len, file->parent) != 0)
3186 			return (-1);
3187 		p[*len] = 0;
3188 		p[*len + 1] = '/';
3189 		*len += 2;
3190 	}
3191 	if (file->utf16be_bytes == 0) {
3192 		if (*len + 2 > max)
3193 			return (-1);/* Path is too long! */
3194 		p[*len] = 0;
3195 		p[*len + 1] = '.';
3196 		*len += 2;
3197 	} else {
3198 		if (*len + file->utf16be_bytes > max)
3199 			return (-1);/* Path is too long! */
3200 		memcpy(p + *len, file->utf16be_name, file->utf16be_bytes);
3201 		*len += file->utf16be_bytes;
3202 	}
3203 	return (0);
3204 }
3205 
3206 #if DEBUG
3207 static void
3208 dump_isodirrec(FILE *out, const unsigned char *isodirrec)
3209 {
3210 	fprintf(out, " l %d,",
3211 	    toi(isodirrec + DR_length_offset, DR_length_size));
3212 	fprintf(out, " a %d,",
3213 	    toi(isodirrec + DR_ext_attr_length_offset, DR_ext_attr_length_size));
3214 	fprintf(out, " ext 0x%x,",
3215 	    toi(isodirrec + DR_extent_offset, DR_extent_size));
3216 	fprintf(out, " s %d,",
3217 	    toi(isodirrec + DR_size_offset, DR_extent_size));
3218 	fprintf(out, " f 0x%x,",
3219 	    toi(isodirrec + DR_flags_offset, DR_flags_size));
3220 	fprintf(out, " u %d,",
3221 	    toi(isodirrec + DR_file_unit_size_offset, DR_file_unit_size_size));
3222 	fprintf(out, " ilv %d,",
3223 	    toi(isodirrec + DR_interleave_offset, DR_interleave_size));
3224 	fprintf(out, " seq %d,",
3225 	    toi(isodirrec + DR_volume_sequence_number_offset,
3226 		DR_volume_sequence_number_size));
3227 	fprintf(out, " nl %d:",
3228 	    toi(isodirrec + DR_name_len_offset, DR_name_len_size));
3229 	fprintf(out, " `%.*s'",
3230 	    toi(isodirrec + DR_name_len_offset, DR_name_len_size),
3231 		isodirrec + DR_name_offset);
3232 }
3233 #endif
3234