1 /*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "libcore_util_CharsetUtils.h"
18
19 #include <string.h>
20
21 #include "dex/utf-inl.h"
22 #include "handle_scope-inl.h"
23 #include "jni/jni_internal.h"
24 #include "mirror/string-inl.h"
25 #include "mirror/string.h"
26 #include "native_util.h"
27 #include "nativehelper/scoped_primitive_array.h"
28 #include "nativehelper/jni_macros.h"
29 #include "scoped_fast_native_object_access-inl.h"
30
31 namespace art {
32
CharsetUtils_asciiBytesToChars(JNIEnv * env,jclass,jbyteArray javaBytes,jint offset,jint length,jcharArray javaChars)33 static void CharsetUtils_asciiBytesToChars(JNIEnv* env, jclass, jbyteArray javaBytes, jint offset,
34 jint length, jcharArray javaChars) {
35 ScopedByteArrayRO bytes(env, javaBytes);
36 if (bytes.get() == nullptr) {
37 return;
38 }
39 ScopedCharArrayRW chars(env, javaChars);
40 if (chars.get() == nullptr) {
41 return;
42 }
43
44 const jbyte* src = &bytes[offset];
45 jchar* dst = &chars[0];
46 static const jchar REPLACEMENT_CHAR = 0xfffd;
47 for (int i = length - 1; i >= 0; --i) {
48 jchar ch = static_cast<jchar>(*src++ & 0xff);
49 *dst++ = (ch <= 0x7f) ? ch : REPLACEMENT_CHAR;
50 }
51 }
52
53 /**
54 * Translates the given characters to US-ASCII or ISO-8859-1 bytes, using the fact that
55 * Unicode code points between U+0000 and U+007f inclusive are identical to US-ASCII, while
56 * U+0000 to U+00ff inclusive are identical to ISO-8859-1.
57 */
charsToBytes(JNIEnv * env,jstring java_string,jint offset,jint length,jchar maxValidChar)58 static jbyteArray charsToBytes(JNIEnv* env, jstring java_string, jint offset, jint length,
59 jchar maxValidChar) {
60 ScopedFastNativeObjectAccess soa(env);
61 StackHandleScope<1> hs(soa.Self());
62 Handle<mirror::String> string(hs.NewHandle(soa.Decode<mirror::String>(java_string)));
63 if (string == nullptr) {
64 return nullptr;
65 }
66
67 ObjPtr<mirror::ByteArray> result = mirror::ByteArray::Alloc(soa.Self(), length);
68 if (result == nullptr) {
69 return nullptr;
70 }
71
72 if (string->IsCompressed()) {
73 // All characters in a compressed string are ASCII and therefore do not need a replacement.
74 DCHECK_GE(maxValidChar, 0x7f);
75 memcpy(result->GetData(), string->GetValueCompressed() + offset, length);
76 } else {
77 const uint16_t* src = string->GetValue() + offset;
78 auto clamp = [maxValidChar](uint16_t c) {
79 return static_cast<jbyte>(dchecked_integral_cast<uint8_t>((c > maxValidChar) ? '?' : c));
80 };
81 std::transform(src, src + length, result->GetData(), clamp);
82 }
83 return soa.AddLocalReference<jbyteArray>(result);
84 }
85
CharsetUtils_toAsciiBytes(JNIEnv * env,jclass,jstring java_string,jint offset,jint length)86 static jbyteArray CharsetUtils_toAsciiBytes(JNIEnv* env, jclass, jstring java_string, jint offset,
87 jint length) {
88 return charsToBytes(env, java_string, offset, length, 0x7f);
89 }
90
CharsetUtils_toIsoLatin1Bytes(JNIEnv * env,jclass,jstring java_string,jint offset,jint length)91 static jbyteArray CharsetUtils_toIsoLatin1Bytes(JNIEnv* env, jclass, jstring java_string,
92 jint offset, jint length) {
93 return charsToBytes(env, java_string, offset, length, 0xff);
94 }
95
CharsetUtils_toUtf8Bytes(JNIEnv * env,jclass,jstring java_string,jint offset,jint length)96 static jbyteArray CharsetUtils_toUtf8Bytes(JNIEnv* env, jclass, jstring java_string, jint offset,
97 jint length) {
98 ScopedFastNativeObjectAccess soa(env);
99 StackHandleScope<1> hs(soa.Self());
100 Handle<mirror::String> string(hs.NewHandle(soa.Decode<mirror::String>(java_string)));
101 if (string == nullptr) {
102 return nullptr;
103 }
104
105 DCHECK_GE(offset, 0);
106 DCHECK_LE(offset, string->GetLength());
107 DCHECK_GE(length, 0);
108 DCHECK_LE(length, string->GetLength() - offset);
109
110 bool compressed = string->IsCompressed();
111 size_t utf8_length = 0;
112 if (compressed) {
113 utf8_length = length;
114 } else {
115 const uint16_t* utf16 = string->GetValue() + offset;
116 auto count_length = [&utf8_length](jbyte c ATTRIBUTE_UNUSED) ALWAYS_INLINE { ++utf8_length; };
117 ConvertUtf16ToUtf8</*kUseShortZero=*/ true,
118 /*kUse4ByteSequence=*/ true,
119 /*kReplaceBadSurrogates=*/ true>(utf16, length, count_length);
120 }
121 ObjPtr<mirror::ByteArray> result =
122 mirror::ByteArray::Alloc(soa.Self(), dchecked_integral_cast<int32_t>(utf8_length));
123 if (result == nullptr) {
124 return nullptr;
125 }
126
127 if (compressed) {
128 memcpy(result->GetData(), string->GetValueCompressed() + offset, length);
129 } else {
130 const uint16_t* utf16 = string->GetValue() + offset;
131 int8_t* data = result->GetData();
132 auto store_data = [&data](jbyte c) ALWAYS_INLINE { *data++ = c; };
133 ConvertUtf16ToUtf8</*kUseShortZero=*/ true,
134 /*kUse4ByteSequence=*/ true,
135 /*kReplaceBadSurrogates=*/ true>(utf16, length, store_data);
136 }
137 return soa.AddLocalReference<jbyteArray>(result);
138 }
139
140 static JNINativeMethod gMethods[] = {
141 FAST_NATIVE_METHOD(CharsetUtils, asciiBytesToChars, "([BII[C)V"),
142 FAST_NATIVE_METHOD(CharsetUtils, toAsciiBytes, "(Ljava/lang/String;II)[B"),
143 FAST_NATIVE_METHOD(CharsetUtils, toIsoLatin1Bytes, "(Ljava/lang/String;II)[B"),
144 FAST_NATIVE_METHOD(CharsetUtils, toUtf8Bytes, "(Ljava/lang/String;II)[B"),
145 };
146
register_libcore_util_CharsetUtils(JNIEnv * env)147 void register_libcore_util_CharsetUtils(JNIEnv* env) {
148 REGISTER_NATIVE_METHODS("libcore/util/CharsetUtils");
149 }
150
151 } // namespace art
152