caddy-hexspell 1.2.3

Dependency-free Rust library for parsing and patching PE, ELF, and Mach-O executables with a 1:1 on-disk API
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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
//! Streaming PE directory inspection and optional RVA lookup indexing.
//!
//! Iterators yield errors once, then stop. Names borrow file bytes when UTF-8 is
//! valid; import names retain the owned parser's lossy decoding policy.

use super::{
    debug::DebugDirectoryEntry,
    export::ExportDirectory,
    header::PEType,
    import::{self, ImageImportDescriptor, ThunkSlot},
    relocation::{BaseRelocationBlock, BaseRelocationEntry},
    section::PeSection,
};
use crate::{
    errors::FileParseError,
    field::Field,
    utils::{extract_u16, extract_u32, extract_u64},
};
use alloc::borrow::Cow;
use alloc::vec::Vec;

fn extent(
    bytes: &[u8],
    start: usize,
    count: usize,
    stride: usize,
) -> Result<usize, FileParseError> {
    let end = count
        .checked_mul(stride)
        .and_then(|n| start.checked_add(n))
        .ok_or(FileParseError::BufferOverflow)?;
    bytes
        .get(start..end)
        .ok_or(FileParseError::BufferOverflow)?;
    Ok(end)
}

fn name(bytes: &[u8], offset: usize) -> Result<Cow<'_, str>, FileParseError> {
    crate::strings::read_c_string_ref(bytes, offset)
}

fn strict_name(bytes: &[u8], offset: usize) -> Result<&str, FileParseError> {
    core::str::from_utf8(crate::strings::read_c_bytes(bytes, offset)?)
        .map_err(|_| FileParseError::InvalidFileFormat)
}

macro_rules! streaming {
    ($typ:ident, $item:ty) => {
        impl<'a> Iterator for $typ<'a> {
            type Item = Result<$item, FileParseError>;
            fn next(&mut self) -> Option<Self::Item> {
                if self.done {
                    return None;
                }
                match self.read_next() {
                    Ok(Some(item)) => Some(Ok(item)),
                    Ok(None) => {
                        self.done = true;
                        None
                    }
                    Err(error) => {
                        self.done = true;
                        Some(Err(error))
                    }
                }
            }
        }
        impl core::iter::FusedIterator for $typ<'_> {}
    };
}

/// Optional index over immutable file-backed section ranges.
///
/// Non-overlapping ranges use binary search. Overlapping ranges fall back to
/// file-order lookup so results match `PE::rva_to_offset`.
pub struct RvaIndex<'a> {
    bytes: &'a [u8],
    sections: &'a [PeSection],
    sorted: Vec<usize>,
    overlapping: bool,
}

impl<'a> RvaIndex<'a> {
    /// Builds an index, retaining borrows so edits cannot silently invalidate it.
    pub fn new(bytes: &'a [u8], sections: &'a [PeSection]) -> Self {
        let mut sorted = Vec::with_capacity(sections.len());
        sorted.extend((0..sections.len()).filter(|&i| sections[i].size_of_raw_data.value != 0));
        sorted.sort_unstable_by_key(|&i| sections[i].virtual_address.value);
        let overlapping = sorted.windows(2).any(|pair| {
            let left = &sections[pair[0]];
            left.virtual_address
                .value
                .saturating_add(left.size_of_raw_data.value)
                > sections[pair[1]].virtual_address.value
        });
        Self {
            bytes,
            sections,
            sorted,
            overlapping,
        }
    }

    /// Translates an RVA with the same file-backed and header rules as the linear mapper.
    pub fn rva_to_offset(&self, rva: u32) -> Result<usize, FileParseError> {
        if rva == 0
            || self.overlapping
            || self
                .sections
                .first()
                .is_some_and(|s| rva < s.virtual_address.value)
        {
            return import::rva_to_offset(self.bytes, self.sections, rva);
        }
        let end = self
            .sorted
            .partition_point(|&i| self.sections[i].virtual_address.value <= rva);
        if end == 0 {
            return Err(FileParseError::InvalidFileFormat);
        }
        let section = &self.sections[self.sorted[end - 1]];
        if rva
            >= section
                .virtual_address
                .value
                .saturating_add(section.size_of_raw_data.value)
        {
            return Err(FileParseError::InvalidFileFormat);
        }
        let offset = (section.pointer_to_raw_data.value as usize)
            .checked_add((rva - section.virtual_address.value) as usize)
            .ok_or(FileParseError::BufferOverflow)?;
        if offset >= self.bytes.len() {
            return Err(FileParseError::BufferOverflow);
        }
        Ok(offset)
    }
}

