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
32 static constexpr uint8_t BIT_MASK_1 = 0x80;
33 static constexpr uint8_t BIT_MASK_2 = 0xC0;
34 static constexpr uint8_t BIT_MASK_3 = 0xE0;
35 static constexpr uint8_t BIT_MASK_4 = 0xF0;
36 static constexpr uint8_t BIT_MASK_5 = 0xF8;
37
38 static constexpr uint8_t UTF8_1B_MAX = 0x7f;
39
40 static constexpr uint16_t UTF8_2B_MAX = 0x7ff;
41 static constexpr uint8_t UTF8_2B_FIRST = 0xc0;
42 static constexpr uint8_t UTF8_2B_SECOND = 0x80;
43
44 static constexpr uint8_t UTF8_3B_FIRST = 0xe0;
45 static constexpr uint8_t UTF8_3B_SECOND = 0x80;
46 static constexpr uint8_t UTF8_3B_THIRD = 0x80;
47
48 static constexpr uint8_t UTF8_4B_FIRST = 0xf0;
49
50 enum UtfLength : uint8_t { ONE = 1, TWO = 2, THREE = 3, FOUR = 4 };
51 enum UtfOffset : uint8_t { SIX = 6, TEN = 10, TWELVE = 12, EIGHTEEN = 18 };
52
53 static constexpr size_t MAX_BYTES = 4;
54 struct Utf8Char {
55 size_t n;
56 std::array<uint8_t, MAX_BYTES> ch;
57 };
58
59 uint32_t UTF16Decode(uint16_t lead, uint16_t trail);
60
61 bool IsValidUTF8(const std::vector<uint8_t> &data);
62
63 Utf8Char ConvertUtf16ToUtf8(uint16_t d0, uint16_t d1, bool modify);
64
65 size_t Utf16ToUtf8Size(const uint16_t *utf16, uint32_t length, bool modify = true);
66
67 size_t ConvertRegionUtf16ToUtf8(const uint16_t *utf16In, uint8_t *utf8Out, size_t utf16Len, size_t utf8Len,
68 size_t start, bool modify = true);
69
70 std::pair<uint32_t, size_t> ConvertUtf8ToUtf16Pair(const uint8_t *data, bool combine = false);
71
72 size_t Utf8ToUtf16Size(const uint8_t *utf8, size_t utf8Len);
73
74 size_t ConvertRegionUtf8ToUtf16(const uint8_t *utf8In, uint16_t *utf16Out, size_t utf8Len, size_t utf16Len,
75 size_t start);
76
CombineTwoU16(uint16_t d0,uint16_t d1)77 static inline uint32_t CombineTwoU16(uint16_t d0, uint16_t d1)
78 {
79 uint32_t codePoint = d0 - utf::HI_SURROGATE_MIN;
80 codePoint <<= UtfOffset::TEN;
81 codePoint |= d1 - utf::LO_SURROGATE_MIN;
82 codePoint += utf::LO_SUPPLEMENTS_MIN;
83 return codePoint;
84 }
85 } // namespace panda::ecmascript::base::utf_helper
86
87 #endif // ECMASCRIPT_BASE_UTF_HELPER_H