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 "utils/log.h" 21 #include "util_helper.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, encoding_.c_str(), encoding_.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 if (!(encoding_ == "utf-8" || encoding_ == "UTF-8")) { 37 EncodeIntoChinese(env, src, encoding_, buffer); 38 } else { 39 size_t bufferSize = 0; 40 if (napi_get_value_string_utf8(env, src, nullptr, 0, &bufferSize) != napi_ok) { 41 HILOG_ERROR("can not get src size"); 42 return nullptr; 43 } 44 buffer.resize(bufferSize); 45 if (napi_get_value_string_utf8(env, src, buffer.data(), bufferSize + 1, &bufferSize) != napi_ok) { 46 HILOG_ERROR("can not get src value"); 47 return nullptr; 48 } 49 } 50 51 size_t outLen = buffer.length(); 52 void *data = nullptr; 53 napi_value arrayBuffer = nullptr; 54 napi_create_arraybuffer(env, outLen, &data, &arrayBuffer); 55 if (memcpy_s(data, outLen, reinterpret_cast<void*>(buffer.data()), outLen) != EOK) { 56 HILOG_ERROR("copy buffer to arraybuffer error"); 57 return nullptr; 58 } 59 napi_value result = nullptr; 60 napi_create_typedarray(env, napi_uint8_array, outLen, arrayBuffer, 0, &result); 61 return result; 62 } 63 EncodeInto(napi_env env,napi_value src,napi_value dest) const64 napi_value TextEncoder::EncodeInto(napi_env env, napi_value src, napi_value dest) const 65 { 66 napi_typedarray_type type; 67 size_t byteOffset = 0; 68 size_t length = 0; 69 void *resultData = nullptr; 70 napi_value resultBuffer = nullptr; 71 NAPI_CALL(env, napi_get_typedarray_info(env, dest, &type, &length, &resultData, &resultBuffer, &byteOffset)); 72 73 char *writeResult = static_cast<char*>(resultData); 74 75 int32_t nchars = 0; 76 int32_t written = 0; 77 NativeEngine *engine = reinterpret_cast<NativeEngine*>(env); 78 NativeValue *nativeValue = reinterpret_cast<NativeValue*>(src); 79 engine->EncodeToUtf8(nativeValue, writeResult, &written, length, &nchars); 80 81 napi_value result = nullptr; 82 NAPI_CALL(env, napi_create_object(env, &result)); 83 84 napi_value read = nullptr; 85 NAPI_CALL(env, napi_create_int32(env, nchars, &read)); 86 87 NAPI_CALL(env, napi_set_named_property(env, result, "read", read)); 88 89 napi_value resWritten = nullptr; 90 NAPI_CALL(env, napi_create_int32(env, written, &resWritten)); 91 92 NAPI_CALL(env, napi_set_named_property(env, result, "written", resWritten)); 93 94 return result; 95 } 96 } 97