Moved Java to C string conversion function to android_jni header file
This commit is contained in:
@@ -81,4 +81,23 @@ static inline bool androidjni_exception_occurred(JNIEnv *env)
|
|||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert a Java string to a C string. Memory is allocated.
|
||||||
|
*
|
||||||
|
* @param env JNIEnv
|
||||||
|
* @param jstr Java string
|
||||||
|
* @return native C string (allocated)
|
||||||
|
*/
|
||||||
|
static inline char *androidjni_convert_jstring(JNIEnv *env, jstring jstr)
|
||||||
|
{
|
||||||
|
char *str;
|
||||||
|
jsize len;
|
||||||
|
|
||||||
|
len = (*env)->GetStringUTFLength(env, jstr);
|
||||||
|
str = malloc(len + 1);
|
||||||
|
(*env)->GetStringUTFRegion(env, jstr, 0, len, str);
|
||||||
|
str[len] = '\0';
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
#endif /** ANDROID_JNI_H_ @}*/
|
#endif /** ANDROID_JNI_H_ @}*/
|
||||||
|
|||||||
@@ -438,21 +438,6 @@ JNI_METHOD(CharonVpnService, deinitializeCharon, void)
|
|||||||
library_deinit();
|
library_deinit();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Convert a Java string to a C string. Memory is allocated.
|
|
||||||
*/
|
|
||||||
static inline char *convert_jstring(JNIEnv *env, jstring jstr)
|
|
||||||
{
|
|
||||||
char *str;
|
|
||||||
jsize len;
|
|
||||||
|
|
||||||
len = (*env)->GetStringUTFLength(env, jstr);
|
|
||||||
str = malloc(len + 1);
|
|
||||||
(*env)->GetStringUTFRegion(env, jstr, 0, len, str);
|
|
||||||
str[len] = '\0';
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initiate SA
|
* Initiate SA
|
||||||
*/
|
*/
|
||||||
@@ -462,10 +447,10 @@ JNI_METHOD(CharonVpnService, initiate, void,
|
|||||||
{
|
{
|
||||||
char *local_address, *gateway, *username, *password;
|
char *local_address, *gateway, *username, *password;
|
||||||
|
|
||||||
local_address = convert_jstring(env, jlocal_address);
|
local_address = androidjni_convert_jstring(env, jlocal_address);
|
||||||
gateway = convert_jstring(env, jgateway);
|
gateway = androidjni_convert_jstring(env, jgateway);
|
||||||
username = convert_jstring(env, jusername);
|
username = androidjni_convert_jstring(env, jusername);
|
||||||
password = convert_jstring(env, jpassword);
|
password = androidjni_convert_jstring(env, jpassword);
|
||||||
|
|
||||||
initiate(local_address, gateway, username, password);
|
initiate(local_address, gateway, username, password);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user