diff --git a/framework/common/tlv_util.cpp b/framework/common/tlv_util.cpp new file mode 100644 index 0000000000000000000000000000000000000000..09fe35ad3c6957253fc69f19a61470e08a4d5aa8 --- /dev/null +++ b/framework/common/tlv_util.cpp @@ -0,0 +1,1278 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "tlv_util.h" + +namespace OHOS { +namespace TLVUtil { +template<> +bool CountBufferSize(const std::shared_ptr &input, TLVObject &data) +{ + data.Count(input->GetType()); + data.Count(input->GetUid()); + auto type = input->GetType(); + switch (type) { + case UDType::TEXT: { + auto text = static_cast(input.get()); + if (text == nullptr) { + return false; + } + data.Count(text->GetDetails()); + break; + } + case UDType::PLAIN_TEXT: { + auto plainText = static_cast(input.get()); + if (plainText == nullptr) { + return false; + } + data.Count(plainText->GetContent()); + data.Count(plainText->GetAbstract()); + auto text = static_cast<Text *>(input.get()); + if (text == nullptr) { + return false; + } + data.Count(text->GetDetails()); + break; + } + case UDType::HTML: { + auto html = static_cast<Html *>(input.get()); + if (html == nullptr) { + return false; + } + data.Count(html->GetHtmlContent()); + data.Count(html->GetPlainContent()); + auto text = static_cast<Text *>(input.get()); + if (text == nullptr) { + return false; + } + data.Count(text->GetDetails()); + break; + } + case UDType::HYPERLINK: { + auto link = static_cast<Link *>(input.get()); + if (link == nullptr) { + return false; + } + data.Count(link->GetUrl()); + data.Count(link->GetDescription()); + auto text = static_cast<Text *>(input.get()); + if (text == nullptr) { + return false; + } + data.Count(text->GetDetails()); + break; + } + case UDType::FILE: { + auto file = static_cast<File *>(input.get()); + if (file == nullptr) { + return false; + } + data.Count(file->GetUri()); + data.Count(file->GetRemoteUri()); + data.Count(file->GetDetails()); + break; + } + case UDType::IMAGE: { + auto image = static_cast<Image *>(input.get()); + if (image == nullptr) { + return false; + } + data.Count(image->GetUri()); + data.Count(image->GetRemoteUri()); + auto file = static_cast<File *>(input.get()); + if (file == nullptr) { + return false; + } + data.Count(file->GetDetails()); + break; + } + case UDType::VIDEO: { + auto video = static_cast<Video *>(input.get()); + if (video == nullptr) { + return false; + } + data.Count(video->GetUri()); + data.Count(video->GetRemoteUri()); + auto file = static_cast<File *>(input.get()); + if (file == nullptr) { + return false; + } + data.Count(file->GetDetails()); + break; + } + case UDType::FOLDER: { + auto folder = static_cast<Folder *>(input.get()); + if (folder == nullptr) { + return false; + } + data.Count(folder->GetUri()); + data.Count(folder->GetRemoteUri()); + auto file = static_cast<File *>(input.get()); + if (file == nullptr) { + return false; + } + data.Count(file->GetDetails()); + break; + } + case UDType::SYSTEM_DEFINED_RECORD: { + auto sdRecord = static_cast<SystemDefinedRecord *>(input.get()); + if (sdRecord == nullptr) { + return false; + } + data.Count(sdRecord->GetDetails()); + break; + } + case UDType::SYSTEM_DEFINED_FORM: { + auto form = static_cast<SystemDefinedForm *>(input.get()); + if (form == nullptr) { + return false; + } + data.Count(form->GetFormId()); + data.Count(form->GetFormName()); + data.Count(form->GetBundleName()); + data.Count(form->GetAbilityName()); + data.Count(form->GetModule()); + auto sdRecord = static_cast<SystemDefinedRecord *>(input.get()); + if (sdRecord == nullptr) { + return false; + } + data.Count(sdRecord->GetDetails()); + break; + } + case UDType::SYSTEM_DEFINED_APP_ITEM: { + auto appItem = static_cast<SystemDefinedAppItem *>(input.get()); + if (appItem == nullptr) { + return false; + } + data.Count(appItem->GetAppId()); + data.Count(appItem->GetAppName()); + data.Count(appItem->GetAppIconId()); + data.Count(appItem->GetAppLabelId()); + data.Count(appItem->GetBundleName()); + data.Count(appItem->GetAbilityName()); + auto sdRecord = static_cast<SystemDefinedRecord *>(input.get()); + if (sdRecord == nullptr) { + return false; + } + data.Count(sdRecord->GetDetails()); + break; + } + case UDType::SYSTEM_DEFINED_PIXEL_MAP: { + auto pixelMap = static_cast<SystemDefinedPixelMap *>(input.get()); + if (pixelMap == nullptr) { + return false; + } + data.Count(pixelMap->GetRawData()); + auto sdRecord = static_cast<SystemDefinedRecord *>(input.get()); + if (sdRecord == nullptr) { + return false; + } + data.Count(sdRecord->GetDetails()); + break; + } + case UDType::APPLICATION_DEFINED_RECORD: { + auto record = static_cast<ApplicationDefinedRecord *>(input.get()); + if (record == nullptr) { + return false; + } + data.Count(record->GetApplicationDefinedType()); + data.Count(record->GetRawData()); + break; + } + default: { + return false; + } + } + return true; +} + +template<> +bool CountBufferSize(const Runtime &input, TLVObject &data) +{ + data.Count(input.key); + data.Count(input.isPrivate); + int32_t size = input.privileges.size(); + data.Count(size); + for (int i = 0; i < size; ++i) { + data.Count(input.privileges[i]); + } + data.Count(static_cast<int64_t>(input.createTime)); + data.Count(static_cast<int64_t>(input.lastModifiedTime)); + data.Count(input.sourcePackage); + data.Count(static_cast<int32_t>(input.dataStatus)); + data.Count(input.dataVersion); + data.Count(input.createPackage); + data.Count(input.deviceId); + return true; +} + +template<typename T> +bool Writing(const T &input, TLVObject &data); + +template<typename T> +bool Reading(T &output, TLVObject &data); + +template<> +bool Writing(const int32_t &input, TLVObject &data) +{ + return data.WriteBasic(TAG_INT32, input); +} + +template<> +bool Reading(int32_t &output, TLVObject &data) +{ + return data.ReadBasic(output); +} + +template<> +bool Writing(const int64_t &input, TLVObject &data) +{ + return data.WriteBasic(TAG_INT64, input); +} + +template<> +bool Reading(int64_t &output, TLVObject &data) +{ + return data.ReadBasic(output); +} + +template<> +bool Writing(const bool &input, TLVObject &data) +{ + return data.WriteBasic(TAG_BOOL, input); +} + +template<> +bool Reading(bool &output, TLVObject &data) +{ + return data.ReadBasic(output); +} + +template<> +bool Writing(const std::string &input, TLVObject &data) +{ + return data.WriteString(input); +} + +template<> +bool Reading(std::string &output, TLVObject &data) +{ + return data.ReadString(output); +} + +template<> +bool Writing(const std::vector<uint8_t> &input, TLVObject &data) +{ + return data.WriteVector(input); +} + +template<> +bool Reading(std::vector<uint8_t> &output, TLVObject &data) +{ + return data.ReadVector(output); +} + +template<> +bool Writing(const UDVariant &input, TLVObject &data) +{ + return data.WriteVariant(input); +} + +template<> +bool Reading(UDVariant &output, TLVObject &data) +{ + return data.ReadVariant(output); +} + +template<> +bool Writing(const UDDetails &input, TLVObject &data) +{ + return data.WriteMap(input); +} + +template<> +bool Reading(UDDetails &output, TLVObject &data) +{ + return data.ReadMap(output); +} + +template<> +bool Writing(const UDType &input, TLVObject &data) +{ + int32_t type = input; + return Writing(type, data); +} + +template<> +bool Reading(UDType &output, TLVObject &data) +{ + int32_t type; + if (!Reading(type, data)) { + return false; + } + if (type < UDType::TEXT || type >= UDType::UD_BUTT) { + return false; + } + output = static_cast<UDType>(type); + return true; +} + +template<> +bool Writing(const Text &input, TLVObject &data) +{ + return Writing(input.GetDetails(), data); +} + +template<> +bool Reading(Text &output, TLVObject &data) +{ + UDDetails details; + if (!Reading(details, data)) { + return false; + } + output.SetDetails(details); + return true; +} + +template<> +bool Writing(const PlainText &input, TLVObject &data) +{ + if (!Writing(input.GetContent(), data)) { + return false; + } + if (!Writing(input.GetAbstract(), data)) { + return false; + } + return true; +} + +template<> +bool Reading(PlainText &output, TLVObject &data) +{ + std::string content; + std::string abstract; + UDDetails details; + if (!Reading(content, data)) { + return false; + } + if (!Reading(abstract, data)) { + return false; + } + if (!Reading(details, data)) { + return false; + } + output.SetContent(content); + output.SetAbstract(abstract); + output.SetDetails(details); + return true; +} + +template<> +bool Writing(const Html &input, TLVObject &data) +{ + if (!Writing(input.GetHtmlContent(), data)) { + return false; + } + if (!Writing(input.GetPlainContent(), data)) { + return false; + } + return true; +} + +template<> +bool Reading(Html &output, TLVObject &data) +{ + std::string htmlContent; + std::string plainContent; + UDDetails details; + if (!Reading(htmlContent, data)) { + return false; + } + if (!Reading(plainContent, data)) { + return false; + } + if (!Reading(details, data)) { + return false; + } + output.SetHtmlContent(htmlContent); + output.SetPlainContent(plainContent); + output.SetDetails(details); + return true; +} + +template<> +bool Writing(const Link &input, TLVObject &data) +{ + if (!Writing(input.GetUrl(), data)) { + return false; + } + if (!Writing(input.GetDescription(), data)) { + return false; + } + return true; +} + +template<> +bool Reading(Link &output, TLVObject &data) +{ + std::string url; + std::string description; + UDDetails details; + if (!Reading(url, data)) { + return false; + } + if (!Reading(description, data)) { + return false; + } + if (!Reading(details, data)) { + return false; + } + output.SetUrl(url); + output.SetDescription(description); + output.SetDetails(details); + return true; +} + +template<> +bool Writing(const File &input, TLVObject &data) +{ + if (!Writing(input.GetUri(), data)) { + return false; + } + if (!Writing(input.GetRemoteUri(), data)) { + return false; + } + if (!Writing(input.GetDetails(), data)) { + return false; + } + return true; +} + +template<> +bool Reading(File &output, TLVObject &data) +{ + std::string uri; + std::string remoteUri; + UDDetails details; + if (!Reading(uri, data)) { + return false; + } + if (!Reading(remoteUri, data)) { + return false; + } + if (!Reading(details, data)) { + return false; + } + output.SetUri(uri); + output.SetRemoteUri(remoteUri); + output.SetDetails(details); + return true; +} + +template<> +bool Writing(const Image &input, TLVObject &data) +{ + return true; +} + +template<> +bool Reading(Image &output, TLVObject &data) +{ + std::string uri; + std::string remoteUri; + UDDetails details; + if (!Reading(uri, data)) { + return false; + } + if (!Reading(remoteUri, data)) { + return false; + } + if (!Reading(details, data)) { + return false; + } + output.SetUri(uri); + output.SetRemoteUri(remoteUri); + output.SetDetails(details); + return true; +} + +template<> +bool Writing(const Video &input, TLVObject &data) +{ + return true; +} + +template<> +bool Reading(Video &output, TLVObject &data) +{ + std::string uri; + std::string remoteUri; + UDDetails details; + if (!Reading(uri, data)) { + return false; + } + if (!Reading(remoteUri, data)) { + return false; + } + if (!Reading(details, data)) { + return false; + } + output.SetUri(uri); + output.SetRemoteUri(remoteUri); + output.SetDetails(details); + return true; +} + +template<> +bool Writing(const Folder &input, TLVObject &data) +{ + return true; +} + +template<> +bool Reading(Folder &output, TLVObject &data) +{ + std::string uri; + std::string remoteUri; + UDDetails details; + if (!Reading(uri, data)) { + return false; + } + if (!Reading(remoteUri, data)) { + return false; + } + if (!Reading(details, data)) { + return false; + } + output.SetUri(uri); + output.SetRemoteUri(remoteUri); + output.SetDetails(details); + return true; +} + +template<> +bool Writing(const SystemDefinedRecord &input, TLVObject &data) +{ + if (!Writing(input.GetDetails(), data)) { + return false; + } + return true; +} + +template<> +bool Reading(SystemDefinedRecord &output, TLVObject &data) +{ + UDDetails details; + if (!Reading(details, data)) { + return false; + } + output.SetDetails(details); + return true; +} + +template<> +bool Writing(const SystemDefinedForm &input, TLVObject &data) +{ + if (!Writing(input.GetFormId(), data)) { + return false; + } + if (!Writing(input.GetFormName(), data)) { + return false; + } + if (!Writing(input.GetBundleName(), data)) { + return false; + } + if (!Writing(input.GetAbilityName(), data)) { + return false; + } + if (!Writing(input.GetModule(), data)) { + return false; + } + return true; +} + +template<> +bool Reading(SystemDefinedForm &output, TLVObject &data) +{ + int32_t formId; + std::string formName; + std::string bundleName; + std::string abilityName; + std::string module; + UDDetails details; + if (!Reading(formId, data)) { + return false; + } + if (!Reading(formName, data)) { + return false; + } + if (!Reading(bundleName, data)) { + return false; + } + if (!Reading(abilityName, data)) { + return false; + } + if (!Reading(module, data)) { + return false; + } + if (!Reading(details, data)) { + return false; + } + output.SetFormId(formId); + output.SetFormName(formName); + output.SetBundleName(bundleName); + output.SetAbilityName(abilityName); + output.SetModule(module); + output.SetDetails(details); + return true; +} + +template<> +bool Writing(const SystemDefinedAppItem &input, TLVObject &data) +{ + if (!Writing(input.GetAppId(), data)) { + return false; + } + if (!Writing(input.GetAppName(), data)) { + return false; + } + if (!Writing(input.GetAppIconId(), data)) { + return false; + } + if (!Writing(input.GetAppLabelId(), data)) { + return false; + } + if (!Writing(input.GetBundleName(), data)) { + return false; + } + if (!Writing(input.GetAbilityName(), data)) { + return false; + } + return true; +} + +template<> +bool Reading(SystemDefinedAppItem &output, TLVObject &data) +{ + std::string appId; + std::string appName; + std::string appIconId; + std::string appLabelId; + std::string bundleName; + std::string abilityName; + UDDetails details; + if (!Reading(appId, data)) { + return false; + } + if (!Reading(appName, data)) { + return false; + } + if (!Reading(appIconId, data)) { + return false; + } + if (!Reading(appLabelId, data)) { + return false; + } + if (!Reading(bundleName, data)) { + return false; + } + if (!Reading(abilityName, data)) { + return false; + } + if (!Reading(details, data)) { + return false; + } + output.SetAppId(appId); + output.SetAppName(appName); + output.SetAppIconId(appIconId); + output.SetAppLabelId(appLabelId); + output.SetBundleName(bundleName); + output.SetAbilityName(abilityName); + output.SetDetails(details); + return true; +} + +template<> +bool Writing(const SystemDefinedPixelMap &input, TLVObject &data) +{ + if (!Writing(input.GetRawData(), data)) { + return false; + } + return true; +} + +template<> +bool Reading(SystemDefinedPixelMap &output, TLVObject &data) +{ + std::vector<uint8_t> rawData; + UDDetails details; + if (!Reading(rawData, data)) { + return false; + } + if (!Reading(details, data)) { + return false; + } + output.SetRawData(rawData); + output.SetDetails(details); + return true; +} + +template<> +bool Writing(const ApplicationDefinedRecord &input, TLVObject &data) +{ + if (!Writing(input.GetApplicationDefinedType(), data)) { + return false; + } + if (!Writing(input.GetRawData(), data)) { + return false; + } + return true; +} + +template<> +bool Reading(ApplicationDefinedRecord &output, TLVObject &data) +{ + std::string type; + std::vector<uint8_t> rawData; + if (!Reading(type, data)) { + return false; + } + if (!Reading(rawData, data)) { + return false; + } + output.SetApplicationDefinedType(type); + output.SetRawData(rawData); + return true; +} + +template<> +bool Writing(const std::shared_ptr<UnifiedRecord> &input, TLVObject &data) +{ + if (!CountBufferSize(input, data)) { + return false; + } + data.UpdateSize(); + + if (!Writing(input->GetType(), data)) { + return false; + } + if (!Writing(input->GetUid(), data)) { + return false; + } + auto type = input->GetType(); + switch (type) { + case UDType::TEXT: { + auto text = static_cast<Text *>(input.get()); + if (text == nullptr) { + return false; + } + return Writing(*text, data); + } + case UDType::PLAIN_TEXT: { + auto plainText = static_cast<PlainText *>(input.get()); + if (plainText == nullptr) { + return false; + } + if (!Writing(*plainText, data)) { + return false; + } + auto text = static_cast<Text *>(input.get()); + if (text == nullptr) { + return false; + } + return Writing(*text, data); + } + case UDType::HTML: { + auto html = static_cast<Html *>(input.get()); + if (html == nullptr) { + return false; + } + if (!Writing(*html, data)) { + return false; + } + auto text = static_cast<Text *>(input.get()); + if (text == nullptr) { + return false; + } + return Writing(*text, data); + } + case UDType::HYPERLINK: { + auto link = static_cast<Link *>(input.get()); + if (link == nullptr) { + return false; + } + if (!Writing(*link, data)) { + return false; + } + auto text = static_cast<Text *>(input.get()); + if (text == nullptr) { + return false; + } + return Writing(*text, data); + } + case UDType::FILE: { + auto file = static_cast<File *>(input.get()); + if (file == nullptr) { + return false; + } + return Writing(*file, data); + } + case UDType::IMAGE: { + auto image = static_cast<Image *>(input.get()); + if (image == nullptr) { + return false; + } + if (!Writing(*image, data)) { + return false; + } + auto file = static_cast<File *>(input.get()); + if (file == nullptr) { + return false; + } + return Writing(*file, data); + } + case UDType::VIDEO: { + auto video = static_cast<Video *>(input.get()); + if (video == nullptr) { + return false; + } + if (!Writing(*video, data)) { + return false; + } + auto file = static_cast<File *>(input.get()); + if (file == nullptr) { + return false; + } + return Writing(*file, data); + } + case UDType::FOLDER: { + auto folder = static_cast<Folder *>(input.get()); + if (folder == nullptr) { + return false; + } + if (!Writing(*folder, data)) { + return false; + } + auto file = static_cast<File *>(input.get()); + if (file == nullptr) { + return false; + } + return Writing(*file, data); + } + case UDType::SYSTEM_DEFINED_RECORD: { + auto sdRecord = static_cast<SystemDefinedRecord *>(input.get()); + if (sdRecord == nullptr) { + return false; + } + return Writing(*sdRecord, data); + } + case UDType::SYSTEM_DEFINED_FORM: { + auto form = static_cast<SystemDefinedForm *>(input.get()); + if (form == nullptr) { + return false; + } + if (!Writing(*form, data)) { + return false; + } + auto sdRecord = static_cast<SystemDefinedRecord *>(input.get()); + if (sdRecord == nullptr) { + return false; + } + return Writing(*sdRecord, data); + } + case UDType::SYSTEM_DEFINED_APP_ITEM: { + auto appItem = static_cast<SystemDefinedAppItem *>(input.get()); + if (appItem == nullptr) { + return false; + } + if (!Writing(*appItem, data)) { + return false; + } + auto sdRecord = static_cast<SystemDefinedRecord *>(input.get()); + if (sdRecord == nullptr) { + return false; + } + return Writing(*sdRecord, data); + } + case UDType::SYSTEM_DEFINED_PIXEL_MAP: { + auto pixelMap = static_cast<SystemDefinedPixelMap *>(input.get()); + if (pixelMap == nullptr) { + return false; + } + if (!Writing(*pixelMap, data)) { + return false; + } + auto sdRecord = static_cast<SystemDefinedRecord *>(input.get()); + if (sdRecord == nullptr) { + return false; + } + return Writing(*sdRecord, data); + } + case UDType::APPLICATION_DEFINED_RECORD: { + auto record = static_cast<ApplicationDefinedRecord *>(input.get()); + if (record == nullptr) { + return false; + } + return Writing(*record, data); + } + default: { + return false; + } + } +} + +template<> +bool Reading(std::shared_ptr<UnifiedRecord> &output, TLVObject &data) +{ + UDType type ; + if (!Reading(type, data)) { + return false; + } + std::string uid; + if (!Reading(uid, data)) { + return false; + } + switch (type) { + case UDType::TEXT: { + std::shared_ptr<Text> text = std::make_shared<Text>(); + if (!Reading(*text, data)) { + return false; + } + output = text; + break; + } + case UDType::PLAIN_TEXT: { + std::shared_ptr<PlainText> plainText = std::make_shared<PlainText>(); + if (!Reading(*plainText, data)) { + return false; + } + output = plainText; + break; + } + case UDType::HTML: { + std::shared_ptr<Html> html = std::make_shared<Html>(); + if (!Reading(*html, data)) { + return false; + } + output = html; + break; + } + case UDType::HYPERLINK: { + std::shared_ptr<Link> link = std::make_shared<Link>(); + if (!Reading(*link, data)) { + return false; + } + output = link; + break; + } + case UDType::FILE: { + std::shared_ptr<File> file = std::make_shared<File>(); + if (!Reading(*file, data)) { + return false; + } + output = file; + break; + } + case UDType::IMAGE: { + std::shared_ptr<Image> image = std::make_shared<Image>(); + if (!Reading(*image, data)) { + return false; + } + output = image; + break; + } + case UDType::VIDEO: { + std::shared_ptr<Video> video = std::make_shared<Video>(); + if (!Reading(*video, data)) { + return false; + } + output = video; + break; + } + case UDType::FOLDER: { + std::shared_ptr<Folder> folder = std::make_shared<Folder>(); + if (!Reading(*folder, data)) { + return false; + } + output = folder; + break; + } + case UDType::SYSTEM_DEFINED_RECORD: { + std::shared_ptr<SystemDefinedRecord> sdRecord = std::make_shared<SystemDefinedRecord>(); + if (!Reading(*sdRecord, data)) { + return false; + } + output = sdRecord; + break; + } + case UDType::SYSTEM_DEFINED_FORM: { + std::shared_ptr<SystemDefinedForm> form = std::make_shared<SystemDefinedForm>(); + if (!Reading(*form, data)) { + return false; + } + output = form; + break; + } + case UDType::SYSTEM_DEFINED_APP_ITEM: { + std::shared_ptr<SystemDefinedAppItem> appItem = std::make_shared<SystemDefinedAppItem>(); + if (!Reading(*appItem, data)) { + return false; + } + output = appItem; + break; + } + case UDType::SYSTEM_DEFINED_PIXEL_MAP: { + std::shared_ptr<SystemDefinedPixelMap> pixelMap = std::make_shared<SystemDefinedPixelMap>(); + if (!Reading(*pixelMap, data)) { + return false; + } + output = pixelMap; + break; + } + case UDType::APPLICATION_DEFINED_RECORD: { + std::shared_ptr<ApplicationDefinedRecord> record = std::make_shared<ApplicationDefinedRecord>(); + if (!Reading(*record, data)) { + return false; + } + output = record; + break; + } + default: { + return false; + } + } + output->SetUid(uid); + output->SetType(type); + return true; +} + +template<> +bool Writing(const UnifiedKey &input, TLVObject &data) +{ + if (!Writing(input.key, data)) { + return false; + } + if (!Writing(input.intention, data)) { + return false; + } + if (!Writing(input.bundleName, data)) { + return false; + } + if (!Writing(input.groupId, data)) { + return false; + } + return true; +} + +template<> +bool Reading(UnifiedKey &output, TLVObject &data) +{ + std::string key; + std::string intention; + std::string bundleName; + std::string groupId; + if (!Reading(key, data)) { + return false; + } + if (!Reading(intention, data)) { + return false; + } + if (!Reading(bundleName, data)) { + return false; + } + if (!Reading(groupId, data)) { + return false; + } + output.key = key; + output.intention = intention; + output.bundleName = bundleName; + output.groupId = groupId; + return true; +} + +template<> +bool Writing(const Privilege &input, TLVObject &data) +{ + if (!Writing(input.tokenId, data)) { + return false; + } + if (!Writing(input.pid, data)) { + return false; + } + if (!Writing(input.readPermission, data)) { + return false; + } + if (!Writing(input.writePermission, data)) { + return false; + } + return true; +} + +template<> +bool Reading(Privilege &output, TLVObject &data) +{ + int32_t tokenId; + int32_t pid; + std::string readPermission; + std::string writePermission; + if (!Reading(tokenId, data)) { + return false; + } + if (!Reading(pid, data)) { + return false; + } + if (!Reading(readPermission, data)) { + return false; + } + if (!Reading(writePermission, data)) { + return false; + } + output.tokenId = tokenId; + output.pid = pid; + output.readPermission = readPermission; + output.writePermission = writePermission; + return true; +} + +template<> +bool Writing(const DataStatus &input, TLVObject &data) +{ + int32_t status = input; + return Writing(status, data); +} + +template<> +bool Reading(DataStatus &output, TLVObject &data) +{ + int32_t status; + if (!Reading(status, data)) { + return false; + } + if (status < DataStatus::WORKING || status >= DataStatus::FADE) { + return false; + } + output = static_cast<DataStatus>(status); + return true; +} + +template<> +bool Writing(const Runtime &input, TLVObject &data) +{ + (void)CountBufferSize(input, data); + data.UpdateSize(); + if (!Writing(input.key, data)) { + return false; + } + if (!Writing(input.isPrivate, data)) { + return false; + } + int32_t size = input.privileges.size(); + if (!Writing(size, data)) { + return false; + } + for (int i = 0; i < size; ++i) { + if (!Writing(input.privileges[i], data)) { + return false; + } + } + if (!Writing(static_cast<int64_t>(input.createTime), data)) { + return false; + } + if (!Writing(input.sourcePackage, data)) { + return false; + } + if (!Writing(input.dataStatus, data)) { + return false; + } + if (!Writing(input.dataVersion, data)) { + return false; + } + if (!Writing(static_cast<int64_t>(input.lastModifiedTime), data)) { + return false; + } + if (!Writing(input.createPackage, data)) { + return false; + } + if (!Writing(input.deviceId, data)) { + return false; + } + return true; +} + +template<> +bool Reading(Runtime &output, TLVObject &data) +{ + UnifiedKey key; + bool isPrivate; + int32_t size; + std::vector<Privilege> privileges; + int64_t createTime; + std::string sourcePackage; + DataStatus dataStatus; + int32_t dataVersion; + int64_t lastModifiedTime; + std::string createPackage; + std::string deviceId; + if (!Reading(key, data)) { + return false; + } + if (!Reading(isPrivate, data)) { + return false; + } + if (!Reading(size, data)) { + return false; + } + for (int i = 0; i < size; ++i) { + Privilege privilege; + if (!Reading(privilege, data)) { + return false; + } + privileges.emplace_back(privilege); + } + if (!Reading(createTime, data)) { + return false; + } + if (!Reading(sourcePackage, data)) { + return false; + } + if (!Reading(dataStatus, data)) { + return false; + } + if (!Reading(dataVersion, data)) { + return false; + } + if (!Reading(lastModifiedTime, data)) { + return false; + } + if (!Reading(createPackage, data)) { + return false; + } + if (!Reading(deviceId, data)) { + return false; + } + output.key = key; + output.isPrivate = isPrivate; + output.privileges = privileges; + output.createTime = createTime; + output.sourcePackage = sourcePackage; + output.dataStatus = dataStatus; + output.dataVersion = dataVersion; + output.lastModifiedTime = lastModifiedTime; + output.createPackage = createPackage; + output.deviceId = deviceId; + return true; +} +} // namespace TLVUtil +} // namespace OHOS \ No newline at end of file diff --git a/framework/common/tlv_util.h b/framework/common/tlv_util.h index 1f900016bc4f82808ef9cf53dd098deb37acacdc..78d933ee7df92746ec788ac2ccf47794c3169f5d 100755 --- a/framework/common/tlv_util.h +++ b/framework/common/tlv_util.h @@ -41,1267 +41,149 @@ namespace OHOS { namespace TLVUtil { using namespace OHOS::UDMF; - template<typename T> bool CountBufferSize(const T &input, TLVObject &data); template<> -bool CountBufferSize(const std::shared_ptr<UnifiedRecord> &input, TLVObject &data) -{ - data.Count(input->GetType()); - data.Count(input->GetUid()); - auto type = input->GetType(); - switch (type) { - case UDType::TEXT: { - auto text = static_cast<Text *>(input.get()); - if (text == nullptr) { - return false; - } - data.Count(text->GetDetails()); - break; - } - case UDType::PLAIN_TEXT: { - auto plainText = static_cast<PlainText *>(input.get()); - if (plainText == nullptr) { - return false; - } - data.Count(plainText->GetContent()); - data.Count(plainText->GetAbstract()); - auto text = static_cast<Text *>(input.get()); - if (text == nullptr) { - return false; - } - data.Count(text->GetDetails()); - break; - } - case UDType::HTML: { - auto html = static_cast<Html *>(input.get()); - if (html == nullptr) { - return false; - } - data.Count(html->GetHtmlContent()); - data.Count(html->GetPlainContent()); - auto text = static_cast<Text *>(input.get()); - if (text == nullptr) { - return false; - } - data.Count(text->GetDetails()); - break; - } - case UDType::HYPERLINK: { - auto link = static_cast<Link *>(input.get()); - if (link == nullptr) { - return false; - } - data.Count(link->GetUrl()); - data.Count(link->GetDescription()); - auto text = static_cast<Text *>(input.get()); - if (text == nullptr) { - return false; - } - data.Count(text->GetDetails()); - break; - } - case UDType::FILE: { - auto file = static_cast<File *>(input.get()); - if (file == nullptr) { - return false; - } - data.Count(file->GetUri()); - data.Count(file->GetRemoteUri()); - data.Count(file->GetDetails()); - break; - } - case UDType::IMAGE: { - auto image = static_cast<Image *>(input.get()); - if (image == nullptr) { - return false; - } - data.Count(image->GetUri()); - data.Count(image->GetRemoteUri()); - auto file = static_cast<File *>(input.get()); - if (file == nullptr) { - return false; - } - data.Count(file->GetDetails()); - break; - } - case UDType::VIDEO: { - auto video = static_cast<Video *>(input.get()); - if (video == nullptr) { - return false; - } - data.Count(video->GetUri()); - data.Count(video->GetRemoteUri()); - auto file = static_cast<File *>(input.get()); - if (file == nullptr) { - return false; - } - data.Count(file->GetDetails()); - break; - } - case UDType::FOLDER: { - auto folder = static_cast<Folder *>(input.get()); - if (folder == nullptr) { - return false; - } - data.Count(folder->GetUri()); - data.Count(folder->GetRemoteUri()); - auto file = static_cast<File *>(input.get()); - if (file == nullptr) { - return false; - } - data.Count(file->GetDetails()); - break; - } - case UDType::SYSTEM_DEFINED_RECORD: { - auto sdRecord = static_cast<SystemDefinedRecord *>(input.get()); - if (sdRecord == nullptr) { - return false; - } - data.Count(sdRecord->GetDetails()); - break; - } - case UDType::SYSTEM_DEFINED_FORM: { - auto form = static_cast<SystemDefinedForm *>(input.get()); - if (form == nullptr) { - return false; - } - data.Count(form->GetFormId()); - data.Count(form->GetFormName()); - data.Count(form->GetBundleName()); - data.Count(form->GetAbilityName()); - data.Count(form->GetModule()); - auto sdRecord = static_cast<SystemDefinedRecord *>(input.get()); - if (sdRecord == nullptr) { - return false; - } - data.Count(sdRecord->GetDetails()); - break; - } - case UDType::SYSTEM_DEFINED_APP_ITEM: { - auto appItem = static_cast<SystemDefinedAppItem *>(input.get()); - if (appItem == nullptr) { - return false; - } - data.Count(appItem->GetAppId()); - data.Count(appItem->GetAppName()); - data.Count(appItem->GetAppIconId()); - data.Count(appItem->GetAppLabelId()); - data.Count(appItem->GetBundleName()); - data.Count(appItem->GetAbilityName()); - auto sdRecord = static_cast<SystemDefinedRecord *>(input.get()); - if (sdRecord == nullptr) { - return false; - } - data.Count(sdRecord->GetDetails()); - break; - } - case UDType::SYSTEM_DEFINED_PIXEL_MAP: { - auto pixelMap = static_cast<SystemDefinedPixelMap *>(input.get()); - if (pixelMap == nullptr) { - return false; - } - data.Count(pixelMap->GetRawData()); - auto sdRecord = static_cast<SystemDefinedRecord *>(input.get()); - if (sdRecord == nullptr) { - return false; - } - data.Count(sdRecord->GetDetails()); - break; - } - case UDType::APPLICATION_DEFINED_RECORD: { - auto record = static_cast<ApplicationDefinedRecord *>(input.get()); - if (record == nullptr) { - return false; - } - data.Count(record->GetApplicationDefinedType()); - data.Count(record->GetRawData()); - break; - } - default: { - return false; - } - } - return true; -} +bool CountBufferSize(const std::shared_ptr<UnifiedRecord> &input, TLVObject &data); template<> -bool CountBufferSize(const Runtime &input, TLVObject &data) -{ - data.Count(input.key); - data.Count(input.isPrivate); - int32_t size = input.privileges.size(); - data.Count(size); - for (int i = 0; i < size; ++i) { - data.Count(input.privileges[i]); - } - data.Count(static_cast<int64_t>(input.createTime)); - data.Count(static_cast<int64_t>(input.lastModifiedTime)); - data.Count(input.sourcePackage); - data.Count(static_cast<int32_t>(input.dataStatus)); - data.Count(input.dataVersion); - data.Count(input.createPackage); - data.Count(input.deviceId); - return true; -} +bool CountBufferSize(const Runtime &input, TLVObject &data); template<typename T> bool Writing(const T &input, TLVObject &data); - template<typename T> bool Reading(T &output, TLVObject &data); template<> -bool Writing(const int32_t &input, TLVObject &data) -{ - return data.WriteBasic(TAG_INT32, input); -} - +bool Writing(const int32_t &input, TLVObject &data); template<> -bool Reading(int32_t &output, TLVObject &data) -{ - return data.ReadBasic(output); -} +bool Reading(int32_t &output, TLVObject &data); template<> -bool Writing(const int64_t &input, TLVObject &data) -{ - return data.WriteBasic(TAG_INT64, input); -} - +bool Writing(const int64_t &input, TLVObject &data); template<> -bool Reading(int64_t &output, TLVObject &data) -{ - return data.ReadBasic(output); -} +bool Reading(int64_t &output, TLVObject &data); template<> -bool Writing(const bool &input, TLVObject &data) -{ - return data.WriteBasic(TAG_BOOL, input); -} - +bool Writing(const bool &input, TLVObject &data); template<> -bool Reading(bool &output, TLVObject &data) -{ - return data.ReadBasic(output); -} +bool Reading(bool &output, TLVObject &data); template<> -bool Writing(const std::string &input, TLVObject &data) -{ - return data.WriteString(input); -} - +bool Writing(const std::string &input, TLVObject &data); template<> -bool Reading(std::string &output, TLVObject &data) -{ - return data.ReadString(output); -} +bool Reading(std::string &output, TLVObject &data); template<> -bool Writing(const std::vector<uint8_t> &input, TLVObject &data) -{ - return data.WriteVector(input); -} - +bool Writing(const std::vector<uint8_t> &input, TLVObject &data); template<> -bool Reading(std::vector<uint8_t> &output, TLVObject &data) -{ - return data.ReadVector(output); -} +bool Reading(std::vector<uint8_t> &output, TLVObject &data); template<> -bool Writing(const UDVariant &input, TLVObject &data) -{ - return data.WriteVariant(input); -} - +bool Writing(const UDVariant &input, TLVObject &data); template<> -bool Reading(UDVariant &output, TLVObject &data) -{ - return data.ReadVariant(output); -} +bool Reading(UDVariant &output, TLVObject &data); template<> -bool Writing(const UDDetails &input, TLVObject &data) -{ - return data.WriteMap(input); -} - +bool Writing(const UDDetails &input, TLVObject &data); template<> -bool Reading(UDDetails &output, TLVObject &data) -{ - return data.ReadMap(output); -} +bool Reading(UDDetails &output, TLVObject &data); template<> -bool Writing(const UDType &input, TLVObject &data) -{ - int32_t type = input; - return Writing(type, data); -} - +bool Writing(const UDType &input, TLVObject &data); template<> -bool Reading(UDType &output, TLVObject &data) -{ - int32_t type; - if (!Reading(type, data)) { - return false; - } - if (type < UDType::TEXT || type >= UDType::UD_BUTT) { - return false; - } - output = static_cast<UDType>(type); - return true; -} +bool Reading(UDType &output, TLVObject &data); template<> -bool Writing(const Text &input, TLVObject &data) -{ - return Writing(input.GetDetails(), data); -} - +bool Writing(const Text &input, TLVObject &data); template<> -bool Reading(Text &output, TLVObject &data) -{ - UDDetails details; - if (!Reading(details, data)) { - return false; - } - output.SetDetails(details); - return true; -} +bool Reading(Text &output, TLVObject &data); template<> -bool Writing(const PlainText &input, TLVObject &data) -{ - if (!Writing(input.GetContent(), data)) { - return false; - } - if (!Writing(input.GetAbstract(), data)) { - return false; - } - return true; -} - +bool Writing(const PlainText &input, TLVObject &data); template<> -bool Reading(PlainText &output, TLVObject &data) -{ - std::string content; - std::string abstract; - UDDetails details; - if (!Reading(content, data)) { - return false; - } - if (!Reading(abstract, data)) { - return false; - } - if (!Reading(details, data)) { - return false; - } - output.SetContent(content); - output.SetAbstract(abstract); - output.SetDetails(details); - return true; -} +bool Reading(PlainText &output, TLVObject &data); template<> -bool Writing(const Html &input, TLVObject &data) -{ - if (!Writing(input.GetHtmlContent(), data)) { - return false; - } - if (!Writing(input.GetPlainContent(), data)) { - return false; - } - return true; -} - +bool Writing(const Html &input, TLVObject &data); template<> -bool Reading(Html &output, TLVObject &data) -{ - std::string htmlContent; - std::string plainContent; - UDDetails details; - if (!Reading(htmlContent, data)) { - return false; - } - if (!Reading(plainContent, data)) { - return false; - } - if (!Reading(details, data)) { - return false; - } - output.SetHtmlContent(htmlContent); - output.SetPlainContent(plainContent); - output.SetDetails(details); - return true; -} +bool Reading(Html &output, TLVObject &data); template<> -bool Writing(const Link &input, TLVObject &data) -{ - if (!Writing(input.GetUrl(), data)) { - return false; - } - if (!Writing(input.GetDescription(), data)) { - return false; - } - return true; -} - +bool Writing(const Link &input, TLVObject &data); template<> -bool Reading(Link &output, TLVObject &data) -{ - std::string url; - std::string description; - UDDetails details; - if (!Reading(url, data)) { - return false; - } - if (!Reading(description, data)) { - return false; - } - if (!Reading(details, data)) { - return false; - } - output.SetUrl(url); - output.SetDescription(description); - output.SetDetails(details); - return true; -} +bool Reading(Link &output, TLVObject &data); template<> -bool Writing(const File &input, TLVObject &data) -{ - if (!Writing(input.GetUri(), data)) { - return false; - } - if (!Writing(input.GetRemoteUri(), data)) { - return false; - } - if (!Writing(input.GetDetails(), data)) { - return false; - } - return true; -} - +bool Writing(const File &input, TLVObject &data); template<> -bool Reading(File &output, TLVObject &data) -{ - std::string uri; - std::string remoteUri; - UDDetails details; - if (!Reading(uri, data)) { - return false; - } - if (!Reading(remoteUri, data)) { - return false; - } - if (!Reading(details, data)) { - return false; - } - output.SetUri(uri); - output.SetRemoteUri(remoteUri); - output.SetDetails(details); - return true; -} +bool Reading(File &output, TLVObject &data); template<> -bool Writing(const Image &input, TLVObject &data) -{ - return true; -} - +bool Writing(const Image &input, TLVObject &data); template<> -bool Reading(Image &output, TLVObject &data) -{ - std::string uri; - std::string remoteUri; - UDDetails details; - if (!Reading(uri, data)) { - return false; - } - if (!Reading(remoteUri, data)) { - return false; - } - if (!Reading(details, data)) { - return false; - } - output.SetUri(uri); - output.SetRemoteUri(remoteUri); - output.SetDetails(details); - return true; -} +bool Reading(Image &output, TLVObject &data); template<> -bool Writing(const Video &input, TLVObject &data) -{ - return true; -} - +bool Writing(const Video &input, TLVObject &data); template<> -bool Reading(Video &output, TLVObject &data) -{ - std::string uri; - std::string remoteUri; - UDDetails details; - if (!Reading(uri, data)) { - return false; - } - if (!Reading(remoteUri, data)) { - return false; - } - if (!Reading(details, data)) { - return false; - } - output.SetUri(uri); - output.SetRemoteUri(remoteUri); - output.SetDetails(details); - return true; -} +bool Reading(Video &output, TLVObject &data); template<> -bool Writing(const Folder &input, TLVObject &data) -{ - return true; -} - +bool Writing(const Folder &input, TLVObject &data); template<> -bool Reading(Folder &output, TLVObject &data) -{ - std::string uri; - std::string remoteUri; - UDDetails details; - if (!Reading(uri, data)) { - return false; - } - if (!Reading(remoteUri, data)) { - return false; - } - if (!Reading(details, data)) { - return false; - } - output.SetUri(uri); - output.SetRemoteUri(remoteUri); - output.SetDetails(details); - return true; -} +bool Reading(Folder &output, TLVObject &data); template<> -bool Writing(const SystemDefinedRecord &input, TLVObject &data) -{ - if (!Writing(input.GetDetails(), data)) { - return false; - } - return true; -} - +bool Writing(const SystemDefinedRecord &input, TLVObject &data); template<> -bool Reading(SystemDefinedRecord &output, TLVObject &data) -{ - UDDetails details; - if (!Reading(details, data)) { - return false; - } - output.SetDetails(details); - return true; -} +bool Reading(SystemDefinedRecord &output, TLVObject &data); template<> -bool Writing(const SystemDefinedForm &input, TLVObject &data) -{ - if (!Writing(input.GetFormId(), data)) { - return false; - } - if (!Writing(input.GetFormName(), data)) { - return false; - } - if (!Writing(input.GetBundleName(), data)) { - return false; - } - if (!Writing(input.GetAbilityName(), data)) { - return false; - } - if (!Writing(input.GetModule(), data)) { - return false; - } - return true; -} - +bool Writing(const SystemDefinedForm &input, TLVObject &data); template<> -bool Reading(SystemDefinedForm &output, TLVObject &data) -{ - int32_t formId; - std::string formName; - std::string bundleName; - std::string abilityName; - std::string module; - UDDetails details; - if (!Reading(formId, data)) { - return false; - } - if (!Reading(formName, data)) { - return false; - } - if (!Reading(bundleName, data)) { - return false; - } - if (!Reading(abilityName, data)) { - return false; - } - if (!Reading(module, data)) { - return false; - } - if (!Reading(details, data)) { - return false; - } - output.SetFormId(formId); - output.SetFormName(formName); - output.SetBundleName(bundleName); - output.SetAbilityName(abilityName); - output.SetModule(module); - output.SetDetails(details); - return true; -} +bool Reading(SystemDefinedForm &output, TLVObject &data); template<> -bool Writing(const SystemDefinedAppItem &input, TLVObject &data) -{ - if (!Writing(input.GetAppId(), data)) { - return false; - } - if (!Writing(input.GetAppName(), data)) { - return false; - } - if (!Writing(input.GetAppIconId(), data)) { - return false; - } - if (!Writing(input.GetAppLabelId(), data)) { - return false; - } - if (!Writing(input.GetBundleName(), data)) { - return false; - } - if (!Writing(input.GetAbilityName(), data)) { - return false; - } - return true; -} - +bool Writing(const SystemDefinedAppItem &input, TLVObject &data); template<> -bool Reading(SystemDefinedAppItem &output, TLVObject &data) -{ - std::string appId; - std::string appName; - std::string appIconId; - std::string appLabelId; - std::string bundleName; - std::string abilityName; - UDDetails details; - if (!Reading(appId, data)) { - return false; - } - if (!Reading(appName, data)) { - return false; - } - if (!Reading(appIconId, data)) { - return false; - } - if (!Reading(appLabelId, data)) { - return false; - } - if (!Reading(bundleName, data)) { - return false; - } - if (!Reading(abilityName, data)) { - return false; - } - if (!Reading(details, data)) { - return false; - } - output.SetAppId(appId); - output.SetAppName(appName); - output.SetAppIconId(appIconId); - output.SetAppLabelId(appLabelId); - output.SetBundleName(bundleName); - output.SetAbilityName(abilityName); - output.SetDetails(details); - return true; -} +bool Reading(SystemDefinedAppItem &output, TLVObject &data); template<> -bool Writing(const SystemDefinedPixelMap &input, TLVObject &data) -{ - if (!Writing(input.GetRawData(), data)) { - return false; - } - return true; -} - +bool Writing(const SystemDefinedPixelMap &input, TLVObject &data); template<> -bool Reading(SystemDefinedPixelMap &output, TLVObject &data) -{ - std::vector<uint8_t> rawData; - UDDetails details; - if (!Reading(rawData, data)) { - return false; - } - if (!Reading(details, data)) { - return false; - } - output.SetRawData(rawData); - output.SetDetails(details); - return true; -} +bool Reading(SystemDefinedPixelMap &output, TLVObject &data); template<> -bool Writing(const ApplicationDefinedRecord &input, TLVObject &data) -{ - if (!Writing(input.GetApplicationDefinedType(), data)) { - return false; - } - if (!Writing(input.GetRawData(), data)) { - return false; - } - return true; -} - +bool Writing(const ApplicationDefinedRecord &input, TLVObject &data); template<> -bool Reading(ApplicationDefinedRecord &output, TLVObject &data) -{ - std::string type; - std::vector<uint8_t> rawData; - if (!Reading(type, data)) { - return false; - } - if (!Reading(rawData, data)) { - return false; - } - output.SetApplicationDefinedType(type); - output.SetRawData(rawData); - return true; -} +bool Reading(ApplicationDefinedRecord &output, TLVObject &data); template<> -bool Writing(const std::shared_ptr<UnifiedRecord> &input, TLVObject &data) -{ - if (!CountBufferSize(input, data)) { - return false; - } - data.UpdateSize(); - - if (!Writing(input->GetType(), data)) { - return false; - } - if (!Writing(input->GetUid(), data)) { - return false; - } - auto type = input->GetType(); - switch (type) { - case UDType::TEXT: { - auto text = static_cast<Text *>(input.get()); - if (text == nullptr) { - return false; - } - return Writing(*text, data); - } - case UDType::PLAIN_TEXT: { - auto plainText = static_cast<PlainText *>(input.get()); - if (plainText == nullptr) { - return false; - } - if (!Writing(*plainText, data)) { - return false; - } - auto text = static_cast<Text *>(input.get()); - if (text == nullptr) { - return false; - } - return Writing(*text, data); - } - case UDType::HTML: { - auto html = static_cast<Html *>(input.get()); - if (html == nullptr) { - return false; - } - if (!Writing(*html, data)) { - return false; - } - auto text = static_cast<Text *>(input.get()); - if (text == nullptr) { - return false; - } - return Writing(*text, data); - } - case UDType::HYPERLINK: { - auto link = static_cast<Link *>(input.get()); - if (link == nullptr) { - return false; - } - if (!Writing(*link, data)) { - return false; - } - auto text = static_cast<Text *>(input.get()); - if (text == nullptr) { - return false; - } - return Writing(*text, data); - } - case UDType::FILE: { - auto file = static_cast<File *>(input.get()); - if (file == nullptr) { - return false; - } - return Writing(*file, data); - } - case UDType::IMAGE: { - auto image = static_cast<Image *>(input.get()); - if (image == nullptr) { - return false; - } - if (!Writing(*image, data)) { - return false; - } - auto file = static_cast<File *>(input.get()); - if (file == nullptr) { - return false; - } - return Writing(*file, data); - } - case UDType::VIDEO: { - auto video = static_cast<Video *>(input.get()); - if (video == nullptr) { - return false; - } - if (!Writing(*video, data)) { - return false; - } - auto file = static_cast<File *>(input.get()); - if (file == nullptr) { - return false; - } - return Writing(*file, data); - } - case UDType::FOLDER: { - auto folder = static_cast<Folder *>(input.get()); - if (folder == nullptr) { - return false; - } - if (!Writing(*folder, data)) { - return false; - } - auto file = static_cast<File *>(input.get()); - if (file == nullptr) { - return false; - } - return Writing(*file, data); - } - case UDType::SYSTEM_DEFINED_RECORD: { - auto sdRecord = static_cast<SystemDefinedRecord *>(input.get()); - if (sdRecord == nullptr) { - return false; - } - return Writing(*sdRecord, data); - } - case UDType::SYSTEM_DEFINED_FORM: { - auto form = static_cast<SystemDefinedForm *>(input.get()); - if (form == nullptr) { - return false; - } - if (!Writing(*form, data)) { - return false; - } - auto sdRecord = static_cast<SystemDefinedRecord *>(input.get()); - if (sdRecord == nullptr) { - return false; - } - return Writing(*sdRecord, data); - } - case UDType::SYSTEM_DEFINED_APP_ITEM: { - auto appItem = static_cast<SystemDefinedAppItem *>(input.get()); - if (appItem == nullptr) { - return false; - } - if (!Writing(*appItem, data)) { - return false; - } - auto sdRecord = static_cast<SystemDefinedRecord *>(input.get()); - if (sdRecord == nullptr) { - return false; - } - return Writing(*sdRecord, data); - } - case UDType::SYSTEM_DEFINED_PIXEL_MAP: { - auto pixelMap = static_cast<SystemDefinedPixelMap *>(input.get()); - if (pixelMap == nullptr) { - return false; - } - if (!Writing(*pixelMap, data)) { - return false; - } - auto sdRecord = static_cast<SystemDefinedRecord *>(input.get()); - if (sdRecord == nullptr) { - return false; - } - return Writing(*sdRecord, data); - } - case UDType::APPLICATION_DEFINED_RECORD: { - auto record = static_cast<ApplicationDefinedRecord *>(input.get()); - if (record == nullptr) { - return false; - } - return Writing(*record, data); - } - default: { - return false; - } - } -} - +bool Writing(const std::shared_ptr<UnifiedRecord> &input, TLVObject &data); template<> -bool Reading(std::shared_ptr<UnifiedRecord> &output, TLVObject &data) -{ - UDType type ; - if (!Reading(type, data)) { - return false; - } - std::string uid; - if (!Reading(uid, data)) { - return false; - } - switch (type) { - case UDType::TEXT: { - std::shared_ptr<Text> text = std::make_shared<Text>(); - if (!Reading(*text, data)) { - return false; - } - output = text; - break; - } - case UDType::PLAIN_TEXT: { - std::shared_ptr<PlainText> plainText = std::make_shared<PlainText>(); - if (!Reading(*plainText, data)) { - return false; - } - output = plainText; - break; - } - case UDType::HTML: { - std::shared_ptr<Html> html = std::make_shared<Html>(); - if (!Reading(*html, data)) { - return false; - } - output = html; - break; - } - case UDType::HYPERLINK: { - std::shared_ptr<Link> link = std::make_shared<Link>(); - if (!Reading(*link, data)) { - return false; - } - output = link; - break; - } - case UDType::FILE: { - std::shared_ptr<File> file = std::make_shared<File>(); - if (!Reading(*file, data)) { - return false; - } - output = file; - break; - } - case UDType::IMAGE: { - std::shared_ptr<Image> image = std::make_shared<Image>(); - if (!Reading(*image, data)) { - return false; - } - output = image; - break; - } - case UDType::VIDEO: { - std::shared_ptr<Video> video = std::make_shared<Video>(); - if (!Reading(*video, data)) { - return false; - } - output = video; - break; - } - case UDType::FOLDER: { - std::shared_ptr<Folder> folder = std::make_shared<Folder>(); - if (!Reading(*folder, data)) { - return false; - } - output = folder; - break; - } - case UDType::SYSTEM_DEFINED_RECORD: { - std::shared_ptr<SystemDefinedRecord> sdRecord = std::make_shared<SystemDefinedRecord>(); - if (!Reading(*sdRecord, data)) { - return false; - } - output = sdRecord; - break; - } - case UDType::SYSTEM_DEFINED_FORM: { - std::shared_ptr<SystemDefinedForm> form = std::make_shared<SystemDefinedForm>(); - if (!Reading(*form, data)) { - return false; - } - output = form; - break; - } - case UDType::SYSTEM_DEFINED_APP_ITEM: { - std::shared_ptr<SystemDefinedAppItem> appItem = std::make_shared<SystemDefinedAppItem>(); - if (!Reading(*appItem, data)) { - return false; - } - output = appItem; - break; - } - case UDType::SYSTEM_DEFINED_PIXEL_MAP: { - std::shared_ptr<SystemDefinedPixelMap> pixelMap = std::make_shared<SystemDefinedPixelMap>(); - if (!Reading(*pixelMap, data)) { - return false; - } - output = pixelMap; - break; - } - case UDType::APPLICATION_DEFINED_RECORD: { - std::shared_ptr<ApplicationDefinedRecord> record = std::make_shared<ApplicationDefinedRecord>(); - if (!Reading(*record, data)) { - return false; - } - output = record; - break; - } - default: { - return false; - } - } - output->SetUid(uid); - output->SetType(type); - return true; -} +bool Reading(std::shared_ptr<UnifiedRecord> &output, TLVObject &data); template<> -bool Writing(const UnifiedKey &input, TLVObject &data) -{ - if (!Writing(input.key, data)) { - return false; - } - if (!Writing(input.intention, data)) { - return false; - } - if (!Writing(input.bundleName, data)) { - return false; - } - if (!Writing(input.groupId, data)) { - return false; - } - return true; -} - +bool Writing(const UnifiedKey &input, TLVObject &data); template<> -bool Reading(UnifiedKey &output, TLVObject &data) -{ - std::string key; - std::string intention; - std::string bundleName; - std::string groupId; - if (!Reading(key, data)) { - return false; - } - if (!Reading(intention, data)) { - return false; - } - if (!Reading(bundleName, data)) { - return false; - } - if (!Reading(groupId, data)) { - return false; - } - output.key = key; - output.intention = intention; - output.bundleName = bundleName; - output.groupId = groupId; - return true; -} +bool Reading(UnifiedKey &output, TLVObject &data); template<> -bool Writing(const Privilege &input, TLVObject &data) -{ - if (!Writing(input.tokenId, data)) { - return false; - } - if (!Writing(input.pid, data)) { - return false; - } - if (!Writing(input.readPermission, data)) { - return false; - } - if (!Writing(input.writePermission, data)) { - return false; - } - return true; -} - +bool Writing(const Privilege &input, TLVObject &data); template<> -bool Reading(Privilege &output, TLVObject &data) -{ - int32_t tokenId; - int32_t pid; - std::string readPermission; - std::string writePermission; - if (!Reading(tokenId, data)) { - return false; - } - if (!Reading(pid, data)) { - return false; - } - if (!Reading(readPermission, data)) { - return false; - } - if (!Reading(writePermission, data)) { - return false; - } - output.tokenId = tokenId; - output.pid = pid; - output.readPermission = readPermission; - output.writePermission = writePermission; - return true; -} +bool Reading(Privilege &output, TLVObject &data); template<> -bool Writing(const DataStatus &input, TLVObject &data) -{ - int32_t status = input; - return Writing(status, data); -} - +bool Writing(const DataStatus &input, TLVObject &data); template<> -bool Reading(DataStatus &output, TLVObject &data) -{ - int32_t status; - if (!Reading(status, data)) { - return false; - } - if (status < DataStatus::WORKING || status >= DataStatus::FADE) { - return false; - } - output = static_cast<DataStatus>(status); - return true; -} +bool Reading(DataStatus &output, TLVObject &data); template<> -bool Writing(const Runtime &input, TLVObject &data) -{ - (void)CountBufferSize(input, data); - data.UpdateSize(); - if (!Writing(input.key, data)) { - return false; - } - if (!Writing(input.isPrivate, data)) { - return false; - } - int32_t size = input.privileges.size(); - if (!Writing(size, data)) { - return false; - } - for (int i = 0; i < size; ++i) { - if (!Writing(input.privileges[i], data)) { - return false; - } - } - if (!Writing(static_cast<int64_t>(input.createTime), data)) { - return false; - } - if (!Writing(input.sourcePackage, data)) { - return false; - } - if (!Writing(input.dataStatus, data)) { - return false; - } - if (!Writing(input.dataVersion, data)) { - return false; - } - if (!Writing(static_cast<int64_t>(input.lastModifiedTime), data)) { - return false; - } - if (!Writing(input.createPackage, data)) { - return false; - } - if (!Writing(input.deviceId, data)) { - return false; - } - return true; -} - +bool Writing(const Runtime &input, TLVObject &data); template<> -bool Reading(Runtime &output, TLVObject &data) -{ - UnifiedKey key; - bool isPrivate; - int32_t size; - std::vector<Privilege> privileges; - int64_t createTime; - std::string sourcePackage; - DataStatus dataStatus; - int32_t dataVersion; - int64_t lastModifiedTime; - std::string createPackage; - std::string deviceId; - if (!Reading(key, data)) { - return false; - } - if (!Reading(isPrivate, data)) { - return false; - } - if (!Reading(size, data)) { - return false; - } - for (int i = 0; i < size; ++i) { - Privilege privilege; - if (!Reading(privilege, data)) { - return false; - } - privileges.emplace_back(privilege); - } - if (!Reading(createTime, data)) { - return false; - } - if (!Reading(sourcePackage, data)) { - return false; - } - if (!Reading(dataStatus, data)) { - return false; - } - if (!Reading(dataVersion, data)) { - return false; - } - if (!Reading(lastModifiedTime, data)) { - return false; - } - if (!Reading(createPackage, data)) { - return false; - } - if (!Reading(deviceId, data)) { - return false; - } - output.key = key; - output.isPrivate = isPrivate; - output.privileges = privileges; - output.createTime = createTime; - output.sourcePackage = sourcePackage; - output.dataStatus = dataStatus; - output.dataVersion = dataVersion; - output.lastModifiedTime = lastModifiedTime; - output.createPackage = createPackage; - output.deviceId = deviceId; - return true; -} +bool Reading(Runtime &output, TLVObject &data); } // namespace TLVUtil } // namespace OHOS #endif // UDMF_TLV_UTIL_H \ No newline at end of file diff --git a/framework/innerkitsimpl/test/unittest/udmf_client_test.cpp b/framework/innerkitsimpl/test/unittest/udmf_client_test.cpp index 094f1e55e34bc1e3bfdf9146f70ae048aabdfbef..30a4aab2fdebde5872c1e669497798044d0331d0 100755 --- a/framework/innerkitsimpl/test/unittest/udmf_client_test.cpp +++ b/framework/innerkitsimpl/test/unittest/udmf_client_test.cpp @@ -922,6 +922,72 @@ HWTEST_F(UdmfClientTest, SetData016, TestSize.Level1) LOG_INFO(UDMF_TEST, "SetData016 end."); } +/** +* @tc.name: SetData017 +* @tc.desc: Set one 2MB record of data with valid params and get data +* @tc.type: FUNC +*/ +HWTEST_F(UdmfClientTest, SetData017, TestSize.Level1) +{ + LOG_INFO(UDMF_TEST, "SetData017 begin."); + + CustomOption customOption = { .intention = Intention::UD_INTENTION_DRAG }; + UnifiedData inputData; + std::string key; + UDDetails details; + std::string value; + int64_t maxSize = 512 * 1024; + for (int64_t i = 0; i < maxSize; ++i) { + value += "11"; + } + details.insert({ value, value }); + Text text; + text.SetDetails(details); + std::shared_ptr<UnifiedRecord> record = std::make_shared<Text>(text); + inputData.AddRecord(record); + + auto status = UdmfClient::GetInstance().SetData(customOption, inputData, key); + ASSERT_EQ(status, E_OK); + + QueryOption queryOption = { .key = key }; + UnifiedData outputData; + status = UdmfClient::GetInstance().GetData(queryOption, outputData); + ASSERT_EQ(status, E_OK); + + LOG_INFO(UDMF_TEST, "SetData017 end."); +} + +/** +* @tc.name: SetData018 +* @tc.desc: Set one over 4MB record of data with valid params and get data +* @tc.type: FUNC +*/ +HWTEST_F(UdmfClientTest, SetData018, TestSize.Level1) +{ + LOG_INFO(UDMF_TEST, "SetData018 begin."); + + CustomOption customOption = { .intention = Intention::UD_INTENTION_DRAG }; + UnifiedData inputData; + std::string key; + UDDetails details; + std::string value; + int64_t maxSize = 512 * 1024; + for (int64_t i = 0; i < maxSize; ++i) { + value += "1111"; + } + details.insert({ value, value }); + details.insert({ "udmf_key", "udmf_value" }); + Text text; + text.SetDetails(details); + std::shared_ptr<UnifiedRecord> record = std::make_shared<Text>(text); + inputData.AddRecord(record); + + auto status = UdmfClient::GetInstance().SetData(customOption, inputData, key); + ASSERT_EQ(status, E_INVALID_VALUE); + + LOG_INFO(UDMF_TEST, "SetData018 end."); +} + /** * @tc.name: GetData001 * @tc.desc: Get data with invalid key diff --git a/framework/service/udmf_service_proxy.cpp b/framework/service/udmf_service_proxy.cpp index 6d8c03ae1389aa16fc3d6e976a9c0d056f3c5a33..dbde67e08733b10bfeccc0f47769dd59710fddde 100755 --- a/framework/service/udmf_service_proxy.cpp +++ b/framework/service/udmf_service_proxy.cpp @@ -19,6 +19,7 @@ #include "preprocess_utils.h" #include "udmf_types_util.h" +#include "tlv_util.h" namespace OHOS { namespace UDMF { @@ -47,7 +48,7 @@ namespace UDMF { __status; \ }) -static constexpr int32_t UDMF_MAX_DATA_SIZE = 5 * 1024 * 1024; +static constexpr int32_t UDMF_MAX_DATA_SIZE = 4 * 1024 * 1024; UdmfServiceProxy::UdmfServiceProxy(const sptr<IRemoteObject> &object) : IRemoteProxy<IUdmfService>(object) { } @@ -67,13 +68,41 @@ int32_t UdmfServiceProxy::SetData(CustomOption &option, UnifiedData &unifiedData LOG_ERROR(UDMF_SERVICE, "Invalid data!"); return E_INVALID_VALUE; } + MessageParcel request; + if (!request.WriteInterfaceToken(GetDescriptor())) { + return E_WRITE_PARCEL_ERROR; + } + if (!ITypesUtil::Marshal(request, option)) { + return E_WRITE_PARCEL_ERROR; + } + auto size = unifiedData.GetRecords().size(); + if (!request.WriteInt32(static_cast<int32_t>(size))) { + return E_WRITE_PARCEL_ERROR; + } + for (const auto &record : unifiedData.GetRecords()) { + if (record == nullptr) { + continue; + } + std::vector<uint8_t> recordBytes; + auto recordTlv = TLVObject(recordBytes); + if (!TLVUtil::Writing(record, recordTlv)) { + LOG_ERROR(UDMF_SERVICE, "TLVUtil writing unified record failed."); + return E_WRITE_PARCEL_ERROR; + } + if (!request.WriteInt32(static_cast<int32_t>(recordBytes.size())) || + !request.WriteRawData(recordBytes.data(), recordBytes.size())) { + return E_WRITE_PARCEL_ERROR; + } + } MessageParcel reply; - int32_t status = IPC_SEND(SET_DATA, reply, option, unifiedData); - if (status != E_OK) { - LOG_ERROR(UDMF_SERVICE, "status:0x%{public}x, key:%{public}s", status, key.c_str()); - return status; + MessageOption messageOption; + int error = Remote()->SendRequest(SET_DATA, request, reply, messageOption); + if (error != 0) { + LOG_ERROR(UDMF_SERVICE, "SendRequest failed, error:%d", error); + return E_WRITE_PARCEL_ERROR; } - ITypesUtil::Unmarshal(reply, key); + int32_t status; + ITypesUtil::Unmarshal(reply, status, key); LOG_DEBUG(UDMF_SERVICE, "end."); return status; } @@ -92,7 +121,24 @@ int32_t UdmfServiceProxy::GetData(QueryOption &query, UnifiedData &unifiedData) LOG_ERROR(UDMF_SERVICE, "status:0x%{public}x, key:%{public}s", status, query.key.c_str()); return status; } - ITypesUtil::Unmarshal(reply, unifiedData); + + int32_t count = reply.ReadInt32(); + for (int32_t index = 0; index < count; ++index) { + std::shared_ptr<UnifiedRecord> record; + auto size = reply.ReadInt32(); + if (size == 0) { + continue; + } + const uint8_t *rawData = reinterpret_cast<const uint8_t *>(reply.ReadRawData(size)); + std::vector<uint8_t> recordBytes(rawData, rawData + size); + auto recordTlv = TLVObject(recordBytes); + if (!TLVUtil::Reading(record, recordTlv)) { + LOG_ERROR(UDMF_SERVICE, "Unmarshall unified record failed."); + return IPC_STUB_INVALID_DATA_ERR; + } + unifiedData.AddRecord(record); + } + LOG_DEBUG(UDMF_SERVICE, "end."); return status; } @@ -150,7 +196,6 @@ int32_t UdmfServiceProxy::Sync(const QueryOption &query, const std::vector<std:: return status; } - int32_t UdmfServiceProxy::SendRequest( IUdmfService::FCode code, MessageParcel &data, MessageParcel &reply, MessageOption &option) { diff --git a/interfaces/innerkits/BUILD.gn b/interfaces/innerkits/BUILD.gn index f52bca9760f051ccad328d15b950c3adf3d3b5a9..011edaed4a913f7113555eefc291d4aec41fa51c 100755 --- a/interfaces/innerkits/BUILD.gn +++ b/interfaces/innerkits/BUILD.gn @@ -36,6 +36,7 @@ config("udmf_client_config") { ohos_shared_library("udmf_client") { sources = [ "${udmf_framework_path}/common/anonymous.cpp", + "${udmf_framework_path}/common/tlv_util.cpp", "${udmf_framework_path}/common/udmf_types_util.cpp", "${udmf_framework_path}/innerkitsimpl/client/udmf_client.cpp", "${udmf_framework_path}/innerkitsimpl/common/unified_key.cpp", @@ -55,18 +56,18 @@ ohos_shared_library("udmf_client") { "${udmf_framework_path}/innerkitsimpl/data/unified_data.cpp", "${udmf_framework_path}/innerkitsimpl/data/unified_record.cpp", "${udmf_framework_path}/innerkitsimpl/data/video.cpp", + "${udmf_framework_path}/manager/data_manager.cpp", + "${udmf_framework_path}/manager/lifecycle/clean_after_get.cpp", + "${udmf_framework_path}/manager/lifecycle/clean_on_startup.cpp", + "${udmf_framework_path}/manager/lifecycle/clean_on_timeout.cpp", + "${udmf_framework_path}/manager/lifecycle/lifecycle_manager.cpp", + "${udmf_framework_path}/manager/lifecycle/lifecycle_policy.cpp", "${udmf_framework_path}/manager/permission/checker_manager.cpp", "${udmf_framework_path}/manager/permission/data_checker.cpp", "${udmf_framework_path}/manager/permission/uri_permission_manager.cpp", "${udmf_framework_path}/manager/preprocess/preprocess_utils.cpp", - "${udmf_framework_path}/manager/store/store_cache.cpp", "${udmf_framework_path}/manager/store/runtime_store.cpp", - "${udmf_framework_path}/manager/data_manager.cpp", - "${udmf_framework_path}/manager/lifecycle/lifecycle_manager.cpp", - "${udmf_framework_path}/manager/lifecycle/lifecycle_policy.cpp", - "${udmf_framework_path}/manager/lifecycle/clean_on_startup.cpp", - "${udmf_framework_path}/manager/lifecycle/clean_after_get.cpp", - "${udmf_framework_path}/manager/lifecycle/clean_on_timeout.cpp", + "${udmf_framework_path}/manager/store/store_cache.cpp", "${udmf_framework_path}/service/udmf_service_client.cpp", "${udmf_framework_path}/service/udmf_service_proxy.cpp", ] @@ -74,8 +75,8 @@ ohos_shared_library("udmf_client") { public_configs = [ ":udmf_client_config" ] deps = [ - "//foundation/filemanagement/app_file_service/interfaces/innerkits/remote_file_share/native:remote_file_share_native", "//foundation/ability/ability_runtime/interfaces/inner_api/uri_permission:uri_permission_mgr", + "//foundation/filemanagement/app_file_service/interfaces/innerkits/remote_file_share/native:remote_file_share_native", ] external_deps = [ diff --git a/service/src/udmf_service_stub.cpp b/service/src/udmf_service_stub.cpp index b9b76a8aefed11897e1c0348ff992959fd5b4d99..fffe58cb89b597a36a99bba8f644937d3e8d7c4f 100755 --- a/service/src/udmf_service_stub.cpp +++ b/service/src/udmf_service_stub.cpp @@ -24,6 +24,7 @@ #include "udmf_types_util.h" #include "unified_data.h" #include "unified_meta.h" +#include "tlv_util.h" namespace OHOS { namespace UDMF { @@ -68,11 +69,30 @@ int32_t UdmfServiceStub::OnSetData(MessageParcel &data, MessageParcel &reply) { LOG_INFO(UDMF_SERVICE, "start"); CustomOption customOption{}; - UnifiedData unifiedData; - if (!ITypesUtil::Unmarshal(data, customOption, unifiedData)) { + if (!ITypesUtil::Unmarshal(data, customOption)) { LOG_ERROR(UDMF_SERVICE, "Unmarshal option"); return IPC_STUB_INVALID_DATA_ERR; } + UnifiedData unifiedData; + int32_t count = data.ReadInt32(); + for (int32_t index = 0; index < count; ++index) { + std::shared_ptr<UnifiedRecord> record; + int32_t size = data.ReadInt32(); + if (size == 0) { + continue; + } + const uint8_t *rawData = reinterpret_cast<const uint8_t *>(data.ReadRawData(size)); + if (rawData == nullptr) { + return IPC_STUB_INVALID_DATA_ERR; + } + std::vector<uint8_t> recordBytes(rawData, rawData + size); + auto recordTlv = TLVObject(recordBytes);; + if (!TLVUtil::Reading(record, recordTlv)) { + LOG_ERROR(UDMF_SERVICE, "Unmarshall unified record failed."); + return IPC_STUB_INVALID_DATA_ERR; + } + unifiedData.AddRecord(record); + } int32_t token = static_cast<int>(IPCSkeleton::GetCallingTokenID()); customOption.tokenId = token; std::string key; @@ -98,10 +118,29 @@ int32_t UdmfServiceStub::OnGetData(MessageParcel &data, MessageParcel &reply) query.pid = pid; UnifiedData unifiedData; int32_t status = GetData(query, unifiedData); - if (!ITypesUtil::Marshal(reply, status, unifiedData)) { + if (!ITypesUtil::Marshal(reply, status)) { LOG_ERROR(UDMF_SERVICE, "Marshal ud data, key: %{public}s", query.key.c_str()); return IPC_STUB_WRITE_PARCEL_ERR; } + auto size = unifiedData.GetRecords().size(); + if (!reply.WriteInt32(static_cast<int32_t>(size))) { + return E_WRITE_PARCEL_ERROR; + } + for (const auto &record : unifiedData.GetRecords()) { + if (record == nullptr) { + continue; + } + std::vector<uint8_t> recordBytes; + auto recordTlv = TLVObject(recordBytes); + if (!TLVUtil::Writing(record, recordTlv)) { + LOG_ERROR(UDMF_SERVICE, "TLVUtil writing unified record failed."); + return E_WRITE_PARCEL_ERROR; + } + if (!reply.WriteInt32(static_cast<int32_t>(recordBytes.size())) || + !reply.WriteRawData(recordBytes.data(), recordBytes.size())) { + return E_WRITE_PARCEL_ERROR; + } + } return E_OK; }