#[derive(Clone, Copy)]
struct Mapper<'a> {
    bytes: &'a [u8],
    sections: &'a [PeSection],
    index: Option<&'a RvaIndex<'a>>,
}

impl Mapper<'_> {
    fn offset(&self, rva: u32) -> Result<usize, FileParseError> {
        match self.index {
            Some(index) => index.rva_to_offset(rva),
            None => import::rva_to_offset(self.bytes, self.sections, rva),
        }
    }
    fn check_index(&self, index: &RvaIndex<'_>) -> Result<(), FileParseError> {
        if core::ptr::eq(self.bytes, index.bytes) && core::ptr::eq(self.sections, index.sections) {
            Ok(())
        } else {
            Err(FileParseError::InvalidFileFormat)
        }
    }
}

/// Imports from one DLL; descriptor metadata is stored once.
pub struct DllImportRef<'a> {
    /// Original descriptor fields.
    pub descriptor: ImageImportDescriptor,
    /// File offset of the DLL name.
    pub dll_name_offset: usize,
    /// Borrowed DLL name, or owned lossy decoding for invalid UTF-8.
    pub dll_name: Cow<'a, str>,
    /// Streaming ILT/IAT entries.
    pub entries: ImportEntries<'a>,
}

/// A resolved import with its original thunk and borrowed name.
pub enum ImportEntryRef<'a> {
    /// Import by ordinal.
    Ordinal { ordinal: u16, thunk: ThunkSlot },
    /// Import by name.
    ByName {
        hint: Field<u16>,
        name_offset: usize,
        name: Cow<'a, str>,
        thunk: ThunkSlot,
    },
}

/// Streaming import descriptors and DLL names.
pub struct Imports<'a> {
    map: Mapper<'a>,
    offset: usize,
    pe_type: PEType,
    done: bool,
}

impl<'a> Imports<'a> {
    /// Begins import inspection without collecting descriptors or thunks.
    pub fn new(
        bytes: &'a [u8],
        sections: &'a [PeSection],
        rva: u32,
        pe_type: PEType,
    ) -> Result<Self, FileParseError> {
        let map = Mapper {
            bytes,
            sections,
            index: None,
        };
        let offset = if rva == 0 { 0 } else { map.offset(rva)? };
        Ok(Self {
            map,
            offset,
            pe_type,
            done: rva == 0,
        })
    }

    /// Uses an index built for the same immutable buffer and section table.
    pub fn with_index(mut self, index: &'a RvaIndex<'a>) -> Result<Self, FileParseError> {
        self.map.check_index(index)?;
        self.map.index = Some(index);
        Ok(self)
    }

    fn read_next(&mut self) -> Result<Option<DllImportRef<'a>>, FileParseError> {
        extent(self.map.bytes, self.offset, 1, ImageImportDescriptor::SIZE)?;
        let descriptor = ImageImportDescriptor::parse(self.map.bytes, self.offset)?;
        if descriptor.is_null() {
            return Ok(None);
        }
        self.offset += ImageImportDescriptor::SIZE;
        let dll_name_offset = self.map.offset(descriptor.name.value)?;
        let dll_name = name(self.map.bytes, dll_name_offset)?;
        let rva = if descriptor.original_first_thunk.value != 0 {
            descriptor.original_first_thunk.value
        } else {
            descriptor.first_thunk.value
        };
        let offset = if rva == 0 { 0 } else { self.map.offset(rva)? };
        let entries = ImportEntries {
            map: self.map,
            offset,
            pe_type: self.pe_type,
            done: rva == 0,
        };
        Ok(Some(DllImportRef {
            descriptor,
            dll_name_offset,
            dll_name,
            entries,
        }))
    }
}
streaming!(Imports, DllImportRef<'a>);

/// Streaming import thunk entries.
pub struct ImportEntries<'a> {
    map: Mapper<'a>,
    offset: usize,
    pe_type: PEType,
    done: bool,
}

impl<'a> ImportEntries<'a> {
    fn read_next(&mut self) -> Result<Option<ImportEntryRef<'a>>, FileParseError> {
        let (raw, stride, flag) = match self.pe_type {
            PEType::PE32 => (
                u64::from(extract_u32(self.map.bytes, self.offset)?),
                4,
                1u64 << 31,
            ),
            PEType::PE32Plus => (extract_u64(self.map.bytes, self.offset)?, 8, 1u64 << 63),
        };
        if raw == 0 {
            return Ok(None);
        }
        let offset = self.offset;
        self.offset = offset
            .checked_add(stride)
            .ok_or(FileParseError::BufferOverflow)?;
        let thunk = if stride == 4 {
            ThunkSlot::U32(Field::new(raw as u32, offset, 4))
        } else {
            ThunkSlot::U64(Field::new(raw, offset, 8))
        };
        if raw & flag != 0 {
            return Ok(Some(ImportEntryRef::Ordinal {
                ordinal: raw as u16,
                thunk,
            }));
        }
        let rva = u32::try_from(raw).map_err(|_| FileParseError::ValueTooLarge)?;
        let offset = self.map.offset(rva)?;
        let hint = Field::new(extract_u16(self.map.bytes, offset)?, offset, 2);
        let name_offset = offset
            .checked_add(2)
            .ok_or(FileParseError::BufferOverflow)?;
        Ok(Some(ImportEntryRef::ByName {
            hint,
            name_offset,
            name: name(self.map.bytes, name_offset)?,
            thunk,
        }))
    }
}
streaming!(ImportEntries, ImportEntryRef<'a>);

/// A named export borrowing the original name bytes.
pub struct NamedExportRef<'a> {
    /// Borrowed UTF-8 export name.
    pub name: &'a str,
    /// Export ordinal.
    pub ordinal: u16,
    /// Original slot in the export address table.
    pub function_rva: Field<u32>,
    /// Original slot in the export name pointer table.
    pub name_rva: Field<u32>,
    /// Original slot in the export name ordinal table.
    pub name_ordinal_index: Field<u16>,
}

/// An export address table slot, including ordinal-only entries.
pub struct FunctionExportRef<'a> {
    /// Export ordinal.
    pub ordinal: u16,
    /// Position in the export address table.
    pub index: usize,
    /// Original slot in the export address table.
    pub function_rva: Field<u32>,
    /// Borrowed forwarder string, when this slot is a forwarder.
    pub forwarder: Option<&'a str>,
}

struct ExportTables<'a> {
    map: Mapper<'a>,
    directory: Option<ExportDirectory>,
    rva: u32,
    size: u32,
    functions: usize,
}

impl<'a> ExportTables<'a> {
    fn new(
        bytes: &'a [u8],
        sections: &'a [PeSection],
        rva: u32,
        size: u32,
    ) -> Result<Self, FileParseError> {
        let map = Mapper {
            bytes,
            sections,
            index: None,
        };
        let directory = if rva == 0 {
            None
        } else {
            let offset = map.offset(rva)?;
            extent(bytes, offset, 1, 40)?;
            Some(ExportDirectory::parse(bytes, offset)?)
        };
        let functions = match &directory {
            Some(d) if d.number_of_functions.value != 0 => {
                let offset = map.offset(d.address_of_functions.value)?;
                extent(bytes, offset, d.number_of_functions.value as usize, 4)?;
                offset
            }
            _ => 0,
        };
        Ok(Self {
            map,
            directory,
            rva,
            size,
            functions,
        })
    }
    fn ordinal(&self, index: u32) -> Result<u16, FileParseError> {
        self.directory
            .as_ref()
            .ok_or(FileParseError::InvalidFileFormat)?
            .base
            .value
            .checked_add(index)
            .and_then(|n| u16::try_from(n).ok())
            .ok_or(FileParseError::ValueTooLarge)
    }
}

