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#include "util_helper.h"
17
18#include "util_plugin.h"
19#include <cstdio>
20#include <unicode/ustring.h>
21#include "native_engine.h"
22#include "securec.h"
23
24namespace Commonlibrary::Platform {
25using namespace OHOS::Util;
26bool isChineseEncoding = false;
27std::string targetEncStr;
28
29UConverter* CreateConverter(const std::string& encStr_, UErrorCode& codeflag)
30{
31    const std::string convertFormat("gbk,GBK,GB2312,gb2312,GB18030,gb18030");
32    targetEncStr = encStr_;
33    std::string encodeStr = "";
34    if (convertFormat.find(targetEncStr.c_str()) != convertFormat.npos) {
35        isChineseEncoding = true;
36        encodeStr = "ISO-8859-1";
37    } else {
38        isChineseEncoding = false;
39        encodeStr = targetEncStr;
40    }
41    UConverter *conv = ucnv_open(encodeStr.c_str(), &codeflag);
42    if (U_FAILURE(codeflag)) {
43        HILOG_ERROR("Unable to create a UConverter object");
44        return nullptr;
45    }
46
47    ucnv_setFromUCallBack(conv, UCNV_FROM_U_CALLBACK_SUBSTITUTE, NULL, NULL, NULL, &codeflag);
48    if (U_FAILURE(codeflag)) {
49        HILOG_ERROR("Unable to set the from Unicode callback function");
50        ucnv_close(conv);
51        return nullptr;
52    }
53
54    ucnv_setToUCallBack(conv, UCNV_TO_U_CALLBACK_SUBSTITUTE, NULL, NULL, NULL, &codeflag);
55    if (U_FAILURE(codeflag)) {
56        HILOG_ERROR("Unable to set the to Unicode callback function");
57        ucnv_close(conv);
58        return nullptr;
59    }
60
61    return conv;
62}
63
64std::string ConvertToString(UChar * uchar, size_t length)
65{
66    std::string tepStr;
67    if (isChineseEncoding) {
68        std::string input = "";
69        for (size_t i = 0; i < length; ++i) {
70            input += static_cast<char>(uchar[i] & 0xFF);
71        }
72        tepStr = UtilPlugin::Decode(input, targetEncStr);
73    } else {
74        std::u16string tempStr16(uchar);
75        tepStr = std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> {}.to_bytes(tempStr16);
76    }
77    return tepStr;
78}
79
80void EncodeIntoChinese(napi_env env, napi_value src, std::string encoding, std::string& buffer)
81{
82    std::string input = "";
83    size_t inputSize = 0;
84    napi_get_value_string_utf8(env, src, nullptr, 0, &inputSize); // 0:buffer size
85    input.resize(inputSize);
86    napi_get_value_string_utf8(env, src, input.data(), inputSize + 1, &inputSize);
87    buffer = UtilPlugin::EncodeIntoChinese(input, encoding);
88}
89
90void EncodeConversion(napi_env env, napi_value src, napi_value* arrayBuffer, size_t &outLens, std::string encoding)
91{
92    std::string buffer = "";
93    EncodeIntoChinese(env, src, encoding, buffer);
94    size_t outLen = buffer.length();
95    outLens = outLen;
96    void *data = nullptr;
97    napi_create_arraybuffer(env, outLen, &data, arrayBuffer);
98    if (memcpy_s(data, outLen, reinterpret_cast<void*>(buffer.data()), outLen) != EOK) {
99        HILOG_FATAL("textencoder::copy buffer to arraybuffer error");
100        return;
101    }
102}
103
104void EncodeToUtf8(TextEcodeInfo encodeInfo, char* writeResult, uint32_t* written, size_t length, int32_t* nchars)
105{
106    NativeEngine *engine = reinterpret_cast<NativeEngine*>(encodeInfo.env);
107    engine->EncodeToUtf8(encodeInfo.src, writeResult, written, length, nchars);
108}
109} // namespace Commonlibrary::Platform