Coverage Report

Created: 2023-11-19 07:09

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