/* * 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 "message_parcel_helper.h" #include #include #include #include "update_helper.h" #include "update_log.h" namespace OHOS { namespace UpdateEngine { static constexpr int32_t MAX_VECTOR_SIZE = 128; void ReadErrorMessages(MessageParcel &reply, std::vector &errorMessages) { int32_t size = reply.ReadInt32(); // codecheck warning fix if (size > MAX_VECTOR_SIZE) { ENGINE_LOGE("ReadErrorMessages size is over MAX_VECTOR_SIZE, size=%{public}d", size); return; } for (size_t i = 0; i < static_cast(size); i++) { ErrorMessage errorMsg; errorMsg.errorCode = reply.ReadInt32(); errorMsg.errorMessage = Str16ToStr8(reply.ReadString16()); errorMessages.push_back(errorMsg); } } void WriteErrorMessages(MessageParcel &data, const std::vector errorMessages) { data.WriteInt32(static_cast(errorMessages.size())); for (size_t i = 0; i < errorMessages.size(); i++) { data.WriteInt32(errorMessages[i].errorCode); data.WriteString16(Str8ToStr16(errorMessages[i].errorMessage)); } } void ReadComponentDescriptions(MessageParcel &reply, std::vector &componentDescriptions) { int32_t size = reply.ReadInt32(); if (size > MAX_VECTOR_SIZE) { ENGINE_LOGE("ReadComponentDescriptions size is over MAX_VECTOR_SIZE, size=%{public}d", size); return; } for (size_t i = 0; i < static_cast(size); i++) { ComponentDescription componentDescription; componentDescription.componentId = Str16ToStr8(reply.ReadString16()); componentDescription.descriptionInfo.descriptionType = static_cast(reply.ReadUint32()); componentDescription.descriptionInfo.content = Str16ToStr8(reply.ReadString16()); componentDescriptions.push_back(componentDescription); } } void WriteComponentDescriptions(MessageParcel &data, const std::vector &componentDescriptions) { data.WriteInt32(static_cast(componentDescriptions.size())); for (size_t i = 0; i < componentDescriptions.size(); i++) { data.WriteString16(Str8ToStr16(componentDescriptions[i].componentId)); data.WriteUint32(static_cast(componentDescriptions[i].descriptionInfo.descriptionType)); data.WriteString16(Str8ToStr16(componentDescriptions[i].descriptionInfo.content)); } } int32_t MessageParcelHelper::ReadUpgradeInfo(MessageParcel &reply, UpgradeInfo &info) { info.upgradeApp = Str16ToStr8(reply.ReadString16()); info.businessType.vendor = Str16ToStr8(reply.ReadString16()); info.businessType.subType = static_cast(reply.ReadInt32()); info.upgradeDevId = Str16ToStr8(reply.ReadString16()); info.controlDevId = Str16ToStr8(reply.ReadString16()); return 0; } int32_t MessageParcelHelper::WriteUpgradeInfo(MessageParcel &data, const UpgradeInfo &info) { data.WriteString16(Str8ToStr16(info.upgradeApp)); data.WriteString16(Str8ToStr16(info.businessType.vendor)); data.WriteInt32(static_cast(info.businessType.subType)); data.WriteString16(Str8ToStr16(info.upgradeDevId)); data.WriteString16(Str8ToStr16(info.controlDevId)); return 0; } int32_t MessageParcelHelper::ReadVersionDescriptionInfo( MessageParcel &reply, VersionDescriptionInfo &versionDescriptionInfo) { ReadComponentDescriptions(reply, versionDescriptionInfo.componentDescriptions); return 0; } int32_t MessageParcelHelper::WriteVersionDescriptionInfo( MessageParcel &data, const VersionDescriptionInfo &versionDescriptionInfo) { WriteComponentDescriptions(data, versionDescriptionInfo.componentDescriptions); return 0; } int32_t MessageParcelHelper::ReadBusinessError(MessageParcel &reply, BusinessError &businessError) { businessError.message = Str16ToStr8(reply.ReadString16()); businessError.errorNum = static_cast(reply.ReadInt32()); ReadErrorMessages(reply, businessError.data); return 0; } int32_t MessageParcelHelper::WriteBusinessError(MessageParcel &data, const BusinessError &businessError) { data.WriteString16(Str8ToStr16(businessError.message)); data.WriteInt32(static_cast(businessError.errorNum)); WriteErrorMessages(data, businessError.data); return 0; } void ReadVersionComponents(MessageParcel &reply, std::vector &versionComponents) { int32_t size = reply.ReadInt32(); if (size > MAX_VECTOR_SIZE) { ENGINE_LOGE("ReadVersionComponents size is over MAX_VECTOR_SIZE, size=%{public}d", size); return; } for (size_t i = 0; i < static_cast(size); i++) { VersionComponent versionComponent; versionComponent.componentId = Str16ToStr8(reply.ReadString16()); versionComponent.componentType = reply.ReadInt32(); versionComponent.upgradeAction = Str16ToStr8(reply.ReadString16()); versionComponent.displayVersion = Str16ToStr8(reply.ReadString16()); versionComponent.innerVersion = Str16ToStr8(reply.ReadString16()); versionComponent.size = static_cast(reply.ReadUint32()); versionComponent.effectiveMode = static_cast(reply.ReadUint32()); versionComponent.descriptionInfo.descriptionType = static_cast(reply.ReadUint32()); versionComponent.descriptionInfo.content = Str16ToStr8(reply.ReadString16()); versionComponent.componentExtra = Str16ToStr8(reply.ReadString16()); versionComponents.push_back(versionComponent); } } void WriteVersionComponents(MessageParcel &data, const std::vector &versionComponents) { data.WriteInt32(static_cast(versionComponents.size())); for (size_t i = 0; i < versionComponents.size(); i++) { const VersionComponent *versionComponent = &versionComponents[i]; data.WriteString16(Str8ToStr16(versionComponent->componentId)); data.WriteInt32(versionComponent->componentType); data.WriteString16(Str8ToStr16(versionComponent->upgradeAction)); data.WriteString16(Str8ToStr16(versionComponent->displayVersion)); data.WriteString16(Str8ToStr16(versionComponent->innerVersion)); data.WriteUint32(static_cast(versionComponent->size)); data.WriteUint32(static_cast(versionComponent->effectiveMode)); data.WriteUint32(static_cast(versionComponent->descriptionInfo.descriptionType)); data.WriteString16(Str8ToStr16(versionComponent->descriptionInfo.content)); data.WriteString16(Str8ToStr16(versionComponent->componentExtra)); } } void ReadNewVersionInfoEx(MessageParcel &reply, NewVersionInfo &newVersionInfo) { newVersionInfo.versionDigestInfo.versionDigest = Str16ToStr8(reply.ReadString16()); ReadVersionComponents(reply, newVersionInfo.versionComponents); } void WriteNewVersionInfoEx(MessageParcel &data, const NewVersionInfo &newVersionInfo) { data.WriteString16(Str8ToStr16(newVersionInfo.versionDigestInfo.versionDigest)); WriteVersionComponents(data, newVersionInfo.versionComponents); } int32_t MessageParcelHelper::ReadCheckResult(MessageParcel &reply, CheckResult &checkResult) { checkResult.isExistNewVersion = reply.ReadBool(); ReadNewVersionInfoEx(reply, checkResult.newVersionInfo); return 0; } int32_t MessageParcelHelper::WriteCheckResult(MessageParcel &data, const CheckResult &checkResult) { data.WriteBool(checkResult.isExistNewVersion); WriteNewVersionInfoEx(data, checkResult.newVersionInfo); return 0; } int32_t MessageParcelHelper::ReadNewVersionInfo(MessageParcel &reply, NewVersionInfo &newVersionInfo) { ReadNewVersionInfoEx(reply, newVersionInfo); return 0; } int32_t MessageParcelHelper::WriteNewVersionInfo(MessageParcel &data, const NewVersionInfo &newVersionInfo) { WriteNewVersionInfoEx(data, newVersionInfo); return 0; } int32_t MessageParcelHelper::ReadCurrentVersionInfo(MessageParcel &reply, CurrentVersionInfo &info) { info.osVersion = Str16ToStr8(reply.ReadString16()); info.deviceName = Str16ToStr8(reply.ReadString16()); ReadVersionComponents(reply, info.versionComponents); return 0; } int32_t MessageParcelHelper::WriteCurrentVersionInfo(MessageParcel &data, const CurrentVersionInfo &info) { data.WriteString16(Str8ToStr16(info.osVersion)); data.WriteString16(Str8ToStr16(info.deviceName)); WriteVersionComponents(data, info.versionComponents); return 0; } void ReadTaskBody(MessageParcel &reply, TaskBody &taskBody) { taskBody.versionDigestInfo.versionDigest = Str16ToStr8(reply.ReadString16()); taskBody.status = static_cast(reply.ReadInt32()); taskBody.subStatus = reply.ReadInt32(); taskBody.progress = reply.ReadInt32(); taskBody.installMode = reply.ReadInt32(); ReadErrorMessages(reply, taskBody.errorMessages); ReadVersionComponents(reply, taskBody.versionComponents); } void WriteTaskBody(MessageParcel &data, const TaskBody &taskBody) { data.WriteString16(Str8ToStr16(taskBody.versionDigestInfo.versionDigest)); data.WriteInt32(static_cast(taskBody.status)); data.WriteInt32(taskBody.subStatus); data.WriteInt32(taskBody.progress); data.WriteInt32(taskBody.installMode); WriteErrorMessages(data, taskBody.errorMessages); WriteVersionComponents(data, taskBody.versionComponents); } int32_t MessageParcelHelper::ReadTaskInfo(MessageParcel &reply, TaskInfo &info) { info.existTask = reply.ReadBool(); ReadTaskBody(reply, info.taskBody); return 0; } int32_t MessageParcelHelper::WriteTaskInfo(MessageParcel &data, const TaskInfo &info) { data.WriteBool(info.existTask); WriteTaskBody(data, info.taskBody); return 0; } int32_t MessageParcelHelper::ReadUpgradePolicy(MessageParcel &reply, UpgradePolicy &policy) { policy.downloadStrategy = static_cast(reply.ReadBool()); policy.autoUpgradeStrategy = static_cast(reply.ReadBool()); size_t size = static_cast(reply.ReadInt32()); size_t arraySize = COUNT_OF(policy.autoUpgradePeriods); if (size > MAX_VECTOR_SIZE) { ENGINE_LOGE("ReadUpgradePolicy size is over MAX_VECTOR_SIZE, size=%{public}zu", size); return -1; } for (size_t i = 0; (i < size) && (i < arraySize); i++) { policy.autoUpgradePeriods[i].start = reply.ReadUint32(); policy.autoUpgradePeriods[i].end = reply.ReadUint32(); } return 0; } int32_t MessageParcelHelper::WriteUpgradePolicy(MessageParcel &data, const UpgradePolicy &policy) { data.WriteBool(policy.downloadStrategy); data.WriteBool(policy.autoUpgradeStrategy); int32_t size = static_cast(COUNT_OF(policy.autoUpgradePeriods)); data.WriteInt32(size); for (int32_t i = 0; i < size; i++) { data.WriteUint32(policy.autoUpgradePeriods[i].start); data.WriteUint32(policy.autoUpgradePeriods[i].end); } return 0; } int32_t MessageParcelHelper::ReadEventInfo(MessageParcel &reply, EventInfo &eventInfo) { eventInfo.eventId = static_cast(reply.ReadUint32()); ReadTaskBody(reply, eventInfo.taskBody); return 0; } int32_t MessageParcelHelper::WriteEventInfo(MessageParcel &data, const EventInfo &eventInfo) { data.WriteUint32(static_cast(eventInfo.eventId)); WriteTaskBody(data, eventInfo.taskBody); return 0; } int32_t MessageParcelHelper::ReadVersionDigestInfo(MessageParcel &reply, VersionDigestInfo &versionDigestInfo) { versionDigestInfo.versionDigest = Str16ToStr8(reply.ReadString16()); return 0; } int32_t MessageParcelHelper::WriteVersionDigestInfo(MessageParcel &data, const VersionDigestInfo &versionDigestInfo) { data.WriteString16(Str8ToStr16(versionDigestInfo.versionDigest)); return 0; } int32_t MessageParcelHelper::ReadDescriptionOptions(MessageParcel &reply, DescriptionOptions &descriptionOptions) { descriptionOptions.format = static_cast(reply.ReadUint32()); descriptionOptions.language = Str16ToStr8(reply.ReadString16()); return 0; } int32_t MessageParcelHelper::WriteDescriptionOptions(MessageParcel &data, const DescriptionOptions &descriptionOptions) { data.WriteUint32(static_cast(descriptionOptions.format)); data.WriteString16(Str8ToStr16(descriptionOptions.language)); return 0; } int32_t MessageParcelHelper::ReadDownloadOptions(MessageParcel &reply, DownloadOptions &downloadOptions) { downloadOptions.allowNetwork = static_cast(reply.ReadUint32()); downloadOptions.order = static_cast(reply.ReadUint32()); return 0; } int32_t MessageParcelHelper::WriteDownloadOptions(MessageParcel &data, const DownloadOptions &downloadOptions) { data.WriteUint32(static_cast(downloadOptions.allowNetwork)); data.WriteUint32(static_cast(downloadOptions.order)); return 0; } int32_t MessageParcelHelper::ReadPauseDownloadOptions(MessageParcel &reply, PauseDownloadOptions &pauseDownloadOptions) { pauseDownloadOptions.isAllowAutoResume = reply.ReadBool(); return 0; } int32_t MessageParcelHelper::WritePauseDownloadOptions( MessageParcel &data, const PauseDownloadOptions &pauseDownloadOptions) { data.WriteBool(pauseDownloadOptions.isAllowAutoResume); return 0; } int32_t MessageParcelHelper::ReadResumeDownloadOptions( MessageParcel &reply, ResumeDownloadOptions &resumeDownloadOptions) { resumeDownloadOptions.allowNetwork = static_cast(reply.ReadUint32()); return 0; } int32_t MessageParcelHelper::WriteResumeDownloadOptions( MessageParcel &data, const ResumeDownloadOptions &resumeDownloadOptions) { data.WriteUint32(static_cast(resumeDownloadOptions.allowNetwork)); return 0; } int32_t MessageParcelHelper::ReadUpgradeOptions(MessageParcel &reply, UpgradeOptions &upgradeOptions) { upgradeOptions.order = static_cast(reply.ReadUint32()); return 0; } int32_t MessageParcelHelper::WriteUpgradeOptions(MessageParcel &data, const UpgradeOptions &upgradeOptions) { data.WriteUint32(static_cast(upgradeOptions.order)); return 0; } int32_t MessageParcelHelper::ReadClearOptions(MessageParcel &reply, ClearOptions &clearOptions) { clearOptions.status = static_cast(reply.ReadUint32()); return 0; } int32_t MessageParcelHelper::WriteClearOptions(MessageParcel &data, const ClearOptions &clearOptions) { data.WriteUint32(static_cast(clearOptions.status)); return 0; } } // namespace UpdateEngine } // namespace OHOS