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     struct TextEcodeInfo {
TextEcodeInfoCommonlibrary::Platform::TextEcodeInfo48         TextEcodeInfo(napi_env napiEnv, napi_value srcValue, std::string encodingStr): env(napiEnv),
49                                                                                        src(srcValue),
50                                                                                        encoding(encodingStr)
51         {}
52         napi_env env;
53         napi_value src;
54         std::string encoding;
55     };
56     struct OutBufferInfo {
OutBufferInfoCommonlibrary::Platform::OutBufferInfo57         OutBufferInfo(size_t length, std::string inputStr, size_t bufferLength, size_t cnt, std::string bufferRst)
58             : writedSize(length),
59               rstBuffer(inputStr),
60               rstBufferLength(bufferLength),
61               cntSize(cnt),
62               bufferResult(bufferRst)
63         {}
64         size_t writedSize;
65         std::string rstBuffer;
66         size_t rstBufferLength;
67         size_t cntSize;
68         std::string bufferResult;
69     };
70     struct InputBufferInfo {
InputBufferInfoCommonlibrary::Platform::InputBufferInfo71         InputBufferInfo(std::string InputEncoding, size_t charSize): encoding(InputEncoding),
72                                                                      inputSize(charSize)
73         {}
74         std::string encoding;
75         size_t inputSize;
76     };
77     UConverter* CreateConverter(const std::string& encStr_, UErrorCode& codeflag);
78     std::string ConvertToString(UChar* uchar, size_t length);
79     void EncodeIntoChinese(napi_env env, napi_value src, std::string encoding, std::string& buffer);
80     std::string UnicodeConversion(std::string encoding, char16_t* originalBuffer, size_t inputSize);
81     void EncodeToUtf8(TextEcodeInfo encodeInfo, char* writeResult, uint32_t* written, size_t length, int32_t* nchars);
82     void EncodeConversion(napi_env env, napi_value src, napi_value* arrayBuffer, size_t &outLens,
83                           std::string encoding);
84     void FreedMemory(char *data);
85     int GetMaxByteSize(std::string encoding);
86     bool IsOneByte(uint8_t u8Char);
87     std::u16string Utf8ToUtf16BE(const std::string &u8Str, bool *ok = nullptr);
88     std::u16string Utf16BEToLE(const std::u16string &wstr);
89     void OtherEncode(napi_env env, napi_value src, napi_value* arrayBuffer, size_t &outLens, std::string encoding);
90     std::u16string EncodeUtf16BE(napi_env env, napi_value src);
91     void OtherEncodeUtf8(TextEcodeInfo encodeInfo, char* writeResult, uint32_t* written, size_t length,
92                          int32_t* nchars);
93     void EncodeTo16BE(TextEcodeInfo encodeInfo, char* writeResult, uint32_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