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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#[cfg(all(feature = "wedpr_f_base64", feature = "wedpr_f_hex"))]
compile_error!(
"Feature wedpr_f_base64 and wedpr_f_hex can not be enabled at same time!"
);
#[cfg(all(not(feature = "wedpr_f_base64"), not(feature = "wedpr_f_hex")))]
compile_error!("Must use feature wedpr_f_base64 or wedpr_f_hex!");
extern crate jni;
use self::jni::objects::JByteBuffer;
use jni::{
objects::{JObject, JString, JValue},
sys::jobject,
JNIEnv,
};
use libc::c_char;
use std::ffi::CStr;
use wedpr_l_utils::{error::WedprError, traits::Coder};
#[cfg(feature = "wedpr_f_base64")]
use wedpr_l_common_coder_base64::WedprBase64;
#[cfg(feature = "wedpr_f_hex")]
use wedpr_l_common_coder_hex::WedprHex;
#[cfg(feature = "wedpr_f_hex")]
lazy_static! {
pub static ref CODER: WedprHex = WedprHex::default();
}
#[cfg(feature = "wedpr_f_base64")]
lazy_static! {
pub static ref CODER: WedprBase64 = WedprBase64::default();
}
const DEFAULT_ERROR_FIELD: &str = "wedprErrorMessage";
pub fn bytes_to_string<T: ?Sized + AsRef<[u8]>>(input: &T) -> String {
CODER.encode(input)
}
pub fn string_to_bytes(input: &str) -> Result<Vec<u8>, WedprError> {
CODER.decode(input)
}
pub fn java_new_jobject<'a>(
_env: &'a JNIEnv,
java_class_name: &'a str,
) -> JObject<'a> {
let java_class = _env
.find_class(java_class_name)
.expect(&format!("Could not find Java class {}", java_class_name));
let java_object = _env.alloc_object(java_class).expect(&format!(
"Could not allocate Java object Ffor class {}",
java_class_name
));
java_object
}
pub fn java_set_error_field_and_extract_jobject(
_env: &JNIEnv,
java_object: &JObject,
error_message: &str,
) -> jobject {
let java_string;
assert!(!error_message.is_empty());
java_string = _env
.new_string(error_message)
.expect("new_string should not fail");
_env.set_field(
*java_object,
DEFAULT_ERROR_FIELD,
"Ljava/lang/String;",
JValue::from(JObject::from(java_string)),
)
.expect("set_field should not fail");
java_object.into_inner()
}
pub fn java_jstring_to_bytes(
_env: &JNIEnv,
java_string: JString,
) -> Result<Vec<u8>, WedprError> {
let rust_string = java_jstring_to_string(&_env, java_string)?;
match string_to_bytes(&rust_string) {
Ok(rust_bytes) => Ok(rust_bytes),
Err(_) => return Err(WedprError::FormatError),
}
}
pub fn java_jstring_to_string(
_env: &JNIEnv,
java_string: JString,
) -> Result<String, WedprError> {
match _env.get_string(java_string) {
Ok(java_string_data) => Ok(java_string_data.into()),
Err(_) => return Err(WedprError::FormatError),
}
}
pub fn java_jbytes_to_bytes(
_env: &JNIEnv,
java_bytes: JByteBuffer,
) -> Result<Vec<u8>, WedprError> {
match _env.get_direct_buffer_address(java_bytes) {
Ok(rust_bytes_array) => Ok(rust_bytes_array.to_vec()),
Err(_) => return Err(WedprError::FormatError),
}
}
pub const SUCCESS: i8 = 0;
pub const FAILURE: i8 = -1;
pub fn c_char_pointer_to_string(
param: *const c_char,
) -> Result<String, WedprError> {
let cstr_param = unsafe { CStr::from_ptr(param) };
match cstr_param.to_str() {
Ok(v) => Ok(v.to_owned()),
Err(_) => Err(WedprError::FormatError),
}
}