hadris-iso 2.0.0

Pure Rust ISO 9660 library with allocation-free reading, Joliet, Rock Ridge, and El Torito
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
use super::directory::DirectoryRef;
use super::io::{self, IsoCursor, LogicalSector, Read, Seek};
use super::path::{PathTableEntry, PathTableInfo, PathTableRef};
use super::volume::{PrimaryVolumeDescriptor, VolumeDescriptorList};
use crate::file::EntryType;
use crate::joliet::JolietLevel;
use hadris_common::types::endian::Endian;
#[cfg(not(feature = "alloc"))]
use hadris_common::types::no_alloc::ArrayVec;
use hadris_path::{Component, Separators, VPath};
pub use volume::VolumeDescriptorIter;

mod boot;
mod directory;
pub use boot::*;
pub use directory::{DirEntry, Extent, IsoDir};
sync_only! {
    pub use directory::{IsoDirIter, RawDirIter};
}
use spin::Mutex;

mod rrip;
pub(crate) use rrip::SuspInfo;
pub use rrip::*;

mod volume;

/// Identifies a FilenameType value.
pub enum FilenameType {
    /// The `Builtin` variant.
    Builtin,
    /// The `Joliet` variant.
    Joliet,
}

#[derive(Debug)]
/// Represents IsoImageInfo.
pub struct IsoImageInfo {
    block_size: usize,
    sector_size: usize,
    root_dirs: RootDirs,
    boot_catalog: Option<u32>,
    path_table: PathTableRef,
    pub(crate) susp_info: SuspInfo,
    /// True if an ISO 9660:1999 Enhanced Volume Descriptor (EVD) was found.
    has_evd: bool,
    /// Cached path table entries, parsed once during open() to avoid
    /// repeated seeks for directory hierarchy traversal.
    #[cfg(feature = "alloc")]
    path_table_cache: alloc::vec::Vec<PathTableEntry>,
}

impl IsoImageInfo {
    /// Returns the logical block size declared by the selected volume descriptor.
    pub fn block_size(&self) -> usize {
        self.block_size
    }

    /// Returns the physical sector size used by the image reader.
    pub fn sector_size(&self) -> usize {
        self.sector_size
    }
}

#[derive(Debug)]
/// Represents RootDirs.
pub struct RootDirs {
    #[cfg(not(feature = "alloc"))]
    dirs: ArrayVec<RootDir, 8>,
    #[cfg(feature = "alloc")]
    dirs: alloc::vec::Vec<RootDir>,
}

impl RootDirs {
    fn new() -> Self {
        Self {
            #[cfg(not(feature = "alloc"))]
            dirs: ArrayVec::new(),
            #[cfg(feature = "alloc")]
            dirs: alloc::vec::Vec::new(),
        }
    }

    /// Returns the number of directory-tree namespaces in the image.
    pub fn len(&self) -> usize {
        self.dirs.len()
    }

    /// Returns whether the image contains no directory-tree namespaces.
    pub fn is_empty(&self) -> bool {
        self.dirs.is_empty()
    }

    /// Iterates over every directory-tree namespace in descriptor order.
    pub fn iter(&self) -> core::slice::Iter<'_, RootDir> {
        self.dirs.iter()
    }

    /// Finds the root whose entry type exactly matches `ty`.
    pub fn get(&self, ty: EntryType) -> Option<RootDir> {
        self.dirs.iter().copied().find(|root| root.ty == ty)
    }

    /// Selects the most useful directory-tree namespace, if one exists.
    pub fn try_best_choice(&self) -> Option<RootDir> {
        if self.dirs.is_empty() {
            return None;
        }
        let mut best = (0, EntryType::default());
        for (idx, dir) in self.dirs.iter().enumerate() {
            if dir.ty > best.1 {
                best = (idx, dir.ty);
            }
        }
        Some(self.dirs[best.0])
    }

    /// Selects the most useful directory-tree namespace.
    ///
    /// # Panics
    ///
    /// Panics if the ISO image contains no directory trees. Use
    /// [`Self::try_best_choice`] when handling a potentially empty collection.
    pub fn best_choice(&self) -> RootDir {
        self.try_best_choice()
            .expect("ISO image contains no directory trees!")
    }
}

