Skip to content

Commit

Permalink
some refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
marirs committed Oct 21, 2024
1 parent 69443be commit ac70c5f
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 41 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "file_info"
authors = ["SG", "Andrey Mnatsakanov", "Clement Karimi"]
description = "Provides file information for PE and ELF files"
version = "0.3.1"
version = "0.3.2"
edition = "2021"

[dependencies]
Expand Down
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Provides some information on PE and ELF files.
- Rust 1.60+

### Example
```bash
```text
$ ./fileinfo ~/Downloads/test.exe
+-----------------+----------------------------------------------+
Expand Down Expand Up @@ -52,23 +52,23 @@ $ ./fileinfo ~/Downloads/test.exe
+==============+=======+============+=======+===============================+
| Product Name | Build | Product ID | Count | Guessed Visual Studio Version |
+--------------+-------+------------+-------+-------------------------------+
| Implib900 | 30729 | 147 | 12 | Unmarked objects (old) |
| Implib900 | 30729 | 147 | 12 | VS2008 SP1 build 30729 |
+--------------+-------+------------+-------+-------------------------------+
| Implib1400 | 29913 | 257 | 2 | Unmarked objects (old) |
| Implib1400 | 29913 | 257 | 2 | VS2019 v16.9.2 build 29913 |
+--------------+-------+------------+-------+-------------------------------+
| Utc1900_CPP | 29913 | 261 | 22 | Unmarked objects (old) |
| Utc1900_CPP | 29913 | 261 | 22 | VS2019 v16.9.2 build 29913 |
+--------------+-------+------------+-------+-------------------------------+
| Utc1900_C | 29913 | 260 | 9 | Unmarked objects (old) |
| Utc1900_C | 29913 | 260 | 9 | VS2019 v16.9.2 build 29913 |
+--------------+-------+------------+-------+-------------------------------+
| Masm1400 | 29913 | 259 | 3 | Unmarked objects (old) |
| Masm1400 | 29913 | 259 | 3 | VS2019 v16.9.2 build 29913 |
+--------------+-------+------------+-------+-------------------------------+
| Implib1400 | 26715 | 257 | 9 | Unmarked objects (old) |
| Implib1400 | 26715 | 257 | 9 | UNKNOWN PRODUCT |
+--------------+-------+------------+-------+-------------------------------+
| Import0 | 0 | 1 | 164 | Unmarked objects (old) |
| Import0 | 0 | 1 | 164 | Unmarked objects |
+--------------+-------+------------+-------+-------------------------------+
| Unknown | 0 | 0 | 17 | Unmarked objects (old) |
+--------------+-------+------------+-------+-------------------------------+
| Linker1400 | 29914 | 258 | 1 | Unmarked objects (old) |
| Linker1400 | 29914 | 258 | 1 | VS2019 v16.9.4 build 29914 |
+--------------+-------+------------+-------+-------------------------------+
+---------------------------+---------------------------------------------------------------------------------------------------------------+
| Signatures |
Expand Down
14 changes: 7 additions & 7 deletions examples/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,11 @@ fn main() -> Result<()> {
if !rich_table.rich_entries.is_empty() {
for v in rich_table.rich_entries.iter() {
tbl.add_row(Row::new(vec![
Cell::new(&v.name),
Cell::new(&v.product_name),
Cell::new(&v.build.to_string()),
Cell::new(&v.product.to_string()),
Cell::new(&v.product_id.to_string()),
Cell::new(&v.count.to_string()),
Cell::new(&v.guessed_vs_ver),
Cell::new(&v.guessed_visual_studio_version),
]));
}
} else {
Expand Down Expand Up @@ -344,10 +344,10 @@ fn main() -> Result<()> {
format!("{:#01x}", v.data_end.unwrap())
};
tbl.add_row(Row::new(vec![
Cell::new(&v.rsrc_type),
Cell::new(&v.type_id.unwrap_or(0).to_string()),
Cell::new(&v.rsrc_id),
Cell::new(&v.lang_id),
Cell::new(&v.resource_type),
Cell::new(&v.offset.unwrap_or(0).to_string()),
Cell::new(&v.resource_id),
Cell::new(&v.language_id),
Cell::new(&data_start),
Cell::new(&data_end),
]));
Expand Down
2 changes: 1 addition & 1 deletion src/elf/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl TryFrom<&elf::Elf<'_>> for Imports {
if sym.is_import() && sym.is_function() {
if let Some(ee) = elf.strtab.get_at(sym.st_name) {
if let Some((func, module)) = ee.split_once("@@") {
let e = acc.entry(module.to_string()).or_insert(vec![]);
let e = acc.entry(module.to_string()).or_default();
e.push(func.to_string())
}
}
Expand Down
16 changes: 8 additions & 8 deletions src/pe/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq, PartialOrd, Ord, Eq)]
pub struct ResourceEntry {
pub rsrc_type: String,
pub type_id: Option<u32>,
pub rsrc_id: String,
pub lang_id: String,
pub resource_type: String,
pub offset: Option<u32>,
pub resource_id: String,
pub language_id: String,
pub data_start: Option<usize>,
pub data_end: Option<usize>,
}
Expand Down Expand Up @@ -41,17 +41,17 @@ impl Resources {
};
for entry in rsrc.resources {
let mut resource_entry = ResourceEntry::default();
resource_entry.rsrc_type = match &entry.type_id {
resource_entry.resource_type = match &entry.type_id {
ResolvedDirectoryID::ID(id) => {
resource_entry.type_id = Some(*id);
resource_entry.offset = Some(*id);
resource_id_to_type(ResourceID::from_u32(*id))
}
ResolvedDirectoryID::Name(name) => name.to_owned(),
};
resource_entry.data_end = None;
resource_entry.data_start = None;
resource_entry.rsrc_id = format!("{:?}", entry.rsrc_id).to_string();
resource_entry.lang_id = format!("{:?}", entry.lang_id).to_string();
resource_entry.resource_id = format!("{:?}", entry.rsrc_id).to_string();
resource_entry.language_id = format!("{:?}", entry.lang_id).to_string();

let data_entry = entry.get_data_entry(pe).unwrap_or(&ImageResourceDataEntry {
offset_to_data: RVA(0),
Expand Down
28 changes: 13 additions & 15 deletions src/pe/rich_headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,13 +266,11 @@ const RICH_MARKER: u32 = 0x68636952; // "Rich"
#[derive(Clone, Debug, Eq, PartialEq, Default, Hash, Serialize, Deserialize)]
#[repr(C)]
pub struct RichRecord {
#[serde(rename = "product_name")]
pub name: String,
pub product_name: String,
pub build: u16,
pub product: u16,
pub product_id: u16,
pub count: u32,
#[serde(rename = "guessed_visual_studio_version")]
pub guessed_vs_ver: String,
pub guessed_visual_studio_version: String,
}

impl RichRecord {
Expand All @@ -283,33 +281,33 @@ impl RichRecord {
let product = ((field >> 16) & 0xffff) as u16;
let count = values[1] ^ key;
let mut rr = RichRecord {
name: "".to_string(),
product_name: "".to_string(),
build,
product,
product_id: product,
count,
guessed_vs_ver: "".to_string(),
guessed_visual_studio_version: "".to_string(),
};
rr.name = rr.get_product_name().to_string();
rr.guessed_vs_ver = rr.lookup_vs_version().to_string();
rr.product_name = rr.get_product_name().to_string();
rr.guessed_visual_studio_version = rr.lookup_vs_version().to_string();
rr
}
/// Encodes the record with the given key.
pub fn encode(&self, key: u32) -> [u32; 2] {
let value = (self.product as u32) << 16 | (self.build as u32);
let value = (self.product_id as u32) << 16 | (self.build as u32);
[value ^ key, self.count ^ key]
}

pub fn lookup_vs_version(&self) -> &'static str {
match COMP_ID_MAP.get(&(((self.product as u32) << 16) | self.build as u32)) {
match COMP_ID_MAP.get(&(((self.product_id as u32) << 16) | self.build as u32)) {
Some(dd) => dd,
_ => "UNKNOWN PRODUCT",
}
}

pub fn get_product_name(&self) -> &'static str {
KNOWN_PRODUCT_IDS
.contains_key(&self.product)
.then(|| KNOWN_PRODUCT_IDS[&self.product])
.contains_key(&self.product_id)
.then(|| KNOWN_PRODUCT_IDS[&self.product_id])
.unwrap_or("")
}
}
Expand Down Expand Up @@ -404,7 +402,7 @@ impl<'a> RichStructure<'a> {
}

for record in records {
let value = (record.product as u32) << 16 | (record.build as u32);
let value = (record.product_id as u32) << 16 | (record.build as u32);
csum = u32::wrapping_add(csum, value.rotate_left(record.count));
}

Expand Down

0 comments on commit ac70c5f

Please sign in to comment.