Skip to content

Commit

Permalink
Merge branch 'master' into 1.3
Browse files Browse the repository at this point in the history
  • Loading branch information
spacelan committed Dec 20, 2019
2 parents 217fb5f + 9ea1197 commit ee05b0a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 31 deletions.
4 changes: 1 addition & 3 deletions java/com_baidu_openrasp_v8_V8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,7 @@ ALIGN_FUNCTION JNIEXPORT jbyteArray JNICALL Java_com_baidu_openrasp_v8_V8_Check(
std::string type;

{
const char* tmp = env->GetStringUTFChars(jtype, nullptr);
type = std::string(tmp);
env->ReleaseStringUTFChars(jtype, tmp);
type = Jstring2String(env, jtype);
if (!v8::String::NewFromUtf8(isolate, type.data(), v8::NewStringType::kInternalized, type.size())
.ToLocal(&request_type)) {
return nullptr;
Expand Down
2 changes: 0 additions & 2 deletions java/header.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ class V8Class {
V8Class(JNIEnv* env) {
auto ref = env->FindClass("com/baidu/openrasp/v8/V8");
cls = (jclass)env->NewGlobalRef(ref);
env->DeleteLocalRef(ref);
Log = env->GetStaticMethodID(cls, "Log", "(Ljava/lang/String;)V");
GetStack = env->GetStaticMethodID(cls, "GetStack", "()[B");
GetFreeMemory = env->GetStaticMethodID(cls, "GetFreeMemory", "()J");
Expand All @@ -67,7 +66,6 @@ class ContextClass {
ContextClass(JNIEnv* env) {
auto ref = env->FindClass("com/baidu/openrasp/v8/Context");
cls = (jclass)env->NewGlobalRef(ref);
env->DeleteLocalRef(ref);
getString = env->GetMethodID(cls, "getString", "(Ljava/lang/String;)Ljava/lang/String;");
getObject = env->GetMethodID(cls, "getObject", "(Ljava/lang/String;)[B");
getBuffer = env->GetMethodID(cls, "getBuffer", "(Ljava/lang/String;)[B");
Expand Down
44 changes: 18 additions & 26 deletions java/utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,27 +79,24 @@ std::string Jstring2String(JNIEnv* env, jstring str) {
if (size > 4 * 1024 * 1024) {
size = 4 * 1024 * 1024;
}
auto data = env->GetStringChars(str, nullptr);
if (data == nullptr) {
return {};
}
// 在windows上用u16string会报错,只能这样拷贝转换
std::wstring u16(size, 0);
for (int i = 0; i < size; i++) {
u16[i] = data[i];
}
env->ReleaseStringChars(str, data);
return std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t>{}.to_bytes(u16);
std::vector<char16_t> u16(size);
env->GetStringRegion(str, 0, size, reinterpret_cast<jchar*>(u16.data()));
#ifdef _WIN32
return std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t>{}.to_bytes(
std::wstring(u16.begin(), u16.end()));
#else
return std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>{}.to_bytes(u16.data(), u16.data() + size);
#endif
}

jstring String2Jstring(JNIEnv* env, const std::string& str) {
std::wstring u16 = std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t>{}.from_bytes(str);
auto size = u16.size();
std::vector<jchar> data(size, 0);
for (int i = 0; i < size; i++) {
data[i] = u16[i];
}
return env->NewString(data.data(), size);
#ifdef _WIN32
std::wstring ws = std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t>{}.from_bytes(str);
std::u16string u16(ws.begin(), ws.end());
#else
std::u16string u16 = std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>{}.from_bytes(str);
#endif
return env->NewString(reinterpret_cast<const jchar*>(u16.data()), u16.size());
}

v8::MaybeLocal<v8::String> Jstring2V8string(JNIEnv* env, jstring jstr) {
Expand All @@ -110,14 +107,9 @@ v8::MaybeLocal<v8::String> Jstring2V8string(JNIEnv* env, jstring jstr) {
if (size > 4 * 1024 * 1024) {
size = 4 * 1024 * 1024;
}
auto data = env->GetStringChars(jstr, nullptr);
if (data == nullptr) {
return {};
}
auto rst = v8::String::NewFromTwoByte(v8::Isolate::GetCurrent(), static_cast<const uint16_t*>(data),
v8::NewStringType::kNormal, size);
env->ReleaseStringChars(jstr, data);
return rst;
std::vector<jchar> u16(size);
env->GetStringRegion(jstr, 0, size, u16.data());
return v8::String::NewFromTwoByte(v8::Isolate::GetCurrent(), u16.data(), v8::NewStringType::kNormal, size);
}

jstring V8value2Jstring(JNIEnv* env, v8::Local<v8::Value> val) {
Expand Down

0 comments on commit ee05b0a

Please sign in to comment.