|
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 { |
| 14 | +fn reg_names<A: ArchTag>(cs: &Capstone<A>, regs: &[A::RegId]) -> String { |
13 | 15 | let names: Vec<String> = regs.iter().map(|&x| cs.reg_name(x).unwrap()).collect(); |
14 | 16 | names.join(", ") |
15 | 17 | } |
16 | 18 |
|
17 | 19 | #[cfg(feature = "full")] |
18 | 20 | /// Print instruction group names |
19 | | -fn group_names(cs: &Capstone, regs: &[InsnGroupId]) -> String { |
| 21 | +fn group_names<A: ArchTag>(cs: &Capstone<A>, regs: &[A::InsnGroupId]) -> String { |
20 | 22 | let names: Vec<String> = regs.iter().map(|&x| cs.group_name(x).unwrap()).collect(); |
21 | 23 | names.join(", ") |
22 | 24 | } |
23 | 25 |
|
24 | 26 | /// Disassemble code and print information |
25 | | -fn arch_example(cs: &mut Capstone, code: &[u8]) -> CsResult<()> { |
| 27 | +fn arch_example<A: ArchTag>(arch: &'static str, cs: &mut Capstone<A>, code: &[u8]) -> CsResult<()> { |
| 28 | + println!("\n*************************************"); |
| 29 | + println!("Architecture {}:", arch); |
| 30 | + |
26 | 31 | let insns = cs.disasm_all(code, 0x1000)?; |
27 | 32 | println!("Found {} instructions", insns.len()); |
28 | 33 | for i in insns.iter() { |
29 | 34 | println!(); |
30 | 35 | println!("{}", i); |
31 | 36 |
|
32 | | - let detail: InsnDetail = cs.insn_detail(i)?; |
33 | | - let arch_detail: ArchDetail = detail.arch_detail(); |
34 | | - let ops = arch_detail.operands(); |
| 37 | + let detail = cs.insn_detail(i)?; |
| 38 | + let arch_detail = detail.arch_detail(); |
| 39 | + let ops: Vec<_> = arch_detail.operands().collect(); |
35 | 40 |
|
36 | 41 | #[cfg(feature = "full")] |
37 | 42 | let output: &[(&str, String)] = &[ |
@@ -61,26 +66,19 @@ fn arch_example(cs: &mut Capstone, code: &[u8]) -> CsResult<()> { |
61 | 66 | } |
62 | 67 |
|
63 | 68 | fn example() -> CsResult<()> { |
64 | | - let cs_mips: Capstone = Capstone::new() |
65 | | - .mips() |
| 69 | + let mut cs_mips = Capstone::<MipsArchTag>::new() |
66 | 70 | .mode(arch::mips::ArchMode::Mips32R6) |
67 | 71 | .detail(true) |
68 | 72 | .build()?; |
69 | 73 |
|
70 | | - let cs_x86 = Capstone::new() |
71 | | - .x86() |
| 74 | + let mut cs_x86 = Capstone::<X86ArchTag>::new() |
72 | 75 | .mode(arch::x86::ArchMode::Mode64) |
73 | 76 | .syntax(arch::x86::ArchSyntax::Att) |
74 | 77 | .detail(true) |
75 | 78 | .build()?; |
76 | 79 |
|
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 | | - } |
| 80 | + arch_example("MIPS", &mut cs_mips, MIPS_CODE)?; |
| 81 | + arch_example("X86", &mut cs_x86, X86_CODE)?; |
84 | 82 |
|
85 | 83 | Ok(()) |
86 | 84 | } |
|
0 commit comments