3
3
*
4
4
* Use of this source code is governed by the MIT license found in the LICENSE file.
5
5
*/
6
- use std:: ops:: BitAnd ;
7
-
8
- use :: capstone:: arch:: BuildsCapstone ;
9
- use :: capstone:: { arch, Capstone , InsnGroupId , InsnGroupIdInt , InsnId , InsnIdInt , RegId , RegIdInt } ;
10
- use jni:: objects:: { JByteArray , JObject , ReleaseMode } ;
6
+ use jni:: objects:: { JByteArray , JObject } ;
11
7
use jni:: sys:: { jint, jlong, jshort, jstring} ;
12
8
use jni:: JNIEnv ;
13
9
14
10
use crate :: capstone:: context:: CapstoneContext ;
15
- use crate :: capstone:: mode:: CapstoneMode ;
16
- use crate :: capstone:: output:: CapstoneOutput ;
17
11
18
12
mod capstone;
19
13
mod obj;
20
14
mod util;
21
15
mod writer;
16
+
22
17
#[ no_mangle]
23
18
pub extern "system" fn Java_org_native4j_capstone_Capstone_init < ' local > (
24
19
mut env : JNIEnv < ' local > ,
25
20
this : JObject < ' local > ,
26
21
mode : JObject < ' local > ,
27
22
) -> jstring {
28
- let mode = CapstoneMode :: from ( & mut env, & mode) ;
29
-
30
- let capstone = match mode {
31
- Some ( CapstoneMode :: ARM32 ) => Capstone :: new ( )
32
- . arm ( )
33
- . mode ( arch:: arm:: ArchMode :: Arm )
34
- . detail ( true )
35
- . build ( ) ,
36
- Some ( CapstoneMode :: ARM64 ) => Capstone :: new ( )
37
- . arm64 ( )
38
- . mode ( arch:: arm64:: ArchMode :: Arm )
39
- . detail ( true )
40
- . build ( ) ,
41
- _ => return make_error ! ( env, "invalid argument 'mode'" ) ,
42
- } ;
43
- check_result ! ( env, capstone) ;
44
-
45
- let instance = CapstoneContext :: new ( capstone. unwrap ( ) , mode. unwrap ( ) ) ;
46
-
47
- let result = CapstoneContext :: surrender_instance ( instance, & mut env, & this) ;
23
+ let result = capstone:: init ( & mut env, this, mode) ;
48
24
check_result ! ( env, result) ;
49
-
50
25
0 as jstring /* null */
51
26
}
52
27
@@ -57,7 +32,6 @@ pub extern "system" fn Java_org_native4j_capstone_Capstone_shutdown<'local>(
57
32
) -> jstring {
58
33
let result = CapstoneContext :: drop_instance ( & mut env, & this) ;
59
34
check_result ! ( env, result) ;
60
-
61
35
0 as jstring /* null */
62
36
}
63
37
@@ -70,36 +44,8 @@ pub extern "system" fn Java_org_native4j_capstone_Capstone_disassemble<'local>(
70
44
count : jint ,
71
45
address : jlong ,
72
46
) -> jstring {
73
- let code: Vec < u8 > = {
74
- let result = unsafe { env. get_array_elements ( & data, ReleaseMode :: NoCopyBack ) } ;
75
- check_result ! ( env, result) ;
76
- result
77
- . unwrap ( )
78
- . iter ( )
79
- . map ( |b| ( * b as u8 ) . bitand ( 0xff ) )
80
- . collect ( )
81
- } ;
82
-
83
- let ctx = CapstoneContext :: get ( & mut env, & this) ;
84
- let capstone = ctx. capstone . lock ( ) . unwrap ( ) ;
85
-
86
- let instructions = {
87
- let result = if count == 0 {
88
- capstone. disasm_all ( & code, address as u64 )
89
- } else {
90
- capstone. disasm_count ( & code, address as u64 , count as usize )
91
- } ;
92
- if let Err ( e) = result {
93
- return make_error ! ( env, e. to_string( ) ) ;
94
- }
95
- result. unwrap ( )
96
- } ;
97
-
98
- let mut output = CapstoneOutput :: new ( & mut env, & ctx. mode , & capstone, & result_object) ;
99
-
100
- let result = output. copy_instructions ( instructions) ;
47
+ let result = capstone:: disassemble ( & mut env, this, result_object, data, count, address) ;
101
48
check_result ! ( env, result) ;
102
-
103
49
0 as jstring /* null */
104
50
}
105
51
@@ -109,12 +55,15 @@ pub extern "system" fn Java_org_native4j_capstone_Capstone_getInsnName<'local>(
109
55
this : JObject < ' local > ,
110
56
insn_id : jint ,
111
57
) -> jstring {
112
- let ctx = CapstoneContext :: get ( & mut env, & this) ;
113
- let capstone = ctx. capstone . lock ( ) . unwrap ( ) ;
114
- match capstone. insn_name ( InsnId ( insn_id as InsnIdInt ) ) {
115
- Some ( str) => make_jstring ! ( env, str ) ,
116
- None => 0 as jstring , /* null */
58
+ let result = capstone:: get_insn_name ( & mut env, this, insn_id) ;
59
+ if let Err ( e) = result {
60
+ capstone:: throw ( & mut env, & e. to_string ( ) ) ;
61
+ return 0 as jstring /* null */ ;
117
62
}
63
+ result
64
+ . unwrap ( )
65
+ . map ( |str| make_jstring ! ( env, str ) )
66
+ . unwrap_or ( 0 as jstring /* null */ )
118
67
}
119
68
120
69
#[ no_mangle]
@@ -123,12 +72,15 @@ pub extern "system" fn Java_org_native4j_capstone_Capstone_getRegName<'local>(
123
72
this : JObject < ' local > ,
124
73
reg_id : jint ,
125
74
) -> jstring {
126
- let ctx = CapstoneContext :: get ( & mut env, & this) ;
127
- let capstone = ctx. capstone . lock ( ) . unwrap ( ) ;
128
- match capstone. reg_name ( RegId ( reg_id as RegIdInt ) ) {
129
- Some ( str) => make_jstring ! ( env, str ) ,
130
- None => 0 as jstring , /* null */
75
+ let result = capstone:: get_reg_name ( & mut env, this, reg_id) ;
76
+ if let Err ( e) = result {
77
+ capstone:: throw ( & mut env, & e. to_string ( ) ) ;
78
+ return 0 as jstring /* null */ ;
131
79
}
80
+ result
81
+ . unwrap ( )
82
+ . map ( |str| make_jstring ! ( env, str ) )
83
+ . unwrap_or ( 0 as jstring /* null */ )
132
84
}
133
85
134
86
#[ no_mangle]
@@ -137,10 +89,13 @@ pub extern "system" fn Java_org_native4j_capstone_Capstone_getGroupName<'local>(
137
89
this : JObject < ' local > ,
138
90
group_id : jshort ,
139
91
) -> jstring {
140
- let ctx = CapstoneContext :: get ( & mut env, & this) ;
141
- let capstone = ctx. capstone . lock ( ) . unwrap ( ) ;
142
- match capstone. group_name ( InsnGroupId ( group_id as InsnGroupIdInt ) ) {
143
- Some ( str) => make_jstring ! ( env, str ) ,
144
- None => 0 as jstring , /* null */
92
+ let result = capstone:: get_group_name ( & mut env, this, group_id) ;
93
+ if let Err ( e) = result {
94
+ capstone:: throw ( & mut env, & e. to_string ( ) ) ;
95
+ return 0 as jstring /* null */ ;
145
96
}
97
+ result
98
+ . unwrap ( )
99
+ . map ( |str| make_jstring ! ( env, str ) )
100
+ . unwrap_or ( 0 as jstring /* null */ )
146
101
}
0 commit comments