From 5ebc479695a27a4ebd47588f48550e1e06ab46d2 Mon Sep 17 00:00:00 2001 From: Clone_Zhang Date: Wed, 21 Dec 2022 12:29:32 +0800 Subject: [PATCH] add verification for parsing json Signed-off-by: Clone_Zhang --- .../dcamera_device/dmetadata_processor.h | 7 ++ .../dstream_operator/dstream_operator.h | 4 + .../dcamera_device/dmetadata_processor.cpp | 73 ++++++++--- .../src/dstream_operator/dstream_operator.cpp | 114 +++++++++++++----- 4 files changed, 150 insertions(+), 48 deletions(-) diff --git a/distributed_camera/hdi_service/include/dcamera_device/dmetadata_processor.h b/distributed_camera/hdi_service/include/dcamera_device/dmetadata_processor.h index 35655c6f3a..c5831f1b86 100644 --- a/distributed_camera/hdi_service/include/dcamera_device/dmetadata_processor.h +++ b/distributed_camera/hdi_service/include/dcamera_device/dmetadata_processor.h @@ -65,6 +65,12 @@ private: uint32_t GetDataSize(uint32_t type); void* GetMetadataItemData(const camera_metadata_item_t &item); std::map> GetDCameraSupportedFormats(const std::string &abilityInfo); + std::map> ParsePhotoFormats(Json::Value rootValue, + std::map>); + std::map> ParsePreviewFormats(Json::Value rootValue, + std::map>); + std::map> ParseVideoFormats(Json::Value rootValue, + std::map>); void InitDcameraBaseAbility(); void GetEachNodeSupportedResolution(std::vector& formats, const std::string rootNode, std::map>& supportedFormats, Json::Value& rootValue); @@ -73,6 +79,7 @@ private: void SetFpsRanges(); private: + constexpr static uint32_t JSON_ARRAY_MAX_SIZE = 1000; std::function)> resultCallback_; std::shared_ptr dCameraAbility_; std::string protocolVersion_; diff --git a/distributed_camera/hdi_service/include/dstream_operator/dstream_operator.h b/distributed_camera/hdi_service/include/dstream_operator/dstream_operator.h index 880d8af09e..f39e371332 100644 --- a/distributed_camera/hdi_service/include/dstream_operator/dstream_operator.h +++ b/distributed_camera/hdi_service/include/dstream_operator/dstream_operator.h @@ -59,6 +59,9 @@ public: const sptr &callbackObj, sptr &offlineOperator) override; DCamRetCode InitOutputConfigurations(const DHBase &dhBase, const std::string &abilityInfo); + DCamRetCode ParsePhotoFormats(Json::Value rootValue); + DCamRetCode ParsePreviewFormats(Json::Value rootValue); + DCamRetCode ParseVideoFormats(Json::Value rootValue); DCamRetCode AcquireBuffer(int streamId, DCameraBuffer &buffer); DCamRetCode ShutterBuffer(int streamId, const DCameraBuffer &buffer); DCamRetCode SetCallBack(OHOS::sptr const &callback); @@ -124,6 +127,7 @@ private: int32_t DoCapture(int32_t captureId, const CaptureInfo &info, bool isStreaming); private: + constexpr static uint32_t JSON_ARRAY_MAX_SIZE = 1000; std::shared_ptr dMetadataProcessor_; OHOS::sptr dcStreamOperatorCallback_; function errorCallback_; diff --git a/distributed_camera/hdi_service/src/dcamera_device/dmetadata_processor.cpp b/distributed_camera/hdi_service/src/dcamera_device/dmetadata_processor.cpp index 9adfd9c2f9..1cfdea93fa 100644 --- a/distributed_camera/hdi_service/src/dcamera_device/dmetadata_processor.cpp +++ b/distributed_camera/hdi_service/src/dcamera_device/dmetadata_processor.cpp @@ -574,10 +574,13 @@ void DMetadataProcessor::GetEachNodeSupportedResolution(std::vector& format { for (const auto &format : formats) { std::string formatStr = std::to_string(format); - if (rootValue[rootNode]["Resolution"][formatStr].isArray() && - rootValue[rootNode]["Resolution"][formatStr].size() > 0) { - GetNodeSupportedResolution(format, formatStr, rootNode, supportedFormats, rootValue); + if (!rootValue[rootNode].isMember("Resolution") || + !rootValue[rootNode]["Resolution"].isMember(formatStr) || + !rootValue[rootNode]["Resolution"][formatStr].isArray() || + rootValue[rootNode]["Resolution"][formatStr].size() == 0) { + return; } + GetNodeSupportedResolution(format, formatStr, rootNode, supportedFormats, rootValue); } } @@ -587,6 +590,9 @@ void DMetadataProcessor::GetNodeSupportedResolution(int format, std::string form std::vector resolutionVec; uint32_t size = rootValue[rootNode]["Resolution"][formatStr].size(); for (uint32_t i = 0; i < size; i++) { + if (!rootValue[rootNode]["Resolution"][formatStr][i].isString()) { + continue; + } std::string resoStr = rootValue[rootNode]["Resolution"][formatStr][i].asString(); std::vector reso; SplitString(resoStr, reso, STAR_SEPARATOR); @@ -631,37 +637,72 @@ std::map> DMetadataProcessor::GetDCameraSupported !rootValue.isObject()) { return supportedFormats; } + ParsePhotoFormats(rootValue, supportedFormats); + ParsePreviewFormats(rootValue, supportedFormats); + ParseVideoFormats(rootValue, supportedFormats); + return supportedFormats; +} +std::map> DMetadataProcessor::ParsePhotoFormats(Json::Value rootValue, + std::map> supportedFormats) +{ + if (!rootValue.isMember("Photo") || !rootValue["Photo"].isMember("OutputFormat") || + !rootValue["Photo"]["OutputFormat"].isArray() || rootValue["Photo"]["OutputFormat"].size() == 0) { + return supportedFormats; + } std::vector photoFormats; - if (rootValue["Photo"]["OutputFormat"].isArray() && (rootValue["Photo"]["OutputFormat"].size() > 0)) { - uint32_t size = rootValue["Photo"]["OutputFormat"].size(); - for (uint32_t i = 0; i < size; i++) { + uint32_t size = rootValue["Photo"]["OutputFormat"].size(); + if (size > JSON_ARRAY_MAX_SIZE) { + return supportedFormats; + } + for (uint32_t i = 0; i < size; i++) { + if ((rootValue["Photo"]["OutputFormat"][i]).isInt()) { photoFormats.push_back((rootValue["Photo"]["OutputFormat"][i]).asInt()); } } - GetEachNodeSupportedResolution(photoFormats, "Photo", supportedFormats, rootValue); + return supportedFormats; +} +std::map> DMetadataProcessor::ParsePreviewFormats(Json::Value rootValue, + std::map> supportedFormats) +{ + if (!rootValue.isMember("Preview") || !rootValue["Preview"].isMember("OutputFormat") || + !rootValue["Preview"]["OutputFormat"].isArray() || (rootValue["Preview"]["OutputFormat"].size() == 0)) { + return supportedFormats; + } std::vector previewFormats; - if (rootValue["Preview"]["OutputFormat"].isArray() && (rootValue["Preview"]["OutputFormat"].size() > 0)) { - uint32_t size = rootValue["Preview"]["OutputFormat"].size(); - for (uint32_t i = 0; i < size; i++) { + uint32_t size = rootValue["Preview"]["OutputFormat"].size(); + if (size > JSON_ARRAY_MAX_SIZE) { + return supportedFormats; + } + for (uint32_t i = 0; i < size; i++) { + if ((rootValue["Preview"]["OutputFormat"][i]).isInt()) { previewFormats.push_back((rootValue["Preview"]["OutputFormat"][i]).asInt()); } } - GetEachNodeSupportedResolution(previewFormats, "Preview", supportedFormats, rootValue); + return supportedFormats; +} +std::map> DMetadataProcessor::ParseVideoFormats(Json::Value rootValue, + std::map> supportedFormats) +{ + if (!rootValue.isMember("Video") || !rootValue["Video"].isMember("OutputFormat") || + !rootValue["Video"]["OutputFormat"].isArray() || (rootValue["Video"]["OutputFormat"].size() == 0)) { + return supportedFormats; + } std::vector videoFormats; - if (rootValue["Video"]["OutputFormat"].isArray() && (rootValue["Video"]["OutputFormat"].size() > 0)) { - uint32_t size = rootValue["Video"]["OutputFormat"].size(); - for (uint32_t i = 0; i < size; i++) { + uint32_t size = rootValue["Video"]["OutputFormat"].size(); + if (size > JSON_ARRAY_MAX_SIZE) { + return supportedFormats; + } + for (uint32_t i = 0; i < size; i++) { + if ((rootValue["Video"]["OutputFormat"][i]).isInt()) { videoFormats.push_back((rootValue["Video"]["OutputFormat"][i]).asInt()); } } - GetEachNodeSupportedResolution(videoFormats, "Video", supportedFormats, rootValue); - return supportedFormats; } diff --git a/distributed_camera/hdi_service/src/dstream_operator/dstream_operator.cpp b/distributed_camera/hdi_service/src/dstream_operator/dstream_operator.cpp index e745833da7..4e257cc802 100644 --- a/distributed_camera/hdi_service/src/dstream_operator/dstream_operator.cpp +++ b/distributed_camera/hdi_service/src/dstream_operator/dstream_operator.cpp @@ -488,11 +488,15 @@ void DStreamOperator::ExtractCameraAttr(Json::Value &rootValue, std::vector { for (const auto &format : formats) { std::string formatStr = std::to_string(format); - if (rootValue[rootNode]["Resolution"][formatStr].isArray() && - rootValue[rootNode]["Resolution"][formatStr].size() > 0) { - GetCameraAttr(rootValue, formatStr, rootNode, format); + if (!rootValue[rootNode].isMember("Resolution") || + !rootValue[rootNode]["Resolution"].isMember(formatStr) || + !rootValue[rootNode]["Resolution"][formatStr].isArray() || + rootValue[rootNode]["Resolution"][formatStr].size() == 0) { + return; } + GetCameraAttr(rootValue, formatStr, rootNode, format); } + } void DStreamOperator::GetCameraAttr(Json::Value &rootValue, std::string formatStr, const std::string rootNode, @@ -501,6 +505,9 @@ void DStreamOperator::GetCameraAttr(Json::Value &rootValue, std::string formatSt std::vector resolutionVec; uint32_t size = rootValue[rootNode]["Resolution"][formatStr].size(); for (uint32_t i = 0; i < size; i++) { + if (!rootValue[rootNode]["Resolution"][formatStr][i].isString()) { + continue; + } std::string resoStr = rootValue[rootNode]["Resolution"][formatStr][i].asString(); std::vector reso; SplitString(resoStr, reso, STAR_SEPARATOR); @@ -534,7 +541,6 @@ void DStreamOperator::GetCameraAttr(Json::Value &rootValue, std::string formatSt DCamRetCode DStreamOperator::InitOutputConfigurations(const DHBase &dhBase, const std::string &abilityInfo) { dhBase_ = dhBase; - JSONCPP_STRING errs; Json::CharReaderBuilder readerBuilder; Json::Value rootValue; @@ -546,56 +552,100 @@ DCamRetCode DStreamOperator::InitOutputConfigurations(const DHBase &dhBase, cons return DCamRetCode::INVALID_ARGUMENT; } - if (rootValue["CodecType"].isArray()) { - uint32_t size = rootValue["CodecType"].size(); - for (uint32_t i = 0; i < size; i++) { + if (!rootValue.isMember("CodecType") || + !rootValue["CodecType"].isArray() || rootValue["CodecType"].size() == 0) { + return DCamRetCode::INVALID_ARGUMENT; + } + uint32_t size = rootValue["CodecType"].size(); + if (size > JSON_ARRAY_MAX_SIZE) { + return DCamRetCode::INVALID_ARGUMENT; + } + for (uint32_t i = 0; i < size; i++) { + if ((rootValue["CodecType"][i]).isString()) { std::string codeType = (rootValue["CodecType"][i]).asString(); dcSupportedCodecType_.push_back(ConvertDCEncodeType(codeType)); } } + if (ParsePhotoFormats(rootValue) != SUCCESS || + ParsePreviewFormats(rootValue) != SUCCESS || + ParseVideoFormats(rootValue) != SUCCESS) { + return DCamRetCode::INVALID_ARGUMENT; + } + + bool resolutionMap = false; + if (!dcSupportedPhotoResolutionMap_.empty() || !dcSupportedPreviewResolutionMap_.empty() || + !dcSupportedVideoResolutionMap_.empty()) { + resolutionMap = true; + } + + if (dcSupportedCodecType_.empty() || dcSupportedFormatMap_.empty() || !resolutionMap) { + DHLOGE("Input ablity info is invalid."); + return DEVICE_NOT_INIT; + } + return SUCCESS; +} + +DCamRetCode DStreamOperator::ParsePhotoFormats(Json::Value rootValue) +{ + if (!rootValue.isMember("Photo") || !rootValue["Photo"].isMember("OutputFormat") || + !rootValue["Photo"]["OutputFormat"].isArray() || (rootValue["Photo"]["OutputFormat"].size() == 0)) { + return DCamRetCode::INVALID_ARGUMENT; + } std::vector photoFormats; - if (rootValue["Photo"]["OutputFormat"].isArray() && (rootValue["Photo"]["OutputFormat"].size() > 0)) { - uint32_t size = rootValue["Photo"]["OutputFormat"].size(); - for (uint32_t i = 0; i < size; i++) { + uint32_t size = rootValue["Photo"]["OutputFormat"].size(); + if (size > JSON_ARRAY_MAX_SIZE) { + return DCamRetCode::INVALID_ARGUMENT; + } + for (uint32_t i = 0; i < size; i++) { + if ((rootValue["Photo"]["OutputFormat"][i]).isInt()) { photoFormats.push_back((rootValue["Photo"]["OutputFormat"][i]).asInt()); } - dcSupportedFormatMap_[DCSceneType::PHOTO] = photoFormats; } + dcSupportedFormatMap_[DCSceneType::PHOTO] = photoFormats; ExtractCameraAttr(rootValue, photoFormats, "Photo"); + return SUCCESS; +} +DCamRetCode DStreamOperator::ParsePreviewFormats(Json::Value rootValue) +{ + if (!rootValue.isMember("Preview") || !rootValue["Preview"].isMember("OutputFormat") || + !rootValue["Preview"]["OutputFormat"].isArray() || (rootValue["Preview"]["OutputFormat"].size() == 0)) { + return DCamRetCode::INVALID_ARGUMENT; + } std::vector previewFormats; - if (rootValue["Preview"]["OutputFormat"].isArray() && (rootValue["Preview"]["OutputFormat"].size() > 0)) { - uint32_t size = rootValue["Preview"]["OutputFormat"].size(); - for (uint32_t i = 0; i < size; i++) { + uint32_t size = rootValue["Preview"]["OutputFormat"].size(); + if (size > JSON_ARRAY_MAX_SIZE) { + return DCamRetCode::INVALID_ARGUMENT; + } + for (uint32_t i = 0; i < size; i++) { + if ((rootValue["Preview"]["OutputFormat"][i]).isInt()) { previewFormats.push_back((rootValue["Preview"]["OutputFormat"][i]).asInt()); } - dcSupportedFormatMap_[DCSceneType::PREVIEW] = previewFormats; } + dcSupportedFormatMap_[DCSceneType::PREVIEW] = previewFormats; ExtractCameraAttr(rootValue, previewFormats, "Preview"); + return SUCCESS; +} +DCamRetCode DStreamOperator::ParseVideoFormats(Json::Value rootValue) +{ + if (!rootValue.isMember("Video") || !rootValue["Video"].isMember("OutputFormat") || + !rootValue["Video"]["OutputFormat"].isArray() || (rootValue["Video"]["OutputFormat"].size() == 0)) { + return DCamRetCode::INVALID_ARGUMENT; + } std::vector videoFormats; - if (rootValue["Video"]["OutputFormat"].isArray() && (rootValue["Video"]["OutputFormat"].size() > 0)) { - uint32_t size = rootValue["Video"]["OutputFormat"].size(); - for (uint32_t i = 0; i < size; i++) { + uint32_t size = rootValue["Video"]["OutputFormat"].size(); + if (size > JSON_ARRAY_MAX_SIZE) { + return DCamRetCode::INVALID_ARGUMENT; + } + for (uint32_t i = 0; i < size; i++) { + if ((rootValue["Video"]["OutputFormat"][i]).isInt()) { videoFormats.push_back((rootValue["Video"]["OutputFormat"][i]).asInt()); } - dcSupportedFormatMap_[DCSceneType::VIDEO] = videoFormats; } + dcSupportedFormatMap_[DCSceneType::VIDEO] = videoFormats; ExtractCameraAttr(rootValue, videoFormats, "Video"); - - - bool resolutionMap = false; - if (!dcSupportedPhotoResolutionMap_.empty() || !dcSupportedPreviewResolutionMap_.empty() || - !dcSupportedVideoResolutionMap_.empty()) { - resolutionMap = true; - } - - if (dcSupportedCodecType_.empty() || dcSupportedFormatMap_.empty() || !resolutionMap) { - DHLOGE("Input ablity info is invalid."); - return DEVICE_NOT_INIT; - } - return SUCCESS; } -- Gitee