1 /*
2 * Copyright (c) 2024 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 COMMON_UTILS_H
17 #define COMMON_UTILS_H
18
19 #include <codecvt>
20 #include <locale>
21 #include <memory.h>
22 #include <securec.h>
23 #include <string.h>
24
25 namespace OHOS {
26 namespace Rosen {
27 namespace Drawing {
28 const uint16_t LOW_BYTE_MASK = 0x00FF;
29 const uint16_t HIGH_BYTE_MASK = 0xFF00;
30 const int BYTE_SHIFT = 8;
31
IsUtf8(const char* text, uint32_t len)32 [[maybe_unused]] static bool IsUtf8(const char* text, uint32_t len)
33 {
34 uint32_t n = 0;
35 for (uint32_t i = 0; i < len; i++) {
36 uint32_t c = text[i];
37 if (c <= 0x7F) { // 0x00 and 0x7F is the range of utf-8
38 n = 0;
39 } else if ((c & 0xE0) == 0xC0) { // 0xE0 and 0xC0 is the range of utf-8
40 n = 1;
41 } else if (c == 0xED && i < (len - 1) && (text[i + 1] & 0xA0) == 0xA0) { // 0xA0 and 0xED is the range of utf-8
42 return false;
43 } else if ((c & 0xF0) == 0xE0) { // 0xE0 and 0xF0 is the range of utf-8
44 n = 2; // 2 means the size of range
45 } else if ((c & 0xF8) == 0xF0) { // 0xF0 and 0xF8 is the range of utf-8
46 n = 3; // 3 means the size of range
47 } else {
48 return false;
49 }
50 for (uint32_t j = 0; j < n && i < len; j++) {
51 // 0x80 and 0xC0 is the range of utf-8
52 i++;
53 if ((i == len) || ((text[i] & 0xC0) != 0x80)) {
54 return false;
55 }
56 }
57 }
58 return true;
59 }
60
IsBigEndian()61 [[maybe_unused]] static inline bool IsBigEndian()
62 {
63 int num = 1;
64 return (*(reinterpret_cast<uint8_t*>(&num)) == 0);
65 }
66
ConvertToString(const uint8_t* data, size_t len, std::string& fullNameString)67 [[maybe_unused]] static bool ConvertToString(const uint8_t* data, size_t len, std::string& fullNameString)
68 {
69 if (data == nullptr || len == 0) {
70 return false;
71 }
72
73 size_t utf16Len = len / sizeof(char16_t);
74 std::unique_ptr<char16_t[]> utf16Str = std::make_unique<char16_t[]>(utf16Len);
75
76 if (IsBigEndian()) {
77 errno_t ret = memcpy_s(utf16Str.get(), utf16Len * sizeof(char16_t), data, len);
78 if (ret != EOK) {
79 return false;
80 }
81 } else {
82 for (size_t i = 0; i < utf16Len; ++i) {
83 const char16_t* utf16Data = reinterpret_cast<const char16_t*>(data);
84 uint16_t temp = static_cast<uint16_t>(utf16Data[i]);
85 utf16Str[i] = static_cast<char16_t>((temp & LOW_BYTE_MASK) << BYTE_SHIFT |
86 (temp & HIGH_BYTE_MASK) >> BYTE_SHIFT);
87 }
88 }
89
90 std::u16string utf16String(utf16Str.get(), utf16Len);
91 std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> converter;
92 fullNameString = converter.to_bytes(utf16String);
93
94 return true;
95 }
96 }
97 }
98 }
99 #endif