1 /* 2 * Copyright (c) 2022 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_textencoder.h" 17 18 #include "native_engine.h" 19 #include "securec.h" 20 #include "util_helper.h" 21 #include "tools/log.h" 22 23 namespace OHOS::Util { 24 using namespace Commonlibrary::Platform; GetEncoding(napi_env env) const25 napi_value TextEncoder::GetEncoding(napi_env env) const 26 { 27 napi_value result = nullptr; 28 NAPI_CALL(env, napi_create_string_utf8(env, orgEncoding_.c_str(), orgEncoding_.length(), &result)); 29 30 return result; 31 } 32 Encode(napi_env env,napi_value src) const33 napi_value TextEncoder::Encode(napi_env env, napi_value src) const 34 { 35 std::string buffer = ""; 36 size_t outLen = 0; 37 size_t outLens = 0; 38 void *data = nullptr; 39 napi_value arrayBuffer = nullptr; 40 if (encoding_ == "utf-8") { 41 size_t bufferSize = 0; 42 if (napi_get_value_string_utf8(env, src, nullptr, 0, &bufferSize) != napi_ok) { 43 HILOG_ERROR("textencoder::can not get src size"); 44 return nullptr; 45 } 46 buffer.resize(bufferSize); 47 if (napi_get_value_string_utf8(env, src, buffer.data(), bufferSize + 1, &bufferSize) != napi_ok) { 48 HILOG_ERROR("textencoder::can not get src value"); 49 return nullptr; 50 } 51 outLen = buffer.length(); 52 outLens = outLen; 53 napi_create_arraybuffer(env, outLen, &data, &arrayBuffer); 54 if (memcpy_s(data, outLen, reinterpret_cast<void*>(buffer.data()), outLen) != EOK) { 55 HILOG_FATAL("textencoder::copy buffer to arraybuffer error"); 56 return nullptr; 57 } 58 } else { 59 EncodeConversion(env, src, &arrayBuffer, outLens, encoding_); 60 } 61 62 napi_value result = nullptr; 63 napi_create_typedarray(env, napi_uint8_array, outLens, arrayBuffer, 0, &result); 64 return result; 65 } 66 EncodeInto(napi_env env,napi_value src,napi_value dest) const67 napi_value TextEncoder::EncodeInto(napi_env env, napi_value src, napi_value dest) const 68 { 69 napi_typedarray_type type; 70 size_t byteOffset = 0; 71 size_t length = 0; 72 void *resultData = nullptr; 73 napi_value resultBuffer = nullptr; 74 NAPI_CALL(env, napi_get_typedarray_info(env, dest, &type, &length, &resultData, &resultBuffer, &byteOffset)); 75 76 char *writeResult = static_cast<char*>(resultData); 77 78 int32_t nchars = 0; 79 int32_t written = 0; 80 TextEcodeInfo encodeInfo(env, src, encoding_); 81 EncodeToUtf8(encodeInfo, writeResult, &written, length, &nchars); 82 83 napi_value result = nullptr; 84 NAPI_CALL(env, napi_create_object(env, &result)); 85 86 napi_value read = nullptr; 87 NAPI_CALL(env, napi_create_int32(env, nchars, &read)); 88 89 NAPI_CALL(env, napi_set_named_property(env, result, "read", read)); 90 91 napi_value resWritten = nullptr; 92 NAPI_CALL(env, napi_create_int32(env, written, &resWritten)); 93 94 NAPI_CALL(env, napi_set_named_property(env, result, "written", resWritten)); 95 96 return result; 97 } 98 } 99