1228753Smm/*-
2228753Smm * Copyright (c) 2003-2007 Tim Kientzle
3228753Smm * Copyright (c) 2009 Andreas Henriksson <andreas@fatal.se>
4228753Smm * Copyright (c) 2009-2011 Michihiro NAKAJIMA
5228753Smm * All rights reserved.
6228753Smm *
7228753Smm * Redistribution and use in source and binary forms, with or without
8228753Smm * modification, are permitted provided that the following conditions
9228753Smm * are met:
10228753Smm * 1. Redistributions of source code must retain the above copyright
11228753Smm *    notice, this list of conditions and the following disclaimer.
12228753Smm * 2. Redistributions in binary form must reproduce the above copyright
13228753Smm *    notice, this list of conditions and the following disclaimer in the
14228753Smm *    documentation and/or other materials provided with the distribution.
15228753Smm *
16228753Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
17228753Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18228753Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19228753Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
20228753Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21228753Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22228753Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23228753Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24228753Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25228753Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26228753Smm */
27228753Smm
28228753Smm#include "archive_platform.h"
29229592Smm__FBSDID("$FreeBSD$");
30228753Smm
31228753Smm#ifdef HAVE_ERRNO_H
32228753Smm#include <errno.h>
33228753Smm#endif
34228753Smm/* #include <stdint.h> */ /* See archive_platform.h */
35228753Smm#include <stdio.h>
36228753Smm#ifdef HAVE_STDLIB_H
37228753Smm#include <stdlib.h>
38228753Smm#endif
39228753Smm#ifdef HAVE_STRING_H
40228753Smm#include <string.h>
41228753Smm#endif
42228753Smm#include <time.h>
43228753Smm#ifdef HAVE_ZLIB_H
44228753Smm#include <zlib.h>
45228753Smm#endif
46228753Smm
47228753Smm#include "archive.h"
48228753Smm#include "archive_endian.h"
49228753Smm#include "archive_entry.h"
50228753Smm#include "archive_private.h"
51228753Smm#include "archive_read_private.h"
52228753Smm#include "archive_string.h"
53228753Smm
54228753Smm/*
55228753Smm * An overview of ISO 9660 format:
56228753Smm *
57228753Smm * Each disk is laid out as follows:
58228753Smm *   * 32k reserved for private use
59228753Smm *   * Volume descriptor table.  Each volume descriptor
60228753Smm *     is 2k and specifies basic format information.
61228753Smm *     The "Primary Volume Descriptor" (PVD) is defined by the
62228753Smm *     standard and should always be present; other volume
63228753Smm *     descriptors include various vendor-specific extensions.
64228753Smm *   * Files and directories.  Each file/dir is specified by
65228753Smm *     an "extent" (starting sector and length in bytes).
66228753Smm *     Dirs are just files with directory records packed one
67228753Smm *     after another.  The PVD contains a single dir entry
68228753Smm *     specifying the location of the root directory.  Everything
69228753Smm *     else follows from there.
70228753Smm *
71228753Smm * This module works by first reading the volume descriptors, then
72228753Smm * building a list of directory entries, sorted by starting
73228753Smm * sector.  At each step, I look for the earliest dir entry that
74228753Smm * hasn't yet been read, seek forward to that location and read
75228753Smm * that entry.  If it's a dir, I slurp in the new dir entries and
76228753Smm * add them to the heap; if it's a regular file, I return the
77228753Smm * corresponding archive_entry and wait for the client to request
78228753Smm * the file body.  This strategy allows us to read most compliant
79228753Smm * CDs with a single pass through the data, as required by libarchive.
80228753Smm */
81228753Smm#define	LOGICAL_BLOCK_SIZE	2048
82228753Smm#define	SYSTEM_AREA_BLOCK	16
83228753Smm
84228753Smm/* Structure of on-disk primary volume descriptor. */
85228753Smm#define PVD_type_offset 0
86228753Smm#define PVD_type_size 1
87228753Smm#define PVD_id_offset (PVD_type_offset + PVD_type_size)
88228753Smm#define PVD_id_size 5
89228753Smm#define PVD_version_offset (PVD_id_offset + PVD_id_size)
90228753Smm#define PVD_version_size 1
91228753Smm#define PVD_reserved1_offset (PVD_version_offset + PVD_version_size)
92228753Smm#define PVD_reserved1_size 1
93228753Smm#define PVD_system_id_offset (PVD_reserved1_offset + PVD_reserved1_size)
94228753Smm#define PVD_system_id_size 32
95228753Smm#define PVD_volume_id_offset (PVD_system_id_offset + PVD_system_id_size)
96228753Smm#define PVD_volume_id_size 32
97228753Smm#define PVD_reserved2_offset (PVD_volume_id_offset + PVD_volume_id_size)
98228753Smm#define PVD_reserved2_size 8
99228753Smm#define PVD_volume_space_size_offset (PVD_reserved2_offset + PVD_reserved2_size)
100228753Smm#define PVD_volume_space_size_size 8
101228753Smm#define PVD_reserved3_offset (PVD_volume_space_size_offset + PVD_volume_space_size_size)
102228753Smm#define PVD_reserved3_size 32
103228753Smm#define PVD_volume_set_size_offset (PVD_reserved3_offset + PVD_reserved3_size)
104228753Smm#define PVD_volume_set_size_size 4
105228753Smm#define PVD_volume_sequence_number_offset (PVD_volume_set_size_offset + PVD_volume_set_size_size)
106228753Smm#define PVD_volume_sequence_number_size 4
107228753Smm#define PVD_logical_block_size_offset (PVD_volume_sequence_number_offset + PVD_volume_sequence_number_size)
108228753Smm#define PVD_logical_block_size_size 4
109228753Smm#define PVD_path_table_size_offset (PVD_logical_block_size_offset + PVD_logical_block_size_size)
110228753Smm#define PVD_path_table_size_size 8
111228753Smm#define PVD_type_1_path_table_offset (PVD_path_table_size_offset + PVD_path_table_size_size)
112228753Smm#define PVD_type_1_path_table_size 4
113228753Smm#define PVD_opt_type_1_path_table_offset (PVD_type_1_path_table_offset + PVD_type_1_path_table_size)
114228753Smm#define PVD_opt_type_1_path_table_size 4
115228753Smm#define PVD_type_m_path_table_offset (PVD_opt_type_1_path_table_offset + PVD_opt_type_1_path_table_size)
116228753Smm#define PVD_type_m_path_table_size 4
117228753Smm#define PVD_opt_type_m_path_table_offset (PVD_type_m_path_table_offset + PVD_type_m_path_table_size)
118228753Smm#define PVD_opt_type_m_path_table_size 4
119228753Smm#define PVD_root_directory_record_offset (PVD_opt_type_m_path_table_offset + PVD_opt_type_m_path_table_size)
120228753Smm#define PVD_root_directory_record_size 34
121228753Smm#define PVD_volume_set_id_offset (PVD_root_directory_record_offset + PVD_root_directory_record_size)
122228753Smm#define PVD_volume_set_id_size 128
123228753Smm#define PVD_publisher_id_offset (PVD_volume_set_id_offset + PVD_volume_set_id_size)
124228753Smm#define PVD_publisher_id_size 128
125228753Smm#define PVD_preparer_id_offset (PVD_publisher_id_offset + PVD_publisher_id_size)
126228753Smm#define PVD_preparer_id_size 128
127228753Smm#define PVD_application_id_offset (PVD_preparer_id_offset + PVD_preparer_id_size)
128228753Smm#define PVD_application_id_size 128
129228753Smm#define PVD_copyright_file_id_offset (PVD_application_id_offset + PVD_application_id_size)
130228753Smm#define PVD_copyright_file_id_size 37
131228753Smm#define PVD_abstract_file_id_offset (PVD_copyright_file_id_offset + PVD_copyright_file_id_size)
132228753Smm#define PVD_abstract_file_id_size 37
133228753Smm#define PVD_bibliographic_file_id_offset (PVD_abstract_file_id_offset + PVD_abstract_file_id_size)
134228753Smm#define PVD_bibliographic_file_id_size 37
135228753Smm#define PVD_creation_date_offset (PVD_bibliographic_file_id_offset + PVD_bibliographic_file_id_size)
136228753Smm#define PVD_creation_date_size 17
137228753Smm#define PVD_modification_date_offset (PVD_creation_date_offset + PVD_creation_date_size)
138228753Smm#define PVD_modification_date_size 17
139228753Smm#define PVD_expiration_date_offset (PVD_modification_date_offset + PVD_modification_date_size)
140228753Smm#define PVD_expiration_date_size 17
141228753Smm#define PVD_effective_date_offset (PVD_expiration_date_offset + PVD_expiration_date_size)
142228753Smm#define PVD_effective_date_size 17
143228753Smm#define PVD_file_structure_version_offset (PVD_effective_date_offset + PVD_effective_date_size)
144228753Smm#define PVD_file_structure_version_size 1
145228753Smm#define PVD_reserved4_offset (PVD_file_structure_version_offset + PVD_file_structure_version_size)
146228753Smm#define PVD_reserved4_size 1
147228753Smm#define PVD_application_data_offset (PVD_reserved4_offset + PVD_reserved4_size)
148228753Smm#define PVD_application_data_size 512
149228753Smm#define PVD_reserved5_offset (PVD_application_data_offset + PVD_application_data_size)
150228753Smm#define PVD_reserved5_size (2048 - PVD_reserved5_offset)
151228753Smm
152228753Smm/* TODO: It would make future maintenance easier to just hardcode the
153228753Smm * above values.  In particular, ECMA119 states the offsets as part of
154228753Smm * the standard.  That would eliminate the need for the following check.*/
155228753Smm#if PVD_reserved5_offset != 1395
156228753Smm#error PVD offset and size definitions are wrong.
157228753Smm#endif
158228753Smm
159228753Smm
160228753Smm/* Structure of optional on-disk supplementary volume descriptor. */
161228753Smm#define SVD_type_offset 0
162228753Smm#define SVD_type_size 1
163228753Smm#define SVD_id_offset (SVD_type_offset + SVD_type_size)
164228753Smm#define SVD_id_size 5
165228753Smm#define SVD_version_offset (SVD_id_offset + SVD_id_size)
166228753Smm#define SVD_version_size 1
167228753Smm/* ... */
168228753Smm#define SVD_reserved1_offset	72
169228753Smm#define SVD_reserved1_size	8
170228753Smm#define SVD_volume_space_size_offset 80
171228753Smm#define SVD_volume_space_size_size 8
172228753Smm#define SVD_escape_sequences_offset (SVD_volume_space_size_offset + SVD_volume_space_size_size)
173228753Smm#define SVD_escape_sequences_size 32
174228753Smm/* ... */
175228753Smm#define SVD_logical_block_size_offset 128
176228753Smm#define SVD_logical_block_size_size 4
177228753Smm#define SVD_type_L_path_table_offset 140
178228753Smm#define SVD_type_M_path_table_offset 148
179228753Smm/* ... */
180228753Smm#define SVD_root_directory_record_offset 156
181228753Smm#define SVD_root_directory_record_size 34
182228753Smm#define SVD_file_structure_version_offset 881
183228753Smm#define SVD_reserved2_offset	882
184228753Smm#define SVD_reserved2_size	1
185228753Smm#define SVD_reserved3_offset	1395
186228753Smm#define SVD_reserved3_size	653
187228753Smm/* ... */
188228753Smm/* FIXME: validate correctness of last SVD entry offset. */
189228753Smm
190228753Smm/* Structure of an on-disk directory record. */
191228753Smm/* Note:  ISO9660 stores each multi-byte integer twice, once in
192228753Smm * each byte order.  The sizes here are the size of just one
193228753Smm * of the two integers.  (This is why the offset of a field isn't
194228753Smm * the same as the offset+size of the previous field.) */
195228753Smm#define DR_length_offset 0
196228753Smm#define DR_length_size 1
197228753Smm#define DR_ext_attr_length_offset 1
198228753Smm#define DR_ext_attr_length_size 1
199228753Smm#define DR_extent_offset 2
200228753Smm#define DR_extent_size 4
201228753Smm#define DR_size_offset 10
202228753Smm#define DR_size_size 4
203228753Smm#define DR_date_offset 18
204228753Smm#define DR_date_size 7
205228753Smm#define DR_flags_offset 25
206228753Smm#define DR_flags_size 1
207228753Smm#define DR_file_unit_size_offset 26
208228753Smm#define DR_file_unit_size_size 1
209228753Smm#define DR_interleave_offset 27
210228753Smm#define DR_interleave_size 1
211228753Smm#define DR_volume_sequence_number_offset 28
212228753Smm#define DR_volume_sequence_number_size 2
213228753Smm#define DR_name_len_offset 32
214228753Smm#define DR_name_len_size 1
215228753Smm#define DR_name_offset 33
216228753Smm
217228753Smm#ifdef HAVE_ZLIB_H
218228753Smmstatic const unsigned char zisofs_magic[8] = {
219228753Smm	0x37, 0xE4, 0x53, 0x96, 0xC9, 0xDB, 0xD6, 0x07
220228753Smm};
221228753Smm
222228753Smmstruct zisofs {
223228753Smm	/* Set 1 if this file compressed by paged zlib */
224228753Smm	int		 pz;
225228753Smm	int		 pz_log2_bs; /* Log2 of block size */
226228753Smm	uint64_t	 pz_uncompressed_size;
227228753Smm
228228753Smm	int		 initialized;
229228753Smm	unsigned char	*uncompressed_buffer;
230228753Smm	size_t		 uncompressed_buffer_size;
231228753Smm
232228753Smm	uint32_t	 pz_offset;
233228753Smm	unsigned char	 header[16];
234228753Smm	size_t		 header_avail;
235228753Smm	int		 header_passed;
236228753Smm	unsigned char	*block_pointers;
237228753Smm	size_t		 block_pointers_alloc;
238228753Smm	size_t		 block_pointers_size;
239228753Smm	size_t		 block_pointers_avail;
240228753Smm	size_t		 block_off;
241228753Smm	uint32_t	 block_avail;
242228753Smm
243228753Smm	z_stream	 stream;
244228753Smm	int		 stream_valid;
245228753Smm};
246228753Smm#else
247228753Smmstruct zisofs {
248228753Smm	/* Set 1 if this file compressed by paged zlib */
249228753Smm	int		 pz;
250228753Smm};
251228753Smm#endif
252228753Smm
253228753Smmstruct content {
254228753Smm	uint64_t	 offset;/* Offset on disk.		*/
255228753Smm	uint64_t	 size;	/* File size in bytes.		*/
256228753Smm	struct content	*next;
257228753Smm};
258228753Smm
259228753Smm/* In-memory storage for a directory record. */
260228753Smmstruct file_info {
261228753Smm	struct file_info	*use_next;
262228753Smm	struct file_info	*parent;
263228753Smm	struct file_info	*next;
264228753Smm	struct file_info	*re_next;
265228753Smm	int		 subdirs;
266228753Smm	uint64_t	 key;		/* Heap Key.			*/
267228753Smm	uint64_t	 offset;	/* Offset on disk.		*/
268228753Smm	uint64_t	 size;		/* File size in bytes.		*/
269228753Smm	uint32_t	 ce_offset;	/* Offset of CE.		*/
270228753Smm	uint32_t	 ce_size;	/* Size of CE.			*/
271228753Smm	char		 rr_moved;	/* Flag to rr_moved.		*/
272228753Smm	char		 rr_moved_has_re_only;
273228753Smm	char		 re;		/* Having RRIP "RE" extension.	*/
274228753Smm	char		 re_descendant;
275228753Smm	uint64_t	 cl_offset;	/* Having RRIP "CL" extension.	*/
276228753Smm	int		 birthtime_is_set;
277228753Smm	time_t		 birthtime;	/* File created time.		*/
278228753Smm	time_t		 mtime;		/* File last modified time.	*/
279228753Smm	time_t		 atime;		/* File last accessed time.	*/
280228753Smm	time_t		 ctime;		/* File attribute change time.	*/
281228753Smm	uint64_t	 rdev;		/* Device number.		*/
282228753Smm	mode_t		 mode;
283228753Smm	uid_t		 uid;
284228753Smm	gid_t		 gid;
285228753Smm	int64_t		 number;
286228753Smm	int		 nlinks;
287228753Smm	struct archive_string name; /* Pathname */
288228753Smm	char		 name_continues; /* Non-zero if name continues */
289228753Smm	struct archive_string symlink;
290228753Smm	char		 symlink_continues; /* Non-zero if link continues */
291228753Smm	/* Set 1 if this file compressed by paged zlib(zisofs) */
292228753Smm	int		 pz;
293228753Smm	int		 pz_log2_bs; /* Log2 of block size */
294228753Smm	uint64_t	 pz_uncompressed_size;
295228753Smm	/* Set 1 if this file is multi extent. */
296228753Smm	int		 multi_extent;
297228753Smm	struct {
298228753Smm		struct content	*first;
299228753Smm		struct content	**last;
300228753Smm	} contents;
301228753Smm	struct {
302228753Smm		struct file_info	*first;
303228753Smm		struct file_info	**last;
304228753Smm	} rede_files;
305228753Smm};
306228753Smm
307228753Smmstruct heap_queue {
308228753Smm	struct file_info **files;
309228753Smm	int		 allocated;
310228753Smm	int		 used;
311228753Smm};
312228753Smm
313228753Smmstruct iso9660 {
314228753Smm	int	magic;
315228753Smm#define ISO9660_MAGIC   0x96609660
316228753Smm
317228753Smm	int opt_support_joliet;
318228753Smm	int opt_support_rockridge;
319228753Smm
320228753Smm	struct archive_string pathname;
321228753Smm	char	seenRockridge;	/* Set true if RR extensions are used. */
322228753Smm	char	seenSUSP;	/* Set true if SUSP is beging used. */
323228753Smm	char	seenJoliet;
324228753Smm
325228753Smm	unsigned char	suspOffset;
326228753Smm	struct file_info *rr_moved;
327228753Smm	struct read_ce_queue {
328228753Smm		struct read_ce_req {
329228753Smm			uint64_t	 offset;/* Offset of CE on disk. */
330228753Smm			struct file_info *file;
331228753Smm		}		*reqs;
332228753Smm		int		 cnt;
333228753Smm		int		 allocated;
334228753Smm	}	read_ce_req;
335228753Smm
336228753Smm	int64_t		previous_number;
337228753Smm	struct archive_string previous_pathname;
338228753Smm
339228753Smm	struct file_info		*use_files;
340228753Smm	struct heap_queue		 pending_files;
341228753Smm	struct {
342228753Smm		struct file_info	*first;
343228753Smm		struct file_info	**last;
344228753Smm	}	cache_files;
345228753Smm	struct {
346228753Smm		struct file_info	*first;
347228753Smm		struct file_info	**last;
348228753Smm	}	re_files;
349228753Smm
350228753Smm	uint64_t current_position;
351228753Smm	ssize_t	logical_block_size;
352228753Smm	uint64_t volume_size; /* Total size of volume in bytes. */
353228753Smm	int32_t  volume_block;/* Total size of volume in logical blocks. */
354228753Smm
355228753Smm	struct vd {
356228753Smm		int		location;	/* Location of Extent.	*/
357228753Smm		uint32_t	size;
358228753Smm	} primary, joliet;
359228753Smm
360228753Smm	off_t	entry_sparse_offset;
361228753Smm	int64_t	entry_bytes_remaining;
362228753Smm	struct zisofs	 entry_zisofs;
363228753Smm	struct content	*entry_content;
364228753Smm};
365228753Smm
366228753Smmstatic int	archive_read_format_iso9660_bid(struct archive_read *);
367228753Smmstatic int	archive_read_format_iso9660_options(struct archive_read *,
368228753Smm		    const char *, const char *);
369228753Smmstatic int	archive_read_format_iso9660_cleanup(struct archive_read *);
370228753Smmstatic int	archive_read_format_iso9660_read_data(struct archive_read *,
371228753Smm		    const void **, size_t *, off_t *);
372228753Smmstatic int	archive_read_format_iso9660_read_data_skip(struct archive_read *);
373228753Smmstatic int	archive_read_format_iso9660_read_header(struct archive_read *,
374228753Smm		    struct archive_entry *);
375228753Smmstatic const char *build_pathname(struct archive_string *, struct file_info *);
376228753Smm#if DEBUG
377228753Smmstatic void	dump_isodirrec(FILE *, const unsigned char *isodirrec);
378228753Smm#endif
379228753Smmstatic time_t	time_from_tm(struct tm *);
380228753Smmstatic time_t	isodate17(const unsigned char *);
381228753Smmstatic time_t	isodate7(const unsigned char *);
382228753Smmstatic int	isBootRecord(struct iso9660 *, const unsigned char *);
383228753Smmstatic int	isVolumePartition(struct iso9660 *, const unsigned char *);
384228753Smmstatic int	isVDSetTerminator(struct iso9660 *, const unsigned char *);
385228753Smmstatic int	isJolietSVD(struct iso9660 *, const unsigned char *);
386228753Smmstatic int	isSVD(struct iso9660 *, const unsigned char *);
387228753Smmstatic int	isEVD(struct iso9660 *, const unsigned char *);
388228753Smmstatic int	isPVD(struct iso9660 *, const unsigned char *);
389228753Smmstatic int	next_cache_entry(struct archive_read *, struct iso9660 *,
390228753Smm		    struct file_info **);
391228753Smmstatic int	next_entry_seek(struct archive_read *a, struct iso9660 *iso9660,
392228753Smm		    struct file_info **pfile);
393228753Smmstatic struct file_info *
394228753Smm		parse_file_info(struct archive_read *a,
395228753Smm		    struct file_info *parent, const unsigned char *isodirrec);
396228753Smmstatic int	parse_rockridge(struct archive_read *a,
397228753Smm		    struct file_info *file, const unsigned char *start,
398228753Smm		    const unsigned char *end);
399228753Smmstatic int	register_CE(struct archive_read *a, int32_t location,
400228753Smm		    struct file_info *file);
401228753Smmstatic int	read_CE(struct archive_read *a, struct iso9660 *iso9660);
402228753Smmstatic void	parse_rockridge_NM1(struct file_info *,
403228753Smm		    const unsigned char *, int);
404228753Smmstatic void	parse_rockridge_SL1(struct file_info *,
405228753Smm		    const unsigned char *, int);
406228753Smmstatic void	parse_rockridge_TF1(struct file_info *,
407228753Smm		    const unsigned char *, int);
408228753Smmstatic void	parse_rockridge_ZF1(struct file_info *,
409228753Smm		    const unsigned char *, int);
410228753Smmstatic void	register_file(struct iso9660 *, struct file_info *);
411228753Smmstatic void	release_files(struct iso9660 *);
412228753Smmstatic unsigned	toi(const void *p, int n);
413228753Smmstatic inline void re_add_entry(struct iso9660 *, struct file_info *);
414228753Smmstatic inline struct file_info * re_get_entry(struct iso9660 *);
415228753Smmstatic inline int rede_add_entry(struct file_info *);
416228753Smmstatic inline struct file_info * rede_get_entry(struct file_info *);
417228753Smmstatic inline void cache_add_entry(struct iso9660 *iso9660,
418228753Smm		    struct file_info *file);
419228753Smmstatic inline struct file_info *cache_get_entry(struct iso9660 *iso9660);
420228753Smmstatic void	heap_add_entry(struct heap_queue *heap,
421228753Smm		    struct file_info *file, uint64_t key);
422228753Smmstatic struct file_info *heap_get_entry(struct heap_queue *heap);
423228753Smm
424228753Smm#define add_entry(iso9660, file)	\
425228753Smm	heap_add_entry(&((iso9660)->pending_files), file, file->offset)
426228753Smm#define next_entry(iso9660)		\
427228753Smm	heap_get_entry(&((iso9660)->pending_files))
428228753Smm
429228753Smmint
430228753Smmarchive_read_support_format_iso9660(struct archive *_a)
431228753Smm{
432228753Smm	struct archive_read *a = (struct archive_read *)_a;
433228753Smm	struct iso9660 *iso9660;
434228753Smm	int r;
435228753Smm
436228753Smm	iso9660 = (struct iso9660 *)malloc(sizeof(*iso9660));
437228753Smm	if (iso9660 == NULL) {
438228753Smm		archive_set_error(&a->archive, ENOMEM, "Can't allocate iso9660 data");
439228753Smm		return (ARCHIVE_FATAL);
440228753Smm	}
441228753Smm	memset(iso9660, 0, sizeof(*iso9660));
442228753Smm	iso9660->magic = ISO9660_MAGIC;
443228753Smm	iso9660->cache_files.first = NULL;
444228753Smm	iso9660->cache_files.last = &(iso9660->cache_files.first);
445228753Smm	iso9660->re_files.first = NULL;
446228753Smm	iso9660->re_files.last = &(iso9660->re_files.first);
447228753Smm	/* Enable to support Joliet extensions by default.	*/
448228753Smm	iso9660->opt_support_joliet = 1;
449228753Smm	/* Enable to support Rock Ridge extensions by default.	*/
450228753Smm	iso9660->opt_support_rockridge = 1;
451228753Smm
452228753Smm	r = __archive_read_register_format(a,
453228753Smm	    iso9660,
454228753Smm	    "iso9660",
455228753Smm	    archive_read_format_iso9660_bid,
456228753Smm	    archive_read_format_iso9660_options,
457228753Smm	    archive_read_format_iso9660_read_header,
458228753Smm	    archive_read_format_iso9660_read_data,
459228753Smm	    archive_read_format_iso9660_read_data_skip,
460228753Smm	    archive_read_format_iso9660_cleanup);
461228753Smm
462228753Smm	if (r != ARCHIVE_OK) {
463228753Smm		free(iso9660);
464228753Smm		return (r);
465228753Smm	}
466228753Smm	return (ARCHIVE_OK);
467228753Smm}
468228753Smm
469228753Smm
470228753Smmstatic int
471228753Smmarchive_read_format_iso9660_bid(struct archive_read *a)
472228753Smm{
473228753Smm	struct iso9660 *iso9660;
474228753Smm	ssize_t bytes_read;
475228753Smm	const void *h;
476228753Smm	const unsigned char *p;
477228753Smm	int seenTerminator;
478228753Smm
479228753Smm	iso9660 = (struct iso9660 *)(a->format->data);
480228753Smm
481228753Smm	/*
482228753Smm	 * Skip the first 32k (reserved area) and get the first
483228753Smm	 * 8 sectors of the volume descriptor table.  Of course,
484228753Smm	 * if the I/O layer gives us more, we'll take it.
485228753Smm	 */
486228753Smm#define RESERVED_AREA	(SYSTEM_AREA_BLOCK * LOGICAL_BLOCK_SIZE)
487228753Smm	h = __archive_read_ahead(a,
488228753Smm	    RESERVED_AREA + 8 * LOGICAL_BLOCK_SIZE,
489228753Smm	    &bytes_read);
490228753Smm	if (h == NULL)
491228753Smm	    return (-1);
492228753Smm	p = (const unsigned char *)h;
493228753Smm
494228753Smm	/* Skip the reserved area. */
495228753Smm	bytes_read -= RESERVED_AREA;
496228753Smm	p += RESERVED_AREA;
497228753Smm
498228753Smm	/* Check each volume descriptor. */
499228753Smm	seenTerminator = 0;
500228753Smm	for (; bytes_read > LOGICAL_BLOCK_SIZE;
501228753Smm	    bytes_read -= LOGICAL_BLOCK_SIZE, p += LOGICAL_BLOCK_SIZE) {
502228753Smm		/* Do not handle undefined Volume Descriptor Type. */
503228753Smm		if (p[0] >= 4 && p[0] <= 254)
504228753Smm			return (0);
505228753Smm		/* Standard Identifier must be "CD001" */
506228753Smm		if (memcmp(p + 1, "CD001", 5) != 0)
507228753Smm			return (0);
508228753Smm		if (!iso9660->primary.location) {
509228753Smm			if (isPVD(iso9660, p))
510228753Smm				continue;
511228753Smm		}
512228753Smm		if (!iso9660->joliet.location) {
513228753Smm			if (isJolietSVD(iso9660, p))
514228753Smm				continue;
515228753Smm		}
516228753Smm		if (isBootRecord(iso9660, p))
517228753Smm			continue;
518228753Smm		if (isEVD(iso9660, p))
519228753Smm			continue;
520228753Smm		if (isSVD(iso9660, p))
521228753Smm			continue;
522228753Smm		if (isVolumePartition(iso9660, p))
523228753Smm			continue;
524228753Smm		if (isVDSetTerminator(iso9660, p)) {
525228753Smm			seenTerminator = 1;
526228753Smm			break;
527228753Smm		}
528228753Smm		return (0);
529228753Smm	}
530228753Smm	/*
531228753Smm	 * ISO 9660 format must have Primary Volume Descriptor and
532228753Smm	 * Volume Descriptor Set Terminator.
533228753Smm	 */
534228753Smm	if (seenTerminator && iso9660->primary.location > 16)
535228753Smm		return (48);
536228753Smm
537228753Smm	/* We didn't find a valid PVD; return a bid of zero. */
538228753Smm	return (0);
539228753Smm}
540228753Smm
541228753Smmstatic int
542228753Smmarchive_read_format_iso9660_options(struct archive_read *a,
543228753Smm		const char *key, const char *val)
544228753Smm{
545228753Smm	struct iso9660 *iso9660;
546228753Smm
547228753Smm	iso9660 = (struct iso9660 *)(a->format->data);
548228753Smm
549228753Smm	if (strcmp(key, "joliet") == 0) {
550228753Smm		if (val == NULL || strcmp(val, "off") == 0 ||
551228753Smm				strcmp(val, "ignore") == 0 ||
552228753Smm				strcmp(val, "disable") == 0 ||
553228753Smm				strcmp(val, "0") == 0)
554228753Smm			iso9660->opt_support_joliet = 0;
555228753Smm		else
556228753Smm			iso9660->opt_support_joliet = 1;
557228753Smm		return (ARCHIVE_OK);
558228753Smm	}
559228753Smm	if (strcmp(key, "rockridge") == 0 ||
560228753Smm	    strcmp(key, "Rockridge") == 0) {
561228753Smm		iso9660->opt_support_rockridge = val != NULL;
562228753Smm		return (ARCHIVE_OK);
563228753Smm	}
564228753Smm
565228753Smm	/* Note: The "warn" return is just to inform the options
566228753Smm	 * supervisor that we didn't handle it.  It will generate
567228753Smm	 * a suitable error if noone used this option. */
568228753Smm	return (ARCHIVE_WARN);
569228753Smm}
570228753Smm
571228753Smmstatic int
572228753SmmisBootRecord(struct iso9660 *iso9660, const unsigned char *h)
573228753Smm{
574228753Smm	(void)iso9660; /* UNUSED */
575228753Smm
576228753Smm	/* Type of the Volume Descriptor Boot Record must be 0. */
577228753Smm	if (h[0] != 0)
578228753Smm		return (0);
579228753Smm
580228753Smm	/* Volume Descriptor Version must be 1. */
581228753Smm	if (h[6] != 1)
582228753Smm		return (0);
583228753Smm
584228753Smm	return (1);
585228753Smm}
586228753Smm
587228753Smmstatic int
588228753SmmisVolumePartition(struct iso9660 *iso9660, const unsigned char *h)
589228753Smm{
590228753Smm	int32_t location;
591228753Smm
592228753Smm	/* Type of the Volume Partition Descriptor must be 3. */
593228753Smm	if (h[0] != 3)
594228753Smm		return (0);
595228753Smm
596228753Smm	/* Volume Descriptor Version must be 1. */
597228753Smm	if (h[6] != 1)
598228753Smm		return (0);
599228753Smm	/* Unused Field */
600228753Smm	if (h[7] != 0)
601228753Smm		return (0);
602228753Smm
603228753Smm	location = archive_le32dec(h + 72);
604228753Smm	if (location <= SYSTEM_AREA_BLOCK ||
605228753Smm	    location >= iso9660->volume_block)
606228753Smm		return (0);
607228753Smm	if ((uint32_t)location != archive_be32dec(h + 76))
608228753Smm		return (0);
609228753Smm
610228753Smm	return (1);
611228753Smm}
612228753Smm
613228753Smmstatic int
614228753SmmisVDSetTerminator(struct iso9660 *iso9660, const unsigned char *h)
615228753Smm{
616228753Smm	int i;
617228753Smm
618228753Smm	(void)iso9660; /* UNUSED */
619228753Smm
620228753Smm	/* Type of the Volume Descriptor Set Terminator must be 255. */
621228753Smm	if (h[0] != 255)
622228753Smm		return (0);
623228753Smm
624228753Smm	/* Volume Descriptor Version must be 1. */
625228753Smm	if (h[6] != 1)
626228753Smm		return (0);
627228753Smm
628228753Smm	/* Reserved field must be 0. */
629228753Smm	for (i = 7; i < 2048; ++i)
630228753Smm		if (h[i] != 0)
631228753Smm			return (0);
632228753Smm
633228753Smm	return (1);
634228753Smm}
635228753Smm
636228753Smmstatic int
637228753SmmisJolietSVD(struct iso9660 *iso9660, const unsigned char *h)
638228753Smm{
639228753Smm	const unsigned char *p;
640228753Smm	ssize_t logical_block_size;
641228753Smm	int32_t volume_block;
642228753Smm
643228753Smm	/* Check if current sector is a kind of Supplementary Volume
644228753Smm	 * Descriptor. */
645228753Smm	if (!isSVD(iso9660, h))
646228753Smm		return (0);
647228753Smm
648228753Smm	/* FIXME: do more validations according to joliet spec. */
649228753Smm
650228753Smm	/* check if this SVD contains joliet extension! */
651228753Smm	p = h + SVD_escape_sequences_offset;
652228753Smm	/* N.B. Joliet spec says p[1] == '\\', but.... */
653228753Smm	if (p[0] == '%' && p[1] == '/') {
654228753Smm		int level = 0;
655228753Smm
656228753Smm		if (p[2] == '@')
657228753Smm			level = 1;
658228753Smm		else if (p[2] == 'C')
659228753Smm			level = 2;
660228753Smm		else if (p[2] == 'E')
661228753Smm			level = 3;
662228753Smm		else /* not joliet */
663228753Smm			return (0);
664228753Smm
665228753Smm		iso9660->seenJoliet = level;
666228753Smm
667228753Smm	} else /* not joliet */
668228753Smm		return (0);
669228753Smm
670228753Smm	logical_block_size =
671228753Smm	    archive_le16dec(h + SVD_logical_block_size_offset);
672228753Smm	volume_block = archive_le32dec(h + SVD_volume_space_size_offset);
673228753Smm
674228753Smm	iso9660->logical_block_size = logical_block_size;
675228753Smm	iso9660->volume_block = volume_block;
676228753Smm	iso9660->volume_size = logical_block_size * (uint64_t)volume_block;
677228753Smm	/* Read Root Directory Record in Volume Descriptor. */
678228753Smm	p = h + SVD_root_directory_record_offset;
679228753Smm	iso9660->joliet.location = archive_le32dec(p + DR_extent_offset);
680228753Smm	iso9660->joliet.size = archive_le32dec(p + DR_size_offset);
681228753Smm
682228753Smm	return (48);
683228753Smm}
684228753Smm
685228753Smmstatic int
686228753SmmisSVD(struct iso9660 *iso9660, const unsigned char *h)
687228753Smm{
688228753Smm	const unsigned char *p;
689228753Smm	ssize_t logical_block_size;
690228753Smm	int32_t volume_block;
691228753Smm	int32_t location;
692228753Smm	int i;
693228753Smm
694228753Smm	(void)iso9660; /* UNUSED */
695228753Smm
696228753Smm	/* Type 2 means it's a SVD. */
697228753Smm	if (h[SVD_type_offset] != 2)
698228753Smm		return (0);
699228753Smm
700228753Smm	/* Reserved field must be 0. */
701228753Smm	for (i = 0; i < SVD_reserved1_size; ++i)
702228753Smm		if (h[SVD_reserved1_offset + i] != 0)
703228753Smm			return (0);
704228753Smm	for (i = 0; i < SVD_reserved2_size; ++i)
705228753Smm		if (h[SVD_reserved2_offset + i] != 0)
706228753Smm			return (0);
707228753Smm	for (i = 0; i < SVD_reserved3_size; ++i)
708228753Smm		if (h[SVD_reserved3_offset + i] != 0)
709228753Smm			return (0);
710228753Smm
711228753Smm	/* File structure version must be 1 for ISO9660/ECMA119. */
712228753Smm	if (h[SVD_file_structure_version_offset] != 1)
713228753Smm		return (0);
714228753Smm
715228753Smm	logical_block_size =
716228753Smm	    archive_le16dec(h + SVD_logical_block_size_offset);
717228753Smm	if (logical_block_size <= 0)
718228753Smm		return (0);
719228753Smm
720228753Smm	volume_block = archive_le32dec(h + SVD_volume_space_size_offset);
721228753Smm	if (volume_block <= SYSTEM_AREA_BLOCK+4)
722228753Smm		return (0);
723228753Smm
724228753Smm	/* Location of Occurrence of Type L Path Table must be
725228753Smm	 * available location,
726228753Smm	 * >= SYSTEM_AREA_BLOCK(16) + 2 and < Volume Space Size. */
727228753Smm	location = archive_le32dec(h+SVD_type_L_path_table_offset);
728228753Smm	if (location < SYSTEM_AREA_BLOCK+2 || location >= volume_block)
729228753Smm		return (0);
730228753Smm
731228753Smm	/* The Type M Path Table must be at a valid location (WinISO
732228753Smm	 * and probably other programs omit this, so we allow zero)
733228753Smm	 *
734228753Smm	 * >= SYSTEM_AREA_BLOCK(16) + 2 and < Volume Space Size. */
735228753Smm	location = archive_be32dec(h+SVD_type_M_path_table_offset);
736228753Smm	if ((location > 0 && location < SYSTEM_AREA_BLOCK+2)
737228753Smm	    || location >= volume_block)
738228753Smm		return (0);
739228753Smm
740228753Smm	/* Read Root Directory Record in Volume Descriptor. */
741228753Smm	p = h + SVD_root_directory_record_offset;
742228753Smm	if (p[DR_length_offset] != 34)
743228753Smm		return (0);
744228753Smm
745228753Smm	return (48);
746228753Smm}
747228753Smm
748228753Smmstatic int
749228753SmmisEVD(struct iso9660 *iso9660, const unsigned char *h)
750228753Smm{
751228753Smm	const unsigned char *p;
752228753Smm	ssize_t logical_block_size;
753228753Smm	int32_t volume_block;
754228753Smm	int32_t location;
755228753Smm	int i;
756228753Smm
757228753Smm	(void)iso9660; /* UNUSED */
758228753Smm
759228753Smm	/* Type of the Enhanced Volume Descriptor must be 2. */
760228753Smm	if (h[PVD_type_offset] != 2)
761228753Smm		return (0);
762228753Smm
763228753Smm	/* EVD version must be 2. */
764228753Smm	if (h[PVD_version_offset] != 2)
765228753Smm		return (0);
766228753Smm
767228753Smm	/* Reserved field must be 0. */
768228753Smm	if (h[PVD_reserved1_offset] != 0)
769228753Smm		return (0);
770228753Smm
771228753Smm	/* Reserved field must be 0. */
772228753Smm	for (i = 0; i < PVD_reserved2_size; ++i)
773228753Smm		if (h[PVD_reserved2_offset + i] != 0)
774228753Smm			return (0);
775228753Smm
776228753Smm	/* Reserved field must be 0. */
777228753Smm	for (i = 0; i < PVD_reserved3_size; ++i)
778228753Smm		if (h[PVD_reserved3_offset + i] != 0)
779228753Smm			return (0);
780228753Smm
781228753Smm	/* Logical block size must be > 0. */
782228753Smm	/* I've looked at Ecma 119 and can't find any stronger
783228753Smm	 * restriction on this field. */
784228753Smm	logical_block_size =
785228753Smm	    archive_le16dec(h + PVD_logical_block_size_offset);
786228753Smm	if (logical_block_size <= 0)
787228753Smm		return (0);
788228753Smm
789228753Smm	volume_block =
790228753Smm	    archive_le32dec(h + PVD_volume_space_size_offset);
791228753Smm	if (volume_block <= SYSTEM_AREA_BLOCK+4)
792228753Smm		return (0);
793228753Smm
794228753Smm	/* File structure version must be 2 for ISO9660:1999. */
795228753Smm	if (h[PVD_file_structure_version_offset] != 2)
796228753Smm		return (0);
797228753Smm
798228753Smm	/* Location of Occurrence of Type L Path Table must be
799228753Smm	 * available location,
800228753Smm	 * >= SYSTEM_AREA_BLOCK(16) + 2 and < Volume Space Size. */
801228753Smm	location = archive_le32dec(h+PVD_type_1_path_table_offset);
802228753Smm	if (location < SYSTEM_AREA_BLOCK+2 || location >= volume_block)
803228753Smm		return (0);
804228753Smm
805228753Smm	/* Location of Occurrence of Type M Path Table must be
806228753Smm	 * available location,
807228753Smm	 * >= SYSTEM_AREA_BLOCK(16) + 2 and < Volume Space Size. */
808228753Smm	location = archive_be32dec(h+PVD_type_m_path_table_offset);
809228753Smm	if ((location > 0 && location < SYSTEM_AREA_BLOCK+2)
810228753Smm	    || location >= volume_block)
811228753Smm		return (0);
812228753Smm
813228753Smm	/* Reserved field must be 0. */
814228753Smm	for (i = 0; i < PVD_reserved4_size; ++i)
815228753Smm		if (h[PVD_reserved4_offset + i] != 0)
816228753Smm			return (0);
817228753Smm
818228753Smm	/* Reserved field must be 0. */
819228753Smm	for (i = 0; i < PVD_reserved5_size; ++i)
820228753Smm		if (h[PVD_reserved5_offset + i] != 0)
821228753Smm			return (0);
822228753Smm
823228753Smm	/* Read Root Directory Record in Volume Descriptor. */
824228753Smm	p = h + PVD_root_directory_record_offset;
825228753Smm	if (p[DR_length_offset] != 34)
826228753Smm		return (0);
827228753Smm
828228753Smm	return (48);
829228753Smm}
830228753Smm
831228753Smmstatic int
832228753SmmisPVD(struct iso9660 *iso9660, const unsigned char *h)
833228753Smm{
834228753Smm	const unsigned char *p;
835228753Smm	ssize_t logical_block_size;
836228753Smm	int32_t volume_block;
837228753Smm	int32_t location;
838228753Smm	int i;
839228753Smm
840228753Smm	/* Type of the Primary Volume Descriptor must be 1. */
841228753Smm	if (h[PVD_type_offset] != 1)
842228753Smm		return (0);
843228753Smm
844228753Smm	/* PVD version must be 1. */
845228753Smm	if (h[PVD_version_offset] != 1)
846228753Smm		return (0);
847228753Smm
848228753Smm	/* Reserved field must be 0. */
849228753Smm	if (h[PVD_reserved1_offset] != 0)
850228753Smm		return (0);
851228753Smm
852228753Smm	/* Reserved field must be 0. */
853228753Smm	for (i = 0; i < PVD_reserved2_size; ++i)
854228753Smm		if (h[PVD_reserved2_offset + i] != 0)
855228753Smm			return (0);
856228753Smm
857228753Smm	/* Reserved field must be 0. */
858228753Smm	for (i = 0; i < PVD_reserved3_size; ++i)
859228753Smm		if (h[PVD_reserved3_offset + i] != 0)
860228753Smm			return (0);
861228753Smm
862228753Smm	/* Logical block size must be > 0. */
863228753Smm	/* I've looked at Ecma 119 and can't find any stronger
864228753Smm	 * restriction on this field. */
865228753Smm	logical_block_size =
866228753Smm	    archive_le16dec(h + PVD_logical_block_size_offset);
867228753Smm	if (logical_block_size <= 0)
868228753Smm		return (0);
869228753Smm
870228753Smm	volume_block = archive_le32dec(h + PVD_volume_space_size_offset);
871228753Smm	if (volume_block <= SYSTEM_AREA_BLOCK+4)
872228753Smm		return (0);
873228753Smm
874228753Smm	/* File structure version must be 1 for ISO9660/ECMA119. */
875228753Smm	if (h[PVD_file_structure_version_offset] != 1)
876228753Smm		return (0);
877228753Smm
878228753Smm	/* Location of Occurrence of Type L Path Table must be
879228753Smm	 * available location,
880228753Smm	 * > SYSTEM_AREA_BLOCK(16) + 2 and < Volume Space Size. */
881228753Smm	location = archive_le32dec(h+PVD_type_1_path_table_offset);
882228753Smm	if (location < SYSTEM_AREA_BLOCK+2 || location >= volume_block)
883228753Smm		return (0);
884228753Smm
885228753Smm	/* The Type M Path Table must also be at a valid location
886228753Smm	 * (although ECMA 119 requires a Type M Path Table, WinISO and
887228753Smm	 * probably other programs omit it, so we permit a zero here)
888228753Smm	 *
889228753Smm	 * >= SYSTEM_AREA_BLOCK(16) + 2 and < Volume Space Size. */
890228753Smm	location = archive_be32dec(h+PVD_type_m_path_table_offset);
891228753Smm	if ((location > 0 && location < SYSTEM_AREA_BLOCK+2)
892228753Smm	    || location >= volume_block)
893228753Smm		return (0);
894228753Smm
895228753Smm	/* Reserved field must be 0. */
896229592Smm	/* FreeBSD: makefs erroneously created images with 0x20 */
897228753Smm	for (i = 0; i < PVD_reserved4_size; ++i)
898229592Smm		if (h[PVD_reserved4_offset + i] != 0 &&
899229592Smm		    h[PVD_reserved4_offset + i] != 32)
900228753Smm			return (0);
901228753Smm
902228753Smm	/* Reserved field must be 0. */
903228753Smm	for (i = 0; i < PVD_reserved5_size; ++i)
904228753Smm		if (h[PVD_reserved5_offset + i] != 0)
905228753Smm			return (0);
906228753Smm
907228753Smm	/* XXX TODO: Check other values for sanity; reject more
908228753Smm	 * malformed PVDs. XXX */
909228753Smm
910228753Smm	/* Read Root Directory Record in Volume Descriptor. */
911228753Smm	p = h + PVD_root_directory_record_offset;
912228753Smm	if (p[DR_length_offset] != 34)
913228753Smm		return (0);
914228753Smm
915228753Smm	iso9660->logical_block_size = logical_block_size;
916228753Smm	iso9660->volume_block = volume_block;
917228753Smm	iso9660->volume_size = logical_block_size * (uint64_t)volume_block;
918228753Smm	iso9660->primary.location = archive_le32dec(p + DR_extent_offset);
919228753Smm	iso9660->primary.size = archive_le32dec(p + DR_size_offset);
920228753Smm
921228753Smm	return (48);
922228753Smm}
923228753Smm
924228753Smmstatic int
925228753Smmread_children(struct archive_read *a, struct file_info *parent)
926228753Smm{
927228753Smm	struct iso9660 *iso9660;
928228753Smm	const unsigned char *b, *p;
929228753Smm	struct file_info *multi;
930228753Smm	size_t step;
931228753Smm
932228753Smm	iso9660 = (struct iso9660 *)(a->format->data);
933228753Smm	if (iso9660->current_position > parent->offset) {
934228753Smm		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
935228753Smm		    "Ignoring out-of-order directory (%s) %jd > %jd",
936228753Smm		    parent->name.s,
937228753Smm		    (intmax_t)iso9660->current_position,
938228753Smm		    (intmax_t)parent->offset);
939228753Smm		return (ARCHIVE_WARN);
940228753Smm	}
941228753Smm	if (parent->offset + parent->size > iso9660->volume_size) {
942228753Smm		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
943228753Smm		    "Directory is beyond end-of-media: %s",
944228753Smm		    parent->name.s);
945228753Smm		return (ARCHIVE_WARN);
946228753Smm	}
947228753Smm	if (iso9660->current_position < parent->offset) {
948228753Smm		int64_t skipsize;
949228753Smm
950228753Smm		skipsize = parent->offset - iso9660->current_position;
951228753Smm		skipsize = __archive_read_skip(a, skipsize);
952228753Smm		if (skipsize < 0)
953228753Smm			return ((int)skipsize);
954228753Smm		iso9660->current_position = parent->offset;
955228753Smm	}
956228753Smm
957228753Smm	step = ((parent->size + iso9660->logical_block_size -1) /
958228753Smm	    iso9660->logical_block_size) * iso9660->logical_block_size;
959228753Smm	b = __archive_read_ahead(a, step, NULL);
960228753Smm	if (b == NULL) {
961228753Smm		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
962228753Smm		    "Failed to read full block when scanning "
963228753Smm		    "ISO9660 directory list");
964228753Smm		return (ARCHIVE_FATAL);
965228753Smm	}
966228753Smm	__archive_read_consume(a, step);
967228753Smm	iso9660->current_position += step;
968228753Smm	multi = NULL;
969228753Smm	while (step) {
970228753Smm		p = b;
971228753Smm		b += iso9660->logical_block_size;
972228753Smm		step -= iso9660->logical_block_size;
973228753Smm		for (; *p != 0 && p < b && p + *p <= b; p += *p) {
974228753Smm			struct file_info *child;
975228753Smm
976228753Smm			/* N.B.: these special directory identifiers
977228753Smm			 * are 8 bit "values" even on a
978228753Smm			 * Joliet CD with UCS-2 (16bit) encoding.
979228753Smm			 */
980228753Smm
981228753Smm			/* Skip '.' entry. */
982228753Smm			if (*(p + DR_name_len_offset) == 1
983228753Smm			    && *(p + DR_name_offset) == '\0')
984228753Smm				continue;
985228753Smm			/* Skip '..' entry. */
986228753Smm			if (*(p + DR_name_len_offset) == 1
987228753Smm			    && *(p + DR_name_offset) == '\001')
988228753Smm				continue;
989228753Smm			child = parse_file_info(a, parent, p);
990228753Smm			if (child == NULL)
991228753Smm				return (ARCHIVE_FATAL);
992228753Smm			if (child->cl_offset == 0 &&
993228753Smm			    (child->multi_extent || multi != NULL)) {
994228753Smm				struct content *con;
995228753Smm
996228753Smm				if (multi == NULL) {
997228753Smm					multi = child;
998228753Smm					multi->contents.first = NULL;
999228753Smm					multi->contents.last =
1000228753Smm					    &(multi->contents.first);
1001228753Smm				}
1002228753Smm				con = malloc(sizeof(struct content));
1003228753Smm				if (con == NULL) {
1004228753Smm					archive_set_error(
1005228753Smm					    &a->archive, ENOMEM,
1006228753Smm					    "No memory for "
1007228753Smm					    "multi extent");
1008228753Smm					return (ARCHIVE_FATAL);
1009228753Smm				}
1010228753Smm				con->offset = child->offset;
1011228753Smm				con->size = child->size;
1012228753Smm				con->next = NULL;
1013228753Smm				*multi->contents.last = con;
1014228753Smm				multi->contents.last = &(con->next);
1015228753Smm				if (multi == child)
1016228753Smm					add_entry(iso9660, child);
1017228753Smm				else {
1018228753Smm					multi->size += child->size;
1019228753Smm					if (!child->multi_extent)
1020228753Smm						multi = NULL;
1021228753Smm				}
1022228753Smm			} else
1023228753Smm				add_entry(iso9660, child);
1024228753Smm		}
1025228753Smm	}
1026228753Smm
1027228753Smm	/* Read data which recorded by RRIP "CE" extension. */
1028228753Smm	if (read_CE(a, iso9660) != ARCHIVE_OK)
1029228753Smm		return (ARCHIVE_FATAL);
1030228753Smm
1031228753Smm	return (ARCHIVE_OK);
1032228753Smm}
1033228753Smm
1034228753Smmstatic int
1035228753Smmarchive_read_format_iso9660_read_header(struct archive_read *a,
1036228753Smm    struct archive_entry *entry)
1037228753Smm{
1038228753Smm	struct iso9660 *iso9660;
1039228753Smm	struct file_info *file;
1040228753Smm	int r, rd_r = ARCHIVE_OK;
1041228753Smm
1042228753Smm	iso9660 = (struct iso9660 *)(a->format->data);
1043228753Smm
1044228753Smm	if (!a->archive.archive_format) {
1045228753Smm		a->archive.archive_format = ARCHIVE_FORMAT_ISO9660;
1046228753Smm		a->archive.archive_format_name = "ISO9660";
1047228753Smm	}
1048228753Smm
1049228753Smm	if (iso9660->current_position == 0) {
1050228753Smm		int64_t skipsize;
1051228753Smm		struct vd *vd;
1052228753Smm		const void *block;
1053228753Smm		char seenJoliet;
1054228753Smm
1055228753Smm		vd = &(iso9660->primary);
1056228753Smm		if (!iso9660->opt_support_joliet)
1057228753Smm			iso9660->seenJoliet = 0;
1058228753Smm		if (iso9660->seenJoliet &&
1059228753Smm			vd->location > iso9660->joliet.location)
1060228753Smm			/* This condition is unlikely; by way of caution. */
1061228753Smm			vd = &(iso9660->joliet);
1062228753Smm
1063228753Smm		skipsize = LOGICAL_BLOCK_SIZE * vd->location;
1064228753Smm		skipsize = __archive_read_skip(a, skipsize);
1065228753Smm		if (skipsize < 0)
1066228753Smm			return ((int)skipsize);
1067228753Smm		iso9660->current_position = skipsize;
1068228753Smm
1069228753Smm		block = __archive_read_ahead(a, vd->size, NULL);
1070228753Smm		if (block == NULL) {
1071228753Smm			archive_set_error(&a->archive,
1072228753Smm			    ARCHIVE_ERRNO_MISC,
1073228753Smm			    "Failed to read full block when scanning "
1074228753Smm			    "ISO9660 directory list");
1075228753Smm			return (ARCHIVE_FATAL);
1076228753Smm		}
1077228753Smm
1078228753Smm		/*
1079228753Smm		 * While reading Root Directory, flag seenJoliet
1080228753Smm		 * must be zero to avoid converting special name
1081228753Smm		 * 0x00(Current Directory) and next byte to UCS2.
1082228753Smm		 */
1083228753Smm		seenJoliet = iso9660->seenJoliet;/* Save flag. */
1084228753Smm		iso9660->seenJoliet = 0;
1085228753Smm		file = parse_file_info(a, NULL, block);
1086228753Smm		if (file == NULL)
1087228753Smm			return (ARCHIVE_FATAL);
1088228753Smm		iso9660->seenJoliet = seenJoliet;
1089228753Smm		if (vd == &(iso9660->primary) && iso9660->seenRockridge
1090228753Smm		    && iso9660->seenJoliet)
1091228753Smm			/*
1092228753Smm			 * If iso image has RockRidge and Joliet,
1093228753Smm			 * we use RockRidge Extensions.
1094228753Smm			 */
1095228753Smm			iso9660->seenJoliet = 0;
1096228753Smm		if (vd == &(iso9660->primary) && !iso9660->seenRockridge
1097228753Smm		    && iso9660->seenJoliet) {
1098228753Smm			/* Switch reading data from primary to joliet. */
1099228753Smm			vd = &(iso9660->joliet);
1100228753Smm			skipsize = LOGICAL_BLOCK_SIZE * vd->location;
1101228753Smm			skipsize -= iso9660->current_position;
1102228753Smm			skipsize = __archive_read_skip(a, skipsize);
1103228753Smm			if (skipsize < 0)
1104228753Smm				return ((int)skipsize);
1105228753Smm			iso9660->current_position += skipsize;
1106228753Smm
1107228753Smm			block = __archive_read_ahead(a, vd->size, NULL);
1108228753Smm			if (block == NULL) {
1109228753Smm				archive_set_error(&a->archive,
1110228753Smm				    ARCHIVE_ERRNO_MISC,
1111228753Smm				    "Failed to read full block when scanning "
1112228753Smm				    "ISO9660 directory list");
1113228753Smm				return (ARCHIVE_FATAL);
1114228753Smm			}
1115228753Smm			seenJoliet = iso9660->seenJoliet;/* Save flag. */
1116228753Smm			iso9660->seenJoliet = 0;
1117228753Smm			file = parse_file_info(a, NULL, block);
1118228753Smm			if (file == NULL)
1119228753Smm				return (ARCHIVE_FATAL);
1120228753Smm			iso9660->seenJoliet = seenJoliet;
1121228753Smm		}
1122228753Smm		/* Store the root directory in the pending list. */
1123228753Smm		add_entry(iso9660, file);
1124228753Smm		if (iso9660->seenRockridge) {
1125228753Smm			a->archive.archive_format =
1126228753Smm			    ARCHIVE_FORMAT_ISO9660_ROCKRIDGE;
1127228753Smm			a->archive.archive_format_name =
1128228753Smm			    "ISO9660 with Rockridge extensions";
1129228753Smm		}
1130228753Smm	}
1131228753Smm
1132228753Smm	/* Get the next entry that appears after the current offset. */
1133228753Smm	r = next_entry_seek(a, iso9660, &file);
1134228753Smm	if (r != ARCHIVE_OK)
1135228753Smm		return (r);
1136228753Smm
1137228753Smm	iso9660->entry_bytes_remaining = file->size;
1138228753Smm	iso9660->entry_sparse_offset = 0; /* Offset for sparse-file-aware clients. */
1139228753Smm
1140228753Smm	if (file->offset + file->size > iso9660->volume_size) {
1141228753Smm		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1142228753Smm		    "File is beyond end-of-media: %s", file->name.s);
1143228753Smm		iso9660->entry_bytes_remaining = 0;
1144228753Smm		iso9660->entry_sparse_offset = 0;
1145228753Smm		return (ARCHIVE_WARN);
1146228753Smm	}
1147228753Smm
1148228753Smm	/* Set up the entry structure with information about this entry. */
1149228753Smm	archive_entry_set_mode(entry, file->mode);
1150228753Smm	archive_entry_set_uid(entry, file->uid);
1151228753Smm	archive_entry_set_gid(entry, file->gid);
1152228753Smm	archive_entry_set_nlink(entry, file->nlinks);
1153228753Smm	if (file->birthtime_is_set)
1154228753Smm		archive_entry_set_birthtime(entry, file->birthtime, 0);
1155228753Smm	else
1156228753Smm		archive_entry_unset_birthtime(entry);
1157228753Smm	archive_entry_set_mtime(entry, file->mtime, 0);
1158228753Smm	archive_entry_set_ctime(entry, file->ctime, 0);
1159228753Smm	archive_entry_set_atime(entry, file->atime, 0);
1160228753Smm	/* N.B.: Rock Ridge supports 64-bit device numbers. */
1161228753Smm	archive_entry_set_rdev(entry, (dev_t)file->rdev);
1162228753Smm	archive_entry_set_size(entry, iso9660->entry_bytes_remaining);
1163228753Smm	archive_string_empty(&iso9660->pathname);
1164228753Smm	archive_entry_set_pathname(entry,
1165228753Smm	    build_pathname(&iso9660->pathname, file));
1166228753Smm	if (file->symlink.s != NULL)
1167228753Smm		archive_entry_copy_symlink(entry, file->symlink.s);
1168228753Smm
1169228753Smm	/* Note: If the input isn't seekable, we can't rewind to
1170228753Smm	 * return the same body again, so if the next entry refers to
1171228753Smm	 * the same data, we have to return it as a hardlink to the
1172228753Smm	 * original entry. */
1173228753Smm	if (file->number != -1 &&
1174228753Smm	    file->number == iso9660->previous_number) {
1175228753Smm		archive_entry_set_hardlink(entry,
1176228753Smm		    iso9660->previous_pathname.s);
1177228753Smm		archive_entry_unset_size(entry);
1178228753Smm		iso9660->entry_bytes_remaining = 0;
1179228753Smm		iso9660->entry_sparse_offset = 0;
1180228753Smm		return (ARCHIVE_OK);
1181228753Smm	}
1182228753Smm
1183228753Smm	/* Except for the hardlink case above, if the offset of the
1184228753Smm	 * next entry is before our current position, we can't seek
1185228753Smm	 * backwards to extract it, so issue a warning.  Note that
1186228753Smm	 * this can only happen if this entry was added to the heap
1187228753Smm	 * after we passed this offset, that is, only if the directory
1188228753Smm	 * mentioning this entry is later than the body of the entry.
1189228753Smm	 * Such layouts are very unusual; most ISO9660 writers lay out
1190228753Smm	 * and record all directory information first, then store
1191228753Smm	 * all file bodies. */
1192228753Smm	/* TODO: Someday, libarchive's I/O core will support optional
1193228753Smm	 * seeking.  When that day comes, this code should attempt to
1194228753Smm	 * seek and only return the error if the seek fails.  That
1195228753Smm	 * will give us support for whacky ISO images that require
1196228753Smm	 * seeking while retaining the ability to read almost all ISO
1197228753Smm	 * images in a streaming fashion. */
1198228753Smm	if ((file->mode & AE_IFMT) != AE_IFDIR &&
1199228753Smm	    file->offset < iso9660->current_position) {
1200228753Smm		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1201228753Smm		    "Ignoring out-of-order file (%s) %jd < %jd",
1202228753Smm		    iso9660->pathname.s,
1203228753Smm		    (intmax_t)file->offset,
1204228753Smm		    (intmax_t)iso9660->current_position);
1205228753Smm		iso9660->entry_bytes_remaining = 0;
1206228753Smm		iso9660->entry_sparse_offset = 0;
1207228753Smm		return (ARCHIVE_WARN);
1208228753Smm	}
1209228753Smm
1210228753Smm	/* Initialize zisofs variables. */
1211228753Smm	iso9660->entry_zisofs.pz = file->pz;
1212228753Smm	if (file->pz) {
1213228753Smm#ifdef HAVE_ZLIB_H
1214228753Smm		struct zisofs  *zisofs;
1215228753Smm
1216228753Smm		zisofs = &iso9660->entry_zisofs;
1217228753Smm		zisofs->initialized = 0;
1218228753Smm		zisofs->pz_log2_bs = file->pz_log2_bs;
1219228753Smm		zisofs->pz_uncompressed_size = file->pz_uncompressed_size;
1220228753Smm		zisofs->pz_offset = 0;
1221228753Smm		zisofs->header_avail = 0;
1222228753Smm		zisofs->header_passed = 0;
1223228753Smm		zisofs->block_pointers_avail = 0;
1224228753Smm#endif
1225228753Smm		archive_entry_set_size(entry, file->pz_uncompressed_size);
1226228753Smm	}
1227228753Smm
1228228753Smm	iso9660->previous_number = file->number;
1229228753Smm	archive_strcpy(&iso9660->previous_pathname, iso9660->pathname.s);
1230228753Smm
1231228753Smm	/* Reset entry_bytes_remaining if the file is multi extent. */
1232228753Smm	iso9660->entry_content = file->contents.first;
1233228753Smm	if (iso9660->entry_content != NULL)
1234228753Smm		iso9660->entry_bytes_remaining = iso9660->entry_content->size;
1235228753Smm
1236228753Smm	if (archive_entry_filetype(entry) == AE_IFDIR) {
1237228753Smm		/* Overwrite nlinks by proper link number which is
1238228753Smm		 * calculated from number of sub directories. */
1239228753Smm		archive_entry_set_nlink(entry, 2 + file->subdirs);
1240228753Smm		/* Directory data has been read completely. */
1241228753Smm		iso9660->entry_bytes_remaining = 0;
1242228753Smm		iso9660->entry_sparse_offset = 0;
1243228753Smm	}
1244228753Smm
1245228753Smm	if (rd_r != ARCHIVE_OK)
1246228753Smm		return (rd_r);
1247228753Smm	return (ARCHIVE_OK);
1248228753Smm}
1249228753Smm
1250228753Smmstatic int
1251228753Smmarchive_read_format_iso9660_read_data_skip(struct archive_read *a)
1252228753Smm{
1253228753Smm	/* Because read_next_header always does an explicit skip
1254228753Smm	 * to the next entry, we don't need to do anything here. */
1255228753Smm	(void)a; /* UNUSED */
1256228753Smm	return (ARCHIVE_OK);
1257228753Smm}
1258228753Smm
1259228753Smm#ifdef HAVE_ZLIB_H
1260228753Smm
1261228753Smmstatic int
1262228753Smmzisofs_read_data(struct archive_read *a,
1263228753Smm    const void **buff, size_t *size, off_t *offset)
1264228753Smm{
1265228753Smm	struct iso9660 *iso9660;
1266228753Smm	struct zisofs  *zisofs;
1267228753Smm	const unsigned char *p;
1268228753Smm	size_t avail;
1269228753Smm	ssize_t bytes_read;
1270228753Smm	size_t uncompressed_size;
1271228753Smm	int r;
1272228753Smm
1273228753Smm	iso9660 = (struct iso9660 *)(a->format->data);
1274228753Smm	zisofs = &iso9660->entry_zisofs;
1275228753Smm
1276228753Smm	p = __archive_read_ahead(a, 1, &bytes_read);
1277228753Smm	if (bytes_read <= 0) {
1278228753Smm		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1279228753Smm		    "Truncated zisofs file body");
1280228753Smm		return (ARCHIVE_FATAL);
1281228753Smm	}
1282228753Smm	if (bytes_read > iso9660->entry_bytes_remaining)
1283228753Smm		bytes_read = iso9660->entry_bytes_remaining;
1284228753Smm	avail = bytes_read;
1285228753Smm	uncompressed_size = 0;
1286228753Smm
1287228753Smm	if (!zisofs->initialized) {
1288228753Smm		size_t ceil, xsize;
1289228753Smm
1290228753Smm		/* Allocate block pointers buffer. */
1291228753Smm		ceil = (zisofs->pz_uncompressed_size +
1292228753Smm			(1LL << zisofs->pz_log2_bs) - 1)
1293228753Smm			>> zisofs->pz_log2_bs;
1294228753Smm		xsize = (ceil + 1) * 4;
1295228753Smm		if (zisofs->block_pointers_alloc < xsize) {
1296228753Smm			size_t alloc;
1297228753Smm
1298228753Smm			if (zisofs->block_pointers != NULL)
1299228753Smm				free(zisofs->block_pointers);
1300228753Smm			alloc = ((xsize >> 10) + 1) << 10;
1301228753Smm			zisofs->block_pointers = malloc(alloc);
1302228753Smm			if (zisofs->block_pointers == NULL) {
1303228753Smm				archive_set_error(&a->archive, ENOMEM,
1304228753Smm				    "No memory for zisofs decompression");
1305228753Smm				return (ARCHIVE_FATAL);
1306228753Smm			}
1307228753Smm			zisofs->block_pointers_alloc = alloc;
1308228753Smm		}
1309228753Smm		zisofs->block_pointers_size = xsize;
1310228753Smm
1311228753Smm		/* Allocate uncompressed data buffer. */
1312228753Smm		xsize = 1UL << zisofs->pz_log2_bs;
1313228753Smm		if (zisofs->uncompressed_buffer_size < xsize) {
1314228753Smm			if (zisofs->uncompressed_buffer != NULL)
1315228753Smm				free(zisofs->uncompressed_buffer);
1316228753Smm			zisofs->uncompressed_buffer = malloc(xsize);
1317228753Smm			if (zisofs->uncompressed_buffer == NULL) {
1318228753Smm				archive_set_error(&a->archive, ENOMEM,
1319228753Smm				    "No memory for zisofs decompression");
1320228753Smm				return (ARCHIVE_FATAL);
1321228753Smm			}
1322228753Smm		}
1323228753Smm		zisofs->uncompressed_buffer_size = xsize;
1324228753Smm
1325228753Smm		/*
1326228753Smm		 * Read the file header, and check the magic code of zisofs.
1327228753Smm		 */
1328228753Smm		if (zisofs->header_avail < sizeof(zisofs->header)) {
1329228753Smm			xsize = sizeof(zisofs->header) - zisofs->header_avail;
1330228753Smm			if (avail < xsize)
1331228753Smm				xsize = avail;
1332228753Smm			memcpy(zisofs->header + zisofs->header_avail, p, xsize);
1333228753Smm			zisofs->header_avail += xsize;
1334228753Smm			avail -= xsize;
1335228753Smm			p += xsize;
1336228753Smm		}
1337228753Smm		if (!zisofs->header_passed &&
1338228753Smm		    zisofs->header_avail == sizeof(zisofs->header)) {
1339228753Smm			int err = 0;
1340228753Smm
1341228753Smm			if (memcmp(zisofs->header, zisofs_magic,
1342228753Smm			    sizeof(zisofs_magic)) != 0)
1343228753Smm				err = 1;
1344228753Smm			if (archive_le32dec(zisofs->header + 8)
1345228753Smm			    != zisofs->pz_uncompressed_size)
1346228753Smm				err = 1;
1347228753Smm			if (zisofs->header[12] != 4)
1348228753Smm				err = 1;
1349228753Smm			if (zisofs->header[13] != zisofs->pz_log2_bs)
1350228753Smm				err = 1;
1351228753Smm			if (err) {
1352228753Smm				archive_set_error(&a->archive,
1353228753Smm				    ARCHIVE_ERRNO_FILE_FORMAT,
1354228753Smm				    "Illegal zisofs file body");
1355228753Smm				return (ARCHIVE_FATAL);
1356228753Smm			}
1357228753Smm			zisofs->header_passed = 1;
1358228753Smm		}
1359228753Smm		/*
1360228753Smm		 * Read block pointers.
1361228753Smm		 */
1362228753Smm		if (zisofs->header_passed &&
1363228753Smm		    zisofs->block_pointers_avail < zisofs->block_pointers_size) {
1364228753Smm			xsize = zisofs->block_pointers_size
1365228753Smm			    - zisofs->block_pointers_avail;
1366228753Smm			if (avail < xsize)
1367228753Smm				xsize = avail;
1368228753Smm			memcpy(zisofs->block_pointers
1369228753Smm			    + zisofs->block_pointers_avail, p, xsize);
1370228753Smm			zisofs->block_pointers_avail += xsize;
1371228753Smm			avail -= xsize;
1372228753Smm			p += xsize;
1373228753Smm		    	if (zisofs->block_pointers_avail
1374228753Smm			    == zisofs->block_pointers_size) {
1375228753Smm				/* We've got all block pointers and initialize
1376228753Smm				 * related variables.	*/
1377228753Smm				zisofs->block_off = 0;
1378228753Smm				zisofs->block_avail = 0;
1379228753Smm				/* Complete a initialization */
1380228753Smm				zisofs->initialized = 1;
1381228753Smm			}
1382228753Smm		}
1383228753Smm
1384228753Smm		if (!zisofs->initialized)
1385228753Smm			goto next_data; /* We need more datas. */
1386228753Smm	}
1387228753Smm
1388228753Smm	/*
1389228753Smm	 * Get block offsets from block pointers.
1390228753Smm	 */
1391228753Smm	if (zisofs->block_avail == 0) {
1392228753Smm		uint32_t bst, bed;
1393228753Smm
1394228753Smm		if (zisofs->block_off + 4 >= zisofs->block_pointers_size) {
1395228753Smm			/* There isn't a pair of offsets. */
1396228753Smm			archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1397228753Smm			    "Illegal zisofs block pointers");
1398228753Smm			return (ARCHIVE_FATAL);
1399228753Smm		}
1400228753Smm		bst = archive_le32dec(zisofs->block_pointers + zisofs->block_off);
1401228753Smm		if (bst != zisofs->pz_offset + (bytes_read - avail)) {
1402228753Smm			/* TODO: Should we seek offset of current file by bst ? */
1403228753Smm			archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1404228753Smm			    "Illegal zisofs block pointers(cannot seek)");
1405228753Smm			return (ARCHIVE_FATAL);
1406228753Smm		}
1407228753Smm		bed = archive_le32dec(
1408228753Smm		    zisofs->block_pointers + zisofs->block_off + 4);
1409228753Smm		if (bed < bst) {
1410228753Smm			archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1411228753Smm			    "Illegal zisofs block pointers");
1412228753Smm			return (ARCHIVE_FATAL);
1413228753Smm		}
1414228753Smm		zisofs->block_avail = bed - bst;
1415228753Smm		zisofs->block_off += 4;
1416228753Smm
1417228753Smm		/* Initialize compression library for new block. */
1418228753Smm		if (zisofs->stream_valid)
1419228753Smm			r = inflateReset(&zisofs->stream);
1420228753Smm		else
1421228753Smm			r = inflateInit(&zisofs->stream);
1422228753Smm		if (r != Z_OK) {
1423228753Smm			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1424228753Smm			    "Can't initialize zisofs decompression.");
1425228753Smm			return (ARCHIVE_FATAL);
1426228753Smm		}
1427228753Smm		zisofs->stream_valid = 1;
1428228753Smm		zisofs->stream.total_in = 0;
1429228753Smm		zisofs->stream.total_out = 0;
1430228753Smm	}
1431228753Smm
1432228753Smm	/*
1433228753Smm	 * Make uncompressed datas.
1434228753Smm	 */
1435228753Smm	if (zisofs->block_avail == 0) {
1436228753Smm		memset(zisofs->uncompressed_buffer, 0,
1437228753Smm		    zisofs->uncompressed_buffer_size);
1438228753Smm		uncompressed_size = zisofs->uncompressed_buffer_size;
1439228753Smm	} else {
1440228753Smm		zisofs->stream.next_in = (Bytef *)(uintptr_t)(const void *)p;
1441228753Smm		if (avail > zisofs->block_avail)
1442228753Smm			zisofs->stream.avail_in = zisofs->block_avail;
1443228753Smm		else
1444228753Smm			zisofs->stream.avail_in = avail;
1445228753Smm		zisofs->stream.next_out = zisofs->uncompressed_buffer;
1446228753Smm		zisofs->stream.avail_out = zisofs->uncompressed_buffer_size;
1447228753Smm
1448228753Smm		r = inflate(&zisofs->stream, 0);
1449228753Smm		switch (r) {
1450228753Smm		case Z_OK: /* Decompressor made some progress.*/
1451228753Smm		case Z_STREAM_END: /* Found end of stream. */
1452228753Smm			break;
1453228753Smm		default:
1454228753Smm			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1455228753Smm			    "zisofs decompression failed (%d)", r);
1456228753Smm			return (ARCHIVE_FATAL);
1457228753Smm		}
1458228753Smm		uncompressed_size =
1459228753Smm		    zisofs->uncompressed_buffer_size - zisofs->stream.avail_out;
1460228753Smm		avail -= zisofs->stream.next_in - p;
1461228753Smm		zisofs->block_avail -= zisofs->stream.next_in - p;
1462228753Smm	}
1463228753Smmnext_data:
1464228753Smm	bytes_read -= avail;
1465228753Smm	*buff = zisofs->uncompressed_buffer;
1466228753Smm	*size = uncompressed_size;
1467228753Smm	*offset = iso9660->entry_sparse_offset;
1468228753Smm	iso9660->entry_sparse_offset += uncompressed_size;
1469228753Smm	iso9660->entry_bytes_remaining -= bytes_read;
1470228753Smm	iso9660->current_position += bytes_read;
1471228753Smm	zisofs->pz_offset += bytes_read;
1472228753Smm	__archive_read_consume(a, bytes_read);
1473228753Smm
1474228753Smm	return (ARCHIVE_OK);
1475228753Smm}
1476228753Smm
1477228753Smm#else /* HAVE_ZLIB_H */
1478228753Smm
1479228753Smmstatic int
1480228753Smmzisofs_read_data(struct archive_read *a,
1481228753Smm    const void **buff, size_t *size, off_t *offset)
1482228753Smm{
1483228753Smm
1484228753Smm	(void)buff;/* UNUSED */
1485228753Smm	(void)size;/* UNUSED */
1486228753Smm	(void)offset;/* UNUSED */
1487228753Smm	archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1488228753Smm	    "zisofs is not supported on this platform.");
1489228753Smm	return (ARCHIVE_FAILED);
1490228753Smm}
1491228753Smm
1492228753Smm#endif /* HAVE_ZLIB_H */
1493228753Smm
1494228753Smmstatic int
1495228753Smmarchive_read_format_iso9660_read_data(struct archive_read *a,
1496228753Smm    const void **buff, size_t *size, off_t *offset)
1497228753Smm{
1498228753Smm	ssize_t bytes_read;
1499228753Smm	struct iso9660 *iso9660;
1500228753Smm
1501228753Smm	iso9660 = (struct iso9660 *)(a->format->data);
1502228753Smm	if (iso9660->entry_bytes_remaining <= 0) {
1503228753Smm		if (iso9660->entry_content != NULL)
1504228753Smm			iso9660->entry_content = iso9660->entry_content->next;
1505228753Smm		if (iso9660->entry_content == NULL) {
1506228753Smm			*buff = NULL;
1507228753Smm			*size = 0;
1508228753Smm			*offset = iso9660->entry_sparse_offset;
1509228753Smm			return (ARCHIVE_EOF);
1510228753Smm		}
1511228753Smm		/* Seek forward to the start of the entry. */
1512228753Smm		if (iso9660->current_position < iso9660->entry_content->offset) {
1513228753Smm			int64_t step;
1514228753Smm
1515228753Smm			step = iso9660->entry_content->offset -
1516228753Smm			    iso9660->current_position;
1517228753Smm			step = __archive_read_skip(a, step);
1518228753Smm			if (step < 0)
1519228753Smm				return ((int)step);
1520228753Smm			iso9660->current_position =
1521228753Smm			    iso9660->entry_content->offset;
1522228753Smm		}
1523228753Smm		if (iso9660->entry_content->offset < iso9660->current_position) {
1524228753Smm			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1525228753Smm			    "Ignoring out-of-order file (%s) %jd < %jd",
1526228753Smm			    iso9660->pathname.s,
1527228753Smm			    (intmax_t)iso9660->entry_content->offset,
1528228753Smm			    (intmax_t)iso9660->current_position);
1529228753Smm			*buff = NULL;
1530228753Smm			*size = 0;
1531228753Smm			*offset = iso9660->entry_sparse_offset;
1532228753Smm			return (ARCHIVE_WARN);
1533228753Smm		}
1534228753Smm		iso9660->entry_bytes_remaining = iso9660->entry_content->size;
1535228753Smm	}
1536228753Smm	if (iso9660->entry_zisofs.pz)
1537228753Smm		return (zisofs_read_data(a, buff, size, offset));
1538228753Smm
1539228753Smm	*buff = __archive_read_ahead(a, 1, &bytes_read);
1540228753Smm	if (bytes_read == 0)
1541228753Smm		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1542228753Smm		    "Truncated input file");
1543228753Smm	if (*buff == NULL)
1544228753Smm		return (ARCHIVE_FATAL);
1545228753Smm	if (bytes_read > iso9660->entry_bytes_remaining)
1546228753Smm		bytes_read = iso9660->entry_bytes_remaining;
1547228753Smm	*size = bytes_read;
1548228753Smm	*offset = iso9660->entry_sparse_offset;
1549228753Smm	iso9660->entry_sparse_offset += bytes_read;
1550228753Smm	iso9660->entry_bytes_remaining -= bytes_read;
1551228753Smm	iso9660->current_position += bytes_read;
1552228753Smm	__archive_read_consume(a, bytes_read);
1553228753Smm	return (ARCHIVE_OK);
1554228753Smm}
1555228753Smm
1556228753Smmstatic int
1557228753Smmarchive_read_format_iso9660_cleanup(struct archive_read *a)
1558228753Smm{
1559228753Smm	struct iso9660 *iso9660;
1560228753Smm	int r = ARCHIVE_OK;
1561228753Smm
1562228753Smm	iso9660 = (struct iso9660 *)(a->format->data);
1563228753Smm	release_files(iso9660);
1564228753Smm	free(iso9660->read_ce_req.reqs);
1565228753Smm	archive_string_free(&iso9660->pathname);
1566228753Smm	archive_string_free(&iso9660->previous_pathname);
1567228753Smm	if (iso9660->pending_files.files)
1568228753Smm		free(iso9660->pending_files.files);
1569228753Smm#ifdef HAVE_ZLIB_H
1570228753Smm	free(iso9660->entry_zisofs.uncompressed_buffer);
1571228753Smm	free(iso9660->entry_zisofs.block_pointers);
1572228753Smm	if (iso9660->entry_zisofs.stream_valid) {
1573228753Smm		if (inflateEnd(&iso9660->entry_zisofs.stream) != Z_OK) {
1574228753Smm			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1575228753Smm			    "Failed to clean up zlib decompressor");
1576228753Smm			r = ARCHIVE_FATAL;
1577228753Smm		}
1578228753Smm	}
1579228753Smm#endif
1580228753Smm	free(iso9660);
1581228753Smm	(a->format->data) = NULL;
1582228753Smm	return (r);
1583228753Smm}
1584228753Smm
1585228753Smm/*
1586228753Smm * This routine parses a single ISO directory record, makes sense
1587228753Smm * of any extensions, and stores the result in memory.
1588228753Smm */
1589228753Smmstatic struct file_info *
1590228753Smmparse_file_info(struct archive_read *a, struct file_info *parent,
1591228753Smm    const unsigned char *isodirrec)
1592228753Smm{
1593228753Smm	struct iso9660 *iso9660;
1594228753Smm	struct file_info *file;
1595228753Smm	size_t name_len;
1596228753Smm	const unsigned char *rr_start, *rr_end;
1597228753Smm	const unsigned char *p;
1598228753Smm	size_t dr_len;
1599228753Smm	uint64_t fsize;
1600228753Smm	int32_t location;
1601228753Smm	int flags;
1602228753Smm
1603228753Smm	iso9660 = (struct iso9660 *)(a->format->data);
1604228753Smm
1605228753Smm	dr_len = (size_t)isodirrec[DR_length_offset];
1606228753Smm	name_len = (size_t)isodirrec[DR_name_len_offset];
1607228753Smm	location = archive_le32dec(isodirrec + DR_extent_offset);
1608228753Smm	fsize = toi(isodirrec + DR_size_offset, DR_size_size);
1609228753Smm	/* Sanity check that dr_len needs at least 34. */
1610228753Smm	if (dr_len < 34) {
1611228753Smm		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1612228753Smm		    "Invalid length of directory record");
1613228753Smm		return (NULL);
1614228753Smm	}
1615228753Smm	/* Sanity check that name_len doesn't exceed dr_len. */
1616228753Smm	if (dr_len - 33 < name_len || name_len == 0) {
1617228753Smm		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1618228753Smm		    "Invalid length of file identifier");
1619228753Smm		return (NULL);
1620228753Smm	}
1621228753Smm	/* Sanity check that location doesn't exceed volume block.
1622228753Smm	 * Don't check lower limit of location; it's possibility
1623228753Smm	 * the location has negative value when file type is symbolic
1624228753Smm	 * link or file size is zero. As far as I know latest mkisofs
1625228753Smm	 * do that.
1626228753Smm	 */
1627228753Smm	if (location > 0 &&
1628228753Smm	    (location + ((fsize + iso9660->logical_block_size -1)
1629229592Smm	       / iso9660->logical_block_size))
1630229592Smm		> (uint32_t)iso9660->volume_block) {
1631228753Smm		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1632228753Smm		    "Invalid location of extent of file");
1633228753Smm		return (NULL);
1634228753Smm	}
1635228753Smm
1636228753Smm	/* Create a new file entry and copy data from the ISO dir record. */
1637228753Smm	file = (struct file_info *)malloc(sizeof(*file));
1638228753Smm	if (file == NULL) {
1639228753Smm		archive_set_error(&a->archive, ENOMEM,
1640228753Smm		    "No memory for file entry");
1641228753Smm		return (NULL);
1642228753Smm	}
1643228753Smm	memset(file, 0, sizeof(*file));
1644228753Smm	file->parent = parent;
1645228753Smm	file->offset = iso9660->logical_block_size * (uint64_t)location;
1646228753Smm	file->size = fsize;
1647228753Smm	file->mtime = isodate7(isodirrec + DR_date_offset);
1648228753Smm	file->ctime = file->atime = file->mtime;
1649228753Smm	file->rede_files.first = NULL;
1650228753Smm	file->rede_files.last = &(file->rede_files.first);
1651228753Smm
1652228753Smm	p = isodirrec + DR_name_offset;
1653228753Smm	/* Rockridge extensions (if any) follow name.  Compute this
1654228753Smm	 * before fidgeting the name_len below. */
1655228753Smm	rr_start = p + name_len + (name_len & 1 ? 0 : 1);
1656228753Smm	rr_end = isodirrec + dr_len;
1657228753Smm
1658228753Smm	if (iso9660->seenJoliet) {
1659228753Smm		/* Joliet names are max 64 chars (128 bytes) according to spec,
1660228753Smm		 * but genisoimage/mkisofs allows recording longer Joliet
1661228753Smm		 * names which are 103 UCS2 characters(206 bytes) by their
1662228753Smm		 * option '-joliet-long'.
1663228753Smm		 */
1664228753Smm		wchar_t wbuff[103+1], *wp;
1665228753Smm		const unsigned char *c;
1666228753Smm
1667228753Smm		if (name_len > 206)
1668228753Smm			name_len = 206;
1669228753Smm		/* convert BE UTF-16 to wchar_t */
1670228753Smm		for (c = p, wp = wbuff;
1671228753Smm				c < (p + name_len) &&
1672228753Smm				wp < (wbuff + sizeof(wbuff)/sizeof(*wbuff) - 1);
1673228753Smm				c += 2) {
1674228753Smm			*wp++ = (((255 & (int)c[0]) << 8) | (255 & (int)c[1]));
1675228753Smm		}
1676228753Smm		*wp = L'\0';
1677228753Smm
1678228753Smm#if 0 /* untested code, is it at all useful on Joliet? */
1679228753Smm		/* trim trailing first version and dot from filename.
1680228753Smm		 *
1681228753Smm		 * Remember we where in UTF-16BE land!
1682228753Smm		 * SEPARATOR 1 (.) and SEPARATOR 2 (;) are both
1683228753Smm		 * 16 bits big endian characters on Joliet.
1684228753Smm		 *
1685228753Smm		 * TODO: sanitize filename?
1686228753Smm		 *       Joliet allows any UCS-2 char except:
1687228753Smm		 *       *, /, :, ;, ? and \.
1688228753Smm		 */
1689228753Smm		/* Chop off trailing ';1' from files. */
1690228753Smm		if (*(wp-2) == ';' && *(wp-1) == '1') {
1691228753Smm			wp-=2;
1692228753Smm			*wp = L'\0';
1693228753Smm		}
1694228753Smm
1695228753Smm		/* Chop off trailing '.' from filenames. */
1696228753Smm		if (*(wp-1) == '.')
1697228753Smm			*(--wp) = L'\0';
1698228753Smm#endif
1699228753Smm
1700228753Smm		/* store the result in the file name field. */
1701228753Smm		archive_strappend_w_utf8(&file->name, wbuff);
1702228753Smm	} else {
1703228753Smm		/* Chop off trailing ';1' from files. */
1704228753Smm		if (name_len > 2 && p[name_len - 2] == ';' &&
1705228753Smm				p[name_len - 1] == '1')
1706228753Smm			name_len -= 2;
1707228753Smm		/* Chop off trailing '.' from filenames. */
1708228753Smm		if (name_len > 1 && p[name_len - 1] == '.')
1709228753Smm			--name_len;
1710228753Smm
1711228753Smm		archive_strncpy(&file->name, (const char *)p, name_len);
1712228753Smm	}
1713228753Smm
1714228753Smm	flags = isodirrec[DR_flags_offset];
1715228753Smm	if (flags & 0x02)
1716228753Smm		file->mode = AE_IFDIR | 0700;
1717228753Smm	else
1718228753Smm		file->mode = AE_IFREG | 0400;
1719228753Smm	if (flags & 0x80)
1720228753Smm		file->multi_extent = 1;
1721228753Smm	else
1722228753Smm		file->multi_extent = 0;
1723228753Smm	/*
1724228753Smm	 * Use location for file number.
1725228753Smm	 * File number is treated as inode number to find out harlink
1726228753Smm	 * target. If Rockridge extensions is being used, file number
1727228753Smm	 * will be overwritten by FILE SERIAL NUMBER of RRIP "PX"
1728228753Smm	 * extension.
1729228753Smm	 * NOTE: Old mkisofs did not record that FILE SERIAL NUMBER
1730228753Smm	 * in ISO images.
1731228753Smm	 */
1732228753Smm	if (file->size == 0 && location >= 0)
1733228753Smm		/* If file->size is zero, its location points wrong place.
1734228753Smm		 * Dot not use it for file number.
1735228753Smm		 * When location has negative value, it can be used
1736228753Smm		 * for file number.
1737228753Smm		 */
1738228753Smm		file->number = -1;
1739228753Smm	else
1740228753Smm		file->number = (int64_t)(uint32_t)location;
1741228753Smm
1742228753Smm	/* Rockridge extensions overwrite information from above. */
1743228753Smm	if (iso9660->opt_support_rockridge) {
1744228753Smm		if (parent == NULL && rr_end - rr_start >= 7) {
1745228753Smm			p = rr_start;
1746228753Smm			if (p[0] == 'S' && p[1] == 'P'
1747228753Smm			    && p[2] == 7 && p[3] == 1
1748228753Smm			    && p[4] == 0xBE && p[5] == 0xEF) {
1749228753Smm				/*
1750228753Smm				 * SP extension stores the suspOffset
1751228753Smm				 * (Number of bytes to skip between
1752228753Smm				 * filename and SUSP records.)
1753228753Smm				 * It is mandatory by the SUSP standard
1754228753Smm				 * (IEEE 1281).
1755228753Smm				 *
1756228753Smm				 * It allows SUSP to coexist with
1757228753Smm				 * non-SUSP uses of the System
1758228753Smm				 * Use Area by placing non-SUSP data
1759228753Smm				 * before SUSP data.
1760228753Smm				 *
1761228753Smm				 * SP extension must be in the root
1762228753Smm				 * directory entry, disable all SUSP
1763228753Smm				 * processing if not found.
1764228753Smm				 */
1765228753Smm				iso9660->suspOffset = p[6];
1766228753Smm				iso9660->seenSUSP = 1;
1767228753Smm				rr_start += 7;
1768228753Smm			}
1769228753Smm		}
1770228753Smm		if (iso9660->seenSUSP) {
1771228753Smm			int r;
1772228753Smm
1773228753Smm			file->name_continues = 0;
1774228753Smm			file->symlink_continues = 0;
1775228753Smm			rr_start += iso9660->suspOffset;
1776228753Smm			r = parse_rockridge(a, file, rr_start, rr_end);
1777228753Smm			if (r != ARCHIVE_OK) {
1778228753Smm				free(file);
1779228753Smm				return (NULL);
1780228753Smm			}
1781228753Smm		} else
1782228753Smm			/* If there isn't SUSP, disable parsing
1783228753Smm			 * rock ridge extensions. */
1784228753Smm			iso9660->opt_support_rockridge = 0;
1785228753Smm	}
1786228753Smm
1787228753Smm	file->nlinks = 1;/* Reset nlink. we'll calculate it later. */
1788228753Smm	/* Tell file's parent how many children that parent has. */
1789228753Smm	if (parent != NULL && (flags & 0x02))
1790228753Smm		parent->subdirs++;
1791228753Smm
1792228753Smm	if (iso9660->seenRockridge) {
1793228753Smm		if (parent != NULL && parent->parent == NULL &&
1794228753Smm		    (flags & 0x02) && iso9660->rr_moved == NULL &&
1795228753Smm		    (strcmp(file->name.s, "rr_moved") == 0 ||
1796228753Smm		     strcmp(file->name.s, ".rr_moved") == 0)) {
1797228753Smm			iso9660->rr_moved = file;
1798228753Smm			file->rr_moved = 1;
1799228753Smm			file->rr_moved_has_re_only = 1;
1800228753Smm			file->re = 0;
1801228753Smm			parent->subdirs--;
1802228753Smm		} else if (file->re) {
1803229592Smm			/*
1804229592Smm			 * Sanity check: file's parent is rr_moved.
1805229592Smm			 */
1806229592Smm			if (parent == NULL || parent->rr_moved == 0) {
1807229592Smm				archive_set_error(&a->archive,
1808229592Smm				    ARCHIVE_ERRNO_MISC,
1809229592Smm				    "Invalid Rockridge RE");
1810229592Smm				return (NULL);
1811228753Smm			}
1812229592Smm			/*
1813229592Smm			 * Sanity check: file does not have "CL" extension.
1814229592Smm			 */
1815229592Smm			if (file->cl_offset) {
1816229592Smm				archive_set_error(&a->archive,
1817229592Smm				    ARCHIVE_ERRNO_MISC,
1818229592Smm				    "Invalid Rockridge RE and CL");
1819229592Smm				return (NULL);
1820229592Smm			}
1821229592Smm			/*
1822229592Smm			 * Sanity check: The file type must be a directory.
1823229592Smm			 */
1824229592Smm			if ((flags & 0x02) == 0) {
1825229592Smm				archive_set_error(&a->archive,
1826229592Smm				    ARCHIVE_ERRNO_MISC,
1827229592Smm				    "Invalid Rockridge RE");
1828229592Smm				return (NULL);
1829229592Smm			}
1830228753Smm		} else if (parent != NULL && parent->rr_moved)
1831228753Smm			file->rr_moved_has_re_only = 0;
1832228753Smm		else if (parent != NULL && (flags & 0x02) &&
1833228753Smm		    (parent->re || parent->re_descendant))
1834228753Smm			file->re_descendant = 1;
1835229592Smm		if (file->cl_offset) {
1836229592Smm			struct file_info *r;
1837229592Smm
1838229592Smm			if (parent == NULL || parent->parent == NULL) {
1839229592Smm				archive_set_error(&a->archive,
1840229592Smm				    ARCHIVE_ERRNO_MISC,
1841229592Smm				    "Invalid Rockridge CL");
1842229592Smm				return (NULL);
1843229592Smm			}
1844229592Smm			/*
1845229592Smm			 * Sanity check: The file type must be a regular file.
1846229592Smm			 */
1847229592Smm			if ((flags & 0x02) != 0) {
1848229592Smm				archive_set_error(&a->archive,
1849229592Smm				    ARCHIVE_ERRNO_MISC,
1850229592Smm				    "Invalid Rockridge CL");
1851229592Smm				return (NULL);
1852229592Smm			}
1853228753Smm			parent->subdirs++;
1854228753Smm			/* Overwrite an offset and a number of this "CL" entry
1855228753Smm			 * to appear before other dirs. "+1" to those is to
1856228753Smm			 * make sure to appear after "RE" entry which this
1857228753Smm			 * "CL" entry should be connected with. */
1858228753Smm			file->offset = file->number = file->cl_offset + 1;
1859229592Smm
1860229592Smm			/*
1861229592Smm			 * Sanity check: cl_offset does not point at its
1862229592Smm			 * the parents or itself.
1863229592Smm			 */
1864229592Smm			for (r = parent; r; r = r->parent) {
1865229592Smm				if (r->offset == file->cl_offset) {
1866229592Smm					archive_set_error(&a->archive,
1867229592Smm					    ARCHIVE_ERRNO_MISC,
1868229592Smm					    "Invalid Rockridge CL");
1869229592Smm					return (NULL);
1870229592Smm				}
1871229592Smm			}
1872229592Smm			if (file->cl_offset == file->offset ||
1873229592Smm			    parent->rr_moved) {
1874229592Smm				archive_set_error(&a->archive,
1875229592Smm				    ARCHIVE_ERRNO_MISC,
1876229592Smm				    "Invalid Rockridge CL");
1877229592Smm				return (NULL);
1878229592Smm			}
1879228753Smm		}
1880228753Smm	}
1881228753Smm
1882228753Smm#if DEBUG
1883228753Smm	/* DEBUGGING: Warn about attributes I don't yet fully support. */
1884228753Smm	if ((flags & ~0x02) != 0) {
1885228753Smm		fprintf(stderr, "\n ** Unrecognized flag: ");
1886228753Smm		dump_isodirrec(stderr, isodirrec);
1887228753Smm		fprintf(stderr, "\n");
1888228753Smm	} else if (toi(isodirrec + DR_volume_sequence_number_offset, 2) != 1) {
1889228753Smm		fprintf(stderr, "\n ** Unrecognized sequence number: ");
1890228753Smm		dump_isodirrec(stderr, isodirrec);
1891228753Smm		fprintf(stderr, "\n");
1892228753Smm	} else if (*(isodirrec + DR_file_unit_size_offset) != 0) {
1893228753Smm		fprintf(stderr, "\n ** Unexpected file unit size: ");
1894228753Smm		dump_isodirrec(stderr, isodirrec);
1895228753Smm		fprintf(stderr, "\n");
1896228753Smm	} else if (*(isodirrec + DR_interleave_offset) != 0) {
1897228753Smm		fprintf(stderr, "\n ** Unexpected interleave: ");
1898228753Smm		dump_isodirrec(stderr, isodirrec);
1899228753Smm		fprintf(stderr, "\n");
1900228753Smm	} else if (*(isodirrec + DR_ext_attr_length_offset) != 0) {
1901228753Smm		fprintf(stderr, "\n ** Unexpected extended attribute length: ");
1902228753Smm		dump_isodirrec(stderr, isodirrec);
1903228753Smm		fprintf(stderr, "\n");
1904228753Smm	}
1905228753Smm#endif
1906228753Smm	register_file(iso9660, file);
1907228753Smm	return (file);
1908228753Smm}
1909228753Smm
1910228753Smmstatic int
1911228753Smmparse_rockridge(struct archive_read *a, struct file_info *file,
1912228753Smm    const unsigned char *p, const unsigned char *end)
1913228753Smm{
1914228753Smm	struct iso9660 *iso9660;
1915228753Smm
1916228753Smm	iso9660 = (struct iso9660 *)(a->format->data);
1917228753Smm
1918228753Smm	while (p + 4 <= end  /* Enough space for another entry. */
1919228753Smm	    && p[0] >= 'A' && p[0] <= 'Z' /* Sanity-check 1st char of name. */
1920228753Smm	    && p[1] >= 'A' && p[1] <= 'Z' /* Sanity-check 2nd char of name. */
1921228753Smm	    && p[2] >= 4 /* Sanity-check length. */
1922228753Smm	    && p + p[2] <= end) { /* Sanity-check length. */
1923228753Smm		const unsigned char *data = p + 4;
1924228753Smm		int data_length = p[2] - 4;
1925228753Smm		int version = p[3];
1926228753Smm
1927228753Smm		/*
1928228753Smm		 * Yes, each 'if' here does test p[0] again.
1929228753Smm		 * Otherwise, the fall-through handling to catch
1930228753Smm		 * unsupported extensions doesn't work.
1931228753Smm		 */
1932228753Smm		switch(p[0]) {
1933228753Smm		case 'C':
1934228753Smm			if (p[0] == 'C' && p[1] == 'E') {
1935228753Smm				if (version == 1 && data_length == 24) {
1936228753Smm					/*
1937228753Smm					 * CE extension comprises:
1938228753Smm					 *   8 byte sector containing extension
1939228753Smm					 *   8 byte offset w/in above sector
1940228753Smm					 *   8 byte length of continuation
1941228753Smm					 */
1942228753Smm					int32_t location =
1943228753Smm					    archive_le32dec(data);
1944228753Smm					file->ce_offset =
1945228753Smm					    archive_le32dec(data+8);
1946228753Smm					file->ce_size =
1947228753Smm					    archive_le32dec(data+16);
1948228753Smm					if (register_CE(a, location, file)
1949228753Smm					    != ARCHIVE_OK)
1950228753Smm						return (ARCHIVE_FATAL);
1951228753Smm				}
1952228753Smm				break;
1953228753Smm			}
1954228753Smm			if (p[0] == 'C' && p[1] == 'L') {
1955228753Smm				if (version == 1 && data_length == 8) {
1956228753Smm					file->cl_offset = (uint64_t)
1957228753Smm					    iso9660->logical_block_size *
1958228753Smm					    (uint64_t)archive_le32dec(data);
1959228753Smm					iso9660->seenRockridge = 1;
1960228753Smm				}
1961228753Smm				break;
1962228753Smm			}
1963228753Smm			/* FALLTHROUGH */
1964228753Smm		case 'N':
1965228753Smm			if (p[0] == 'N' && p[1] == 'M') {
1966228753Smm				if (version == 1) {
1967228753Smm					parse_rockridge_NM1(file,
1968228753Smm					    data, data_length);
1969228753Smm					iso9660->seenRockridge = 1;
1970228753Smm				}
1971228753Smm				break;
1972228753Smm			}
1973228753Smm			/* FALLTHROUGH */
1974228753Smm		case 'P':
1975228753Smm			if (p[0] == 'P' && p[1] == 'D') {
1976228753Smm				/*
1977228753Smm				 * PD extension is padding;
1978228753Smm				 * contents are always ignored.
1979228753Smm				 */
1980228753Smm				break;
1981228753Smm			}
1982229592Smm			if (p[0] == 'P' && p[1] == 'L') {
1983229592Smm				/*
1984229592Smm				 * PL extension won't appear;
1985229592Smm				 * contents are always ignored.
1986229592Smm				 */
1987229592Smm				break;
1988229592Smm			}
1989228753Smm			if (p[0] == 'P' && p[1] == 'N') {
1990228753Smm				if (version == 1 && data_length == 16) {
1991228753Smm					file->rdev = toi(data,4);
1992228753Smm					file->rdev <<= 32;
1993228753Smm					file->rdev |= toi(data + 8, 4);
1994228753Smm					iso9660->seenRockridge = 1;
1995228753Smm				}
1996228753Smm				break;
1997228753Smm			}
1998228753Smm			if (p[0] == 'P' && p[1] == 'X') {
1999228753Smm				/*
2000228753Smm				 * PX extension comprises:
2001228753Smm				 *   8 bytes for mode,
2002228753Smm				 *   8 bytes for nlinks,
2003228753Smm				 *   8 bytes for uid,
2004228753Smm				 *   8 bytes for gid,
2005228753Smm				 *   8 bytes for inode.
2006228753Smm				 */
2007228753Smm				if (version == 1) {
2008228753Smm					if (data_length >= 8)
2009228753Smm						file->mode
2010228753Smm						    = toi(data, 4);
2011228753Smm					if (data_length >= 16)
2012228753Smm						file->nlinks
2013228753Smm						    = toi(data + 8, 4);
2014228753Smm					if (data_length >= 24)
2015228753Smm						file->uid
2016228753Smm						    = toi(data + 16, 4);
2017228753Smm					if (data_length >= 32)
2018228753Smm						file->gid
2019228753Smm						    = toi(data + 24, 4);
2020228753Smm					if (data_length >= 40)
2021228753Smm						file->number
2022228753Smm						    = toi(data + 32, 4);
2023228753Smm					iso9660->seenRockridge = 1;
2024228753Smm				}
2025228753Smm				break;
2026228753Smm			}
2027228753Smm			/* FALLTHROUGH */
2028228753Smm		case 'R':
2029228753Smm			if (p[0] == 'R' && p[1] == 'E' && version == 1) {
2030228753Smm				file->re = 1;
2031228753Smm				iso9660->seenRockridge = 1;
2032228753Smm				break;
2033228753Smm			}
2034228753Smm			if (p[0] == 'R' && p[1] == 'R' && version == 1) {
2035228753Smm				/*
2036228753Smm				 * RR extension comprises:
2037228753Smm				 *    one byte flag value
2038228753Smm				 * This extension is obsolete,
2039228753Smm				 * so contents are always ignored.
2040228753Smm				 */
2041228753Smm				break;
2042228753Smm			}
2043228753Smm			/* FALLTHROUGH */
2044228753Smm		case 'S':
2045228753Smm			if (p[0] == 'S' && p[1] == 'L') {
2046228753Smm				if (version == 1) {
2047228753Smm					parse_rockridge_SL1(file,
2048228753Smm					    data, data_length);
2049228753Smm					iso9660->seenRockridge = 1;
2050228753Smm				}
2051228753Smm				break;
2052228753Smm			}
2053228753Smm			if (p[0] == 'S' && p[1] == 'T'
2054228753Smm			    && data_length == 0 && version == 1) {
2055228753Smm				/*
2056228753Smm				 * ST extension marks end of this
2057228753Smm				 * block of SUSP entries.
2058228753Smm				 *
2059228753Smm				 * It allows SUSP to coexist with
2060228753Smm				 * non-SUSP uses of the System
2061228753Smm				 * Use Area by placing non-SUSP data
2062228753Smm				 * after SUSP data.
2063228753Smm				 */
2064228753Smm				iso9660->seenSUSP = 0;
2065228753Smm				iso9660->seenRockridge = 0;
2066228753Smm				return (ARCHIVE_OK);
2067228753Smm			}
2068228753Smm		case 'T':
2069228753Smm			if (p[0] == 'T' && p[1] == 'F') {
2070228753Smm				if (version == 1) {
2071228753Smm					parse_rockridge_TF1(file,
2072228753Smm					    data, data_length);
2073228753Smm					iso9660->seenRockridge = 1;
2074228753Smm				}
2075228753Smm				break;
2076228753Smm			}
2077228753Smm			/* FALLTHROUGH */
2078228753Smm		case 'Z':
2079228753Smm			if (p[0] == 'Z' && p[1] == 'F') {
2080228753Smm				if (version == 1)
2081228753Smm					parse_rockridge_ZF1(file,
2082228753Smm					    data, data_length);
2083228753Smm				break;
2084228753Smm			}
2085228753Smm			/* FALLTHROUGH */
2086228753Smm		default:
2087228753Smm			/* The FALLTHROUGHs above leave us here for
2088228753Smm			 * any unsupported extension. */
2089228753Smm			break;
2090228753Smm		}
2091228753Smm
2092228753Smm
2093228753Smm
2094228753Smm		p += p[2];
2095228753Smm	}
2096228753Smm	return (ARCHIVE_OK);
2097228753Smm}
2098228753Smm
2099228753Smmstatic int
2100228753Smmregister_CE(struct archive_read *a, int32_t location,
2101228753Smm    struct file_info *file)
2102228753Smm{
2103228753Smm	struct iso9660 *iso9660;
2104228753Smm	struct read_ce_queue *heap;
2105228753Smm	struct read_ce_req *p;
2106228753Smm	uint64_t offset, parent_offset;
2107228753Smm	int hole, parent;
2108228753Smm
2109228753Smm	iso9660 = (struct iso9660 *)(a->format->data);
2110228753Smm	offset = ((uint64_t)location) * (uint64_t)iso9660->logical_block_size;
2111228753Smm	if (((file->mode & AE_IFMT) == AE_IFREG &&
2112228753Smm	    offset >= file->offset) ||
2113228753Smm	    offset < iso9660->current_position) {
2114228753Smm		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
2115228753Smm		    "Invalid location in SUSP \"CE\" extension");
2116228753Smm		return (ARCHIVE_FATAL);
2117228753Smm	}
2118228753Smm
2119228753Smm	/* Expand our CE list as necessary. */
2120228753Smm	heap = &(iso9660->read_ce_req);
2121228753Smm	if (heap->cnt >= heap->allocated) {
2122228753Smm		int new_size;
2123228753Smm
2124228753Smm		if (heap->allocated < 16)
2125228753Smm			new_size = 16;
2126228753Smm		else
2127228753Smm			new_size = heap->allocated * 2;
2128228753Smm		/* Overflow might keep us from growing the list. */
2129228753Smm		if (new_size <= heap->allocated)
2130228753Smm			__archive_errx(1, "Out of memory");
2131228753Smm		p = malloc(new_size * sizeof(p[0]));
2132228753Smm		if (p == NULL)
2133228753Smm			__archive_errx(1, "Out of memory");
2134228753Smm		if (heap->reqs != NULL) {
2135228753Smm			memcpy(p, heap->reqs, heap->cnt * sizeof(*p));
2136228753Smm			free(heap->reqs);
2137228753Smm		}
2138228753Smm		heap->reqs = p;
2139228753Smm		heap->allocated = new_size;
2140228753Smm	}
2141228753Smm
2142228753Smm	/*
2143228753Smm	 * Start with hole at end, walk it up tree to find insertion point.
2144228753Smm	 */
2145228753Smm	hole = heap->cnt++;
2146228753Smm	while (hole > 0) {
2147228753Smm		parent = (hole - 1)/2;
2148228753Smm		parent_offset = heap->reqs[parent].offset;
2149228753Smm		if (offset >= parent_offset) {
2150228753Smm			heap->reqs[hole].offset = offset;
2151228753Smm			heap->reqs[hole].file = file;
2152228753Smm			return (ARCHIVE_OK);
2153228753Smm		}
2154228753Smm		// Move parent into hole <==> move hole up tree.
2155228753Smm		heap->reqs[hole] = heap->reqs[parent];
2156228753Smm		hole = parent;
2157228753Smm	}
2158228753Smm	heap->reqs[0].offset = offset;
2159228753Smm	heap->reqs[0].file = file;
2160228753Smm	return (ARCHIVE_OK);
2161228753Smm}
2162228753Smm
2163228753Smmstatic void
2164228753Smmnext_CE(struct read_ce_queue *heap)
2165228753Smm{
2166228753Smm	uint64_t a_offset, b_offset, c_offset;
2167228753Smm	int a, b, c;
2168228753Smm	struct read_ce_req tmp;
2169228753Smm
2170228753Smm	if (heap->cnt < 1)
2171228753Smm		return;
2172228753Smm
2173228753Smm	/*
2174228753Smm	 * Move the last item in the heap to the root of the tree
2175228753Smm	 */
2176228753Smm	heap->reqs[0] = heap->reqs[--(heap->cnt)];
2177228753Smm
2178228753Smm	/*
2179228753Smm	 * Rebalance the heap.
2180228753Smm	 */
2181228753Smm	a = 0; // Starting element and its offset
2182228753Smm	a_offset = heap->reqs[a].offset;
2183228753Smm	for (;;) {
2184228753Smm		b = a + a + 1; // First child
2185228753Smm		if (b >= heap->cnt)
2186228753Smm			return;
2187228753Smm		b_offset = heap->reqs[b].offset;
2188228753Smm		c = b + 1; // Use second child if it is smaller.
2189228753Smm		if (c < heap->cnt) {
2190228753Smm			c_offset = heap->reqs[c].offset;
2191228753Smm			if (c_offset < b_offset) {
2192228753Smm				b = c;
2193228753Smm				b_offset = c_offset;
2194228753Smm			}
2195228753Smm		}
2196228753Smm		if (a_offset <= b_offset)
2197228753Smm			return;
2198228753Smm		tmp = heap->reqs[a];
2199228753Smm		heap->reqs[a] = heap->reqs[b];
2200228753Smm		heap->reqs[b] = tmp;
2201228753Smm		a = b;
2202228753Smm	}
2203228753Smm}
2204228753Smm
2205228753Smm
2206228753Smmstatic int
2207228753Smmread_CE(struct archive_read *a, struct iso9660 *iso9660)
2208228753Smm{
2209228753Smm	struct read_ce_queue *heap;
2210228753Smm	const unsigned char *b, *p, *end;
2211228753Smm	struct file_info *file;
2212228753Smm	size_t step;
2213228753Smm	int r;
2214228753Smm
2215228753Smm	/* Read data which RRIP "CE" extension points. */
2216228753Smm	heap = &(iso9660->read_ce_req);
2217228753Smm	step = iso9660->logical_block_size;
2218228753Smm	while (heap->cnt &&
2219228753Smm	    heap->reqs[0].offset == iso9660->current_position) {
2220228753Smm		b = __archive_read_ahead(a, step, NULL);
2221228753Smm		if (b == NULL) {
2222228753Smm			archive_set_error(&a->archive,
2223228753Smm			    ARCHIVE_ERRNO_MISC,
2224228753Smm			    "Failed to read full block when scanning "
2225228753Smm			    "ISO9660 directory list");
2226228753Smm			return (ARCHIVE_FATAL);
2227228753Smm		}
2228228753Smm		do {
2229228753Smm			file = heap->reqs[0].file;
2230228753Smm			p = b + file->ce_offset;
2231228753Smm			end = p + file->ce_size;
2232228753Smm			next_CE(heap);
2233228753Smm			r = parse_rockridge(a, file, p, end);
2234228753Smm			if (r != ARCHIVE_OK)
2235228753Smm				return (ARCHIVE_FATAL);
2236228753Smm		} while (heap->cnt &&
2237228753Smm		    heap->reqs[0].offset == iso9660->current_position);
2238228753Smm		/* NOTE: Do not move this consume's code to fron of
2239228753Smm		 * do-while loop. Registration of nested CE extension
2240228753Smm		 * might cause error because of current position. */
2241228753Smm		__archive_read_consume(a, step);
2242228753Smm		iso9660->current_position += step;
2243228753Smm	}
2244228753Smm	return (ARCHIVE_OK);
2245228753Smm}
2246228753Smm
2247228753Smmstatic void
2248228753Smmparse_rockridge_NM1(struct file_info *file,
2249228753Smm		    const unsigned char *data, int data_length)
2250228753Smm{
2251228753Smm	if (!file->name_continues)
2252228753Smm		archive_string_empty(&file->name);
2253228753Smm	file->name_continues = 0;
2254228753Smm	if (data_length < 1)
2255228753Smm		return;
2256228753Smm	/*
2257228753Smm	 * NM version 1 extension comprises:
2258228753Smm	 *   1 byte flag, value is one of:
2259228753Smm	 *     = 0: remainder is name
2260228753Smm	 *     = 1: remainder is name, next NM entry continues name
2261228753Smm	 *     = 2: "."
2262228753Smm	 *     = 4: ".."
2263228753Smm	 *     = 32: Implementation specific
2264228753Smm	 *     All other values are reserved.
2265228753Smm	 */
2266228753Smm	switch(data[0]) {
2267228753Smm	case 0:
2268228753Smm		if (data_length < 2)
2269228753Smm			return;
2270228753Smm		archive_strncat(&file->name, (const char *)data + 1, data_length - 1);
2271228753Smm		break;
2272228753Smm	case 1:
2273228753Smm		if (data_length < 2)
2274228753Smm			return;
2275228753Smm		archive_strncat(&file->name, (const char *)data + 1, data_length - 1);
2276228753Smm		file->name_continues = 1;
2277228753Smm		break;
2278228753Smm	case 2:
2279228753Smm		archive_strcat(&file->name, ".");
2280228753Smm		break;
2281228753Smm	case 4:
2282228753Smm		archive_strcat(&file->name, "..");
2283228753Smm		break;
2284228753Smm	default:
2285228753Smm		return;
2286228753Smm	}
2287228753Smm
2288228753Smm}
2289228753Smm
2290228753Smmstatic void
2291228753Smmparse_rockridge_TF1(struct file_info *file, const unsigned char *data,
2292228753Smm    int data_length)
2293228753Smm{
2294228753Smm	char flag;
2295228753Smm	/*
2296228753Smm	 * TF extension comprises:
2297228753Smm	 *   one byte flag
2298228753Smm	 *   create time (optional)
2299228753Smm	 *   modify time (optional)
2300228753Smm	 *   access time (optional)
2301228753Smm	 *   attribute time (optional)
2302228753Smm	 *  Time format and presence of fields
2303228753Smm	 *  is controlled by flag bits.
2304228753Smm	 */
2305228753Smm	if (data_length < 1)
2306228753Smm		return;
2307228753Smm	flag = data[0];
2308228753Smm	++data;
2309228753Smm	--data_length;
2310228753Smm	if (flag & 0x80) {
2311228753Smm		/* Use 17-byte time format. */
2312228753Smm		if ((flag & 1) && data_length >= 17) {
2313228753Smm			/* Create time. */
2314228753Smm			file->birthtime_is_set = 1;
2315228753Smm			file->birthtime = isodate17(data);
2316228753Smm			data += 17;
2317228753Smm			data_length -= 17;
2318228753Smm		}
2319228753Smm		if ((flag & 2) && data_length >= 17) {
2320228753Smm			/* Modify time. */
2321228753Smm			file->mtime = isodate17(data);
2322228753Smm			data += 17;
2323228753Smm			data_length -= 17;
2324228753Smm		}
2325228753Smm		if ((flag & 4) && data_length >= 17) {
2326228753Smm			/* Access time. */
2327228753Smm			file->atime = isodate17(data);
2328228753Smm			data += 17;
2329228753Smm			data_length -= 17;
2330228753Smm		}
2331228753Smm		if ((flag & 8) && data_length >= 17) {
2332228753Smm			/* Attribute change time. */
2333228753Smm			file->ctime = isodate17(data);
2334228753Smm		}
2335228753Smm	} else {
2336228753Smm		/* Use 7-byte time format. */
2337228753Smm		if ((flag & 1) && data_length >= 7) {
2338228753Smm			/* Create time. */
2339228753Smm			file->birthtime_is_set = 1;
2340228753Smm			file->birthtime = isodate7(data);
2341228753Smm			data += 7;
2342228753Smm			data_length -= 7;
2343228753Smm		}
2344228753Smm		if ((flag & 2) && data_length >= 7) {
2345228753Smm			/* Modify time. */
2346228753Smm			file->mtime = isodate7(data);
2347228753Smm			data += 7;
2348228753Smm			data_length -= 7;
2349228753Smm		}
2350228753Smm		if ((flag & 4) && data_length >= 7) {
2351228753Smm			/* Access time. */
2352228753Smm			file->atime = isodate7(data);
2353228753Smm			data += 7;
2354228753Smm			data_length -= 7;
2355228753Smm		}
2356228753Smm		if ((flag & 8) && data_length >= 7) {
2357228753Smm			/* Attribute change time. */
2358228753Smm			file->ctime = isodate7(data);
2359228753Smm		}
2360228753Smm	}
2361228753Smm}
2362228753Smm
2363228753Smmstatic void
2364228753Smmparse_rockridge_SL1(struct file_info *file, const unsigned char *data,
2365228753Smm    int data_length)
2366228753Smm{
2367228753Smm	const char *separator = "";
2368228753Smm
2369228753Smm	if (!file->symlink_continues || file->symlink.length < 1)
2370228753Smm		archive_string_empty(&file->symlink);
2371228753Smm	else if (!file->symlink_continues &&
2372228753Smm	    file->symlink.s[file->symlink.length - 1] != '/')
2373228753Smm		separator = "/";
2374228753Smm	file->symlink_continues = 0;
2375228753Smm
2376228753Smm	/*
2377228753Smm	 * Defined flag values:
2378228753Smm	 *  0: This is the last SL record for this symbolic link
2379228753Smm	 *  1: this symbolic link field continues in next SL entry
2380228753Smm	 *  All other values are reserved.
2381228753Smm	 */
2382228753Smm	if (data_length < 1)
2383228753Smm		return;
2384228753Smm	switch(*data) {
2385228753Smm	case 0:
2386228753Smm		break;
2387228753Smm	case 1:
2388228753Smm		file->symlink_continues = 1;
2389228753Smm		break;
2390228753Smm	default:
2391228753Smm		return;
2392228753Smm	}
2393228753Smm	++data;  /* Skip flag byte. */
2394228753Smm	--data_length;
2395228753Smm
2396228753Smm	/*
2397228753Smm	 * SL extension body stores "components".
2398228753Smm	 * Basically, this is a complicated way of storing
2399228753Smm	 * a POSIX path.  It also interferes with using
2400228753Smm	 * symlinks for storing non-path data. <sigh>
2401228753Smm	 *
2402228753Smm	 * Each component is 2 bytes (flag and length)
2403228753Smm	 * possibly followed by name data.
2404228753Smm	 */
2405228753Smm	while (data_length >= 2) {
2406228753Smm		unsigned char flag = *data++;
2407228753Smm		unsigned char nlen = *data++;
2408228753Smm		data_length -= 2;
2409228753Smm
2410228753Smm		archive_strcat(&file->symlink, separator);
2411228753Smm		separator = "/";
2412228753Smm
2413228753Smm		switch(flag) {
2414228753Smm		case 0: /* Usual case, this is text. */
2415228753Smm			if (data_length < nlen)
2416228753Smm				return;
2417228753Smm			archive_strncat(&file->symlink,
2418228753Smm			    (const char *)data, nlen);
2419228753Smm			break;
2420228753Smm		case 0x01: /* Text continues in next component. */
2421228753Smm			if (data_length < nlen)
2422228753Smm				return;
2423228753Smm			archive_strncat(&file->symlink,
2424228753Smm			    (const char *)data, nlen);
2425228753Smm			separator = "";
2426228753Smm			break;
2427228753Smm		case 0x02: /* Current dir. */
2428228753Smm			archive_strcat(&file->symlink, ".");
2429228753Smm			break;
2430228753Smm		case 0x04: /* Parent dir. */
2431228753Smm			archive_strcat(&file->symlink, "..");
2432228753Smm			break;
2433228753Smm		case 0x08: /* Root of filesystem. */
2434228753Smm			archive_strcat(&file->symlink, "/");
2435228753Smm			separator = "";
2436228753Smm			break;
2437228753Smm		case 0x10: /* Undefined (historically "volume root" */
2438228753Smm			archive_string_empty(&file->symlink);
2439228753Smm			archive_strcat(&file->symlink, "ROOT");
2440228753Smm			break;
2441228753Smm		case 0x20: /* Undefined (historically "hostname") */
2442228753Smm			archive_strcat(&file->symlink, "hostname");
2443228753Smm			break;
2444228753Smm		default:
2445228753Smm			/* TODO: issue a warning ? */
2446228753Smm			return;
2447228753Smm		}
2448228753Smm		data += nlen;
2449228753Smm		data_length -= nlen;
2450228753Smm	}
2451228753Smm}
2452228753Smm
2453228753Smmstatic void
2454228753Smmparse_rockridge_ZF1(struct file_info *file, const unsigned char *data,
2455228753Smm    int data_length)
2456228753Smm{
2457228753Smm
2458228753Smm	if (data[0] == 0x70 && data[1] == 0x7a && data_length == 12) {
2459228753Smm		/* paged zlib */
2460228753Smm		file->pz = 1;
2461228753Smm		file->pz_log2_bs = data[3];
2462228753Smm		file->pz_uncompressed_size = archive_le32dec(&data[4]);
2463228753Smm	}
2464228753Smm}
2465228753Smm
2466228753Smmstatic void
2467228753Smmregister_file(struct iso9660 *iso9660, struct file_info *file)
2468228753Smm{
2469228753Smm
2470228753Smm	file->use_next = iso9660->use_files;
2471228753Smm	iso9660->use_files = file;
2472228753Smm}
2473228753Smm
2474228753Smmstatic void
2475228753Smmrelease_files(struct iso9660 *iso9660)
2476228753Smm{
2477228753Smm	struct content *con, *connext;
2478228753Smm	struct file_info *file;
2479228753Smm
2480228753Smm	file = iso9660->use_files;
2481228753Smm	while (file != NULL) {
2482228753Smm		struct file_info *next = file->use_next;
2483228753Smm
2484228753Smm		archive_string_free(&file->name);
2485228753Smm		archive_string_free(&file->symlink);
2486228753Smm		con = file->contents.first;
2487228753Smm		while (con != NULL) {
2488228753Smm			connext = con->next;
2489228753Smm			free(con);
2490228753Smm			con = connext;
2491228753Smm		}
2492228753Smm		free(file);
2493228753Smm		file = next;
2494228753Smm	}
2495228753Smm}
2496228753Smm
2497228753Smmstatic int
2498228753Smmnext_entry_seek(struct archive_read *a, struct iso9660 *iso9660,
2499228753Smm    struct file_info **pfile)
2500228753Smm{
2501228753Smm	struct file_info *file;
2502228753Smm	int r;
2503228753Smm
2504228753Smm	r = next_cache_entry(a, iso9660, pfile);
2505228753Smm	if (r != ARCHIVE_OK)
2506228753Smm		return (r);
2507228753Smm	file = *pfile;
2508228753Smm
2509228753Smm	/* Don't waste time seeking for zero-length bodies. */
2510228753Smm	if (file->size == 0)
2511228753Smm		file->offset = iso9660->current_position;
2512228753Smm
2513228753Smm	/* Seek forward to the start of the entry. */
2514228753Smm	if (iso9660->current_position < file->offset) {
2515228753Smm		int64_t step;
2516228753Smm
2517228753Smm		step = file->offset - iso9660->current_position;
2518228753Smm		step = __archive_read_skip(a, step);
2519228753Smm		if (step < 0)
2520228753Smm			return ((int)step);
2521228753Smm		iso9660->current_position = file->offset;
2522228753Smm	}
2523228753Smm
2524228753Smm	/* We found body of file; handle it now. */
2525228753Smm	return (ARCHIVE_OK);
2526228753Smm}
2527228753Smm
2528228753Smmstatic int
2529228753Smmnext_cache_entry(struct archive_read *a, struct iso9660 *iso9660,
2530228753Smm    struct file_info **pfile)
2531228753Smm{
2532228753Smm	struct file_info *file;
2533228753Smm	struct {
2534228753Smm		struct file_info	*first;
2535228753Smm		struct file_info	**last;
2536228753Smm	}	empty_files;
2537228753Smm	int64_t number;
2538228753Smm	int count;
2539228753Smm
2540228753Smm	file = cache_get_entry(iso9660);
2541228753Smm	if (file != NULL) {
2542228753Smm		*pfile = file;
2543228753Smm		return (ARCHIVE_OK);
2544228753Smm	}
2545228753Smm
2546228753Smm	for (;;) {
2547228753Smm		struct file_info *re, *d;
2548228753Smm
2549228753Smm		*pfile = file = next_entry(iso9660);
2550228753Smm		if (file == NULL) {
2551228753Smm			/*
2552228753Smm			 * If directory entries all which are descendant of
2553228753Smm			 * rr_moved are stil remaning, expose their.
2554228753Smm			 */
2555228753Smm			if (iso9660->re_files.first != NULL &&
2556228753Smm			    iso9660->rr_moved != NULL &&
2557228753Smm			    iso9660->rr_moved->rr_moved_has_re_only)
2558228753Smm				/* Expose "rr_moved" entry. */
2559228753Smm				cache_add_entry(iso9660, iso9660->rr_moved);
2560228753Smm			while ((re = re_get_entry(iso9660)) != NULL) {
2561228753Smm				/* Expose its descendant dirs. */
2562228753Smm				while ((d = rede_get_entry(re)) != NULL)
2563228753Smm					cache_add_entry(iso9660, d);
2564228753Smm			}
2565228753Smm			if (iso9660->cache_files.first != NULL)
2566228753Smm				return (next_cache_entry(a, iso9660, pfile));
2567228753Smm			return (ARCHIVE_EOF);
2568228753Smm		}
2569228753Smm
2570228753Smm		if (file->cl_offset) {
2571228753Smm			struct file_info *first_re = NULL;
2572228753Smm			int nexted_re = 0;
2573228753Smm
2574228753Smm			/*
2575228753Smm			 * Find "RE" dir for the current file, which
2576228753Smm			 * has "CL" flag.
2577228753Smm			 */
2578228753Smm			while ((re = re_get_entry(iso9660))
2579228753Smm			    != first_re) {
2580228753Smm				if (first_re == NULL)
2581228753Smm					first_re = re;
2582228753Smm				if (re->offset == file->cl_offset) {
2583228753Smm					re->parent->subdirs--;
2584228753Smm					re->parent = file->parent;
2585228753Smm					re->re = 0;
2586228753Smm					if (re->parent->re_descendant) {
2587228753Smm						nexted_re = 1;
2588228753Smm						re->re_descendant = 1;
2589228753Smm						if (rede_add_entry(re) < 0)
2590228753Smm							goto fatal_rr;
2591228753Smm						/* Move a list of descendants
2592228753Smm						 * to a new ancestor. */
2593228753Smm						while ((d = rede_get_entry(
2594228753Smm						    re)) != NULL)
2595228753Smm							if (rede_add_entry(d)
2596228753Smm							    < 0)
2597228753Smm								goto fatal_rr;
2598228753Smm						break;
2599228753Smm					}
2600228753Smm					/* Replace the current file
2601228753Smm					 * with "RE" dir */
2602228753Smm					*pfile = file = re;
2603228753Smm					/* Expose its descendant */
2604228753Smm					while ((d = rede_get_entry(
2605228753Smm					    file)) != NULL)
2606228753Smm						cache_add_entry(
2607228753Smm						    iso9660, d);
2608228753Smm					break;
2609228753Smm				} else
2610228753Smm					re_add_entry(iso9660, re);
2611228753Smm			}
2612228753Smm			if (nexted_re) {
2613228753Smm				/*
2614228753Smm				 * Do not expose this at this time
2615228753Smm				 * because we have not gotten its full-path
2616228753Smm				 * name yet.
2617228753Smm				 */
2618228753Smm				continue;
2619228753Smm			}
2620228753Smm		} else if ((file->mode & AE_IFMT) == AE_IFDIR) {
2621228753Smm			int r;
2622228753Smm
2623228753Smm			/* Read file entries in this dir. */
2624228753Smm			r = read_children(a, file);
2625228753Smm			if (r != ARCHIVE_OK)
2626228753Smm				return (r);
2627228753Smm
2628228753Smm			/*
2629228753Smm			 * Handle a special dir of Rockridge extensions,
2630228753Smm			 * "rr_moved".
2631228753Smm			 */
2632228753Smm			if (file->rr_moved) {
2633228753Smm				/*
2634228753Smm				 * If this has only the subdirectories which
2635228753Smm				 * have "RE" flags, do not expose at this time.
2636228753Smm				 */
2637228753Smm				if (file->rr_moved_has_re_only)
2638228753Smm					continue;
2639228753Smm				/* Otherwise expose "rr_moved" entry. */
2640228753Smm			} else if (file->re) {
2641228753Smm				/*
2642228753Smm				 * Do not expose this at this time
2643228753Smm				 * because we have not gotten its full-path
2644228753Smm				 * name yet.
2645228753Smm				 */
2646228753Smm				re_add_entry(iso9660, file);
2647228753Smm				continue;
2648228753Smm			} else if (file->re_descendant) {
2649228753Smm				/*
2650228753Smm				 * If the top level "RE" entry of this entry
2651228753Smm				 * is not exposed, we, accordingly, should not
2652228753Smm				 * expose this entry at this time because
2653228753Smm				 * we cannot make its proper full-path name.
2654228753Smm				 */
2655228753Smm				if (rede_add_entry(file) == 0)
2656228753Smm					continue;
2657228753Smm				/* Otherwise we can expose this entry because
2658228753Smm				 * it seems its top level "RE" has already been
2659228753Smm				 * exposed. */
2660228753Smm			}
2661228753Smm		}
2662228753Smm		break;
2663228753Smm	}
2664228753Smm
2665228753Smm	if ((file->mode & AE_IFMT) != AE_IFREG || file->number == -1)
2666228753Smm		return (ARCHIVE_OK);
2667228753Smm
2668228753Smm	count = 0;
2669228753Smm	number = file->number;
2670228753Smm	iso9660->cache_files.first = NULL;
2671228753Smm	iso9660->cache_files.last = &(iso9660->cache_files.first);
2672228753Smm	empty_files.first = NULL;
2673228753Smm	empty_files.last = &empty_files.first;
2674228753Smm	/* Collect files which has the same file serial number.
2675228753Smm	 * Peek pending_files so that file which number is different
2676228753Smm	 * is not put bak. */
2677228753Smm	while (iso9660->pending_files.used > 0 &&
2678228753Smm	    (iso9660->pending_files.files[0]->number == -1 ||
2679228753Smm	     iso9660->pending_files.files[0]->number == number)) {
2680228753Smm		if (file->number == -1) {
2681228753Smm			/* This file has the same offset
2682228753Smm			 * but it's wrong offset which empty files
2683228753Smm			 * and symlink files have.
2684228753Smm			 * NOTE: This wrong offse was recorded by
2685228753Smm			 * old mkisofs utility. If ISO images is
2686228753Smm			 * created by latest mkisofs, this does not
2687228753Smm			 * happen.
2688228753Smm			 */
2689228753Smm			file->next = NULL;
2690228753Smm			*empty_files.last = file;
2691228753Smm			empty_files.last = &(file->next);
2692228753Smm		} else {
2693228753Smm			count++;
2694228753Smm			cache_add_entry(iso9660, file);
2695228753Smm		}
2696228753Smm		file = next_entry(iso9660);
2697228753Smm	}
2698228753Smm
2699228753Smm	if (count == 0) {
2700228753Smm		*pfile = file;
2701228753Smm		return ((file == NULL)?ARCHIVE_EOF:ARCHIVE_OK);
2702228753Smm	}
2703228753Smm	if (file->number == -1) {
2704228753Smm		file->next = NULL;
2705228753Smm		*empty_files.last = file;
2706228753Smm		empty_files.last = &(file->next);
2707228753Smm	} else {
2708228753Smm		count++;
2709228753Smm		cache_add_entry(iso9660, file);
2710228753Smm	}
2711228753Smm
2712228753Smm	if (count > 1) {
2713228753Smm		/* The count is the same as number of hardlink,
2714228753Smm		 * so much so that each nlinks of files in cache_file
2715228753Smm		 * is overwritten by value of the count.
2716228753Smm		 */
2717228753Smm		for (file = iso9660->cache_files.first;
2718228753Smm		    file != NULL; file = file->next)
2719228753Smm			file->nlinks = count;
2720228753Smm	}
2721228753Smm	/* If there are empty files, that files are added
2722228753Smm	 * to the tail of the cache_files. */
2723228753Smm	if (empty_files.first != NULL) {
2724228753Smm		*iso9660->cache_files.last = empty_files.first;
2725228753Smm		iso9660->cache_files.last = empty_files.last;
2726228753Smm	}
2727228753Smm	*pfile = cache_get_entry(iso9660);
2728228753Smm	return ((*pfile == NULL)?ARCHIVE_EOF:ARCHIVE_OK);
2729228753Smm
2730228753Smmfatal_rr:
2731228753Smm	archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
2732228753Smm	    "Failed to connect 'CL' pointer to 'RE' rr_moved pointer of"
2733228753Smm	    "Rockridge extensions");
2734228753Smm	return (ARCHIVE_FATAL);
2735228753Smm}
2736228753Smm
2737228753Smmstatic inline void
2738228753Smmre_add_entry(struct iso9660 *iso9660, struct file_info *file)
2739228753Smm{
2740228753Smm	file->re_next = NULL;
2741228753Smm	*iso9660->re_files.last = file;
2742228753Smm	iso9660->re_files.last = &(file->re_next);
2743228753Smm}
2744228753Smm
2745228753Smmstatic inline struct file_info *
2746228753Smmre_get_entry(struct iso9660 *iso9660)
2747228753Smm{
2748228753Smm	struct file_info *file;
2749228753Smm
2750228753Smm	if ((file = iso9660->re_files.first) != NULL) {
2751228753Smm		iso9660->re_files.first = file->re_next;
2752228753Smm		if (iso9660->re_files.first == NULL)
2753228753Smm			iso9660->re_files.last =
2754228753Smm			    &(iso9660->re_files.first);
2755228753Smm	}
2756228753Smm	return (file);
2757228753Smm}
2758228753Smm
2759228753Smmstatic inline int
2760228753Smmrede_add_entry(struct file_info *file)
2761228753Smm{
2762228753Smm	struct file_info *re;
2763228753Smm
2764229592Smm	/*
2765229592Smm	 * Find "RE" entry.
2766229592Smm	 */
2767228753Smm	re = file->parent;
2768229592Smm	while (re != NULL && !re->re)
2769228753Smm		re = re->parent;
2770228753Smm	if (re == NULL)
2771228753Smm		return (-1);
2772228753Smm
2773228753Smm	file->re_next = NULL;
2774228753Smm	*re->rede_files.last = file;
2775228753Smm	re->rede_files.last = &(file->re_next);
2776228753Smm	return (0);
2777228753Smm}
2778228753Smm
2779228753Smmstatic inline struct file_info *
2780228753Smmrede_get_entry(struct file_info *re)
2781228753Smm{
2782228753Smm	struct file_info *file;
2783228753Smm
2784228753Smm	if ((file = re->rede_files.first) != NULL) {
2785228753Smm		re->rede_files.first = file->re_next;
2786228753Smm		if (re->rede_files.first == NULL)
2787228753Smm			re->rede_files.last =
2788228753Smm			    &(re->rede_files.first);
2789228753Smm	}
2790228753Smm	return (file);
2791228753Smm}
2792228753Smm
2793228753Smmstatic inline void
2794228753Smmcache_add_entry(struct iso9660 *iso9660, struct file_info *file)
2795228753Smm{
2796228753Smm	file->next = NULL;
2797228753Smm	*iso9660->cache_files.last = file;
2798228753Smm	iso9660->cache_files.last = &(file->next);
2799228753Smm}
2800228753Smm
2801228753Smmstatic inline struct file_info *
2802228753Smmcache_get_entry(struct iso9660 *iso9660)
2803228753Smm{
2804228753Smm	struct file_info *file;
2805228753Smm
2806228753Smm	if ((file = iso9660->cache_files.first) != NULL) {
2807228753Smm		iso9660->cache_files.first = file->next;
2808228753Smm		if (iso9660->cache_files.first == NULL)
2809228753Smm			iso9660->cache_files.last = &(iso9660->cache_files.first);
2810228753Smm	}
2811228753Smm	return (file);
2812228753Smm}
2813228753Smm
2814228753Smmstatic void
2815228753Smmheap_add_entry(struct heap_queue *heap, struct file_info *file, uint64_t key)
2816228753Smm{
2817228753Smm	uint64_t file_key, parent_key;
2818228753Smm	int hole, parent;
2819228753Smm
2820228753Smm	/* Expand our pending files list as necessary. */
2821228753Smm	if (heap->used >= heap->allocated) {
2822228753Smm		struct file_info **new_pending_files;
2823228753Smm		int new_size = heap->allocated * 2;
2824228753Smm
2825228753Smm		if (heap->allocated < 1024)
2826228753Smm			new_size = 1024;
2827228753Smm		/* Overflow might keep us from growing the list. */
2828228753Smm		if (new_size <= heap->allocated)
2829228753Smm			__archive_errx(1, "Out of memory");
2830228753Smm		new_pending_files = (struct file_info **)
2831228753Smm		    malloc(new_size * sizeof(new_pending_files[0]));
2832228753Smm		if (new_pending_files == NULL)
2833228753Smm			__archive_errx(1, "Out of memory");
2834228753Smm		memcpy(new_pending_files, heap->files,
2835228753Smm		    heap->allocated * sizeof(new_pending_files[0]));
2836228753Smm		if (heap->files != NULL)
2837228753Smm			free(heap->files);
2838228753Smm		heap->files = new_pending_files;
2839228753Smm		heap->allocated = new_size;
2840228753Smm	}
2841228753Smm
2842228753Smm	file_key = file->key = key;
2843228753Smm
2844228753Smm	/*
2845228753Smm	 * Start with hole at end, walk it up tree to find insertion point.
2846228753Smm	 */
2847228753Smm	hole = heap->used++;
2848228753Smm	while (hole > 0) {
2849228753Smm		parent = (hole - 1)/2;
2850228753Smm		parent_key = heap->files[parent]->key;
2851228753Smm		if (file_key >= parent_key) {
2852228753Smm			heap->files[hole] = file;
2853228753Smm			return;
2854228753Smm		}
2855228753Smm		// Move parent into hole <==> move hole up tree.
2856228753Smm		heap->files[hole] = heap->files[parent];
2857228753Smm		hole = parent;
2858228753Smm	}
2859228753Smm	heap->files[0] = file;
2860228753Smm}
2861228753Smm
2862228753Smmstatic struct file_info *
2863228753Smmheap_get_entry(struct heap_queue *heap)
2864228753Smm{
2865228753Smm	uint64_t a_key, b_key, c_key;
2866228753Smm	int a, b, c;
2867228753Smm	struct file_info *r, *tmp;
2868228753Smm
2869228753Smm	if (heap->used < 1)
2870228753Smm		return (NULL);
2871228753Smm
2872228753Smm	/*
2873228753Smm	 * The first file in the list is the earliest; we'll return this.
2874228753Smm	 */
2875228753Smm	r = heap->files[0];
2876228753Smm
2877228753Smm	/*
2878228753Smm	 * Move the last item in the heap to the root of the tree
2879228753Smm	 */
2880228753Smm	heap->files[0] = heap->files[--(heap->used)];
2881228753Smm
2882228753Smm	/*
2883228753Smm	 * Rebalance the heap.
2884228753Smm	 */
2885228753Smm	a = 0; // Starting element and its heap key
2886228753Smm	a_key = heap->files[a]->key;
2887228753Smm	for (;;) {
2888228753Smm		b = a + a + 1; // First child
2889228753Smm		if (b >= heap->used)
2890228753Smm			return (r);
2891228753Smm		b_key = heap->files[b]->key;
2892228753Smm		c = b + 1; // Use second child if it is smaller.
2893228753Smm		if (c < heap->used) {
2894228753Smm			c_key = heap->files[c]->key;
2895228753Smm			if (c_key < b_key) {
2896228753Smm				b = c;
2897228753Smm				b_key = c_key;
2898228753Smm			}
2899228753Smm		}
2900228753Smm		if (a_key <= b_key)
2901228753Smm			return (r);
2902228753Smm		tmp = heap->files[a];
2903228753Smm		heap->files[a] = heap->files[b];
2904228753Smm		heap->files[b] = tmp;
2905228753Smm		a = b;
2906228753Smm	}
2907228753Smm}
2908228753Smm
2909228753Smmstatic unsigned int
2910228753Smmtoi(const void *p, int n)
2911228753Smm{
2912228753Smm	const unsigned char *v = (const unsigned char *)p;
2913228753Smm	if (n > 1)
2914228753Smm		return v[0] + 256 * toi(v + 1, n - 1);
2915228753Smm	if (n == 1)
2916228753Smm		return v[0];
2917228753Smm	return (0);
2918228753Smm}
2919228753Smm
2920228753Smmstatic time_t
2921228753Smmisodate7(const unsigned char *v)
2922228753Smm{
2923228753Smm	struct tm tm;
2924228753Smm	int offset;
2925228753Smm	memset(&tm, 0, sizeof(tm));
2926228753Smm	tm.tm_year = v[0];
2927228753Smm	tm.tm_mon = v[1] - 1;
2928228753Smm	tm.tm_mday = v[2];
2929228753Smm	tm.tm_hour = v[3];
2930228753Smm	tm.tm_min = v[4];
2931228753Smm	tm.tm_sec = v[5];
2932228753Smm	/* v[6] is the signed timezone offset, in 1/4-hour increments. */
2933228753Smm	offset = ((const signed char *)v)[6];
2934228753Smm	if (offset > -48 && offset < 52) {
2935228753Smm		tm.tm_hour -= offset / 4;
2936228753Smm		tm.tm_min -= (offset % 4) * 15;
2937228753Smm	}
2938228753Smm	return (time_from_tm(&tm));
2939228753Smm}
2940228753Smm
2941228753Smmstatic time_t
2942228753Smmisodate17(const unsigned char *v)
2943228753Smm{
2944228753Smm	struct tm tm;
2945228753Smm	int offset;
2946228753Smm	memset(&tm, 0, sizeof(tm));
2947228753Smm	tm.tm_year = (v[0] - '0') * 1000 + (v[1] - '0') * 100
2948228753Smm	    + (v[2] - '0') * 10 + (v[3] - '0')
2949228753Smm	    - 1900;
2950228753Smm	tm.tm_mon = (v[4] - '0') * 10 + (v[5] - '0');
2951228753Smm	tm.tm_mday = (v[6] - '0') * 10 + (v[7] - '0');
2952228753Smm	tm.tm_hour = (v[8] - '0') * 10 + (v[9] - '0');
2953228753Smm	tm.tm_min = (v[10] - '0') * 10 + (v[11] - '0');
2954228753Smm	tm.tm_sec = (v[12] - '0') * 10 + (v[13] - '0');
2955228753Smm	/* v[16] is the signed timezone offset, in 1/4-hour increments. */
2956228753Smm	offset = ((const signed char *)v)[16];
2957228753Smm	if (offset > -48 && offset < 52) {
2958228753Smm		tm.tm_hour -= offset / 4;
2959228753Smm		tm.tm_min -= (offset % 4) * 15;
2960228753Smm	}
2961228753Smm	return (time_from_tm(&tm));
2962228753Smm}
2963228753Smm
2964228753Smmstatic time_t
2965228753Smmtime_from_tm(struct tm *t)
2966228753Smm{
2967228753Smm#if HAVE_TIMEGM
2968228753Smm	/* Use platform timegm() if available. */
2969228753Smm	return (timegm(t));
2970228753Smm#else
2971228753Smm	/* Else use direct calculation using POSIX assumptions. */
2972228753Smm	/* First, fix up tm_yday based on the year/month/day. */
2973228753Smm	mktime(t);
2974228753Smm	/* Then we can compute timegm() from first principles. */
2975228753Smm	return (t->tm_sec + t->tm_min * 60 + t->tm_hour * 3600
2976228753Smm	    + t->tm_yday * 86400 + (t->tm_year - 70) * 31536000
2977228753Smm	    + ((t->tm_year - 69) / 4) * 86400 -
2978228753Smm	    ((t->tm_year - 1) / 100) * 86400
2979228753Smm	    + ((t->tm_year + 299) / 400) * 86400);
2980228753Smm#endif
2981228753Smm}
2982228753Smm
2983228753Smmstatic const char *
2984228753Smmbuild_pathname(struct archive_string *as, struct file_info *file)
2985228753Smm{
2986228753Smm	if (file->parent != NULL && archive_strlen(&file->parent->name) > 0) {
2987228753Smm		build_pathname(as, file->parent);
2988228753Smm		archive_strcat(as, "/");
2989228753Smm	}
2990228753Smm	if (archive_strlen(&file->name) == 0)
2991228753Smm		archive_strcat(as, ".");
2992228753Smm	else
2993228753Smm		archive_string_concat(as, &file->name);
2994228753Smm	return (as->s);
2995228753Smm}
2996228753Smm
2997228753Smm#if DEBUG
2998228753Smmstatic void
2999228753Smmdump_isodirrec(FILE *out, const unsigned char *isodirrec)
3000228753Smm{
3001228753Smm	fprintf(out, " l %d,",
3002228753Smm	    toi(isodirrec + DR_length_offset, DR_length_size));
3003228753Smm	fprintf(out, " a %d,",
3004228753Smm	    toi(isodirrec + DR_ext_attr_length_offset, DR_ext_attr_length_size));
3005228753Smm	fprintf(out, " ext 0x%x,",
3006228753Smm	    toi(isodirrec + DR_extent_offset, DR_extent_size));
3007228753Smm	fprintf(out, " s %d,",
3008228753Smm	    toi(isodirrec + DR_size_offset, DR_extent_size));
3009228753Smm	fprintf(out, " f 0x%02x,",
3010228753Smm	    toi(isodirrec + DR_flags_offset, DR_flags_size));
3011228753Smm	fprintf(out, " u %d,",
3012228753Smm	    toi(isodirrec + DR_file_unit_size_offset, DR_file_unit_size_size));
3013228753Smm	fprintf(out, " ilv %d,",
3014228753Smm	    toi(isodirrec + DR_interleave_offset, DR_interleave_size));
3015228753Smm	fprintf(out, " seq %d,",
3016228753Smm	    toi(isodirrec + DR_volume_sequence_number_offset, DR_volume_sequence_number_size));
3017228753Smm	fprintf(out, " nl %d:",
3018228753Smm	    toi(isodirrec + DR_name_len_offset, DR_name_len_size));
3019228753Smm	fprintf(out, " `%.*s'",
3020228753Smm	    toi(isodirrec + DR_name_len_offset, DR_name_len_size), isodirrec + DR_name_offset);
3021228753Smm}
3022228753Smm#endif
3023