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