archive_read_support_format_iso9660.c revision 228753
159243Sobrien/*-
259243Sobrien * Copyright (c) 2003-2007 Tim Kientzle
359243Sobrien * Copyright (c) 2009 Andreas Henriksson <andreas@fatal.se>
459243Sobrien * Copyright (c) 2009-2011 Michihiro NAKAJIMA
559243Sobrien * All rights reserved.
659243Sobrien *
759243Sobrien * Redistribution and use in source and binary forms, with or without
859243Sobrien * modification, are permitted provided that the following conditions
959243Sobrien * are met:
1059243Sobrien * 1. Redistributions of source code must retain the above copyright
1159243Sobrien *    notice, this list of conditions and the following disclaimer.
1259243Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1359243Sobrien *    notice, this list of conditions and the following disclaimer in the
1459243Sobrien *    documentation and/or other materials provided with the distribution.
1559243Sobrien *
16100616Smp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
1759243Sobrien * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1859243Sobrien * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1959243Sobrien * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
2059243Sobrien * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2159243Sobrien * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2259243Sobrien * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2359243Sobrien * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2459243Sobrien * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2559243Sobrien * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2659243Sobrien */
2759243Sobrien
2859243Sobrien#include "archive_platform.h"
2959243Sobrien__FBSDID("$FreeBSD: head/lib/libarchive/archive_read_support_format_iso9660.c 201246 2009-12-30 05:30:35Z kientzle $");
3059243Sobrien
3159243Sobrien#ifdef HAVE_ERRNO_H
3259243Sobrien#include <errno.h>
3359243Sobrien#endif
3459243Sobrien/* #include <stdint.h> */ /* See archive_platform.h */
3559243Sobrien#include <stdio.h>
3659243Sobrien#ifdef HAVE_STDLIB_H
3759243Sobrien#include <stdlib.h>
3859243Sobrien#endif
3959243Sobrien#ifdef HAVE_STRING_H
4059243Sobrien#include <string.h>
4159243Sobrien#endif
4259243Sobrien#include <time.h>
4359243Sobrien#ifdef HAVE_ZLIB_H
4459243Sobrien#include <zlib.h>
4559243Sobrien#endif
4659243Sobrien
4759243Sobrien#include "archive.h"
4859243Sobrien#include "archive_endian.h"
4959243Sobrien#include "archive_entry.h"
5059243Sobrien#include "archive_private.h"
5159243Sobrien#include "archive_read_private.h"
5259243Sobrien#include "archive_string.h"
5359243Sobrien
5459243Sobrien/*
5559243Sobrien * An overview of ISO 9660 format:
5659243Sobrien *
5759243Sobrien * Each disk is laid out as follows:
5859243Sobrien *   * 32k reserved for private use
5959243Sobrien *   * Volume descriptor table.  Each volume descriptor
6059243Sobrien *     is 2k and specifies basic format information.
6159243Sobrien *     The "Primary Volume Descriptor" (PVD) is defined by the
6269408Sache *     standard and should always be present; other volume
6359243Sobrien *     descriptors include various vendor-specific extensions.
6469408Sache *   * Files and directories.  Each file/dir is specified by
6559243Sobrien *     an "extent" (starting sector and length in bytes).
6659243Sobrien *     Dirs are just files with directory records packed one
6759243Sobrien *     after another.  The PVD contains a single dir entry
6859243Sobrien *     specifying the location of the root directory.  Everything
6959243Sobrien *     else follows from there.
7059243Sobrien *
7159243Sobrien * This module works by first reading the volume descriptors, then
7259243Sobrien * building a list of directory entries, sorted by starting
7359243Sobrien * sector.  At each step, I look for the earliest dir entry that
7459243Sobrien * hasn't yet been read, seek forward to that location and read
7559243Sobrien * that entry.  If it's a dir, I slurp in the new dir entries and
7659243Sobrien * add them to the heap; if it's a regular file, I return the
7759243Sobrien * corresponding archive_entry and wait for the client to request
7859243Sobrien * the file body.  This strategy allows us to read most compliant
7959243Sobrien * CDs with a single pass through the data, as required by libarchive.
8059243Sobrien */
8159243Sobrien#define	LOGICAL_BLOCK_SIZE	2048
8259243Sobrien#define	SYSTEM_AREA_BLOCK	16
8359243Sobrien
8459243Sobrien/* Structure of on-disk primary volume descriptor. */
8559243Sobrien#define PVD_type_offset 0
8659243Sobrien#define PVD_type_size 1
8759243Sobrien#define PVD_id_offset (PVD_type_offset + PVD_type_size)
8859243Sobrien#define PVD_id_size 5
8959243Sobrien#define PVD_version_offset (PVD_id_offset + PVD_id_size)
9059243Sobrien#define PVD_version_size 1
9159243Sobrien#define PVD_reserved1_offset (PVD_version_offset + PVD_version_size)
9259243Sobrien#define PVD_reserved1_size 1
9359243Sobrien#define PVD_system_id_offset (PVD_reserved1_offset + PVD_reserved1_size)
9459243Sobrien#define PVD_system_id_size 32
9559243Sobrien#define PVD_volume_id_offset (PVD_system_id_offset + PVD_system_id_size)
9659243Sobrien#define PVD_volume_id_size 32
9759243Sobrien#define PVD_reserved2_offset (PVD_volume_id_offset + PVD_volume_id_size)
9859243Sobrien#define PVD_reserved2_size 8
9959243Sobrien#define PVD_volume_space_size_offset (PVD_reserved2_offset + PVD_reserved2_size)
10059243Sobrien#define PVD_volume_space_size_size 8
10159243Sobrien#define PVD_reserved3_offset (PVD_volume_space_size_offset + PVD_volume_space_size_size)
10259243Sobrien#define PVD_reserved3_size 32
10359243Sobrien#define PVD_volume_set_size_offset (PVD_reserved3_offset + PVD_reserved3_size)
10459243Sobrien#define PVD_volume_set_size_size 4
10559243Sobrien#define PVD_volume_sequence_number_offset (PVD_volume_set_size_offset + PVD_volume_set_size_size)
10659243Sobrien#define PVD_volume_sequence_number_size 4
10759243Sobrien#define PVD_logical_block_size_offset (PVD_volume_sequence_number_offset + PVD_volume_sequence_number_size)
10859243Sobrien#define PVD_logical_block_size_size 4
10959243Sobrien#define PVD_path_table_size_offset (PVD_logical_block_size_offset + PVD_logical_block_size_size)
11059243Sobrien#define PVD_path_table_size_size 8
11159243Sobrien#define PVD_type_1_path_table_offset (PVD_path_table_size_offset + PVD_path_table_size_size)
11259243Sobrien#define PVD_type_1_path_table_size 4
11359243Sobrien#define PVD_opt_type_1_path_table_offset (PVD_type_1_path_table_offset + PVD_type_1_path_table_size)
11459243Sobrien#define PVD_opt_type_1_path_table_size 4
11559243Sobrien#define PVD_type_m_path_table_offset (PVD_opt_type_1_path_table_offset + PVD_opt_type_1_path_table_size)
11659243Sobrien#define PVD_type_m_path_table_size 4
11759243Sobrien#define PVD_opt_type_m_path_table_offset (PVD_type_m_path_table_offset + PVD_type_m_path_table_size)
11859243Sobrien#define PVD_opt_type_m_path_table_size 4
11959243Sobrien#define PVD_root_directory_record_offset (PVD_opt_type_m_path_table_offset + PVD_opt_type_m_path_table_size)
12059243Sobrien#define PVD_root_directory_record_size 34
12159243Sobrien#define PVD_volume_set_id_offset (PVD_root_directory_record_offset + PVD_root_directory_record_size)
12259243Sobrien#define PVD_volume_set_id_size 128
12359243Sobrien#define PVD_publisher_id_offset (PVD_volume_set_id_offset + PVD_volume_set_id_size)
12459243Sobrien#define PVD_publisher_id_size 128
12559243Sobrien#define PVD_preparer_id_offset (PVD_publisher_id_offset + PVD_publisher_id_size)
12659243Sobrien#define PVD_preparer_id_size 128
12759243Sobrien#define PVD_application_id_offset (PVD_preparer_id_offset + PVD_preparer_id_size)
12859243Sobrien#define PVD_application_id_size 128
12959243Sobrien#define PVD_copyright_file_id_offset (PVD_application_id_offset + PVD_application_id_size)
13059243Sobrien#define PVD_copyright_file_id_size 37
13159243Sobrien#define PVD_abstract_file_id_offset (PVD_copyright_file_id_offset + PVD_copyright_file_id_size)
13259243Sobrien#define PVD_abstract_file_id_size 37
13359243Sobrien#define PVD_bibliographic_file_id_offset (PVD_abstract_file_id_offset + PVD_abstract_file_id_size)
13459243Sobrien#define PVD_bibliographic_file_id_size 37
13559243Sobrien#define PVD_creation_date_offset (PVD_bibliographic_file_id_offset + PVD_bibliographic_file_id_size)
13659243Sobrien#define PVD_creation_date_size 17
13759243Sobrien#define PVD_modification_date_offset (PVD_creation_date_offset + PVD_creation_date_size)
13859243Sobrien#define PVD_modification_date_size 17
13959243Sobrien#define PVD_expiration_date_offset (PVD_modification_date_offset + PVD_modification_date_size)
14059243Sobrien#define PVD_expiration_date_size 17
14159243Sobrien#define PVD_effective_date_offset (PVD_expiration_date_offset + PVD_expiration_date_size)
14259243Sobrien#define PVD_effective_date_size 17
14359243Sobrien#define PVD_file_structure_version_offset (PVD_effective_date_offset + PVD_effective_date_size)
14459243Sobrien#define PVD_file_structure_version_size 1
14559243Sobrien#define PVD_reserved4_offset (PVD_file_structure_version_offset + PVD_file_structure_version_size)
14659243Sobrien#define PVD_reserved4_size 1
14759243Sobrien#define PVD_application_data_offset (PVD_reserved4_offset + PVD_reserved4_size)
14859243Sobrien#define PVD_application_data_size 512
149131962Smp#define PVD_reserved5_offset (PVD_application_data_offset + PVD_application_data_size)
150131962Smp#define PVD_reserved5_size (2048 - PVD_reserved5_offset)
15159243Sobrien
15259243Sobrien/* TODO: It would make future maintenance easier to just hardcode the
15359243Sobrien * above values.  In particular, ECMA119 states the offsets as part of
15459243Sobrien * the standard.  That would eliminate the need for the following check.*/
15559243Sobrien#if PVD_reserved5_offset != 1395
15659243Sobrien#error PVD offset and size definitions are wrong.
15759415Sobrien#endif
15859415Sobrien
15959415Sobrien
16059415Sobrien/* Structure of optional on-disk supplementary volume descriptor. */
161131962Smp#define SVD_type_offset 0
162131962Smp#define SVD_type_size 1
163131962Smp#define SVD_id_offset (SVD_type_offset + SVD_type_size)
164131962Smp#define SVD_id_size 5
165131962Smp#define SVD_version_offset (SVD_id_offset + SVD_id_size)
166131962Smp#define SVD_version_size 1
167131962Smp/* ... */
16859243Sobrien#define SVD_reserved1_offset	72
16959243Sobrien#define SVD_reserved1_size	8
17059243Sobrien#define SVD_volume_space_size_offset 80
17159243Sobrien#define SVD_volume_space_size_size 8
17259243Sobrien#define SVD_escape_sequences_offset (SVD_volume_space_size_offset + SVD_volume_space_size_size)
17359243Sobrien#define SVD_escape_sequences_size 32
17459243Sobrien/* ... */
17559243Sobrien#define SVD_logical_block_size_offset 128
17659243Sobrien#define SVD_logical_block_size_size 4
17759243Sobrien#define SVD_type_L_path_table_offset 140
17859243Sobrien#define SVD_type_M_path_table_offset 148
17959243Sobrien/* ... */
18059243Sobrien#define SVD_root_directory_record_offset 156
18159243Sobrien#define SVD_root_directory_record_size 34
18259243Sobrien#define SVD_file_structure_version_offset 881
18359243Sobrien#define SVD_reserved2_offset	882
18459243Sobrien#define SVD_reserved2_size	1
18559243Sobrien#define SVD_reserved3_offset	1395
18659243Sobrien#define SVD_reserved3_size	653
18759243Sobrien/* ... */
18859243Sobrien/* FIXME: validate correctness of last SVD entry offset. */
18959243Sobrien
19059243Sobrien/* Structure of an on-disk directory record. */
19159243Sobrien/* Note:  ISO9660 stores each multi-byte integer twice, once in
19259243Sobrien * each byte order.  The sizes here are the size of just one
19359243Sobrien * of the two integers.  (This is why the offset of a field isn't
19459243Sobrien * the same as the offset+size of the previous field.) */
19559243Sobrien#define DR_length_offset 0
19659243Sobrien#define DR_length_size 1
19759243Sobrien#define DR_ext_attr_length_offset 1
19859243Sobrien#define DR_ext_attr_length_size 1
19959243Sobrien#define DR_extent_offset 2
20059243Sobrien#define DR_extent_size 4
20159243Sobrien#define DR_size_offset 10
20259243Sobrien#define DR_size_size 4
20359243Sobrien#define DR_date_offset 18
20459243Sobrien#define DR_date_size 7
20559243Sobrien#define DR_flags_offset 25
20659243Sobrien#define DR_flags_size 1
20759243Sobrien#define DR_file_unit_size_offset 26
20859243Sobrien#define DR_file_unit_size_size 1
20959243Sobrien#define DR_interleave_offset 27
21059243Sobrien#define DR_interleave_size 1
21159243Sobrien#define DR_volume_sequence_number_offset 28
21259243Sobrien#define DR_volume_sequence_number_size 2
21359243Sobrien#define DR_name_len_offset 32
21459243Sobrien#define DR_name_len_size 1
21559243Sobrien#define DR_name_offset 33
21659243Sobrien
21759243Sobrien#ifdef HAVE_ZLIB_H
21859243Sobrienstatic const unsigned char zisofs_magic[8] = {
21959243Sobrien	0x37, 0xE4, 0x53, 0x96, 0xC9, 0xDB, 0xD6, 0x07
22059243Sobrien};
22159243Sobrien
22259243Sobrienstruct zisofs {
22359243Sobrien	/* Set 1 if this file compressed by paged zlib */
22459243Sobrien	int		 pz;
22559243Sobrien	int		 pz_log2_bs; /* Log2 of block size */
22659243Sobrien	uint64_t	 pz_uncompressed_size;
22759243Sobrien
22859243Sobrien	int		 initialized;
22959243Sobrien	unsigned char	*uncompressed_buffer;
23059243Sobrien	size_t		 uncompressed_buffer_size;
23159243Sobrien
23259243Sobrien	uint32_t	 pz_offset;
23359243Sobrien	unsigned char	 header[16];
23459243Sobrien	size_t		 header_avail;
23559243Sobrien	int		 header_passed;
23659243Sobrien	unsigned char	*block_pointers;
23759243Sobrien	size_t		 block_pointers_alloc;
23859243Sobrien	size_t		 block_pointers_size;
23959243Sobrien	size_t		 block_pointers_avail;
24059243Sobrien	size_t		 block_off;
24159243Sobrien	uint32_t	 block_avail;
24259243Sobrien
24359243Sobrien	z_stream	 stream;
24459243Sobrien	int		 stream_valid;
24559243Sobrien};
24659243Sobrien#else
24759243Sobrienstruct zisofs {
24859243Sobrien	/* Set 1 if this file compressed by paged zlib */
24959243Sobrien	int		 pz;
25059243Sobrien};
25159243Sobrien#endif
25259243Sobrien
25359243Sobrienstruct content {
25459243Sobrien	uint64_t	 offset;/* Offset on disk.		*/
25559243Sobrien	uint64_t	 size;	/* File size in bytes.		*/
25659243Sobrien	struct content	*next;
25759243Sobrien};
25859243Sobrien
25959243Sobrien/* In-memory storage for a directory record. */
26059243Sobrienstruct file_info {
26159243Sobrien	struct file_info	*use_next;
26259243Sobrien	struct file_info	*parent;
26359243Sobrien	struct file_info	*next;
26459243Sobrien	struct file_info	*re_next;
26559243Sobrien	int		 subdirs;
26659243Sobrien	uint64_t	 key;		/* Heap Key.			*/
26759243Sobrien	uint64_t	 offset;	/* Offset on disk.		*/
26859243Sobrien	uint64_t	 size;		/* File size in bytes.		*/
26959243Sobrien	uint32_t	 ce_offset;	/* Offset of CE.		*/
27059243Sobrien	uint32_t	 ce_size;	/* Size of CE.			*/
27159243Sobrien	char		 rr_moved;	/* Flag to rr_moved.		*/
27259243Sobrien	char		 rr_moved_has_re_only;
27359243Sobrien	char		 re;		/* Having RRIP "RE" extension.	*/
27459243Sobrien	char		 re_descendant;
27559243Sobrien	uint64_t	 cl_offset;	/* Having RRIP "CL" extension.	*/
27659243Sobrien	int		 birthtime_is_set;
27759243Sobrien	time_t		 birthtime;	/* File created time.		*/
27859243Sobrien	time_t		 mtime;		/* File last modified time.	*/
27959243Sobrien	time_t		 atime;		/* File last accessed time.	*/
28059243Sobrien	time_t		 ctime;		/* File attribute change time.	*/
28159243Sobrien	uint64_t	 rdev;		/* Device number.		*/
28259243Sobrien	mode_t		 mode;
28359243Sobrien	uid_t		 uid;
28459243Sobrien	gid_t		 gid;
28559243Sobrien	int64_t		 number;
28659243Sobrien	int		 nlinks;
28759243Sobrien	struct archive_string name; /* Pathname */
28859243Sobrien	char		 name_continues; /* Non-zero if name continues */
28959243Sobrien	struct archive_string symlink;
29059243Sobrien	char		 symlink_continues; /* Non-zero if link continues */
29159243Sobrien	/* Set 1 if this file compressed by paged zlib(zisofs) */
29259243Sobrien	int		 pz;
29359243Sobrien	int		 pz_log2_bs; /* Log2 of block size */
29459243Sobrien	uint64_t	 pz_uncompressed_size;
29559243Sobrien	/* Set 1 if this file is multi extent. */
29659243Sobrien	int		 multi_extent;
29759243Sobrien	struct {
29859243Sobrien		struct content	*first;
29959243Sobrien		struct content	**last;
30059243Sobrien	} contents;
30159243Sobrien	struct {
30259243Sobrien		struct file_info	*first;
30359243Sobrien		struct file_info	**last;
30459243Sobrien	} rede_files;
30559243Sobrien	/* To check a ininity loop. */
30659243Sobrien	struct file_info	*loop_by;
30759243Sobrien};
30859243Sobrien
30959243Sobrienstruct heap_queue {
31059243Sobrien	struct file_info **files;
31159243Sobrien	int		 allocated;
31259243Sobrien	int		 used;
31359243Sobrien};
31459243Sobrien
31559243Sobrienstruct iso9660 {
31659243Sobrien	int	magic;
31759243Sobrien#define ISO9660_MAGIC   0x96609660
31859243Sobrien
31959243Sobrien	int opt_support_joliet;
32059243Sobrien	int opt_support_rockridge;
32159243Sobrien
32259243Sobrien	struct archive_string pathname;
32359243Sobrien	char	seenRockridge;	/* Set true if RR extensions are used. */
32459243Sobrien	char	seenSUSP;	/* Set true if SUSP is beging used. */
32559243Sobrien	char	seenJoliet;
32659243Sobrien
32759243Sobrien	unsigned char	suspOffset;
32859243Sobrien	struct file_info *rr_moved;
32959243Sobrien	struct read_ce_queue {
33059243Sobrien		struct read_ce_req {
33159243Sobrien			uint64_t	 offset;/* Offset of CE on disk. */
33259243Sobrien			struct file_info *file;
33359243Sobrien		}		*reqs;
33459243Sobrien		int		 cnt;
33559243Sobrien		int		 allocated;
33659243Sobrien	}	read_ce_req;
33759243Sobrien
33859243Sobrien	int64_t		previous_number;
33959243Sobrien	struct archive_string previous_pathname;
34059243Sobrien
34159243Sobrien	struct file_info		*use_files;
34259243Sobrien	struct heap_queue		 pending_files;
34359243Sobrien	struct {
34459243Sobrien		struct file_info	*first;
34559243Sobrien		struct file_info	**last;
34659243Sobrien	}	cache_files;
34759243Sobrien	struct {
34859243Sobrien		struct file_info	*first;
34959243Sobrien		struct file_info	**last;
35059243Sobrien	}	re_files;
35159243Sobrien
35259243Sobrien	uint64_t current_position;
35359243Sobrien	ssize_t	logical_block_size;
35459243Sobrien	uint64_t volume_size; /* Total size of volume in bytes. */
35559243Sobrien	int32_t  volume_block;/* Total size of volume in logical blocks. */
35659243Sobrien
35759243Sobrien	struct vd {
35859415Sobrien		int		location;	/* Location of Extent.	*/
35959415Sobrien		uint32_t	size;
36059415Sobrien	} primary, joliet;
36159415Sobrien
36259415Sobrien	off_t	entry_sparse_offset;
36359415Sobrien	int64_t	entry_bytes_remaining;
36459415Sobrien	struct zisofs	 entry_zisofs;
36559415Sobrien	struct content	*entry_content;
36659243Sobrien};
36759243Sobrien
36859243Sobrienstatic int	archive_read_format_iso9660_bid(struct archive_read *);
36959243Sobrienstatic int	archive_read_format_iso9660_options(struct archive_read *,
37059243Sobrien		    const char *, const char *);
37159243Sobrienstatic int	archive_read_format_iso9660_cleanup(struct archive_read *);
37259243Sobrienstatic int	archive_read_format_iso9660_read_data(struct archive_read *,
37359243Sobrien		    const void **, size_t *, off_t *);
37459243Sobrienstatic int	archive_read_format_iso9660_read_data_skip(struct archive_read *);
37559243Sobrienstatic int	archive_read_format_iso9660_read_header(struct archive_read *,
37659243Sobrien		    struct archive_entry *);
37759243Sobrienstatic const char *build_pathname(struct archive_string *, struct file_info *);
37859243Sobrien#if DEBUG
37959243Sobrienstatic void	dump_isodirrec(FILE *, const unsigned char *isodirrec);
38059243Sobrien#endif
38159243Sobrienstatic time_t	time_from_tm(struct tm *);
38259243Sobrienstatic time_t	isodate17(const unsigned char *);
38359243Sobrienstatic time_t	isodate7(const unsigned char *);
38459243Sobrienstatic int	isBootRecord(struct iso9660 *, const unsigned char *);
38559415Sobrienstatic int	isVolumePartition(struct iso9660 *, const unsigned char *);
38659415Sobrienstatic int	isVDSetTerminator(struct iso9660 *, const unsigned char *);
38759415Sobrienstatic int	isJolietSVD(struct iso9660 *, const unsigned char *);
38859415Sobrienstatic int	isSVD(struct iso9660 *, const unsigned char *);
38959415Sobrienstatic int	isEVD(struct iso9660 *, const unsigned char *);
39059415Sobrienstatic int	isPVD(struct iso9660 *, const unsigned char *);
39159415Sobrienstatic int	next_cache_entry(struct archive_read *, struct iso9660 *,
39259415Sobrien		    struct file_info **);
39359243Sobrienstatic int	next_entry_seek(struct archive_read *a, struct iso9660 *iso9660,
39459243Sobrien		    struct file_info **pfile);
39559243Sobrienstatic struct file_info *
39659243Sobrien		parse_file_info(struct archive_read *a,
39759243Sobrien		    struct file_info *parent, const unsigned char *isodirrec);
39859243Sobrienstatic int	parse_rockridge(struct archive_read *a,
39959243Sobrien		    struct file_info *file, const unsigned char *start,
40059243Sobrien		    const unsigned char *end);
40159243Sobrienstatic int	register_CE(struct archive_read *a, int32_t location,
40259243Sobrien		    struct file_info *file);
40359243Sobrienstatic int	read_CE(struct archive_read *a, struct iso9660 *iso9660);
40459243Sobrienstatic void	parse_rockridge_NM1(struct file_info *,
40559243Sobrien		    const unsigned char *, int);
40659243Sobrienstatic void	parse_rockridge_SL1(struct file_info *,
40759243Sobrien		    const unsigned char *, int);
40859243Sobrienstatic void	parse_rockridge_TF1(struct file_info *,
40959243Sobrien		    const unsigned char *, int);
41059243Sobrienstatic void	parse_rockridge_ZF1(struct file_info *,
41159243Sobrien		    const unsigned char *, int);
41259243Sobrienstatic void	register_file(struct iso9660 *, struct file_info *);
41359243Sobrienstatic void	release_files(struct iso9660 *);
41459243Sobrienstatic unsigned	toi(const void *p, int n);
41559243Sobrienstatic inline void re_add_entry(struct iso9660 *, struct file_info *);
41659243Sobrienstatic inline struct file_info * re_get_entry(struct iso9660 *);
41759243Sobrienstatic inline int rede_add_entry(struct file_info *);
41859243Sobrienstatic inline struct file_info * rede_get_entry(struct file_info *);
41959243Sobrienstatic inline void cache_add_entry(struct iso9660 *iso9660,
42059243Sobrien		    struct file_info *file);
42159243Sobrienstatic inline struct file_info *cache_get_entry(struct iso9660 *iso9660);
42259243Sobrienstatic void	heap_add_entry(struct heap_queue *heap,
42359243Sobrien		    struct file_info *file, uint64_t key);
42459243Sobrienstatic struct file_info *heap_get_entry(struct heap_queue *heap);
42559243Sobrien
42659243Sobrien#define add_entry(iso9660, file)	\
42759243Sobrien	heap_add_entry(&((iso9660)->pending_files), file, file->offset)
42859243Sobrien#define next_entry(iso9660)		\
42959243Sobrien	heap_get_entry(&((iso9660)->pending_files))
43059243Sobrien
43159243Sobrienint
43259243Sobrienarchive_read_support_format_iso9660(struct archive *_a)
43359243Sobrien{
43459243Sobrien	struct archive_read *a = (struct archive_read *)_a;
43559243Sobrien	struct iso9660 *iso9660;
43659243Sobrien	int r;
43759243Sobrien
43859243Sobrien	iso9660 = (struct iso9660 *)malloc(sizeof(*iso9660));
43959243Sobrien	if (iso9660 == NULL) {
44059243Sobrien		archive_set_error(&a->archive, ENOMEM, "Can't allocate iso9660 data");
44159243Sobrien		return (ARCHIVE_FATAL);
44259243Sobrien	}
44359243Sobrien	memset(iso9660, 0, sizeof(*iso9660));
44459243Sobrien	iso9660->magic = ISO9660_MAGIC;
44559243Sobrien	iso9660->cache_files.first = NULL;
44659243Sobrien	iso9660->cache_files.last = &(iso9660->cache_files.first);
44759243Sobrien	iso9660->re_files.first = NULL;
44859243Sobrien	iso9660->re_files.last = &(iso9660->re_files.first);
44959243Sobrien	/* Enable to support Joliet extensions by default.	*/
45059243Sobrien	iso9660->opt_support_joliet = 1;
45159243Sobrien	/* Enable to support Rock Ridge extensions by default.	*/
45259243Sobrien	iso9660->opt_support_rockridge = 1;
45359243Sobrien
45459243Sobrien	r = __archive_read_register_format(a,
45559243Sobrien	    iso9660,
45659243Sobrien	    "iso9660",
45759243Sobrien	    archive_read_format_iso9660_bid,
45859243Sobrien	    archive_read_format_iso9660_options,
45959243Sobrien	    archive_read_format_iso9660_read_header,
46059243Sobrien	    archive_read_format_iso9660_read_data,
46159243Sobrien	    archive_read_format_iso9660_read_data_skip,
46259243Sobrien	    archive_read_format_iso9660_cleanup);
46359243Sobrien
46459243Sobrien	if (r != ARCHIVE_OK) {
46559243Sobrien		free(iso9660);
46659243Sobrien		return (r);
46759243Sobrien	}
46859243Sobrien	return (ARCHIVE_OK);
46959243Sobrien}
47059243Sobrien
47159243Sobrien
47259243Sobrienstatic int
47359243Sobrienarchive_read_format_iso9660_bid(struct archive_read *a)
47459243Sobrien{
47559243Sobrien	struct iso9660 *iso9660;
47659243Sobrien	ssize_t bytes_read;
47759243Sobrien	const void *h;
47859415Sobrien	const unsigned char *p;
47959243Sobrien	int seenTerminator;
48059243Sobrien
48159243Sobrien	iso9660 = (struct iso9660 *)(a->format->data);
48259243Sobrien
48359243Sobrien	/*
48459243Sobrien	 * Skip the first 32k (reserved area) and get the first
48559243Sobrien	 * 8 sectors of the volume descriptor table.  Of course,
48659243Sobrien	 * if the I/O layer gives us more, we'll take it.
48759243Sobrien	 */
48859243Sobrien#define RESERVED_AREA	(SYSTEM_AREA_BLOCK * LOGICAL_BLOCK_SIZE)
48959243Sobrien	h = __archive_read_ahead(a,
49059243Sobrien	    RESERVED_AREA + 8 * LOGICAL_BLOCK_SIZE,
49159243Sobrien	    &bytes_read);
49259243Sobrien	if (h == NULL)
49359243Sobrien	    return (-1);
49459243Sobrien	p = (const unsigned char *)h;
49559243Sobrien
49659243Sobrien	/* Skip the reserved area. */
49759243Sobrien	bytes_read -= RESERVED_AREA;
49859243Sobrien	p += RESERVED_AREA;
49959243Sobrien
50059243Sobrien	/* Check each volume descriptor. */
50159243Sobrien	seenTerminator = 0;
50259243Sobrien	for (; bytes_read > LOGICAL_BLOCK_SIZE;
50359243Sobrien	    bytes_read -= LOGICAL_BLOCK_SIZE, p += LOGICAL_BLOCK_SIZE) {
50459243Sobrien		/* Do not handle undefined Volume Descriptor Type. */
50559243Sobrien		if (p[0] >= 4 && p[0] <= 254)
50659243Sobrien			return (0);
50759243Sobrien		/* Standard Identifier must be "CD001" */
50859243Sobrien		if (memcmp(p + 1, "CD001", 5) != 0)
50959243Sobrien			return (0);
51059243Sobrien		if (!iso9660->primary.location) {
51159243Sobrien			if (isPVD(iso9660, p))
51259243Sobrien				continue;
51359243Sobrien		}
51459243Sobrien		if (!iso9660->joliet.location) {
51559243Sobrien			if (isJolietSVD(iso9660, p))
51659243Sobrien				continue;
51759243Sobrien		}
51859243Sobrien		if (isBootRecord(iso9660, p))
51959243Sobrien			continue;
52059243Sobrien		if (isEVD(iso9660, p))
52159243Sobrien			continue;
52259243Sobrien		if (isSVD(iso9660, p))
52359243Sobrien			continue;
52459243Sobrien		if (isVolumePartition(iso9660, p))
52559243Sobrien			continue;
52659243Sobrien		if (isVDSetTerminator(iso9660, p)) {
52759243Sobrien			seenTerminator = 1;
52859243Sobrien			break;
52959243Sobrien		}
53059243Sobrien		return (0);
53159243Sobrien	}
53259243Sobrien	/*
53359243Sobrien	 * ISO 9660 format must have Primary Volume Descriptor and
53459243Sobrien	 * Volume Descriptor Set Terminator.
53559243Sobrien	 */
53659243Sobrien	if (seenTerminator && iso9660->primary.location > 16)
53759243Sobrien		return (48);
53859243Sobrien
53959243Sobrien	/* We didn't find a valid PVD; return a bid of zero. */
54059243Sobrien	return (0);
54159243Sobrien}
54259243Sobrien
54359243Sobrienstatic int
54459243Sobrienarchive_read_format_iso9660_options(struct archive_read *a,
54559243Sobrien		const char *key, const char *val)
54659243Sobrien{
54759243Sobrien	struct iso9660 *iso9660;
54859243Sobrien
54959243Sobrien	iso9660 = (struct iso9660 *)(a->format->data);
55059243Sobrien
55159243Sobrien	if (strcmp(key, "joliet") == 0) {
55259243Sobrien		if (val == NULL || strcmp(val, "off") == 0 ||
55359243Sobrien				strcmp(val, "ignore") == 0 ||
55459243Sobrien				strcmp(val, "disable") == 0 ||
55559243Sobrien				strcmp(val, "0") == 0)
55659243Sobrien			iso9660->opt_support_joliet = 0;
55759243Sobrien		else
55859243Sobrien			iso9660->opt_support_joliet = 1;
55959243Sobrien		return (ARCHIVE_OK);
56059243Sobrien	}
56159243Sobrien	if (strcmp(key, "rockridge") == 0 ||
56259243Sobrien	    strcmp(key, "Rockridge") == 0) {
56359243Sobrien		iso9660->opt_support_rockridge = val != NULL;
56459243Sobrien		return (ARCHIVE_OK);
56559243Sobrien	}
56659243Sobrien
56759243Sobrien	/* Note: The "warn" return is just to inform the options
56859243Sobrien	 * supervisor that we didn't handle it.  It will generate
56959243Sobrien	 * a suitable error if noone used this option. */
57059243Sobrien	return (ARCHIVE_WARN);
57159243Sobrien}
57259243Sobrien
57359243Sobrienstatic int
57459243SobrienisBootRecord(struct iso9660 *iso9660, const unsigned char *h)
57559243Sobrien{
57659243Sobrien	(void)iso9660; /* UNUSED */
57759243Sobrien
57859243Sobrien	/* Type of the Volume Descriptor Boot Record must be 0. */
57959243Sobrien	if (h[0] != 0)
58059243Sobrien		return (0);
58159243Sobrien
58259243Sobrien	/* Volume Descriptor Version must be 1. */
58359243Sobrien	if (h[6] != 1)
58459243Sobrien		return (0);
58559243Sobrien
58659243Sobrien	return (1);
58759243Sobrien}
58859243Sobrien
58959243Sobrienstatic int
59059243SobrienisVolumePartition(struct iso9660 *iso9660, const unsigned char *h)
59159243Sobrien{
59259243Sobrien	int32_t location;
59359243Sobrien
59459243Sobrien	/* Type of the Volume Partition Descriptor must be 3. */
59559243Sobrien	if (h[0] != 3)
59659243Sobrien		return (0);
59759243Sobrien
59859243Sobrien	/* Volume Descriptor Version must be 1. */
59959243Sobrien	if (h[6] != 1)
60059243Sobrien		return (0);
60159243Sobrien	/* Unused Field */
60259243Sobrien	if (h[7] != 0)
60359243Sobrien		return (0);
60459243Sobrien
60559243Sobrien	location = archive_le32dec(h + 72);
60659243Sobrien	if (location <= SYSTEM_AREA_BLOCK ||
60759243Sobrien	    location >= iso9660->volume_block)
60859243Sobrien		return (0);
60959243Sobrien	if ((uint32_t)location != archive_be32dec(h + 76))
61059243Sobrien		return (0);
61159243Sobrien
61259243Sobrien	return (1);
61359243Sobrien}
61459243Sobrien
61559243Sobrienstatic int
61659243SobrienisVDSetTerminator(struct iso9660 *iso9660, const unsigned char *h)
61759243Sobrien{
61859243Sobrien	int i;
61959243Sobrien
62059243Sobrien	(void)iso9660; /* UNUSED */
62159243Sobrien
62259243Sobrien	/* Type of the Volume Descriptor Set Terminator must be 255. */
62359243Sobrien	if (h[0] != 255)
62459243Sobrien		return (0);
62559243Sobrien
62659243Sobrien	/* Volume Descriptor Version must be 1. */
62759243Sobrien	if (h[6] != 1)
62859243Sobrien		return (0);
62959243Sobrien
63059243Sobrien	/* Reserved field must be 0. */
63159243Sobrien	for (i = 7; i < 2048; ++i)
63259243Sobrien		if (h[i] != 0)
63359243Sobrien			return (0);
63459243Sobrien
63559243Sobrien	return (1);
63659243Sobrien}
63759243Sobrien
63859243Sobrienstatic int
63959243SobrienisJolietSVD(struct iso9660 *iso9660, const unsigned char *h)
64059243Sobrien{
64159243Sobrien	const unsigned char *p;
64259243Sobrien	ssize_t logical_block_size;
64359243Sobrien	int32_t volume_block;
64459243Sobrien
64559243Sobrien	/* Check if current sector is a kind of Supplementary Volume
64659243Sobrien	 * Descriptor. */
64759243Sobrien	if (!isSVD(iso9660, h))
64859243Sobrien		return (0);
64959243Sobrien
65059243Sobrien	/* FIXME: do more validations according to joliet spec. */
65159243Sobrien
65259243Sobrien	/* check if this SVD contains joliet extension! */
65359243Sobrien	p = h + SVD_escape_sequences_offset;
65459243Sobrien	/* N.B. Joliet spec says p[1] == '\\', but.... */
65559243Sobrien	if (p[0] == '%' && p[1] == '/') {
65659243Sobrien		int level = 0;
65759243Sobrien
65859243Sobrien		if (p[2] == '@')
65959243Sobrien			level = 1;
66059243Sobrien		else if (p[2] == 'C')
66159243Sobrien			level = 2;
66259243Sobrien		else if (p[2] == 'E')
66359243Sobrien			level = 3;
66459243Sobrien		else /* not joliet */
66559243Sobrien			return (0);
66659243Sobrien
66759243Sobrien		iso9660->seenJoliet = level;
66859243Sobrien
66959243Sobrien	} else /* not joliet */
67059243Sobrien		return (0);
67159243Sobrien
67259243Sobrien	logical_block_size =
67359243Sobrien	    archive_le16dec(h + SVD_logical_block_size_offset);
67459243Sobrien	volume_block = archive_le32dec(h + SVD_volume_space_size_offset);
67559243Sobrien
67659243Sobrien	iso9660->logical_block_size = logical_block_size;
67759243Sobrien	iso9660->volume_block = volume_block;
67859243Sobrien	iso9660->volume_size = logical_block_size * (uint64_t)volume_block;
67959243Sobrien	/* Read Root Directory Record in Volume Descriptor. */
68059243Sobrien	p = h + SVD_root_directory_record_offset;
68159243Sobrien	iso9660->joliet.location = archive_le32dec(p + DR_extent_offset);
68259243Sobrien	iso9660->joliet.size = archive_le32dec(p + DR_size_offset);
68359243Sobrien
68459243Sobrien	return (48);
68559243Sobrien}
68659243Sobrien
68759243Sobrienstatic int
68859243SobrienisSVD(struct iso9660 *iso9660, const unsigned char *h)
68959243Sobrien{
69059243Sobrien	const unsigned char *p;
69159243Sobrien	ssize_t logical_block_size;
69259243Sobrien	int32_t volume_block;
69359243Sobrien	int32_t location;
69459243Sobrien	int i;
69559243Sobrien
69659243Sobrien	(void)iso9660; /* UNUSED */
69759243Sobrien
69859243Sobrien	/* Type 2 means it's a SVD. */
69959243Sobrien	if (h[SVD_type_offset] != 2)
70059243Sobrien		return (0);
70159243Sobrien
70259243Sobrien	/* Reserved field must be 0. */
70359243Sobrien	for (i = 0; i < SVD_reserved1_size; ++i)
70459243Sobrien		if (h[SVD_reserved1_offset + i] != 0)
70559243Sobrien			return (0);
70659243Sobrien	for (i = 0; i < SVD_reserved2_size; ++i)
70759243Sobrien		if (h[SVD_reserved2_offset + i] != 0)
70859243Sobrien			return (0);
709131962Smp	for (i = 0; i < SVD_reserved3_size; ++i)
710131962Smp		if (h[SVD_reserved3_offset + i] != 0)
71159243Sobrien			return (0);
71259243Sobrien
71359243Sobrien	/* File structure version must be 1 for ISO9660/ECMA119. */
71459243Sobrien	if (h[SVD_file_structure_version_offset] != 1)
71559243Sobrien		return (0);
71659243Sobrien
71759243Sobrien	logical_block_size =
71859243Sobrien	    archive_le16dec(h + SVD_logical_block_size_offset);
71959243Sobrien	if (logical_block_size <= 0)
72059243Sobrien		return (0);
72159243Sobrien
72259243Sobrien	volume_block = archive_le32dec(h + SVD_volume_space_size_offset);
72359243Sobrien	if (volume_block <= SYSTEM_AREA_BLOCK+4)
72459243Sobrien		return (0);
72559243Sobrien
72659243Sobrien	/* Location of Occurrence of Type L Path Table must be
72759243Sobrien	 * available location,
72859243Sobrien	 * >= SYSTEM_AREA_BLOCK(16) + 2 and < Volume Space Size. */
72959243Sobrien	location = archive_le32dec(h+SVD_type_L_path_table_offset);
73059243Sobrien	if (location < SYSTEM_AREA_BLOCK+2 || location >= volume_block)
73159243Sobrien		return (0);
73259243Sobrien
73359243Sobrien	/* The Type M Path Table must be at a valid location (WinISO
73459243Sobrien	 * and probably other programs omit this, so we allow zero)
73559243Sobrien	 *
73659243Sobrien	 * >= SYSTEM_AREA_BLOCK(16) + 2 and < Volume Space Size. */
73759243Sobrien	location = archive_be32dec(h+SVD_type_M_path_table_offset);
73859243Sobrien	if ((location > 0 && location < SYSTEM_AREA_BLOCK+2)
73959243Sobrien	    || location >= volume_block)
74059243Sobrien		return (0);
74159243Sobrien
74259243Sobrien	/* Read Root Directory Record in Volume Descriptor. */
74359243Sobrien	p = h + SVD_root_directory_record_offset;
74459243Sobrien	if (p[DR_length_offset] != 34)
74559243Sobrien		return (0);
746
747	return (48);
748}
749
750static int
751isEVD(struct iso9660 *iso9660, const unsigned char *h)
752{
753	const unsigned char *p;
754	ssize_t logical_block_size;
755	int32_t volume_block;
756	int32_t location;
757	int i;
758
759	(void)iso9660; /* UNUSED */
760
761	/* Type of the Enhanced Volume Descriptor must be 2. */
762	if (h[PVD_type_offset] != 2)
763		return (0);
764
765	/* EVD version must be 2. */
766	if (h[PVD_version_offset] != 2)
767		return (0);
768
769	/* Reserved field must be 0. */
770	if (h[PVD_reserved1_offset] != 0)
771		return (0);
772
773	/* Reserved field must be 0. */
774	for (i = 0; i < PVD_reserved2_size; ++i)
775		if (h[PVD_reserved2_offset + i] != 0)
776			return (0);
777
778	/* Reserved field must be 0. */
779	for (i = 0; i < PVD_reserved3_size; ++i)
780		if (h[PVD_reserved3_offset + i] != 0)
781			return (0);
782
783	/* Logical block size must be > 0. */
784	/* I've looked at Ecma 119 and can't find any stronger
785	 * restriction on this field. */
786	logical_block_size =
787	    archive_le16dec(h + PVD_logical_block_size_offset);
788	if (logical_block_size <= 0)
789		return (0);
790
791	volume_block =
792	    archive_le32dec(h + PVD_volume_space_size_offset);
793	if (volume_block <= SYSTEM_AREA_BLOCK+4)
794		return (0);
795
796	/* File structure version must be 2 for ISO9660:1999. */
797	if (h[PVD_file_structure_version_offset] != 2)
798		return (0);
799
800	/* Location of Occurrence of Type L Path Table must be
801	 * available location,
802	 * >= SYSTEM_AREA_BLOCK(16) + 2 and < Volume Space Size. */
803	location = archive_le32dec(h+PVD_type_1_path_table_offset);
804	if (location < SYSTEM_AREA_BLOCK+2 || location >= volume_block)
805		return (0);
806
807	/* Location of Occurrence of Type M Path Table must be
808	 * available location,
809	 * >= SYSTEM_AREA_BLOCK(16) + 2 and < Volume Space Size. */
810	location = archive_be32dec(h+PVD_type_m_path_table_offset);
811	if ((location > 0 && location < SYSTEM_AREA_BLOCK+2)
812	    || location >= volume_block)
813		return (0);
814
815	/* Reserved field must be 0. */
816	for (i = 0; i < PVD_reserved4_size; ++i)
817		if (h[PVD_reserved4_offset + i] != 0)
818			return (0);
819
820	/* Reserved field must be 0. */
821	for (i = 0; i < PVD_reserved5_size; ++i)
822		if (h[PVD_reserved5_offset + i] != 0)
823			return (0);
824
825	/* Read Root Directory Record in Volume Descriptor. */
826	p = h + PVD_root_directory_record_offset;
827	if (p[DR_length_offset] != 34)
828		return (0);
829
830	return (48);
831}
832
833static int
834isPVD(struct iso9660 *iso9660, const unsigned char *h)
835{
836	const unsigned char *p;
837	ssize_t logical_block_size;
838	int32_t volume_block;
839	int32_t location;
840	int i;
841
842	/* Type of the Primary Volume Descriptor must be 1. */
843	if (h[PVD_type_offset] != 1)
844		return (0);
845
846	/* PVD version must be 1. */
847	if (h[PVD_version_offset] != 1)
848		return (0);
849
850	/* Reserved field must be 0. */
851	if (h[PVD_reserved1_offset] != 0)
852		return (0);
853
854	/* Reserved field must be 0. */
855	for (i = 0; i < PVD_reserved2_size; ++i)
856		if (h[PVD_reserved2_offset + i] != 0)
857			return (0);
858
859	/* Reserved field must be 0. */
860	for (i = 0; i < PVD_reserved3_size; ++i)
861		if (h[PVD_reserved3_offset + i] != 0)
862			return (0);
863
864	/* Logical block size must be > 0. */
865	/* I've looked at Ecma 119 and can't find any stronger
866	 * restriction on this field. */
867	logical_block_size =
868	    archive_le16dec(h + PVD_logical_block_size_offset);
869	if (logical_block_size <= 0)
870		return (0);
871
872	volume_block = archive_le32dec(h + PVD_volume_space_size_offset);
873	if (volume_block <= SYSTEM_AREA_BLOCK+4)
874		return (0);
875
876	/* File structure version must be 1 for ISO9660/ECMA119. */
877	if (h[PVD_file_structure_version_offset] != 1)
878		return (0);
879
880	/* Location of Occurrence of Type L Path Table must be
881	 * available location,
882	 * > SYSTEM_AREA_BLOCK(16) + 2 and < Volume Space Size. */
883	location = archive_le32dec(h+PVD_type_1_path_table_offset);
884	if (location < SYSTEM_AREA_BLOCK+2 || location >= volume_block)
885		return (0);
886
887	/* The Type M Path Table must also be at a valid location
888	 * (although ECMA 119 requires a Type M Path Table, WinISO and
889	 * probably other programs omit it, so we permit a zero here)
890	 *
891	 * >= SYSTEM_AREA_BLOCK(16) + 2 and < Volume Space Size. */
892	location = archive_be32dec(h+PVD_type_m_path_table_offset);
893	if ((location > 0 && location < SYSTEM_AREA_BLOCK+2)
894	    || location >= volume_block)
895		return (0);
896
897	/* Reserved field must be 0. */
898	for (i = 0; i < PVD_reserved4_size; ++i)
899		if (h[PVD_reserved4_offset + i] != 0)
900			return (0);
901
902	/* Reserved field must be 0. */
903	for (i = 0; i < PVD_reserved5_size; ++i)
904		if (h[PVD_reserved5_offset + i] != 0)
905			return (0);
906
907	/* XXX TODO: Check other values for sanity; reject more
908	 * malformed PVDs. XXX */
909
910	/* Read Root Directory Record in Volume Descriptor. */
911	p = h + PVD_root_directory_record_offset;
912	if (p[DR_length_offset] != 34)
913		return (0);
914
915	iso9660->logical_block_size = logical_block_size;
916	iso9660->volume_block = volume_block;
917	iso9660->volume_size = logical_block_size * (uint64_t)volume_block;
918	iso9660->primary.location = archive_le32dec(p + DR_extent_offset);
919	iso9660->primary.size = archive_le32dec(p + DR_size_offset);
920
921	return (48);
922}
923
924static int
925read_children(struct archive_read *a, struct file_info *parent)
926{
927	struct iso9660 *iso9660;
928	const unsigned char *b, *p;
929	struct file_info *multi;
930	size_t step;
931
932	iso9660 = (struct iso9660 *)(a->format->data);
933	if (iso9660->current_position > parent->offset) {
934		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
935		    "Ignoring out-of-order directory (%s) %jd > %jd",
936		    parent->name.s,
937		    (intmax_t)iso9660->current_position,
938		    (intmax_t)parent->offset);
939		return (ARCHIVE_WARN);
940	}
941	if (parent->offset + parent->size > iso9660->volume_size) {
942		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
943		    "Directory is beyond end-of-media: %s",
944		    parent->name.s);
945		return (ARCHIVE_WARN);
946	}
947	if (iso9660->current_position < parent->offset) {
948		int64_t skipsize;
949
950		skipsize = parent->offset - iso9660->current_position;
951		skipsize = __archive_read_skip(a, skipsize);
952		if (skipsize < 0)
953			return ((int)skipsize);
954		iso9660->current_position = parent->offset;
955	}
956
957	step = ((parent->size + iso9660->logical_block_size -1) /
958	    iso9660->logical_block_size) * iso9660->logical_block_size;
959	b = __archive_read_ahead(a, step, NULL);
960	if (b == NULL) {
961		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
962		    "Failed to read full block when scanning "
963		    "ISO9660 directory list");
964		return (ARCHIVE_FATAL);
965	}
966	__archive_read_consume(a, step);
967	iso9660->current_position += step;
968	multi = NULL;
969	while (step) {
970		p = b;
971		b += iso9660->logical_block_size;
972		step -= iso9660->logical_block_size;
973		for (; *p != 0 && p < b && p + *p <= b; p += *p) {
974			struct file_info *child;
975
976			/* N.B.: these special directory identifiers
977			 * are 8 bit "values" even on a
978			 * Joliet CD with UCS-2 (16bit) encoding.
979			 */
980
981			/* Skip '.' entry. */
982			if (*(p + DR_name_len_offset) == 1
983			    && *(p + DR_name_offset) == '\0')
984				continue;
985			/* Skip '..' entry. */
986			if (*(p + DR_name_len_offset) == 1
987			    && *(p + DR_name_offset) == '\001')
988				continue;
989			child = parse_file_info(a, parent, p);
990			if (child == NULL)
991				return (ARCHIVE_FATAL);
992			if (child->cl_offset == 0 &&
993			    (child->multi_extent || multi != NULL)) {
994				struct content *con;
995
996				if (multi == NULL) {
997					multi = child;
998					multi->contents.first = NULL;
999					multi->contents.last =
1000					    &(multi->contents.first);
1001				}
1002				con = malloc(sizeof(struct content));
1003				if (con == NULL) {
1004					archive_set_error(
1005					    &a->archive, ENOMEM,
1006					    "No memory for "
1007					    "multi extent");
1008					return (ARCHIVE_FATAL);
1009				}
1010				con->offset = child->offset;
1011				con->size = child->size;
1012				con->next = NULL;
1013				*multi->contents.last = con;
1014				multi->contents.last = &(con->next);
1015				if (multi == child)
1016					add_entry(iso9660, child);
1017				else {
1018					multi->size += child->size;
1019					if (!child->multi_extent)
1020						multi = NULL;
1021				}
1022			} else
1023				add_entry(iso9660, child);
1024		}
1025	}
1026
1027	/* Read data which recorded by RRIP "CE" extension. */
1028	if (read_CE(a, iso9660) != ARCHIVE_OK)
1029		return (ARCHIVE_FATAL);
1030
1031	return (ARCHIVE_OK);
1032}
1033
1034static int
1035archive_read_format_iso9660_read_header(struct archive_read *a,
1036    struct archive_entry *entry)
1037{
1038	struct iso9660 *iso9660;
1039	struct file_info *file;
1040	int r, rd_r = ARCHIVE_OK;
1041
1042	iso9660 = (struct iso9660 *)(a->format->data);
1043
1044	if (!a->archive.archive_format) {
1045		a->archive.archive_format = ARCHIVE_FORMAT_ISO9660;
1046		a->archive.archive_format_name = "ISO9660";
1047	}
1048
1049	if (iso9660->current_position == 0) {
1050		int64_t skipsize;
1051		struct vd *vd;
1052		const void *block;
1053		char seenJoliet;
1054
1055		vd = &(iso9660->primary);
1056		if (!iso9660->opt_support_joliet)
1057			iso9660->seenJoliet = 0;
1058		if (iso9660->seenJoliet &&
1059			vd->location > iso9660->joliet.location)
1060			/* This condition is unlikely; by way of caution. */
1061			vd = &(iso9660->joliet);
1062
1063		skipsize = LOGICAL_BLOCK_SIZE * vd->location;
1064		skipsize = __archive_read_skip(a, skipsize);
1065		if (skipsize < 0)
1066			return ((int)skipsize);
1067		iso9660->current_position = skipsize;
1068
1069		block = __archive_read_ahead(a, vd->size, NULL);
1070		if (block == NULL) {
1071			archive_set_error(&a->archive,
1072			    ARCHIVE_ERRNO_MISC,
1073			    "Failed to read full block when scanning "
1074			    "ISO9660 directory list");
1075			return (ARCHIVE_FATAL);
1076		}
1077
1078		/*
1079		 * While reading Root Directory, flag seenJoliet
1080		 * must be zero to avoid converting special name
1081		 * 0x00(Current Directory) and next byte to UCS2.
1082		 */
1083		seenJoliet = iso9660->seenJoliet;/* Save flag. */
1084		iso9660->seenJoliet = 0;
1085		file = parse_file_info(a, NULL, block);
1086		if (file == NULL)
1087			return (ARCHIVE_FATAL);
1088		iso9660->seenJoliet = seenJoliet;
1089		if (vd == &(iso9660->primary) && iso9660->seenRockridge
1090		    && iso9660->seenJoliet)
1091			/*
1092			 * If iso image has RockRidge and Joliet,
1093			 * we use RockRidge Extensions.
1094			 */
1095			iso9660->seenJoliet = 0;
1096		if (vd == &(iso9660->primary) && !iso9660->seenRockridge
1097		    && iso9660->seenJoliet) {
1098			/* Switch reading data from primary to joliet. */
1099			vd = &(iso9660->joliet);
1100			skipsize = LOGICAL_BLOCK_SIZE * vd->location;
1101			skipsize -= iso9660->current_position;
1102			skipsize = __archive_read_skip(a, skipsize);
1103			if (skipsize < 0)
1104				return ((int)skipsize);
1105			iso9660->current_position += skipsize;
1106
1107			block = __archive_read_ahead(a, vd->size, NULL);
1108			if (block == NULL) {
1109				archive_set_error(&a->archive,
1110				    ARCHIVE_ERRNO_MISC,
1111				    "Failed to read full block when scanning "
1112				    "ISO9660 directory list");
1113				return (ARCHIVE_FATAL);
1114			}
1115			seenJoliet = iso9660->seenJoliet;/* Save flag. */
1116			iso9660->seenJoliet = 0;
1117			file = parse_file_info(a, NULL, block);
1118			if (file == NULL)
1119				return (ARCHIVE_FATAL);
1120			iso9660->seenJoliet = seenJoliet;
1121		}
1122		/* Store the root directory in the pending list. */
1123		add_entry(iso9660, file);
1124		if (iso9660->seenRockridge) {
1125			a->archive.archive_format =
1126			    ARCHIVE_FORMAT_ISO9660_ROCKRIDGE;
1127			a->archive.archive_format_name =
1128			    "ISO9660 with Rockridge extensions";
1129		}
1130	}
1131
1132	/* Get the next entry that appears after the current offset. */
1133	r = next_entry_seek(a, iso9660, &file);
1134	if (r != ARCHIVE_OK)
1135		return (r);
1136
1137	iso9660->entry_bytes_remaining = file->size;
1138	iso9660->entry_sparse_offset = 0; /* Offset for sparse-file-aware clients. */
1139
1140	if (file->offset + file->size > iso9660->volume_size) {
1141		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1142		    "File is beyond end-of-media: %s", file->name.s);
1143		iso9660->entry_bytes_remaining = 0;
1144		iso9660->entry_sparse_offset = 0;
1145		return (ARCHIVE_WARN);
1146	}
1147
1148	/* Set up the entry structure with information about this entry. */
1149	archive_entry_set_mode(entry, file->mode);
1150	archive_entry_set_uid(entry, file->uid);
1151	archive_entry_set_gid(entry, file->gid);
1152	archive_entry_set_nlink(entry, file->nlinks);
1153	if (file->birthtime_is_set)
1154		archive_entry_set_birthtime(entry, file->birthtime, 0);
1155	else
1156		archive_entry_unset_birthtime(entry);
1157	archive_entry_set_mtime(entry, file->mtime, 0);
1158	archive_entry_set_ctime(entry, file->ctime, 0);
1159	archive_entry_set_atime(entry, file->atime, 0);
1160	/* N.B.: Rock Ridge supports 64-bit device numbers. */
1161	archive_entry_set_rdev(entry, (dev_t)file->rdev);
1162	archive_entry_set_size(entry, iso9660->entry_bytes_remaining);
1163	archive_string_empty(&iso9660->pathname);
1164	archive_entry_set_pathname(entry,
1165	    build_pathname(&iso9660->pathname, file));
1166	if (file->symlink.s != NULL)
1167		archive_entry_copy_symlink(entry, file->symlink.s);
1168
1169	/* Note: If the input isn't seekable, we can't rewind to
1170	 * return the same body again, so if the next entry refers to
1171	 * the same data, we have to return it as a hardlink to the
1172	 * original entry. */
1173	if (file->number != -1 &&
1174	    file->number == iso9660->previous_number) {
1175		archive_entry_set_hardlink(entry,
1176		    iso9660->previous_pathname.s);
1177		archive_entry_unset_size(entry);
1178		iso9660->entry_bytes_remaining = 0;
1179		iso9660->entry_sparse_offset = 0;
1180		return (ARCHIVE_OK);
1181	}
1182
1183	/* Except for the hardlink case above, if the offset of the
1184	 * next entry is before our current position, we can't seek
1185	 * backwards to extract it, so issue a warning.  Note that
1186	 * this can only happen if this entry was added to the heap
1187	 * after we passed this offset, that is, only if the directory
1188	 * mentioning this entry is later than the body of the entry.
1189	 * Such layouts are very unusual; most ISO9660 writers lay out
1190	 * and record all directory information first, then store
1191	 * all file bodies. */
1192	/* TODO: Someday, libarchive's I/O core will support optional
1193	 * seeking.  When that day comes, this code should attempt to
1194	 * seek and only return the error if the seek fails.  That
1195	 * will give us support for whacky ISO images that require
1196	 * seeking while retaining the ability to read almost all ISO
1197	 * images in a streaming fashion. */
1198	if ((file->mode & AE_IFMT) != AE_IFDIR &&
1199	    file->offset < iso9660->current_position) {
1200		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1201		    "Ignoring out-of-order file (%s) %jd < %jd",
1202		    iso9660->pathname.s,
1203		    (intmax_t)file->offset,
1204		    (intmax_t)iso9660->current_position);
1205		iso9660->entry_bytes_remaining = 0;
1206		iso9660->entry_sparse_offset = 0;
1207		return (ARCHIVE_WARN);
1208	}
1209
1210	/* Initialize zisofs variables. */
1211	iso9660->entry_zisofs.pz = file->pz;
1212	if (file->pz) {
1213#ifdef HAVE_ZLIB_H
1214		struct zisofs  *zisofs;
1215
1216		zisofs = &iso9660->entry_zisofs;
1217		zisofs->initialized = 0;
1218		zisofs->pz_log2_bs = file->pz_log2_bs;
1219		zisofs->pz_uncompressed_size = file->pz_uncompressed_size;
1220		zisofs->pz_offset = 0;
1221		zisofs->header_avail = 0;
1222		zisofs->header_passed = 0;
1223		zisofs->block_pointers_avail = 0;
1224#endif
1225		archive_entry_set_size(entry, file->pz_uncompressed_size);
1226	}
1227
1228	iso9660->previous_number = file->number;
1229	archive_strcpy(&iso9660->previous_pathname, iso9660->pathname.s);
1230
1231	/* Reset entry_bytes_remaining if the file is multi extent. */
1232	iso9660->entry_content = file->contents.first;
1233	if (iso9660->entry_content != NULL)
1234		iso9660->entry_bytes_remaining = iso9660->entry_content->size;
1235
1236	if (archive_entry_filetype(entry) == AE_IFDIR) {
1237		/* Overwrite nlinks by proper link number which is
1238		 * calculated from number of sub directories. */
1239		archive_entry_set_nlink(entry, 2 + file->subdirs);
1240		/* Directory data has been read completely. */
1241		iso9660->entry_bytes_remaining = 0;
1242		iso9660->entry_sparse_offset = 0;
1243	}
1244
1245	if (rd_r != ARCHIVE_OK)
1246		return (rd_r);
1247	return (ARCHIVE_OK);
1248}
1249
1250static int
1251archive_read_format_iso9660_read_data_skip(struct archive_read *a)
1252{
1253	/* Because read_next_header always does an explicit skip
1254	 * to the next entry, we don't need to do anything here. */
1255	(void)a; /* UNUSED */
1256	return (ARCHIVE_OK);
1257}
1258
1259#ifdef HAVE_ZLIB_H
1260
1261static int
1262zisofs_read_data(struct archive_read *a,
1263    const void **buff, size_t *size, off_t *offset)
1264{
1265	struct iso9660 *iso9660;
1266	struct zisofs  *zisofs;
1267	const unsigned char *p;
1268	size_t avail;
1269	ssize_t bytes_read;
1270	size_t uncompressed_size;
1271	int r;
1272
1273	iso9660 = (struct iso9660 *)(a->format->data);
1274	zisofs = &iso9660->entry_zisofs;
1275
1276	p = __archive_read_ahead(a, 1, &bytes_read);
1277	if (bytes_read <= 0) {
1278		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1279		    "Truncated zisofs file body");
1280		return (ARCHIVE_FATAL);
1281	}
1282	if (bytes_read > iso9660->entry_bytes_remaining)
1283		bytes_read = iso9660->entry_bytes_remaining;
1284	avail = bytes_read;
1285	uncompressed_size = 0;
1286
1287	if (!zisofs->initialized) {
1288		size_t ceil, xsize;
1289
1290		/* Allocate block pointers buffer. */
1291		ceil = (zisofs->pz_uncompressed_size +
1292			(1LL << zisofs->pz_log2_bs) - 1)
1293			>> zisofs->pz_log2_bs;
1294		xsize = (ceil + 1) * 4;
1295		if (zisofs->block_pointers_alloc < xsize) {
1296			size_t alloc;
1297
1298			if (zisofs->block_pointers != NULL)
1299				free(zisofs->block_pointers);
1300			alloc = ((xsize >> 10) + 1) << 10;
1301			zisofs->block_pointers = malloc(alloc);
1302			if (zisofs->block_pointers == NULL) {
1303				archive_set_error(&a->archive, ENOMEM,
1304				    "No memory for zisofs decompression");
1305				return (ARCHIVE_FATAL);
1306			}
1307			zisofs->block_pointers_alloc = alloc;
1308		}
1309		zisofs->block_pointers_size = xsize;
1310
1311		/* Allocate uncompressed data buffer. */
1312		xsize = 1UL << zisofs->pz_log2_bs;
1313		if (zisofs->uncompressed_buffer_size < xsize) {
1314			if (zisofs->uncompressed_buffer != NULL)
1315				free(zisofs->uncompressed_buffer);
1316			zisofs->uncompressed_buffer = malloc(xsize);
1317			if (zisofs->uncompressed_buffer == NULL) {
1318				archive_set_error(&a->archive, ENOMEM,
1319				    "No memory for zisofs decompression");
1320				return (ARCHIVE_FATAL);
1321			}
1322		}
1323		zisofs->uncompressed_buffer_size = xsize;
1324
1325		/*
1326		 * Read the file header, and check the magic code of zisofs.
1327		 */
1328		if (zisofs->header_avail < sizeof(zisofs->header)) {
1329			xsize = sizeof(zisofs->header) - zisofs->header_avail;
1330			if (avail < xsize)
1331				xsize = avail;
1332			memcpy(zisofs->header + zisofs->header_avail, p, xsize);
1333			zisofs->header_avail += xsize;
1334			avail -= xsize;
1335			p += xsize;
1336		}
1337		if (!zisofs->header_passed &&
1338		    zisofs->header_avail == sizeof(zisofs->header)) {
1339			int err = 0;
1340
1341			if (memcmp(zisofs->header, zisofs_magic,
1342			    sizeof(zisofs_magic)) != 0)
1343				err = 1;
1344			if (archive_le32dec(zisofs->header + 8)
1345			    != zisofs->pz_uncompressed_size)
1346				err = 1;
1347			if (zisofs->header[12] != 4)
1348				err = 1;
1349			if (zisofs->header[13] != zisofs->pz_log2_bs)
1350				err = 1;
1351			if (err) {
1352				archive_set_error(&a->archive,
1353				    ARCHIVE_ERRNO_FILE_FORMAT,
1354				    "Illegal zisofs file body");
1355				return (ARCHIVE_FATAL);
1356			}
1357			zisofs->header_passed = 1;
1358		}
1359		/*
1360		 * Read block pointers.
1361		 */
1362		if (zisofs->header_passed &&
1363		    zisofs->block_pointers_avail < zisofs->block_pointers_size) {
1364			xsize = zisofs->block_pointers_size
1365			    - zisofs->block_pointers_avail;
1366			if (avail < xsize)
1367				xsize = avail;
1368			memcpy(zisofs->block_pointers
1369			    + zisofs->block_pointers_avail, p, xsize);
1370			zisofs->block_pointers_avail += xsize;
1371			avail -= xsize;
1372			p += xsize;
1373		    	if (zisofs->block_pointers_avail
1374			    == zisofs->block_pointers_size) {
1375				/* We've got all block pointers and initialize
1376				 * related variables.	*/
1377				zisofs->block_off = 0;
1378				zisofs->block_avail = 0;
1379				/* Complete a initialization */
1380				zisofs->initialized = 1;
1381			}
1382		}
1383
1384		if (!zisofs->initialized)
1385			goto next_data; /* We need more datas. */
1386	}
1387
1388	/*
1389	 * Get block offsets from block pointers.
1390	 */
1391	if (zisofs->block_avail == 0) {
1392		uint32_t bst, bed;
1393
1394		if (zisofs->block_off + 4 >= zisofs->block_pointers_size) {
1395			/* There isn't a pair of offsets. */
1396			archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1397			    "Illegal zisofs block pointers");
1398			return (ARCHIVE_FATAL);
1399		}
1400		bst = archive_le32dec(zisofs->block_pointers + zisofs->block_off);
1401		if (bst != zisofs->pz_offset + (bytes_read - avail)) {
1402			/* TODO: Should we seek offset of current file by bst ? */
1403			archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1404			    "Illegal zisofs block pointers(cannot seek)");
1405			return (ARCHIVE_FATAL);
1406		}
1407		bed = archive_le32dec(
1408		    zisofs->block_pointers + zisofs->block_off + 4);
1409		if (bed < bst) {
1410			archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1411			    "Illegal zisofs block pointers");
1412			return (ARCHIVE_FATAL);
1413		}
1414		zisofs->block_avail = bed - bst;
1415		zisofs->block_off += 4;
1416
1417		/* Initialize compression library for new block. */
1418		if (zisofs->stream_valid)
1419			r = inflateReset(&zisofs->stream);
1420		else
1421			r = inflateInit(&zisofs->stream);
1422		if (r != Z_OK) {
1423			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1424			    "Can't initialize zisofs decompression.");
1425			return (ARCHIVE_FATAL);
1426		}
1427		zisofs->stream_valid = 1;
1428		zisofs->stream.total_in = 0;
1429		zisofs->stream.total_out = 0;
1430	}
1431
1432	/*
1433	 * Make uncompressed datas.
1434	 */
1435	if (zisofs->block_avail == 0) {
1436		memset(zisofs->uncompressed_buffer, 0,
1437		    zisofs->uncompressed_buffer_size);
1438		uncompressed_size = zisofs->uncompressed_buffer_size;
1439	} else {
1440		zisofs->stream.next_in = (Bytef *)(uintptr_t)(const void *)p;
1441		if (avail > zisofs->block_avail)
1442			zisofs->stream.avail_in = zisofs->block_avail;
1443		else
1444			zisofs->stream.avail_in = avail;
1445		zisofs->stream.next_out = zisofs->uncompressed_buffer;
1446		zisofs->stream.avail_out = zisofs->uncompressed_buffer_size;
1447
1448		r = inflate(&zisofs->stream, 0);
1449		switch (r) {
1450		case Z_OK: /* Decompressor made some progress.*/
1451		case Z_STREAM_END: /* Found end of stream. */
1452			break;
1453		default:
1454			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1455			    "zisofs decompression failed (%d)", r);
1456			return (ARCHIVE_FATAL);
1457		}
1458		uncompressed_size =
1459		    zisofs->uncompressed_buffer_size - zisofs->stream.avail_out;
1460		avail -= zisofs->stream.next_in - p;
1461		zisofs->block_avail -= zisofs->stream.next_in - p;
1462	}
1463next_data:
1464	bytes_read -= avail;
1465	*buff = zisofs->uncompressed_buffer;
1466	*size = uncompressed_size;
1467	*offset = iso9660->entry_sparse_offset;
1468	iso9660->entry_sparse_offset += uncompressed_size;
1469	iso9660->entry_bytes_remaining -= bytes_read;
1470	iso9660->current_position += bytes_read;
1471	zisofs->pz_offset += bytes_read;
1472	__archive_read_consume(a, bytes_read);
1473
1474	return (ARCHIVE_OK);
1475}
1476
1477#else /* HAVE_ZLIB_H */
1478
1479static int
1480zisofs_read_data(struct archive_read *a,
1481    const void **buff, size_t *size, off_t *offset)
1482{
1483
1484	(void)buff;/* UNUSED */
1485	(void)size;/* UNUSED */
1486	(void)offset;/* UNUSED */
1487	archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1488	    "zisofs is not supported on this platform.");
1489	return (ARCHIVE_FAILED);
1490}
1491
1492#endif /* HAVE_ZLIB_H */
1493
1494static int
1495archive_read_format_iso9660_read_data(struct archive_read *a,
1496    const void **buff, size_t *size, off_t *offset)
1497{
1498	ssize_t bytes_read;
1499	struct iso9660 *iso9660;
1500
1501	iso9660 = (struct iso9660 *)(a->format->data);
1502	if (iso9660->entry_bytes_remaining <= 0) {
1503		if (iso9660->entry_content != NULL)
1504			iso9660->entry_content = iso9660->entry_content->next;
1505		if (iso9660->entry_content == NULL) {
1506			*buff = NULL;
1507			*size = 0;
1508			*offset = iso9660->entry_sparse_offset;
1509			return (ARCHIVE_EOF);
1510		}
1511		/* Seek forward to the start of the entry. */
1512		if (iso9660->current_position < iso9660->entry_content->offset) {
1513			int64_t step;
1514
1515			step = iso9660->entry_content->offset -
1516			    iso9660->current_position;
1517			step = __archive_read_skip(a, step);
1518			if (step < 0)
1519				return ((int)step);
1520			iso9660->current_position =
1521			    iso9660->entry_content->offset;
1522		}
1523		if (iso9660->entry_content->offset < iso9660->current_position) {
1524			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1525			    "Ignoring out-of-order file (%s) %jd < %jd",
1526			    iso9660->pathname.s,
1527			    (intmax_t)iso9660->entry_content->offset,
1528			    (intmax_t)iso9660->current_position);
1529			*buff = NULL;
1530			*size = 0;
1531			*offset = iso9660->entry_sparse_offset;
1532			return (ARCHIVE_WARN);
1533		}
1534		iso9660->entry_bytes_remaining = iso9660->entry_content->size;
1535	}
1536	if (iso9660->entry_zisofs.pz)
1537		return (zisofs_read_data(a, buff, size, offset));
1538
1539	*buff = __archive_read_ahead(a, 1, &bytes_read);
1540	if (bytes_read == 0)
1541		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1542		    "Truncated input file");
1543	if (*buff == NULL)
1544		return (ARCHIVE_FATAL);
1545	if (bytes_read > iso9660->entry_bytes_remaining)
1546		bytes_read = iso9660->entry_bytes_remaining;
1547	*size = bytes_read;
1548	*offset = iso9660->entry_sparse_offset;
1549	iso9660->entry_sparse_offset += bytes_read;
1550	iso9660->entry_bytes_remaining -= bytes_read;
1551	iso9660->current_position += bytes_read;
1552	__archive_read_consume(a, bytes_read);
1553	return (ARCHIVE_OK);
1554}
1555
1556static int
1557archive_read_format_iso9660_cleanup(struct archive_read *a)
1558{
1559	struct iso9660 *iso9660;
1560	int r = ARCHIVE_OK;
1561
1562	iso9660 = (struct iso9660 *)(a->format->data);
1563	release_files(iso9660);
1564	free(iso9660->read_ce_req.reqs);
1565	archive_string_free(&iso9660->pathname);
1566	archive_string_free(&iso9660->previous_pathname);
1567	if (iso9660->pending_files.files)
1568		free(iso9660->pending_files.files);
1569#ifdef HAVE_ZLIB_H
1570	free(iso9660->entry_zisofs.uncompressed_buffer);
1571	free(iso9660->entry_zisofs.block_pointers);
1572	if (iso9660->entry_zisofs.stream_valid) {
1573		if (inflateEnd(&iso9660->entry_zisofs.stream) != Z_OK) {
1574			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1575			    "Failed to clean up zlib decompressor");
1576			r = ARCHIVE_FATAL;
1577		}
1578	}
1579#endif
1580	free(iso9660);
1581	(a->format->data) = NULL;
1582	return (r);
1583}
1584
1585/*
1586 * This routine parses a single ISO directory record, makes sense
1587 * of any extensions, and stores the result in memory.
1588 */
1589static struct file_info *
1590parse_file_info(struct archive_read *a, struct file_info *parent,
1591    const unsigned char *isodirrec)
1592{
1593	struct iso9660 *iso9660;
1594	struct file_info *file;
1595	size_t name_len;
1596	const unsigned char *rr_start, *rr_end;
1597	const unsigned char *p;
1598	size_t dr_len;
1599	uint64_t fsize;
1600	int32_t location;
1601	int flags;
1602
1603	iso9660 = (struct iso9660 *)(a->format->data);
1604
1605	dr_len = (size_t)isodirrec[DR_length_offset];
1606	name_len = (size_t)isodirrec[DR_name_len_offset];
1607	location = archive_le32dec(isodirrec + DR_extent_offset);
1608	fsize = toi(isodirrec + DR_size_offset, DR_size_size);
1609	/* Sanity check that dr_len needs at least 34. */
1610	if (dr_len < 34) {
1611		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1612		    "Invalid length of directory record");
1613		return (NULL);
1614	}
1615	/* Sanity check that name_len doesn't exceed dr_len. */
1616	if (dr_len - 33 < name_len || name_len == 0) {
1617		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1618		    "Invalid length of file identifier");
1619		return (NULL);
1620	}
1621	/* Sanity check that location doesn't exceed volume block.
1622	 * Don't check lower limit of location; it's possibility
1623	 * the location has negative value when file type is symbolic
1624	 * link or file size is zero. As far as I know latest mkisofs
1625	 * do that.
1626	 */
1627	if (location > 0 &&
1628	    (location + ((fsize + iso9660->logical_block_size -1)
1629	       / iso9660->logical_block_size)) > iso9660->volume_block) {
1630		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1631		    "Invalid location of extent of file");
1632		return (NULL);
1633	}
1634
1635	/* Create a new file entry and copy data from the ISO dir record. */
1636	file = (struct file_info *)malloc(sizeof(*file));
1637	if (file == NULL) {
1638		archive_set_error(&a->archive, ENOMEM,
1639		    "No memory for file entry");
1640		return (NULL);
1641	}
1642	memset(file, 0, sizeof(*file));
1643	file->parent = parent;
1644	file->offset = iso9660->logical_block_size * (uint64_t)location;
1645	file->size = fsize;
1646	file->mtime = isodate7(isodirrec + DR_date_offset);
1647	file->ctime = file->atime = file->mtime;
1648	file->rede_files.first = NULL;
1649	file->rede_files.last = &(file->rede_files.first);
1650
1651	p = isodirrec + DR_name_offset;
1652	/* Rockridge extensions (if any) follow name.  Compute this
1653	 * before fidgeting the name_len below. */
1654	rr_start = p + name_len + (name_len & 1 ? 0 : 1);
1655	rr_end = isodirrec + dr_len;
1656
1657	if (iso9660->seenJoliet) {
1658		/* Joliet names are max 64 chars (128 bytes) according to spec,
1659		 * but genisoimage/mkisofs allows recording longer Joliet
1660		 * names which are 103 UCS2 characters(206 bytes) by their
1661		 * option '-joliet-long'.
1662		 */
1663		wchar_t wbuff[103+1], *wp;
1664		const unsigned char *c;
1665
1666		if (name_len > 206)
1667			name_len = 206;
1668		/* convert BE UTF-16 to wchar_t */
1669		for (c = p, wp = wbuff;
1670				c < (p + name_len) &&
1671				wp < (wbuff + sizeof(wbuff)/sizeof(*wbuff) - 1);
1672				c += 2) {
1673			*wp++ = (((255 & (int)c[0]) << 8) | (255 & (int)c[1]));
1674		}
1675		*wp = L'\0';
1676
1677#if 0 /* untested code, is it at all useful on Joliet? */
1678		/* trim trailing first version and dot from filename.
1679		 *
1680		 * Remember we where in UTF-16BE land!
1681		 * SEPARATOR 1 (.) and SEPARATOR 2 (;) are both
1682		 * 16 bits big endian characters on Joliet.
1683		 *
1684		 * TODO: sanitize filename?
1685		 *       Joliet allows any UCS-2 char except:
1686		 *       *, /, :, ;, ? and \.
1687		 */
1688		/* Chop off trailing ';1' from files. */
1689		if (*(wp-2) == ';' && *(wp-1) == '1') {
1690			wp-=2;
1691			*wp = L'\0';
1692		}
1693
1694		/* Chop off trailing '.' from filenames. */
1695		if (*(wp-1) == '.')
1696			*(--wp) = L'\0';
1697#endif
1698
1699		/* store the result in the file name field. */
1700		archive_strappend_w_utf8(&file->name, wbuff);
1701	} else {
1702		/* Chop off trailing ';1' from files. */
1703		if (name_len > 2 && p[name_len - 2] == ';' &&
1704				p[name_len - 1] == '1')
1705			name_len -= 2;
1706		/* Chop off trailing '.' from filenames. */
1707		if (name_len > 1 && p[name_len - 1] == '.')
1708			--name_len;
1709
1710		archive_strncpy(&file->name, (const char *)p, name_len);
1711	}
1712
1713	flags = isodirrec[DR_flags_offset];
1714	if (flags & 0x02)
1715		file->mode = AE_IFDIR | 0700;
1716	else
1717		file->mode = AE_IFREG | 0400;
1718	if (flags & 0x80)
1719		file->multi_extent = 1;
1720	else
1721		file->multi_extent = 0;
1722	/*
1723	 * Use location for file number.
1724	 * File number is treated as inode number to find out harlink
1725	 * target. If Rockridge extensions is being used, file number
1726	 * will be overwritten by FILE SERIAL NUMBER of RRIP "PX"
1727	 * extension.
1728	 * NOTE: Old mkisofs did not record that FILE SERIAL NUMBER
1729	 * in ISO images.
1730	 */
1731	if (file->size == 0 && location >= 0)
1732		/* If file->size is zero, its location points wrong place.
1733		 * Dot not use it for file number.
1734		 * When location has negative value, it can be used
1735		 * for file number.
1736		 */
1737		file->number = -1;
1738	else
1739		file->number = (int64_t)(uint32_t)location;
1740
1741	/* Rockridge extensions overwrite information from above. */
1742	if (iso9660->opt_support_rockridge) {
1743		if (parent == NULL && rr_end - rr_start >= 7) {
1744			p = rr_start;
1745			if (p[0] == 'S' && p[1] == 'P'
1746			    && p[2] == 7 && p[3] == 1
1747			    && p[4] == 0xBE && p[5] == 0xEF) {
1748				/*
1749				 * SP extension stores the suspOffset
1750				 * (Number of bytes to skip between
1751				 * filename and SUSP records.)
1752				 * It is mandatory by the SUSP standard
1753				 * (IEEE 1281).
1754				 *
1755				 * It allows SUSP to coexist with
1756				 * non-SUSP uses of the System
1757				 * Use Area by placing non-SUSP data
1758				 * before SUSP data.
1759				 *
1760				 * SP extension must be in the root
1761				 * directory entry, disable all SUSP
1762				 * processing if not found.
1763				 */
1764				iso9660->suspOffset = p[6];
1765				iso9660->seenSUSP = 1;
1766				rr_start += 7;
1767			}
1768		}
1769		if (iso9660->seenSUSP) {
1770			int r;
1771
1772			file->name_continues = 0;
1773			file->symlink_continues = 0;
1774			rr_start += iso9660->suspOffset;
1775			r = parse_rockridge(a, file, rr_start, rr_end);
1776			if (r != ARCHIVE_OK) {
1777				free(file);
1778				return (NULL);
1779			}
1780		} else
1781			/* If there isn't SUSP, disable parsing
1782			 * rock ridge extensions. */
1783			iso9660->opt_support_rockridge = 0;
1784	}
1785
1786	file->nlinks = 1;/* Reset nlink. we'll calculate it later. */
1787	/* Tell file's parent how many children that parent has. */
1788	if (parent != NULL && (flags & 0x02))
1789		parent->subdirs++;
1790
1791	if (iso9660->seenRockridge) {
1792		if (parent != NULL && parent->parent == NULL &&
1793		    (flags & 0x02) && iso9660->rr_moved == NULL &&
1794		    (strcmp(file->name.s, "rr_moved") == 0 ||
1795		     strcmp(file->name.s, ".rr_moved") == 0)) {
1796			iso9660->rr_moved = file;
1797			file->rr_moved = 1;
1798			file->rr_moved_has_re_only = 1;
1799			file->re = 0;
1800			parent->subdirs--;
1801		} else if (file->re) {
1802			/* This file's parent is not rr_moved, clear invalid
1803			 * "RE" mark. */
1804			if (parent == NULL || parent->rr_moved == 0)
1805				file->re = 0;
1806			else if ((flags & 0x02) == 0) {
1807				file->rr_moved_has_re_only = 0;
1808				file->re = 0;
1809			}
1810		} else if (parent != NULL && parent->rr_moved)
1811			file->rr_moved_has_re_only = 0;
1812		else if (parent != NULL && (flags & 0x02) &&
1813		    (parent->re || parent->re_descendant))
1814			file->re_descendant = 1;
1815		if (file->cl_offset != 0) {
1816			parent->subdirs++;
1817			/* Overwrite an offset and a number of this "CL" entry
1818			 * to appear before other dirs. "+1" to those is to
1819			 * make sure to appear after "RE" entry which this
1820			 * "CL" entry should be connected with. */
1821			file->offset = file->number = file->cl_offset + 1;
1822		}
1823	}
1824
1825#if DEBUG
1826	/* DEBUGGING: Warn about attributes I don't yet fully support. */
1827	if ((flags & ~0x02) != 0) {
1828		fprintf(stderr, "\n ** Unrecognized flag: ");
1829		dump_isodirrec(stderr, isodirrec);
1830		fprintf(stderr, "\n");
1831	} else if (toi(isodirrec + DR_volume_sequence_number_offset, 2) != 1) {
1832		fprintf(stderr, "\n ** Unrecognized sequence number: ");
1833		dump_isodirrec(stderr, isodirrec);
1834		fprintf(stderr, "\n");
1835	} else if (*(isodirrec + DR_file_unit_size_offset) != 0) {
1836		fprintf(stderr, "\n ** Unexpected file unit size: ");
1837		dump_isodirrec(stderr, isodirrec);
1838		fprintf(stderr, "\n");
1839	} else if (*(isodirrec + DR_interleave_offset) != 0) {
1840		fprintf(stderr, "\n ** Unexpected interleave: ");
1841		dump_isodirrec(stderr, isodirrec);
1842		fprintf(stderr, "\n");
1843	} else if (*(isodirrec + DR_ext_attr_length_offset) != 0) {
1844		fprintf(stderr, "\n ** Unexpected extended attribute length: ");
1845		dump_isodirrec(stderr, isodirrec);
1846		fprintf(stderr, "\n");
1847	}
1848#endif
1849	register_file(iso9660, file);
1850	return (file);
1851}
1852
1853static int
1854parse_rockridge(struct archive_read *a, struct file_info *file,
1855    const unsigned char *p, const unsigned char *end)
1856{
1857	struct iso9660 *iso9660;
1858
1859	iso9660 = (struct iso9660 *)(a->format->data);
1860
1861	while (p + 4 <= end  /* Enough space for another entry. */
1862	    && p[0] >= 'A' && p[0] <= 'Z' /* Sanity-check 1st char of name. */
1863	    && p[1] >= 'A' && p[1] <= 'Z' /* Sanity-check 2nd char of name. */
1864	    && p[2] >= 4 /* Sanity-check length. */
1865	    && p + p[2] <= end) { /* Sanity-check length. */
1866		const unsigned char *data = p + 4;
1867		int data_length = p[2] - 4;
1868		int version = p[3];
1869
1870		/*
1871		 * Yes, each 'if' here does test p[0] again.
1872		 * Otherwise, the fall-through handling to catch
1873		 * unsupported extensions doesn't work.
1874		 */
1875		switch(p[0]) {
1876		case 'C':
1877			if (p[0] == 'C' && p[1] == 'E') {
1878				if (version == 1 && data_length == 24) {
1879					/*
1880					 * CE extension comprises:
1881					 *   8 byte sector containing extension
1882					 *   8 byte offset w/in above sector
1883					 *   8 byte length of continuation
1884					 */
1885					int32_t location =
1886					    archive_le32dec(data);
1887					file->ce_offset =
1888					    archive_le32dec(data+8);
1889					file->ce_size =
1890					    archive_le32dec(data+16);
1891					if (register_CE(a, location, file)
1892					    != ARCHIVE_OK)
1893						return (ARCHIVE_FATAL);
1894				}
1895				break;
1896			}
1897			if (p[0] == 'C' && p[1] == 'L') {
1898				if (version == 1 && data_length == 8) {
1899					file->cl_offset = (uint64_t)
1900					    iso9660->logical_block_size *
1901					    (uint64_t)archive_le32dec(data);
1902					iso9660->seenRockridge = 1;
1903				}
1904				break;
1905			}
1906			/* FALLTHROUGH */
1907		case 'N':
1908			if (p[0] == 'N' && p[1] == 'M') {
1909				if (version == 1) {
1910					parse_rockridge_NM1(file,
1911					    data, data_length);
1912					iso9660->seenRockridge = 1;
1913				}
1914				break;
1915			}
1916			/* FALLTHROUGH */
1917		case 'P':
1918			if (p[0] == 'P' && p[1] == 'D') {
1919				/*
1920				 * PD extension is padding;
1921				 * contents are always ignored.
1922				 */
1923				break;
1924			}
1925			if (p[0] == 'P' && p[1] == 'N') {
1926				if (version == 1 && data_length == 16) {
1927					file->rdev = toi(data,4);
1928					file->rdev <<= 32;
1929					file->rdev |= toi(data + 8, 4);
1930					iso9660->seenRockridge = 1;
1931				}
1932				break;
1933			}
1934			if (p[0] == 'P' && p[1] == 'X') {
1935				/*
1936				 * PX extension comprises:
1937				 *   8 bytes for mode,
1938				 *   8 bytes for nlinks,
1939				 *   8 bytes for uid,
1940				 *   8 bytes for gid,
1941				 *   8 bytes for inode.
1942				 */
1943				if (version == 1) {
1944					if (data_length >= 8)
1945						file->mode
1946						    = toi(data, 4);
1947					if (data_length >= 16)
1948						file->nlinks
1949						    = toi(data + 8, 4);
1950					if (data_length >= 24)
1951						file->uid
1952						    = toi(data + 16, 4);
1953					if (data_length >= 32)
1954						file->gid
1955						    = toi(data + 24, 4);
1956					if (data_length >= 40)
1957						file->number
1958						    = toi(data + 32, 4);
1959					iso9660->seenRockridge = 1;
1960				}
1961				break;
1962			}
1963			/* FALLTHROUGH */
1964		case 'R':
1965			if (p[0] == 'R' && p[1] == 'E' && version == 1) {
1966				file->re = 1;
1967				iso9660->seenRockridge = 1;
1968				break;
1969			}
1970			if (p[0] == 'R' && p[1] == 'R' && version == 1) {
1971				/*
1972				 * RR extension comprises:
1973				 *    one byte flag value
1974				 * This extension is obsolete,
1975				 * so contents are always ignored.
1976				 */
1977				break;
1978			}
1979			/* FALLTHROUGH */
1980		case 'S':
1981			if (p[0] == 'S' && p[1] == 'L') {
1982				if (version == 1) {
1983					parse_rockridge_SL1(file,
1984					    data, data_length);
1985					iso9660->seenRockridge = 1;
1986				}
1987				break;
1988			}
1989			if (p[0] == 'S' && p[1] == 'T'
1990			    && data_length == 0 && version == 1) {
1991				/*
1992				 * ST extension marks end of this
1993				 * block of SUSP entries.
1994				 *
1995				 * It allows SUSP to coexist with
1996				 * non-SUSP uses of the System
1997				 * Use Area by placing non-SUSP data
1998				 * after SUSP data.
1999				 */
2000				iso9660->seenSUSP = 0;
2001				iso9660->seenRockridge = 0;
2002				return (ARCHIVE_OK);
2003			}
2004		case 'T':
2005			if (p[0] == 'T' && p[1] == 'F') {
2006				if (version == 1) {
2007					parse_rockridge_TF1(file,
2008					    data, data_length);
2009					iso9660->seenRockridge = 1;
2010				}
2011				break;
2012			}
2013			/* FALLTHROUGH */
2014		case 'Z':
2015			if (p[0] == 'Z' && p[1] == 'F') {
2016				if (version == 1)
2017					parse_rockridge_ZF1(file,
2018					    data, data_length);
2019				break;
2020			}
2021			/* FALLTHROUGH */
2022		default:
2023			/* The FALLTHROUGHs above leave us here for
2024			 * any unsupported extension. */
2025			break;
2026		}
2027
2028
2029
2030		p += p[2];
2031	}
2032	return (ARCHIVE_OK);
2033}
2034
2035static int
2036register_CE(struct archive_read *a, int32_t location,
2037    struct file_info *file)
2038{
2039	struct iso9660 *iso9660;
2040	struct read_ce_queue *heap;
2041	struct read_ce_req *p;
2042	uint64_t offset, parent_offset;
2043	int hole, parent;
2044
2045	iso9660 = (struct iso9660 *)(a->format->data);
2046	offset = ((uint64_t)location) * (uint64_t)iso9660->logical_block_size;
2047	if (((file->mode & AE_IFMT) == AE_IFREG &&
2048	    offset >= file->offset) ||
2049	    offset < iso9660->current_position) {
2050		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
2051		    "Invalid location in SUSP \"CE\" extension");
2052		return (ARCHIVE_FATAL);
2053	}
2054
2055	/* Expand our CE list as necessary. */
2056	heap = &(iso9660->read_ce_req);
2057	if (heap->cnt >= heap->allocated) {
2058		int new_size;
2059
2060		if (heap->allocated < 16)
2061			new_size = 16;
2062		else
2063			new_size = heap->allocated * 2;
2064		/* Overflow might keep us from growing the list. */
2065		if (new_size <= heap->allocated)
2066			__archive_errx(1, "Out of memory");
2067		p = malloc(new_size * sizeof(p[0]));
2068		if (p == NULL)
2069			__archive_errx(1, "Out of memory");
2070		if (heap->reqs != NULL) {
2071			memcpy(p, heap->reqs, heap->cnt * sizeof(*p));
2072			free(heap->reqs);
2073		}
2074		heap->reqs = p;
2075		heap->allocated = new_size;
2076	}
2077
2078	/*
2079	 * Start with hole at end, walk it up tree to find insertion point.
2080	 */
2081	hole = heap->cnt++;
2082	while (hole > 0) {
2083		parent = (hole - 1)/2;
2084		parent_offset = heap->reqs[parent].offset;
2085		if (offset >= parent_offset) {
2086			heap->reqs[hole].offset = offset;
2087			heap->reqs[hole].file = file;
2088			return (ARCHIVE_OK);
2089		}
2090		// Move parent into hole <==> move hole up tree.
2091		heap->reqs[hole] = heap->reqs[parent];
2092		hole = parent;
2093	}
2094	heap->reqs[0].offset = offset;
2095	heap->reqs[0].file = file;
2096	return (ARCHIVE_OK);
2097}
2098
2099static void
2100next_CE(struct read_ce_queue *heap)
2101{
2102	uint64_t a_offset, b_offset, c_offset;
2103	int a, b, c;
2104	struct read_ce_req tmp;
2105
2106	if (heap->cnt < 1)
2107		return;
2108
2109	/*
2110	 * Move the last item in the heap to the root of the tree
2111	 */
2112	heap->reqs[0] = heap->reqs[--(heap->cnt)];
2113
2114	/*
2115	 * Rebalance the heap.
2116	 */
2117	a = 0; // Starting element and its offset
2118	a_offset = heap->reqs[a].offset;
2119	for (;;) {
2120		b = a + a + 1; // First child
2121		if (b >= heap->cnt)
2122			return;
2123		b_offset = heap->reqs[b].offset;
2124		c = b + 1; // Use second child if it is smaller.
2125		if (c < heap->cnt) {
2126			c_offset = heap->reqs[c].offset;
2127			if (c_offset < b_offset) {
2128				b = c;
2129				b_offset = c_offset;
2130			}
2131		}
2132		if (a_offset <= b_offset)
2133			return;
2134		tmp = heap->reqs[a];
2135		heap->reqs[a] = heap->reqs[b];
2136		heap->reqs[b] = tmp;
2137		a = b;
2138	}
2139}
2140
2141
2142static int
2143read_CE(struct archive_read *a, struct iso9660 *iso9660)
2144{
2145	struct read_ce_queue *heap;
2146	const unsigned char *b, *p, *end;
2147	struct file_info *file;
2148	size_t step;
2149	int r;
2150
2151	/* Read data which RRIP "CE" extension points. */
2152	heap = &(iso9660->read_ce_req);
2153	step = iso9660->logical_block_size;
2154	while (heap->cnt &&
2155	    heap->reqs[0].offset == iso9660->current_position) {
2156		b = __archive_read_ahead(a, step, NULL);
2157		if (b == NULL) {
2158			archive_set_error(&a->archive,
2159			    ARCHIVE_ERRNO_MISC,
2160			    "Failed to read full block when scanning "
2161			    "ISO9660 directory list");
2162			return (ARCHIVE_FATAL);
2163		}
2164		do {
2165			file = heap->reqs[0].file;
2166			p = b + file->ce_offset;
2167			end = p + file->ce_size;
2168			next_CE(heap);
2169			r = parse_rockridge(a, file, p, end);
2170			if (r != ARCHIVE_OK)
2171				return (ARCHIVE_FATAL);
2172		} while (heap->cnt &&
2173		    heap->reqs[0].offset == iso9660->current_position);
2174		/* NOTE: Do not move this consume's code to fron of
2175		 * do-while loop. Registration of nested CE extension
2176		 * might cause error because of current position. */
2177		__archive_read_consume(a, step);
2178		iso9660->current_position += step;
2179	}
2180	return (ARCHIVE_OK);
2181}
2182
2183static void
2184parse_rockridge_NM1(struct file_info *file,
2185		    const unsigned char *data, int data_length)
2186{
2187	if (!file->name_continues)
2188		archive_string_empty(&file->name);
2189	file->name_continues = 0;
2190	if (data_length < 1)
2191		return;
2192	/*
2193	 * NM version 1 extension comprises:
2194	 *   1 byte flag, value is one of:
2195	 *     = 0: remainder is name
2196	 *     = 1: remainder is name, next NM entry continues name
2197	 *     = 2: "."
2198	 *     = 4: ".."
2199	 *     = 32: Implementation specific
2200	 *     All other values are reserved.
2201	 */
2202	switch(data[0]) {
2203	case 0:
2204		if (data_length < 2)
2205			return;
2206		archive_strncat(&file->name, (const char *)data + 1, data_length - 1);
2207		break;
2208	case 1:
2209		if (data_length < 2)
2210			return;
2211		archive_strncat(&file->name, (const char *)data + 1, data_length - 1);
2212		file->name_continues = 1;
2213		break;
2214	case 2:
2215		archive_strcat(&file->name, ".");
2216		break;
2217	case 4:
2218		archive_strcat(&file->name, "..");
2219		break;
2220	default:
2221		return;
2222	}
2223
2224}
2225
2226static void
2227parse_rockridge_TF1(struct file_info *file, const unsigned char *data,
2228    int data_length)
2229{
2230	char flag;
2231	/*
2232	 * TF extension comprises:
2233	 *   one byte flag
2234	 *   create time (optional)
2235	 *   modify time (optional)
2236	 *   access time (optional)
2237	 *   attribute time (optional)
2238	 *  Time format and presence of fields
2239	 *  is controlled by flag bits.
2240	 */
2241	if (data_length < 1)
2242		return;
2243	flag = data[0];
2244	++data;
2245	--data_length;
2246	if (flag & 0x80) {
2247		/* Use 17-byte time format. */
2248		if ((flag & 1) && data_length >= 17) {
2249			/* Create time. */
2250			file->birthtime_is_set = 1;
2251			file->birthtime = isodate17(data);
2252			data += 17;
2253			data_length -= 17;
2254		}
2255		if ((flag & 2) && data_length >= 17) {
2256			/* Modify time. */
2257			file->mtime = isodate17(data);
2258			data += 17;
2259			data_length -= 17;
2260		}
2261		if ((flag & 4) && data_length >= 17) {
2262			/* Access time. */
2263			file->atime = isodate17(data);
2264			data += 17;
2265			data_length -= 17;
2266		}
2267		if ((flag & 8) && data_length >= 17) {
2268			/* Attribute change time. */
2269			file->ctime = isodate17(data);
2270		}
2271	} else {
2272		/* Use 7-byte time format. */
2273		if ((flag & 1) && data_length >= 7) {
2274			/* Create time. */
2275			file->birthtime_is_set = 1;
2276			file->birthtime = isodate7(data);
2277			data += 7;
2278			data_length -= 7;
2279		}
2280		if ((flag & 2) && data_length >= 7) {
2281			/* Modify time. */
2282			file->mtime = isodate7(data);
2283			data += 7;
2284			data_length -= 7;
2285		}
2286		if ((flag & 4) && data_length >= 7) {
2287			/* Access time. */
2288			file->atime = isodate7(data);
2289			data += 7;
2290			data_length -= 7;
2291		}
2292		if ((flag & 8) && data_length >= 7) {
2293			/* Attribute change time. */
2294			file->ctime = isodate7(data);
2295		}
2296	}
2297}
2298
2299static void
2300parse_rockridge_SL1(struct file_info *file, const unsigned char *data,
2301    int data_length)
2302{
2303	const char *separator = "";
2304
2305	if (!file->symlink_continues || file->symlink.length < 1)
2306		archive_string_empty(&file->symlink);
2307	else if (!file->symlink_continues &&
2308	    file->symlink.s[file->symlink.length - 1] != '/')
2309		separator = "/";
2310	file->symlink_continues = 0;
2311
2312	/*
2313	 * Defined flag values:
2314	 *  0: This is the last SL record for this symbolic link
2315	 *  1: this symbolic link field continues in next SL entry
2316	 *  All other values are reserved.
2317	 */
2318	if (data_length < 1)
2319		return;
2320	switch(*data) {
2321	case 0:
2322		break;
2323	case 1:
2324		file->symlink_continues = 1;
2325		break;
2326	default:
2327		return;
2328	}
2329	++data;  /* Skip flag byte. */
2330	--data_length;
2331
2332	/*
2333	 * SL extension body stores "components".
2334	 * Basically, this is a complicated way of storing
2335	 * a POSIX path.  It also interferes with using
2336	 * symlinks for storing non-path data. <sigh>
2337	 *
2338	 * Each component is 2 bytes (flag and length)
2339	 * possibly followed by name data.
2340	 */
2341	while (data_length >= 2) {
2342		unsigned char flag = *data++;
2343		unsigned char nlen = *data++;
2344		data_length -= 2;
2345
2346		archive_strcat(&file->symlink, separator);
2347		separator = "/";
2348
2349		switch(flag) {
2350		case 0: /* Usual case, this is text. */
2351			if (data_length < nlen)
2352				return;
2353			archive_strncat(&file->symlink,
2354			    (const char *)data, nlen);
2355			break;
2356		case 0x01: /* Text continues in next component. */
2357			if (data_length < nlen)
2358				return;
2359			archive_strncat(&file->symlink,
2360			    (const char *)data, nlen);
2361			separator = "";
2362			break;
2363		case 0x02: /* Current dir. */
2364			archive_strcat(&file->symlink, ".");
2365			break;
2366		case 0x04: /* Parent dir. */
2367			archive_strcat(&file->symlink, "..");
2368			break;
2369		case 0x08: /* Root of filesystem. */
2370			archive_strcat(&file->symlink, "/");
2371			separator = "";
2372			break;
2373		case 0x10: /* Undefined (historically "volume root" */
2374			archive_string_empty(&file->symlink);
2375			archive_strcat(&file->symlink, "ROOT");
2376			break;
2377		case 0x20: /* Undefined (historically "hostname") */
2378			archive_strcat(&file->symlink, "hostname");
2379			break;
2380		default:
2381			/* TODO: issue a warning ? */
2382			return;
2383		}
2384		data += nlen;
2385		data_length -= nlen;
2386	}
2387}
2388
2389static void
2390parse_rockridge_ZF1(struct file_info *file, const unsigned char *data,
2391    int data_length)
2392{
2393
2394	if (data[0] == 0x70 && data[1] == 0x7a && data_length == 12) {
2395		/* paged zlib */
2396		file->pz = 1;
2397		file->pz_log2_bs = data[3];
2398		file->pz_uncompressed_size = archive_le32dec(&data[4]);
2399	}
2400}
2401
2402static void
2403register_file(struct iso9660 *iso9660, struct file_info *file)
2404{
2405
2406	file->use_next = iso9660->use_files;
2407	iso9660->use_files = file;
2408}
2409
2410static void
2411release_files(struct iso9660 *iso9660)
2412{
2413	struct content *con, *connext;
2414	struct file_info *file;
2415
2416	file = iso9660->use_files;
2417	while (file != NULL) {
2418		struct file_info *next = file->use_next;
2419
2420		archive_string_free(&file->name);
2421		archive_string_free(&file->symlink);
2422		con = file->contents.first;
2423		while (con != NULL) {
2424			connext = con->next;
2425			free(con);
2426			con = connext;
2427		}
2428		free(file);
2429		file = next;
2430	}
2431}
2432
2433static int
2434next_entry_seek(struct archive_read *a, struct iso9660 *iso9660,
2435    struct file_info **pfile)
2436{
2437	struct file_info *file;
2438	int r;
2439
2440	r = next_cache_entry(a, iso9660, pfile);
2441	if (r != ARCHIVE_OK)
2442		return (r);
2443	file = *pfile;
2444
2445	/* Don't waste time seeking for zero-length bodies. */
2446	if (file->size == 0)
2447		file->offset = iso9660->current_position;
2448
2449	/* Seek forward to the start of the entry. */
2450	if (iso9660->current_position < file->offset) {
2451		int64_t step;
2452
2453		step = file->offset - iso9660->current_position;
2454		step = __archive_read_skip(a, step);
2455		if (step < 0)
2456			return ((int)step);
2457		iso9660->current_position = file->offset;
2458	}
2459
2460	/* We found body of file; handle it now. */
2461	return (ARCHIVE_OK);
2462}
2463
2464static int
2465next_cache_entry(struct archive_read *a, struct iso9660 *iso9660,
2466    struct file_info **pfile)
2467{
2468	struct file_info *file;
2469	struct {
2470		struct file_info	*first;
2471		struct file_info	**last;
2472	}	empty_files;
2473	int64_t number;
2474	int count;
2475
2476	file = cache_get_entry(iso9660);
2477	if (file != NULL) {
2478		*pfile = file;
2479		return (ARCHIVE_OK);
2480	}
2481
2482	for (;;) {
2483		struct file_info *re, *d;
2484
2485		*pfile = file = next_entry(iso9660);
2486		if (file == NULL) {
2487			/*
2488			 * If directory entries all which are descendant of
2489			 * rr_moved are stil remaning, expose their.
2490			 */
2491			if (iso9660->re_files.first != NULL &&
2492			    iso9660->rr_moved != NULL &&
2493			    iso9660->rr_moved->rr_moved_has_re_only)
2494				/* Expose "rr_moved" entry. */
2495				cache_add_entry(iso9660, iso9660->rr_moved);
2496			while ((re = re_get_entry(iso9660)) != NULL) {
2497				/* Expose its descendant dirs. */
2498				while ((d = rede_get_entry(re)) != NULL)
2499					cache_add_entry(iso9660, d);
2500			}
2501			if (iso9660->cache_files.first != NULL)
2502				return (next_cache_entry(a, iso9660, pfile));
2503			return (ARCHIVE_EOF);
2504		}
2505
2506		if (file->cl_offset) {
2507			struct file_info *first_re = NULL;
2508			int nexted_re = 0;
2509
2510			/*
2511			 * Find "RE" dir for the current file, which
2512			 * has "CL" flag.
2513			 */
2514			while ((re = re_get_entry(iso9660))
2515			    != first_re) {
2516				if (first_re == NULL)
2517					first_re = re;
2518				if (re->offset == file->cl_offset) {
2519					re->parent->subdirs--;
2520					re->parent = file->parent;
2521					re->re = 0;
2522					if (re->parent->re_descendant) {
2523						nexted_re = 1;
2524						re->re_descendant = 1;
2525						if (rede_add_entry(re) < 0)
2526							goto fatal_rr;
2527						/* Move a list of descendants
2528						 * to a new ancestor. */
2529						while ((d = rede_get_entry(
2530						    re)) != NULL)
2531							if (rede_add_entry(d)
2532							    < 0)
2533								goto fatal_rr;
2534						break;
2535					}
2536					/* Replace the current file
2537					 * with "RE" dir */
2538					*pfile = file = re;
2539					/* Expose its descendant */
2540					while ((d = rede_get_entry(
2541					    file)) != NULL)
2542						cache_add_entry(
2543						    iso9660, d);
2544					break;
2545				} else
2546					re_add_entry(iso9660, re);
2547			}
2548			if (nexted_re) {
2549				/*
2550				 * Do not expose this at this time
2551				 * because we have not gotten its full-path
2552				 * name yet.
2553				 */
2554				continue;
2555			}
2556		} else if ((file->mode & AE_IFMT) == AE_IFDIR) {
2557			int r;
2558
2559			/* Read file entries in this dir. */
2560			r = read_children(a, file);
2561			if (r != ARCHIVE_OK)
2562				return (r);
2563
2564			/*
2565			 * Handle a special dir of Rockridge extensions,
2566			 * "rr_moved".
2567			 */
2568			if (file->rr_moved) {
2569				/*
2570				 * If this has only the subdirectories which
2571				 * have "RE" flags, do not expose at this time.
2572				 */
2573				if (file->rr_moved_has_re_only)
2574					continue;
2575				/* Otherwise expose "rr_moved" entry. */
2576			} else if (file->re) {
2577				/*
2578				 * Do not expose this at this time
2579				 * because we have not gotten its full-path
2580				 * name yet.
2581				 */
2582				re_add_entry(iso9660, file);
2583				continue;
2584			} else if (file->re_descendant) {
2585				/*
2586				 * If the top level "RE" entry of this entry
2587				 * is not exposed, we, accordingly, should not
2588				 * expose this entry at this time because
2589				 * we cannot make its proper full-path name.
2590				 */
2591				if (rede_add_entry(file) == 0)
2592					continue;
2593				/* Otherwise we can expose this entry because
2594				 * it seems its top level "RE" has already been
2595				 * exposed. */
2596			}
2597		}
2598		break;
2599	}
2600
2601	if ((file->mode & AE_IFMT) != AE_IFREG || file->number == -1)
2602		return (ARCHIVE_OK);
2603
2604	count = 0;
2605	number = file->number;
2606	iso9660->cache_files.first = NULL;
2607	iso9660->cache_files.last = &(iso9660->cache_files.first);
2608	empty_files.first = NULL;
2609	empty_files.last = &empty_files.first;
2610	/* Collect files which has the same file serial number.
2611	 * Peek pending_files so that file which number is different
2612	 * is not put bak. */
2613	while (iso9660->pending_files.used > 0 &&
2614	    (iso9660->pending_files.files[0]->number == -1 ||
2615	     iso9660->pending_files.files[0]->number == number)) {
2616		if (file->number == -1) {
2617			/* This file has the same offset
2618			 * but it's wrong offset which empty files
2619			 * and symlink files have.
2620			 * NOTE: This wrong offse was recorded by
2621			 * old mkisofs utility. If ISO images is
2622			 * created by latest mkisofs, this does not
2623			 * happen.
2624			 */
2625			file->next = NULL;
2626			*empty_files.last = file;
2627			empty_files.last = &(file->next);
2628		} else {
2629			count++;
2630			cache_add_entry(iso9660, file);
2631		}
2632		file = next_entry(iso9660);
2633	}
2634
2635	if (count == 0) {
2636		*pfile = file;
2637		return ((file == NULL)?ARCHIVE_EOF:ARCHIVE_OK);
2638	}
2639	if (file->number == -1) {
2640		file->next = NULL;
2641		*empty_files.last = file;
2642		empty_files.last = &(file->next);
2643	} else {
2644		count++;
2645		cache_add_entry(iso9660, file);
2646	}
2647
2648	if (count > 1) {
2649		/* The count is the same as number of hardlink,
2650		 * so much so that each nlinks of files in cache_file
2651		 * is overwritten by value of the count.
2652		 */
2653		for (file = iso9660->cache_files.first;
2654		    file != NULL; file = file->next)
2655			file->nlinks = count;
2656	}
2657	/* If there are empty files, that files are added
2658	 * to the tail of the cache_files. */
2659	if (empty_files.first != NULL) {
2660		*iso9660->cache_files.last = empty_files.first;
2661		iso9660->cache_files.last = empty_files.last;
2662	}
2663	*pfile = cache_get_entry(iso9660);
2664	return ((*pfile == NULL)?ARCHIVE_EOF:ARCHIVE_OK);
2665
2666fatal_rr:
2667	archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
2668	    "Failed to connect 'CL' pointer to 'RE' rr_moved pointer of"
2669	    "Rockridge extensions");
2670	return (ARCHIVE_FATAL);
2671}
2672
2673static inline void
2674re_add_entry(struct iso9660 *iso9660, struct file_info *file)
2675{
2676	file->re_next = NULL;
2677	*iso9660->re_files.last = file;
2678	iso9660->re_files.last = &(file->re_next);
2679}
2680
2681static inline struct file_info *
2682re_get_entry(struct iso9660 *iso9660)
2683{
2684	struct file_info *file;
2685
2686	if ((file = iso9660->re_files.first) != NULL) {
2687		iso9660->re_files.first = file->re_next;
2688		if (iso9660->re_files.first == NULL)
2689			iso9660->re_files.last =
2690			    &(iso9660->re_files.first);
2691	}
2692	return (file);
2693}
2694
2695static inline int
2696rede_add_entry(struct file_info *file)
2697{
2698	struct file_info *re;
2699
2700	re = file->parent;
2701	while (re != NULL && !re->re) {
2702		/* Sanity check to prevent a infinity loop
2703		 * cause by a currupted iso file. */
2704		if (re->loop_by == file)
2705			return (-1);
2706		re->loop_by = file;
2707		re = re->parent;
2708	}
2709	if (re == NULL)
2710		return (-1);
2711
2712	file->re_next = NULL;
2713	*re->rede_files.last = file;
2714	re->rede_files.last = &(file->re_next);
2715	return (0);
2716}
2717
2718static inline struct file_info *
2719rede_get_entry(struct file_info *re)
2720{
2721	struct file_info *file;
2722
2723	if ((file = re->rede_files.first) != NULL) {
2724		re->rede_files.first = file->re_next;
2725		if (re->rede_files.first == NULL)
2726			re->rede_files.last =
2727			    &(re->rede_files.first);
2728	}
2729	return (file);
2730}
2731
2732static inline void
2733cache_add_entry(struct iso9660 *iso9660, struct file_info *file)
2734{
2735	file->next = NULL;
2736	*iso9660->cache_files.last = file;
2737	iso9660->cache_files.last = &(file->next);
2738}
2739
2740static inline struct file_info *
2741cache_get_entry(struct iso9660 *iso9660)
2742{
2743	struct file_info *file;
2744
2745	if ((file = iso9660->cache_files.first) != NULL) {
2746		iso9660->cache_files.first = file->next;
2747		if (iso9660->cache_files.first == NULL)
2748			iso9660->cache_files.last = &(iso9660->cache_files.first);
2749	}
2750	return (file);
2751}
2752
2753static void
2754heap_add_entry(struct heap_queue *heap, struct file_info *file, uint64_t key)
2755{
2756	uint64_t file_key, parent_key;
2757	int hole, parent;
2758
2759	/* Expand our pending files list as necessary. */
2760	if (heap->used >= heap->allocated) {
2761		struct file_info **new_pending_files;
2762		int new_size = heap->allocated * 2;
2763
2764		if (heap->allocated < 1024)
2765			new_size = 1024;
2766		/* Overflow might keep us from growing the list. */
2767		if (new_size <= heap->allocated)
2768			__archive_errx(1, "Out of memory");
2769		new_pending_files = (struct file_info **)
2770		    malloc(new_size * sizeof(new_pending_files[0]));
2771		if (new_pending_files == NULL)
2772			__archive_errx(1, "Out of memory");
2773		memcpy(new_pending_files, heap->files,
2774		    heap->allocated * sizeof(new_pending_files[0]));
2775		if (heap->files != NULL)
2776			free(heap->files);
2777		heap->files = new_pending_files;
2778		heap->allocated = new_size;
2779	}
2780
2781	file_key = file->key = key;
2782
2783	/*
2784	 * Start with hole at end, walk it up tree to find insertion point.
2785	 */
2786	hole = heap->used++;
2787	while (hole > 0) {
2788		parent = (hole - 1)/2;
2789		parent_key = heap->files[parent]->key;
2790		if (file_key >= parent_key) {
2791			heap->files[hole] = file;
2792			return;
2793		}
2794		// Move parent into hole <==> move hole up tree.
2795		heap->files[hole] = heap->files[parent];
2796		hole = parent;
2797	}
2798	heap->files[0] = file;
2799}
2800
2801static struct file_info *
2802heap_get_entry(struct heap_queue *heap)
2803{
2804	uint64_t a_key, b_key, c_key;
2805	int a, b, c;
2806	struct file_info *r, *tmp;
2807
2808	if (heap->used < 1)
2809		return (NULL);
2810
2811	/*
2812	 * The first file in the list is the earliest; we'll return this.
2813	 */
2814	r = heap->files[0];
2815
2816	/*
2817	 * Move the last item in the heap to the root of the tree
2818	 */
2819	heap->files[0] = heap->files[--(heap->used)];
2820
2821	/*
2822	 * Rebalance the heap.
2823	 */
2824	a = 0; // Starting element and its heap key
2825	a_key = heap->files[a]->key;
2826	for (;;) {
2827		b = a + a + 1; // First child
2828		if (b >= heap->used)
2829			return (r);
2830		b_key = heap->files[b]->key;
2831		c = b + 1; // Use second child if it is smaller.
2832		if (c < heap->used) {
2833			c_key = heap->files[c]->key;
2834			if (c_key < b_key) {
2835				b = c;
2836				b_key = c_key;
2837			}
2838		}
2839		if (a_key <= b_key)
2840			return (r);
2841		tmp = heap->files[a];
2842		heap->files[a] = heap->files[b];
2843		heap->files[b] = tmp;
2844		a = b;
2845	}
2846}
2847
2848static unsigned int
2849toi(const void *p, int n)
2850{
2851	const unsigned char *v = (const unsigned char *)p;
2852	if (n > 1)
2853		return v[0] + 256 * toi(v + 1, n - 1);
2854	if (n == 1)
2855		return v[0];
2856	return (0);
2857}
2858
2859static time_t
2860isodate7(const unsigned char *v)
2861{
2862	struct tm tm;
2863	int offset;
2864	memset(&tm, 0, sizeof(tm));
2865	tm.tm_year = v[0];
2866	tm.tm_mon = v[1] - 1;
2867	tm.tm_mday = v[2];
2868	tm.tm_hour = v[3];
2869	tm.tm_min = v[4];
2870	tm.tm_sec = v[5];
2871	/* v[6] is the signed timezone offset, in 1/4-hour increments. */
2872	offset = ((const signed char *)v)[6];
2873	if (offset > -48 && offset < 52) {
2874		tm.tm_hour -= offset / 4;
2875		tm.tm_min -= (offset % 4) * 15;
2876	}
2877	return (time_from_tm(&tm));
2878}
2879
2880static time_t
2881isodate17(const unsigned char *v)
2882{
2883	struct tm tm;
2884	int offset;
2885	memset(&tm, 0, sizeof(tm));
2886	tm.tm_year = (v[0] - '0') * 1000 + (v[1] - '0') * 100
2887	    + (v[2] - '0') * 10 + (v[3] - '0')
2888	    - 1900;
2889	tm.tm_mon = (v[4] - '0') * 10 + (v[5] - '0');
2890	tm.tm_mday = (v[6] - '0') * 10 + (v[7] - '0');
2891	tm.tm_hour = (v[8] - '0') * 10 + (v[9] - '0');
2892	tm.tm_min = (v[10] - '0') * 10 + (v[11] - '0');
2893	tm.tm_sec = (v[12] - '0') * 10 + (v[13] - '0');
2894	/* v[16] is the signed timezone offset, in 1/4-hour increments. */
2895	offset = ((const signed char *)v)[16];
2896	if (offset > -48 && offset < 52) {
2897		tm.tm_hour -= offset / 4;
2898		tm.tm_min -= (offset % 4) * 15;
2899	}
2900	return (time_from_tm(&tm));
2901}
2902
2903static time_t
2904time_from_tm(struct tm *t)
2905{
2906#if HAVE_TIMEGM
2907	/* Use platform timegm() if available. */
2908	return (timegm(t));
2909#else
2910	/* Else use direct calculation using POSIX assumptions. */
2911	/* First, fix up tm_yday based on the year/month/day. */
2912	mktime(t);
2913	/* Then we can compute timegm() from first principles. */
2914	return (t->tm_sec + t->tm_min * 60 + t->tm_hour * 3600
2915	    + t->tm_yday * 86400 + (t->tm_year - 70) * 31536000
2916	    + ((t->tm_year - 69) / 4) * 86400 -
2917	    ((t->tm_year - 1) / 100) * 86400
2918	    + ((t->tm_year + 299) / 400) * 86400);
2919#endif
2920}
2921
2922static const char *
2923build_pathname(struct archive_string *as, struct file_info *file)
2924{
2925	if (file->parent != NULL && archive_strlen(&file->parent->name) > 0) {
2926		build_pathname(as, file->parent);
2927		archive_strcat(as, "/");
2928	}
2929	if (archive_strlen(&file->name) == 0)
2930		archive_strcat(as, ".");
2931	else
2932		archive_string_concat(as, &file->name);
2933	return (as->s);
2934}
2935
2936#if DEBUG
2937static void
2938dump_isodirrec(FILE *out, const unsigned char *isodirrec)
2939{
2940	fprintf(out, " l %d,",
2941	    toi(isodirrec + DR_length_offset, DR_length_size));
2942	fprintf(out, " a %d,",
2943	    toi(isodirrec + DR_ext_attr_length_offset, DR_ext_attr_length_size));
2944	fprintf(out, " ext 0x%x,",
2945	    toi(isodirrec + DR_extent_offset, DR_extent_size));
2946	fprintf(out, " s %d,",
2947	    toi(isodirrec + DR_size_offset, DR_extent_size));
2948	fprintf(out, " f 0x%02x,",
2949	    toi(isodirrec + DR_flags_offset, DR_flags_size));
2950	fprintf(out, " u %d,",
2951	    toi(isodirrec + DR_file_unit_size_offset, DR_file_unit_size_size));
2952	fprintf(out, " ilv %d,",
2953	    toi(isodirrec + DR_interleave_offset, DR_interleave_size));
2954	fprintf(out, " seq %d,",
2955	    toi(isodirrec + DR_volume_sequence_number_offset, DR_volume_sequence_number_size));
2956	fprintf(out, " nl %d:",
2957	    toi(isodirrec + DR_name_len_offset, DR_name_len_size));
2958	fprintf(out, " `%.*s'",
2959	    toi(isodirrec + DR_name_len_offset, DR_name_len_size), isodirrec + DR_name_offset);
2960}
2961#endif
2962