Home | History | Annotate | Download | only in runtime
      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  */
     16 
     17 #ifndef ART_RUNTIME_UTF_H_
     18 #define ART_RUNTIME_UTF_H_
     19 
     20 #include "base/macros.h"
     21 
     22 #include <stddef.h>
     23 #include <stdint.h>
     24 
     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  */
     31 namespace art {
     32 namespace mirror {
     33 template<class T> class PrimitiveArray;
     34 typedef PrimitiveArray<uint16_t> CharArray;
     35 }  // namespace mirror
     36 
     37 /*
     38  * Returns the number of UTF-16 characters in the given modified UTF-8 string.
     39  */
     40 size_t CountModifiedUtf8Chars(const char* utf8);
     41 
     42 /*
     43  * Returns the number of modified UTF-8 bytes needed to represent the given
     44  * UTF-16 string.
     45  */
     46 size_t CountUtf8Bytes(const uint16_t* chars, size_t char_count);
     47 
     48 /*
     49  * Convert from Modified UTF-8 to UTF-16.
     50  */
     51 void ConvertModifiedUtf8ToUtf16(uint16_t* utf16_out, const char* utf8_in);
     52 
     53 /*
     54  * Compare two modified UTF-8 strings as UTF-16 code point values in a non-locale sensitive manner
     55  */
     56 int CompareModifiedUtf8ToModifiedUtf8AsUtf16CodePointValues(const char* utf8_1, const char* utf8_2);
     57 
     58 /*
     59  * Compare a modified UTF-8 string with a UTF-16 string as code point values in a non-locale
     60  * sensitive manner.
     61  */
     62 int CompareModifiedUtf8ToUtf16AsCodePointValues(const char* utf8_1, const uint16_t* utf8_2);
     63 
     64 /*
     65  * Convert from UTF-16 to Modified UTF-8. Note that the output is _not_
     66  * NUL-terminated. You probably need to call CountUtf8Bytes before calling
     67  * this anyway, so if you want a NUL-terminated string, you know where to
     68  * put the NUL byte.
     69  */
     70 void ConvertUtf16ToModifiedUtf8(char* utf8_out, const uint16_t* utf16_in, size_t char_count);
     71 
     72 /*
     73  * The java.lang.String hashCode() algorithm.
     74  */
     75 int32_t ComputeUtf16Hash(const mirror::CharArray* chars, int32_t offset, size_t char_count)
     76     SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
     77 int32_t ComputeUtf16Hash(const uint16_t* chars, size_t char_count);
     78 
     79 /*
     80  * Retrieve the next UTF-16 character from a UTF-8 string.
     81  *
     82  * Advances "*utf8_data_in" to the start of the next character.
     83  *
     84  * WARNING: If a string is corrupted by dropping a '\0' in the middle
     85  * of a 3-byte sequence, you can end up overrunning the buffer with
     86  * reads (and possibly with the writes if the length was computed and
     87  * cached before the damage). For performance reasons, this function
     88  * assumes that the string being parsed is known to be valid (e.g., by
     89  * already being verified). Most strings we process here are coming
     90  * out of dex files or other internal translations, so the only real
     91  * risk comes from the JNI NewStringUTF call.
     92  */
     93 uint16_t GetUtf16FromUtf8(const char** utf8_data_in);
     94 
     95 }  // namespace art
     96 
     97 #endif  // ART_RUNTIME_UTF_H_
     98