• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 PLATFORM_UTIL_HELPER_H
17 #define PLATFORM_UTIL_HELPER_H
18 
19 #include <codecvt>
20 #include <locale>
21 #include <string>
22 
23 #include "unicode/unistr.h"
24 #include "unicode/ucnv.h"
25 #include "napi/native_api.h"
26 #include "napi/native_node_api.h"
27 #include "tools/log.h"
28 
29 namespace Commonlibrary::Platform {
30     constexpr uint8_t HIGER_4_BITS_MASK = 0xF0;
31     constexpr uint8_t FOUR_BYTES_STYLE = 0xF0;
32     constexpr uint8_t THREE_BYTES_STYLE = 0xE0;
33     constexpr uint8_t TWO_BYTES_STYLE1 = 0xD0;
34     constexpr uint8_t TWO_BYTES_STYLE2 = 0xC0;
35     constexpr uint32_t LOWER_10_BITS_MASK = 0x03FFU;
36     constexpr uint8_t LOWER_6_BITS_MASK = 0x3FU;
37     constexpr uint8_t LOWER_5_BITS_MASK = 0x1FU;
38     constexpr uint8_t LOWER_4_BITS_MASK = 0x0FU;
39     constexpr uint8_t LOWER_3_BITS_MASK = 0x07U;
40     constexpr uint32_t HIGH_AGENT_MASK = 0xD800U;
41     constexpr uint32_t LOW_AGENT_MASK = 0xDC00U;
42     constexpr uint32_t UTF8_VALID_BITS = 6;
43     constexpr uint32_t UTF16_SPECIAL_VALUE = 0x10000;
44 
45     struct TextEcodeInfo {
TextEcodeInfoTextEcodeInfo46         TextEcodeInfo(napi_env napiEnv, napi_value srcValue, std::string encodingStr): env(napiEnv),
47                                                                                        src(srcValue),
48                                                                                        encoding(encodingStr)
49         {}
50         napi_env env;
51         napi_value src;
52         std::string encoding;
53     };
54     UConverter* CreateConverter(const std::string& encStr_, UErrorCode& codeflag);
55     std::string ConvertToString(UChar* uchar, size_t length);
56     void EncodeIntoChinese(napi_env env, napi_value src, std::string encoding, std::string& buffer);
57     std::string UnicodeConversion(std::string encoding, char16_t* originalBuffer, size_t inputSize);
58     void EncodeToUtf8(TextEcodeInfo encodeInfo, char* writeResult, int32_t* written, size_t length, int32_t* nchars);
59     void EncodeConversion(napi_env env, napi_value src, napi_value* arrayBuffer, size_t &outLens,
60                           std::string encoding);
61     void FreedMemory(char *data);
62     int GetMaxByteSize(std::string encoding);
63     bool IsOneByte(uint8_t u8Char);
64     std::u16string Utf8ToUtf16BE(const std::string &u8Str, bool *ok = nullptr);
65     std::u16string Utf16BEToLE(const std::u16string &wstr);
66     void OtherEncode(napi_env env, napi_value src, napi_value* arrayBuffer, size_t &outLens, std::string encoding);
67     std::u16string EncodeUtf16BE(napi_env env, napi_value src);
68     void OtherEncodeUtf8(TextEcodeInfo encodeInfo, char* writeResult, int32_t* written, size_t length, int32_t* nchars);
69     void EncodeTo16BE(TextEcodeInfo encodeInfo, char* writeResult, int32_t* written, size_t length, int32_t* nchars);
70 } // namespace Commonlibrary::Platform
71 #endif // PLATFORM_UTIL_HELPER_H