• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_LIBDEXFILE_DEX_UTF_INL_H_
18 #define ART_LIBDEXFILE_DEX_UTF_INL_H_
19 
20 #include "utf.h"
21 
22 namespace art {
23 
GetTrailingUtf16Char(uint32_t maybe_pair)24 inline uint16_t GetTrailingUtf16Char(uint32_t maybe_pair) {
25   return static_cast<uint16_t>(maybe_pair >> 16);
26 }
27 
GetLeadingUtf16Char(uint32_t maybe_pair)28 inline uint16_t GetLeadingUtf16Char(uint32_t maybe_pair) {
29   return static_cast<uint16_t>(maybe_pair & 0x0000FFFF);
30 }
31 
GetUtf16FromUtf8(const char ** utf8_data_in)32 inline uint32_t GetUtf16FromUtf8(const char** utf8_data_in) {
33   const uint8_t one = *(*utf8_data_in)++;
34   if ((one & 0x80) == 0) {
35     // one-byte encoding
36     return one;
37   }
38 
39   const uint8_t two = *(*utf8_data_in)++;
40   if ((one & 0x20) == 0) {
41     // two-byte encoding
42     return ((one & 0x1f) << 6) | (two & 0x3f);
43   }
44 
45   const uint8_t three = *(*utf8_data_in)++;
46   if ((one & 0x10) == 0) {
47     return ((one & 0x0f) << 12) | ((two & 0x3f) << 6) | (three & 0x3f);
48   }
49 
50   // Four byte encodings need special handling. We'll have
51   // to convert them into a surrogate pair.
52   const uint8_t four = *(*utf8_data_in)++;
53 
54   // Since this is a 4 byte UTF-8 sequence, it will lie between
55   // U+10000 and U+1FFFFF.
56   //
57   // TODO: What do we do about values in (U+10FFFF, U+1FFFFF) ? The
58   // spec says they're invalid but nobody appears to check for them.
59   const uint32_t code_point = ((one & 0x0f) << 18) | ((two & 0x3f) << 12)
60       | ((three & 0x3f) << 6) | (four & 0x3f);
61 
62   uint32_t surrogate_pair = 0;
63   // Step two: Write out the high (leading) surrogate to the bottom 16 bits
64   // of the of the 32 bit type.
65   surrogate_pair |= ((code_point >> 10) + 0xd7c0) & 0xffff;
66   // Step three : Write out the low (trailing) surrogate to the top 16 bits.
67   surrogate_pair |= ((code_point & 0x03ff) + 0xdc00) << 16;
68 
69   return surrogate_pair;
70 }
71 
CompareModifiedUtf8ToModifiedUtf8AsUtf16CodePointValues(const char * utf8_1,const char * utf8_2)72 inline int CompareModifiedUtf8ToModifiedUtf8AsUtf16CodePointValues(const char* utf8_1,
73                                                                    const char* utf8_2) {
74   uint32_t c1, c2;
75   do {
76     c1 = *utf8_1;
77     c2 = *utf8_2;
78     // Did we reach a terminating character?
79     if (c1 == 0) {
80       return (c2 == 0) ? 0 : -1;
81     } else if (c2 == 0) {
82       return 1;
83     }
84 
85     c1 = GetUtf16FromUtf8(&utf8_1);
86     c2 = GetUtf16FromUtf8(&utf8_2);
87   } while (c1 == c2);
88 
89   const uint32_t leading_surrogate_diff = GetLeadingUtf16Char(c1) - GetLeadingUtf16Char(c2);
90   if (leading_surrogate_diff != 0) {
91       return static_cast<int>(leading_surrogate_diff);
92   }
93 
94   return GetTrailingUtf16Char(c1) - GetTrailingUtf16Char(c2);
95 }
96 
97 template <bool kUseShortZero, bool kUse4ByteSequence, bool kReplaceBadSurrogates, typename Append>
ConvertUtf16ToUtf8(const uint16_t * utf16,size_t char_count,Append && append)98 inline void ConvertUtf16ToUtf8(const uint16_t* utf16, size_t char_count, Append&& append) {
99   static_assert(kUse4ByteSequence || !kReplaceBadSurrogates);
100 
101   // Use local helpers instead of macros from `libicu` to avoid the dependency on `libicu`.
102   auto is_lead = [](uint16_t ch) ALWAYS_INLINE { return (ch & 0xfc00u) == 0xd800u; };
103   auto is_trail = [](uint16_t ch) ALWAYS_INLINE { return (ch & 0xfc00u) == 0xdc00u; };
104   auto is_surrogate = [](uint16_t ch) ALWAYS_INLINE { return (ch & 0xf800u) == 0xd800u; };
105   auto is_surrogate_lead = [](uint16_t ch) ALWAYS_INLINE { return (ch & 0x0400u) == 0u; };
106   auto get_supplementary = [](uint16_t lead, uint16_t trail) ALWAYS_INLINE {
107     constexpr uint32_t offset = (0xd800u << 10) + 0xdc00u - 0x10000u;
108     return (static_cast<uint32_t>(lead) << 10) + static_cast<uint32_t>(trail) - offset;
109   };
110 
111   for (size_t i = 0u; i < char_count; ++i) {
112     auto has_trail = [&]() { return i + 1u != char_count && is_trail(utf16[i + 1u]); };
113 
114     uint16_t ch = utf16[i];
115     if (ch < 0x80u && (kUseShortZero || ch != 0u)) {
116       // One byte.
117       append(ch);
118     } else if (ch < 0x800u) {
119       // Two bytes.
120       append((ch >> 6) | 0xc0);
121       append((ch & 0x3f) | 0x80);
122     } else if (kReplaceBadSurrogates
123                    ? is_surrogate(ch)
124                    : kUse4ByteSequence && is_lead(ch) && has_trail()) {
125       if (kReplaceBadSurrogates && (!is_surrogate_lead(ch) || !has_trail())) {
126         append('?');
127       } else {
128         // We have a *valid* surrogate pair.
129         uint32_t code_point = get_supplementary(ch, utf16[i + 1u]);
130         ++i;  //  Consume the leading surrogate.
131         // Four bytes.
132         append((code_point >> 18) | 0xf0);
133         append(((code_point >> 12) & 0x3f) | 0x80);
134         append(((code_point >> 6) & 0x3f) | 0x80);
135         append((code_point & 0x3f) | 0x80);
136       }
137     } else {
138       // Three bytes.
139       append((ch >> 12) | 0xe0);
140       append(((ch >> 6) & 0x3f) | 0x80);
141       append((ch & 0x3f) | 0x80);
142     }
143   }
144 }
145 
146 }  // namespace art
147 
148 #endif  // ART_LIBDEXFILE_DEX_UTF_INL_H_
149