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 uint32_t STATE_BREAK_ZERO = 0;
31     constexpr uint32_t STATE_CONTINUE_ONE = 1;
32     constexpr uint32_t STATE_OTHER_TWO = 2;
33     constexpr uint8_t HIGER_4_BITS_MASK = 0xF0;
34     constexpr uint8_t FOUR_BYTES_STYLE = 0xF0;
35     constexpr uint8_t THREE_BYTES_STYLE = 0xE0;
36     constexpr uint8_t TWO_BYTES_STYLE1 = 0xD0;
37     constexpr uint8_t TWO_BYTES_STYLE2 = 0xC0;
38     constexpr uint32_t LOWER_10_BITS_MASK = 0x03FFU;
39     constexpr uint8_t LOWER_6_BITS_MASK = 0x3FU;
40     constexpr uint8_t LOWER_5_BITS_MASK = 0x1FU;
41     constexpr uint8_t LOWER_4_BITS_MASK = 0x0FU;
42     constexpr uint8_t LOWER_3_BITS_MASK = 0x07U;
43     constexpr uint32_t HIGH_AGENT_MASK = 0xD800U;
44     constexpr uint32_t LOW_AGENT_MASK = 0xDC00U;
45     constexpr uint32_t UTF8_VALID_BITS = 6;
46     constexpr uint32_t UTF16_SPECIAL_VALUE = 0x10000;
47 
48     struct TextEcodeInfo {
TextEcodeInfoTextEcodeInfo49         TextEcodeInfo(napi_env napiEnv, napi_value srcValue, std::string encodingStr): env(napiEnv),
50                                                                                        src(srcValue),
51                                                                                        encoding(encodingStr)
52         {}
53         napi_env env;
54         napi_value src;
55         std::string encoding;
56     };
57     struct OutBufferInfo {
OutBufferInfoOutBufferInfo58         OutBufferInfo(size_t length, std::string inputStr, size_t bufferLength, size_t cnt, std::string bufferRst)
59             : writedSize(length),
60               rstBuffer(inputStr),
61               rstBufferLength(bufferLength),
62               cntSize(cnt),
63               bufferResult(bufferRst)
64         {}
65         size_t writedSize;
66         std::string rstBuffer;
67         size_t rstBufferLength;
68         size_t cntSize;
69         std::string bufferResult;
70     };
71     struct InputBufferInfo {
InputBufferInfoInputBufferInfo72         InputBufferInfo(std::string InputEncoding, size_t charSize): encoding(InputEncoding),
73                                                                      inputSize(charSize)
74         {}
75         std::string encoding;
76         size_t inputSize;
77     };
78     UConverter* CreateConverter(const std::string& encStr_, UErrorCode& codeflag);
79     std::string ConvertToString(UChar* uchar, size_t length);
80     void EncodeIntoChinese(napi_env env, napi_value src, std::string encoding, std::string& buffer);
81     std::string UnicodeConversion(std::string encoding, char16_t* originalBuffer, size_t inputSize);
82     void EncodeToUtf8(TextEcodeInfo encodeInfo, char* writeResult, int32_t* written, size_t length, int32_t* nchars);
83     void EncodeConversion(napi_env env, napi_value src, napi_value* arrayBuffer, size_t &outLens,
84                           std::string encoding);
85     void FreedMemory(char *data);
86     int GetMaxByteSize(std::string encoding);
87     bool IsOneByte(uint8_t u8Char);
88     std::u16string Utf8ToUtf16BE(const std::string &u8Str, bool *ok = nullptr);
89     std::u16string Utf16BEToLE(const std::u16string &wstr);
90     void OtherEncode(napi_env env, napi_value src, napi_value* arrayBuffer, size_t &outLens, std::string encoding);
91     std::u16string EncodeUtf16BE(napi_env env, napi_value src);
92     void OtherEncodeUtf8(TextEcodeInfo encodeInfo, char* writeResult, int32_t* written, size_t length, int32_t* nchars);
93     void EncodeTo16BE(TextEcodeInfo encodeInfo, char* writeResult, int32_t* written, size_t length, int32_t* nchars);
94     void FreedMemory(char16_t *&data);
95     char16_t *ApplyMemory(const size_t &inputSize);
96 } // namespace Commonlibrary::Platform
97 #endif // PLATFORM_UTIL_HELPER_H