impl<'a> IntoIterator for &'a RootDirs {
    type Item = &'a RootDir;
    type IntoIter = core::slice::Iter<'a, RootDir>;

    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

#[derive(Debug, Default, Clone, Copy)]
/// Represents RootDir.
pub struct RootDir {
    ty: EntryType,
    dir_ref: DirectoryRef,
}

impl RootDir {
    /// Returns the filename namespace represented by this root.
    pub fn entry_type(&self) -> EntryType {
        self.ty
    }

    /// Performs the `iter` operation.
    pub fn iter<'a, DATA: Read + Seek>(&self, iso: &'a IsoImage<DATA>) -> IsoDir<'a, DATA> {
        IsoDir {
            image: iso,
            directory: self.dir_ref,
        }
    }

    /// Returns the underlying `DirectoryRef` for this root directory.
    pub fn dir_ref(&self) -> DirectoryRef {
        self.dir_ref
    }
}

#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
/// Identifies a PathSeparator value.
pub enum PathSeparator {
    /// The `ForwardSlash` variant.
    ForwardSlash = b'/',
    /// The `Backslash` variant.
    Backslash = b'\\',
}

impl PathSeparator {
    /// Performs the `as_char` operation.
    pub fn as_char(self) -> char {
        self as u8 as char
    }
}

/// A struct representing an open ISO9660 Image
///
/// This struct is interior mutable.
#[derive(Debug)]
pub struct IsoImage<DATA: Seek> {
    pub(crate) data: Mutex<IsoCursor<DATA>>,
    pub(crate) info: IsoImageInfo,
}

impl<DATA: Seek> IsoImage<DATA> {
    /// Consumes the image handle and returns its underlying data source.
    pub fn into_inner(self) -> DATA {
        self.data.into_inner().data
    }
}

