LCOV - code coverage report
Current view: top level - src - binary.rs (source / functions) Coverage Total Hit
Test: lief.lcov Lines: 85.0 % 40 34
Test Date: 2024-10-27:00:00:00 Functions: 50.0 % 4 2

            Line data    Source code
       1              : use lief_ffi as ffi;
       2              : 
       3              : use super::elf;
       4              : use super::macho;
       5              : use super::pe;
       6              : use crate::common::FromFFI;
       7              : use std::io::{Read, Seek};
       8              : 
       9            0 : #[derive(Debug)]
      10              : /// Enum that wraps all the executable formats supported by LIEF
      11              : pub enum Binary {
      12              :     ELF(elf::Binary),
      13              :     PE(pe::Binary),
      14              :     MachO(macho::FatBinary),
      15              : }
      16              : 
      17              : impl Binary {
      18              :     /// Parse form a file path
      19              :     ///
      20              :     /// ```
      21              :     /// if let Some(Binary::ELF(elf)) = Binary::parse("/bin/ls") {
      22              :     ///     // ...
      23              :     /// }
      24              :     /// ```
      25          224 :     pub fn parse(path: &str) -> Option<Binary> {
      26          224 :         if ffi::ELF_Utils::is_elf(path) {
      27           64 :             return Some(Binary::ELF(elf::Binary::parse(path)));
      28          160 :         }
      29          160 :         if ffi::PE_Utils::is_pe(path) {
      30           64 :             return Some(Binary::PE(pe::Binary::parse(path)));
      31           96 :         }
      32           96 :         if ffi::MachO_Utils::is_macho(path) {
      33           96 :             return Some(Binary::MachO(macho::FatBinary::parse(path)));
      34            0 :         }
      35            0 :         None
      36          224 :     }
      37              :     /// Parse from an input that implements `Read + Seek` traits
      38              :     ///
      39              :     /// ```
      40              :     /// let mut file = std::fs::File::open("C:/test.ext").expect("Can't open the file");
      41              :     /// if let Some(Binary::PE(pe)) = Binary::from(&mut file) {
      42              :     ///     // ...
      43              :     /// }
      44              :     /// ```
      45           27 :     pub fn from<R: Read + Seek>(reader: &mut R) -> Option<Binary> {
      46           27 :         let mut buffer = std::vec::Vec::new();
      47           27 :         if reader.read_to_end(&mut buffer).is_err() {
      48            0 :             return None;
      49           27 :         }
      50           27 : 
      51           27 :         let mut ffi_stream =
      52           27 :             unsafe { ffi::RustStream::from_rust(buffer.as_mut_ptr(), buffer.len()) };
      53           27 :         buffer.clear();
      54           27 : 
      55           27 :         if ffi_stream.is_elf() {
      56            8 :             return Some(Binary::ELF(elf::Binary::from_ffi(
      57            8 :                 ffi_stream.pin_mut().as_elf(),
      58            8 :             )));
      59           19 :         }
      60           19 :         if ffi_stream.is_macho() {
      61           11 :             return Some(Binary::MachO(macho::FatBinary::from_ffi(
      62           11 :                 ffi_stream.pin_mut().as_macho(),
      63           11 :             )));
      64            8 :         }
      65            8 :         if ffi_stream.is_pe() {
      66            8 :             return Some(Binary::PE(pe::Binary::from_ffi(
      67            8 :                 ffi_stream.pin_mut().as_pe(),
      68            8 :             )));
      69            0 :         }
      70            0 :         None
      71           27 :     }
      72              : }
        

Generated by: LCOV version 2.1-1