/// Streaming named export table rows.
pub struct Exports<'a> {
    tables: ExportTables<'a>,
    names: usize,
    ordinals: usize,
    current: usize,
    count: usize,
    done: bool,
}

impl<'a> Exports<'a> {
    /// Validates export table extents without collecting named entries.
    pub fn new(
        bytes: &'a [u8],
        sections: &'a [PeSection],
        rva: u32,
        size: u32,
    ) -> Result<Self, FileParseError> {
        let tables = ExportTables::new(bytes, sections, rva, size)?;
        let (count, names, ordinals) = match &tables.directory {
            Some(d) if d.number_of_names.value != 0 => {
                let count = d.number_of_names.value as usize;
                let names = tables.map.offset(d.address_of_names.value)?;
                let ordinals = tables.map.offset(d.address_of_name_ordinals.value)?;
                extent(bytes, names, count, 4)?;
                extent(bytes, ordinals, count, 2)?;
                (count, names, ordinals)
            }
            _ => (0, 0, 0),
        };
        Ok(Self {
            tables,
            names,
            ordinals,
            current: 0,
            count,
            done: false,
        })
    }

    /// Uses an index built for the same immutable buffer and section table.
    pub fn with_index(mut self, index: &'a RvaIndex<'a>) -> Result<Self, FileParseError> {
        self.tables.map.check_index(index)?;
        self.tables.map.index = Some(index);
        Ok(self)
    }

    fn read_next(&mut self) -> Result<Option<NamedExportRef<'a>>, FileParseError> {
        if self.current == self.count {
            return Ok(None);
        }
        let i = self.current;
        self.current += 1;
        let bytes = self.tables.map.bytes;
        let noff = self.names + i * 4;
        let ooff = self.ordinals + i * 2;
        let name_rva = Field::new(extract_u32(bytes, noff)?, noff, 4);
        let name_ordinal_index = Field::new(extract_u16(bytes, ooff)?, ooff, 2);
        if u32::from(name_ordinal_index.value)
            >= self
                .tables
                .directory
                .as_ref()
                .ok_or(FileParseError::InvalidFileFormat)?
                .number_of_functions
                .value
        {
            return Err(FileParseError::InvalidFileFormat);
        }
        let foff = self.tables.functions + usize::from(name_ordinal_index.value) * 4;
        Ok(Some(NamedExportRef {
            name: strict_name(bytes, self.tables.map.offset(name_rva.value)?)?,
            ordinal: self.tables.ordinal(u32::from(name_ordinal_index.value))?,
            function_rva: Field::new(extract_u32(bytes, foff)?, foff, 4),
            name_rva,
            name_ordinal_index,
        }))
    }
}
streaming!(Exports, NamedExportRef<'a>);

/// Streaming export address table slots.
pub struct ExportFunctions<'a> {
    tables: ExportTables<'a>,
    current: usize,
    done: bool,
}

impl<'a> ExportFunctions<'a> {
    /// Validates the address table extent without collecting entries.
    pub fn new(
        bytes: &'a [u8],
        sections: &'a [PeSection],
        rva: u32,
        size: u32,
    ) -> Result<Self, FileParseError> {
        Ok(Self {
            tables: ExportTables::new(bytes, sections, rva, size)?,
            current: 0,
            done: false,
        })
    }

    /// Uses an index built for the same immutable buffer and section table.
    pub fn with_index(mut self, index: &'a RvaIndex<'a>) -> Result<Self, FileParseError> {
        self.tables.map.check_index(index)?;
        self.tables.map.index = Some(index);
        Ok(self)
    }

