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