diff --git a/frameworks/kits/js/common/image_napi_utils.cpp b/frameworks/kits/js/common/image_napi_utils.cpp index 6b351b4e494add2635cb59488043a17fadb9ed4c..f1cb8f9bf15f992db0a744f65af0ac4e3f7513ca 100644 --- a/frameworks/kits/js/common/image_napi_utils.cpp +++ b/frameworks/kits/js/common/image_napi_utils.cpp @@ -73,14 +73,16 @@ bool ImageNapiUtils::GetNodeByName(napi_env env, napi_value root, const char* na return true; } -bool ImageNapiUtils::GetUtf8String(napi_env env, napi_value root, std::string &res) +bool ImageNapiUtils::GetUtf8String(napi_env env, napi_value root, std::string &res, bool eof) { size_t bufferSize = NUM0; IMG_NAPI_CHECK_RET(IMG_IS_OK(napi_get_value_string_utf8(env, root, nullptr, NUM0, &bufferSize)) && bufferSize > NUM0, false); size_t resultSize = NUM0; - bufferSize = bufferSize + NUM1; + if (eof) { + bufferSize = bufferSize + NUM1; + } std::vector buffer(bufferSize); IMG_NAPI_CHECK_RET(IMG_IS_OK(napi_get_value_string_utf8(env, root, &(buffer[NUM0]), bufferSize, &resultSize)) && resultSize > NUM0, false); diff --git a/frameworks/kits/js/common/image_packer_napi.cpp b/frameworks/kits/js/common/image_packer_napi.cpp index feb9b13365d74cf09e1aa3ddef1fdcfa54a2d6fd..dd19cc66c29fd2a1bd82660c9a8ce8fd102e34cc 100644 --- a/frameworks/kits/js/common/image_packer_napi.cpp +++ b/frameworks/kits/js/common/image_packer_napi.cpp @@ -41,6 +41,7 @@ const int ARGS_THREE = 3; const int PARAM0 = 0; const int PARAM1 = 1; const int PARAM2 = 2; +const uint8_t BYTE_FULL = 0xFF; const int32_t SIZE = 100; const int32_t TYPE_IMAGE_SOURCE = 1; const int32_t TYPE_PIXEL_MAP = 2; @@ -171,7 +172,7 @@ STATIC_COMPLETE_FUNC(Packing) STATIC_EXEC_FUNC(PackingFromPixelMap) { HiLog::Debug(LABEL, "PackingFromPixelMapExec enter"); - uint64_t bufferSize = 2 * 1024 * 1024; + uint64_t bufferSize = 10 * 1024 * 1024; int64_t packedSize = 0; auto context = static_cast(data); HiLog::Debug(LABEL, "image packer get supported format"); @@ -304,41 +305,59 @@ void ImagePackerNapi::Destructor(napi_env env, void *nativeObject, void *finaliz static bool parsePackOptions(napi_env env, napi_value root, PackOption* opts) { - char buffer[SIZE]; - napi_value property = nullptr; - bool present = false; - size_t res = 0; - uint32_t len = 0; - napi_value stringItem = nullptr; - int32_t quality = 0; - HiLog::Debug(LABEL, "GetPackingOptionsParam IN"); - - napi_has_named_property(env, root, "format", &present); - if (present && napi_get_named_property(env, root, "format", &property) == napi_ok) { - HiLog::Debug(LABEL, "GetPackingOptionsParam IN 1"); - napi_get_array_length(env, property, &len); + napi_value tmpValue = nullptr; + uint32_t tmpNumber = 0; + + HiLog::Debug(LABEL, "parsePackOptions IN"); + if (!GET_NODE_BY_NAME(root, "format", tmpValue)) { + HiLog::Error(LABEL, "No format in pack option"); + return false; + } + + bool isFormatArray = false; + napi_is_array(env, tmpValue, &isFormatArray); + auto formatType = ImageNapiUtils::getType(env, tmpValue); + + HiLog::Debug(LABEL, "parsePackOptions format type %{public}d, is array %{public}d", + formatType, isFormatArray); + + if (napi_string == formatType) { + if (!ImageNapiUtils::GetUtf8String(env, tmpValue, opts->format, false)) { + HiLog::Error(LABEL, "Parse pack option format failed"); + return false; + } + } else if (isFormatArray) { + uint32_t len = 0; + if (napi_get_array_length(env, tmpValue, &len) != napi_ok) { + HiLog::Error(LABEL, "TONY Parse pack napi_get_array_length failed"); + return false; + } + HiLog::Debug(LABEL, "TONY Parse pack array_length=%{public}d", len); for (size_t i = 0; i < len; i++) { - napi_get_element(env, property, i, &stringItem); - napi_get_value_string_utf8(env, stringItem, buffer, SIZE, &res); - opts->format = std::string(buffer); - HiLog::Debug(LABEL, "format is %{public}s.", opts->format.c_str()); - if (memset_s(buffer, SIZE, 0, sizeof(buffer)) != EOK) { - HiLog::Error(LABEL, "memset buffer failed."); - free(buffer); + napi_value item; + napi_get_element(env, tmpValue, i, &item); + if (!ImageNapiUtils::GetUtf8String(env, item, opts->format, false)) { + HiLog::Error(LABEL, "Parse format in item failed %{public}zu", i); } + HiLog::Debug(LABEL, "format is %{public}s.", opts->format.c_str()); } + } else { + HiLog::Error(LABEL, "Invalid pack option format type"); + return false; } - present = false; - napi_has_named_property(env, root, "quality", &present); - if (present) { - if (napi_get_named_property(env, root, "quality", &property) != napi_ok) { - HiLog::Error(LABEL, "Could not get the quality argument!"); - } else { - napi_get_value_int32(env, property, &quality); - opts->quality = quality; - HiLog::Debug(LABEL, "quality is %{public}d.", quality); - } + HiLog::Debug(LABEL, "parsePackOptions format:[%{public}s]", opts->format.c_str()); + + if (!GET_UINT32_BY_NAME(root, "quality", tmpNumber)) { + HiLog::Error(LABEL, "No quality in pack option"); + return false; + } + if (tmpNumber > SIZE) { + HiLog::Error(LABEL, "Invalid quality"); + opts->quality = BYTE_FULL; + } else { + opts->quality = static_cast(tmpNumber & 0xff); } + HiLog::Debug(LABEL, "parsePackOptions OUT"); return true; } diff --git a/frameworks/kits/js/common/include/image_napi_utils.h b/frameworks/kits/js/common/include/image_napi_utils.h index 03e59c691f70411e6e0de9c33c2b3ec70beb4ffd..72a9675271c0ea3ef90a46bf724900257f541cdf 100644 --- a/frameworks/kits/js/common/include/image_napi_utils.h +++ b/frameworks/kits/js/common/include/image_napi_utils.h @@ -111,7 +111,7 @@ public: static bool GetInt32ByName(napi_env env, napi_value root, const char* name, int32_t *res); static bool GetBoolByName(napi_env env, napi_value root, const char* name, bool *res); static bool GetNodeByName(napi_env env, napi_value root, const char* name, napi_value *res); - static bool GetUtf8String(napi_env env, napi_value root, std::string &res); + static bool GetUtf8String(napi_env env, napi_value root, std::string &res, bool eof = true); static napi_valuetype getType(napi_env env, napi_value root); static bool CreateArrayBuffer(napi_env env, void* src, size_t srcLen, napi_value *res); static void HicheckerReport();