    fn read_next(&mut self) -> Result<Option<FunctionExportRef<'a>>, FileParseError> {
        let Some(d) = &self.tables.directory else {
            return Ok(None);
        };
        if self.current == d.number_of_functions.value as usize {
            return Ok(None);
        }
        let index = self.current;
        self.current += 1;
        let offset = self.tables.functions + index * 4;
        let function_rva = Field::new(extract_u32(self.tables.map.bytes, offset)?, offset, 4);
        let forwarder = if d.is_forwarder_rva(self.tables.rva, self.tables.size, function_rva.value)
        {
            Some(strict_name(
                self.tables.map.bytes,
                self.tables.map.offset(function_rva.value)?,
            )?)
        } else {
            None
        };
        Ok(Some(FunctionExportRef {
            ordinal: self.tables.ordinal(index as u32)?,
            index,
            function_rva,
            forwarder,
        }))
    }
}
streaming!(ExportFunctions, FunctionExportRef<'a>);

/// A borrowed relocation block, retaining its original header fields.
pub struct RelocationBlockRef<'a> {
    bytes: &'a [u8],
    start: usize,
    end: usize,
    /// Page RVA from the block header.
    pub page_rva: Field<u32>,
    /// Total block size from the block header.
    pub block_size: Field<u32>,
}

impl RelocationBlockRef<'_> {
    /// Iterates relocation words without allocating.
    pub fn entries(&self) -> impl ExactSizeIterator<Item = BaseRelocationEntry> + '_ {
        self.bytes[self.start..self.end]
            .as_chunks::<2>()
            .0
            .iter()
            .enumerate()
            .map(|(i, raw)| BaseRelocationEntry {
                raw: Field::new(u16::from_le_bytes([raw[0], raw[1]]), self.start + i * 2, 2),
            })
    }

    /// Explicitly collects this block for callers that need editable metadata.
    pub fn to_owned(&self) -> BaseRelocationBlock {
        BaseRelocationBlock {
            page_rva: self.page_rva.clone(),
            block_size: self.block_size.clone(),
            entries: self.entries().collect(),
        }
    }
}

/// Streaming relocation blocks backed by the directory byte range.
pub struct Relocations<'a> {
    bytes: &'a [u8],
    offset: usize,
    end: usize,
    done: bool,
}

impl<'a> Relocations<'a> {
    /// Validates the directory extent; each block is checked when visited.
    pub fn new(bytes: &'a [u8], offset: usize, size: usize) -> Result<Self, FileParseError> {
        let end = extent(bytes, offset, size, 1)?;
        Ok(Self {
            bytes,
            offset,
            end,
            done: false,
        })
    }

    fn read_next(&mut self) -> Result<Option<RelocationBlockRef<'a>>, FileParseError> {
        if self.offset == self.end {
            return Ok(None);
        }
        if self.end - self.offset < 8 {
            return Err(FileParseError::InvalidFileFormat);
        }
        let offset = self.offset;
        let page_rva = Field::new(extract_u32(self.bytes, offset)?, offset, 4);
        let size = extract_u32(self.bytes, offset + 4)?;
        if size < 8 || size % 2 != 0 {
            return Err(FileParseError::InvalidFileFormat);
        }
        let next = offset
            .checked_add(size as usize)
            .ok_or(FileParseError::BufferOverflow)?;
        if next > self.end {
            return Err(FileParseError::BufferOverflow);
        }
        self.offset = next;
        Ok(Some(RelocationBlockRef {
            bytes: self.bytes,
            start: offset + 8,
            end: next,
            page_rva,
            block_size: Field::new(size, offset + 4, 4),
        }))
    }
}
streaming!(Relocations, RelocationBlockRef<'a>);

/// Streaming fixed-size debug directory records.
pub struct DebugEntries<'a> {
    bytes: &'a [u8],
    offset: usize,
    end: usize,
    done: bool,
}

impl<'a> DebugEntries<'a> {
    /// Validates the directory extent and fixed record size.
    pub fn new(bytes: &'a [u8], offset: usize, size: usize) -> Result<Self, FileParseError> {
        if !size.is_multiple_of(DebugDirectoryEntry::SIZE) {
            return Err(FileParseError::InvalidFileFormat);
        }
        let end = extent(bytes, offset, size, 1)?;
        Ok(Self {
            bytes,
            offset,
            end,
            done: false,
        })
    }

    fn read_next(&mut self) -> Result<Option<DebugDirectoryEntry>, FileParseError> {
        if self.offset == self.end {
            return Ok(None);
        }
        let record = DebugDirectoryEntry::parse(self.bytes, self.offset)?;
        self.offset += DebugDirectoryEntry::SIZE;
        Ok(Some(record))
    }
}
streaming!(DebugEntries, DebugDirectoryEntry);