Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
orecham committed Sep 10, 2024
1 parent f3145b6 commit 193eebd
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 46 deletions.
1 change: 0 additions & 1 deletion iceoryx2-cli/iox2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,3 @@ cargo_metadata = "0.18.1"
iceoryx2-bb-testing = { workspace = true }
tempfile = "3.12.0"
winapi = { version = "0.3", features = ["winnt"] }
pelite = "0.10"
52 changes: 7 additions & 45 deletions iceoryx2-cli/iox2/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,48 +279,6 @@ mod tests {
const BAR_COMMAND: &str = "m3Qf8RzN";
const BAZ_COMMAND: &str = "P5hJ2wAc";

#[cfg(windows)]
fn create_minimal_exe(path: &Path) -> Result<(), Box<dyn std::error::Error>> {
use std::io::Write;

let mut file = std::fs::File::create(path)?;

// Create a minimal PE file
let mut image = vec![0; 1024];
let mut pe = PeFile::from_bytes_mut(&mut image)?;

// Set up the DOS header
pe.dos_header_mut().e_magic = pelite::image::IMAGE_DOS_SIGNATURE;
pe.dos_header_mut().e_lfanew = 0x40;

// Set up the NT headers
let nt = pe.nt_headers_mut();
nt.Signature = pelite::image::IMAGE_NT_SIGNATURE;
nt.FileHeader.Machine = pelite::image::IMAGE_FILE_MACHINE_AMD64;
nt.FileHeader.NumberOfSections = 1;
nt.FileHeader.SizeOfOptionalHeader = 0xF0;
nt.FileHeader.Characteristics = FileCharacteristics::EXECUTABLE_IMAGE.bits();

// Set up the Optional header
let opt = &mut nt.OptionalHeader;
opt.Magic = pelite::image::IMAGE_NT_OPTIONAL_HDR64_MAGIC;
opt.SizeOfCode = 0x200;
opt.AddressOfEntryPoint = 0x1000;
opt.BaseOfCode = 0x1000;
opt.ImageBase = 0x140000000;
opt.SectionAlignment = 0x1000;
opt.FileAlignment = 0x200;
opt.MajorSubsystemVersion = 6;
opt.SizeOfImage = 0x2000;
opt.SizeOfHeaders = 0x200;
opt.Subsystem = pelite::image::IMAGE_SUBSYSTEM_WINDOWS_CUI;

// Write the PE file
file.write_all(&image)?;

Ok(())
}

macro_rules! create_file {
($path:expr, $file:expr) => {{
let file_path = $path.join($file);
Expand All @@ -336,9 +294,13 @@ mod tests {
}
#[cfg(windows)]
{
if file_path.extension().and_then(|s| s.to_str()) == Some("exe") {
create_minimal_exe(&file_path).expect("Failed to create minimal executable");
}
use std::os::windows::fs::OpenOptionsExt;
std::fs::OpenOptions::new()
.write(true)
.create(true)
.attributes(winapi::um::winnt::FILE_ATTRIBUTE_NORMAL)
.open(&file_path)
.expect("Failed to create file");
}
}};
}
Expand Down

0 comments on commit 193eebd

Please sign in to comment.