1/*- 2 * Copyright (c) 2003-2007 Tim Kientzle 3 * Copyright (c) 2009 Andreas Henriksson <andreas@fatal.se> 4 * Copyright (c) 2009 Michihiro NAKAJIMA 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28#include "archive_platform.h" 29__FBSDID("$FreeBSD: head/lib/libarchive/archive_read_support_format_iso9660.c 201246 2009-12-30 05:30:35Z kientzle $"); 30 31#ifdef HAVE_ERRNO_H 32#include <errno.h> 33#endif 34/* #include <stdint.h> */ /* See archive_platform.h */ 35#include <stdio.h> 36#ifdef HAVE_STDLIB_H 37#include <stdlib.h> 38#endif 39#ifdef HAVE_STRING_H 40#include <string.h> 41#endif 42#include <time.h> 43#ifdef HAVE_ZLIB_H 44#include <zlib.h> 45#endif 46 47#include "archive.h" 48#include "archive_endian.h" 49#include "archive_entry.h" 50#include "archive_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 int subdirs; 265 uint64_t key; /* Heap Key. */ 266 uint64_t offset; /* Offset on disk. */ 267 uint64_t size; /* File size in bytes. */ 268 uint32_t ce_offset; /* Offset of CE. */ 269 uint32_t ce_size; /* Size of CE. */ 270 char re; /* Having RRIP "RE" extension. */ 271 uint64_t cl_offset; /* Having RRIP "CL" extension. */ 272 int birthtime_is_set; 273 time_t birthtime; /* File created time. */ 274 time_t mtime; /* File last modified time. */ 275 time_t atime; /* File last accessed time. */ 276 time_t ctime; /* File attribute change time. */ 277 uint64_t rdev; /* Device number. */ 278 mode_t mode; 279 uid_t uid; 280 gid_t gid; 281 int64_t number; 282 int nlinks; 283 struct archive_string name; /* Pathname */ 284 char name_continues; /* Non-zero if name continues */ 285 struct archive_string symlink; 286 char symlink_continues; /* Non-zero if link continues */ 287 /* Set 1 if this file compressed by paged zlib(zisofs) */ 288 int pz; 289 int pz_log2_bs; /* Log2 of block size */ 290 uint64_t pz_uncompressed_size; 291 /* Set 1 if this file is multi extent. */ 292 int multi_extent; 293 struct { 294 struct content *first; 295 struct content **last; 296 } contents; 297 char exposed; 298}; 299 300struct heap_queue { 301 struct file_info **files; 302 int allocated; 303 int used; 304}; 305 306struct iso9660 { 307 int magic; 308#define ISO9660_MAGIC 0x96609660 309 310 int opt_support_joliet; 311 int opt_support_rockridge; 312 313 struct archive_string pathname; 314 char seenRockridge; /* Set true if RR extensions are used. */ 315 char seenSUSP; /* Set true if SUSP is beging used. */ 316 char seenJoliet; 317 318 unsigned char suspOffset; 319 struct file_info *rr_moved; 320 struct heap_queue re_dirs; 321 struct heap_queue cl_files; 322 struct read_ce_queue { 323 struct read_ce_req { 324 uint64_t offset;/* Offset of CE on disk. */ 325 struct file_info *file; 326 } *reqs; 327 int cnt; 328 int allocated; 329 } read_ce_req; 330 331 int64_t previous_number; 332 struct archive_string previous_pathname; 333 334 struct file_info *use_files; 335 struct heap_queue pending_files; 336 struct { 337 struct file_info *first; 338 struct file_info **last; 339 } cache_files; 340 341 uint64_t current_position; 342 ssize_t logical_block_size; 343 uint64_t volume_size; /* Total size of volume in bytes. */ 344 int32_t volume_block;/* Total size of volume in logical blocks. */ 345 346 struct vd { 347 int location; /* Location of Extent. */ 348 uint32_t size; 349 } primary, joliet; 350 351 off_t entry_sparse_offset; 352 int64_t entry_bytes_remaining; 353 struct zisofs entry_zisofs; 354 struct content *entry_content; 355}; 356 357static int archive_read_format_iso9660_bid(struct archive_read *); 358static int archive_read_format_iso9660_options(struct archive_read *, 359 const char *, const char *); 360static int archive_read_format_iso9660_cleanup(struct archive_read *); 361static int archive_read_format_iso9660_read_data(struct archive_read *, 362 const void **, size_t *, off_t *); 363static int archive_read_format_iso9660_read_data_skip(struct archive_read *); 364static int archive_read_format_iso9660_read_header(struct archive_read *, 365 struct archive_entry *); 366static const char *build_pathname(struct archive_string *, struct file_info *); 367#if DEBUG 368static void dump_isodirrec(FILE *, const unsigned char *isodirrec); 369#endif 370static time_t time_from_tm(struct tm *); 371static time_t isodate17(const unsigned char *); 372static time_t isodate7(const unsigned char *); 373static int isBootRecord(struct iso9660 *, const unsigned char *); 374static int isVolumePartition(struct iso9660 *, const unsigned char *); 375static int isVDSetTerminator(struct iso9660 *, const unsigned char *); 376static int isJolietSVD(struct iso9660 *, const unsigned char *); 377static int isSVD(struct iso9660 *, const unsigned char *); 378static int isEVD(struct iso9660 *, const unsigned char *); 379static int isPVD(struct iso9660 *, const unsigned char *); 380static struct file_info *next_cache_entry(struct iso9660 *iso9660); 381static int next_entry_seek(struct archive_read *a, struct iso9660 *iso9660, 382 struct file_info **pfile); 383static struct file_info * 384 parse_file_info(struct archive_read *a, 385 struct file_info *parent, const unsigned char *isodirrec); 386static int parse_rockridge(struct archive_read *a, 387 struct file_info *file, const unsigned char *start, 388 const unsigned char *end); 389static int register_CE(struct archive_read *a, int32_t location, 390 struct file_info *file); 391static int read_CE(struct archive_read *a, struct iso9660 *iso9660); 392static void parse_rockridge_NM1(struct file_info *, 393 const unsigned char *, int); 394static void parse_rockridge_SL1(struct file_info *, 395 const unsigned char *, int); 396static void parse_rockridge_TF1(struct file_info *, 397 const unsigned char *, int); 398static void parse_rockridge_ZF1(struct file_info *, 399 const unsigned char *, int); 400static void register_file(struct iso9660 *, struct file_info *); 401static void release_files(struct iso9660 *); 402static unsigned toi(const void *p, int n); 403static inline void cache_add_entry(struct iso9660 *iso9660, 404 struct file_info *file); 405static inline void cache_add_to_next_of_parent(struct iso9660 *iso9660, 406 struct file_info *file); 407static inline struct file_info *cache_get_entry(struct iso9660 *iso9660); 408static int heap_add_entry(struct archive_read *a, struct heap_queue *heap, 409 struct file_info *file, uint64_t key); 410static struct file_info *heap_get_entry(struct heap_queue *heap); 411 412#define add_entry(arch, iso9660, file) \ 413 heap_add_entry(arch, &((iso9660)->pending_files), file, file->offset) 414#define next_entry(iso9660) \ 415 heap_get_entry(&((iso9660)->pending_files)) 416 417int 418archive_read_support_format_iso9660(struct archive *_a) 419{ 420 struct archive_read *a = (struct archive_read *)_a; 421 struct iso9660 *iso9660; 422 int r; 423 424 iso9660 = (struct iso9660 *)malloc(sizeof(*iso9660)); 425 if (iso9660 == NULL) { 426 archive_set_error(&a->archive, ENOMEM, "Can't allocate iso9660 data"); 427 return (ARCHIVE_FATAL); 428 } 429 memset(iso9660, 0, sizeof(*iso9660)); 430 iso9660->magic = ISO9660_MAGIC; 431 iso9660->cache_files.first = NULL; 432 iso9660->cache_files.last = &(iso9660->cache_files.first); 433 /* Enable to support Joliet extensions by default. */ 434 iso9660->opt_support_joliet = 1; 435 /* Enable to support Rock Ridge extensions by default. */ 436 iso9660->opt_support_rockridge = 1; 437 438 r = __archive_read_register_format(a, 439 iso9660, 440 "iso9660", 441 archive_read_format_iso9660_bid, 442 archive_read_format_iso9660_options, 443 archive_read_format_iso9660_read_header, 444 archive_read_format_iso9660_read_data, 445 archive_read_format_iso9660_read_data_skip, 446 archive_read_format_iso9660_cleanup); 447 448 if (r != ARCHIVE_OK) { 449 free(iso9660); 450 return (r); 451 } 452 return (ARCHIVE_OK); 453} 454 455 456static int 457archive_read_format_iso9660_bid(struct archive_read *a) 458{ 459 struct iso9660 *iso9660; 460 ssize_t bytes_read; 461 const void *h; 462 const unsigned char *p; 463 int seenTerminator; 464 465 iso9660 = (struct iso9660 *)(a->format->data); 466 467 /* 468 * Skip the first 32k (reserved area) and get the first 469 * 8 sectors of the volume descriptor table. Of course, 470 * if the I/O layer gives us more, we'll take it. 471 */ 472#define RESERVED_AREA (SYSTEM_AREA_BLOCK * LOGICAL_BLOCK_SIZE) 473 h = __archive_read_ahead(a, 474 RESERVED_AREA + 8 * LOGICAL_BLOCK_SIZE, 475 &bytes_read); 476 if (h == NULL) 477 return (-1); 478 p = (const unsigned char *)h; 479 480 /* Skip the reserved area. */ 481 bytes_read -= RESERVED_AREA; 482 p += RESERVED_AREA; 483 484 /* Check each volume descriptor. */ 485 seenTerminator = 0; 486 for (; bytes_read > LOGICAL_BLOCK_SIZE; 487 bytes_read -= LOGICAL_BLOCK_SIZE, p += LOGICAL_BLOCK_SIZE) { 488 /* Do not handle undefined Volume Descriptor Type. */ 489 if (p[0] >= 4 && p[0] <= 254) 490 return (0); 491 /* Standard Identifier must be "CD001" */ 492 if (memcmp(p + 1, "CD001", 5) != 0) 493 return (0); 494 if (!iso9660->primary.location) { 495 if (isPVD(iso9660, p)) 496 continue; 497 } 498 if (!iso9660->joliet.location) { 499 if (isJolietSVD(iso9660, p)) 500 continue; 501 } 502 if (isBootRecord(iso9660, p)) 503 continue; 504 if (isEVD(iso9660, p)) 505 continue; 506 if (isSVD(iso9660, p)) 507 continue; 508 if (isVolumePartition(iso9660, p)) 509 continue; 510 if (isVDSetTerminator(iso9660, p)) { 511 seenTerminator = 1; 512 break; 513 } 514 return (0); 515 } 516 /* 517 * ISO 9660 format must have Primary Volume Descriptor and 518 * Volume Descriptor Set Terminator. 519 */ 520 if (seenTerminator && iso9660->primary.location > 16) 521 return (48); 522 523 /* We didn't find a valid PVD; return a bid of zero. */ 524 return (0); 525} 526 527static int 528archive_read_format_iso9660_options(struct archive_read *a, 529 const char *key, const char *val) 530{ 531 struct iso9660 *iso9660; 532 533 iso9660 = (struct iso9660 *)(a->format->data); 534 535 if (strcmp(key, "joliet") == 0) { 536 if (val == NULL || strcmp(val, "off") == 0 || 537 strcmp(val, "ignore") == 0 || 538 strcmp(val, "disable") == 0 || 539 strcmp(val, "0") == 0) 540 iso9660->opt_support_joliet = 0; 541 else 542 iso9660->opt_support_joliet = 1; 543 return (ARCHIVE_OK); 544 } 545 if (strcmp(key, "rockridge") == 0 || 546 strcmp(key, "Rockridge") == 0) { 547 iso9660->opt_support_rockridge = val != NULL; 548 return (ARCHIVE_OK); 549 } 550 551 /* Note: The "warn" return is just to inform the options 552 * supervisor that we didn't handle it. It will generate 553 * a suitable error if noone used this option. */ 554 return (ARCHIVE_WARN); 555} 556 557static int 558isBootRecord(struct iso9660 *iso9660, const unsigned char *h) 559{ 560 (void)iso9660; /* UNUSED */ 561 562 /* Type of the Volume Descriptor Boot Record must be 0. */ 563 if (h[0] != 0) 564 return (0); 565 566 /* Volume Descriptor Version must be 1. */ 567 if (h[6] != 1) 568 return (0); 569 570 return (1); 571} 572 573static int 574isVolumePartition(struct iso9660 *iso9660, const unsigned char *h) 575{ 576 int32_t location; 577 578 /* Type of the Volume Partition Descriptor must be 3. */ 579 if (h[0] != 3) 580 return (0); 581 582 /* Volume Descriptor Version must be 1. */ 583 if (h[6] != 1) 584 return (0); 585 /* Unused Field */ 586 if (h[7] != 0) 587 return (0); 588 589 location = archive_le32dec(h + 72); 590 if (location <= SYSTEM_AREA_BLOCK || 591 location >= iso9660->volume_block) 592 return (0); 593 if ((uint32_t)location != archive_be32dec(h + 76)) 594 return (0); 595 596 return (1); 597} 598 599static int 600isVDSetTerminator(struct iso9660 *iso9660, const unsigned char *h) 601{ 602 int i; 603 604 (void)iso9660; /* UNUSED */ 605 606 /* Type of the Volume Descriptor Set Terminator must be 255. */ 607 if (h[0] != 255) 608 return (0); 609 610 /* Volume Descriptor Version must be 1. */ 611 if (h[6] != 1) 612 return (0); 613 614 /* Reserved field must be 0. */ 615 for (i = 7; i < 2048; ++i) 616 if (h[i] != 0) 617 return (0); 618 619 return (1); 620} 621 622static int 623isJolietSVD(struct iso9660 *iso9660, const unsigned char *h) 624{ 625 const unsigned char *p; 626 ssize_t logical_block_size; 627 int32_t volume_block; 628 629 /* Check if current sector is a kind of Supplementary Volume 630 * Descriptor. */ 631 if (!isSVD(iso9660, h)) 632 return (0); 633 634 /* FIXME: do more validations according to joliet spec. */ 635 636 /* check if this SVD contains joliet extension! */ 637 p = h + SVD_escape_sequences_offset; 638 /* N.B. Joliet spec says p[1] == '\\', but.... */ 639 if (p[0] == '%' && p[1] == '/') { 640 int level = 0; 641 642 if (p[2] == '@') 643 level = 1; 644 else if (p[2] == 'C') 645 level = 2; 646 else if (p[2] == 'E') 647 level = 3; 648 else /* not joliet */ 649 return (0); 650 651 iso9660->seenJoliet = level; 652 653 } else /* not joliet */ 654 return (0); 655 656 logical_block_size = 657 archive_le16dec(h + SVD_logical_block_size_offset); 658 volume_block = archive_le32dec(h + SVD_volume_space_size_offset); 659 660 iso9660->logical_block_size = logical_block_size; 661 iso9660->volume_block = volume_block; 662 iso9660->volume_size = logical_block_size * (uint64_t)volume_block; 663 /* Read Root Directory Record in Volume Descriptor. */ 664 p = h + SVD_root_directory_record_offset; 665 iso9660->joliet.location = archive_le32dec(p + DR_extent_offset); 666 iso9660->joliet.size = archive_le32dec(p + DR_size_offset); 667 668 return (48); 669} 670 671static int 672isSVD(struct iso9660 *iso9660, const unsigned char *h) 673{ 674 const unsigned char *p; 675 ssize_t logical_block_size; 676 int32_t volume_block; 677 int32_t location; 678 int i; 679 680 (void)iso9660; /* UNUSED */ 681 682 /* Type 2 means it's a SVD. */ 683 if (h[SVD_type_offset] != 2) 684 return (0); 685 686 /* Reserved field must be 0. */ 687 for (i = 0; i < SVD_reserved1_size; ++i) 688 if (h[SVD_reserved1_offset + i] != 0) 689 return (0); 690 for (i = 0; i < SVD_reserved2_size; ++i) 691 if (h[SVD_reserved2_offset + i] != 0) 692 return (0); 693 for (i = 0; i < SVD_reserved3_size; ++i) 694 if (h[SVD_reserved3_offset + i] != 0) 695 return (0); 696 697 /* File structure version must be 1 for ISO9660/ECMA119. */ 698 if (h[SVD_file_structure_version_offset] != 1) 699 return (0); 700 701 logical_block_size = 702 archive_le16dec(h + SVD_logical_block_size_offset); 703 if (logical_block_size <= 0) 704 return (0); 705 706 volume_block = archive_le32dec(h + SVD_volume_space_size_offset); 707 if (volume_block <= SYSTEM_AREA_BLOCK+4) 708 return (0); 709 710 /* Location of Occurrence of Type L Path Table must be 711 * available location, 712 * > SYSTEM_AREA_BLOCK(16) + 2 and < Volume Space Size. */ 713 location = archive_le32dec(h+SVD_type_L_path_table_offset); 714 if (location <= SYSTEM_AREA_BLOCK+2 || location >= volume_block) 715 return (0); 716 717 /* Location of Occurrence of Type M Path Table must be 718 * available location, 719 * > SYSTEM_AREA_BLOCK(16) + 2 and < Volume Space Size. */ 720 location = archive_be32dec(h+SVD_type_M_path_table_offset); 721 if (location <= SYSTEM_AREA_BLOCK+2 || location >= volume_block) 722 return (0); 723 724 /* Read Root Directory Record in Volume Descriptor. */ 725 p = h + SVD_root_directory_record_offset; 726 if (p[DR_length_offset] != 34) 727 return (0); 728 729 return (48); 730} 731 732static int 733isEVD(struct iso9660 *iso9660, const unsigned char *h) 734{ 735 const unsigned char *p; 736 ssize_t logical_block_size; 737 int32_t volume_block; 738 int32_t location; 739 int i; 740 741 (void)iso9660; /* UNUSED */ 742 743 /* Type of the Enhanced Volume Descriptor must be 2. */ 744 if (h[PVD_type_offset] != 2) 745 return (0); 746 747 /* EVD version must be 2. */ 748 if (h[PVD_version_offset] != 2) 749 return (0); 750 751 /* Reserved field must be 0. */ 752 if (h[PVD_reserved1_offset] != 0) 753 return (0); 754 755 /* Reserved field must be 0. */ 756 for (i = 0; i < PVD_reserved2_size; ++i) 757 if (h[PVD_reserved2_offset + i] != 0) 758 return (0); 759 760 /* Reserved field must be 0. */ 761 for (i = 0; i < PVD_reserved3_size; ++i) 762 if (h[PVD_reserved3_offset + i] != 0) 763 return (0); 764 765 /* Logical block size must be > 0. */ 766 /* I've looked at Ecma 119 and can't find any stronger 767 * restriction on this field. */ 768 logical_block_size = 769 archive_le16dec(h + PVD_logical_block_size_offset); 770 if (logical_block_size <= 0) 771 return (0); 772 773 volume_block = 774 archive_le32dec(h + PVD_volume_space_size_offset); 775 if (volume_block <= SYSTEM_AREA_BLOCK+4) 776 return (0); 777 778 /* File structure version must be 2 for ISO9660:1999. */ 779 if (h[PVD_file_structure_version_offset] != 2) 780 return (0); 781 782 /* Location of Occurrence of Type L Path Table must be 783 * available location, 784 * > SYSTEM_AREA_BLOCK(16) + 2 and < Volume Space Size. */ 785 location = archive_le32dec(h+PVD_type_1_path_table_offset); 786 if (location <= SYSTEM_AREA_BLOCK+2 || location >= volume_block) 787 return (0); 788 789 /* Location of Occurrence of Type M Path Table must be 790 * available location, 791 * > SYSTEM_AREA_BLOCK(16) + 2 and < Volume Space Size. */ 792 location = archive_be32dec(h+PVD_type_m_path_table_offset); 793 if (location <= SYSTEM_AREA_BLOCK+2 || location >= volume_block) 794 return (0); 795 796 /* Reserved field must be 0. */ 797 for (i = 0; i < PVD_reserved4_size; ++i) 798 if (h[PVD_reserved4_offset + i] != 0) 799 return (0); 800 801 /* Reserved field must be 0. */ 802 for (i = 0; i < PVD_reserved5_size; ++i) 803 if (h[PVD_reserved5_offset + i] != 0) 804 return (0); 805 806 /* Read Root Directory Record in Volume Descriptor. */ 807 p = h + PVD_root_directory_record_offset; 808 if (p[DR_length_offset] != 34) 809 return (0); 810 811 return (48); 812} 813 814static int 815isPVD(struct iso9660 *iso9660, const unsigned char *h) 816{ 817 const unsigned char *p; 818 ssize_t logical_block_size; 819 int32_t volume_block; 820 int32_t location; 821 int i; 822 823 /* Type of the Primary Volume Descriptor must be 1. */ 824 if (h[PVD_type_offset] != 1) 825 return (0); 826 827 /* PVD version must be 1. */ 828 if (h[PVD_version_offset] != 1) 829 return (0); 830 831 /* Reserved field must be 0. */ 832 if (h[PVD_reserved1_offset] != 0) 833 return (0); 834 835 /* Reserved field must be 0. */ 836 for (i = 0; i < PVD_reserved2_size; ++i) 837 if (h[PVD_reserved2_offset + i] != 0) 838 return (0); 839 840 /* Reserved field must be 0. */ 841 for (i = 0; i < PVD_reserved3_size; ++i) 842 if (h[PVD_reserved3_offset + i] != 0) 843 return (0); 844 845 /* Logical block size must be > 0. */ 846 /* I've looked at Ecma 119 and can't find any stronger 847 * restriction on this field. */ 848 logical_block_size = 849 archive_le16dec(h + PVD_logical_block_size_offset); 850 if (logical_block_size <= 0) 851 return (0); 852 853 volume_block = archive_le32dec(h + PVD_volume_space_size_offset); 854 if (volume_block <= SYSTEM_AREA_BLOCK+4) 855 return (0); 856 857 /* File structure version must be 1 for ISO9660/ECMA119. */ 858 if (h[PVD_file_structure_version_offset] != 1) 859 return (0); 860 861 /* Location of Occurrence of Type L Path Table must be 862 * available location, 863 * > SYSTEM_AREA_BLOCK(16) + 2 and < Volume Space Size. */ 864 location = archive_le32dec(h+PVD_type_1_path_table_offset); 865 if (location <= SYSTEM_AREA_BLOCK+2 || location >= volume_block) 866 return (0); 867 868 /* Location of Occurrence of Type M Path Table must be 869 * available location, 870 * > SYSTEM_AREA_BLOCK(16) + 2 and < Volume Space Size. */ 871 location = archive_be32dec(h+PVD_type_m_path_table_offset); 872 if (location <= SYSTEM_AREA_BLOCK+2 || location >= volume_block) 873 return (0); 874 875 /* Reserved field must be 0. */ 876 for (i = 0; i < PVD_reserved4_size; ++i) 877 if (h[PVD_reserved4_offset + i] != 0) 878 return (0); 879 880 /* Reserved field must be 0. */ 881 for (i = 0; i < PVD_reserved5_size; ++i) 882 if (h[PVD_reserved5_offset + i] != 0) 883 return (0); 884 885 /* XXX TODO: Check other values for sanity; reject more 886 * malformed PVDs. XXX */ 887 888 /* Read Root Directory Record in Volume Descriptor. */ 889 p = h + PVD_root_directory_record_offset; 890 if (p[DR_length_offset] != 34) 891 return (0); 892 893 iso9660->logical_block_size = logical_block_size; 894 iso9660->volume_block = volume_block; 895 iso9660->volume_size = logical_block_size * (uint64_t)volume_block; 896 iso9660->primary.location = archive_le32dec(p + DR_extent_offset); 897 iso9660->primary.size = archive_le32dec(p + DR_size_offset); 898 899 return (48); 900} 901 902static int 903read_children(struct archive_read *a, struct file_info *parent) 904{ 905 struct iso9660 *iso9660; 906 const unsigned char *b, *p; 907 struct file_info *multi; 908 size_t step; 909 910 iso9660 = (struct iso9660 *)(a->format->data); 911 if (iso9660->current_position > parent->offset) { 912 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 913 "Ignoring out-of-order directory (%s) %jd > %jd", 914 parent->name.s, 915 iso9660->current_position, 916 parent->offset); 917 return (ARCHIVE_WARN); 918 } 919 if (parent->offset + parent->size > iso9660->volume_size) { 920 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 921 "Directory is beyond end-of-media: %s", 922 parent->name); 923 return (ARCHIVE_WARN); 924 } 925 if (iso9660->current_position < parent->offset) { 926 int64_t skipsize; 927 928 skipsize = parent->offset - iso9660->current_position; 929 skipsize = __archive_read_skip(a, skipsize); 930 if (skipsize < 0) 931 return ((int)skipsize); 932 iso9660->current_position = parent->offset; 933 } 934 935 step = ((parent->size + iso9660->logical_block_size -1) / 936 iso9660->logical_block_size) * iso9660->logical_block_size; 937 b = __archive_read_ahead(a, step, NULL); 938 if (b == NULL) { 939 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 940 "Failed to read full block when scanning " 941 "ISO9660 directory list"); 942 return (ARCHIVE_FATAL); 943 } 944 __archive_read_consume(a, step); 945 iso9660->current_position += step; 946 multi = NULL; 947 while (step) { 948 p = b; 949 b += iso9660->logical_block_size; 950 step -= iso9660->logical_block_size; 951 for (; *p != 0 && p < b && p + *p <= b; p += *p) { 952 struct file_info *child; 953 954 /* N.B.: these special directory identifiers 955 * are 8 bit "values" even on a 956 * Joliet CD with UCS-2 (16bit) encoding. 957 */ 958 959 /* Skip '.' entry. */ 960 if (*(p + DR_name_len_offset) == 1 961 && *(p + DR_name_offset) == '\0') 962 continue; 963 /* Skip '..' entry. */ 964 if (*(p + DR_name_len_offset) == 1 965 && *(p + DR_name_offset) == '\001') 966 continue; 967 child = parse_file_info(a, parent, p); 968 if (child == NULL) 969 return (ARCHIVE_FATAL); 970 if (child->cl_offset) { 971 if (heap_add_entry(a, &(iso9660->cl_files), 972 child, child->cl_offset) != ARCHIVE_OK) 973 return (ARCHIVE_FATAL); 974 } else { 975 if (child->multi_extent || multi != NULL) { 976 struct content *con; 977 978 if (multi == NULL) { 979 multi = child; 980 multi->contents.first = NULL; 981 multi->contents.last = 982 &(multi->contents.first); 983 } 984 con = malloc(sizeof(struct content)); 985 if (con == NULL) { 986 archive_set_error( 987 &a->archive, ENOMEM, 988 "No memory for " 989 "multi extent"); 990 return (ARCHIVE_FATAL); 991 } 992 con->offset = child->offset; 993 con->size = child->size; 994 con->next = NULL; 995 *multi->contents.last = con; 996 multi->contents.last = &(con->next); 997 if (multi == child) { 998 if (add_entry(a, iso9660, child) 999 != ARCHIVE_OK) 1000 return (ARCHIVE_FATAL); 1001 } else { 1002 multi->size += child->size; 1003 if (!child->multi_extent) 1004 multi = NULL; 1005 } 1006 } else { 1007 if (add_entry(a, iso9660, child) 1008 != ARCHIVE_OK) 1009 return (ARCHIVE_FATAL); 1010 } 1011 } 1012 } 1013 } 1014 1015 /* Read data which recorded by RRIP "CE" extension. */ 1016 if (read_CE(a, iso9660) != ARCHIVE_OK) 1017 return (ARCHIVE_FATAL); 1018 1019 return (ARCHIVE_OK); 1020} 1021 1022static int 1023relocate_dir(struct archive_read *a, struct iso9660 *iso9660, 1024 struct file_info *file) 1025{ 1026 struct file_info *re; 1027 1028 re = heap_get_entry(&(iso9660->re_dirs)); 1029 while (re != NULL && re->offset < file->cl_offset) { 1030 /* This case is wrong pattern. 1031 * But dont't reject this directory entry to be robust. */ 1032 cache_add_entry(iso9660, re); 1033 re = heap_get_entry(&(iso9660->re_dirs)); 1034 } 1035 if (re == NULL) 1036 /* This case is wrong pattern. */ 1037 return (0); 1038 if (re->offset == file->cl_offset) { 1039 re->parent->subdirs--; 1040 re->parent = file->parent; 1041 re->parent->subdirs++; 1042 cache_add_to_next_of_parent(iso9660, re); 1043 return (1); 1044 } else 1045 /* This case is wrong pattern. */ 1046 if (heap_add_entry(a, &(iso9660->re_dirs), re, re->offset) 1047 != ARCHIVE_OK) 1048 return (ARCHIVE_FATAL); 1049 return (0); 1050} 1051 1052static int 1053read_entries(struct archive_read *a) 1054{ 1055 struct iso9660 *iso9660; 1056 struct file_info *file; 1057 int r; 1058 1059 iso9660 = (struct iso9660 *)(a->format->data); 1060 1061 while ((file = next_entry(iso9660)) != NULL && 1062 (file->mode & AE_IFMT) == AE_IFDIR) { 1063 r = read_children(a, file); 1064 if (r != ARCHIVE_OK) 1065 return (r); 1066 1067 if (iso9660->seenRockridge && 1068 file->parent != NULL && 1069 file->parent->parent == NULL && 1070 iso9660->rr_moved == NULL && 1071 (strcmp(file->name.s, "rr_moved") == 0 || 1072 strcmp(file->name.s, ".rr_moved") == 0)) { 1073 iso9660->rr_moved = file; 1074 } else if (file->re) { 1075 if (heap_add_entry(a, &(iso9660->re_dirs), file, 1076 file->offset) != ARCHIVE_OK) 1077 return (ARCHIVE_FATAL); 1078 } else { 1079 cache_add_entry(iso9660, file); 1080 } 1081 } 1082 if (file != NULL) 1083 if (add_entry(a, iso9660, file) != ARCHIVE_OK) 1084 return (ARCHIVE_FATAL); 1085 1086 if (iso9660->rr_moved != NULL) { 1087 /* 1088 * Relocate directory which rr_moved has. 1089 */ 1090 while ((file = heap_get_entry(&(iso9660->cl_files))) != NULL) 1091 if (relocate_dir(a, iso9660, file) != ARCHIVE_OK) 1092 return ARCHIVE_FATAL; 1093 1094 /* If rr_moved directory still has children, 1095 * Add rr_moved into pending_files to show 1096 */ 1097 if (iso9660->rr_moved->subdirs) { 1098 cache_add_entry(iso9660, iso9660->rr_moved); 1099 /* If entries which have "RE" extension are still 1100 * remaining(this case is unlikely except ISO image 1101 * is broken), the entries won't be exposed. */ 1102 while ((file = heap_get_entry(&(iso9660->re_dirs))) != NULL) 1103 cache_add_entry(iso9660, file); 1104 } else 1105 iso9660->rr_moved->parent->subdirs--; 1106 } else { 1107 /* 1108 * In case ISO image is broken. If the name of rr_moved 1109 * directory has been changed by damage, subdirectories 1110 * of rr_moved entry won't be exposed. 1111 */ 1112 while ((file = heap_get_entry(&(iso9660->re_dirs))) != NULL) 1113 cache_add_entry(iso9660, file); 1114 } 1115 1116 return (ARCHIVE_OK); 1117} 1118 1119static int 1120archive_read_format_iso9660_read_header(struct archive_read *a, 1121 struct archive_entry *entry) 1122{ 1123 struct iso9660 *iso9660; 1124 struct file_info *file; 1125 int r, rd_r; 1126 1127 iso9660 = (struct iso9660 *)(a->format->data); 1128 1129 if (!a->archive.archive_format) { 1130 a->archive.archive_format = ARCHIVE_FORMAT_ISO9660; 1131 a->archive.archive_format_name = "ISO9660"; 1132 } 1133 1134 if (iso9660->current_position == 0) { 1135 int64_t skipsize; 1136 struct vd *vd; 1137 const void *block; 1138 char seenJoliet; 1139 1140 vd = &(iso9660->primary); 1141 if (!iso9660->opt_support_joliet) 1142 iso9660->seenJoliet = 0; 1143 if (iso9660->seenJoliet && 1144 vd->location > iso9660->joliet.location) 1145 /* This condition is unlikely; by way of caution. */ 1146 vd = &(iso9660->joliet); 1147 1148 skipsize = LOGICAL_BLOCK_SIZE * vd->location; 1149 skipsize = __archive_read_skip(a, skipsize); 1150 if (skipsize < 0) 1151 return ((int)skipsize); 1152 iso9660->current_position = skipsize; 1153 1154 block = __archive_read_ahead(a, vd->size, NULL); 1155 if (block == NULL) { 1156 archive_set_error(&a->archive, 1157 ARCHIVE_ERRNO_MISC, 1158 "Failed to read full block when scanning " 1159 "ISO9660 directory list"); 1160 return (ARCHIVE_FATAL); 1161 } 1162 1163 /* 1164 * While reading Root Directory, flag seenJoliet 1165 * must be zero to avoid converting special name 1166 * 0x00(Current Directory) and next byte to UCS2. 1167 */ 1168 seenJoliet = iso9660->seenJoliet;/* Save flag. */ 1169 iso9660->seenJoliet = 0; 1170 file = parse_file_info(a, NULL, block); 1171 if (file == NULL) 1172 return (ARCHIVE_FATAL); 1173 iso9660->seenJoliet = seenJoliet; 1174 if (vd == &(iso9660->primary) && iso9660->seenRockridge 1175 && iso9660->seenJoliet) 1176 /* 1177 * If iso image has RockRidge and Joliet, 1178 * we use RockRidge Extensions. 1179 */ 1180 iso9660->seenJoliet = 0; 1181 if (vd == &(iso9660->primary) && !iso9660->seenRockridge 1182 && iso9660->seenJoliet) { 1183 /* Switch reading data from primary to joliet. */ 1184 vd = &(iso9660->joliet); 1185 skipsize = LOGICAL_BLOCK_SIZE * vd->location; 1186 skipsize -= iso9660->current_position; 1187 skipsize = __archive_read_skip(a, skipsize); 1188 if (skipsize < 0) 1189 return ((int)skipsize); 1190 iso9660->current_position += skipsize; 1191 1192 block = __archive_read_ahead(a, vd->size, NULL); 1193 if (block == NULL) { 1194 archive_set_error(&a->archive, 1195 ARCHIVE_ERRNO_MISC, 1196 "Failed to read full block when scanning " 1197 "ISO9660 directory list"); 1198 return (ARCHIVE_FATAL); 1199 } 1200 seenJoliet = iso9660->seenJoliet;/* Save flag. */ 1201 iso9660->seenJoliet = 0; 1202 file = parse_file_info(a, NULL, block); 1203 if (file == NULL) 1204 return (ARCHIVE_FATAL); 1205 iso9660->seenJoliet = seenJoliet; 1206 } 1207 /* Store the root directory in the pending list. */ 1208 if (add_entry(a, iso9660, file) != ARCHIVE_OK) 1209 return (ARCHIVE_FATAL); 1210 if (iso9660->seenRockridge) { 1211 a->archive.archive_format = 1212 ARCHIVE_FORMAT_ISO9660_ROCKRIDGE; 1213 a->archive.archive_format_name = 1214 "ISO9660 with Rockridge extensions"; 1215 } 1216 rd_r = read_entries(a); 1217 if (rd_r == ARCHIVE_FATAL) 1218 return (ARCHIVE_FATAL); 1219 } else 1220 rd_r = ARCHIVE_OK; 1221 1222 /* Get the next entry that appears after the current offset. */ 1223 r = next_entry_seek(a, iso9660, &file); 1224 if (r != ARCHIVE_OK) 1225 return (r); 1226 1227 iso9660->entry_bytes_remaining = file->size; 1228 iso9660->entry_sparse_offset = 0; /* Offset for sparse-file-aware clients. */ 1229 1230 if (file->offset + file->size > iso9660->volume_size) { 1231 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 1232 "File is beyond end-of-media: %s", file->name); 1233 iso9660->entry_bytes_remaining = 0; 1234 iso9660->entry_sparse_offset = 0; 1235 return (ARCHIVE_WARN); 1236 } 1237 1238 /* Set up the entry structure with information about this entry. */ 1239 archive_entry_set_mode(entry, file->mode); 1240 archive_entry_set_uid(entry, file->uid); 1241 archive_entry_set_gid(entry, file->gid); 1242 archive_entry_set_nlink(entry, file->nlinks); 1243 if (file->birthtime_is_set) 1244 archive_entry_set_birthtime(entry, file->birthtime, 0); 1245 else 1246 archive_entry_unset_birthtime(entry); 1247 archive_entry_set_mtime(entry, file->mtime, 0); 1248 archive_entry_set_ctime(entry, file->ctime, 0); 1249 archive_entry_set_atime(entry, file->atime, 0); 1250 /* N.B.: Rock Ridge supports 64-bit device numbers. */ 1251 archive_entry_set_rdev(entry, (dev_t)file->rdev); 1252 archive_entry_set_size(entry, iso9660->entry_bytes_remaining); 1253 archive_string_empty(&iso9660->pathname); 1254 archive_entry_set_pathname(entry, 1255 build_pathname(&iso9660->pathname, file)); 1256 if (file->symlink.s != NULL) 1257 archive_entry_copy_symlink(entry, file->symlink.s); 1258 1259 /* Note: If the input isn't seekable, we can't rewind to 1260 * return the same body again, so if the next entry refers to 1261 * the same data, we have to return it as a hardlink to the 1262 * original entry. */ 1263 if (file->number != -1 && 1264 file->number == iso9660->previous_number) { 1265 archive_entry_set_hardlink(entry, 1266 iso9660->previous_pathname.s); 1267 archive_entry_unset_size(entry); 1268 iso9660->entry_bytes_remaining = 0; 1269 iso9660->entry_sparse_offset = 0; 1270 return (ARCHIVE_OK); 1271 } 1272 1273 /* Except for the hardlink case above, if the offset of the 1274 * next entry is before our current position, we can't seek 1275 * backwards to extract it, so issue a warning. Note that 1276 * this can only happen if this entry was added to the heap 1277 * after we passed this offset, that is, only if the directory 1278 * mentioning this entry is later than the body of the entry. 1279 * Such layouts are very unusual; most ISO9660 writers lay out 1280 * and record all directory information first, then store 1281 * all file bodies. */ 1282 /* TODO: Someday, libarchive's I/O core will support optional 1283 * seeking. When that day comes, this code should attempt to 1284 * seek and only return the error if the seek fails. That 1285 * will give us support for whacky ISO images that require 1286 * seeking while retaining the ability to read almost all ISO 1287 * images in a streaming fashion. */ 1288 if ((file->mode & AE_IFMT) != AE_IFDIR && 1289 file->offset < iso9660->current_position) { 1290 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 1291 "Ignoring out-of-order file @%x (%s) %jd < %jd", 1292 file, 1293 iso9660->pathname.s, 1294 file->offset, iso9660->current_position); 1295 iso9660->entry_bytes_remaining = 0; 1296 iso9660->entry_sparse_offset = 0; 1297 return (ARCHIVE_WARN); 1298 } 1299 1300 /* Initialize zisofs variables. */ 1301 iso9660->entry_zisofs.pz = file->pz; 1302 if (file->pz) { 1303#ifdef HAVE_ZLIB_H 1304 struct zisofs *zisofs; 1305 1306 zisofs = &iso9660->entry_zisofs; 1307 zisofs->initialized = 0; 1308 zisofs->pz_log2_bs = file->pz_log2_bs; 1309 zisofs->pz_uncompressed_size = file->pz_uncompressed_size; 1310 zisofs->pz_offset = 0; 1311 zisofs->header_avail = 0; 1312 zisofs->header_passed = 0; 1313 zisofs->block_pointers_avail = 0; 1314#endif 1315 archive_entry_set_size(entry, file->pz_uncompressed_size); 1316 } 1317 1318 iso9660->previous_number = file->number; 1319 archive_strcpy(&iso9660->previous_pathname, iso9660->pathname.s); 1320 1321 /* Reset entry_bytes_remaining if the file is multi extent. */ 1322 iso9660->entry_content = file->contents.first; 1323 if (iso9660->entry_content != NULL) 1324 iso9660->entry_bytes_remaining = iso9660->entry_content->size; 1325 1326 if (archive_entry_filetype(entry) == AE_IFDIR) { 1327 /* Overwrite nlinks by proper link number which is 1328 * calculated from number of sub directories. */ 1329 archive_entry_set_nlink(entry, 2 + file->subdirs); 1330 /* Directory data has been read completely. */ 1331 iso9660->entry_bytes_remaining = 0; 1332 iso9660->entry_sparse_offset = 0; 1333 file->exposed = 1; 1334 } 1335 1336 if (rd_r != ARCHIVE_OK) 1337 return (rd_r); 1338 return (ARCHIVE_OK); 1339} 1340 1341static int 1342archive_read_format_iso9660_read_data_skip(struct archive_read *a) 1343{ 1344 /* Because read_next_header always does an explicit skip 1345 * to the next entry, we don't need to do anything here. */ 1346 (void)a; /* UNUSED */ 1347 return (ARCHIVE_OK); 1348} 1349 1350#ifdef HAVE_ZLIB_H 1351 1352static int 1353zisofs_read_data(struct archive_read *a, 1354 const void **buff, size_t *size, off_t *offset) 1355{ 1356 struct iso9660 *iso9660; 1357 struct zisofs *zisofs; 1358 const unsigned char *p; 1359 size_t avail; 1360 ssize_t bytes_read; 1361 size_t uncompressed_size; 1362 int r; 1363 1364 iso9660 = (struct iso9660 *)(a->format->data); 1365 zisofs = &iso9660->entry_zisofs; 1366 1367 p = __archive_read_ahead(a, 1, &bytes_read); 1368 if (bytes_read <= 0) { 1369 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, 1370 "Truncated zisofs file body"); 1371 return (ARCHIVE_FATAL); 1372 } 1373 if (bytes_read > iso9660->entry_bytes_remaining) 1374 bytes_read = iso9660->entry_bytes_remaining; 1375 avail = bytes_read; 1376 uncompressed_size = 0; 1377 1378 if (!zisofs->initialized) { 1379 size_t ceil, xsize; 1380 1381 /* Allocate block pointers buffer. */ 1382 ceil = (zisofs->pz_uncompressed_size + 1383 (1LL << zisofs->pz_log2_bs) - 1) 1384 >> zisofs->pz_log2_bs; 1385 xsize = (ceil + 1) * 4; 1386 if (zisofs->block_pointers_alloc < xsize) { 1387 size_t alloc; 1388 1389 if (zisofs->block_pointers != NULL) 1390 free(zisofs->block_pointers); 1391 alloc = ((xsize >> 10) + 1) << 10; 1392 zisofs->block_pointers = malloc(alloc); 1393 if (zisofs->block_pointers == NULL) { 1394 archive_set_error(&a->archive, ENOMEM, 1395 "No memory for zisofs decompression"); 1396 return (ARCHIVE_FATAL); 1397 } 1398 zisofs->block_pointers_alloc = alloc; 1399 } 1400 zisofs->block_pointers_size = xsize; 1401 1402 /* Allocate uncompressed data buffer. */ 1403 xsize = 1UL << zisofs->pz_log2_bs; 1404 if (zisofs->uncompressed_buffer_size < xsize) { 1405 if (zisofs->uncompressed_buffer != NULL) 1406 free(zisofs->uncompressed_buffer); 1407 zisofs->uncompressed_buffer = malloc(xsize); 1408 if (zisofs->uncompressed_buffer == NULL) { 1409 archive_set_error(&a->archive, ENOMEM, 1410 "No memory for zisofs decompression"); 1411 return (ARCHIVE_FATAL); 1412 } 1413 } 1414 zisofs->uncompressed_buffer_size = xsize; 1415 1416 /* 1417 * Read the file header, and check the magic code of zisofs. 1418 */ 1419 if (zisofs->header_avail < sizeof(zisofs->header)) { 1420 xsize = sizeof(zisofs->header) - zisofs->header_avail; 1421 if (avail < xsize) 1422 xsize = avail; 1423 memcpy(zisofs->header + zisofs->header_avail, p, xsize); 1424 zisofs->header_avail += xsize; 1425 avail -= xsize; 1426 p += xsize; 1427 } 1428 if (!zisofs->header_passed && 1429 zisofs->header_avail == sizeof(zisofs->header)) { 1430 int err = 0; 1431 1432 if (memcmp(zisofs->header, zisofs_magic, 1433 sizeof(zisofs_magic)) != 0) 1434 err = 1; 1435 if (archive_le32dec(zisofs->header + 8) 1436 != zisofs->pz_uncompressed_size) 1437 err = 1; 1438 if (zisofs->header[12] != 4) 1439 err = 1; 1440 if (zisofs->header[13] != zisofs->pz_log2_bs) 1441 err = 1; 1442 if (err) { 1443 archive_set_error(&a->archive, 1444 ARCHIVE_ERRNO_FILE_FORMAT, 1445 "Illegal zisofs file body"); 1446 return (ARCHIVE_FATAL); 1447 } 1448 zisofs->header_passed = 1; 1449 } 1450 /* 1451 * Read block pointers. 1452 */ 1453 if (zisofs->header_passed && 1454 zisofs->block_pointers_avail < zisofs->block_pointers_size) { 1455 xsize = zisofs->block_pointers_size 1456 - zisofs->block_pointers_avail; 1457 if (avail < xsize) 1458 xsize = avail; 1459 memcpy(zisofs->block_pointers 1460 + zisofs->block_pointers_avail, p, xsize); 1461 zisofs->block_pointers_avail += xsize; 1462 avail -= xsize; 1463 p += xsize; 1464 if (zisofs->block_pointers_avail 1465 == zisofs->block_pointers_size) { 1466 /* We've got all block pointers and initialize 1467 * related variables. */ 1468 zisofs->block_off = 0; 1469 zisofs->block_avail = 0; 1470 /* Complete a initialization */ 1471 zisofs->initialized = 1; 1472 } 1473 } 1474 1475 if (!zisofs->initialized) 1476 goto next_data; /* We need more datas. */ 1477 } 1478 1479 /* 1480 * Get block offsets from block pointers. 1481 */ 1482 if (zisofs->block_avail == 0) { 1483 uint32_t bst, bed; 1484 1485 if (zisofs->block_off + 4 >= zisofs->block_pointers_size) { 1486 /* There isn't a pair of offsets. */ 1487 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, 1488 "Illegal zisofs block pointers"); 1489 return (ARCHIVE_FATAL); 1490 } 1491 bst = archive_le32dec(zisofs->block_pointers + zisofs->block_off); 1492 if (bst != zisofs->pz_offset + (bytes_read - avail)) { 1493 /* TODO: Should we seek offset of current file by bst ? */ 1494 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, 1495 "Illegal zisofs block pointers(cannot seek)"); 1496 return (ARCHIVE_FATAL); 1497 } 1498 bed = archive_le32dec( 1499 zisofs->block_pointers + zisofs->block_off + 4); 1500 if (bed < bst) { 1501 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, 1502 "Illegal zisofs block pointers"); 1503 return (ARCHIVE_FATAL); 1504 } 1505 zisofs->block_avail = bed - bst; 1506 zisofs->block_off += 4; 1507 1508 /* Initialize compression library for new block. */ 1509 if (zisofs->stream_valid) 1510 r = inflateReset(&zisofs->stream); 1511 else 1512 r = inflateInit(&zisofs->stream); 1513 if (r != Z_OK) { 1514 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 1515 "Can't initialize zisofs decompression."); 1516 return (ARCHIVE_FATAL); 1517 } 1518 zisofs->stream_valid = 1; 1519 zisofs->stream.total_in = 0; 1520 zisofs->stream.total_out = 0; 1521 } 1522 1523 /* 1524 * Make uncompressed datas. 1525 */ 1526 if (zisofs->block_avail == 0) { 1527 memset(zisofs->uncompressed_buffer, 0, 1528 zisofs->uncompressed_buffer_size); 1529 uncompressed_size = zisofs->uncompressed_buffer_size; 1530 } else { 1531 zisofs->stream.next_in = (Bytef *)(uintptr_t)(const void *)p; 1532 if (avail > zisofs->block_avail) 1533 zisofs->stream.avail_in = zisofs->block_avail; 1534 else 1535 zisofs->stream.avail_in = avail; 1536 zisofs->stream.next_out = zisofs->uncompressed_buffer; 1537 zisofs->stream.avail_out = zisofs->uncompressed_buffer_size; 1538 1539 r = inflate(&zisofs->stream, 0); 1540 switch (r) { 1541 case Z_OK: /* Decompressor made some progress.*/ 1542 case Z_STREAM_END: /* Found end of stream. */ 1543 break; 1544 default: 1545 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 1546 "zisofs decompression failed (%d)", r); 1547 return (ARCHIVE_FATAL); 1548 } 1549 uncompressed_size = 1550 zisofs->uncompressed_buffer_size - zisofs->stream.avail_out; 1551 avail -= zisofs->stream.next_in - p; 1552 zisofs->block_avail -= zisofs->stream.next_in - p; 1553 } 1554next_data: 1555 bytes_read -= avail; 1556 *buff = zisofs->uncompressed_buffer; 1557 *size = uncompressed_size; 1558 *offset = iso9660->entry_sparse_offset; 1559 iso9660->entry_sparse_offset += uncompressed_size; 1560 iso9660->entry_bytes_remaining -= bytes_read; 1561 iso9660->current_position += bytes_read; 1562 zisofs->pz_offset += bytes_read; 1563 __archive_read_consume(a, bytes_read); 1564 1565 return (ARCHIVE_OK); 1566} 1567 1568#else /* HAVE_ZLIB_H */ 1569 1570static int 1571zisofs_read_data(struct archive_read *a, 1572 const void **buff, size_t *size, off_t *offset) 1573{ 1574 1575 (void)buff;/* UNUSED */ 1576 (void)size;/* UNUSED */ 1577 (void)offset;/* UNUSED */ 1578 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, 1579 "zisofs is not supported on this platform."); 1580 return (ARCHIVE_FAILED); 1581} 1582 1583#endif /* HAVE_ZLIB_H */ 1584 1585static int 1586archive_read_format_iso9660_read_data(struct archive_read *a, 1587 const void **buff, size_t *size, off_t *offset) 1588{ 1589 ssize_t bytes_read; 1590 struct iso9660 *iso9660; 1591 1592 iso9660 = (struct iso9660 *)(a->format->data); 1593 if (iso9660->entry_bytes_remaining <= 0) { 1594 if (iso9660->entry_content != NULL) 1595 iso9660->entry_content = iso9660->entry_content->next; 1596 if (iso9660->entry_content == NULL) { 1597 *buff = NULL; 1598 *size = 0; 1599 *offset = iso9660->entry_sparse_offset; 1600 return (ARCHIVE_EOF); 1601 } 1602 /* Seek forward to the start of the entry. */ 1603 if (iso9660->current_position < iso9660->entry_content->offset) { 1604 int64_t step; 1605 1606 step = iso9660->entry_content->offset - 1607 iso9660->current_position; 1608 step = __archive_read_skip(a, step); 1609 if (step < 0) 1610 return ((int)step); 1611 iso9660->current_position = 1612 iso9660->entry_content->offset; 1613 } 1614 if (iso9660->entry_content->offset < iso9660->current_position) { 1615 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 1616 "Ignoring out-of-order file (%s) %jd < %jd", 1617 iso9660->pathname.s, 1618 iso9660->entry_content->offset, 1619 iso9660->current_position); 1620 *buff = NULL; 1621 *size = 0; 1622 *offset = iso9660->entry_sparse_offset; 1623 return (ARCHIVE_WARN); 1624 } 1625 iso9660->entry_bytes_remaining = iso9660->entry_content->size; 1626 } 1627 if (iso9660->entry_zisofs.pz) 1628 return (zisofs_read_data(a, buff, size, offset)); 1629 1630 *buff = __archive_read_ahead(a, 1, &bytes_read); 1631 if (bytes_read == 0) 1632 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 1633 "Truncated input file"); 1634 if (*buff == NULL) 1635 return (ARCHIVE_FATAL); 1636 if (bytes_read > iso9660->entry_bytes_remaining) 1637 bytes_read = iso9660->entry_bytes_remaining; 1638 *size = bytes_read; 1639 *offset = iso9660->entry_sparse_offset; 1640 iso9660->entry_sparse_offset += bytes_read; 1641 iso9660->entry_bytes_remaining -= bytes_read; 1642 iso9660->current_position += bytes_read; 1643 __archive_read_consume(a, bytes_read); 1644 return (ARCHIVE_OK); 1645} 1646 1647static int 1648archive_read_format_iso9660_cleanup(struct archive_read *a) 1649{ 1650 struct iso9660 *iso9660; 1651 int r = ARCHIVE_OK; 1652 1653 iso9660 = (struct iso9660 *)(a->format->data); 1654 release_files(iso9660); 1655 free(iso9660->read_ce_req.reqs); 1656 archive_string_free(&iso9660->pathname); 1657 archive_string_free(&iso9660->previous_pathname); 1658 if (iso9660->pending_files.files) 1659 free(iso9660->pending_files.files); 1660 if (iso9660->re_dirs.files) 1661 free(iso9660->re_dirs.files); 1662 if (iso9660->cl_files.files) 1663 free(iso9660->cl_files.files); 1664#ifdef HAVE_ZLIB_H 1665 free(iso9660->entry_zisofs.uncompressed_buffer); 1666 free(iso9660->entry_zisofs.block_pointers); 1667 if (iso9660->entry_zisofs.stream_valid) { 1668 if (inflateEnd(&iso9660->entry_zisofs.stream) != Z_OK) { 1669 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 1670 "Failed to clean up zlib decompressor"); 1671 r = ARCHIVE_FATAL; 1672 } 1673 } 1674#endif 1675 free(iso9660); 1676 (a->format->data) = NULL; 1677 return (r); 1678} 1679 1680/* 1681 * This routine parses a single ISO directory record, makes sense 1682 * of any extensions, and stores the result in memory. 1683 */ 1684static struct file_info * 1685parse_file_info(struct archive_read *a, struct file_info *parent, 1686 const unsigned char *isodirrec) 1687{ 1688 struct iso9660 *iso9660; 1689 struct file_info *file; 1690 size_t name_len; 1691 const unsigned char *rr_start, *rr_end; 1692 const unsigned char *p; 1693 size_t dr_len; 1694 int32_t location; 1695 int flags; 1696 1697 iso9660 = (struct iso9660 *)(a->format->data); 1698 1699 dr_len = (size_t)isodirrec[DR_length_offset]; 1700 name_len = (size_t)isodirrec[DR_name_len_offset]; 1701 location = archive_le32dec(isodirrec + DR_extent_offset); 1702 /* Sanity check that dr_len needs at least 34. */ 1703 if (dr_len < 34) { 1704 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 1705 "Invalid length of directory record"); 1706 return (NULL); 1707 } 1708 /* Sanity check that name_len doesn't exceed dr_len. */ 1709 if (dr_len - 33 < name_len || name_len == 0) { 1710 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 1711 "Invalid length of file identifier"); 1712 return (NULL); 1713 } 1714 /* Sanity check that location doesn't exceed volume block. 1715 * Don't check lower limit of location; it's possibility 1716 * the location has negative value when file type is symbolic 1717 * link or file size is zero. As far as I know latest mkisofs 1718 * do that. 1719 */ 1720 if (location >= iso9660->volume_block) { 1721 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 1722 "Invalid location of extent of file"); 1723 return (NULL); 1724 } 1725 1726 /* Create a new file entry and copy data from the ISO dir record. */ 1727 file = (struct file_info *)malloc(sizeof(*file)); 1728 if (file == NULL) { 1729 archive_set_error(&a->archive, ENOMEM, 1730 "No memory for file entry"); 1731 return (NULL); 1732 } 1733 memset(file, 0, sizeof(*file)); 1734 file->parent = parent; 1735 file->offset = iso9660->logical_block_size * (uint64_t)location; 1736 file->size = toi(isodirrec + DR_size_offset, DR_size_size); 1737 file->mtime = isodate7(isodirrec + DR_date_offset); 1738 file->ctime = file->atime = file->mtime; 1739 1740 p = isodirrec + DR_name_offset; 1741 /* Rockridge extensions (if any) follow name. Compute this 1742 * before fidgeting the name_len below. */ 1743 rr_start = p + name_len + (name_len & 1 ? 0 : 1); 1744 rr_end = isodirrec + dr_len; 1745 1746 if (iso9660->seenJoliet) { 1747 /* Joliet names are max 64 chars (128 bytes) according to spec, 1748 * but genisoimage/mkisofs allows recording longer Joliet 1749 * names which are 103 UCS2 characters(206 bytes) by their 1750 * option '-joliet-long'. 1751 */ 1752 wchar_t wbuff[103+1], *wp; 1753 const unsigned char *c; 1754 1755 if (name_len > 206) 1756 name_len = 206; 1757 /* convert BE UTF-16 to wchar_t */ 1758 for (c = p, wp = wbuff; 1759 c < (p + name_len) && 1760 wp < (wbuff + sizeof(wbuff)/sizeof(*wbuff) - 1); 1761 c += 2) { 1762 *wp++ = (((255 & (int)c[0]) << 8) | (255 & (int)c[1])); 1763 } 1764 *wp = L'\0'; 1765 1766#if 0 /* untested code, is it at all useful on Joliet? */ 1767 /* trim trailing first version and dot from filename. 1768 * 1769 * Remember we where in UTF-16BE land! 1770 * SEPARATOR 1 (.) and SEPARATOR 2 (;) are both 1771 * 16 bits big endian characters on Joliet. 1772 * 1773 * TODO: sanitize filename? 1774 * Joliet allows any UCS-2 char except: 1775 * *, /, :, ;, ? and \. 1776 */ 1777 /* Chop off trailing ';1' from files. */ 1778 if (*(wp-2) == ';' && *(wp-1) == '1') { 1779 wp-=2; 1780 *wp = L'\0'; 1781 } 1782 1783 /* Chop off trailing '.' from filenames. */ 1784 if (*(wp-1) == '.') 1785 *(--wp) = L'\0'; 1786#endif 1787 1788 /* store the result in the file name field. */ 1789 archive_strappend_w_utf8(&file->name, wbuff); 1790 } else { 1791 /* Chop off trailing ';1' from files. */ 1792 if (name_len > 2 && p[name_len - 2] == ';' && 1793 p[name_len - 1] == '1') 1794 name_len -= 2; 1795 /* Chop off trailing '.' from filenames. */ 1796 if (name_len > 1 && p[name_len - 1] == '.') 1797 --name_len; 1798 1799 archive_strncpy(&file->name, (const char *)p, name_len); 1800 } 1801 1802 flags = isodirrec[DR_flags_offset]; 1803 if (flags & 0x02) 1804 file->mode = AE_IFDIR | 0700; 1805 else 1806 file->mode = AE_IFREG | 0400; 1807 if (flags & 0x80) 1808 file->multi_extent = 1; 1809 else 1810 file->multi_extent = 0; 1811 /* 1812 * Use location for file number. 1813 * File number is treated as inode number to find out harlink 1814 * target. If Rockridge extensions is being used, file number 1815 * will be overwritten by FILE SERIAL NUMBER of RRIP "PX" 1816 * extension. 1817 * NOTE: Old mkisofs did not record that FILE SERIAL NUMBER 1818 * in ISO images. 1819 */ 1820 if (file->size == 0 && location >= 0) 1821 /* If file->size is zero, its location points wrong place. 1822 * Dot not use it for file number. 1823 * When location has negative value, it can be used 1824 * for file number. 1825 */ 1826 file->number = -1; 1827 else 1828 file->number = (int64_t)(uint32_t)location; 1829 1830 /* Rockridge extensions overwrite information from above. */ 1831 if (iso9660->opt_support_rockridge) { 1832 if (parent == NULL && rr_end - rr_start >= 7) { 1833 p = rr_start; 1834 if (p[0] == 'S' && p[1] == 'P' 1835 && p[2] == 7 && p[3] == 1 1836 && p[4] == 0xBE && p[5] == 0xEF) { 1837 /* 1838 * SP extension stores the suspOffset 1839 * (Number of bytes to skip between 1840 * filename and SUSP records.) 1841 * It is mandatory by the SUSP standard 1842 * (IEEE 1281). 1843 * 1844 * It allows SUSP to coexist with 1845 * non-SUSP uses of the System 1846 * Use Area by placing non-SUSP data 1847 * before SUSP data. 1848 * 1849 * SP extension must be in the root 1850 * directory entry, disable all SUSP 1851 * processing if not found. 1852 */ 1853 iso9660->suspOffset = p[6]; 1854 iso9660->seenSUSP = 1; 1855 rr_start += 7; 1856 } 1857 } 1858 if (iso9660->seenSUSP) { 1859 int r; 1860 1861 file->name_continues = 0; 1862 file->symlink_continues = 0; 1863 rr_start += iso9660->suspOffset; 1864 r = parse_rockridge(a, file, rr_start, rr_end); 1865 if (r != ARCHIVE_OK) { 1866 free(file); 1867 return (NULL); 1868 } 1869 } else 1870 /* If there isn't SUSP, disable parsing 1871 * rock ridge extensions. */ 1872 iso9660->opt_support_rockridge = 0; 1873 } 1874 1875 file->nlinks = 1;/* Reset nlink. we'll calculate it later. */ 1876 /* Tell file's parent how many children that parent has. */ 1877 if (parent != NULL && (flags & 0x02) && file->cl_offset == 0) 1878 parent->subdirs++; 1879 1880#if DEBUG 1881 /* DEBUGGING: Warn about attributes I don't yet fully support. */ 1882 if ((flags & ~0x02) != 0) { 1883 fprintf(stderr, "\n ** Unrecognized flag: "); 1884 dump_isodirrec(stderr, isodirrec); 1885 fprintf(stderr, "\n"); 1886 } else if (toi(isodirrec + DR_volume_sequence_number_offset, 2) != 1) { 1887 fprintf(stderr, "\n ** Unrecognized sequence number: "); 1888 dump_isodirrec(stderr, isodirrec); 1889 fprintf(stderr, "\n"); 1890 } else if (*(isodirrec + DR_file_unit_size_offset) != 0) { 1891 fprintf(stderr, "\n ** Unexpected file unit size: "); 1892 dump_isodirrec(stderr, isodirrec); 1893 fprintf(stderr, "\n"); 1894 } else if (*(isodirrec + DR_interleave_offset) != 0) { 1895 fprintf(stderr, "\n ** Unexpected interleave: "); 1896 dump_isodirrec(stderr, isodirrec); 1897 fprintf(stderr, "\n"); 1898 } else if (*(isodirrec + DR_ext_attr_length_offset) != 0) { 1899 fprintf(stderr, "\n ** Unexpected extended attribute length: "); 1900 dump_isodirrec(stderr, isodirrec); 1901 fprintf(stderr, "\n"); 1902 } 1903#endif 1904 register_file(iso9660, file); 1905 return (file); 1906} 1907 1908static int 1909parse_rockridge(struct archive_read *a, struct file_info *file, 1910 const unsigned char *p, const unsigned char *end) 1911{ 1912 struct iso9660 *iso9660; 1913 1914 iso9660 = (struct iso9660 *)(a->format->data); 1915 1916 while (p + 4 <= end /* Enough space for another entry. */ 1917 && p[0] >= 'A' && p[0] <= 'Z' /* Sanity-check 1st char of name. */ 1918 && p[1] >= 'A' && p[1] <= 'Z' /* Sanity-check 2nd char of name. */ 1919 && p[2] >= 4 /* Sanity-check length. */ 1920 && p + p[2] <= end) { /* Sanity-check length. */ 1921 const unsigned char *data = p + 4; 1922 int data_length = p[2] - 4; 1923 int version = p[3]; 1924 1925 /* 1926 * Yes, each 'if' here does test p[0] again. 1927 * Otherwise, the fall-through handling to catch 1928 * unsupported extensions doesn't work. 1929 */ 1930 switch(p[0]) { 1931 case 'C': 1932 if (p[0] == 'C' && p[1] == 'E') { 1933 if (version == 1 && data_length == 24) { 1934 /* 1935 * CE extension comprises: 1936 * 8 byte sector containing extension 1937 * 8 byte offset w/in above sector 1938 * 8 byte length of continuation 1939 */ 1940 int32_t location = 1941 archive_le32dec(data); 1942 file->ce_offset = 1943 archive_le32dec(data+8); 1944 file->ce_size = 1945 archive_le32dec(data+16); 1946 if (register_CE(a, location, file) 1947 != ARCHIVE_OK) 1948 return (ARCHIVE_FATAL); 1949 } 1950 break; 1951 } 1952 if (p[0] == 'C' && p[1] == 'L') { 1953 if (version == 1 && data_length == 8) { 1954 file->cl_offset = (uint64_t) 1955 iso9660->logical_block_size * 1956 (uint64_t)archive_le32dec(data); 1957 iso9660->seenRockridge = 1; 1958 } 1959 break; 1960 } 1961 /* FALLTHROUGH */ 1962 case 'N': 1963 if (p[0] == 'N' && p[1] == 'M') { 1964 if (version == 1) { 1965 parse_rockridge_NM1(file, 1966 data, data_length); 1967 iso9660->seenRockridge = 1; 1968 } 1969 break; 1970 } 1971 /* FALLTHROUGH */ 1972 case 'P': 1973 if (p[0] == 'P' && p[1] == 'D') { 1974 /* 1975 * PD extension is padding; 1976 * contents are always ignored. 1977 */ 1978 break; 1979 } 1980 if (p[0] == 'P' && p[1] == 'N') { 1981 if (version == 1 && data_length == 16) { 1982 file->rdev = toi(data,4); 1983 file->rdev <<= 32; 1984 file->rdev |= toi(data + 8, 4); 1985 iso9660->seenRockridge = 1; 1986 } 1987 break; 1988 } 1989 if (p[0] == 'P' && p[1] == 'X') { 1990 /* 1991 * PX extension comprises: 1992 * 8 bytes for mode, 1993 * 8 bytes for nlinks, 1994 * 8 bytes for uid, 1995 * 8 bytes for gid, 1996 * 8 bytes for inode. 1997 */ 1998 if (version == 1) { 1999 if (data_length >= 8) 2000 file->mode 2001 = toi(data, 4); 2002 if (data_length >= 16) 2003 file->nlinks 2004 = toi(data + 8, 4); 2005 if (data_length >= 24) 2006 file->uid 2007 = toi(data + 16, 4); 2008 if (data_length >= 32) 2009 file->gid 2010 = toi(data + 24, 4); 2011 if (data_length >= 40) 2012 file->number 2013 = toi(data + 32, 4); 2014 iso9660->seenRockridge = 1; 2015 } 2016 break; 2017 } 2018 /* FALLTHROUGH */ 2019 case 'R': 2020 if (p[0] == 'R' && p[1] == 'E' && version == 1) { 2021 file->re = 1; 2022 iso9660->seenRockridge = 1; 2023 break; 2024 } 2025 if (p[0] == 'R' && p[1] == 'R' && version == 1) { 2026 /* 2027 * RR extension comprises: 2028 * one byte flag value 2029 * This extension is obsolete, 2030 * so contents are always ignored. 2031 */ 2032 break; 2033 } 2034 /* FALLTHROUGH */ 2035 case 'S': 2036 if (p[0] == 'S' && p[1] == 'L') { 2037 if (version == 1) { 2038 parse_rockridge_SL1(file, 2039 data, data_length); 2040 iso9660->seenRockridge = 1; 2041 } 2042 break; 2043 } 2044 if (p[0] == 'S' && p[1] == 'T' 2045 && data_length == 0 && version == 1) { 2046 /* 2047 * ST extension marks end of this 2048 * block of SUSP entries. 2049 * 2050 * It allows SUSP to coexist with 2051 * non-SUSP uses of the System 2052 * Use Area by placing non-SUSP data 2053 * after SUSP data. 2054 */ 2055 iso9660->seenSUSP = 0; 2056 iso9660->seenRockridge = 0; 2057 return (ARCHIVE_OK); 2058 } 2059 case 'T': 2060 if (p[0] == 'T' && p[1] == 'F') { 2061 if (version == 1) { 2062 parse_rockridge_TF1(file, 2063 data, data_length); 2064 iso9660->seenRockridge = 1; 2065 } 2066 break; 2067 } 2068 /* FALLTHROUGH */ 2069 case 'Z': 2070 if (p[0] == 'Z' && p[1] == 'F') { 2071 if (version == 1) 2072 parse_rockridge_ZF1(file, 2073 data, data_length); 2074 break; 2075 } 2076 /* FALLTHROUGH */ 2077 default: 2078 /* The FALLTHROUGHs above leave us here for 2079 * any unsupported extension. */ 2080 break; 2081 } 2082 2083 2084 2085 p += p[2]; 2086 } 2087 return (ARCHIVE_OK); 2088} 2089 2090static int 2091register_CE(struct archive_read *a, int32_t location, 2092 struct file_info *file) 2093{ 2094 struct iso9660 *iso9660; 2095 struct read_ce_queue *heap; 2096 struct read_ce_req *p; 2097 uint64_t offset, parent_offset; 2098 int hole, parent; 2099 2100 iso9660 = (struct iso9660 *)(a->format->data); 2101 offset = ((uint64_t)location) * (uint64_t)iso9660->logical_block_size; 2102 if (((file->mode & AE_IFMT) == AE_IFREG && 2103 offset >= file->offset) || 2104 offset < iso9660->current_position) { 2105 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 2106 "Invalid location in SUSP \"CE\" extension"); 2107 return (ARCHIVE_FATAL); 2108 } 2109 2110 /* Expand our CE list as necessary. */ 2111 heap = &(iso9660->read_ce_req); 2112 if (heap->cnt >= heap->allocated) { 2113 int new_size; 2114 2115 if (heap->allocated < 16) 2116 new_size = 16; 2117 else 2118 new_size = heap->allocated * 2; 2119 /* Overflow might keep us from growing the list. */ 2120 if (new_size <= heap->allocated) 2121 __archive_errx(1, "Out of memory"); 2122 p = malloc(new_size * sizeof(p[0])); 2123 if (p == NULL) 2124 __archive_errx(1, "Out of memory"); 2125 if (heap->reqs != NULL) { 2126 memcpy(p, heap->reqs, heap->cnt * sizeof(*p)); 2127 free(heap->reqs); 2128 } 2129 heap->reqs = p; 2130 heap->allocated = new_size; 2131 } 2132 2133 /* 2134 * Start with hole at end, walk it up tree to find insertion point. 2135 */ 2136 hole = heap->cnt++; 2137 while (hole > 0) { 2138 parent = (hole - 1)/2; 2139 parent_offset = heap->reqs[parent].offset; 2140 if (offset >= parent_offset) { 2141 heap->reqs[hole].offset = offset; 2142 heap->reqs[hole].file = file; 2143 return (ARCHIVE_OK); 2144 } 2145 // Move parent into hole <==> move hole up tree. 2146 heap->reqs[hole] = heap->reqs[parent]; 2147 hole = parent; 2148 } 2149 heap->reqs[0].offset = offset; 2150 heap->reqs[0].file = file; 2151 return (ARCHIVE_OK); 2152} 2153 2154static void 2155next_CE(struct read_ce_queue *heap) 2156{ 2157 uint64_t a_offset, b_offset, c_offset; 2158 int a, b, c; 2159 struct read_ce_req tmp; 2160 2161 if (heap->cnt < 1) 2162 return; 2163 2164 /* 2165 * Move the last item in the heap to the root of the tree 2166 */ 2167 heap->reqs[0] = heap->reqs[--(heap->cnt)]; 2168 2169 /* 2170 * Rebalance the heap. 2171 */ 2172 a = 0; // Starting element and its offset 2173 a_offset = heap->reqs[a].offset; 2174 for (;;) { 2175 b = a + a + 1; // First child 2176 if (b >= heap->cnt) 2177 return; 2178 b_offset = heap->reqs[b].offset; 2179 c = b + 1; // Use second child if it is smaller. 2180 if (c < heap->cnt) { 2181 c_offset = heap->reqs[c].offset; 2182 if (c_offset < b_offset) { 2183 b = c; 2184 b_offset = c_offset; 2185 } 2186 } 2187 if (a_offset <= b_offset) 2188 return; 2189 tmp = heap->reqs[a]; 2190 heap->reqs[a] = heap->reqs[b]; 2191 heap->reqs[b] = tmp; 2192 a = b; 2193 } 2194} 2195 2196 2197static int 2198read_CE(struct archive_read *a, struct iso9660 *iso9660) 2199{ 2200 struct read_ce_queue *heap; 2201 const unsigned char *b, *p, *end; 2202 struct file_info *file; 2203 size_t step; 2204 int r; 2205 2206 /* Read data which RRIP "CE" extension points. */ 2207 heap = &(iso9660->read_ce_req); 2208 step = iso9660->logical_block_size; 2209 while (heap->cnt && 2210 heap->reqs[0].offset == iso9660->current_position) { 2211 b = __archive_read_ahead(a, step, NULL); 2212 if (b == NULL) { 2213 archive_set_error(&a->archive, 2214 ARCHIVE_ERRNO_MISC, 2215 "Failed to read full block when scanning " 2216 "ISO9660 directory list"); 2217 return (ARCHIVE_FATAL); 2218 } 2219 do { 2220 file = heap->reqs[0].file; 2221 p = b + file->ce_offset; 2222 end = p + file->ce_size; 2223 next_CE(heap); 2224 r = parse_rockridge(a, file, p, end); 2225 if (r != ARCHIVE_OK) 2226 return (ARCHIVE_FATAL); 2227 } while (heap->cnt && 2228 heap->reqs[0].offset == iso9660->current_position); 2229 /* NOTE: Do not move this consume's code to fron of 2230 * do-while loop. Registration of nested CE extension 2231 * might cause error because of current position. */ 2232 __archive_read_consume(a, step); 2233 iso9660->current_position += step; 2234 } 2235 return (ARCHIVE_OK); 2236} 2237 2238static void 2239parse_rockridge_NM1(struct file_info *file, 2240 const unsigned char *data, int data_length) 2241{ 2242 if (!file->name_continues) 2243 archive_string_empty(&file->name); 2244 file->name_continues = 0; 2245 if (data_length < 1) 2246 return; 2247 /* 2248 * NM version 1 extension comprises: 2249 * 1 byte flag, value is one of: 2250 * = 0: remainder is name 2251 * = 1: remainder is name, next NM entry continues name 2252 * = 2: "." 2253 * = 4: ".." 2254 * = 32: Implementation specific 2255 * All other values are reserved. 2256 */ 2257 switch(data[0]) { 2258 case 0: 2259 if (data_length < 2) 2260 return; 2261 archive_strncat(&file->name, (const char *)data + 1, data_length - 1); 2262 break; 2263 case 1: 2264 if (data_length < 2) 2265 return; 2266 archive_strncat(&file->name, (const char *)data + 1, data_length - 1); 2267 file->name_continues = 1; 2268 break; 2269 case 2: 2270 archive_strcat(&file->name, "."); 2271 break; 2272 case 4: 2273 archive_strcat(&file->name, ".."); 2274 break; 2275 default: 2276 return; 2277 } 2278 2279} 2280 2281static void 2282parse_rockridge_TF1(struct file_info *file, const unsigned char *data, 2283 int data_length) 2284{ 2285 char flag; 2286 /* 2287 * TF extension comprises: 2288 * one byte flag 2289 * create time (optional) 2290 * modify time (optional) 2291 * access time (optional) 2292 * attribute time (optional) 2293 * Time format and presence of fields 2294 * is controlled by flag bits. 2295 */ 2296 if (data_length < 1) 2297 return; 2298 flag = data[0]; 2299 ++data; 2300 --data_length; 2301 if (flag & 0x80) { 2302 /* Use 17-byte time format. */ 2303 if ((flag & 1) && data_length >= 17) { 2304 /* Create time. */ 2305 file->birthtime_is_set = 1; 2306 file->birthtime = isodate17(data); 2307 data += 17; 2308 data_length -= 17; 2309 } 2310 if ((flag & 2) && data_length >= 17) { 2311 /* Modify time. */ 2312 file->mtime = isodate17(data); 2313 data += 17; 2314 data_length -= 17; 2315 } 2316 if ((flag & 4) && data_length >= 17) { 2317 /* Access time. */ 2318 file->atime = isodate17(data); 2319 data += 17; 2320 data_length -= 17; 2321 } 2322 if ((flag & 8) && data_length >= 17) { 2323 /* Attribute change time. */ 2324 file->ctime = isodate17(data); 2325 } 2326 } else { 2327 /* Use 7-byte time format. */ 2328 if ((flag & 1) && data_length >= 7) { 2329 /* Create time. */ 2330 file->birthtime_is_set = 1; 2331 file->birthtime = isodate7(data); 2332 data += 7; 2333 data_length -= 7; 2334 } 2335 if ((flag & 2) && data_length >= 7) { 2336 /* Modify time. */ 2337 file->mtime = isodate7(data); 2338 data += 7; 2339 data_length -= 7; 2340 } 2341 if ((flag & 4) && data_length >= 7) { 2342 /* Access time. */ 2343 file->atime = isodate7(data); 2344 data += 7; 2345 data_length -= 7; 2346 } 2347 if ((flag & 8) && data_length >= 7) { 2348 /* Attribute change time. */ 2349 file->ctime = isodate7(data); 2350 } 2351 } 2352} 2353 2354static void 2355parse_rockridge_SL1(struct file_info *file, const unsigned char *data, 2356 int data_length) 2357{ 2358 const char *separator = ""; 2359 2360 if (!file->symlink_continues || file->symlink.length < 1) 2361 archive_string_empty(&file->symlink); 2362 else if (!file->symlink_continues && 2363 file->symlink.s[file->symlink.length - 1] != '/') 2364 separator = "/"; 2365 file->symlink_continues = 0; 2366 2367 /* 2368 * Defined flag values: 2369 * 0: This is the last SL record for this symbolic link 2370 * 1: this symbolic link field continues in next SL entry 2371 * All other values are reserved. 2372 */ 2373 if (data_length < 1) 2374 return; 2375 switch(*data) { 2376 case 0: 2377 break; 2378 case 1: 2379 file->symlink_continues = 1; 2380 break; 2381 default: 2382 return; 2383 } 2384 ++data; /* Skip flag byte. */ 2385 --data_length; 2386 2387 /* 2388 * SL extension body stores "components". 2389 * Basically, this is a complicated way of storing 2390 * a POSIX path. It also interferes with using 2391 * symlinks for storing non-path data. <sigh> 2392 * 2393 * Each component is 2 bytes (flag and length) 2394 * possibly followed by name data. 2395 */ 2396 while (data_length >= 2) { 2397 unsigned char flag = *data++; 2398 unsigned char nlen = *data++; 2399 data_length -= 2; 2400 2401 archive_strcat(&file->symlink, separator); 2402 separator = "/"; 2403 2404 switch(flag) { 2405 case 0: /* Usual case, this is text. */ 2406 if (data_length < nlen) 2407 return; 2408 archive_strncat(&file->symlink, 2409 (const char *)data, nlen); 2410 break; 2411 case 0x01: /* Text continues in next component. */ 2412 if (data_length < nlen) 2413 return; 2414 archive_strncat(&file->symlink, 2415 (const char *)data, nlen); 2416 separator = ""; 2417 break; 2418 case 0x02: /* Current dir. */ 2419 archive_strcat(&file->symlink, "."); 2420 break; 2421 case 0x04: /* Parent dir. */ 2422 archive_strcat(&file->symlink, ".."); 2423 break; 2424 case 0x08: /* Root of filesystem. */ 2425 archive_strcat(&file->symlink, "/"); 2426 separator = ""; 2427 break; 2428 case 0x10: /* Undefined (historically "volume root" */ 2429 archive_string_empty(&file->symlink); 2430 archive_strcat(&file->symlink, "ROOT"); 2431 break; 2432 case 0x20: /* Undefined (historically "hostname") */ 2433 archive_strcat(&file->symlink, "hostname"); 2434 break; 2435 default: 2436 /* TODO: issue a warning ? */ 2437 return; 2438 } 2439 data += nlen; 2440 data_length -= nlen; 2441 } 2442} 2443 2444static void 2445parse_rockridge_ZF1(struct file_info *file, const unsigned char *data, 2446 int data_length) 2447{ 2448 2449 if (data[0] == 0x70 && data[1] == 0x7a && data_length == 12) { 2450 /* paged zlib */ 2451 file->pz = 1; 2452 file->pz_log2_bs = data[3]; 2453 file->pz_uncompressed_size = archive_le32dec(&data[4]); 2454 } 2455} 2456 2457static void 2458register_file(struct iso9660 *iso9660, struct file_info *file) 2459{ 2460 2461 file->use_next = iso9660->use_files; 2462 iso9660->use_files = file; 2463} 2464 2465static void 2466release_files(struct iso9660 *iso9660) 2467{ 2468 struct content *con, *connext; 2469 struct file_info *file; 2470 2471 file = iso9660->use_files; 2472 while (file != NULL) { 2473 struct file_info *next = file->use_next; 2474 2475 archive_string_free(&file->name); 2476 archive_string_free(&file->symlink); 2477 con = file->contents.first; 2478 while (con != NULL) { 2479 connext = con->next; 2480 free(con); 2481 con = connext; 2482 } 2483 free(file); 2484 file = next; 2485 } 2486} 2487 2488static int 2489next_entry_seek(struct archive_read *a, struct iso9660 *iso9660, 2490 struct file_info **pfile) 2491{ 2492 struct file_info *file; 2493 2494 *pfile = file = next_cache_entry(iso9660); 2495 if (file == NULL) 2496 return (ARCHIVE_EOF); 2497 2498 /* Don't waste time seeking for zero-length bodies. */ 2499 if (file->size == 0) 2500 file->offset = iso9660->current_position; 2501 2502 /* Seek forward to the start of the entry. */ 2503 if (iso9660->current_position < file->offset) { 2504 int64_t step; 2505 2506 step = file->offset - iso9660->current_position; 2507 step = __archive_read_skip(a, step); 2508 if (step < 0) 2509 return ((int)step); 2510 iso9660->current_position = file->offset; 2511 } 2512 2513 /* We found body of file; handle it now. */ 2514 return (ARCHIVE_OK); 2515} 2516 2517static struct file_info * 2518next_cache_entry(struct iso9660 *iso9660) 2519{ 2520 struct file_info *file; 2521 struct { 2522 struct file_info *first; 2523 struct file_info **last; 2524 } empty_files; 2525 int64_t number; 2526 int count; 2527 2528 file = cache_get_entry(iso9660); 2529 if (file != NULL) { 2530 while (file->parent != NULL && !file->parent->exposed) { 2531 /* If file's parent is not exposed, it's moved 2532 * to next entry of its parent. */ 2533 cache_add_to_next_of_parent(iso9660, file); 2534 file = cache_get_entry(iso9660); 2535 } 2536 return (file); 2537 } 2538 2539 file = next_entry(iso9660); 2540 if (file == NULL) 2541 return (NULL); 2542 2543 if ((file->mode & AE_IFMT) != AE_IFREG || file->number == -1) 2544 return (file); 2545 2546 count = 0; 2547 number = file->number; 2548 iso9660->cache_files.first = NULL; 2549 iso9660->cache_files.last = &(iso9660->cache_files.first); 2550 empty_files.first = NULL; 2551 empty_files.last = &empty_files.first; 2552 /* Collect files which has the same file serial number. 2553 * Peek pending_files so that file which number is different 2554 * is not put bak. */ 2555 while (iso9660->pending_files.used > 0 && 2556 (iso9660->pending_files.files[0]->number == -1 || 2557 iso9660->pending_files.files[0]->number == number)) { 2558 if (file->number == -1) { 2559 /* This file has the same offset 2560 * but it's wrong offset which empty files 2561 * and symlink files have. 2562 * NOTE: This wrong offse was recorded by 2563 * old mkisofs utility. If ISO images is 2564 * created by latest mkisofs, this does not 2565 * happen. 2566 */ 2567 file->next = NULL; 2568 *empty_files.last = file; 2569 empty_files.last = &(file->next); 2570 } else { 2571 count++; 2572 cache_add_entry(iso9660, file); 2573 } 2574 file = next_entry(iso9660); 2575 } 2576 2577 if (count == 0) 2578 return (file); 2579 if (file->number == -1) { 2580 file->next = NULL; 2581 *empty_files.last = file; 2582 empty_files.last = &(file->next); 2583 } else { 2584 count++; 2585 cache_add_entry(iso9660, file); 2586 } 2587 2588 if (count > 1) { 2589 /* The count is the same as number of hardlink, 2590 * so much so that each nlinks of files in cache_file 2591 * is overwritten by value of the count. 2592 */ 2593 for (file = iso9660->cache_files.first; 2594 file != NULL; file = file->next) 2595 file->nlinks = count; 2596 } 2597 /* If there are empty files, that files are added 2598 * to the tail of the cache_files. */ 2599 if (empty_files.first != NULL) { 2600 *iso9660->cache_files.last = empty_files.first; 2601 iso9660->cache_files.last = empty_files.last; 2602 } 2603 return (cache_get_entry(iso9660)); 2604} 2605 2606static inline void 2607cache_add_entry(struct iso9660 *iso9660, struct file_info *file) 2608{ 2609 file->next = NULL; 2610 *iso9660->cache_files.last = file; 2611 iso9660->cache_files.last = &(file->next); 2612} 2613 2614static inline void 2615cache_add_to_next_of_parent(struct iso9660 *iso9660, struct file_info *file) 2616{ 2617 file->next = file->parent->next; 2618 file->parent->next = file; 2619 if (iso9660->cache_files.last == &(file->parent->next)) 2620 iso9660->cache_files.last = &(file->next); 2621} 2622 2623static inline struct file_info * 2624cache_get_entry(struct iso9660 *iso9660) 2625{ 2626 struct file_info *file; 2627 2628 if ((file = iso9660->cache_files.first) != NULL) { 2629 iso9660->cache_files.first = file->next; 2630 if (iso9660->cache_files.first == NULL) 2631 iso9660->cache_files.last = &(iso9660->cache_files.first); 2632 } 2633 return (file); 2634} 2635 2636static int 2637heap_add_entry(struct archive_read *a, struct heap_queue *heap, struct file_info *file, uint64_t key) 2638{ 2639 uint64_t file_key, parent_key; 2640 int hole, parent; 2641 2642 /* Expand our pending files list as necessary. */ 2643 if (heap->used >= heap->allocated) { 2644 struct file_info **new_pending_files; 2645 int new_size = heap->allocated * 2; 2646 2647 if (heap->allocated < 1024) 2648 new_size = 1024; 2649 /* Overflow might keep us from growing the list. */ 2650 if (new_size <= heap->allocated) { 2651 archive_set_error(&a->archive, 2652 ENOMEM, "Out of memory"); 2653 return (ARCHIVE_FATAL); 2654 } 2655 new_pending_files = (struct file_info **) 2656 malloc(new_size * sizeof(new_pending_files[0])); 2657 if (new_pending_files == NULL) { 2658 archive_set_error(&a->archive, 2659 ENOMEM, "Out of memory"); 2660 return (ARCHIVE_FATAL); 2661 } 2662 memcpy(new_pending_files, heap->files, 2663 heap->allocated * sizeof(new_pending_files[0])); 2664 if (heap->files != NULL) 2665 free(heap->files); 2666 heap->files = new_pending_files; 2667 heap->allocated = new_size; 2668 } 2669 2670 file_key = file->key = key; 2671 2672 /* 2673 * Start with hole at end, walk it up tree to find insertion point. 2674 */ 2675 hole = heap->used++; 2676 while (hole > 0) { 2677 parent = (hole - 1)/2; 2678 parent_key = heap->files[parent]->key; 2679 if (file_key >= parent_key) { 2680 heap->files[hole] = file; 2681 return; 2682 } 2683 // Move parent into hole <==> move hole up tree. 2684 heap->files[hole] = heap->files[parent]; 2685 hole = parent; 2686 } 2687 heap->files[0] = file; 2688 2689 return (ARCHIVE_OK); 2690} 2691 2692static struct file_info * 2693heap_get_entry(struct heap_queue *heap) 2694{ 2695 uint64_t a_key, b_key, c_key; 2696 int a, b, c; 2697 struct file_info *r, *tmp; 2698 2699 if (heap->used < 1) 2700 return (NULL); 2701 2702 /* 2703 * The first file in the list is the earliest; we'll return this. 2704 */ 2705 r = heap->files[0]; 2706 2707 /* 2708 * Move the last item in the heap to the root of the tree 2709 */ 2710 heap->files[0] = heap->files[--(heap->used)]; 2711 2712 /* 2713 * Rebalance the heap. 2714 */ 2715 a = 0; // Starting element and its heap key 2716 a_key = heap->files[a]->key; 2717 for (;;) { 2718 b = a + a + 1; // First child 2719 if (b >= heap->used) 2720 return (r); 2721 b_key = heap->files[b]->key; 2722 c = b + 1; // Use second child if it is smaller. 2723 if (c < heap->used) { 2724 c_key = heap->files[c]->key; 2725 if (c_key < b_key) { 2726 b = c; 2727 b_key = c_key; 2728 } 2729 } 2730 if (a_key <= b_key) 2731 return (r); 2732 tmp = heap->files[a]; 2733 heap->files[a] = heap->files[b]; 2734 heap->files[b] = tmp; 2735 a = b; 2736 } 2737} 2738 2739static unsigned int 2740toi(const void *p, int n) 2741{ 2742 const unsigned char *v = (const unsigned char *)p; 2743 if (n > 1) 2744 return v[0] + 256 * toi(v + 1, n - 1); 2745 if (n == 1) 2746 return v[0]; 2747 return (0); 2748} 2749 2750static time_t 2751isodate7(const unsigned char *v) 2752{ 2753 struct tm tm; 2754 int offset; 2755 memset(&tm, 0, sizeof(tm)); 2756 tm.tm_year = v[0]; 2757 tm.tm_mon = v[1] - 1; 2758 tm.tm_mday = v[2]; 2759 tm.tm_hour = v[3]; 2760 tm.tm_min = v[4]; 2761 tm.tm_sec = v[5]; 2762 /* v[6] is the signed timezone offset, in 1/4-hour increments. */ 2763 offset = ((const signed char *)v)[6]; 2764 if (offset > -48 && offset < 52) { 2765 tm.tm_hour -= offset / 4; 2766 tm.tm_min -= (offset % 4) * 15; 2767 } 2768 return (time_from_tm(&tm)); 2769} 2770 2771static time_t 2772isodate17(const unsigned char *v) 2773{ 2774 struct tm tm; 2775 int offset; 2776 memset(&tm, 0, sizeof(tm)); 2777 tm.tm_year = (v[0] - '0') * 1000 + (v[1] - '0') * 100 2778 + (v[2] - '0') * 10 + (v[3] - '0') 2779 - 1900; 2780 tm.tm_mon = (v[4] - '0') * 10 + (v[5] - '0'); 2781 tm.tm_mday = (v[6] - '0') * 10 + (v[7] - '0'); 2782 tm.tm_hour = (v[8] - '0') * 10 + (v[9] - '0'); 2783 tm.tm_min = (v[10] - '0') * 10 + (v[11] - '0'); 2784 tm.tm_sec = (v[12] - '0') * 10 + (v[13] - '0'); 2785 /* v[16] is the signed timezone offset, in 1/4-hour increments. */ 2786 offset = ((const signed char *)v)[16]; 2787 if (offset > -48 && offset < 52) { 2788 tm.tm_hour -= offset / 4; 2789 tm.tm_min -= (offset % 4) * 15; 2790 } 2791 return (time_from_tm(&tm)); 2792} 2793 2794static time_t 2795time_from_tm(struct tm *t) 2796{ 2797#if HAVE_TIMEGM 2798 /* Use platform timegm() if available. */ 2799 return (timegm(t)); 2800#else 2801 /* Else use direct calculation using POSIX assumptions. */ 2802 /* First, fix up tm_yday based on the year/month/day. */ 2803 mktime(t); 2804 /* Then we can compute timegm() from first principles. */ 2805 return (t->tm_sec + t->tm_min * 60 + t->tm_hour * 3600 2806 + t->tm_yday * 86400 + (t->tm_year - 70) * 31536000 2807 + ((t->tm_year - 69) / 4) * 86400 - 2808 ((t->tm_year - 1) / 100) * 86400 2809 + ((t->tm_year + 299) / 400) * 86400); 2810#endif 2811} 2812 2813static const char * 2814build_pathname(struct archive_string *as, struct file_info *file) 2815{ 2816 if (file->parent != NULL && archive_strlen(&file->parent->name) > 0) { 2817 build_pathname(as, file->parent); 2818 archive_strcat(as, "/"); 2819 } 2820 if (archive_strlen(&file->name) == 0) 2821 archive_strcat(as, "."); 2822 else 2823 archive_string_concat(as, &file->name); 2824 return (as->s); 2825} 2826 2827#if DEBUG 2828static void 2829dump_isodirrec(FILE *out, const unsigned char *isodirrec) 2830{ 2831 fprintf(out, " l %d,", 2832 toi(isodirrec + DR_length_offset, DR_length_size)); 2833 fprintf(out, " a %d,", 2834 toi(isodirrec + DR_ext_attr_length_offset, DR_ext_attr_length_size)); 2835 fprintf(out, " ext 0x%x,", 2836 toi(isodirrec + DR_extent_offset, DR_extent_size)); 2837 fprintf(out, " s %d,", 2838 toi(isodirrec + DR_size_offset, DR_extent_size)); 2839 fprintf(out, " f 0x%02x,", 2840 toi(isodirrec + DR_flags_offset, DR_flags_size)); 2841 fprintf(out, " u %d,", 2842 toi(isodirrec + DR_file_unit_size_offset, DR_file_unit_size_size)); 2843 fprintf(out, " ilv %d,", 2844 toi(isodirrec + DR_interleave_offset, DR_interleave_size)); 2845 fprintf(out, " seq %d,", 2846 toi(isodirrec + DR_volume_sequence_number_offset, DR_volume_sequence_number_size)); 2847 fprintf(out, " nl %d:", 2848 toi(isodirrec + DR_name_len_offset, DR_name_len_size)); 2849 fprintf(out, " `%.*s'", 2850 toi(isodirrec + DR_name_len_offset, DR_name_len_size), isodirrec + DR_name_offset); 2851} 2852#endif 2853