blob: 72cf7a5107919da7af60190095b922dd564cec42 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
use goblin::mach::{self, Mach};
use std::env;
use std::process;
use std::path::Path;
use std::fs::File;
use std::io::{Read, Write};
fn usage() -> ! {
println!("usage: lipo <options> <mach-o fat file>");
println!(" -m64 Extracts and writes the 64-bit binary in this fat container, if any");
process::exit(1);
}
fn main () {
let len = env::args().len();
if len <= 1 {
usage();
} else {
let mut m64 = false;
{
let mut flags = env::args().collect::<Vec<_>>();
flags.pop();
flags.remove(0);
for option in flags {
match option.as_str() {
"-m64" => { m64 = true }
other => {
println!("unknown flag: {}", other);
println!();
usage();
}
}
}
}
let path_name = env::args_os().last().unwrap();
let path = Path::new(&path_name);
let buffer = { let mut v = Vec::new(); let mut f = File::open(&path).unwrap(); f.read_to_end(&mut v).unwrap(); v};
match mach::Mach::parse(&buffer) {
Ok(Mach::Binary(_macho)) => {
println!("Already a single arch binary");
process::exit(2);
},
Ok(Mach::Fat(fat)) => {
for (i, arch) in fat.iter_arches().enumerate() {
let arch = arch.unwrap();
let name = format!("{}.{}", &path_name.to_string_lossy(), i);
let path = Path::new(&name);
if arch.is_64() && m64 {
let bytes = &buffer[arch.offset as usize..][..arch.size as usize];
let mut file = File::create(path).unwrap();
file.write_all(bytes).unwrap();
break;
} else if !m64 {
let bytes = &buffer[arch.offset as usize..][..arch.size as usize];
let mut file = File::create(path).unwrap();
file.write_all(bytes).unwrap();
}
}
},
Err(err) => {
println!("err: {:?}", err);
process::exit(2);
}
}
}
}
|