• 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 }  // namespace art
98 
99 #endif  // ART_LIBDEXFILE_DEX_UTF_INL_H_
100