| Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2011 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 | */ | 
| Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 16 |  | 
| Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_UTF_H_ | 
|  | 18 | #define ART_RUNTIME_UTF_H_ | 
| Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 19 |  | 
| Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 20 | #include "base/macros.h" | 
|  | 21 |  | 
| Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 22 | #include <stddef.h> | 
|  | 23 | #include <stdint.h> | 
|  | 24 |  | 
| Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 25 | /* | 
|  | 26 | * All UTF-8 in art is actually modified UTF-8. Mostly, this distinction | 
|  | 27 | * doesn't matter. | 
|  | 28 | * | 
|  | 29 | * See http://en.wikipedia.org/wiki/UTF-8#Modified_UTF-8 for the details. | 
|  | 30 | */ | 
| Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 31 | namespace art { | 
| Ian Rogers | a672490 | 2013-09-23 09:23:37 -0700 | [diff] [blame] | 32 |  | 
| Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 33 | namespace mirror { | 
| Ian Rogers | a672490 | 2013-09-23 09:23:37 -0700 | [diff] [blame] | 34 | template<class T> class PrimitiveArray; | 
|  | 35 | typedef PrimitiveArray<uint16_t> CharArray; | 
| Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 36 | }  // namespace mirror | 
| Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 37 |  | 
| Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 38 | /* | 
|  | 39 | * Returns the number of UTF-16 characters in the given modified UTF-8 string. | 
|  | 40 | */ | 
|  | 41 | size_t CountModifiedUtf8Chars(const char* utf8); | 
|  | 42 |  | 
|  | 43 | /* | 
|  | 44 | * Returns the number of modified UTF-8 bytes needed to represent the given | 
|  | 45 | * UTF-16 string. | 
|  | 46 | */ | 
|  | 47 | size_t CountUtf8Bytes(const uint16_t* chars, size_t char_count); | 
|  | 48 |  | 
|  | 49 | /* | 
| Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 50 | * Convert from Modified UTF-8 to UTF-16. | 
| Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 51 | */ | 
| Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 52 | void ConvertModifiedUtf8ToUtf16(uint16_t* utf16_out, const char* utf8_in); | 
|  | 53 |  | 
|  | 54 | /* | 
| Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 55 | * Compare two modified UTF-8 strings as UTF-16 code point values in a non-locale sensitive manner | 
|  | 56 | */ | 
|  | 57 | int CompareModifiedUtf8ToModifiedUtf8AsUtf16CodePointValues(const char* utf8_1, const char* utf8_2); | 
|  | 58 |  | 
|  | 59 | /* | 
| Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 60 | * Compare a modified UTF-8 string with a UTF-16 string as code point values in a non-locale | 
|  | 61 | * sensitive manner. | 
|  | 62 | */ | 
|  | 63 | int CompareModifiedUtf8ToUtf16AsCodePointValues(const char* utf8_1, const uint16_t* utf8_2); | 
|  | 64 |  | 
|  | 65 | /* | 
| Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 66 | * Convert from UTF-16 to Modified UTF-8. Note that the output is _not_ | 
|  | 67 | * NUL-terminated. You probably need to call CountUtf8Bytes before calling | 
|  | 68 | * this anyway, so if you want a NUL-terminated string, you know where to | 
|  | 69 | * put the NUL byte. | 
|  | 70 | */ | 
|  | 71 | void ConvertUtf16ToModifiedUtf8(char* utf8_out, const uint16_t* utf16_in, size_t char_count); | 
| Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 72 |  | 
|  | 73 | /* | 
|  | 74 | * The java.lang.String hashCode() algorithm. | 
|  | 75 | */ | 
| Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame^] | 76 | int32_t ComputeUtf16Hash(mirror::CharArray* chars, int32_t offset, size_t char_count) | 
| Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 77 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); | 
| Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 78 | int32_t ComputeUtf16Hash(const uint16_t* chars, size_t char_count); | 
|  | 79 |  | 
|  | 80 | /* | 
|  | 81 | * Retrieve the next UTF-16 character from a UTF-8 string. | 
|  | 82 | * | 
|  | 83 | * Advances "*utf8_data_in" to the start of the next character. | 
|  | 84 | * | 
|  | 85 | * WARNING: If a string is corrupted by dropping a '\0' in the middle | 
|  | 86 | * of a 3-byte sequence, you can end up overrunning the buffer with | 
|  | 87 | * reads (and possibly with the writes if the length was computed and | 
|  | 88 | * cached before the damage). For performance reasons, this function | 
|  | 89 | * assumes that the string being parsed is known to be valid (e.g., by | 
|  | 90 | * already being verified). Most strings we process here are coming | 
|  | 91 | * out of dex files or other internal translations, so the only real | 
|  | 92 | * risk comes from the JNI NewStringUTF call. | 
|  | 93 | */ | 
|  | 94 | uint16_t GetUtf16FromUtf8(const char** utf8_data_in); | 
|  | 95 |  | 
|  | 96 | }  // namespace art | 
|  | 97 |  | 
| Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 98 | #endif  // ART_RUNTIME_UTF_H_ |