| Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 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 | |
| Andreas Gampe | a14100c | 2017-04-24 15:09:56 -0700 | [diff] [blame] | 17 | #include "libcore_util_CharsetUtils.h" |
| 18 | |
| 19 | #include <string.h> |
| 20 | |
| Andreas Gampe | e15b9b1 | 2018-10-29 12:54:27 -0700 | [diff] [blame] | 21 | #include "handle_scope-inl.h" |
| Vladimir Marko | a3ad0cd | 2018-05-04 10:06:38 +0100 | [diff] [blame] | 22 | #include "jni/jni_internal.h" |
| Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 23 | #include "mirror/string-inl.h" |
| Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 24 | #include "mirror/string.h" |
| Andreas Gampe | 87583b3 | 2017-05-25 11:22:18 -0700 | [diff] [blame] | 25 | #include "native_util.h" |
| Andreas Gampe | 373a9b5 | 2017-10-18 09:01:57 -0700 | [diff] [blame] | 26 | #include "nativehelper/scoped_primitive_array.h" |
| Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 27 | #include "nativehelper/jni_macros.h" |
| Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 28 | #include "scoped_fast_native_object_access-inl.h" |
| Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 29 | #include "unicode/utf16.h" |
| 30 | |
| Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 31 | namespace art { |
| 32 | |
| 33 | /** |
| 34 | * Approximates java.lang.UnsafeByteSequence so we don't have to pay the cost of calling back into |
| 35 | * Java when converting a char[] to a UTF-8 byte[]. This lets us have UTF-8 conversions slightly |
| 36 | * faster than ICU for large char[]s without paying for the NIO overhead with small char[]s. |
| 37 | * |
| 38 | * We could avoid this by keeping the UTF-8 bytes on the native heap until we're done and only |
| 39 | * creating a byte[] on the Java heap when we know how big it needs to be, but one shouldn't lie |
| 40 | * to the garbage collector (nor hide potentially large allocations from it). |
| 41 | * |
| 42 | * Because a call to append might require an allocation, it might fail. Callers should always |
| 43 | * check the return value of append. |
| 44 | */ |
| 45 | class NativeUnsafeByteSequence { |
| 46 | public: |
| 47 | explicit NativeUnsafeByteSequence(JNIEnv* env) |
| 48 | : mEnv(env), mJavaArray(nullptr), mRawArray(nullptr), mSize(-1), mOffset(0) { |
| 49 | } |
| 50 | |
| 51 | ~NativeUnsafeByteSequence() { |
| 52 | // Release our pointer to the raw array, copying changes back to the Java heap. |
| 53 | if (mRawArray != nullptr) { |
| 54 | mEnv->ReleaseByteArrayElements(mJavaArray, mRawArray, 0); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | bool append(jbyte b) { |
| 59 | if (mOffset == mSize && !resize(mSize * 2)) { |
| 60 | return false; |
| 61 | } |
| 62 | mRawArray[mOffset++] = b; |
| 63 | return true; |
| 64 | } |
| 65 | |
| 66 | bool resize(int newSize) { |
| 67 | if (newSize == mSize) { |
| 68 | return true; |
| 69 | } |
| 70 | |
| 71 | // Allocate a new array. |
| 72 | jbyteArray newJavaArray = mEnv->NewByteArray(newSize); |
| 73 | if (newJavaArray == nullptr) { |
| 74 | return false; |
| 75 | } |
| 76 | jbyte* newRawArray = mEnv->GetByteArrayElements(newJavaArray, nullptr); |
| 77 | if (newRawArray == nullptr) { |
| 78 | return false; |
| 79 | } |
| 80 | |
| 81 | // Copy data out of the old array and then let go of it. |
| 82 | // Note that we may be trimming the array. |
| 83 | if (mRawArray != nullptr) { |
| 84 | memcpy(newRawArray, mRawArray, mOffset); |
| 85 | mEnv->ReleaseByteArrayElements(mJavaArray, mRawArray, JNI_ABORT); |
| 86 | mEnv->DeleteLocalRef(mJavaArray); |
| 87 | } |
| 88 | |
| 89 | // Point ourselves at the new array. |
| 90 | mJavaArray = newJavaArray; |
| 91 | mRawArray = newRawArray; |
| 92 | mSize = newSize; |
| 93 | return true; |
| 94 | } |
| 95 | |
| 96 | jbyteArray toByteArray() { |
| 97 | // Trim any unused space, if necessary. |
| 98 | bool okay = resize(mOffset); |
| 99 | return okay ? mJavaArray : nullptr; |
| 100 | } |
| 101 | |
| 102 | private: |
| 103 | JNIEnv* mEnv; |
| 104 | jbyteArray mJavaArray; |
| 105 | jbyte* mRawArray; |
| 106 | jint mSize; |
| 107 | jint mOffset; |
| 108 | |
| 109 | // Disallow copy and assignment. |
| 110 | NativeUnsafeByteSequence(const NativeUnsafeByteSequence&); |
| 111 | void operator=(const NativeUnsafeByteSequence&); |
| 112 | }; |
| 113 | |
| 114 | static void CharsetUtils_asciiBytesToChars(JNIEnv* env, jclass, jbyteArray javaBytes, jint offset, |
| 115 | jint length, jcharArray javaChars) { |
| 116 | ScopedByteArrayRO bytes(env, javaBytes); |
| 117 | if (bytes.get() == nullptr) { |
| 118 | return; |
| 119 | } |
| 120 | ScopedCharArrayRW chars(env, javaChars); |
| 121 | if (chars.get() == nullptr) { |
| 122 | return; |
| 123 | } |
| 124 | |
| 125 | const jbyte* src = &bytes[offset]; |
| 126 | jchar* dst = &chars[0]; |
| 127 | static const jchar REPLACEMENT_CHAR = 0xfffd; |
| 128 | for (int i = length - 1; i >= 0; --i) { |
| 129 | jchar ch = static_cast<jchar>(*src++ & 0xff); |
| 130 | *dst++ = (ch <= 0x7f) ? ch : REPLACEMENT_CHAR; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | static void CharsetUtils_isoLatin1BytesToChars(JNIEnv* env, jclass, jbyteArray javaBytes, |
| 135 | jint offset, jint length, jcharArray javaChars) { |
| 136 | ScopedByteArrayRO bytes(env, javaBytes); |
| 137 | if (bytes.get() == nullptr) { |
| 138 | return; |
| 139 | } |
| 140 | ScopedCharArrayRW chars(env, javaChars); |
| 141 | if (chars.get() == nullptr) { |
| 142 | return; |
| 143 | } |
| 144 | |
| 145 | const jbyte* src = &bytes[offset]; |
| 146 | jchar* dst = &chars[0]; |
| 147 | for (int i = length - 1; i >= 0; --i) { |
| 148 | *dst++ = static_cast<jchar>(*src++ & 0xff); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Translates the given characters to US-ASCII or ISO-8859-1 bytes, using the fact that |
| 154 | * Unicode code points between U+0000 and U+007f inclusive are identical to US-ASCII, while |
| 155 | * U+0000 to U+00ff inclusive are identical to ISO-8859-1. |
| 156 | */ |
| 157 | static jbyteArray charsToBytes(JNIEnv* env, jstring java_string, jint offset, jint length, |
| 158 | jchar maxValidChar) { |
| 159 | ScopedObjectAccess soa(env); |
| 160 | StackHandleScope<1> hs(soa.Self()); |
| Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 161 | Handle<mirror::String> string(hs.NewHandle(soa.Decode<mirror::String>(java_string))); |
| Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 162 | if (string == nullptr) { |
| Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 163 | return nullptr; |
| 164 | } |
| 165 | |
| 166 | jbyteArray javaBytes = env->NewByteArray(length); |
| 167 | ScopedByteArrayRW bytes(env, javaBytes); |
| 168 | if (bytes.get() == nullptr) { |
| 169 | return nullptr; |
| 170 | } |
| 171 | |
| Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 172 | jbyte* dst = &bytes[0]; |
| jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 173 | for (int i = 0; i < length; ++i) { |
| 174 | jchar ch = string->CharAt(offset + i); |
| Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 175 | if (ch > maxValidChar) { |
| 176 | ch = '?'; |
| 177 | } |
| 178 | *dst++ = static_cast<jbyte>(ch); |
| 179 | } |
| 180 | |
| 181 | return javaBytes; |
| 182 | } |
| 183 | |
| 184 | static jbyteArray CharsetUtils_toAsciiBytes(JNIEnv* env, jclass, jstring java_string, jint offset, |
| 185 | jint length) { |
| 186 | return charsToBytes(env, java_string, offset, length, 0x7f); |
| 187 | } |
| 188 | |
| 189 | static jbyteArray CharsetUtils_toIsoLatin1Bytes(JNIEnv* env, jclass, jstring java_string, |
| 190 | jint offset, jint length) { |
| 191 | return charsToBytes(env, java_string, offset, length, 0xff); |
| 192 | } |
| 193 | |
| 194 | static jbyteArray CharsetUtils_toUtf8Bytes(JNIEnv* env, jclass, jstring java_string, jint offset, |
| 195 | jint length) { |
| 196 | ScopedObjectAccess soa(env); |
| 197 | StackHandleScope<1> hs(soa.Self()); |
| Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 198 | Handle<mirror::String> string(hs.NewHandle(soa.Decode<mirror::String>(java_string))); |
| Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 199 | if (string == nullptr) { |
| Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 200 | return nullptr; |
| 201 | } |
| 202 | |
| 203 | NativeUnsafeByteSequence out(env); |
| 204 | if (!out.resize(length)) { |
| 205 | return nullptr; |
| 206 | } |
| 207 | |
| 208 | const int end = offset + length; |
| 209 | for (int i = offset; i < end; ++i) { |
| 210 | jint ch = string->CharAt(i); |
| 211 | if (ch < 0x80) { |
| 212 | // One byte. |
| 213 | if (!out.append(ch)) { |
| 214 | return nullptr; |
| 215 | } |
| 216 | } else if (ch < 0x800) { |
| 217 | // Two bytes. |
| 218 | if (!out.append((ch >> 6) | 0xc0) || !out.append((ch & 0x3f) | 0x80)) { |
| 219 | return nullptr; |
| 220 | } |
| 221 | } else if (U16_IS_SURROGATE(ch)) { |
| 222 | // A supplementary character. |
| 223 | jchar high = static_cast<jchar>(ch); |
| 224 | jchar low = (i + 1 != end) ? string->CharAt(i + 1) : 0; |
| 225 | if (!U16_IS_SURROGATE_LEAD(high) || !U16_IS_SURROGATE_TRAIL(low)) { |
| 226 | if (!out.append('?')) { |
| 227 | return nullptr; |
| 228 | } |
| 229 | continue; |
| 230 | } |
| 231 | // Now we know we have a *valid* surrogate pair, we can consume the low surrogate. |
| 232 | ++i; |
| 233 | ch = U16_GET_SUPPLEMENTARY(high, low); |
| 234 | // Four bytes. |
| 235 | jbyte b1 = (ch >> 18) | 0xf0; |
| 236 | jbyte b2 = ((ch >> 12) & 0x3f) | 0x80; |
| 237 | jbyte b3 = ((ch >> 6) & 0x3f) | 0x80; |
| 238 | jbyte b4 = (ch & 0x3f) | 0x80; |
| 239 | if (!out.append(b1) || !out.append(b2) || !out.append(b3) || !out.append(b4)) { |
| 240 | return nullptr; |
| 241 | } |
| 242 | } else { |
| 243 | // Three bytes. |
| 244 | jbyte b1 = (ch >> 12) | 0xe0; |
| 245 | jbyte b2 = ((ch >> 6) & 0x3f) | 0x80; |
| 246 | jbyte b3 = (ch & 0x3f) | 0x80; |
| 247 | if (!out.append(b1) || !out.append(b2) || !out.append(b3)) { |
| 248 | return nullptr; |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | return out.toByteArray(); |
| 253 | } |
| 254 | |
| 255 | static JNINativeMethod gMethods[] = { |
| Igor Murashkin | 3b6f440 | 2017-02-16 16:13:17 -0800 | [diff] [blame] | 256 | FAST_NATIVE_METHOD(CharsetUtils, asciiBytesToChars, "([BII[C)V"), |
| 257 | FAST_NATIVE_METHOD(CharsetUtils, isoLatin1BytesToChars, "([BII[C)V"), |
| 258 | FAST_NATIVE_METHOD(CharsetUtils, toAsciiBytes, "(Ljava/lang/String;II)[B"), |
| 259 | FAST_NATIVE_METHOD(CharsetUtils, toIsoLatin1Bytes, "(Ljava/lang/String;II)[B"), |
| 260 | FAST_NATIVE_METHOD(CharsetUtils, toUtf8Bytes, "(Ljava/lang/String;II)[B"), |
| Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 261 | }; |
| 262 | |
| 263 | void register_libcore_util_CharsetUtils(JNIEnv* env) { |
| 264 | REGISTER_NATIVE_METHODS("libcore/util/CharsetUtils"); |
| 265 | } |
| 266 | |
| 267 | } // namespace art |