use alloc::format;
use alloc::string::{String, ToString};
use alloc::vec::Vec;
const RT_VERSION: u32 = 16;
const THE_MARK_OF_THE_FIXED: u32 = 0xFEEF_04BD;
pub fn the_version_of(bytes: &[u8]) -> Option<String> {
let blob = the_blob_of_the_version(bytes)?;
let fixed = the_fixed_of_the_blob(&blob);
match fixed {
Some(version) => Some(version),
None => the_text_of_the_version(&blob),
}
}
fn the_blob_of_the_version(bytes: &[u8]) -> Option<Vec<u8>> {
if bytes.len() < 0x40 || &bytes[0..2] != b"MZ" {
return None;
}
let header = the_u32(bytes, 0x3C)? as usize;
if bytes.len() < header + 24 || &bytes[header..header + 4] != b"PE\0\0" {
return None;
}
let sections = the_u16(bytes, header + 6)? as usize;
let size_of_the_optional = the_u16(bytes, header + 20)? as usize;
let optional = header + 24;
let magic = the_u16(bytes, optional)?;
let directories = match magic {
0x10B => optional + 96,
0x20B => optional + 112,
_ => return None,
};
let resource = the_u32(bytes, directories + 16)?;
let size = the_u32(bytes, directories + 20)? as usize;
if resource == 0 || size == 0 {
return None;
}
let table = optional + size_of_the_optional;
let the_file_place = |place: u32| -> Option<usize> {
for number in 0..sections {
let row = table + number * 40;
let address = the_u32(bytes, row + 12)?;
let virtual_size = the_u32(bytes, row + 8)?;
let raw_size = the_u32(bytes, row + 16)?;
let size = virtual_size.max(raw_size);
let raw = the_u32(bytes, row + 20)?;
if place >= address && place < address + size {
return Some((raw + (place - address)) as usize);
}
}
None
};
let root = the_file_place(resource)?;
let leaf = the_entry_of_the_kind(bytes, root, RT_VERSION)?;
let rva = the_u32(bytes, leaf)?;
let size = the_u32(bytes, leaf + 4)? as usize;
let blob = the_file_place(rva)?;
bytes.get(blob..blob + size).map(|slice| slice.to_vec())
}
fn the_entry_of_the_kind(bytes: &[u8], root: usize, kind: u32) -> Option<usize> {
let entries = the_u16(bytes, root + 12)? as usize + the_u16(bytes, root + 14)? as usize;
for number in 0..entries {
let entry = root + 16 + number * 8;
let name = the_u32(bytes, entry)?;
if name & 0x8000_0000 != 0 || name != kind {
continue;
}
let offset = the_u32(bytes, entry + 4)?;
if offset & 0x8000_0000 == 0 {
return Some(root + (offset & 0x7FFF_FFFF) as usize);
}
let directory = root + (offset & 0x7FFF_FFFF) as usize;
let names =
the_u16(bytes, directory + 12)? as usize + the_u16(bytes, directory + 14)? as usize;
if names == 0 {
return None;
}
let name_entry = directory + 16;
let name_offset = the_u32(bytes, name_entry + 4)?;
if name_offset & 0x8000_0000 == 0 {
return Some(root + (name_offset & 0x7FFF_FFFF) as usize);
}
let languages = root + (name_offset & 0x7FFF_FFFF) as usize;
let count =
the_u16(bytes, languages + 12)? as usize + the_u16(bytes, languages + 14)? as usize;
if count == 0 {
return None;
}
let language = languages + 16;
let leaf = the_u32(bytes, language + 4)?;
if leaf & 0x8000_0000 != 0 {
return None;
}
return Some(root + leaf as usize);
}
None
}
fn the_fixed_of_the_blob(blob: &[u8]) -> Option<String> {
let mut at = 0;
while at + 52 <= blob.len() {
if the_u32(blob, at) == Some(THE_MARK_OF_THE_FIXED) {
let high = the_u32(blob, at + 8)?;
let low = the_u32(blob, at + 12)?;
return Some(format!(
"{}.{}.{}.{}",
high >> 16,
high & 0xFFFF,
low >> 16,
low & 0xFFFF
));
}
at += 4;
}
None
}
fn the_text_of_the_version(blob: &[u8]) -> Option<String> {
let text = the_utf16_of(blob);
let key = "FileVersion";
let mut at = 0;
while let Some(found) = text[at..].find(key) {
let start = at + found + key.len();
let value: String = text[start..]
.chars()
.take_while(|letter| *letter != '\0')
.collect();
let value = value.trim_start_matches(' ').trim();
if !value.is_empty() {
return Some(value.to_string());
}
at = start;
}
None
}
fn the_utf16_of(blob: &[u8]) -> String {
let mut letters = Vec::new();
let mut at = 0;
while at + 1 < blob.len() {
letters.push(u16::from_le_bytes([blob[at], blob[at + 1]]));
at += 2;
}
String::from_utf16_lossy(&letters)
}
fn the_u16(bytes: &[u8], at: usize) -> Option<u16> {
let slice = bytes.get(at..at + 2)?;
Some(u16::from_le_bytes([slice[0], slice[1]]))
}
fn the_u32(bytes: &[u8], at: usize) -> Option<u32> {
let slice = bytes.get(at..at + 4)?;
Some(u32::from_le_bytes([slice[0], slice[1], slice[2], slice[3]]))
}
#[cfg(test)]
mod tests {
use super::the_version_of;
fn a_program(version: (u16, u16, u16, u16)) -> Vec<u8> {
let mut file = vec![0u8; 0x600];
file[0] = b'M';
file[1] = b'Z';
file[0x3C] = 0x80;
let header = 0x80;
file[header..header + 4].copy_from_slice(b"PE\0\0");
file[header + 6..header + 8].copy_from_slice(&1u16.to_le_bytes()); file[header + 20..header + 22].copy_from_slice(&0xE0u16.to_le_bytes()); let optional = header + 24;
file[optional..optional + 2].copy_from_slice(&0x10Bu16.to_le_bytes());
let directories = optional + 96;
file[directories + 16..directories + 20].copy_from_slice(&0x400u32.to_le_bytes());
file[directories + 20..directories + 24].copy_from_slice(&0x200u32.to_le_bytes());
let table = optional + 0xE0;
file[table..table + 8].copy_from_slice(b".rsrc\0\0\0");
file[table + 12..table + 16].copy_from_slice(&0x400u32.to_le_bytes()); file[table + 16..table + 20].copy_from_slice(&0x200u32.to_le_bytes()); file[table + 20..table + 24].copy_from_slice(&0x400u32.to_le_bytes()); let root = 0x400;
file[root + 12..root + 14].copy_from_slice(&1u16.to_le_bytes()); file[root + 16..root + 20].copy_from_slice(&16u32.to_le_bytes()); file[root + 20..root + 24].copy_from_slice(&0x8000_0020u32.to_le_bytes()); let names = root + 0x20;
file[names + 12..names + 14].copy_from_slice(&1u16.to_le_bytes());
file[names + 16..names + 20].copy_from_slice(&1u32.to_le_bytes());
file[names + 20..names + 24].copy_from_slice(&0x8000_0040u32.to_le_bytes());
let languages = root + 0x40;
file[languages + 12..languages + 14].copy_from_slice(&1u16.to_le_bytes());
file[languages + 16..languages + 20].copy_from_slice(&0x409u32.to_le_bytes());
file[languages + 20..languages + 24].copy_from_slice(&0x60u32.to_le_bytes());
let leaf = root + 0x60;
file[leaf..leaf + 4].copy_from_slice(&0x480u32.to_le_bytes());
file[leaf + 4..leaf + 8].copy_from_slice(&0x100u32.to_le_bytes());
let blob = 0x480;
let key: Vec<u16> = "VS_VERSION_INFO".encode_utf16().chain([0]).collect();
let mut at = blob;
file[at..at + 2].copy_from_slice(&0x100u16.to_le_bytes()); file[at + 2..at + 4].copy_from_slice(&52u16.to_le_bytes()); for letter in key {
file[at..at + 2].copy_from_slice(&letter.to_le_bytes());
at += 2;
}
at = (at + 3) & !3;
file[at..at + 4].copy_from_slice(&0xFEEF_04BDu32.to_le_bytes());
let high = ((version.0 as u32) << 16) | version.1 as u32;
let low = ((version.2 as u32) << 16) | version.3 as u32;
file[at + 8..at + 12].copy_from_slice(&high.to_le_bytes());
file[at + 12..at + 16].copy_from_slice(&low.to_le_bytes());
file
}
#[test]
fn the_version_of_a_program_is_the_four_numbers_of_it() {
let file = a_program((10, 0, 26100, 9818));
assert_eq!(the_version_of(&file), Some("10.0.26100.9818".to_string()));
}
#[test]
fn a_file_that_is_not_a_program_of_windows_has_no_version() {
assert_eq!(the_version_of(b"not a program at all"), None);
let mut file = a_program((1, 0, 0, 0));
file[0x80] = 0;
assert_eq!(the_version_of(&file), None);
}
#[test]
fn a_program_without_a_section_of_resources_has_no_version() {
let mut file = a_program((1, 0, 0, 0));
let directories = 0x80 + 24 + 96;
file[directories + 16..directories + 20].copy_from_slice(&0u32.to_le_bytes());
assert_eq!(the_version_of(&file), None);
}
}