• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef ECMASCRIPT_BASE_UTF_HELPER_H
17 #define ECMASCRIPT_BASE_UTF_HELPER_H
18 
19 #include <cstdint>
20 #include <vector>
21 
22 #include "libpandabase/utils/utf.h"
23 
24 namespace panda::ecmascript::base::utf_helper {
25 static constexpr uint16_t DECODE_LEAD_LOW = 0xD800;
26 static constexpr uint16_t DECODE_LEAD_HIGH = 0xDBFF;
27 static constexpr uint16_t DECODE_TRAIL_LOW = 0xDC00;
28 static constexpr uint16_t DECODE_TRAIL_HIGH = 0xDFFF;
29 static constexpr uint32_t DECODE_FIRST_FACTOR = 0x400;
30 static constexpr uint32_t DECODE_SECOND_FACTOR = 0x10000;
31 static constexpr uint32_t UTF8_OFFSET = 6;
32 static constexpr uint32_t UTF16_OFFSET = 10;
33 static constexpr uint16_t SURROGATE_MASK = 0xF800;
34 static constexpr uint16_t UTF16_REPLACEMENT_CHARACTER = 0xFFFD;
35 
36 static constexpr uint8_t BIT_MASK_1 = 0x80;
37 static constexpr uint8_t BIT_MASK_2 = 0xC0;
38 static constexpr uint8_t BIT_MASK_3 = 0xE0;
39 static constexpr uint8_t BIT_MASK_4 = 0xF0;
40 static constexpr uint8_t BIT_MASK_5 = 0xF8;
41 
42 static constexpr uint8_t UTF8_1B_MAX = 0x7f;
43 
44 static constexpr uint16_t UTF8_2B_MAX = 0x7ff;
45 static constexpr uint8_t UTF8_2B_FIRST = 0xc0;
46 static constexpr uint8_t UTF8_2B_SECOND = 0x80;
47 static constexpr uint8_t UTF8_2B_THIRD = 0x3f;
48 static constexpr uint8_t UTF8_2B_FIRST_MIN = 0xc2;  // the minimum for 2 bytes is 128, which is 0xc280
49 
50 static constexpr uint16_t UTF8_3B_MAX = 0xffff;
51 static constexpr uint8_t UTF8_3B_FIRST = 0xe0;
52 static constexpr uint8_t UTF8_3B_SECOND = 0x80;
53 static constexpr uint8_t UTF8_3B_THIRD = 0x80;
54 static constexpr uint8_t UTF8_3B_SECOND_MIN = 0xa0;  // the minimum for 3 bytes is 2048, which is 0xe0a080
55 
56 static constexpr uint8_t UTF8_4B_FIRST = 0xf0;
57 static constexpr uint8_t UTF8_4B_SECOND_MIN = 0x90;  // the minimum for 4 bytes is 65536, which is 0xf0908080
58 
59 static constexpr uint8_t byteMask = 0xbf;
60 static constexpr uint8_t byteMark = 0x80;
61 
62 static constexpr uint8_t latin1Limit = 0xFF;
63 
64 static constexpr int32_t INVALID_UTF8 = -1;
65 
66 enum UtfLength : uint8_t { ONE = 1, TWO = 2, THREE = 3, FOUR = 4 };
67 enum UtfOffset : uint8_t { SIX = 6, TEN = 10, TWELVE = 12, EIGHTEEN = 18 };
68 
69 static constexpr size_t MAX_BYTES = 4;
70 struct Utf8Char {
71     size_t n;
72     std::array<uint8_t, MAX_BYTES> ch;
73 };
74 
75 static const unsigned char firstByteMark[7] = {0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC};
76 
77 uint32_t DecodeUTF16(uint16_t const *utf16, size_t len, size_t *index);
78 
79 size_t EncodeUTF8(uint32_t codepoint, uint8_t* utf8, size_t len, size_t index);
80 
81 uint32_t UTF16Decode(uint16_t lead, uint16_t trail);
82 
83 bool IsValidUTF8(const std::vector<uint8_t> &data);
84 
85 Utf8Char ConvertUtf16ToUtf8(uint16_t d0, uint16_t d1, bool modify, bool isWriteBuffer = false);
86 
87 size_t Utf16ToUtf8Size(const uint16_t *utf16, uint32_t length, bool modify = true);
88 
89 size_t ConvertRegionUtf16ToUtf8(const uint16_t *utf16In, uint8_t *utf8Out, size_t utf16Len, size_t utf8Len,
90                                 size_t start, bool modify = true, bool isWriteBuffer = false);
91 
92 size_t DebuggerConvertRegionUtf16ToUtf8(const uint16_t *utf16In, uint8_t *utf8Out, size_t utf16Len, size_t utf8Len,
93                                         size_t start, bool modify = true, bool isWriteBuffer = false);
94 
95 uint32_t HandleAndDecodeInvalidUTF16(uint16_t const *utf16, size_t len, size_t *index);
96 
97 std::pair<uint32_t, size_t> ConvertUtf8ToUtf16Pair(const uint8_t *data, bool combine = false);
98 
99 size_t Utf8ToUtf16Size(const uint8_t *utf8, size_t utf8Len);
100 
101 size_t ConvertRegionUtf8ToUtf16(const uint8_t *utf8In, uint16_t *utf16Out, size_t utf8Len, size_t utf16Len,
102                                 size_t start);
103 
104 size_t ConvertRegionUtf16ToLatin1(const uint16_t *utf16In, uint8_t *latin1Out, size_t utf16Len, size_t latin1Len);
105 
CombineTwoU16(uint16_t d0,uint16_t d1)106 static inline uint32_t CombineTwoU16(uint16_t d0, uint16_t d1)
107 {
108     uint32_t codePoint = d0 - utf::HI_SURROGATE_MIN;
109     codePoint <<= UtfOffset::TEN;
110     codePoint |= d1 - utf::LO_SURROGATE_MIN;
111     codePoint += utf::LO_SUPPLEMENTS_MIN;
112     return codePoint;
113 }
114 
115 std::pair<int32_t, size_t> ConvertUtf8ToUnicodeChar(const uint8_t *utf8, size_t maxLen);
116 }  // namespace panda::ecmascript::base::utf_helper
117 
118 #endif  // ECMASCRIPT_BASE_UTF_HELPER_H