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