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