• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

google / authenticode-rs / 16243311102

12 May 2025 06:11PM UTC coverage: 88.085% (-0.6%) from 88.65%
16243311102

push

github

nicholasbishop
chore(deps): lock file maintenance

414 of 470 relevant lines covered (88.09%)

7.57 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.72
/authenticode/src/pe_object.rs
1
// Copyright 2023 Google LLC
2
//
3
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
6
// option. This file may not be copied, modified, or distributed
7
// except according to those terms.
8

9
use crate::pe::{PeOffsetError, PeOffsets, PeTrait};
10
use crate::usize_from_u32;
11
use core::mem;
12
use core::ops::Range;
13
use object::pe::{ImageDataDirectory, IMAGE_DIRECTORY_ENTRY_SECURITY};
14
use object::read::pe::ImageOptionalHeader;
15
use object::read::pe::{ImageNtHeaders, PeFile};
16
use object::{pod, LittleEndian, SectionIndex};
17

18
impl<'data, I> PeTrait for PeFile<'data, I>
19
where
20
    I: ImageNtHeaders,
21
{
22
    fn data(&self) -> &'data [u8] {
47✔
23
        self.data()
47✔
24
    }
47✔
25

26
    fn num_sections(&self) -> usize {
5✔
27
        self.section_table().len()
5✔
28
    }
5✔
29

30
    fn section_data_range(
10✔
31
        &self,
10✔
32
        index: usize,
10✔
33
    ) -> Result<Range<usize>, PeOffsetError> {
10✔
34
        let section = self
10✔
35
            .section_table()
10✔
36
            .section(SectionIndex(index))
10✔
37
            .expect("invalid index");
10✔
38
        let start =
10✔
39
            usize_from_u32(section.pointer_to_raw_data.get(LittleEndian));
10✔
40
        let size = usize_from_u32(section.size_of_raw_data.get(LittleEndian));
10✔
41
        let end = start.checked_add(size).ok_or(PeOffsetError)?;
10✔
42
        Ok(start..end)
10✔
43
    }
10✔
44

45
    fn certificate_table_range(
21✔
46
        &self,
21✔
47
    ) -> Result<Option<Range<usize>>, PeOffsetError> {
21✔
48
        if let Some(dir) = self.data_directory(IMAGE_DIRECTORY_ENTRY_SECURITY) {
21✔
49
            let start = usize_from_u32(dir.virtual_address.get(LittleEndian));
18✔
50
            let size = usize_from_u32(dir.size.get(LittleEndian));
18✔
51
            let end = start.checked_add(size).ok_or(PeOffsetError)?;
18✔
52
            Ok(Some(start..end))
18✔
53
        } else {
54
            Ok(None)
3✔
55
        }
56
    }
21✔
57

58
    fn offsets(&self) -> Result<PeOffsets, PeOffsetError> {
5✔
59
        object_offsets_impl(self).ok_or(PeOffsetError)
5✔
60
    }
5✔
61
}
62

63
fn object_offsets_impl<I>(pe: &PeFile<I>) -> Option<PeOffsets>
5✔
64
where
5✔
65
    I: ImageNtHeaders,
5✔
66
{
67
    // Calculate the offset from the start of the pe data to the
68
    // beginning of `bytes`.
69
    let get_offset = |bytes: &[u8]| -> Option<usize> {
5✔
70
        let base = pe.data().as_ptr() as usize;
5✔
71
        let bytes_start = bytes.as_ptr() as usize;
5✔
72
        bytes_start.checked_sub(base)
5✔
73
    };
5✔
74

75
    // Get checksum offset.
76
    let optional_header = pe.nt_headers().optional_header();
5✔
77
    let optional_header_bytes = pod::bytes_of(optional_header);
5✔
78
    let optional_header_offset = get_offset(optional_header_bytes)?;
5✔
79
    let check_sum_offset = optional_header_offset.checked_add(
5✔
80
        // The offset of the `check_sum` field is the same within both
81
        // the 32-bit and 64-bit headers.
82
        64,
83
    )?;
×
84

85
    // Hash from checksum to the security data directory.
86
    let data_dirs_offset =
5✔
87
        optional_header_offset.checked_add(optional_header_bytes.len())?;
5✔
88
    let sec_dir_offset = data_dirs_offset.checked_add(
5✔
89
        mem::size_of::<ImageDataDirectory>()
5✔
90
            .checked_mul(IMAGE_DIRECTORY_ENTRY_SECURITY)?,
5✔
91
    )?;
×
92

93
    // Hash from the security data directory to the end of the header.
94
    let sec_dir_size = mem::size_of::<ImageDataDirectory>();
5✔
95
    let size_of_headers =
5✔
96
        usize_from_u32(pe.nt_headers().optional_header().size_of_headers());
5✔
97

98
    Some(PeOffsets {
99
        check_sum: check_sum_offset,
5✔
100
        after_check_sum: check_sum_offset.checked_add(mem::size_of::<u32>())?,
5✔
101

102
        security_data_dir: sec_dir_offset,
5✔
103
        after_security_data_dir: sec_dir_offset.checked_add(sec_dir_size)?,
5✔
104

105
        after_header: size_of_headers,
5✔
106
    })
107
}
5✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc