1228753Smm/*- 2228753Smm * Copyright (c) 2003-2007 Tim Kientzle 3228753Smm * Copyright (c) 2009 Andreas Henriksson <andreas@fatal.se> 4248616Smm * Copyright (c) 2009-2012 Michihiro NAKAJIMA 5228753Smm * All rights reserved. 6228753Smm * 7228753Smm * Redistribution and use in source and binary forms, with or without 8228753Smm * modification, are permitted provided that the following conditions 9228753Smm * are met: 10228753Smm * 1. Redistributions of source code must retain the above copyright 11228753Smm * notice, this list of conditions and the following disclaimer. 12228753Smm * 2. Redistributions in binary form must reproduce the above copyright 13228753Smm * notice, this list of conditions and the following disclaimer in the 14228753Smm * documentation and/or other materials provided with the distribution. 15228753Smm * 16228753Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 17228753Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18228753Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19228753Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 20228753Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21228753Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22228753Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23228753Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24228753Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25228753Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26228753Smm */ 27228753Smm 28228753Smm#include "archive_platform.h" 29228763Smm__FBSDID("$FreeBSD: stable/10/contrib/libarchive/libarchive/archive_read_support_format_iso9660.c 344674 2019-02-28 22:57:09Z mm $"); 30228753Smm 31228753Smm#ifdef HAVE_ERRNO_H 32228753Smm#include <errno.h> 33228753Smm#endif 34228753Smm/* #include <stdint.h> */ /* See archive_platform.h */ 35228753Smm#include <stdio.h> 36228753Smm#ifdef HAVE_STDLIB_H 37228753Smm#include <stdlib.h> 38228753Smm#endif 39228753Smm#ifdef HAVE_STRING_H 40228753Smm#include <string.h> 41228753Smm#endif 42228753Smm#include <time.h> 43228753Smm#ifdef HAVE_ZLIB_H 44228753Smm#include <zlib.h> 45228753Smm#endif 46228753Smm 47228753Smm#include "archive.h" 48228753Smm#include "archive_endian.h" 49228753Smm#include "archive_entry.h" 50232153Smm#include "archive_entry_locale.h" 51228753Smm#include "archive_private.h" 52228753Smm#include "archive_read_private.h" 53228753Smm#include "archive_string.h" 54228753Smm 55228753Smm/* 56228753Smm * An overview of ISO 9660 format: 57228753Smm * 58228753Smm * Each disk is laid out as follows: 59228753Smm * * 32k reserved for private use 60228753Smm * * Volume descriptor table. Each volume descriptor 61228753Smm * is 2k and specifies basic format information. 62228753Smm * The "Primary Volume Descriptor" (PVD) is defined by the 63228753Smm * standard and should always be present; other volume 64228753Smm * descriptors include various vendor-specific extensions. 65228753Smm * * Files and directories. Each file/dir is specified by 66228753Smm * an "extent" (starting sector and length in bytes). 67228753Smm * Dirs are just files with directory records packed one 68228753Smm * after another. The PVD contains a single dir entry 69228753Smm * specifying the location of the root directory. Everything 70228753Smm * else follows from there. 71228753Smm * 72228753Smm * This module works by first reading the volume descriptors, then 73228753Smm * building a list of directory entries, sorted by starting 74228753Smm * sector. At each step, I look for the earliest dir entry that 75228753Smm * hasn't yet been read, seek forward to that location and read 76228753Smm * that entry. If it's a dir, I slurp in the new dir entries and 77228753Smm * add them to the heap; if it's a regular file, I return the 78228753Smm * corresponding archive_entry and wait for the client to request 79228753Smm * the file body. This strategy allows us to read most compliant 80228753Smm * CDs with a single pass through the data, as required by libarchive. 81228753Smm */ 82228753Smm#define LOGICAL_BLOCK_SIZE 2048 83228753Smm#define SYSTEM_AREA_BLOCK 16 84228753Smm 85228753Smm/* Structure of on-disk primary volume descriptor. */ 86228753Smm#define PVD_type_offset 0 87228753Smm#define PVD_type_size 1 88228753Smm#define PVD_id_offset (PVD_type_offset + PVD_type_size) 89228753Smm#define PVD_id_size 5 90228753Smm#define PVD_version_offset (PVD_id_offset + PVD_id_size) 91228753Smm#define PVD_version_size 1 92228753Smm#define PVD_reserved1_offset (PVD_version_offset + PVD_version_size) 93228753Smm#define PVD_reserved1_size 1 94228753Smm#define PVD_system_id_offset (PVD_reserved1_offset + PVD_reserved1_size) 95228753Smm#define PVD_system_id_size 32 96228753Smm#define PVD_volume_id_offset (PVD_system_id_offset + PVD_system_id_size) 97228753Smm#define PVD_volume_id_size 32 98228753Smm#define PVD_reserved2_offset (PVD_volume_id_offset + PVD_volume_id_size) 99228753Smm#define PVD_reserved2_size 8 100228753Smm#define PVD_volume_space_size_offset (PVD_reserved2_offset + PVD_reserved2_size) 101228753Smm#define PVD_volume_space_size_size 8 102228753Smm#define PVD_reserved3_offset (PVD_volume_space_size_offset + PVD_volume_space_size_size) 103228753Smm#define PVD_reserved3_size 32 104228753Smm#define PVD_volume_set_size_offset (PVD_reserved3_offset + PVD_reserved3_size) 105228753Smm#define PVD_volume_set_size_size 4 106228753Smm#define PVD_volume_sequence_number_offset (PVD_volume_set_size_offset + PVD_volume_set_size_size) 107228753Smm#define PVD_volume_sequence_number_size 4 108228753Smm#define PVD_logical_block_size_offset (PVD_volume_sequence_number_offset + PVD_volume_sequence_number_size) 109228753Smm#define PVD_logical_block_size_size 4 110228753Smm#define PVD_path_table_size_offset (PVD_logical_block_size_offset + PVD_logical_block_size_size) 111228753Smm#define PVD_path_table_size_size 8 112228753Smm#define PVD_type_1_path_table_offset (PVD_path_table_size_offset + PVD_path_table_size_size) 113228753Smm#define PVD_type_1_path_table_size 4 114228753Smm#define PVD_opt_type_1_path_table_offset (PVD_type_1_path_table_offset + PVD_type_1_path_table_size) 115228753Smm#define PVD_opt_type_1_path_table_size 4 116228753Smm#define PVD_type_m_path_table_offset (PVD_opt_type_1_path_table_offset + PVD_opt_type_1_path_table_size) 117228753Smm#define PVD_type_m_path_table_size 4 118228753Smm#define PVD_opt_type_m_path_table_offset (PVD_type_m_path_table_offset + PVD_type_m_path_table_size) 119228753Smm#define PVD_opt_type_m_path_table_size 4 120228753Smm#define PVD_root_directory_record_offset (PVD_opt_type_m_path_table_offset + PVD_opt_type_m_path_table_size) 121228753Smm#define PVD_root_directory_record_size 34 122228753Smm#define PVD_volume_set_id_offset (PVD_root_directory_record_offset + PVD_root_directory_record_size) 123228753Smm#define PVD_volume_set_id_size 128 124228753Smm#define PVD_publisher_id_offset (PVD_volume_set_id_offset + PVD_volume_set_id_size) 125228753Smm#define PVD_publisher_id_size 128 126228753Smm#define PVD_preparer_id_offset (PVD_publisher_id_offset + PVD_publisher_id_size) 127228753Smm#define PVD_preparer_id_size 128 128228753Smm#define PVD_application_id_offset (PVD_preparer_id_offset + PVD_preparer_id_size) 129228753Smm#define PVD_application_id_size 128 130228753Smm#define PVD_copyright_file_id_offset (PVD_application_id_offset + PVD_application_id_size) 131228753Smm#define PVD_copyright_file_id_size 37 132228753Smm#define PVD_abstract_file_id_offset (PVD_copyright_file_id_offset + PVD_copyright_file_id_size) 133228753Smm#define PVD_abstract_file_id_size 37 134228753Smm#define PVD_bibliographic_file_id_offset (PVD_abstract_file_id_offset + PVD_abstract_file_id_size) 135228753Smm#define PVD_bibliographic_file_id_size 37 136228753Smm#define PVD_creation_date_offset (PVD_bibliographic_file_id_offset + PVD_bibliographic_file_id_size) 137228753Smm#define PVD_creation_date_size 17 138228753Smm#define PVD_modification_date_offset (PVD_creation_date_offset + PVD_creation_date_size) 139228753Smm#define PVD_modification_date_size 17 140228753Smm#define PVD_expiration_date_offset (PVD_modification_date_offset + PVD_modification_date_size) 141228753Smm#define PVD_expiration_date_size 17 142228753Smm#define PVD_effective_date_offset (PVD_expiration_date_offset + PVD_expiration_date_size) 143228753Smm#define PVD_effective_date_size 17 144228753Smm#define PVD_file_structure_version_offset (PVD_effective_date_offset + PVD_effective_date_size) 145228753Smm#define PVD_file_structure_version_size 1 146228753Smm#define PVD_reserved4_offset (PVD_file_structure_version_offset + PVD_file_structure_version_size) 147228753Smm#define PVD_reserved4_size 1 148228753Smm#define PVD_application_data_offset (PVD_reserved4_offset + PVD_reserved4_size) 149228753Smm#define PVD_application_data_size 512 150228753Smm#define PVD_reserved5_offset (PVD_application_data_offset + PVD_application_data_size) 151228753Smm#define PVD_reserved5_size (2048 - PVD_reserved5_offset) 152228753Smm 153228753Smm/* TODO: It would make future maintenance easier to just hardcode the 154228753Smm * above values. In particular, ECMA119 states the offsets as part of 155228753Smm * the standard. That would eliminate the need for the following check.*/ 156228753Smm#if PVD_reserved5_offset != 1395 157228753Smm#error PVD offset and size definitions are wrong. 158228753Smm#endif 159228753Smm 160228753Smm 161228753Smm/* Structure of optional on-disk supplementary volume descriptor. */ 162228753Smm#define SVD_type_offset 0 163228753Smm#define SVD_type_size 1 164228753Smm#define SVD_id_offset (SVD_type_offset + SVD_type_size) 165228753Smm#define SVD_id_size 5 166228753Smm#define SVD_version_offset (SVD_id_offset + SVD_id_size) 167228753Smm#define SVD_version_size 1 168228753Smm/* ... */ 169228753Smm#define SVD_reserved1_offset 72 170228753Smm#define SVD_reserved1_size 8 171228753Smm#define SVD_volume_space_size_offset 80 172228753Smm#define SVD_volume_space_size_size 8 173228753Smm#define SVD_escape_sequences_offset (SVD_volume_space_size_offset + SVD_volume_space_size_size) 174228753Smm#define SVD_escape_sequences_size 32 175228753Smm/* ... */ 176228753Smm#define SVD_logical_block_size_offset 128 177228753Smm#define SVD_logical_block_size_size 4 178228753Smm#define SVD_type_L_path_table_offset 140 179228753Smm#define SVD_type_M_path_table_offset 148 180228753Smm/* ... */ 181228753Smm#define SVD_root_directory_record_offset 156 182228753Smm#define SVD_root_directory_record_size 34 183228753Smm#define SVD_file_structure_version_offset 881 184228753Smm#define SVD_reserved2_offset 882 185228753Smm#define SVD_reserved2_size 1 186228753Smm#define SVD_reserved3_offset 1395 187228753Smm#define SVD_reserved3_size 653 188228753Smm/* ... */ 189228753Smm/* FIXME: validate correctness of last SVD entry offset. */ 190228753Smm 191228753Smm/* Structure of an on-disk directory record. */ 192228753Smm/* Note: ISO9660 stores each multi-byte integer twice, once in 193228753Smm * each byte order. The sizes here are the size of just one 194228753Smm * of the two integers. (This is why the offset of a field isn't 195228753Smm * the same as the offset+size of the previous field.) */ 196228753Smm#define DR_length_offset 0 197228753Smm#define DR_length_size 1 198228753Smm#define DR_ext_attr_length_offset 1 199228753Smm#define DR_ext_attr_length_size 1 200228753Smm#define DR_extent_offset 2 201228753Smm#define DR_extent_size 4 202228753Smm#define DR_size_offset 10 203228753Smm#define DR_size_size 4 204228753Smm#define DR_date_offset 18 205228753Smm#define DR_date_size 7 206228753Smm#define DR_flags_offset 25 207228753Smm#define DR_flags_size 1 208228753Smm#define DR_file_unit_size_offset 26 209228753Smm#define DR_file_unit_size_size 1 210228753Smm#define DR_interleave_offset 27 211228753Smm#define DR_interleave_size 1 212228753Smm#define DR_volume_sequence_number_offset 28 213228753Smm#define DR_volume_sequence_number_size 2 214228753Smm#define DR_name_len_offset 32 215228753Smm#define DR_name_len_size 1 216228753Smm#define DR_name_offset 33 217228753Smm 218228753Smm#ifdef HAVE_ZLIB_H 219228753Smmstatic const unsigned char zisofs_magic[8] = { 220228753Smm 0x37, 0xE4, 0x53, 0x96, 0xC9, 0xDB, 0xD6, 0x07 221228753Smm}; 222228753Smm 223228753Smmstruct zisofs { 224228753Smm /* Set 1 if this file compressed by paged zlib */ 225228753Smm int pz; 226228753Smm int pz_log2_bs; /* Log2 of block size */ 227228753Smm uint64_t pz_uncompressed_size; 228228753Smm 229228753Smm int initialized; 230228753Smm unsigned char *uncompressed_buffer; 231228753Smm size_t uncompressed_buffer_size; 232228753Smm 233228753Smm uint32_t pz_offset; 234228753Smm unsigned char header[16]; 235228753Smm size_t header_avail; 236228753Smm int header_passed; 237228753Smm unsigned char *block_pointers; 238228753Smm size_t block_pointers_alloc; 239228753Smm size_t block_pointers_size; 240228753Smm size_t block_pointers_avail; 241228753Smm size_t block_off; 242228753Smm uint32_t block_avail; 243228753Smm 244228753Smm z_stream stream; 245228753Smm int stream_valid; 246228753Smm}; 247228753Smm#else 248228753Smmstruct zisofs { 249228753Smm /* Set 1 if this file compressed by paged zlib */ 250228753Smm int pz; 251228753Smm}; 252228753Smm#endif 253228753Smm 254228753Smmstruct content { 255228753Smm uint64_t offset;/* Offset on disk. */ 256228753Smm uint64_t size; /* File size in bytes. */ 257228753Smm struct content *next; 258228753Smm}; 259228753Smm 260228753Smm/* In-memory storage for a directory record. */ 261228753Smmstruct file_info { 262228753Smm struct file_info *use_next; 263228753Smm struct file_info *parent; 264228753Smm struct file_info *next; 265228753Smm struct file_info *re_next; 266228753Smm int subdirs; 267228753Smm uint64_t key; /* Heap Key. */ 268228753Smm uint64_t offset; /* Offset on disk. */ 269228753Smm uint64_t size; /* File size in bytes. */ 270228753Smm uint32_t ce_offset; /* Offset of CE. */ 271228753Smm uint32_t ce_size; /* Size of CE. */ 272228753Smm char rr_moved; /* Flag to rr_moved. */ 273228753Smm char rr_moved_has_re_only; 274228753Smm char re; /* Having RRIP "RE" extension. */ 275228753Smm char re_descendant; 276228753Smm uint64_t cl_offset; /* Having RRIP "CL" extension. */ 277228753Smm int birthtime_is_set; 278228753Smm time_t birthtime; /* File created time. */ 279228753Smm time_t mtime; /* File last modified time. */ 280228753Smm time_t atime; /* File last accessed time. */ 281228753Smm time_t ctime; /* File attribute change time. */ 282228753Smm uint64_t rdev; /* Device number. */ 283228753Smm mode_t mode; 284228753Smm uid_t uid; 285228753Smm gid_t gid; 286228753Smm int64_t number; 287228753Smm int nlinks; 288228753Smm struct archive_string name; /* Pathname */ 289232153Smm unsigned char *utf16be_name; 290232153Smm size_t utf16be_bytes; 291228753Smm char name_continues; /* Non-zero if name continues */ 292228753Smm struct archive_string symlink; 293228753Smm char symlink_continues; /* Non-zero if link continues */ 294228753Smm /* Set 1 if this file compressed by paged zlib(zisofs) */ 295228753Smm int pz; 296228753Smm int pz_log2_bs; /* Log2 of block size */ 297228753Smm uint64_t pz_uncompressed_size; 298228753Smm /* Set 1 if this file is multi extent. */ 299228753Smm int multi_extent; 300228753Smm struct { 301228753Smm struct content *first; 302228753Smm struct content **last; 303228753Smm } contents; 304228753Smm struct { 305228753Smm struct file_info *first; 306228753Smm struct file_info **last; 307228753Smm } rede_files; 308228753Smm}; 309228753Smm 310228753Smmstruct heap_queue { 311228753Smm struct file_info **files; 312228753Smm int allocated; 313228753Smm int used; 314228753Smm}; 315228753Smm 316228753Smmstruct iso9660 { 317228753Smm int magic; 318228753Smm#define ISO9660_MAGIC 0x96609660 319228753Smm 320228753Smm int opt_support_joliet; 321228753Smm int opt_support_rockridge; 322228753Smm 323228753Smm struct archive_string pathname; 324228753Smm char seenRockridge; /* Set true if RR extensions are used. */ 325313571Smm char seenSUSP; /* Set true if SUSP is being used. */ 326228753Smm char seenJoliet; 327228753Smm 328228753Smm unsigned char suspOffset; 329228753Smm struct file_info *rr_moved; 330228753Smm struct read_ce_queue { 331228753Smm struct read_ce_req { 332228753Smm uint64_t offset;/* Offset of CE on disk. */ 333228753Smm struct file_info *file; 334228753Smm } *reqs; 335228753Smm int cnt; 336228753Smm int allocated; 337228753Smm } read_ce_req; 338228753Smm 339228753Smm int64_t previous_number; 340228753Smm struct archive_string previous_pathname; 341228753Smm 342228753Smm struct file_info *use_files; 343228753Smm struct heap_queue pending_files; 344228753Smm struct { 345228753Smm struct file_info *first; 346228753Smm struct file_info **last; 347228753Smm } cache_files; 348228753Smm struct { 349228753Smm struct file_info *first; 350228753Smm struct file_info **last; 351228753Smm } re_files; 352228753Smm 353228753Smm uint64_t current_position; 354228753Smm ssize_t logical_block_size; 355228753Smm uint64_t volume_size; /* Total size of volume in bytes. */ 356228753Smm int32_t volume_block;/* Total size of volume in logical blocks. */ 357228753Smm 358228753Smm struct vd { 359228753Smm int location; /* Location of Extent. */ 360228753Smm uint32_t size; 361228753Smm } primary, joliet; 362228753Smm 363232153Smm int64_t entry_sparse_offset; 364228753Smm int64_t entry_bytes_remaining; 365232153Smm size_t entry_bytes_unconsumed; 366228753Smm struct zisofs entry_zisofs; 367228753Smm struct content *entry_content; 368232153Smm struct archive_string_conv *sconv_utf16be; 369232153Smm /* 370232153Smm * Buffers for a full pathname in UTF-16BE in Joliet extensions. 371232153Smm */ 372232153Smm#define UTF16_NAME_MAX 1024 373232153Smm unsigned char *utf16be_path; 374232153Smm size_t utf16be_path_len; 375232153Smm unsigned char *utf16be_previous_path; 376232153Smm size_t utf16be_previous_path_len; 377313571Smm /* Null buffer used in bidder to improve its performance. */ 378248616Smm unsigned char null[2048]; 379228753Smm}; 380228753Smm 381232153Smmstatic int archive_read_format_iso9660_bid(struct archive_read *, int); 382228753Smmstatic int archive_read_format_iso9660_options(struct archive_read *, 383228753Smm const char *, const char *); 384228753Smmstatic int archive_read_format_iso9660_cleanup(struct archive_read *); 385228753Smmstatic int archive_read_format_iso9660_read_data(struct archive_read *, 386232153Smm const void **, size_t *, int64_t *); 387228753Smmstatic int archive_read_format_iso9660_read_data_skip(struct archive_read *); 388228753Smmstatic int archive_read_format_iso9660_read_header(struct archive_read *, 389228753Smm struct archive_entry *); 390302001Smmstatic const char *build_pathname(struct archive_string *, struct file_info *, int); 391232153Smmstatic int build_pathname_utf16be(unsigned char *, size_t, size_t *, 392232153Smm struct file_info *); 393228753Smm#if DEBUG 394228753Smmstatic void dump_isodirrec(FILE *, const unsigned char *isodirrec); 395228753Smm#endif 396228753Smmstatic time_t time_from_tm(struct tm *); 397228753Smmstatic time_t isodate17(const unsigned char *); 398228753Smmstatic time_t isodate7(const unsigned char *); 399228753Smmstatic int isBootRecord(struct iso9660 *, const unsigned char *); 400228753Smmstatic int isVolumePartition(struct iso9660 *, const unsigned char *); 401228753Smmstatic int isVDSetTerminator(struct iso9660 *, const unsigned char *); 402228753Smmstatic int isJolietSVD(struct iso9660 *, const unsigned char *); 403228753Smmstatic int isSVD(struct iso9660 *, const unsigned char *); 404228753Smmstatic int isEVD(struct iso9660 *, const unsigned char *); 405228753Smmstatic int isPVD(struct iso9660 *, const unsigned char *); 406228753Smmstatic int next_cache_entry(struct archive_read *, struct iso9660 *, 407228753Smm struct file_info **); 408232153Smmstatic int next_entry_seek(struct archive_read *, struct iso9660 *, 409232153Smm struct file_info **); 410228753Smmstatic struct file_info * 411228753Smm parse_file_info(struct archive_read *a, 412338034Smm struct file_info *parent, const unsigned char *isodirrec, 413338034Smm size_t reclen); 414228753Smmstatic int parse_rockridge(struct archive_read *a, 415228753Smm struct file_info *file, const unsigned char *start, 416228753Smm const unsigned char *end); 417228753Smmstatic int register_CE(struct archive_read *a, int32_t location, 418228753Smm struct file_info *file); 419228753Smmstatic int read_CE(struct archive_read *a, struct iso9660 *iso9660); 420228753Smmstatic void parse_rockridge_NM1(struct file_info *, 421228753Smm const unsigned char *, int); 422228753Smmstatic void parse_rockridge_SL1(struct file_info *, 423228753Smm const unsigned char *, int); 424228753Smmstatic void parse_rockridge_TF1(struct file_info *, 425228753Smm const unsigned char *, int); 426228753Smmstatic void parse_rockridge_ZF1(struct file_info *, 427228753Smm const unsigned char *, int); 428228753Smmstatic void register_file(struct iso9660 *, struct file_info *); 429228753Smmstatic void release_files(struct iso9660 *); 430228753Smmstatic unsigned toi(const void *p, int n); 431228753Smmstatic inline void re_add_entry(struct iso9660 *, struct file_info *); 432228753Smmstatic inline struct file_info * re_get_entry(struct iso9660 *); 433228753Smmstatic inline int rede_add_entry(struct file_info *); 434228753Smmstatic inline struct file_info * rede_get_entry(struct file_info *); 435228753Smmstatic inline void cache_add_entry(struct iso9660 *iso9660, 436228753Smm struct file_info *file); 437228753Smmstatic inline struct file_info *cache_get_entry(struct iso9660 *iso9660); 438232153Smmstatic int heap_add_entry(struct archive_read *a, struct heap_queue *heap, 439228753Smm struct file_info *file, uint64_t key); 440228753Smmstatic struct file_info *heap_get_entry(struct heap_queue *heap); 441228753Smm 442232153Smm#define add_entry(arch, iso9660, file) \ 443232153Smm heap_add_entry(arch, &((iso9660)->pending_files), file, file->offset) 444228753Smm#define next_entry(iso9660) \ 445228753Smm heap_get_entry(&((iso9660)->pending_files)) 446228753Smm 447228753Smmint 448228753Smmarchive_read_support_format_iso9660(struct archive *_a) 449228753Smm{ 450228753Smm struct archive_read *a = (struct archive_read *)_a; 451228753Smm struct iso9660 *iso9660; 452228753Smm int r; 453228753Smm 454232153Smm archive_check_magic(_a, ARCHIVE_READ_MAGIC, 455232153Smm ARCHIVE_STATE_NEW, "archive_read_support_format_iso9660"); 456232153Smm 457232153Smm iso9660 = (struct iso9660 *)calloc(1, sizeof(*iso9660)); 458228753Smm if (iso9660 == NULL) { 459232153Smm archive_set_error(&a->archive, ENOMEM, 460232153Smm "Can't allocate iso9660 data"); 461228753Smm return (ARCHIVE_FATAL); 462228753Smm } 463228753Smm iso9660->magic = ISO9660_MAGIC; 464228753Smm iso9660->cache_files.first = NULL; 465228753Smm iso9660->cache_files.last = &(iso9660->cache_files.first); 466228753Smm iso9660->re_files.first = NULL; 467228753Smm iso9660->re_files.last = &(iso9660->re_files.first); 468228753Smm /* Enable to support Joliet extensions by default. */ 469228753Smm iso9660->opt_support_joliet = 1; 470228753Smm /* Enable to support Rock Ridge extensions by default. */ 471228753Smm iso9660->opt_support_rockridge = 1; 472228753Smm 473228753Smm r = __archive_read_register_format(a, 474228753Smm iso9660, 475228753Smm "iso9660", 476228753Smm archive_read_format_iso9660_bid, 477228753Smm archive_read_format_iso9660_options, 478228753Smm archive_read_format_iso9660_read_header, 479228753Smm archive_read_format_iso9660_read_data, 480228753Smm archive_read_format_iso9660_read_data_skip, 481248616Smm NULL, 482302001Smm archive_read_format_iso9660_cleanup, 483302001Smm NULL, 484302001Smm NULL); 485228753Smm 486228753Smm if (r != ARCHIVE_OK) { 487228753Smm free(iso9660); 488228753Smm return (r); 489228753Smm } 490228753Smm return (ARCHIVE_OK); 491228753Smm} 492228753Smm 493228753Smm 494228753Smmstatic int 495232153Smmarchive_read_format_iso9660_bid(struct archive_read *a, int best_bid) 496228753Smm{ 497228753Smm struct iso9660 *iso9660; 498228753Smm ssize_t bytes_read; 499228753Smm const unsigned char *p; 500228753Smm int seenTerminator; 501228753Smm 502232153Smm /* If there's already a better bid than we can ever 503232153Smm make, don't bother testing. */ 504232153Smm if (best_bid > 48) 505232153Smm return (-1); 506232153Smm 507228753Smm iso9660 = (struct iso9660 *)(a->format->data); 508228753Smm 509228753Smm /* 510228753Smm * Skip the first 32k (reserved area) and get the first 511228753Smm * 8 sectors of the volume descriptor table. Of course, 512228753Smm * if the I/O layer gives us more, we'll take it. 513228753Smm */ 514228753Smm#define RESERVED_AREA (SYSTEM_AREA_BLOCK * LOGICAL_BLOCK_SIZE) 515232153Smm p = __archive_read_ahead(a, 516228753Smm RESERVED_AREA + 8 * LOGICAL_BLOCK_SIZE, 517228753Smm &bytes_read); 518232153Smm if (p == NULL) 519228753Smm return (-1); 520228753Smm 521228753Smm /* Skip the reserved area. */ 522228753Smm bytes_read -= RESERVED_AREA; 523228753Smm p += RESERVED_AREA; 524228753Smm 525228753Smm /* Check each volume descriptor. */ 526228753Smm seenTerminator = 0; 527228753Smm for (; bytes_read > LOGICAL_BLOCK_SIZE; 528228753Smm bytes_read -= LOGICAL_BLOCK_SIZE, p += LOGICAL_BLOCK_SIZE) { 529228753Smm /* Do not handle undefined Volume Descriptor Type. */ 530228753Smm if (p[0] >= 4 && p[0] <= 254) 531228753Smm return (0); 532228753Smm /* Standard Identifier must be "CD001" */ 533228753Smm if (memcmp(p + 1, "CD001", 5) != 0) 534228753Smm return (0); 535232153Smm if (isPVD(iso9660, p)) 536232153Smm continue; 537228753Smm if (!iso9660->joliet.location) { 538228753Smm if (isJolietSVD(iso9660, p)) 539228753Smm continue; 540228753Smm } 541228753Smm if (isBootRecord(iso9660, p)) 542228753Smm continue; 543228753Smm if (isEVD(iso9660, p)) 544228753Smm continue; 545228753Smm if (isSVD(iso9660, p)) 546228753Smm continue; 547228753Smm if (isVolumePartition(iso9660, p)) 548228753Smm continue; 549228753Smm if (isVDSetTerminator(iso9660, p)) { 550228753Smm seenTerminator = 1; 551228753Smm break; 552228753Smm } 553228753Smm return (0); 554228753Smm } 555228753Smm /* 556228753Smm * ISO 9660 format must have Primary Volume Descriptor and 557228753Smm * Volume Descriptor Set Terminator. 558228753Smm */ 559228753Smm if (seenTerminator && iso9660->primary.location > 16) 560228753Smm return (48); 561228753Smm 562228753Smm /* We didn't find a valid PVD; return a bid of zero. */ 563228753Smm return (0); 564228753Smm} 565228753Smm 566228753Smmstatic int 567228753Smmarchive_read_format_iso9660_options(struct archive_read *a, 568228753Smm const char *key, const char *val) 569228753Smm{ 570228753Smm struct iso9660 *iso9660; 571228753Smm 572228753Smm iso9660 = (struct iso9660 *)(a->format->data); 573228753Smm 574228753Smm if (strcmp(key, "joliet") == 0) { 575228753Smm if (val == NULL || strcmp(val, "off") == 0 || 576228753Smm strcmp(val, "ignore") == 0 || 577228753Smm strcmp(val, "disable") == 0 || 578228753Smm strcmp(val, "0") == 0) 579228753Smm iso9660->opt_support_joliet = 0; 580228753Smm else 581228753Smm iso9660->opt_support_joliet = 1; 582228753Smm return (ARCHIVE_OK); 583228753Smm } 584228753Smm if (strcmp(key, "rockridge") == 0 || 585228753Smm strcmp(key, "Rockridge") == 0) { 586228753Smm iso9660->opt_support_rockridge = val != NULL; 587228753Smm return (ARCHIVE_OK); 588228753Smm } 589228753Smm 590228753Smm /* Note: The "warn" return is just to inform the options 591228753Smm * supervisor that we didn't handle it. It will generate 592232153Smm * a suitable error if no one used this option. */ 593228753Smm return (ARCHIVE_WARN); 594228753Smm} 595228753Smm 596228753Smmstatic int 597248616SmmisNull(struct iso9660 *iso9660, const unsigned char *h, unsigned offset, 598248616Smmunsigned bytes) 599248616Smm{ 600248616Smm 601248616Smm while (bytes >= sizeof(iso9660->null)) { 602248616Smm if (!memcmp(iso9660->null, h + offset, sizeof(iso9660->null))) 603248616Smm return (0); 604248616Smm offset += sizeof(iso9660->null); 605248616Smm bytes -= sizeof(iso9660->null); 606248616Smm } 607248616Smm if (bytes) 608248616Smm return memcmp(iso9660->null, h + offset, bytes) == 0; 609248616Smm else 610248616Smm return (1); 611248616Smm} 612248616Smm 613248616Smmstatic int 614228753SmmisBootRecord(struct iso9660 *iso9660, const unsigned char *h) 615228753Smm{ 616228753Smm (void)iso9660; /* UNUSED */ 617228753Smm 618228753Smm /* Type of the Volume Descriptor Boot Record must be 0. */ 619228753Smm if (h[0] != 0) 620228753Smm return (0); 621228753Smm 622228753Smm /* Volume Descriptor Version must be 1. */ 623228753Smm if (h[6] != 1) 624228753Smm return (0); 625228753Smm 626228753Smm return (1); 627228753Smm} 628228753Smm 629228753Smmstatic int 630228753SmmisVolumePartition(struct iso9660 *iso9660, const unsigned char *h) 631228753Smm{ 632228753Smm int32_t location; 633228753Smm 634228753Smm /* Type of the Volume Partition Descriptor must be 3. */ 635228753Smm if (h[0] != 3) 636228753Smm return (0); 637228753Smm 638228753Smm /* Volume Descriptor Version must be 1. */ 639228753Smm if (h[6] != 1) 640228753Smm return (0); 641228753Smm /* Unused Field */ 642228753Smm if (h[7] != 0) 643228753Smm return (0); 644228753Smm 645228753Smm location = archive_le32dec(h + 72); 646228753Smm if (location <= SYSTEM_AREA_BLOCK || 647228753Smm location >= iso9660->volume_block) 648228753Smm return (0); 649228753Smm if ((uint32_t)location != archive_be32dec(h + 76)) 650228753Smm return (0); 651228753Smm 652228753Smm return (1); 653228753Smm} 654228753Smm 655228753Smmstatic int 656228753SmmisVDSetTerminator(struct iso9660 *iso9660, const unsigned char *h) 657228753Smm{ 658228753Smm (void)iso9660; /* UNUSED */ 659228753Smm 660228753Smm /* Type of the Volume Descriptor Set Terminator must be 255. */ 661228753Smm if (h[0] != 255) 662228753Smm return (0); 663228753Smm 664228753Smm /* Volume Descriptor Version must be 1. */ 665228753Smm if (h[6] != 1) 666228753Smm return (0); 667228753Smm 668228753Smm /* Reserved field must be 0. */ 669248616Smm if (!isNull(iso9660, h, 7, 2048-7)) 670248616Smm return (0); 671228753Smm 672228753Smm return (1); 673228753Smm} 674228753Smm 675228753Smmstatic int 676228753SmmisJolietSVD(struct iso9660 *iso9660, const unsigned char *h) 677228753Smm{ 678228753Smm const unsigned char *p; 679228753Smm ssize_t logical_block_size; 680228753Smm int32_t volume_block; 681228753Smm 682228753Smm /* Check if current sector is a kind of Supplementary Volume 683228753Smm * Descriptor. */ 684228753Smm if (!isSVD(iso9660, h)) 685228753Smm return (0); 686228753Smm 687228753Smm /* FIXME: do more validations according to joliet spec. */ 688228753Smm 689228753Smm /* check if this SVD contains joliet extension! */ 690228753Smm p = h + SVD_escape_sequences_offset; 691228753Smm /* N.B. Joliet spec says p[1] == '\\', but.... */ 692228753Smm if (p[0] == '%' && p[1] == '/') { 693228753Smm int level = 0; 694228753Smm 695228753Smm if (p[2] == '@') 696228753Smm level = 1; 697228753Smm else if (p[2] == 'C') 698228753Smm level = 2; 699228753Smm else if (p[2] == 'E') 700228753Smm level = 3; 701228753Smm else /* not joliet */ 702228753Smm return (0); 703228753Smm 704228753Smm iso9660->seenJoliet = level; 705228753Smm 706228753Smm } else /* not joliet */ 707228753Smm return (0); 708228753Smm 709228753Smm logical_block_size = 710228753Smm archive_le16dec(h + SVD_logical_block_size_offset); 711228753Smm volume_block = archive_le32dec(h + SVD_volume_space_size_offset); 712228753Smm 713228753Smm iso9660->logical_block_size = logical_block_size; 714228753Smm iso9660->volume_block = volume_block; 715228753Smm iso9660->volume_size = logical_block_size * (uint64_t)volume_block; 716228753Smm /* Read Root Directory Record in Volume Descriptor. */ 717228753Smm p = h + SVD_root_directory_record_offset; 718228753Smm iso9660->joliet.location = archive_le32dec(p + DR_extent_offset); 719228753Smm iso9660->joliet.size = archive_le32dec(p + DR_size_offset); 720228753Smm 721228753Smm return (48); 722228753Smm} 723228753Smm 724228753Smmstatic int 725228753SmmisSVD(struct iso9660 *iso9660, const unsigned char *h) 726228753Smm{ 727228753Smm const unsigned char *p; 728228753Smm ssize_t logical_block_size; 729228753Smm int32_t volume_block; 730228753Smm int32_t location; 731228753Smm 732228753Smm (void)iso9660; /* UNUSED */ 733228753Smm 734228753Smm /* Type 2 means it's a SVD. */ 735228753Smm if (h[SVD_type_offset] != 2) 736228753Smm return (0); 737228753Smm 738228753Smm /* Reserved field must be 0. */ 739248616Smm if (!isNull(iso9660, h, SVD_reserved1_offset, SVD_reserved1_size)) 740248616Smm return (0); 741248616Smm if (!isNull(iso9660, h, SVD_reserved2_offset, SVD_reserved2_size)) 742248616Smm return (0); 743248616Smm if (!isNull(iso9660, h, SVD_reserved3_offset, SVD_reserved3_size)) 744248616Smm return (0); 745228753Smm 746228753Smm /* File structure version must be 1 for ISO9660/ECMA119. */ 747228753Smm if (h[SVD_file_structure_version_offset] != 1) 748228753Smm return (0); 749228753Smm 750228753Smm logical_block_size = 751228753Smm archive_le16dec(h + SVD_logical_block_size_offset); 752228753Smm if (logical_block_size <= 0) 753228753Smm return (0); 754228753Smm 755228753Smm volume_block = archive_le32dec(h + SVD_volume_space_size_offset); 756228753Smm if (volume_block <= SYSTEM_AREA_BLOCK+4) 757228753Smm return (0); 758228753Smm 759228753Smm /* Location of Occurrence of Type L Path Table must be 760228753Smm * available location, 761228753Smm * >= SYSTEM_AREA_BLOCK(16) + 2 and < Volume Space Size. */ 762228753Smm location = archive_le32dec(h+SVD_type_L_path_table_offset); 763228753Smm if (location < SYSTEM_AREA_BLOCK+2 || location >= volume_block) 764228753Smm return (0); 765228753Smm 766228753Smm /* The Type M Path Table must be at a valid location (WinISO 767228753Smm * and probably other programs omit this, so we allow zero) 768228753Smm * 769228753Smm * >= SYSTEM_AREA_BLOCK(16) + 2 and < Volume Space Size. */ 770228753Smm location = archive_be32dec(h+SVD_type_M_path_table_offset); 771228753Smm if ((location > 0 && location < SYSTEM_AREA_BLOCK+2) 772228753Smm || location >= volume_block) 773228753Smm return (0); 774228753Smm 775228753Smm /* Read Root Directory Record in Volume Descriptor. */ 776228753Smm p = h + SVD_root_directory_record_offset; 777228753Smm if (p[DR_length_offset] != 34) 778228753Smm return (0); 779228753Smm 780228753Smm return (48); 781228753Smm} 782228753Smm 783228753Smmstatic int 784228753SmmisEVD(struct iso9660 *iso9660, const unsigned char *h) 785228753Smm{ 786228753Smm const unsigned char *p; 787228753Smm ssize_t logical_block_size; 788228753Smm int32_t volume_block; 789228753Smm int32_t location; 790228753Smm 791228753Smm (void)iso9660; /* UNUSED */ 792228753Smm 793228753Smm /* Type of the Enhanced Volume Descriptor must be 2. */ 794228753Smm if (h[PVD_type_offset] != 2) 795228753Smm return (0); 796228753Smm 797228753Smm /* EVD version must be 2. */ 798228753Smm if (h[PVD_version_offset] != 2) 799228753Smm return (0); 800228753Smm 801228753Smm /* Reserved field must be 0. */ 802228753Smm if (h[PVD_reserved1_offset] != 0) 803228753Smm return (0); 804228753Smm 805228753Smm /* Reserved field must be 0. */ 806248616Smm if (!isNull(iso9660, h, PVD_reserved2_offset, PVD_reserved2_size)) 807248616Smm return (0); 808228753Smm 809228753Smm /* Reserved field must be 0. */ 810248616Smm if (!isNull(iso9660, h, PVD_reserved3_offset, PVD_reserved3_size)) 811248616Smm return (0); 812228753Smm 813228753Smm /* Logical block size must be > 0. */ 814228753Smm /* I've looked at Ecma 119 and can't find any stronger 815228753Smm * restriction on this field. */ 816228753Smm logical_block_size = 817228753Smm archive_le16dec(h + PVD_logical_block_size_offset); 818228753Smm if (logical_block_size <= 0) 819228753Smm return (0); 820228753Smm 821228753Smm volume_block = 822228753Smm archive_le32dec(h + PVD_volume_space_size_offset); 823228753Smm if (volume_block <= SYSTEM_AREA_BLOCK+4) 824228753Smm return (0); 825228753Smm 826228753Smm /* File structure version must be 2 for ISO9660:1999. */ 827228753Smm if (h[PVD_file_structure_version_offset] != 2) 828228753Smm return (0); 829228753Smm 830228753Smm /* Location of Occurrence of Type L Path Table must be 831228753Smm * available location, 832228753Smm * >= SYSTEM_AREA_BLOCK(16) + 2 and < Volume Space Size. */ 833228753Smm location = archive_le32dec(h+PVD_type_1_path_table_offset); 834228753Smm if (location < SYSTEM_AREA_BLOCK+2 || location >= volume_block) 835228753Smm return (0); 836228753Smm 837228753Smm /* Location of Occurrence of Type M Path Table must be 838228753Smm * available location, 839228753Smm * >= SYSTEM_AREA_BLOCK(16) + 2 and < Volume Space Size. */ 840228753Smm location = archive_be32dec(h+PVD_type_m_path_table_offset); 841228753Smm if ((location > 0 && location < SYSTEM_AREA_BLOCK+2) 842228753Smm || location >= volume_block) 843228753Smm return (0); 844228753Smm 845228753Smm /* Reserved field must be 0. */ 846248616Smm if (!isNull(iso9660, h, PVD_reserved4_offset, PVD_reserved4_size)) 847248616Smm return (0); 848228753Smm 849228753Smm /* Reserved field must be 0. */ 850248616Smm if (!isNull(iso9660, h, PVD_reserved5_offset, PVD_reserved5_size)) 851248616Smm return (0); 852228753Smm 853228753Smm /* Read Root Directory Record in Volume Descriptor. */ 854228753Smm p = h + PVD_root_directory_record_offset; 855228753Smm if (p[DR_length_offset] != 34) 856228753Smm return (0); 857228753Smm 858228753Smm return (48); 859228753Smm} 860228753Smm 861228753Smmstatic int 862228753SmmisPVD(struct iso9660 *iso9660, const unsigned char *h) 863228753Smm{ 864228753Smm const unsigned char *p; 865228753Smm ssize_t logical_block_size; 866228753Smm int32_t volume_block; 867228753Smm int32_t location; 868228753Smm int i; 869228753Smm 870228753Smm /* Type of the Primary Volume Descriptor must be 1. */ 871228753Smm if (h[PVD_type_offset] != 1) 872228753Smm return (0); 873228753Smm 874228753Smm /* PVD version must be 1. */ 875228753Smm if (h[PVD_version_offset] != 1) 876228753Smm return (0); 877228753Smm 878228753Smm /* Reserved field must be 0. */ 879228753Smm if (h[PVD_reserved1_offset] != 0) 880228753Smm return (0); 881228753Smm 882228753Smm /* Reserved field must be 0. */ 883248616Smm if (!isNull(iso9660, h, PVD_reserved2_offset, PVD_reserved2_size)) 884248616Smm return (0); 885228753Smm 886228753Smm /* Reserved field must be 0. */ 887248616Smm if (!isNull(iso9660, h, PVD_reserved3_offset, PVD_reserved3_size)) 888248616Smm return (0); 889228753Smm 890228753Smm /* Logical block size must be > 0. */ 891228753Smm /* I've looked at Ecma 119 and can't find any stronger 892228753Smm * restriction on this field. */ 893228753Smm logical_block_size = 894228753Smm archive_le16dec(h + PVD_logical_block_size_offset); 895228753Smm if (logical_block_size <= 0) 896228753Smm return (0); 897228753Smm 898228753Smm volume_block = archive_le32dec(h + PVD_volume_space_size_offset); 899228753Smm if (volume_block <= SYSTEM_AREA_BLOCK+4) 900228753Smm return (0); 901228753Smm 902228753Smm /* File structure version must be 1 for ISO9660/ECMA119. */ 903228753Smm if (h[PVD_file_structure_version_offset] != 1) 904228753Smm return (0); 905228753Smm 906228753Smm /* Location of Occurrence of Type L Path Table must be 907228753Smm * available location, 908228753Smm * > SYSTEM_AREA_BLOCK(16) + 2 and < Volume Space Size. */ 909228753Smm location = archive_le32dec(h+PVD_type_1_path_table_offset); 910228753Smm if (location < SYSTEM_AREA_BLOCK+2 || location >= volume_block) 911228753Smm return (0); 912228753Smm 913228753Smm /* The Type M Path Table must also be at a valid location 914228753Smm * (although ECMA 119 requires a Type M Path Table, WinISO and 915228753Smm * probably other programs omit it, so we permit a zero here) 916228753Smm * 917228753Smm * >= SYSTEM_AREA_BLOCK(16) + 2 and < Volume Space Size. */ 918228753Smm location = archive_be32dec(h+PVD_type_m_path_table_offset); 919228753Smm if ((location > 0 && location < SYSTEM_AREA_BLOCK+2) 920228753Smm || location >= volume_block) 921228753Smm return (0); 922228753Smm 923228753Smm /* Reserved field must be 0. */ 924232153Smm /* But accept NetBSD/FreeBSD "makefs" images with 0x20 here. */ 925228753Smm for (i = 0; i < PVD_reserved4_size; ++i) 926232153Smm if (h[PVD_reserved4_offset + i] != 0 927232153Smm && h[PVD_reserved4_offset + i] != 0x20) 928228753Smm return (0); 929228753Smm 930228753Smm /* Reserved field must be 0. */ 931248616Smm if (!isNull(iso9660, h, PVD_reserved5_offset, PVD_reserved5_size)) 932248616Smm return (0); 933228753Smm 934228753Smm /* XXX TODO: Check other values for sanity; reject more 935228753Smm * malformed PVDs. XXX */ 936228753Smm 937228753Smm /* Read Root Directory Record in Volume Descriptor. */ 938228753Smm p = h + PVD_root_directory_record_offset; 939228753Smm if (p[DR_length_offset] != 34) 940228753Smm return (0); 941228753Smm 942232153Smm if (!iso9660->primary.location) { 943232153Smm iso9660->logical_block_size = logical_block_size; 944232153Smm iso9660->volume_block = volume_block; 945248616Smm iso9660->volume_size = 946248616Smm logical_block_size * (uint64_t)volume_block; 947248616Smm iso9660->primary.location = 948248616Smm archive_le32dec(p + DR_extent_offset); 949232153Smm iso9660->primary.size = archive_le32dec(p + DR_size_offset); 950232153Smm } 951228753Smm 952228753Smm return (48); 953228753Smm} 954228753Smm 955228753Smmstatic int 956228753Smmread_children(struct archive_read *a, struct file_info *parent) 957228753Smm{ 958228753Smm struct iso9660 *iso9660; 959228753Smm const unsigned char *b, *p; 960228753Smm struct file_info *multi; 961232153Smm size_t step, skip_size; 962228753Smm 963228753Smm iso9660 = (struct iso9660 *)(a->format->data); 964248616Smm /* flush any remaining bytes from the last round to ensure 965248616Smm * we're positioned */ 966248616Smm if (iso9660->entry_bytes_unconsumed) { 967248616Smm __archive_read_consume(a, iso9660->entry_bytes_unconsumed); 968248616Smm iso9660->entry_bytes_unconsumed = 0; 969248616Smm } 970228753Smm if (iso9660->current_position > parent->offset) { 971228753Smm archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 972228753Smm "Ignoring out-of-order directory (%s) %jd > %jd", 973228753Smm parent->name.s, 974228753Smm (intmax_t)iso9660->current_position, 975228753Smm (intmax_t)parent->offset); 976228753Smm return (ARCHIVE_WARN); 977228753Smm } 978228753Smm if (parent->offset + parent->size > iso9660->volume_size) { 979228753Smm archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 980228753Smm "Directory is beyond end-of-media: %s", 981228753Smm parent->name.s); 982228753Smm return (ARCHIVE_WARN); 983228753Smm } 984228753Smm if (iso9660->current_position < parent->offset) { 985228753Smm int64_t skipsize; 986228753Smm 987228753Smm skipsize = parent->offset - iso9660->current_position; 988232153Smm skipsize = __archive_read_consume(a, skipsize); 989228753Smm if (skipsize < 0) 990228753Smm return ((int)skipsize); 991228753Smm iso9660->current_position = parent->offset; 992228753Smm } 993228753Smm 994238856Smm step = (size_t)(((parent->size + iso9660->logical_block_size -1) / 995238856Smm iso9660->logical_block_size) * iso9660->logical_block_size); 996228753Smm b = __archive_read_ahead(a, step, NULL); 997228753Smm if (b == NULL) { 998228753Smm archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 999228753Smm "Failed to read full block when scanning " 1000228753Smm "ISO9660 directory list"); 1001228753Smm return (ARCHIVE_FATAL); 1002228753Smm } 1003228753Smm iso9660->current_position += step; 1004228753Smm multi = NULL; 1005232153Smm skip_size = step; 1006228753Smm while (step) { 1007228753Smm p = b; 1008228753Smm b += iso9660->logical_block_size; 1009228753Smm step -= iso9660->logical_block_size; 1010228753Smm for (; *p != 0 && p < b && p + *p <= b; p += *p) { 1011228753Smm struct file_info *child; 1012228753Smm 1013228753Smm /* N.B.: these special directory identifiers 1014228753Smm * are 8 bit "values" even on a 1015228753Smm * Joliet CD with UCS-2 (16bit) encoding. 1016228753Smm */ 1017228753Smm 1018228753Smm /* Skip '.' entry. */ 1019228753Smm if (*(p + DR_name_len_offset) == 1 1020228753Smm && *(p + DR_name_offset) == '\0') 1021228753Smm continue; 1022228753Smm /* Skip '..' entry. */ 1023228753Smm if (*(p + DR_name_len_offset) == 1 1024228753Smm && *(p + DR_name_offset) == '\001') 1025228753Smm continue; 1026338034Smm child = parse_file_info(a, parent, p, b - p); 1027232153Smm if (child == NULL) { 1028232153Smm __archive_read_consume(a, skip_size); 1029228753Smm return (ARCHIVE_FATAL); 1030232153Smm } 1031228753Smm if (child->cl_offset == 0 && 1032228753Smm (child->multi_extent || multi != NULL)) { 1033228753Smm struct content *con; 1034228753Smm 1035228753Smm if (multi == NULL) { 1036228753Smm multi = child; 1037228753Smm multi->contents.first = NULL; 1038228753Smm multi->contents.last = 1039228753Smm &(multi->contents.first); 1040228753Smm } 1041228753Smm con = malloc(sizeof(struct content)); 1042228753Smm if (con == NULL) { 1043228753Smm archive_set_error( 1044228753Smm &a->archive, ENOMEM, 1045232153Smm "No memory for multi extent"); 1046232153Smm __archive_read_consume(a, skip_size); 1047228753Smm return (ARCHIVE_FATAL); 1048228753Smm } 1049228753Smm con->offset = child->offset; 1050228753Smm con->size = child->size; 1051228753Smm con->next = NULL; 1052228753Smm *multi->contents.last = con; 1053228753Smm multi->contents.last = &(con->next); 1054232153Smm if (multi == child) { 1055232153Smm if (add_entry(a, iso9660, child) 1056232153Smm != ARCHIVE_OK) 1057232153Smm return (ARCHIVE_FATAL); 1058232153Smm } else { 1059228753Smm multi->size += child->size; 1060228753Smm if (!child->multi_extent) 1061228753Smm multi = NULL; 1062228753Smm } 1063228753Smm } else 1064232153Smm if (add_entry(a, iso9660, child) != ARCHIVE_OK) 1065232153Smm return (ARCHIVE_FATAL); 1066228753Smm } 1067228753Smm } 1068228753Smm 1069232153Smm __archive_read_consume(a, skip_size); 1070232153Smm 1071228753Smm /* Read data which recorded by RRIP "CE" extension. */ 1072228753Smm if (read_CE(a, iso9660) != ARCHIVE_OK) 1073228753Smm return (ARCHIVE_FATAL); 1074228753Smm 1075228753Smm return (ARCHIVE_OK); 1076228753Smm} 1077228753Smm 1078228753Smmstatic int 1079248616Smmchoose_volume(struct archive_read *a, struct iso9660 *iso9660) 1080228753Smm{ 1081228753Smm struct file_info *file; 1082248616Smm int64_t skipsize; 1083248616Smm struct vd *vd; 1084248616Smm const void *block; 1085248616Smm char seenJoliet; 1086228753Smm 1087248616Smm vd = &(iso9660->primary); 1088248616Smm if (!iso9660->opt_support_joliet) 1089248616Smm iso9660->seenJoliet = 0; 1090248616Smm if (iso9660->seenJoliet && 1091248616Smm vd->location > iso9660->joliet.location) 1092248616Smm /* This condition is unlikely; by way of caution. */ 1093248616Smm vd = &(iso9660->joliet); 1094228753Smm 1095302295Smm skipsize = LOGICAL_BLOCK_SIZE * (int64_t)vd->location; 1096248616Smm skipsize = __archive_read_consume(a, skipsize); 1097248616Smm if (skipsize < 0) 1098248616Smm return ((int)skipsize); 1099248616Smm iso9660->current_position = skipsize; 1100248616Smm 1101248616Smm block = __archive_read_ahead(a, vd->size, NULL); 1102248616Smm if (block == NULL) { 1103248616Smm archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 1104248616Smm "Failed to read full block when scanning " 1105248616Smm "ISO9660 directory list"); 1106248616Smm return (ARCHIVE_FATAL); 1107228753Smm } 1108228753Smm 1109248616Smm /* 1110248616Smm * While reading Root Directory, flag seenJoliet must be zero to 1111248616Smm * avoid converting special name 0x00(Current Directory) and 1112248616Smm * next byte to UCS2. 1113248616Smm */ 1114248616Smm seenJoliet = iso9660->seenJoliet;/* Save flag. */ 1115248616Smm iso9660->seenJoliet = 0; 1116338034Smm file = parse_file_info(a, NULL, block, vd->size); 1117248616Smm if (file == NULL) 1118248616Smm return (ARCHIVE_FATAL); 1119248616Smm iso9660->seenJoliet = seenJoliet; 1120228753Smm 1121248616Smm /* 1122248616Smm * If the iso image has both RockRidge and Joliet, we preferentially 1123248616Smm * use RockRidge Extensions rather than Joliet ones. 1124248616Smm */ 1125248616Smm if (vd == &(iso9660->primary) && iso9660->seenRockridge 1126248616Smm && iso9660->seenJoliet) 1127248616Smm iso9660->seenJoliet = 0; 1128228753Smm 1129248616Smm if (vd == &(iso9660->primary) && !iso9660->seenRockridge 1130248616Smm && iso9660->seenJoliet) { 1131248616Smm /* Switch reading data from primary to joliet. */ 1132248616Smm vd = &(iso9660->joliet); 1133302295Smm skipsize = LOGICAL_BLOCK_SIZE * (int64_t)vd->location; 1134248616Smm skipsize -= iso9660->current_position; 1135232153Smm skipsize = __archive_read_consume(a, skipsize); 1136228753Smm if (skipsize < 0) 1137228753Smm return ((int)skipsize); 1138248616Smm iso9660->current_position += skipsize; 1139228753Smm 1140228753Smm block = __archive_read_ahead(a, vd->size, NULL); 1141228753Smm if (block == NULL) { 1142248616Smm archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 1143228753Smm "Failed to read full block when scanning " 1144228753Smm "ISO9660 directory list"); 1145228753Smm return (ARCHIVE_FATAL); 1146228753Smm } 1147228753Smm iso9660->seenJoliet = 0; 1148338034Smm file = parse_file_info(a, NULL, block, vd->size); 1149228753Smm if (file == NULL) 1150228753Smm return (ARCHIVE_FATAL); 1151228753Smm iso9660->seenJoliet = seenJoliet; 1152248616Smm } 1153228753Smm 1154248616Smm /* Store the root directory in the pending list. */ 1155248616Smm if (add_entry(a, iso9660, file) != ARCHIVE_OK) 1156248616Smm return (ARCHIVE_FATAL); 1157248616Smm if (iso9660->seenRockridge) { 1158248616Smm a->archive.archive_format = ARCHIVE_FORMAT_ISO9660_ROCKRIDGE; 1159248616Smm a->archive.archive_format_name = 1160248616Smm "ISO9660 with Rockridge extensions"; 1161228753Smm } 1162228753Smm 1163248616Smm return (ARCHIVE_OK); 1164248616Smm} 1165248616Smm 1166248616Smmstatic int 1167248616Smmarchive_read_format_iso9660_read_header(struct archive_read *a, 1168248616Smm struct archive_entry *entry) 1169248616Smm{ 1170248616Smm struct iso9660 *iso9660; 1171248616Smm struct file_info *file; 1172248616Smm int r, rd_r = ARCHIVE_OK; 1173248616Smm 1174248616Smm iso9660 = (struct iso9660 *)(a->format->data); 1175248616Smm 1176248616Smm if (!a->archive.archive_format) { 1177248616Smm a->archive.archive_format = ARCHIVE_FORMAT_ISO9660; 1178248616Smm a->archive.archive_format_name = "ISO9660"; 1179248616Smm } 1180248616Smm 1181248616Smm if (iso9660->current_position == 0) { 1182248616Smm r = choose_volume(a, iso9660); 1183248616Smm if (r != ARCHIVE_OK) 1184248616Smm return (r); 1185248616Smm } 1186248616Smm 1187232153Smm file = NULL;/* Eliminate a warning. */ 1188228753Smm /* Get the next entry that appears after the current offset. */ 1189228753Smm r = next_entry_seek(a, iso9660, &file); 1190228753Smm if (r != ARCHIVE_OK) 1191228753Smm return (r); 1192228753Smm 1193232153Smm if (iso9660->seenJoliet) { 1194232153Smm /* 1195232153Smm * Convert UTF-16BE of a filename to local locale MBS 1196232153Smm * and store the result into a filename field. 1197232153Smm */ 1198232153Smm if (iso9660->sconv_utf16be == NULL) { 1199232153Smm iso9660->sconv_utf16be = 1200232153Smm archive_string_conversion_from_charset( 1201232153Smm &(a->archive), "UTF-16BE", 1); 1202232153Smm if (iso9660->sconv_utf16be == NULL) 1203313571Smm /* Couldn't allocate memory */ 1204232153Smm return (ARCHIVE_FATAL); 1205232153Smm } 1206232153Smm if (iso9660->utf16be_path == NULL) { 1207232153Smm iso9660->utf16be_path = malloc(UTF16_NAME_MAX); 1208232153Smm if (iso9660->utf16be_path == NULL) { 1209232153Smm archive_set_error(&a->archive, ENOMEM, 1210232153Smm "No memory"); 1211232153Smm return (ARCHIVE_FATAL); 1212232153Smm } 1213232153Smm } 1214232153Smm if (iso9660->utf16be_previous_path == NULL) { 1215232153Smm iso9660->utf16be_previous_path = malloc(UTF16_NAME_MAX); 1216232153Smm if (iso9660->utf16be_previous_path == NULL) { 1217232153Smm archive_set_error(&a->archive, ENOMEM, 1218232153Smm "No memory"); 1219232153Smm return (ARCHIVE_FATAL); 1220232153Smm } 1221232153Smm } 1222232153Smm 1223232153Smm iso9660->utf16be_path_len = 0; 1224232153Smm if (build_pathname_utf16be(iso9660->utf16be_path, 1225232153Smm UTF16_NAME_MAX, &(iso9660->utf16be_path_len), file) != 0) { 1226232153Smm archive_set_error(&a->archive, 1227232153Smm ARCHIVE_ERRNO_FILE_FORMAT, 1228232153Smm "Pathname is too long"); 1229302001Smm return (ARCHIVE_FATAL); 1230232153Smm } 1231232153Smm 1232232153Smm r = archive_entry_copy_pathname_l(entry, 1233232153Smm (const char *)iso9660->utf16be_path, 1234232153Smm iso9660->utf16be_path_len, 1235232153Smm iso9660->sconv_utf16be); 1236232153Smm if (r != 0) { 1237232153Smm if (errno == ENOMEM) { 1238232153Smm archive_set_error(&a->archive, ENOMEM, 1239232153Smm "No memory for Pathname"); 1240232153Smm return (ARCHIVE_FATAL); 1241232153Smm } 1242232153Smm archive_set_error(&a->archive, 1243232153Smm ARCHIVE_ERRNO_FILE_FORMAT, 1244232153Smm "Pathname cannot be converted " 1245232153Smm "from %s to current locale.", 1246232153Smm archive_string_conversion_charset_name( 1247232153Smm iso9660->sconv_utf16be)); 1248232153Smm 1249232153Smm rd_r = ARCHIVE_WARN; 1250232153Smm } 1251232153Smm } else { 1252302001Smm const char *path = build_pathname(&iso9660->pathname, file, 0); 1253302001Smm if (path == NULL) { 1254302001Smm archive_set_error(&a->archive, 1255302001Smm ARCHIVE_ERRNO_FILE_FORMAT, 1256302001Smm "Pathname is too long"); 1257302001Smm return (ARCHIVE_FATAL); 1258302001Smm } else { 1259302001Smm archive_string_empty(&iso9660->pathname); 1260302001Smm archive_entry_set_pathname(entry, path); 1261302001Smm } 1262232153Smm } 1263232153Smm 1264228753Smm iso9660->entry_bytes_remaining = file->size; 1265248616Smm /* Offset for sparse-file-aware clients. */ 1266248616Smm iso9660->entry_sparse_offset = 0; 1267228753Smm 1268228753Smm if (file->offset + file->size > iso9660->volume_size) { 1269228753Smm archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 1270232153Smm "File is beyond end-of-media: %s", 1271232153Smm archive_entry_pathname(entry)); 1272228753Smm iso9660->entry_bytes_remaining = 0; 1273228753Smm return (ARCHIVE_WARN); 1274228753Smm } 1275228753Smm 1276228753Smm /* Set up the entry structure with information about this entry. */ 1277228753Smm archive_entry_set_mode(entry, file->mode); 1278228753Smm archive_entry_set_uid(entry, file->uid); 1279228753Smm archive_entry_set_gid(entry, file->gid); 1280228753Smm archive_entry_set_nlink(entry, file->nlinks); 1281228753Smm if (file->birthtime_is_set) 1282228753Smm archive_entry_set_birthtime(entry, file->birthtime, 0); 1283228753Smm else 1284228753Smm archive_entry_unset_birthtime(entry); 1285228753Smm archive_entry_set_mtime(entry, file->mtime, 0); 1286228753Smm archive_entry_set_ctime(entry, file->ctime, 0); 1287228753Smm archive_entry_set_atime(entry, file->atime, 0); 1288228753Smm /* N.B.: Rock Ridge supports 64-bit device numbers. */ 1289228753Smm archive_entry_set_rdev(entry, (dev_t)file->rdev); 1290228753Smm archive_entry_set_size(entry, iso9660->entry_bytes_remaining); 1291228753Smm if (file->symlink.s != NULL) 1292228753Smm archive_entry_copy_symlink(entry, file->symlink.s); 1293228753Smm 1294228753Smm /* Note: If the input isn't seekable, we can't rewind to 1295228753Smm * return the same body again, so if the next entry refers to 1296228753Smm * the same data, we have to return it as a hardlink to the 1297228753Smm * original entry. */ 1298228753Smm if (file->number != -1 && 1299228753Smm file->number == iso9660->previous_number) { 1300232153Smm if (iso9660->seenJoliet) { 1301232153Smm r = archive_entry_copy_hardlink_l(entry, 1302232153Smm (const char *)iso9660->utf16be_previous_path, 1303232153Smm iso9660->utf16be_previous_path_len, 1304232153Smm iso9660->sconv_utf16be); 1305232153Smm if (r != 0) { 1306232153Smm if (errno == ENOMEM) { 1307232153Smm archive_set_error(&a->archive, ENOMEM, 1308232153Smm "No memory for Linkname"); 1309232153Smm return (ARCHIVE_FATAL); 1310232153Smm } 1311232153Smm archive_set_error(&a->archive, 1312232153Smm ARCHIVE_ERRNO_FILE_FORMAT, 1313232153Smm "Linkname cannot be converted " 1314232153Smm "from %s to current locale.", 1315232153Smm archive_string_conversion_charset_name( 1316232153Smm iso9660->sconv_utf16be)); 1317232153Smm rd_r = ARCHIVE_WARN; 1318232153Smm } 1319232153Smm } else 1320232153Smm archive_entry_set_hardlink(entry, 1321232153Smm iso9660->previous_pathname.s); 1322228753Smm archive_entry_unset_size(entry); 1323228753Smm iso9660->entry_bytes_remaining = 0; 1324232153Smm return (rd_r); 1325228753Smm } 1326228753Smm 1327228753Smm if ((file->mode & AE_IFMT) != AE_IFDIR && 1328228753Smm file->offset < iso9660->current_position) { 1329248616Smm int64_t r64; 1330248616Smm 1331248616Smm r64 = __archive_read_seek(a, file->offset, SEEK_SET); 1332248616Smm if (r64 != (int64_t)file->offset) { 1333248616Smm /* We can't seek backwards to extract it, so issue 1334248616Smm * a warning. Note that this can only happen if 1335248616Smm * this entry was added to the heap after we passed 1336248616Smm * this offset, that is, only if the directory 1337248616Smm * mentioning this entry is later than the body of 1338248616Smm * the entry. Such layouts are very unusual; most 1339248616Smm * ISO9660 writers lay out and record all directory 1340248616Smm * information first, then store all file bodies. */ 1341248616Smm archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 1342248616Smm "Ignoring out-of-order file @%jx (%s) %jd < %jd", 1343248616Smm (intmax_t)file->number, 1344248616Smm iso9660->pathname.s, 1345248616Smm (intmax_t)file->offset, 1346248616Smm (intmax_t)iso9660->current_position); 1347248616Smm iso9660->entry_bytes_remaining = 0; 1348248616Smm return (ARCHIVE_WARN); 1349248616Smm } 1350248616Smm iso9660->current_position = (uint64_t)r64; 1351228753Smm } 1352228753Smm 1353228753Smm /* Initialize zisofs variables. */ 1354228753Smm iso9660->entry_zisofs.pz = file->pz; 1355228753Smm if (file->pz) { 1356228753Smm#ifdef HAVE_ZLIB_H 1357228753Smm struct zisofs *zisofs; 1358228753Smm 1359228753Smm zisofs = &iso9660->entry_zisofs; 1360228753Smm zisofs->initialized = 0; 1361228753Smm zisofs->pz_log2_bs = file->pz_log2_bs; 1362228753Smm zisofs->pz_uncompressed_size = file->pz_uncompressed_size; 1363228753Smm zisofs->pz_offset = 0; 1364228753Smm zisofs->header_avail = 0; 1365228753Smm zisofs->header_passed = 0; 1366228753Smm zisofs->block_pointers_avail = 0; 1367228753Smm#endif 1368228753Smm archive_entry_set_size(entry, file->pz_uncompressed_size); 1369228753Smm } 1370228753Smm 1371228753Smm iso9660->previous_number = file->number; 1372232153Smm if (iso9660->seenJoliet) { 1373232153Smm memcpy(iso9660->utf16be_previous_path, iso9660->utf16be_path, 1374232153Smm iso9660->utf16be_path_len); 1375232153Smm iso9660->utf16be_previous_path_len = iso9660->utf16be_path_len; 1376232153Smm } else 1377232153Smm archive_strcpy( 1378232153Smm &iso9660->previous_pathname, iso9660->pathname.s); 1379228753Smm 1380228753Smm /* Reset entry_bytes_remaining if the file is multi extent. */ 1381228753Smm iso9660->entry_content = file->contents.first; 1382228753Smm if (iso9660->entry_content != NULL) 1383228753Smm iso9660->entry_bytes_remaining = iso9660->entry_content->size; 1384228753Smm 1385228753Smm if (archive_entry_filetype(entry) == AE_IFDIR) { 1386228753Smm /* Overwrite nlinks by proper link number which is 1387228753Smm * calculated from number of sub directories. */ 1388228753Smm archive_entry_set_nlink(entry, 2 + file->subdirs); 1389228753Smm /* Directory data has been read completely. */ 1390228753Smm iso9660->entry_bytes_remaining = 0; 1391228753Smm } 1392228753Smm 1393228753Smm if (rd_r != ARCHIVE_OK) 1394228753Smm return (rd_r); 1395228753Smm return (ARCHIVE_OK); 1396228753Smm} 1397228753Smm 1398228753Smmstatic int 1399228753Smmarchive_read_format_iso9660_read_data_skip(struct archive_read *a) 1400228753Smm{ 1401228753Smm /* Because read_next_header always does an explicit skip 1402228753Smm * to the next entry, we don't need to do anything here. */ 1403228753Smm (void)a; /* UNUSED */ 1404228753Smm return (ARCHIVE_OK); 1405228753Smm} 1406228753Smm 1407228753Smm#ifdef HAVE_ZLIB_H 1408228753Smm 1409228753Smmstatic int 1410228753Smmzisofs_read_data(struct archive_read *a, 1411232153Smm const void **buff, size_t *size, int64_t *offset) 1412228753Smm{ 1413228753Smm struct iso9660 *iso9660; 1414228753Smm struct zisofs *zisofs; 1415228753Smm const unsigned char *p; 1416228753Smm size_t avail; 1417228753Smm ssize_t bytes_read; 1418228753Smm size_t uncompressed_size; 1419228753Smm int r; 1420228753Smm 1421228753Smm iso9660 = (struct iso9660 *)(a->format->data); 1422228753Smm zisofs = &iso9660->entry_zisofs; 1423228753Smm 1424228753Smm p = __archive_read_ahead(a, 1, &bytes_read); 1425228753Smm if (bytes_read <= 0) { 1426228753Smm archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, 1427228753Smm "Truncated zisofs file body"); 1428228753Smm return (ARCHIVE_FATAL); 1429228753Smm } 1430228753Smm if (bytes_read > iso9660->entry_bytes_remaining) 1431238856Smm bytes_read = (ssize_t)iso9660->entry_bytes_remaining; 1432228753Smm avail = bytes_read; 1433228753Smm uncompressed_size = 0; 1434228753Smm 1435228753Smm if (!zisofs->initialized) { 1436228753Smm size_t ceil, xsize; 1437228753Smm 1438228753Smm /* Allocate block pointers buffer. */ 1439238856Smm ceil = (size_t)((zisofs->pz_uncompressed_size + 1440238856Smm (((int64_t)1) << zisofs->pz_log2_bs) - 1) 1441238856Smm >> zisofs->pz_log2_bs); 1442228753Smm xsize = (ceil + 1) * 4; 1443228753Smm if (zisofs->block_pointers_alloc < xsize) { 1444228753Smm size_t alloc; 1445228753Smm 1446228753Smm if (zisofs->block_pointers != NULL) 1447228753Smm free(zisofs->block_pointers); 1448228753Smm alloc = ((xsize >> 10) + 1) << 10; 1449228753Smm zisofs->block_pointers = malloc(alloc); 1450228753Smm if (zisofs->block_pointers == NULL) { 1451228753Smm archive_set_error(&a->archive, ENOMEM, 1452228753Smm "No memory for zisofs decompression"); 1453228753Smm return (ARCHIVE_FATAL); 1454228753Smm } 1455228753Smm zisofs->block_pointers_alloc = alloc; 1456228753Smm } 1457228753Smm zisofs->block_pointers_size = xsize; 1458228753Smm 1459228753Smm /* Allocate uncompressed data buffer. */ 1460248616Smm xsize = (size_t)1UL << zisofs->pz_log2_bs; 1461228753Smm if (zisofs->uncompressed_buffer_size < xsize) { 1462228753Smm if (zisofs->uncompressed_buffer != NULL) 1463228753Smm free(zisofs->uncompressed_buffer); 1464228753Smm zisofs->uncompressed_buffer = malloc(xsize); 1465228753Smm if (zisofs->uncompressed_buffer == NULL) { 1466228753Smm archive_set_error(&a->archive, ENOMEM, 1467228753Smm "No memory for zisofs decompression"); 1468228753Smm return (ARCHIVE_FATAL); 1469228753Smm } 1470228753Smm } 1471228753Smm zisofs->uncompressed_buffer_size = xsize; 1472228753Smm 1473228753Smm /* 1474228753Smm * Read the file header, and check the magic code of zisofs. 1475228753Smm */ 1476228753Smm if (zisofs->header_avail < sizeof(zisofs->header)) { 1477228753Smm xsize = sizeof(zisofs->header) - zisofs->header_avail; 1478228753Smm if (avail < xsize) 1479228753Smm xsize = avail; 1480228753Smm memcpy(zisofs->header + zisofs->header_avail, p, xsize); 1481228753Smm zisofs->header_avail += xsize; 1482228753Smm avail -= xsize; 1483228753Smm p += xsize; 1484228753Smm } 1485228753Smm if (!zisofs->header_passed && 1486228753Smm zisofs->header_avail == sizeof(zisofs->header)) { 1487228753Smm int err = 0; 1488228753Smm 1489228753Smm if (memcmp(zisofs->header, zisofs_magic, 1490228753Smm sizeof(zisofs_magic)) != 0) 1491228753Smm err = 1; 1492228753Smm if (archive_le32dec(zisofs->header + 8) 1493228753Smm != zisofs->pz_uncompressed_size) 1494228753Smm err = 1; 1495228753Smm if (zisofs->header[12] != 4) 1496228753Smm err = 1; 1497228753Smm if (zisofs->header[13] != zisofs->pz_log2_bs) 1498228753Smm err = 1; 1499228753Smm if (err) { 1500228753Smm archive_set_error(&a->archive, 1501228753Smm ARCHIVE_ERRNO_FILE_FORMAT, 1502228753Smm "Illegal zisofs file body"); 1503228753Smm return (ARCHIVE_FATAL); 1504228753Smm } 1505228753Smm zisofs->header_passed = 1; 1506228753Smm } 1507228753Smm /* 1508228753Smm * Read block pointers. 1509228753Smm */ 1510228753Smm if (zisofs->header_passed && 1511228753Smm zisofs->block_pointers_avail < zisofs->block_pointers_size) { 1512228753Smm xsize = zisofs->block_pointers_size 1513228753Smm - zisofs->block_pointers_avail; 1514228753Smm if (avail < xsize) 1515228753Smm xsize = avail; 1516228753Smm memcpy(zisofs->block_pointers 1517228753Smm + zisofs->block_pointers_avail, p, xsize); 1518228753Smm zisofs->block_pointers_avail += xsize; 1519228753Smm avail -= xsize; 1520228753Smm p += xsize; 1521228753Smm if (zisofs->block_pointers_avail 1522228753Smm == zisofs->block_pointers_size) { 1523228753Smm /* We've got all block pointers and initialize 1524228753Smm * related variables. */ 1525228753Smm zisofs->block_off = 0; 1526228753Smm zisofs->block_avail = 0; 1527228753Smm /* Complete a initialization */ 1528228753Smm zisofs->initialized = 1; 1529228753Smm } 1530228753Smm } 1531228753Smm 1532228753Smm if (!zisofs->initialized) 1533232153Smm goto next_data; /* We need more data. */ 1534228753Smm } 1535228753Smm 1536228753Smm /* 1537228753Smm * Get block offsets from block pointers. 1538228753Smm */ 1539228753Smm if (zisofs->block_avail == 0) { 1540228753Smm uint32_t bst, bed; 1541228753Smm 1542228753Smm if (zisofs->block_off + 4 >= zisofs->block_pointers_size) { 1543228753Smm /* There isn't a pair of offsets. */ 1544232153Smm archive_set_error(&a->archive, 1545232153Smm ARCHIVE_ERRNO_FILE_FORMAT, 1546228753Smm "Illegal zisofs block pointers"); 1547228753Smm return (ARCHIVE_FATAL); 1548228753Smm } 1549232153Smm bst = archive_le32dec( 1550232153Smm zisofs->block_pointers + zisofs->block_off); 1551228753Smm if (bst != zisofs->pz_offset + (bytes_read - avail)) { 1552232153Smm /* TODO: Should we seek offset of current file 1553232153Smm * by bst ? */ 1554232153Smm archive_set_error(&a->archive, 1555232153Smm ARCHIVE_ERRNO_FILE_FORMAT, 1556228753Smm "Illegal zisofs block pointers(cannot seek)"); 1557228753Smm return (ARCHIVE_FATAL); 1558228753Smm } 1559228753Smm bed = archive_le32dec( 1560228753Smm zisofs->block_pointers + zisofs->block_off + 4); 1561228753Smm if (bed < bst) { 1562232153Smm archive_set_error(&a->archive, 1563232153Smm ARCHIVE_ERRNO_FILE_FORMAT, 1564228753Smm "Illegal zisofs block pointers"); 1565228753Smm return (ARCHIVE_FATAL); 1566228753Smm } 1567228753Smm zisofs->block_avail = bed - bst; 1568228753Smm zisofs->block_off += 4; 1569228753Smm 1570228753Smm /* Initialize compression library for new block. */ 1571228753Smm if (zisofs->stream_valid) 1572228753Smm r = inflateReset(&zisofs->stream); 1573228753Smm else 1574228753Smm r = inflateInit(&zisofs->stream); 1575228753Smm if (r != Z_OK) { 1576228753Smm archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 1577228753Smm "Can't initialize zisofs decompression."); 1578228753Smm return (ARCHIVE_FATAL); 1579228753Smm } 1580228753Smm zisofs->stream_valid = 1; 1581228753Smm zisofs->stream.total_in = 0; 1582228753Smm zisofs->stream.total_out = 0; 1583228753Smm } 1584228753Smm 1585228753Smm /* 1586232153Smm * Make uncompressed data. 1587228753Smm */ 1588228753Smm if (zisofs->block_avail == 0) { 1589228753Smm memset(zisofs->uncompressed_buffer, 0, 1590228753Smm zisofs->uncompressed_buffer_size); 1591228753Smm uncompressed_size = zisofs->uncompressed_buffer_size; 1592228753Smm } else { 1593228753Smm zisofs->stream.next_in = (Bytef *)(uintptr_t)(const void *)p; 1594228753Smm if (avail > zisofs->block_avail) 1595228753Smm zisofs->stream.avail_in = zisofs->block_avail; 1596228753Smm else 1597248616Smm zisofs->stream.avail_in = (uInt)avail; 1598228753Smm zisofs->stream.next_out = zisofs->uncompressed_buffer; 1599248616Smm zisofs->stream.avail_out = 1600248616Smm (uInt)zisofs->uncompressed_buffer_size; 1601228753Smm 1602228753Smm r = inflate(&zisofs->stream, 0); 1603228753Smm switch (r) { 1604228753Smm case Z_OK: /* Decompressor made some progress.*/ 1605228753Smm case Z_STREAM_END: /* Found end of stream. */ 1606228753Smm break; 1607228753Smm default: 1608228753Smm archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 1609228753Smm "zisofs decompression failed (%d)", r); 1610228753Smm return (ARCHIVE_FATAL); 1611228753Smm } 1612228753Smm uncompressed_size = 1613228753Smm zisofs->uncompressed_buffer_size - zisofs->stream.avail_out; 1614228753Smm avail -= zisofs->stream.next_in - p; 1615248616Smm zisofs->block_avail -= (uint32_t)(zisofs->stream.next_in - p); 1616228753Smm } 1617228753Smmnext_data: 1618228753Smm bytes_read -= avail; 1619228753Smm *buff = zisofs->uncompressed_buffer; 1620228753Smm *size = uncompressed_size; 1621228753Smm *offset = iso9660->entry_sparse_offset; 1622228753Smm iso9660->entry_sparse_offset += uncompressed_size; 1623228753Smm iso9660->entry_bytes_remaining -= bytes_read; 1624228753Smm iso9660->current_position += bytes_read; 1625248616Smm zisofs->pz_offset += (uint32_t)bytes_read; 1626232153Smm iso9660->entry_bytes_unconsumed += bytes_read; 1627228753Smm 1628228753Smm return (ARCHIVE_OK); 1629228753Smm} 1630228753Smm 1631228753Smm#else /* HAVE_ZLIB_H */ 1632228753Smm 1633228753Smmstatic int 1634228753Smmzisofs_read_data(struct archive_read *a, 1635232153Smm const void **buff, size_t *size, int64_t *offset) 1636228753Smm{ 1637228753Smm 1638228753Smm (void)buff;/* UNUSED */ 1639228753Smm (void)size;/* UNUSED */ 1640228753Smm (void)offset;/* UNUSED */ 1641228753Smm archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, 1642228753Smm "zisofs is not supported on this platform."); 1643228753Smm return (ARCHIVE_FAILED); 1644228753Smm} 1645228753Smm 1646228753Smm#endif /* HAVE_ZLIB_H */ 1647228753Smm 1648228753Smmstatic int 1649228753Smmarchive_read_format_iso9660_read_data(struct archive_read *a, 1650232153Smm const void **buff, size_t *size, int64_t *offset) 1651228753Smm{ 1652228753Smm ssize_t bytes_read; 1653228753Smm struct iso9660 *iso9660; 1654228753Smm 1655228753Smm iso9660 = (struct iso9660 *)(a->format->data); 1656232153Smm 1657232153Smm if (iso9660->entry_bytes_unconsumed) { 1658232153Smm __archive_read_consume(a, iso9660->entry_bytes_unconsumed); 1659232153Smm iso9660->entry_bytes_unconsumed = 0; 1660232153Smm } 1661232153Smm 1662228753Smm if (iso9660->entry_bytes_remaining <= 0) { 1663228753Smm if (iso9660->entry_content != NULL) 1664228753Smm iso9660->entry_content = iso9660->entry_content->next; 1665228753Smm if (iso9660->entry_content == NULL) { 1666228753Smm *buff = NULL; 1667228753Smm *size = 0; 1668228753Smm *offset = iso9660->entry_sparse_offset; 1669228753Smm return (ARCHIVE_EOF); 1670228753Smm } 1671228753Smm /* Seek forward to the start of the entry. */ 1672228753Smm if (iso9660->current_position < iso9660->entry_content->offset) { 1673228753Smm int64_t step; 1674228753Smm 1675228753Smm step = iso9660->entry_content->offset - 1676228753Smm iso9660->current_position; 1677232153Smm step = __archive_read_consume(a, step); 1678228753Smm if (step < 0) 1679228753Smm return ((int)step); 1680228753Smm iso9660->current_position = 1681228753Smm iso9660->entry_content->offset; 1682228753Smm } 1683228753Smm if (iso9660->entry_content->offset < iso9660->current_position) { 1684228753Smm archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 1685228753Smm "Ignoring out-of-order file (%s) %jd < %jd", 1686228753Smm iso9660->pathname.s, 1687228753Smm (intmax_t)iso9660->entry_content->offset, 1688228753Smm (intmax_t)iso9660->current_position); 1689228753Smm *buff = NULL; 1690228753Smm *size = 0; 1691228753Smm *offset = iso9660->entry_sparse_offset; 1692228753Smm return (ARCHIVE_WARN); 1693228753Smm } 1694228753Smm iso9660->entry_bytes_remaining = iso9660->entry_content->size; 1695228753Smm } 1696228753Smm if (iso9660->entry_zisofs.pz) 1697228753Smm return (zisofs_read_data(a, buff, size, offset)); 1698228753Smm 1699228753Smm *buff = __archive_read_ahead(a, 1, &bytes_read); 1700228753Smm if (bytes_read == 0) 1701228753Smm archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 1702228753Smm "Truncated input file"); 1703228753Smm if (*buff == NULL) 1704228753Smm return (ARCHIVE_FATAL); 1705228753Smm if (bytes_read > iso9660->entry_bytes_remaining) 1706238856Smm bytes_read = (ssize_t)iso9660->entry_bytes_remaining; 1707228753Smm *size = bytes_read; 1708228753Smm *offset = iso9660->entry_sparse_offset; 1709228753Smm iso9660->entry_sparse_offset += bytes_read; 1710228753Smm iso9660->entry_bytes_remaining -= bytes_read; 1711232153Smm iso9660->entry_bytes_unconsumed = bytes_read; 1712228753Smm iso9660->current_position += bytes_read; 1713228753Smm return (ARCHIVE_OK); 1714228753Smm} 1715228753Smm 1716228753Smmstatic int 1717228753Smmarchive_read_format_iso9660_cleanup(struct archive_read *a) 1718228753Smm{ 1719228753Smm struct iso9660 *iso9660; 1720228753Smm int r = ARCHIVE_OK; 1721228753Smm 1722228753Smm iso9660 = (struct iso9660 *)(a->format->data); 1723228753Smm release_files(iso9660); 1724228753Smm free(iso9660->read_ce_req.reqs); 1725228753Smm archive_string_free(&iso9660->pathname); 1726228753Smm archive_string_free(&iso9660->previous_pathname); 1727344674Smm free(iso9660->pending_files.files); 1728228753Smm#ifdef HAVE_ZLIB_H 1729228753Smm free(iso9660->entry_zisofs.uncompressed_buffer); 1730228753Smm free(iso9660->entry_zisofs.block_pointers); 1731228753Smm if (iso9660->entry_zisofs.stream_valid) { 1732228753Smm if (inflateEnd(&iso9660->entry_zisofs.stream) != Z_OK) { 1733228753Smm archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 1734228753Smm "Failed to clean up zlib decompressor"); 1735228753Smm r = ARCHIVE_FATAL; 1736228753Smm } 1737228753Smm } 1738228753Smm#endif 1739232153Smm free(iso9660->utf16be_path); 1740232153Smm free(iso9660->utf16be_previous_path); 1741228753Smm free(iso9660); 1742228753Smm (a->format->data) = NULL; 1743228753Smm return (r); 1744228753Smm} 1745228753Smm 1746228753Smm/* 1747228753Smm * This routine parses a single ISO directory record, makes sense 1748228753Smm * of any extensions, and stores the result in memory. 1749228753Smm */ 1750228753Smmstatic struct file_info * 1751228753Smmparse_file_info(struct archive_read *a, struct file_info *parent, 1752338034Smm const unsigned char *isodirrec, size_t reclen) 1753228753Smm{ 1754228753Smm struct iso9660 *iso9660; 1755302001Smm struct file_info *file, *filep; 1756228753Smm size_t name_len; 1757228753Smm const unsigned char *rr_start, *rr_end; 1758228753Smm const unsigned char *p; 1759228753Smm size_t dr_len; 1760302001Smm uint64_t fsize, offset; 1761228753Smm int32_t location; 1762228753Smm int flags; 1763228753Smm 1764228753Smm iso9660 = (struct iso9660 *)(a->format->data); 1765228753Smm 1766338034Smm if (reclen != 0) 1767338034Smm dr_len = (size_t)isodirrec[DR_length_offset]; 1768338034Smm /* 1769338034Smm * Sanity check that reclen is not zero and dr_len is greater than 1770338034Smm * reclen but at least 34 1771338034Smm */ 1772338034Smm if (reclen == 0 || reclen < dr_len || dr_len < 34) { 1773338034Smm archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 1774338034Smm "Invalid length of directory record"); 1775338034Smm return (NULL); 1776338034Smm } 1777228753Smm name_len = (size_t)isodirrec[DR_name_len_offset]; 1778228753Smm location = archive_le32dec(isodirrec + DR_extent_offset); 1779228753Smm fsize = toi(isodirrec + DR_size_offset, DR_size_size); 1780228753Smm /* Sanity check that name_len doesn't exceed dr_len. */ 1781228753Smm if (dr_len - 33 < name_len || name_len == 0) { 1782228753Smm archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 1783228753Smm "Invalid length of file identifier"); 1784228753Smm return (NULL); 1785228753Smm } 1786228753Smm /* Sanity check that location doesn't exceed volume block. 1787228753Smm * Don't check lower limit of location; it's possibility 1788228753Smm * the location has negative value when file type is symbolic 1789228753Smm * link or file size is zero. As far as I know latest mkisofs 1790228753Smm * do that. 1791228753Smm */ 1792228753Smm if (location > 0 && 1793228753Smm (location + ((fsize + iso9660->logical_block_size -1) 1794228772Smm / iso9660->logical_block_size)) 1795232153Smm > (uint32_t)iso9660->volume_block) { 1796228753Smm archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 1797228753Smm "Invalid location of extent of file"); 1798228753Smm return (NULL); 1799228753Smm } 1800232153Smm /* Sanity check that location doesn't have a negative value 1801232153Smm * when the file is not empty. it's too large. */ 1802232153Smm if (fsize != 0 && location < 0) { 1803232153Smm archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 1804232153Smm "Invalid location of extent of file"); 1805232153Smm return (NULL); 1806232153Smm } 1807228753Smm 1808302001Smm /* Sanity check that this entry does not create a cycle. */ 1809302001Smm offset = iso9660->logical_block_size * (uint64_t)location; 1810302001Smm for (filep = parent; filep != NULL; filep = filep->parent) { 1811302001Smm if (filep->offset == offset) { 1812302001Smm archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, 1813302001Smm "Directory structure contains loop"); 1814302001Smm return (NULL); 1815302001Smm } 1816302001Smm } 1817302001Smm 1818228753Smm /* Create a new file entry and copy data from the ISO dir record. */ 1819232153Smm file = (struct file_info *)calloc(1, sizeof(*file)); 1820228753Smm if (file == NULL) { 1821228753Smm archive_set_error(&a->archive, ENOMEM, 1822228753Smm "No memory for file entry"); 1823228753Smm return (NULL); 1824228753Smm } 1825228753Smm file->parent = parent; 1826302001Smm file->offset = offset; 1827228753Smm file->size = fsize; 1828228753Smm file->mtime = isodate7(isodirrec + DR_date_offset); 1829228753Smm file->ctime = file->atime = file->mtime; 1830228753Smm file->rede_files.first = NULL; 1831228753Smm file->rede_files.last = &(file->rede_files.first); 1832228753Smm 1833228753Smm p = isodirrec + DR_name_offset; 1834228753Smm /* Rockridge extensions (if any) follow name. Compute this 1835228753Smm * before fidgeting the name_len below. */ 1836228753Smm rr_start = p + name_len + (name_len & 1 ? 0 : 1); 1837228753Smm rr_end = isodirrec + dr_len; 1838228753Smm 1839228753Smm if (iso9660->seenJoliet) { 1840228753Smm /* Joliet names are max 64 chars (128 bytes) according to spec, 1841228753Smm * but genisoimage/mkisofs allows recording longer Joliet 1842228753Smm * names which are 103 UCS2 characters(206 bytes) by their 1843228753Smm * option '-joliet-long'. 1844228753Smm */ 1845228753Smm if (name_len > 206) 1846228753Smm name_len = 206; 1847232153Smm name_len &= ~1; 1848228753Smm 1849228753Smm /* trim trailing first version and dot from filename. 1850228753Smm * 1851232153Smm * Remember we were in UTF-16BE land! 1852228753Smm * SEPARATOR 1 (.) and SEPARATOR 2 (;) are both 1853228753Smm * 16 bits big endian characters on Joliet. 1854228753Smm * 1855228753Smm * TODO: sanitize filename? 1856228753Smm * Joliet allows any UCS-2 char except: 1857228753Smm * *, /, :, ;, ? and \. 1858228753Smm */ 1859228753Smm /* Chop off trailing ';1' from files. */ 1860232153Smm if (name_len > 4 && p[name_len-4] == 0 && p[name_len-3] == ';' 1861232153Smm && p[name_len-2] == 0 && p[name_len-1] == '1') 1862232153Smm name_len -= 4; 1863232153Smm#if 0 /* XXX: this somehow manages to strip of single-character file extensions, like '.c'. */ 1864228753Smm /* Chop off trailing '.' from filenames. */ 1865232153Smm if (name_len > 2 && p[name_len-2] == 0 && p[name_len-1] == '.') 1866232153Smm name_len -= 2; 1867228753Smm#endif 1868232153Smm if ((file->utf16be_name = malloc(name_len)) == NULL) { 1869232153Smm archive_set_error(&a->archive, ENOMEM, 1870232153Smm "No memory for file name"); 1871313571Smm goto fail; 1872232153Smm } 1873232153Smm memcpy(file->utf16be_name, p, name_len); 1874232153Smm file->utf16be_bytes = name_len; 1875228753Smm } else { 1876228753Smm /* Chop off trailing ';1' from files. */ 1877228753Smm if (name_len > 2 && p[name_len - 2] == ';' && 1878228753Smm p[name_len - 1] == '1') 1879228753Smm name_len -= 2; 1880228753Smm /* Chop off trailing '.' from filenames. */ 1881228753Smm if (name_len > 1 && p[name_len - 1] == '.') 1882228753Smm --name_len; 1883228753Smm 1884228753Smm archive_strncpy(&file->name, (const char *)p, name_len); 1885228753Smm } 1886228753Smm 1887228753Smm flags = isodirrec[DR_flags_offset]; 1888228753Smm if (flags & 0x02) 1889228753Smm file->mode = AE_IFDIR | 0700; 1890228753Smm else 1891228753Smm file->mode = AE_IFREG | 0400; 1892228753Smm if (flags & 0x80) 1893228753Smm file->multi_extent = 1; 1894228753Smm else 1895228753Smm file->multi_extent = 0; 1896228753Smm /* 1897232153Smm * Use a location for the file number, which is treated as an inode 1898232153Smm * number to find out hardlink target. If Rockridge extensions is 1899232153Smm * being used, the file number will be overwritten by FILE SERIAL 1900232153Smm * NUMBER of RRIP "PX" extension. 1901232153Smm * Note: Old mkisofs did not record that FILE SERIAL NUMBER 1902228753Smm * in ISO images. 1903232153Smm * Note2: xorriso set 0 to the location of a symlink file. 1904228753Smm */ 1905232153Smm if (file->size == 0 && location >= 0) { 1906232153Smm /* If file->size is zero, its location points wrong place, 1907232153Smm * and so we should not use it for the file number. 1908232153Smm * When the location has negative value, it can be used 1909232153Smm * for the file number. 1910228753Smm */ 1911228753Smm file->number = -1; 1912232153Smm /* Do not appear before any directory entries. */ 1913232153Smm file->offset = -1; 1914232153Smm } else 1915228753Smm file->number = (int64_t)(uint32_t)location; 1916228753Smm 1917228753Smm /* Rockridge extensions overwrite information from above. */ 1918228753Smm if (iso9660->opt_support_rockridge) { 1919228753Smm if (parent == NULL && rr_end - rr_start >= 7) { 1920228753Smm p = rr_start; 1921248616Smm if (memcmp(p, "SP\x07\x01\xbe\xef", 6) == 0) { 1922228753Smm /* 1923228753Smm * SP extension stores the suspOffset 1924228753Smm * (Number of bytes to skip between 1925228753Smm * filename and SUSP records.) 1926228753Smm * It is mandatory by the SUSP standard 1927228753Smm * (IEEE 1281). 1928228753Smm * 1929228753Smm * It allows SUSP to coexist with 1930228753Smm * non-SUSP uses of the System 1931228753Smm * Use Area by placing non-SUSP data 1932228753Smm * before SUSP data. 1933228753Smm * 1934228753Smm * SP extension must be in the root 1935228753Smm * directory entry, disable all SUSP 1936228753Smm * processing if not found. 1937228753Smm */ 1938228753Smm iso9660->suspOffset = p[6]; 1939228753Smm iso9660->seenSUSP = 1; 1940228753Smm rr_start += 7; 1941228753Smm } 1942228753Smm } 1943228753Smm if (iso9660->seenSUSP) { 1944228753Smm int r; 1945228753Smm 1946228753Smm file->name_continues = 0; 1947228753Smm file->symlink_continues = 0; 1948228753Smm rr_start += iso9660->suspOffset; 1949228753Smm r = parse_rockridge(a, file, rr_start, rr_end); 1950313571Smm if (r != ARCHIVE_OK) 1951313571Smm goto fail; 1952230759Smm /* 1953230759Smm * A file size of symbolic link files in ISO images 1954230759Smm * made by makefs is not zero and its location is 1955230759Smm * the same as those of next regular file. That is 1956230759Smm * the same as hard like file and it causes unexpected 1957230759Smm * error. 1958230759Smm */ 1959230759Smm if (file->size > 0 && 1960230759Smm (file->mode & AE_IFMT) == AE_IFLNK) { 1961230759Smm file->size = 0; 1962230759Smm file->number = -1; 1963230759Smm file->offset = -1; 1964230759Smm } 1965228753Smm } else 1966228753Smm /* If there isn't SUSP, disable parsing 1967228753Smm * rock ridge extensions. */ 1968228753Smm iso9660->opt_support_rockridge = 0; 1969228753Smm } 1970228753Smm 1971228753Smm file->nlinks = 1;/* Reset nlink. we'll calculate it later. */ 1972228753Smm /* Tell file's parent how many children that parent has. */ 1973228753Smm if (parent != NULL && (flags & 0x02)) 1974228753Smm parent->subdirs++; 1975228753Smm 1976228753Smm if (iso9660->seenRockridge) { 1977228753Smm if (parent != NULL && parent->parent == NULL && 1978228753Smm (flags & 0x02) && iso9660->rr_moved == NULL && 1979248616Smm file->name.s && 1980228753Smm (strcmp(file->name.s, "rr_moved") == 0 || 1981228753Smm strcmp(file->name.s, ".rr_moved") == 0)) { 1982228753Smm iso9660->rr_moved = file; 1983228753Smm file->rr_moved = 1; 1984228753Smm file->rr_moved_has_re_only = 1; 1985228753Smm file->re = 0; 1986228753Smm parent->subdirs--; 1987228753Smm } else if (file->re) { 1988228911Smm /* 1989228911Smm * Sanity check: file's parent is rr_moved. 1990228911Smm */ 1991228911Smm if (parent == NULL || parent->rr_moved == 0) { 1992228911Smm archive_set_error(&a->archive, 1993228911Smm ARCHIVE_ERRNO_MISC, 1994228911Smm "Invalid Rockridge RE"); 1995313571Smm goto fail; 1996228753Smm } 1997228911Smm /* 1998228911Smm * Sanity check: file does not have "CL" extension. 1999228911Smm */ 2000228911Smm if (file->cl_offset) { 2001228911Smm archive_set_error(&a->archive, 2002228911Smm ARCHIVE_ERRNO_MISC, 2003228911Smm "Invalid Rockridge RE and CL"); 2004313571Smm goto fail; 2005228911Smm } 2006228911Smm /* 2007228911Smm * Sanity check: The file type must be a directory. 2008228911Smm */ 2009228911Smm if ((flags & 0x02) == 0) { 2010228911Smm archive_set_error(&a->archive, 2011228911Smm ARCHIVE_ERRNO_MISC, 2012228911Smm "Invalid Rockridge RE"); 2013313571Smm goto fail; 2014228911Smm } 2015228753Smm } else if (parent != NULL && parent->rr_moved) 2016228753Smm file->rr_moved_has_re_only = 0; 2017228753Smm else if (parent != NULL && (flags & 0x02) && 2018228753Smm (parent->re || parent->re_descendant)) 2019228753Smm file->re_descendant = 1; 2020228911Smm if (file->cl_offset) { 2021228911Smm struct file_info *r; 2022228911Smm 2023228911Smm if (parent == NULL || parent->parent == NULL) { 2024228911Smm archive_set_error(&a->archive, 2025228911Smm ARCHIVE_ERRNO_MISC, 2026228911Smm "Invalid Rockridge CL"); 2027313571Smm goto fail; 2028228911Smm } 2029228911Smm /* 2030228911Smm * Sanity check: The file type must be a regular file. 2031228911Smm */ 2032228911Smm if ((flags & 0x02) != 0) { 2033228911Smm archive_set_error(&a->archive, 2034228911Smm ARCHIVE_ERRNO_MISC, 2035228911Smm "Invalid Rockridge CL"); 2036313571Smm goto fail; 2037228911Smm } 2038228753Smm parent->subdirs++; 2039228753Smm /* Overwrite an offset and a number of this "CL" entry 2040228753Smm * to appear before other dirs. "+1" to those is to 2041228753Smm * make sure to appear after "RE" entry which this 2042228753Smm * "CL" entry should be connected with. */ 2043228753Smm file->offset = file->number = file->cl_offset + 1; 2044228911Smm 2045228911Smm /* 2046228911Smm * Sanity check: cl_offset does not point at its 2047228911Smm * the parents or itself. 2048228911Smm */ 2049228911Smm for (r = parent; r; r = r->parent) { 2050228911Smm if (r->offset == file->cl_offset) { 2051228911Smm archive_set_error(&a->archive, 2052228911Smm ARCHIVE_ERRNO_MISC, 2053228911Smm "Invalid Rockridge CL"); 2054313571Smm goto fail; 2055228911Smm } 2056228911Smm } 2057228911Smm if (file->cl_offset == file->offset || 2058228911Smm parent->rr_moved) { 2059228911Smm archive_set_error(&a->archive, 2060228911Smm ARCHIVE_ERRNO_MISC, 2061228911Smm "Invalid Rockridge CL"); 2062313571Smm goto fail; 2063228911Smm } 2064228753Smm } 2065228753Smm } 2066228753Smm 2067228753Smm#if DEBUG 2068228753Smm /* DEBUGGING: Warn about attributes I don't yet fully support. */ 2069228753Smm if ((flags & ~0x02) != 0) { 2070228753Smm fprintf(stderr, "\n ** Unrecognized flag: "); 2071228753Smm dump_isodirrec(stderr, isodirrec); 2072228753Smm fprintf(stderr, "\n"); 2073228753Smm } else if (toi(isodirrec + DR_volume_sequence_number_offset, 2) != 1) { 2074228753Smm fprintf(stderr, "\n ** Unrecognized sequence number: "); 2075228753Smm dump_isodirrec(stderr, isodirrec); 2076228753Smm fprintf(stderr, "\n"); 2077228753Smm } else if (*(isodirrec + DR_file_unit_size_offset) != 0) { 2078228753Smm fprintf(stderr, "\n ** Unexpected file unit size: "); 2079228753Smm dump_isodirrec(stderr, isodirrec); 2080228753Smm fprintf(stderr, "\n"); 2081228753Smm } else if (*(isodirrec + DR_interleave_offset) != 0) { 2082228753Smm fprintf(stderr, "\n ** Unexpected interleave: "); 2083228753Smm dump_isodirrec(stderr, isodirrec); 2084228753Smm fprintf(stderr, "\n"); 2085228753Smm } else if (*(isodirrec + DR_ext_attr_length_offset) != 0) { 2086228753Smm fprintf(stderr, "\n ** Unexpected extended attribute length: "); 2087228753Smm dump_isodirrec(stderr, isodirrec); 2088228753Smm fprintf(stderr, "\n"); 2089228753Smm } 2090228753Smm#endif 2091228753Smm register_file(iso9660, file); 2092228753Smm return (file); 2093313571Smmfail: 2094313571Smm archive_string_free(&file->name); 2095313571Smm free(file); 2096313571Smm return (NULL); 2097228753Smm} 2098228753Smm 2099228753Smmstatic int 2100228753Smmparse_rockridge(struct archive_read *a, struct file_info *file, 2101228753Smm const unsigned char *p, const unsigned char *end) 2102228753Smm{ 2103228753Smm struct iso9660 *iso9660; 2104344674Smm int entry_seen = 0; 2105228753Smm 2106228753Smm iso9660 = (struct iso9660 *)(a->format->data); 2107228753Smm 2108228753Smm while (p + 4 <= end /* Enough space for another entry. */ 2109228753Smm && p[0] >= 'A' && p[0] <= 'Z' /* Sanity-check 1st char of name. */ 2110228753Smm && p[1] >= 'A' && p[1] <= 'Z' /* Sanity-check 2nd char of name. */ 2111228753Smm && p[2] >= 4 /* Sanity-check length. */ 2112228753Smm && p + p[2] <= end) { /* Sanity-check length. */ 2113228753Smm const unsigned char *data = p + 4; 2114228753Smm int data_length = p[2] - 4; 2115228753Smm int version = p[3]; 2116228753Smm 2117228753Smm switch(p[0]) { 2118228753Smm case 'C': 2119248616Smm if (p[1] == 'E') { 2120228753Smm if (version == 1 && data_length == 24) { 2121228753Smm /* 2122228753Smm * CE extension comprises: 2123228753Smm * 8 byte sector containing extension 2124228753Smm * 8 byte offset w/in above sector 2125228753Smm * 8 byte length of continuation 2126228753Smm */ 2127228753Smm int32_t location = 2128228753Smm archive_le32dec(data); 2129228753Smm file->ce_offset = 2130228753Smm archive_le32dec(data+8); 2131228753Smm file->ce_size = 2132228753Smm archive_le32dec(data+16); 2133228753Smm if (register_CE(a, location, file) 2134228753Smm != ARCHIVE_OK) 2135228753Smm return (ARCHIVE_FATAL); 2136228753Smm } 2137228753Smm } 2138248616Smm else if (p[1] == 'L') { 2139228753Smm if (version == 1 && data_length == 8) { 2140228753Smm file->cl_offset = (uint64_t) 2141228753Smm iso9660->logical_block_size * 2142228753Smm (uint64_t)archive_le32dec(data); 2143228753Smm iso9660->seenRockridge = 1; 2144228753Smm } 2145228753Smm } 2146248616Smm break; 2147228753Smm case 'N': 2148248616Smm if (p[1] == 'M') { 2149228753Smm if (version == 1) { 2150228753Smm parse_rockridge_NM1(file, 2151228753Smm data, data_length); 2152228753Smm iso9660->seenRockridge = 1; 2153228753Smm } 2154228753Smm } 2155248616Smm break; 2156228753Smm case 'P': 2157248616Smm /* 2158248616Smm * PD extension is padding; 2159248616Smm * contents are always ignored. 2160248616Smm * 2161248616Smm * PL extension won't appear; 2162248616Smm * contents are always ignored. 2163248616Smm */ 2164248616Smm if (p[1] == 'N') { 2165228753Smm if (version == 1 && data_length == 16) { 2166228753Smm file->rdev = toi(data,4); 2167228753Smm file->rdev <<= 32; 2168228753Smm file->rdev |= toi(data + 8, 4); 2169228753Smm iso9660->seenRockridge = 1; 2170228753Smm } 2171228753Smm } 2172248616Smm else if (p[1] == 'X') { 2173228753Smm /* 2174228753Smm * PX extension comprises: 2175228753Smm * 8 bytes for mode, 2176228753Smm * 8 bytes for nlinks, 2177228753Smm * 8 bytes for uid, 2178228753Smm * 8 bytes for gid, 2179228753Smm * 8 bytes for inode. 2180228753Smm */ 2181228753Smm if (version == 1) { 2182228753Smm if (data_length >= 8) 2183228753Smm file->mode 2184228753Smm = toi(data, 4); 2185228753Smm if (data_length >= 16) 2186228753Smm file->nlinks 2187228753Smm = toi(data + 8, 4); 2188228753Smm if (data_length >= 24) 2189228753Smm file->uid 2190228753Smm = toi(data + 16, 4); 2191228753Smm if (data_length >= 32) 2192228753Smm file->gid 2193228753Smm = toi(data + 24, 4); 2194228753Smm if (data_length >= 40) 2195228753Smm file->number 2196228753Smm = toi(data + 32, 4); 2197228753Smm iso9660->seenRockridge = 1; 2198228753Smm } 2199228753Smm } 2200248616Smm break; 2201228753Smm case 'R': 2202248616Smm if (p[1] == 'E' && version == 1) { 2203228753Smm file->re = 1; 2204228753Smm iso9660->seenRockridge = 1; 2205228753Smm } 2206248616Smm else if (p[1] == 'R' && version == 1) { 2207228753Smm /* 2208228753Smm * RR extension comprises: 2209228753Smm * one byte flag value 2210228753Smm * This extension is obsolete, 2211228753Smm * so contents are always ignored. 2212228753Smm */ 2213228753Smm } 2214248616Smm break; 2215228753Smm case 'S': 2216248616Smm if (p[1] == 'L') { 2217228753Smm if (version == 1) { 2218228753Smm parse_rockridge_SL1(file, 2219228753Smm data, data_length); 2220228753Smm iso9660->seenRockridge = 1; 2221228753Smm } 2222228753Smm } 2223248616Smm else if (p[1] == 'T' 2224228753Smm && data_length == 0 && version == 1) { 2225228753Smm /* 2226228753Smm * ST extension marks end of this 2227228753Smm * block of SUSP entries. 2228228753Smm * 2229228753Smm * It allows SUSP to coexist with 2230228753Smm * non-SUSP uses of the System 2231228753Smm * Use Area by placing non-SUSP data 2232228753Smm * after SUSP data. 2233228753Smm */ 2234228753Smm iso9660->seenSUSP = 0; 2235228753Smm iso9660->seenRockridge = 0; 2236228753Smm return (ARCHIVE_OK); 2237228753Smm } 2238248616Smm break; 2239228753Smm case 'T': 2240248616Smm if (p[1] == 'F') { 2241228753Smm if (version == 1) { 2242228753Smm parse_rockridge_TF1(file, 2243228753Smm data, data_length); 2244228753Smm iso9660->seenRockridge = 1; 2245228753Smm } 2246228753Smm } 2247248616Smm break; 2248228753Smm case 'Z': 2249248616Smm if (p[1] == 'F') { 2250228753Smm if (version == 1) 2251228753Smm parse_rockridge_ZF1(file, 2252228753Smm data, data_length); 2253228753Smm } 2254248616Smm break; 2255228753Smm default: 2256228753Smm break; 2257228753Smm } 2258228753Smm 2259228753Smm p += p[2]; 2260344674Smm entry_seen = 1; 2261228753Smm } 2262344674Smm 2263344674Smm if (entry_seen) 2264344674Smm return (ARCHIVE_OK); 2265344674Smm else { 2266344674Smm archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, 2267344674Smm "Tried to parse Rockridge extensions, but none found"); 2268344674Smm return (ARCHIVE_WARN); 2269344674Smm } 2270228753Smm} 2271228753Smm 2272228753Smmstatic int 2273228753Smmregister_CE(struct archive_read *a, int32_t location, 2274228753Smm struct file_info *file) 2275228753Smm{ 2276228753Smm struct iso9660 *iso9660; 2277228753Smm struct read_ce_queue *heap; 2278228753Smm struct read_ce_req *p; 2279228753Smm uint64_t offset, parent_offset; 2280228753Smm int hole, parent; 2281228753Smm 2282228753Smm iso9660 = (struct iso9660 *)(a->format->data); 2283228753Smm offset = ((uint64_t)location) * (uint64_t)iso9660->logical_block_size; 2284228753Smm if (((file->mode & AE_IFMT) == AE_IFREG && 2285228753Smm offset >= file->offset) || 2286232153Smm offset < iso9660->current_position || 2287232153Smm (((uint64_t)file->ce_offset) + file->ce_size) 2288232153Smm > (uint64_t)iso9660->logical_block_size || 2289232153Smm offset + file->ce_offset + file->ce_size 2290232153Smm > iso9660->volume_size) { 2291228753Smm archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 2292232153Smm "Invalid parameter in SUSP \"CE\" extension"); 2293228753Smm return (ARCHIVE_FATAL); 2294228753Smm } 2295228753Smm 2296228753Smm /* Expand our CE list as necessary. */ 2297228753Smm heap = &(iso9660->read_ce_req); 2298228753Smm if (heap->cnt >= heap->allocated) { 2299228753Smm int new_size; 2300228753Smm 2301228753Smm if (heap->allocated < 16) 2302228753Smm new_size = 16; 2303228753Smm else 2304228753Smm new_size = heap->allocated * 2; 2305228753Smm /* Overflow might keep us from growing the list. */ 2306232153Smm if (new_size <= heap->allocated) { 2307232153Smm archive_set_error(&a->archive, ENOMEM, "Out of memory"); 2308232153Smm return (ARCHIVE_FATAL); 2309232153Smm } 2310238856Smm p = calloc(new_size, sizeof(p[0])); 2311232153Smm if (p == NULL) { 2312232153Smm archive_set_error(&a->archive, ENOMEM, "Out of memory"); 2313232153Smm return (ARCHIVE_FATAL); 2314232153Smm } 2315228753Smm if (heap->reqs != NULL) { 2316228753Smm memcpy(p, heap->reqs, heap->cnt * sizeof(*p)); 2317228753Smm free(heap->reqs); 2318228753Smm } 2319228753Smm heap->reqs = p; 2320228753Smm heap->allocated = new_size; 2321228753Smm } 2322228753Smm 2323228753Smm /* 2324228753Smm * Start with hole at end, walk it up tree to find insertion point. 2325228753Smm */ 2326228753Smm hole = heap->cnt++; 2327228753Smm while (hole > 0) { 2328228753Smm parent = (hole - 1)/2; 2329228753Smm parent_offset = heap->reqs[parent].offset; 2330228753Smm if (offset >= parent_offset) { 2331228753Smm heap->reqs[hole].offset = offset; 2332228753Smm heap->reqs[hole].file = file; 2333228753Smm return (ARCHIVE_OK); 2334228753Smm } 2335232153Smm /* Move parent into hole <==> move hole up tree. */ 2336228753Smm heap->reqs[hole] = heap->reqs[parent]; 2337228753Smm hole = parent; 2338228753Smm } 2339228753Smm heap->reqs[0].offset = offset; 2340228753Smm heap->reqs[0].file = file; 2341228753Smm return (ARCHIVE_OK); 2342228753Smm} 2343228753Smm 2344228753Smmstatic void 2345228753Smmnext_CE(struct read_ce_queue *heap) 2346228753Smm{ 2347228753Smm uint64_t a_offset, b_offset, c_offset; 2348228753Smm int a, b, c; 2349228753Smm struct read_ce_req tmp; 2350228753Smm 2351228753Smm if (heap->cnt < 1) 2352228753Smm return; 2353228753Smm 2354228753Smm /* 2355228753Smm * Move the last item in the heap to the root of the tree 2356228753Smm */ 2357228753Smm heap->reqs[0] = heap->reqs[--(heap->cnt)]; 2358228753Smm 2359228753Smm /* 2360228753Smm * Rebalance the heap. 2361228753Smm */ 2362232153Smm a = 0; /* Starting element and its offset */ 2363228753Smm a_offset = heap->reqs[a].offset; 2364228753Smm for (;;) { 2365232153Smm b = a + a + 1; /* First child */ 2366228753Smm if (b >= heap->cnt) 2367228753Smm return; 2368228753Smm b_offset = heap->reqs[b].offset; 2369232153Smm c = b + 1; /* Use second child if it is smaller. */ 2370228753Smm if (c < heap->cnt) { 2371228753Smm c_offset = heap->reqs[c].offset; 2372228753Smm if (c_offset < b_offset) { 2373228753Smm b = c; 2374228753Smm b_offset = c_offset; 2375228753Smm } 2376228753Smm } 2377228753Smm if (a_offset <= b_offset) 2378228753Smm return; 2379228753Smm tmp = heap->reqs[a]; 2380228753Smm heap->reqs[a] = heap->reqs[b]; 2381228753Smm heap->reqs[b] = tmp; 2382228753Smm a = b; 2383228753Smm } 2384228753Smm} 2385228753Smm 2386228753Smm 2387228753Smmstatic int 2388228753Smmread_CE(struct archive_read *a, struct iso9660 *iso9660) 2389228753Smm{ 2390228753Smm struct read_ce_queue *heap; 2391228753Smm const unsigned char *b, *p, *end; 2392228753Smm struct file_info *file; 2393228753Smm size_t step; 2394228753Smm int r; 2395228753Smm 2396228753Smm /* Read data which RRIP "CE" extension points. */ 2397228753Smm heap = &(iso9660->read_ce_req); 2398228753Smm step = iso9660->logical_block_size; 2399228753Smm while (heap->cnt && 2400228753Smm heap->reqs[0].offset == iso9660->current_position) { 2401228753Smm b = __archive_read_ahead(a, step, NULL); 2402228753Smm if (b == NULL) { 2403228753Smm archive_set_error(&a->archive, 2404228753Smm ARCHIVE_ERRNO_MISC, 2405228753Smm "Failed to read full block when scanning " 2406228753Smm "ISO9660 directory list"); 2407228753Smm return (ARCHIVE_FATAL); 2408228753Smm } 2409228753Smm do { 2410228753Smm file = heap->reqs[0].file; 2411232153Smm if (file->ce_offset + file->ce_size > step) { 2412232153Smm archive_set_error(&a->archive, 2413232153Smm ARCHIVE_ERRNO_FILE_FORMAT, 2414232153Smm "Malformed CE information"); 2415232153Smm return (ARCHIVE_FATAL); 2416232153Smm } 2417228753Smm p = b + file->ce_offset; 2418228753Smm end = p + file->ce_size; 2419228753Smm next_CE(heap); 2420228753Smm r = parse_rockridge(a, file, p, end); 2421228753Smm if (r != ARCHIVE_OK) 2422228753Smm return (ARCHIVE_FATAL); 2423228753Smm } while (heap->cnt && 2424228753Smm heap->reqs[0].offset == iso9660->current_position); 2425313571Smm /* NOTE: Do not move this consume's code to front of 2426228753Smm * do-while loop. Registration of nested CE extension 2427228753Smm * might cause error because of current position. */ 2428228753Smm __archive_read_consume(a, step); 2429228753Smm iso9660->current_position += step; 2430228753Smm } 2431228753Smm return (ARCHIVE_OK); 2432228753Smm} 2433228753Smm 2434228753Smmstatic void 2435228753Smmparse_rockridge_NM1(struct file_info *file, 2436228753Smm const unsigned char *data, int data_length) 2437228753Smm{ 2438228753Smm if (!file->name_continues) 2439228753Smm archive_string_empty(&file->name); 2440228753Smm file->name_continues = 0; 2441228753Smm if (data_length < 1) 2442228753Smm return; 2443228753Smm /* 2444228753Smm * NM version 1 extension comprises: 2445228753Smm * 1 byte flag, value is one of: 2446228753Smm * = 0: remainder is name 2447228753Smm * = 1: remainder is name, next NM entry continues name 2448228753Smm * = 2: "." 2449228753Smm * = 4: ".." 2450228753Smm * = 32: Implementation specific 2451228753Smm * All other values are reserved. 2452228753Smm */ 2453228753Smm switch(data[0]) { 2454228753Smm case 0: 2455228753Smm if (data_length < 2) 2456228753Smm return; 2457232153Smm archive_strncat(&file->name, 2458232153Smm (const char *)data + 1, data_length - 1); 2459228753Smm break; 2460228753Smm case 1: 2461228753Smm if (data_length < 2) 2462228753Smm return; 2463232153Smm archive_strncat(&file->name, 2464232153Smm (const char *)data + 1, data_length - 1); 2465228753Smm file->name_continues = 1; 2466228753Smm break; 2467228753Smm case 2: 2468228753Smm archive_strcat(&file->name, "."); 2469228753Smm break; 2470228753Smm case 4: 2471228753Smm archive_strcat(&file->name, ".."); 2472228753Smm break; 2473228753Smm default: 2474228753Smm return; 2475228753Smm } 2476228753Smm 2477228753Smm} 2478228753Smm 2479228753Smmstatic void 2480228753Smmparse_rockridge_TF1(struct file_info *file, const unsigned char *data, 2481228753Smm int data_length) 2482228753Smm{ 2483228753Smm char flag; 2484228753Smm /* 2485228753Smm * TF extension comprises: 2486228753Smm * one byte flag 2487228753Smm * create time (optional) 2488228753Smm * modify time (optional) 2489228753Smm * access time (optional) 2490228753Smm * attribute time (optional) 2491228753Smm * Time format and presence of fields 2492228753Smm * is controlled by flag bits. 2493228753Smm */ 2494228753Smm if (data_length < 1) 2495228753Smm return; 2496228753Smm flag = data[0]; 2497228753Smm ++data; 2498228753Smm --data_length; 2499228753Smm if (flag & 0x80) { 2500228753Smm /* Use 17-byte time format. */ 2501228753Smm if ((flag & 1) && data_length >= 17) { 2502228753Smm /* Create time. */ 2503228753Smm file->birthtime_is_set = 1; 2504228753Smm file->birthtime = isodate17(data); 2505228753Smm data += 17; 2506228753Smm data_length -= 17; 2507228753Smm } 2508228753Smm if ((flag & 2) && data_length >= 17) { 2509228753Smm /* Modify time. */ 2510228753Smm file->mtime = isodate17(data); 2511228753Smm data += 17; 2512228753Smm data_length -= 17; 2513228753Smm } 2514228753Smm if ((flag & 4) && data_length >= 17) { 2515228753Smm /* Access time. */ 2516228753Smm file->atime = isodate17(data); 2517228753Smm data += 17; 2518228753Smm data_length -= 17; 2519228753Smm } 2520228753Smm if ((flag & 8) && data_length >= 17) { 2521228753Smm /* Attribute change time. */ 2522228753Smm file->ctime = isodate17(data); 2523228753Smm } 2524228753Smm } else { 2525228753Smm /* Use 7-byte time format. */ 2526228753Smm if ((flag & 1) && data_length >= 7) { 2527228753Smm /* Create time. */ 2528228753Smm file->birthtime_is_set = 1; 2529228753Smm file->birthtime = isodate7(data); 2530228753Smm data += 7; 2531228753Smm data_length -= 7; 2532228753Smm } 2533228753Smm if ((flag & 2) && data_length >= 7) { 2534228753Smm /* Modify time. */ 2535228753Smm file->mtime = isodate7(data); 2536228753Smm data += 7; 2537228753Smm data_length -= 7; 2538228753Smm } 2539228753Smm if ((flag & 4) && data_length >= 7) { 2540228753Smm /* Access time. */ 2541228753Smm file->atime = isodate7(data); 2542228753Smm data += 7; 2543228753Smm data_length -= 7; 2544228753Smm } 2545228753Smm if ((flag & 8) && data_length >= 7) { 2546228753Smm /* Attribute change time. */ 2547228753Smm file->ctime = isodate7(data); 2548228753Smm } 2549228753Smm } 2550228753Smm} 2551228753Smm 2552228753Smmstatic void 2553228753Smmparse_rockridge_SL1(struct file_info *file, const unsigned char *data, 2554228753Smm int data_length) 2555228753Smm{ 2556228753Smm const char *separator = ""; 2557228753Smm 2558228753Smm if (!file->symlink_continues || file->symlink.length < 1) 2559228753Smm archive_string_empty(&file->symlink); 2560228753Smm file->symlink_continues = 0; 2561228753Smm 2562228753Smm /* 2563228753Smm * Defined flag values: 2564228753Smm * 0: This is the last SL record for this symbolic link 2565228753Smm * 1: this symbolic link field continues in next SL entry 2566228753Smm * All other values are reserved. 2567228753Smm */ 2568228753Smm if (data_length < 1) 2569228753Smm return; 2570228753Smm switch(*data) { 2571228753Smm case 0: 2572228753Smm break; 2573228753Smm case 1: 2574228753Smm file->symlink_continues = 1; 2575228753Smm break; 2576228753Smm default: 2577228753Smm return; 2578228753Smm } 2579228753Smm ++data; /* Skip flag byte. */ 2580228753Smm --data_length; 2581228753Smm 2582228753Smm /* 2583228753Smm * SL extension body stores "components". 2584228753Smm * Basically, this is a complicated way of storing 2585228753Smm * a POSIX path. It also interferes with using 2586228753Smm * symlinks for storing non-path data. <sigh> 2587228753Smm * 2588228753Smm * Each component is 2 bytes (flag and length) 2589228753Smm * possibly followed by name data. 2590228753Smm */ 2591228753Smm while (data_length >= 2) { 2592228753Smm unsigned char flag = *data++; 2593228753Smm unsigned char nlen = *data++; 2594228753Smm data_length -= 2; 2595228753Smm 2596228753Smm archive_strcat(&file->symlink, separator); 2597228753Smm separator = "/"; 2598228753Smm 2599228753Smm switch(flag) { 2600228753Smm case 0: /* Usual case, this is text. */ 2601228753Smm if (data_length < nlen) 2602228753Smm return; 2603228753Smm archive_strncat(&file->symlink, 2604228753Smm (const char *)data, nlen); 2605228753Smm break; 2606228753Smm case 0x01: /* Text continues in next component. */ 2607228753Smm if (data_length < nlen) 2608228753Smm return; 2609228753Smm archive_strncat(&file->symlink, 2610228753Smm (const char *)data, nlen); 2611228753Smm separator = ""; 2612228753Smm break; 2613228753Smm case 0x02: /* Current dir. */ 2614228753Smm archive_strcat(&file->symlink, "."); 2615228753Smm break; 2616228753Smm case 0x04: /* Parent dir. */ 2617228753Smm archive_strcat(&file->symlink, ".."); 2618228753Smm break; 2619228753Smm case 0x08: /* Root of filesystem. */ 2620228753Smm archive_strcat(&file->symlink, "/"); 2621228753Smm separator = ""; 2622228753Smm break; 2623228753Smm case 0x10: /* Undefined (historically "volume root" */ 2624228753Smm archive_string_empty(&file->symlink); 2625228753Smm archive_strcat(&file->symlink, "ROOT"); 2626228753Smm break; 2627228753Smm case 0x20: /* Undefined (historically "hostname") */ 2628228753Smm archive_strcat(&file->symlink, "hostname"); 2629228753Smm break; 2630228753Smm default: 2631228753Smm /* TODO: issue a warning ? */ 2632228753Smm return; 2633228753Smm } 2634228753Smm data += nlen; 2635228753Smm data_length -= nlen; 2636228753Smm } 2637228753Smm} 2638228753Smm 2639228753Smmstatic void 2640228753Smmparse_rockridge_ZF1(struct file_info *file, const unsigned char *data, 2641228753Smm int data_length) 2642228753Smm{ 2643228753Smm 2644228753Smm if (data[0] == 0x70 && data[1] == 0x7a && data_length == 12) { 2645228753Smm /* paged zlib */ 2646228753Smm file->pz = 1; 2647228753Smm file->pz_log2_bs = data[3]; 2648228753Smm file->pz_uncompressed_size = archive_le32dec(&data[4]); 2649228753Smm } 2650228753Smm} 2651228753Smm 2652228753Smmstatic void 2653228753Smmregister_file(struct iso9660 *iso9660, struct file_info *file) 2654228753Smm{ 2655228753Smm 2656228753Smm file->use_next = iso9660->use_files; 2657228753Smm iso9660->use_files = file; 2658228753Smm} 2659228753Smm 2660228753Smmstatic void 2661228753Smmrelease_files(struct iso9660 *iso9660) 2662228753Smm{ 2663228753Smm struct content *con, *connext; 2664228753Smm struct file_info *file; 2665228753Smm 2666228753Smm file = iso9660->use_files; 2667228753Smm while (file != NULL) { 2668228753Smm struct file_info *next = file->use_next; 2669228753Smm 2670228753Smm archive_string_free(&file->name); 2671228753Smm archive_string_free(&file->symlink); 2672232153Smm free(file->utf16be_name); 2673228753Smm con = file->contents.first; 2674228753Smm while (con != NULL) { 2675228753Smm connext = con->next; 2676228753Smm free(con); 2677228753Smm con = connext; 2678228753Smm } 2679228753Smm free(file); 2680228753Smm file = next; 2681228753Smm } 2682228753Smm} 2683228753Smm 2684228753Smmstatic int 2685228753Smmnext_entry_seek(struct archive_read *a, struct iso9660 *iso9660, 2686228753Smm struct file_info **pfile) 2687228753Smm{ 2688228753Smm struct file_info *file; 2689228753Smm int r; 2690228753Smm 2691228753Smm r = next_cache_entry(a, iso9660, pfile); 2692228753Smm if (r != ARCHIVE_OK) 2693228753Smm return (r); 2694228753Smm file = *pfile; 2695228753Smm 2696228753Smm /* Don't waste time seeking for zero-length bodies. */ 2697228753Smm if (file->size == 0) 2698228753Smm file->offset = iso9660->current_position; 2699228753Smm 2700232153Smm /* flush any remaining bytes from the last round to ensure 2701232153Smm * we're positioned */ 2702232153Smm if (iso9660->entry_bytes_unconsumed) { 2703232153Smm __archive_read_consume(a, iso9660->entry_bytes_unconsumed); 2704232153Smm iso9660->entry_bytes_unconsumed = 0; 2705232153Smm } 2706232153Smm 2707228753Smm /* Seek forward to the start of the entry. */ 2708228753Smm if (iso9660->current_position < file->offset) { 2709228753Smm int64_t step; 2710228753Smm 2711228753Smm step = file->offset - iso9660->current_position; 2712232153Smm step = __archive_read_consume(a, step); 2713228753Smm if (step < 0) 2714228753Smm return ((int)step); 2715228753Smm iso9660->current_position = file->offset; 2716228753Smm } 2717228753Smm 2718228753Smm /* We found body of file; handle it now. */ 2719228753Smm return (ARCHIVE_OK); 2720228753Smm} 2721228753Smm 2722228753Smmstatic int 2723228753Smmnext_cache_entry(struct archive_read *a, struct iso9660 *iso9660, 2724228753Smm struct file_info **pfile) 2725228753Smm{ 2726228753Smm struct file_info *file; 2727228753Smm struct { 2728228753Smm struct file_info *first; 2729228753Smm struct file_info **last; 2730228753Smm } empty_files; 2731228753Smm int64_t number; 2732228753Smm int count; 2733228753Smm 2734228753Smm file = cache_get_entry(iso9660); 2735228753Smm if (file != NULL) { 2736228753Smm *pfile = file; 2737228753Smm return (ARCHIVE_OK); 2738228753Smm } 2739228753Smm 2740228753Smm for (;;) { 2741228753Smm struct file_info *re, *d; 2742228753Smm 2743228753Smm *pfile = file = next_entry(iso9660); 2744228753Smm if (file == NULL) { 2745228753Smm /* 2746228753Smm * If directory entries all which are descendant of 2747313571Smm * rr_moved are still remaining, expose their. 2748228753Smm */ 2749228753Smm if (iso9660->re_files.first != NULL && 2750228753Smm iso9660->rr_moved != NULL && 2751228753Smm iso9660->rr_moved->rr_moved_has_re_only) 2752228753Smm /* Expose "rr_moved" entry. */ 2753228753Smm cache_add_entry(iso9660, iso9660->rr_moved); 2754228753Smm while ((re = re_get_entry(iso9660)) != NULL) { 2755228753Smm /* Expose its descendant dirs. */ 2756228753Smm while ((d = rede_get_entry(re)) != NULL) 2757228753Smm cache_add_entry(iso9660, d); 2758228753Smm } 2759228753Smm if (iso9660->cache_files.first != NULL) 2760228753Smm return (next_cache_entry(a, iso9660, pfile)); 2761228753Smm return (ARCHIVE_EOF); 2762228753Smm } 2763228753Smm 2764228753Smm if (file->cl_offset) { 2765228753Smm struct file_info *first_re = NULL; 2766228753Smm int nexted_re = 0; 2767228753Smm 2768228753Smm /* 2769228753Smm * Find "RE" dir for the current file, which 2770228753Smm * has "CL" flag. 2771228753Smm */ 2772228753Smm while ((re = re_get_entry(iso9660)) 2773228753Smm != first_re) { 2774228753Smm if (first_re == NULL) 2775228753Smm first_re = re; 2776228753Smm if (re->offset == file->cl_offset) { 2777228753Smm re->parent->subdirs--; 2778228753Smm re->parent = file->parent; 2779228753Smm re->re = 0; 2780228753Smm if (re->parent->re_descendant) { 2781228753Smm nexted_re = 1; 2782228753Smm re->re_descendant = 1; 2783228753Smm if (rede_add_entry(re) < 0) 2784228753Smm goto fatal_rr; 2785228753Smm /* Move a list of descendants 2786228753Smm * to a new ancestor. */ 2787228753Smm while ((d = rede_get_entry( 2788228753Smm re)) != NULL) 2789228753Smm if (rede_add_entry(d) 2790228753Smm < 0) 2791228753Smm goto fatal_rr; 2792228753Smm break; 2793228753Smm } 2794228753Smm /* Replace the current file 2795228753Smm * with "RE" dir */ 2796228753Smm *pfile = file = re; 2797228753Smm /* Expose its descendant */ 2798228753Smm while ((d = rede_get_entry( 2799228753Smm file)) != NULL) 2800228753Smm cache_add_entry( 2801228753Smm iso9660, d); 2802228753Smm break; 2803228753Smm } else 2804228753Smm re_add_entry(iso9660, re); 2805228753Smm } 2806228753Smm if (nexted_re) { 2807228753Smm /* 2808228753Smm * Do not expose this at this time 2809228753Smm * because we have not gotten its full-path 2810228753Smm * name yet. 2811228753Smm */ 2812228753Smm continue; 2813228753Smm } 2814228753Smm } else if ((file->mode & AE_IFMT) == AE_IFDIR) { 2815228753Smm int r; 2816228753Smm 2817228753Smm /* Read file entries in this dir. */ 2818228753Smm r = read_children(a, file); 2819228753Smm if (r != ARCHIVE_OK) 2820228753Smm return (r); 2821228753Smm 2822228753Smm /* 2823228753Smm * Handle a special dir of Rockridge extensions, 2824228753Smm * "rr_moved". 2825228753Smm */ 2826228753Smm if (file->rr_moved) { 2827228753Smm /* 2828228753Smm * If this has only the subdirectories which 2829228753Smm * have "RE" flags, do not expose at this time. 2830228753Smm */ 2831228753Smm if (file->rr_moved_has_re_only) 2832228753Smm continue; 2833228753Smm /* Otherwise expose "rr_moved" entry. */ 2834228753Smm } else if (file->re) { 2835228753Smm /* 2836228753Smm * Do not expose this at this time 2837228753Smm * because we have not gotten its full-path 2838228753Smm * name yet. 2839228753Smm */ 2840228753Smm re_add_entry(iso9660, file); 2841228753Smm continue; 2842228753Smm } else if (file->re_descendant) { 2843228753Smm /* 2844228753Smm * If the top level "RE" entry of this entry 2845228753Smm * is not exposed, we, accordingly, should not 2846228753Smm * expose this entry at this time because 2847228753Smm * we cannot make its proper full-path name. 2848228753Smm */ 2849228753Smm if (rede_add_entry(file) == 0) 2850228753Smm continue; 2851228753Smm /* Otherwise we can expose this entry because 2852228753Smm * it seems its top level "RE" has already been 2853228753Smm * exposed. */ 2854228753Smm } 2855228753Smm } 2856228753Smm break; 2857228753Smm } 2858228753Smm 2859228753Smm if ((file->mode & AE_IFMT) != AE_IFREG || file->number == -1) 2860228753Smm return (ARCHIVE_OK); 2861228753Smm 2862228753Smm count = 0; 2863228753Smm number = file->number; 2864228753Smm iso9660->cache_files.first = NULL; 2865228753Smm iso9660->cache_files.last = &(iso9660->cache_files.first); 2866228753Smm empty_files.first = NULL; 2867228753Smm empty_files.last = &empty_files.first; 2868228753Smm /* Collect files which has the same file serial number. 2869228753Smm * Peek pending_files so that file which number is different 2870313571Smm * is not put back. */ 2871228753Smm while (iso9660->pending_files.used > 0 && 2872228753Smm (iso9660->pending_files.files[0]->number == -1 || 2873228753Smm iso9660->pending_files.files[0]->number == number)) { 2874228753Smm if (file->number == -1) { 2875228753Smm /* This file has the same offset 2876228753Smm * but it's wrong offset which empty files 2877228753Smm * and symlink files have. 2878313571Smm * NOTE: This wrong offset was recorded by 2879228753Smm * old mkisofs utility. If ISO images is 2880228753Smm * created by latest mkisofs, this does not 2881228753Smm * happen. 2882228753Smm */ 2883228753Smm file->next = NULL; 2884228753Smm *empty_files.last = file; 2885228753Smm empty_files.last = &(file->next); 2886228753Smm } else { 2887228753Smm count++; 2888228753Smm cache_add_entry(iso9660, file); 2889228753Smm } 2890228753Smm file = next_entry(iso9660); 2891228753Smm } 2892228753Smm 2893228753Smm if (count == 0) { 2894228753Smm *pfile = file; 2895228753Smm return ((file == NULL)?ARCHIVE_EOF:ARCHIVE_OK); 2896228753Smm } 2897228753Smm if (file->number == -1) { 2898228753Smm file->next = NULL; 2899228753Smm *empty_files.last = file; 2900228753Smm empty_files.last = &(file->next); 2901228753Smm } else { 2902228753Smm count++; 2903228753Smm cache_add_entry(iso9660, file); 2904228753Smm } 2905228753Smm 2906228753Smm if (count > 1) { 2907228753Smm /* The count is the same as number of hardlink, 2908228753Smm * so much so that each nlinks of files in cache_file 2909228753Smm * is overwritten by value of the count. 2910228753Smm */ 2911228753Smm for (file = iso9660->cache_files.first; 2912228753Smm file != NULL; file = file->next) 2913228753Smm file->nlinks = count; 2914228753Smm } 2915228753Smm /* If there are empty files, that files are added 2916228753Smm * to the tail of the cache_files. */ 2917228753Smm if (empty_files.first != NULL) { 2918228753Smm *iso9660->cache_files.last = empty_files.first; 2919228753Smm iso9660->cache_files.last = empty_files.last; 2920228753Smm } 2921228753Smm *pfile = cache_get_entry(iso9660); 2922228753Smm return ((*pfile == NULL)?ARCHIVE_EOF:ARCHIVE_OK); 2923228753Smm 2924228753Smmfatal_rr: 2925228753Smm archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 2926248616Smm "Failed to connect 'CL' pointer to 'RE' rr_moved pointer of " 2927248616Smm "Rockridge extensions: current position = %jd, CL offset = %jd", 2928248616Smm (intmax_t)iso9660->current_position, (intmax_t)file->cl_offset); 2929228753Smm return (ARCHIVE_FATAL); 2930228753Smm} 2931228753Smm 2932228753Smmstatic inline void 2933228753Smmre_add_entry(struct iso9660 *iso9660, struct file_info *file) 2934228753Smm{ 2935228753Smm file->re_next = NULL; 2936228753Smm *iso9660->re_files.last = file; 2937228753Smm iso9660->re_files.last = &(file->re_next); 2938228753Smm} 2939228753Smm 2940228753Smmstatic inline struct file_info * 2941228753Smmre_get_entry(struct iso9660 *iso9660) 2942228753Smm{ 2943228753Smm struct file_info *file; 2944228753Smm 2945228753Smm if ((file = iso9660->re_files.first) != NULL) { 2946228753Smm iso9660->re_files.first = file->re_next; 2947228753Smm if (iso9660->re_files.first == NULL) 2948228753Smm iso9660->re_files.last = 2949228753Smm &(iso9660->re_files.first); 2950228753Smm } 2951228753Smm return (file); 2952228753Smm} 2953228753Smm 2954228753Smmstatic inline int 2955228753Smmrede_add_entry(struct file_info *file) 2956228753Smm{ 2957228753Smm struct file_info *re; 2958228753Smm 2959228911Smm /* 2960228911Smm * Find "RE" entry. 2961228911Smm */ 2962228753Smm re = file->parent; 2963228911Smm while (re != NULL && !re->re) 2964228753Smm re = re->parent; 2965228753Smm if (re == NULL) 2966228753Smm return (-1); 2967228753Smm 2968228753Smm file->re_next = NULL; 2969228753Smm *re->rede_files.last = file; 2970228753Smm re->rede_files.last = &(file->re_next); 2971228753Smm return (0); 2972228753Smm} 2973228753Smm 2974228753Smmstatic inline struct file_info * 2975228753Smmrede_get_entry(struct file_info *re) 2976228753Smm{ 2977228753Smm struct file_info *file; 2978228753Smm 2979228753Smm if ((file = re->rede_files.first) != NULL) { 2980228753Smm re->rede_files.first = file->re_next; 2981228753Smm if (re->rede_files.first == NULL) 2982228753Smm re->rede_files.last = 2983228753Smm &(re->rede_files.first); 2984228753Smm } 2985228753Smm return (file); 2986228753Smm} 2987228753Smm 2988228753Smmstatic inline void 2989228753Smmcache_add_entry(struct iso9660 *iso9660, struct file_info *file) 2990228753Smm{ 2991228753Smm file->next = NULL; 2992228753Smm *iso9660->cache_files.last = file; 2993228753Smm iso9660->cache_files.last = &(file->next); 2994228753Smm} 2995228753Smm 2996228753Smmstatic inline struct file_info * 2997228753Smmcache_get_entry(struct iso9660 *iso9660) 2998228753Smm{ 2999228753Smm struct file_info *file; 3000228753Smm 3001228753Smm if ((file = iso9660->cache_files.first) != NULL) { 3002228753Smm iso9660->cache_files.first = file->next; 3003228753Smm if (iso9660->cache_files.first == NULL) 3004232153Smm iso9660->cache_files.last = 3005232153Smm &(iso9660->cache_files.first); 3006228753Smm } 3007228753Smm return (file); 3008228753Smm} 3009228753Smm 3010232153Smmstatic int 3011232153Smmheap_add_entry(struct archive_read *a, struct heap_queue *heap, 3012232153Smm struct file_info *file, uint64_t key) 3013228753Smm{ 3014228753Smm uint64_t file_key, parent_key; 3015228753Smm int hole, parent; 3016228753Smm 3017228753Smm /* Expand our pending files list as necessary. */ 3018228753Smm if (heap->used >= heap->allocated) { 3019228753Smm struct file_info **new_pending_files; 3020228753Smm int new_size = heap->allocated * 2; 3021228753Smm 3022228753Smm if (heap->allocated < 1024) 3023228753Smm new_size = 1024; 3024228753Smm /* Overflow might keep us from growing the list. */ 3025232153Smm if (new_size <= heap->allocated) { 3026232153Smm archive_set_error(&a->archive, 3027232153Smm ENOMEM, "Out of memory"); 3028232153Smm return (ARCHIVE_FATAL); 3029232153Smm } 3030228753Smm new_pending_files = (struct file_info **) 3031228753Smm malloc(new_size * sizeof(new_pending_files[0])); 3032232153Smm if (new_pending_files == NULL) { 3033232153Smm archive_set_error(&a->archive, 3034232153Smm ENOMEM, "Out of memory"); 3035232153Smm return (ARCHIVE_FATAL); 3036232153Smm } 3037318483Smm if (heap->allocated) 3038318483Smm memcpy(new_pending_files, heap->files, 3039318483Smm heap->allocated * sizeof(new_pending_files[0])); 3040344674Smm free(heap->files); 3041228753Smm heap->files = new_pending_files; 3042228753Smm heap->allocated = new_size; 3043228753Smm } 3044228753Smm 3045228753Smm file_key = file->key = key; 3046228753Smm 3047228753Smm /* 3048228753Smm * Start with hole at end, walk it up tree to find insertion point. 3049228753Smm */ 3050228753Smm hole = heap->used++; 3051228753Smm while (hole > 0) { 3052228753Smm parent = (hole - 1)/2; 3053228753Smm parent_key = heap->files[parent]->key; 3054228753Smm if (file_key >= parent_key) { 3055228753Smm heap->files[hole] = file; 3056232153Smm return (ARCHIVE_OK); 3057228753Smm } 3058232153Smm /* Move parent into hole <==> move hole up tree. */ 3059228753Smm heap->files[hole] = heap->files[parent]; 3060228753Smm hole = parent; 3061228753Smm } 3062228753Smm heap->files[0] = file; 3063232153Smm 3064232153Smm return (ARCHIVE_OK); 3065228753Smm} 3066228753Smm 3067228753Smmstatic struct file_info * 3068228753Smmheap_get_entry(struct heap_queue *heap) 3069228753Smm{ 3070228753Smm uint64_t a_key, b_key, c_key; 3071228753Smm int a, b, c; 3072228753Smm struct file_info *r, *tmp; 3073228753Smm 3074228753Smm if (heap->used < 1) 3075228753Smm return (NULL); 3076228753Smm 3077228753Smm /* 3078228753Smm * The first file in the list is the earliest; we'll return this. 3079228753Smm */ 3080228753Smm r = heap->files[0]; 3081228753Smm 3082228753Smm /* 3083228753Smm * Move the last item in the heap to the root of the tree 3084228753Smm */ 3085228753Smm heap->files[0] = heap->files[--(heap->used)]; 3086228753Smm 3087228753Smm /* 3088228753Smm * Rebalance the heap. 3089228753Smm */ 3090232153Smm a = 0; /* Starting element and its heap key */ 3091228753Smm a_key = heap->files[a]->key; 3092228753Smm for (;;) { 3093232153Smm b = a + a + 1; /* First child */ 3094228753Smm if (b >= heap->used) 3095228753Smm return (r); 3096228753Smm b_key = heap->files[b]->key; 3097232153Smm c = b + 1; /* Use second child if it is smaller. */ 3098228753Smm if (c < heap->used) { 3099228753Smm c_key = heap->files[c]->key; 3100228753Smm if (c_key < b_key) { 3101228753Smm b = c; 3102228753Smm b_key = c_key; 3103228753Smm } 3104228753Smm } 3105228753Smm if (a_key <= b_key) 3106228753Smm return (r); 3107228753Smm tmp = heap->files[a]; 3108228753Smm heap->files[a] = heap->files[b]; 3109228753Smm heap->files[b] = tmp; 3110228753Smm a = b; 3111228753Smm } 3112228753Smm} 3113228753Smm 3114228753Smmstatic unsigned int 3115228753Smmtoi(const void *p, int n) 3116228753Smm{ 3117228753Smm const unsigned char *v = (const unsigned char *)p; 3118228753Smm if (n > 1) 3119228753Smm return v[0] + 256 * toi(v + 1, n - 1); 3120228753Smm if (n == 1) 3121228753Smm return v[0]; 3122228753Smm return (0); 3123228753Smm} 3124228753Smm 3125228753Smmstatic time_t 3126228753Smmisodate7(const unsigned char *v) 3127228753Smm{ 3128228753Smm struct tm tm; 3129228753Smm int offset; 3130238856Smm time_t t; 3131238856Smm 3132228753Smm memset(&tm, 0, sizeof(tm)); 3133228753Smm tm.tm_year = v[0]; 3134228753Smm tm.tm_mon = v[1] - 1; 3135228753Smm tm.tm_mday = v[2]; 3136228753Smm tm.tm_hour = v[3]; 3137228753Smm tm.tm_min = v[4]; 3138228753Smm tm.tm_sec = v[5]; 3139228753Smm /* v[6] is the signed timezone offset, in 1/4-hour increments. */ 3140228753Smm offset = ((const signed char *)v)[6]; 3141228753Smm if (offset > -48 && offset < 52) { 3142228753Smm tm.tm_hour -= offset / 4; 3143228753Smm tm.tm_min -= (offset % 4) * 15; 3144228753Smm } 3145238856Smm t = time_from_tm(&tm); 3146238856Smm if (t == (time_t)-1) 3147238856Smm return ((time_t)0); 3148238856Smm return (t); 3149228753Smm} 3150228753Smm 3151228753Smmstatic time_t 3152228753Smmisodate17(const unsigned char *v) 3153228753Smm{ 3154228753Smm struct tm tm; 3155228753Smm int offset; 3156238856Smm time_t t; 3157238856Smm 3158228753Smm memset(&tm, 0, sizeof(tm)); 3159228753Smm tm.tm_year = (v[0] - '0') * 1000 + (v[1] - '0') * 100 3160228753Smm + (v[2] - '0') * 10 + (v[3] - '0') 3161228753Smm - 1900; 3162228753Smm tm.tm_mon = (v[4] - '0') * 10 + (v[5] - '0'); 3163228753Smm tm.tm_mday = (v[6] - '0') * 10 + (v[7] - '0'); 3164228753Smm tm.tm_hour = (v[8] - '0') * 10 + (v[9] - '0'); 3165228753Smm tm.tm_min = (v[10] - '0') * 10 + (v[11] - '0'); 3166228753Smm tm.tm_sec = (v[12] - '0') * 10 + (v[13] - '0'); 3167228753Smm /* v[16] is the signed timezone offset, in 1/4-hour increments. */ 3168228753Smm offset = ((const signed char *)v)[16]; 3169228753Smm if (offset > -48 && offset < 52) { 3170228753Smm tm.tm_hour -= offset / 4; 3171228753Smm tm.tm_min -= (offset % 4) * 15; 3172228753Smm } 3173238856Smm t = time_from_tm(&tm); 3174238856Smm if (t == (time_t)-1) 3175238856Smm return ((time_t)0); 3176238856Smm return (t); 3177228753Smm} 3178228753Smm 3179228753Smmstatic time_t 3180228753Smmtime_from_tm(struct tm *t) 3181228753Smm{ 3182228753Smm#if HAVE_TIMEGM 3183302001Smm /* Use platform timegm() if available. */ 3184302001Smm return (timegm(t)); 3185232153Smm#elif HAVE__MKGMTIME64 3186302001Smm return (_mkgmtime64(t)); 3187228753Smm#else 3188302001Smm /* Else use direct calculation using POSIX assumptions. */ 3189302001Smm /* First, fix up tm_yday based on the year/month/day. */ 3190302001Smm if (mktime(t) == (time_t)-1) 3191302001Smm return ((time_t)-1); 3192302001Smm /* Then we can compute timegm() from first principles. */ 3193302001Smm return (t->tm_sec 3194302001Smm + t->tm_min * 60 3195302001Smm + t->tm_hour * 3600 3196302001Smm + t->tm_yday * 86400 3197302001Smm + (t->tm_year - 70) * 31536000 3198302001Smm + ((t->tm_year - 69) / 4) * 86400 3199302001Smm - ((t->tm_year - 1) / 100) * 86400 3200302001Smm + ((t->tm_year + 299) / 400) * 86400); 3201228753Smm#endif 3202228753Smm} 3203228753Smm 3204228753Smmstatic const char * 3205302001Smmbuild_pathname(struct archive_string *as, struct file_info *file, int depth) 3206228753Smm{ 3207302001Smm // Plain ISO9660 only allows 8 dir levels; if we get 3208302001Smm // to 1000, then something is very, very wrong. 3209302001Smm if (depth > 1000) { 3210302001Smm return NULL; 3211302001Smm } 3212228753Smm if (file->parent != NULL && archive_strlen(&file->parent->name) > 0) { 3213302001Smm if (build_pathname(as, file->parent, depth + 1) == NULL) { 3214302001Smm return NULL; 3215302001Smm } 3216228753Smm archive_strcat(as, "/"); 3217228753Smm } 3218228753Smm if (archive_strlen(&file->name) == 0) 3219228753Smm archive_strcat(as, "."); 3220228753Smm else 3221228753Smm archive_string_concat(as, &file->name); 3222228753Smm return (as->s); 3223228753Smm} 3224228753Smm 3225232153Smmstatic int 3226232153Smmbuild_pathname_utf16be(unsigned char *p, size_t max, size_t *len, 3227232153Smm struct file_info *file) 3228232153Smm{ 3229232153Smm if (file->parent != NULL && file->parent->utf16be_bytes > 0) { 3230232153Smm if (build_pathname_utf16be(p, max, len, file->parent) != 0) 3231232153Smm return (-1); 3232232153Smm p[*len] = 0; 3233232153Smm p[*len + 1] = '/'; 3234232153Smm *len += 2; 3235232153Smm } 3236232153Smm if (file->utf16be_bytes == 0) { 3237232153Smm if (*len + 2 > max) 3238232153Smm return (-1);/* Path is too long! */ 3239232153Smm p[*len] = 0; 3240232153Smm p[*len + 1] = '.'; 3241232153Smm *len += 2; 3242232153Smm } else { 3243232153Smm if (*len + file->utf16be_bytes > max) 3244232153Smm return (-1);/* Path is too long! */ 3245232153Smm memcpy(p + *len, file->utf16be_name, file->utf16be_bytes); 3246232153Smm *len += file->utf16be_bytes; 3247232153Smm } 3248232153Smm return (0); 3249232153Smm} 3250232153Smm 3251228753Smm#if DEBUG 3252228753Smmstatic void 3253228753Smmdump_isodirrec(FILE *out, const unsigned char *isodirrec) 3254228753Smm{ 3255228753Smm fprintf(out, " l %d,", 3256228753Smm toi(isodirrec + DR_length_offset, DR_length_size)); 3257228753Smm fprintf(out, " a %d,", 3258228753Smm toi(isodirrec + DR_ext_attr_length_offset, DR_ext_attr_length_size)); 3259228753Smm fprintf(out, " ext 0x%x,", 3260228753Smm toi(isodirrec + DR_extent_offset, DR_extent_size)); 3261228753Smm fprintf(out, " s %d,", 3262228753Smm toi(isodirrec + DR_size_offset, DR_extent_size)); 3263232153Smm fprintf(out, " f 0x%x,", 3264228753Smm toi(isodirrec + DR_flags_offset, DR_flags_size)); 3265228753Smm fprintf(out, " u %d,", 3266228753Smm toi(isodirrec + DR_file_unit_size_offset, DR_file_unit_size_size)); 3267228753Smm fprintf(out, " ilv %d,", 3268228753Smm toi(isodirrec + DR_interleave_offset, DR_interleave_size)); 3269228753Smm fprintf(out, " seq %d,", 3270248616Smm toi(isodirrec + DR_volume_sequence_number_offset, 3271248616Smm DR_volume_sequence_number_size)); 3272228753Smm fprintf(out, " nl %d:", 3273228753Smm toi(isodirrec + DR_name_len_offset, DR_name_len_size)); 3274228753Smm fprintf(out, " `%.*s'", 3275248616Smm toi(isodirrec + DR_name_len_offset, DR_name_len_size), 3276248616Smm isodirrec + DR_name_offset); 3277228753Smm} 3278228753Smm#endif 3279