|
1 | 1 | extern crate capstone; |
2 | 2 |
|
| 3 | +use capstone::arch::mips::MipsArchTag; |
| 4 | +use capstone::arch::x86::X86ArchTag; |
| 5 | +use capstone::arch::{ArchTag, DetailsArchInsn}; |
3 | 6 | use capstone::prelude::*; |
4 | | -use capstone::InsnDetail; |
5 | 7 |
|
6 | 8 | const MIPS_CODE: &[u8] = b"\x56\x34\x21\x34\xc2\x17\x01\x00"; |
7 | 9 |
|
8 | 10 | const X86_CODE: &[u8] = b"\x55\x48\x8b\x05\xb8\x13\x00\x00\xe9\x14\x9e\x08\x00\x45\x31\xe4"; |
9 | 11 |
|
10 | 12 | #[cfg(feature = "full")] |
11 | 13 | /// Print register names |
12 | | -fn reg_names(cs: &Capstone, regs: &[RegId]) -> String { |
13 | | - let names: Vec<String> = regs.iter().map(|&x| cs.reg_name(x).unwrap()).collect(); |
| 14 | +fn reg_names<A, I>(cs: &Capstone<A>, regs: I) -> String |
| 15 | +where |
| 16 | + A: ArchTag, |
| 17 | + I: Iterator<Item = A::RegId>, |
| 18 | +{ |
| 19 | + let names: Vec<String> = regs.map(|x| cs.reg_name(x).unwrap()).collect(); |
14 | 20 | names.join(", ") |
15 | 21 | } |
16 | 22 |
|
17 | 23 | #[cfg(feature = "full")] |
18 | 24 | /// Print instruction group names |
19 | | -fn group_names(cs: &Capstone, regs: &[InsnGroupId]) -> String { |
20 | | - let names: Vec<String> = regs.iter().map(|&x| cs.group_name(x).unwrap()).collect(); |
| 25 | +fn group_names<A, I>(cs: &Capstone<A>, regs: I) -> String |
| 26 | +where |
| 27 | + A: ArchTag, |
| 28 | + I: Iterator<Item = A::InsnGroupId>, |
| 29 | +{ |
| 30 | + let names: Vec<String> = regs.map(|x| cs.group_name(x).unwrap()).collect(); |
21 | 31 | names.join(", ") |
22 | 32 | } |
23 | 33 |
|
24 | 34 | /// Disassemble code and print information |
25 | | -fn arch_example(cs: &mut Capstone, code: &[u8]) -> CsResult<()> { |
| 35 | +fn arch_example<A: ArchTag>(arch: &'static str, cs: &mut Capstone<A>, code: &[u8]) -> CsResult<()> { |
| 36 | + println!("\n*************************************"); |
| 37 | + println!("Architecture {}:", arch); |
| 38 | + |
26 | 39 | let insns = cs.disasm_all(code, 0x1000)?; |
27 | 40 | println!("Found {} instructions", insns.len()); |
28 | 41 | for i in insns.iter() { |
29 | 42 | println!(); |
30 | 43 | println!("{}", i); |
31 | 44 |
|
32 | | - let detail: InsnDetail = cs.insn_detail(i)?; |
33 | | - let arch_detail: ArchDetail = detail.arch_detail(); |
34 | | - let ops = arch_detail.operands(); |
| 45 | + let detail = cs.insn_detail(i)?; |
| 46 | + let arch_detail = detail.arch_detail(); |
| 47 | + let ops: Vec<_> = arch_detail.operands().collect(); |
35 | 48 |
|
36 | 49 | #[cfg(feature = "full")] |
37 | 50 | let output: &[(&str, String)] = &[ |
@@ -61,26 +74,19 @@ fn arch_example(cs: &mut Capstone, code: &[u8]) -> CsResult<()> { |
61 | 74 | } |
62 | 75 |
|
63 | 76 | fn example() -> CsResult<()> { |
64 | | - let cs_mips: Capstone = Capstone::new() |
65 | | - .mips() |
| 77 | + let mut cs_mips = Capstone::<MipsArchTag>::new() |
66 | 78 | .mode(arch::mips::ArchMode::Mips32R6) |
67 | 79 | .detail(true) |
68 | 80 | .build()?; |
69 | 81 |
|
70 | | - let cs_x86 = Capstone::new() |
71 | | - .x86() |
| 82 | + let mut cs_x86 = Capstone::<X86ArchTag>::new() |
72 | 83 | .mode(arch::x86::ArchMode::Mode64) |
73 | 84 | .syntax(arch::x86::ArchSyntax::Att) |
74 | 85 | .detail(true) |
75 | 86 | .build()?; |
76 | 87 |
|
77 | | - let mut examples = [("MIPS", cs_mips, MIPS_CODE), ("X86", cs_x86, X86_CODE)]; |
78 | | - |
79 | | - for &mut (arch, ref mut cs, code) in examples.iter_mut() { |
80 | | - println!("\n*************************************"); |
81 | | - println!("Architecture {}:", arch); |
82 | | - arch_example(cs, code)?; |
83 | | - } |
| 88 | + arch_example("MIPS", &mut cs_mips, MIPS_CODE)?; |
| 89 | + arch_example("X86", &mut cs_x86, X86_CODE)?; |
84 | 90 |
|
85 | 91 | Ok(()) |
86 | 92 | } |
|
0 commit comments