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