io_transform! {
impl<DATA: Read + Seek> IsoImage<DATA> {
    /// Opens a ISO9660 Image
    pub async fn open(data: DATA) -> io::Result<Self> {
        let sector_size = 2048;
        let mut data = IsoCursor::new(data, sector_size);
        data.seek_sector(LogicalSector(16)).await?;
        let mut root_dirs = RootDirs::new();
        let volume_descriptors = VolumeDescriptorList::parse(&mut data).await?;
        let pvd = volume_descriptors.try_primary().ok_or_else(|| {
            io::Error::new(
                io::ErrorKind::InvalidData,
                "volume descriptor sequence has no primary descriptor",
            )
        })?;
        if !pvd.volume_space_size.is_consistent()
            || !pvd.volume_set_size.is_consistent()
            || !pvd.volume_sequence_number.is_consistent()
            || !pvd.logical_block_size.is_consistent()
            || !pvd.path_table_size.is_consistent()
            || !pvd.dir_record.header.extent.is_consistent()
            || !pvd.dir_record.header.data_len.is_consistent()
            || !pvd
                .dir_record
                .header
                .volume_sequence_number
                .is_consistent()
        {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                "primary volume descriptor has inconsistent redundant endian fields",
            ));
        }
        let block_size = pvd.logical_block_size.read() as usize;
        // IsoImage's extent→byte math assumes a 2048-byte logical block. Rather
        // than silently misread an image that declares a different block size,
        // reject it. (The allocation-free `IsoReader` honors the PVD block size;
        // full non-2048 support in `IsoImage` is future work.)
        if block_size != 2048 {
            return Err(io::Error::new(
                io::ErrorKind::Unsupported,
                "ISO logical block size other than 2048 is not supported by IsoImage",
            ));
        }
        let root_extent = LogicalSector(
            pvd.dir_record.header.extent.read() as usize
                + pvd.dir_record.header.extended_attr_record as usize,
        );
        let root_size = pvd.dir_record.header.data_len.read() as usize;
        let root_dir = DirectoryRef {
            extent: root_extent,
            size: root_size,
        };

        // Detect SUSP/RRIP from root directory's "." entry
        let susp_info = rrip::detect_susp_rrip(&mut data, root_extent).await?;
        let supports_rrip = susp_info.rrip_detected;

        root_dirs.dirs.push(RootDir {
            ty: EntryType::Level1 {
                supports_lowercase: false,
                supports_rrip,
            },
            dir_ref: root_dir,
        });

        let path_table = PathTableRef {
            lpt: LogicalSector(pvd.type_l_path_table.get() as usize),
            mpt: LogicalSector(pvd.type_m_path_table.get() as usize),
            size: pvd.path_table_size.read() as u64,
        };

        // Parse and cache path table entries
        #[cfg(feature = "alloc")]
        let path_table_cache = {
            use crate::types::EndianType;
            let pt_start = if cfg!(target_endian = "little") {
                path_table.lpt
            } else {
                path_table.mpt
            };
            let start_byte = pt_start.0 as u64 * sector_size as u64;
            let end_byte = start_byte + path_table.size;
            data.seek(super::io::SeekFrom::Start(start_byte))
                .await
                .map_err(super::io::Error::erase)?;
            let mut entries = alloc::vec::Vec::new();
            let mut pos = start_byte;
            while pos < end_byte {
                let entry = PathTableEntry::parse(&mut data, EndianType::NativeEndian).await?;
                pos += entry.size() as u64;
                entries.push(entry);
            }
            entries
        };

        let mut info = IsoImageInfo {
            block_size,
            sector_size,
            root_dirs,
            boot_catalog: None,
            path_table,
            susp_info,
            has_evd: false,
            #[cfg(feature = "alloc")]
            path_table_cache,
        };

        for svd in volume_descriptors.supplementary() {
            if svd.header.version == 1 {
                // Joliet Check
                for &level in JolietLevel::all() {
                    if svd.escape_sequences == level.escape_sequence() {
                        info.root_dirs.dirs.push(RootDir {
                            ty: EntryType::Joliet {
                                level,
                                supports_rrip: false,
                            },
                            dir_ref: DirectoryRef {
                                extent: LogicalSector(
                                    svd.dir_record.header.extent.read() as usize
                                        + svd.dir_record.header.extended_attr_record as usize,
                                ),
                                size: svd.dir_record.header.data_len.read() as usize,
                            },
                        });
                    }
                }
            } else if svd.file_structure_version == 2 {
                // ISO 9660:1999 enhanced namespace (ECMA-119, 3rd edition).
                info.has_evd = true;
                info.root_dirs.dirs.push(RootDir {
                    ty: EntryType::Level3 {
                        supports_lowercase: true,
                        supports_rrip: false,
                    },
                    dir_ref: DirectoryRef {
                        extent: LogicalSector(
                            svd.dir_record.header.extent.read() as usize
                                + svd.dir_record.header.extended_attr_record as usize,
                        ),
                        size: svd.dir_record.header.data_len.read() as usize,
                    },
                });
            }
        }

        if let Some(boot_record) = volume_descriptors.boot_record() {
            info.boot_catalog = Some(boot_record.catalog_ptr.get());
        }

        Ok(Self {
            data: Mutex::new(data),
            info,
        })
    }

    /// Read raw bytes from an absolute byte position in the image.
    pub async fn read_bytes_at(&self, byte_offset: u64, buf: &mut [u8]) -> io::Result<()> {
        let mut data = self.data.lock();
        data.seek(super::io::SeekFrom::Start(byte_offset))
            .await
            .map_err(super::io::Error::erase)?;
        data.read_exact(buf).await?;
        Ok(())
    }

    /// Finds an entry by a slash- or backslash-delimited path.
    ///
    /// Leading and repeated separators and `.` components are ignored. The
    /// root itself has no directory entry, so an empty or root-only path
    /// returns `None`. Parent (`..`) components are rejected.
    pub async fn find_path(&self, path: &str) -> io::Result<Option<DirEntry>> {
        let mut components = VPath::with_separators(path, Separators::SlashOrBackslash)
            .components()
            .filter_map(|component| match component {
                Component::Root | Component::Current => None,
                Component::Parent => Some(Err(io::Error::other(
                    "parent path components are not supported",
                ))),
                Component::Normal(component) => Some(Ok(component)),
            })
            .peekable();
        let mut directory = self.open_dir(self.root_dir().dir_ref());

        while let Some(component) = components.next() {
            let component = component?;
            let Some(entry) = directory.find(component).await? else {
                return Ok(None);
            };
            if components.peek().is_none() {
                return Ok(Some(entry));
            }
            if !entry.is_directory() {
                return Ok(None);
            }
            directory = self.open_dir(entry.as_dir_ref(self).await?);
        }
        Ok(None)
    }

    /// Read the complete contents of a file, handling multi-extent files.
    ///
    /// For single-extent files, this reads from the entry's extent.
    /// For multi-extent files (using `NOT_FINAL` flag), this reads and
    /// concatenates all extents in order.
    #[cfg(feature = "alloc")]
    pub async fn read_file(&self, entry: &directory::DirEntry) -> io::Result<alloc::vec::Vec<u8>> {
        if entry.header().file_unit_size != 0 || entry.header().interleave_gap_size != 0 {
            return Err(io::Error::new(
                io::ErrorKind::Unsupported,
                "interleaved ISO files are not supported",
            ));
        }
        let total = entry.total_size();
        // `total` comes from on-disk directory-record data-length fields (u32 each,
        // summed across extents) and is untrusted. Bound it against the actual
        // image size before allocating, otherwise a tiny image whose record claims
        // ~4 GiB would force that allocation up front — a DoS that aborts the
        // process on no-overcommit / embedded targets.
        let image_len = {
            let mut data = self.data.lock();
            data.seek(super::io::SeekFrom::End(0))
                .await
                .map_err(super::io::Error::erase)?
        };
        if total > image_len {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                "directory entry claims more data than the image contains",
            ));
        }
        let mut buf = alloc::vec![0u8; total as usize];

        if entry.is_multi_extent() {
            let mut offset = 0usize;
            for extent in entry.extents() {
                let len = extent.length as usize;
                let byte_offset = extent.sector.0 as u64 * 2048;
                self.read_bytes_at(byte_offset, &mut buf[offset..offset + len]).await?;
                offset += len;
            }
        } else {
            let header = entry.header();
            let byte_offset = (header.extent.read() as u64
                + header.extended_attr_record as u64)
                * 2048;
            let len = header.data_len.read() as usize;
            self.read_bytes_at(byte_offset, &mut buf[..len]).await?;
        }

        Ok(buf)
    }
}
} // io_transform!

