1 /*
2 * Copyright (c) 2024 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 "js_stringdecoder.h"
17 #include "util_helper.h"
18
19 namespace OHOS::Util {
20 using namespace Commonlibrary::Platform;
21 static const char* ERROR_CODE = "401";
22
StringDecoder(const std::string & encoding)23 StringDecoder::StringDecoder(const std::string &encoding)
24 {
25 UErrorCode codeflag = U_ZERO_ERROR;
26 conv_ = CreateConverter(encoding, codeflag);
27 }
28
Write(napi_env env,napi_value src,UBool flush)29 napi_value StringDecoder::Write(napi_env env, napi_value src, UBool flush)
30 {
31 napi_typedarray_type type;
32 size_t length = 0;
33 void *data = nullptr;
34 size_t byteOffset = 0;
35 napi_value arrayBuffer = nullptr;
36 NAPI_CALL(env, napi_get_typedarray_info(env, src, &type, &length, &data, &arrayBuffer, &byteOffset));
37 const char *source = static_cast<char*>(data);
38 size_t limit = static_cast<size_t>(ucnv_getMinCharSize(conv_)) * length;
39 size_t len = limit * sizeof(UChar);
40 UChar *arr = nullptr;
41 if (limit > 0) {
42 arr = new UChar[limit + 1] { 0 };
43 } else {
44 napi_throw_error(env, ERROR_CODE, "Error obtaining minimum number of input bytes");
45 return nullptr;
46 }
47 UChar *target = arr;
48 UErrorCode codeFlag = U_ZERO_ERROR;
49 ucnv_toUnicode(conv_, &target, target + len, &source, source + length, nullptr, flush, &codeFlag);
50 if (U_FAILURE(codeFlag)) {
51 FreedMemory(arr);
52 std::string err = "decoder error, ";
53 err += u_errorName(codeFlag);
54 napi_throw_error(env, ERROR_CODE, err.c_str());
55 return nullptr;
56 }
57 pendingLen_ = ucnv_toUCountPending(conv_, &codeFlag);
58 pend_ = source + length - pendingLen_;
59 std::string tepStr = ConvertToString(arr, length);
60 napi_value resultStr = nullptr;
61 NAPI_CALL(env, napi_create_string_utf8(env, tepStr.c_str(), tepStr.size(), &resultStr));
62 FreedMemory(arr);
63 return resultStr;
64 }
65
End(napi_env env,napi_value src)66 napi_value StringDecoder::End(napi_env env, napi_value src)
67 {
68 return Write(env, src, true);
69 }
70
End(napi_env env)71 napi_value StringDecoder::End(napi_env env)
72 {
73 napi_value resultStr = nullptr;
74 if (pendingLen_ == 0) {
75 NAPI_CALL(env, napi_create_string_utf8(env, "", NAPI_AUTO_LENGTH, &resultStr));
76 return resultStr;
77 }
78 UErrorCode errorCode = U_ZERO_ERROR;
79 UChar outputBuffer[pendingLen_];
80 UChar *targetEnd = outputBuffer + pendingLen_;
81 UChar *target = outputBuffer;
82 const char *src = pend_;
83 const char *sourceEnd = pend_ + pendingLen_;
84 UBool flush = true;
85 ucnv_toUnicode(conv_, &target, targetEnd, &src, sourceEnd, nullptr, flush, &errorCode);
86 if (U_FAILURE(errorCode)) {
87 std::string err = "decoder error, ";
88 err += u_errorName(errorCode);
89 napi_throw_error(env, ERROR_CODE, err.c_str());
90 return nullptr;
91 }
92 std::string tepStr = ConvertToString(target, pendingLen_);
93 NAPI_CALL(env, napi_create_string_utf8(env, tepStr.c_str(), tepStr.size(), &resultStr));
94 return resultStr;
95 }
96
FreedMemory(UChar * pData)97 void StringDecoder::FreedMemory(UChar *pData)
98 {
99 if (pData != nullptr) {
100 delete[] pData;
101 pData = nullptr;
102 }
103 }
104 }
105