33 *
44 * Use of this source code is governed by the MIT license found in the LICENSE file.
55 */
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 } ;
117use jni:: sys:: { jint, jlong, jshort, jstring} ;
128use jni:: JNIEnv ;
139
1410use crate :: capstone:: context:: CapstoneContext ;
15- use crate :: capstone:: mode:: CapstoneMode ;
16- use crate :: capstone:: output:: CapstoneOutput ;
1711
1812mod capstone;
1913mod obj;
2014mod util;
2115mod writer;
16+
2217#[ no_mangle]
2318pub extern "system" fn Java_org_native4j_capstone_Capstone_init < ' local > (
2419 mut env : JNIEnv < ' local > ,
2520 this : JObject < ' local > ,
2621 mode : JObject < ' local > ,
2722) -> 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) ;
4824 check_result ! ( env, result) ;
49-
5025 0 as jstring /* null */
5126}
5227
@@ -57,7 +32,6 @@ pub extern "system" fn Java_org_native4j_capstone_Capstone_shutdown<'local>(
5732) -> jstring {
5833 let result = CapstoneContext :: drop_instance ( & mut env, & this) ;
5934 check_result ! ( env, result) ;
60-
6135 0 as jstring /* null */
6236}
6337
@@ -70,36 +44,8 @@ pub extern "system" fn Java_org_native4j_capstone_Capstone_disassemble<'local>(
7044 count : jint ,
7145 address : jlong ,
7246) -> 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) ;
10148 check_result ! ( env, result) ;
102-
10349 0 as jstring /* null */
10450}
10551
@@ -109,12 +55,15 @@ pub extern "system" fn Java_org_native4j_capstone_Capstone_getInsnName<'local>(
10955 this : JObject < ' local > ,
11056 insn_id : jint ,
11157) -> 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 */ ;
11762 }
63+ result
64+ . unwrap ( )
65+ . map ( |str| make_jstring ! ( env, str ) )
66+ . unwrap_or ( 0 as jstring /* null */ )
11867}
11968
12069#[ no_mangle]
@@ -123,12 +72,15 @@ pub extern "system" fn Java_org_native4j_capstone_Capstone_getRegName<'local>(
12372 this : JObject < ' local > ,
12473 reg_id : jint ,
12574) -> 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 */ ;
13179 }
80+ result
81+ . unwrap ( )
82+ . map ( |str| make_jstring ! ( env, str ) )
83+ . unwrap_or ( 0 as jstring /* null */ )
13284}
13385
13486#[ no_mangle]
@@ -137,10 +89,13 @@ pub extern "system" fn Java_org_native4j_capstone_Capstone_getGroupName<'local>(
13789 this : JObject < ' local > ,
13890 group_id : jshort ,
13991) -> 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 */ ;
14596 }
97+ result
98+ . unwrap ( )
99+ . map ( |str| make_jstring ! ( env, str ) )
100+ . unwrap_or ( 0 as jstring /* null */ )
146101}
0 commit comments