impl<DATA: Read + Seek> IsoImage<DATA> {
    /// Performs the `root_dir` operation.
    pub fn root_dir(&self) -> RootDir {
        self.root_dirs().best_choice()
    }

    /// Performs the `root_dirs` operation.
    pub fn root_dirs(&self) -> &RootDirs {
        &self.info.root_dirs
    }

    /// Returns whether RRIP (Rock Ridge) extensions were detected in the image.
    pub fn supports_rrip(&self) -> bool {
        self.info.susp_info.rrip_detected
    }

    /// Returns whether an ISO 9660:1999 Enhanced Volume Descriptor was found.
    pub fn has_evd(&self) -> bool {
        self.info.has_evd
    }

    /// Open a directory by its `DirectoryRef`, enabling navigation into subdirectories.
    pub fn open_dir(&self, dir_ref: DirectoryRef) -> IsoDir<'_, DATA> {
        IsoDir {
            image: self,
            directory: dir_ref,
        }
    }

    /// Returns the path table information for this image.
    pub fn path_table(&self) -> PathTableInfo {
        PathTableInfo {
            path_table: self.info.path_table,
        }
    }

    /// Creates a volume-descriptor cursor starting at logical sector 16.
    ///
    /// Async callers use [`VolumeDescriptorIter::next_descriptor`]. In sync
    /// builds the cursor also implements [`Iterator`].
    pub fn read_volume_descriptors(&self) -> VolumeDescriptorIter<'_, DATA> {
        VolumeDescriptorIter {
            data: &self.data,
            current_sector: LogicalSector(16),
            done: false,
        }
    }

    /// Returns the cached path table entries, parsed once during `open()`.
    ///
    /// Each entry contains a directory name, its LBA, and its parent index,
    /// enabling fast directory hierarchy traversal without repeated seeks.
    #[cfg(feature = "alloc")]
    pub fn path_table_entries(&self) -> &[PathTableEntry] {
        &self.info.path_table_cache
    }
}

io_transform! {
impl<DATA: Read + Seek> IsoImage<DATA> {
    /// Reads the primary volume descriptor.
    ///
    /// Returns an I/O error if the descriptor sequence is malformed, truncated,
    /// or contains no primary descriptor.
    pub async fn read_pvd(&self) -> io::Result<PrimaryVolumeDescriptor> {
        let mut descriptors = self.read_volume_descriptors();
        while let Some(descriptor) = descriptors.next_descriptor().await? {
            if let super::volume::VolumeDescriptor::Primary(pvd) = descriptor {
                return Ok(pvd);
            }
        }
        Err(io::Error::new(
            io::ErrorKind::InvalidData,
            "primary volume descriptor not found",
        ))
    }
}
} // io_transform!