1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "message_parcel_helper.h"
17
18 #include <cstring>
19 #include <string>
20 #include <string_ex.h>
21 #include <vector>
22
23 #include "update_log.h"
24
25 namespace OHOS {
26 namespace UpdateEngine {
27 static constexpr int32_t MAX_VECTOR_SIZE = 128;
28
ReadErrorMessages(MessageParcel & reply,std::vector<ErrorMessage> & errorMessages)29 void ReadErrorMessages(MessageParcel &reply, std::vector<ErrorMessage> &errorMessages)
30 {
31 int32_t size = reply.ReadInt32();
32 // codecheck warning fix
33 if (size > MAX_VECTOR_SIZE) {
34 ENGINE_LOGE("ReadErrorMessages size is over MAX_VECTOR_SIZE, size=%{public}d", size);
35 return;
36 }
37 for (size_t i = 0; i < static_cast<size_t>(size); i++) {
38 ErrorMessage errorMsg;
39 errorMsg.errorCode = reply.ReadInt32();
40 errorMsg.errorMessage = Str16ToStr8(reply.ReadString16());
41 errorMessages.push_back(errorMsg);
42 }
43 }
44
WriteErrorMessages(MessageParcel & data,const std::vector<ErrorMessage> & errorMessages)45 void WriteErrorMessages(MessageParcel &data, const std::vector<ErrorMessage> &errorMessages)
46 {
47 data.WriteInt32(static_cast<int32_t>(errorMessages.size()));
48 for (size_t i = 0; i < errorMessages.size(); i++) {
49 data.WriteInt32(errorMessages[i].errorCode);
50 data.WriteString16(Str8ToStr16(errorMessages[i].errorMessage));
51 }
52 }
53
ReadComponentDescriptions(MessageParcel & reply,std::vector<ComponentDescription> & componentDescriptions)54 void ReadComponentDescriptions(MessageParcel &reply, std::vector<ComponentDescription> &componentDescriptions)
55 {
56 int32_t size = reply.ReadInt32();
57 if (size > MAX_VECTOR_SIZE) {
58 ENGINE_LOGE("ReadComponentDescriptions size is over MAX_VECTOR_SIZE, size=%{public}d", size);
59 return;
60 }
61 for (size_t i = 0; i < static_cast<size_t>(size); i++) {
62 ComponentDescription componentDescription;
63 componentDescription.componentId = Str16ToStr8(reply.ReadString16());
64 componentDescription.descriptionInfo.descriptionType = static_cast<DescriptionType>(reply.ReadUint32());
65 componentDescription.descriptionInfo.content = Str16ToStr8(reply.ReadString16());
66 componentDescription.notifyDescriptionInfo.descriptionType = static_cast<DescriptionType>(reply.ReadUint32());
67 componentDescription.notifyDescriptionInfo.content = Str16ToStr8(reply.ReadString16());
68 componentDescriptions.push_back(componentDescription);
69 }
70 }
71
WriteComponentDescriptions(MessageParcel & data,const std::vector<ComponentDescription> & componentDescriptions)72 void WriteComponentDescriptions(MessageParcel &data, const std::vector<ComponentDescription> &componentDescriptions)
73 {
74 data.WriteInt32(static_cast<int32_t>(componentDescriptions.size()));
75 for (size_t i = 0; i < componentDescriptions.size(); i++) {
76 data.WriteString16(Str8ToStr16(componentDescriptions[i].componentId));
77 data.WriteUint32(static_cast<uint32_t>(componentDescriptions[i].descriptionInfo.descriptionType));
78 data.WriteString16(Str8ToStr16(componentDescriptions[i].descriptionInfo.content));
79 data.WriteUint32(static_cast<uint32_t>(componentDescriptions[i].notifyDescriptionInfo.descriptionType));
80 data.WriteString16(Str8ToStr16(componentDescriptions[i].notifyDescriptionInfo.content));
81 }
82 }
83
ReadUpgradeInfo(MessageParcel & reply,UpgradeInfo & info)84 int32_t MessageParcelHelper::ReadUpgradeInfo(MessageParcel &reply, UpgradeInfo &info)
85 {
86 info.upgradeApp = Str16ToStr8(reply.ReadString16());
87 info.businessType.vendor = Str16ToStr8(reply.ReadString16());
88 info.businessType.subType = static_cast<BusinessSubType>(reply.ReadInt32());
89 info.upgradeDevId = Str16ToStr8(reply.ReadString16());
90 info.controlDevId = Str16ToStr8(reply.ReadString16());
91 info.processId = reply.ReadInt32();
92 info.deviceType = static_cast<DeviceType>(reply.ReadInt32());
93 return 0;
94 }
95
WriteUpgradeInfo(MessageParcel & data,const UpgradeInfo & info)96 int32_t MessageParcelHelper::WriteUpgradeInfo(MessageParcel &data, const UpgradeInfo &info)
97 {
98 data.WriteString16(Str8ToStr16(info.upgradeApp));
99 data.WriteString16(Str8ToStr16(info.businessType.vendor));
100 data.WriteInt32(static_cast<int32_t>(info.businessType.subType));
101 data.WriteString16(Str8ToStr16(info.upgradeDevId));
102 data.WriteString16(Str8ToStr16(info.controlDevId));
103 data.WriteInt32(info.processId);
104 data.WriteInt32(static_cast<int32_t>(info.deviceType));
105 return 0;
106 }
107
ReadVersionDescriptionInfo(MessageParcel & reply,VersionDescriptionInfo & versionDescriptionInfo)108 int32_t MessageParcelHelper::ReadVersionDescriptionInfo(
109 MessageParcel &reply, VersionDescriptionInfo &versionDescriptionInfo)
110 {
111 ReadComponentDescriptions(reply, versionDescriptionInfo.componentDescriptions);
112 return 0;
113 }
114
WriteVersionDescriptionInfo(MessageParcel & data,const VersionDescriptionInfo & versionDescriptionInfo)115 int32_t MessageParcelHelper::WriteVersionDescriptionInfo(
116 MessageParcel &data, const VersionDescriptionInfo &versionDescriptionInfo)
117 {
118 WriteComponentDescriptions(data, versionDescriptionInfo.componentDescriptions);
119 return 0;
120 }
121
ReadBusinessError(MessageParcel & reply,BusinessError & businessError)122 int32_t MessageParcelHelper::ReadBusinessError(MessageParcel &reply, BusinessError &businessError)
123 {
124 businessError.message = Str16ToStr8(reply.ReadString16());
125 businessError.errorNum = static_cast<CallResult>(reply.ReadInt32());
126 ReadErrorMessages(reply, businessError.data);
127 return 0;
128 }
129
WriteBusinessError(MessageParcel & data,const BusinessError & businessError)130 int32_t MessageParcelHelper::WriteBusinessError(MessageParcel &data, const BusinessError &businessError)
131 {
132 data.WriteString16(Str8ToStr16(businessError.message));
133 data.WriteInt32(static_cast<int32_t>(businessError.errorNum));
134 WriteErrorMessages(data, businessError.data);
135 return 0;
136 }
137
ReadVersionComponents(MessageParcel & reply,std::vector<VersionComponent> & versionComponents)138 void ReadVersionComponents(MessageParcel &reply, std::vector<VersionComponent> &versionComponents)
139 {
140 int32_t size = reply.ReadInt32();
141 if (size > MAX_VECTOR_SIZE) {
142 ENGINE_LOGE("ReadVersionComponents size is over MAX_VECTOR_SIZE, size=%{public}d", size);
143 return;
144 }
145 for (size_t i = 0; i < static_cast<size_t>(size); i++) {
146 VersionComponent versionComponent;
147 versionComponent.componentId = Str16ToStr8(reply.ReadString16());
148 versionComponent.componentType = reply.ReadInt32();
149 versionComponent.upgradeAction = Str16ToStr8(reply.ReadString16());
150 versionComponent.displayVersion = Str16ToStr8(reply.ReadString16());
151 versionComponent.innerVersion = Str16ToStr8(reply.ReadString16());
152 versionComponent.size = static_cast<size_t>(reply.ReadUint64());
153 versionComponent.effectiveMode = static_cast<size_t>(reply.ReadUint32());
154
155 versionComponent.descriptionInfo.descriptionType = static_cast<DescriptionType>(reply.ReadUint32());
156 versionComponent.descriptionInfo.content = Str16ToStr8(reply.ReadString16());
157 versionComponent.componentExtra = Str16ToStr8(reply.ReadString16());
158 versionComponents.push_back(versionComponent);
159 }
160 }
161
WriteVersionComponents(MessageParcel & data,const std::vector<VersionComponent> & versionComponents)162 void WriteVersionComponents(MessageParcel &data, const std::vector<VersionComponent> &versionComponents)
163 {
164 data.WriteInt32(static_cast<int32_t>(versionComponents.size()));
165 for (size_t i = 0; i < versionComponents.size(); i++) {
166 const VersionComponent *versionComponent = &versionComponents[i];
167 data.WriteString16(Str8ToStr16(versionComponent->componentId));
168 data.WriteInt32(versionComponent->componentType);
169 data.WriteString16(Str8ToStr16(versionComponent->upgradeAction));
170 data.WriteString16(Str8ToStr16(versionComponent->displayVersion));
171 data.WriteString16(Str8ToStr16(versionComponent->innerVersion));
172 data.WriteUint64(static_cast<uint64_t>(versionComponent->size));
173 data.WriteUint32(static_cast<uint32_t>(versionComponent->effectiveMode));
174
175 data.WriteUint32(static_cast<uint32_t>(versionComponent->descriptionInfo.descriptionType));
176 data.WriteString16(Str8ToStr16(versionComponent->descriptionInfo.content));
177 data.WriteString16(Str8ToStr16(versionComponent->componentExtra));
178 }
179 }
180
ReadNewVersionInfoEx(MessageParcel & reply,NewVersionInfo & newVersionInfo)181 void ReadNewVersionInfoEx(MessageParcel &reply, NewVersionInfo &newVersionInfo)
182 {
183 newVersionInfo.versionDigestInfo.versionDigest = Str16ToStr8(reply.ReadString16());
184 ReadVersionComponents(reply, newVersionInfo.versionComponents);
185 }
186
WriteNewVersionInfoEx(MessageParcel & data,const NewVersionInfo & newVersionInfo)187 void WriteNewVersionInfoEx(MessageParcel &data, const NewVersionInfo &newVersionInfo)
188 {
189 data.WriteString16(Str8ToStr16(newVersionInfo.versionDigestInfo.versionDigest));
190 WriteVersionComponents(data, newVersionInfo.versionComponents);
191 }
192
ReadCheckResult(MessageParcel & reply,CheckResult & checkResult)193 int32_t MessageParcelHelper::ReadCheckResult(MessageParcel &reply, CheckResult &checkResult)
194 {
195 checkResult.isExistNewVersion = reply.ReadBool();
196 ReadNewVersionInfoEx(reply, checkResult.newVersionInfo);
197 return 0;
198 }
199
WriteCheckResult(MessageParcel & data,const CheckResult & checkResult)200 int32_t MessageParcelHelper::WriteCheckResult(MessageParcel &data, const CheckResult &checkResult)
201 {
202 data.WriteBool(checkResult.isExistNewVersion);
203 WriteNewVersionInfoEx(data, checkResult.newVersionInfo);
204 return 0;
205 }
206
ReadNewVersionInfo(MessageParcel & reply,NewVersionInfo & newVersionInfo)207 int32_t MessageParcelHelper::ReadNewVersionInfo(MessageParcel &reply, NewVersionInfo &newVersionInfo)
208 {
209 ReadNewVersionInfoEx(reply, newVersionInfo);
210 return 0;
211 }
212
WriteNewVersionInfo(MessageParcel & data,const NewVersionInfo & newVersionInfo)213 int32_t MessageParcelHelper::WriteNewVersionInfo(MessageParcel &data, const NewVersionInfo &newVersionInfo)
214 {
215 WriteNewVersionInfoEx(data, newVersionInfo);
216 return 0;
217 }
218
ReadCurrentVersionInfo(MessageParcel & reply,CurrentVersionInfo & info)219 int32_t MessageParcelHelper::ReadCurrentVersionInfo(MessageParcel &reply, CurrentVersionInfo &info)
220 {
221 info.osVersion = Str16ToStr8(reply.ReadString16());
222 info.deviceName = Str16ToStr8(reply.ReadString16());
223 ReadVersionComponents(reply, info.versionComponents);
224 return 0;
225 }
226
WriteCurrentVersionInfo(MessageParcel & data,const CurrentVersionInfo & info)227 int32_t MessageParcelHelper::WriteCurrentVersionInfo(MessageParcel &data, const CurrentVersionInfo &info)
228 {
229 data.WriteString16(Str8ToStr16(info.osVersion));
230 data.WriteString16(Str8ToStr16(info.deviceName));
231 WriteVersionComponents(data, info.versionComponents);
232 return 0;
233 }
234
ReadTaskBody(MessageParcel & reply,TaskBody & taskBody)235 void ReadTaskBody(MessageParcel &reply, TaskBody &taskBody)
236 {
237 taskBody.versionDigestInfo.versionDigest = Str16ToStr8(reply.ReadString16());
238 taskBody.status = static_cast<UpgradeStatus>(reply.ReadInt32());
239 taskBody.subStatus = reply.ReadInt32();
240 taskBody.progress = reply.ReadInt32();
241 taskBody.installMode = reply.ReadInt32();
242 ReadErrorMessages(reply, taskBody.errorMessages);
243 ReadVersionComponents(reply, taskBody.versionComponents);
244 }
245
WriteTaskBody(MessageParcel & data,const TaskBody & taskBody)246 void WriteTaskBody(MessageParcel &data, const TaskBody &taskBody)
247 {
248 data.WriteString16(Str8ToStr16(taskBody.versionDigestInfo.versionDigest));
249 data.WriteInt32(static_cast<int32_t>(taskBody.status));
250 data.WriteInt32(taskBody.subStatus);
251 data.WriteInt32(taskBody.progress);
252 data.WriteInt32(taskBody.installMode);
253 WriteErrorMessages(data, taskBody.errorMessages);
254 WriteVersionComponents(data, taskBody.versionComponents);
255 }
256
ReadTaskInfo(MessageParcel & reply,TaskInfo & info)257 int32_t MessageParcelHelper::ReadTaskInfo(MessageParcel &reply, TaskInfo &info)
258 {
259 info.existTask = reply.ReadBool();
260 ReadTaskBody(reply, info.taskBody);
261 return 0;
262 }
263
WriteTaskInfo(MessageParcel & data,const TaskInfo & info)264 int32_t MessageParcelHelper::WriteTaskInfo(MessageParcel &data, const TaskInfo &info)
265 {
266 data.WriteBool(info.existTask);
267 WriteTaskBody(data, info.taskBody);
268 return 0;
269 }
270
ReadUpgradePolicy(MessageParcel & reply,UpgradePolicy & policy)271 int32_t MessageParcelHelper::ReadUpgradePolicy(MessageParcel &reply, UpgradePolicy &policy)
272 {
273 policy.downloadStrategy = static_cast<bool>(reply.ReadBool());
274 policy.autoUpgradeStrategy = static_cast<bool>(reply.ReadBool());
275 size_t size = static_cast<size_t>(reply.ReadInt32());
276 size_t arraySize = COUNT_OF(policy.autoUpgradePeriods);
277 if (size > MAX_VECTOR_SIZE) {
278 ENGINE_LOGE("ReadUpgradePolicy size is over MAX_VECTOR_SIZE, size=%{public}zu", size);
279 return -1;
280 }
281 for (size_t i = 0; (i < size) && (i < arraySize); i++) {
282 policy.autoUpgradePeriods[i].start = reply.ReadUint32();
283 policy.autoUpgradePeriods[i].end = reply.ReadUint32();
284 }
285 return 0;
286 }
287
WriteUpgradePolicy(MessageParcel & data,const UpgradePolicy & policy)288 int32_t MessageParcelHelper::WriteUpgradePolicy(MessageParcel &data, const UpgradePolicy &policy)
289 {
290 data.WriteBool(policy.downloadStrategy);
291 data.WriteBool(policy.autoUpgradeStrategy);
292 int32_t size = static_cast<int32_t>(COUNT_OF(policy.autoUpgradePeriods));
293 data.WriteInt32(size);
294 for (int32_t i = 0; i < size; i++) {
295 data.WriteUint32(policy.autoUpgradePeriods[i].start);
296 data.WriteUint32(policy.autoUpgradePeriods[i].end);
297 }
298 return 0;
299 }
300
ReadEventInfo(MessageParcel & reply,EventInfo & eventInfo)301 int32_t MessageParcelHelper::ReadEventInfo(MessageParcel &reply, EventInfo &eventInfo)
302 {
303 eventInfo.eventId = static_cast<EventId>(reply.ReadUint32());
304 ReadTaskBody(reply, eventInfo.taskBody);
305 return 0;
306 }
307
WriteEventInfo(MessageParcel & data,const EventInfo & eventInfo)308 int32_t MessageParcelHelper::WriteEventInfo(MessageParcel &data, const EventInfo &eventInfo)
309 {
310 data.WriteUint32(static_cast<uint32_t>(eventInfo.eventId));
311 WriteTaskBody(data, eventInfo.taskBody);
312 return 0;
313 }
314
ReadVersionDigestInfo(MessageParcel & reply,VersionDigestInfo & versionDigestInfo)315 int32_t MessageParcelHelper::ReadVersionDigestInfo(MessageParcel &reply, VersionDigestInfo &versionDigestInfo)
316 {
317 versionDigestInfo.versionDigest = Str16ToStr8(reply.ReadString16());
318 return 0;
319 }
320
WriteVersionDigestInfo(MessageParcel & data,const VersionDigestInfo & versionDigestInfo)321 int32_t MessageParcelHelper::WriteVersionDigestInfo(MessageParcel &data, const VersionDigestInfo &versionDigestInfo)
322 {
323 data.WriteString16(Str8ToStr16(versionDigestInfo.versionDigest));
324 return 0;
325 }
326
ReadDescriptionOptions(MessageParcel & reply,DescriptionOptions & descriptionOptions)327 int32_t MessageParcelHelper::ReadDescriptionOptions(MessageParcel &reply, DescriptionOptions &descriptionOptions)
328 {
329 descriptionOptions.format = static_cast<DescriptionFormat>(reply.ReadUint32());
330 descriptionOptions.language = Str16ToStr8(reply.ReadString16());
331 return 0;
332 }
333
WriteDescriptionOptions(MessageParcel & data,const DescriptionOptions & descriptionOptions)334 int32_t MessageParcelHelper::WriteDescriptionOptions(MessageParcel &data, const DescriptionOptions &descriptionOptions)
335 {
336 data.WriteUint32(static_cast<uint32_t>(descriptionOptions.format));
337 data.WriteString16(Str8ToStr16(descriptionOptions.language));
338 return 0;
339 }
340
ReadDownloadOptions(MessageParcel & reply,DownloadOptions & downloadOptions)341 int32_t MessageParcelHelper::ReadDownloadOptions(MessageParcel &reply, DownloadOptions &downloadOptions)
342 {
343 downloadOptions.allowNetwork = static_cast<NetType>(reply.ReadUint32());
344 downloadOptions.order = static_cast<Order>(reply.ReadUint32());
345 return 0;
346 }
347
WriteDownloadOptions(MessageParcel & data,const DownloadOptions & downloadOptions)348 int32_t MessageParcelHelper::WriteDownloadOptions(MessageParcel &data, const DownloadOptions &downloadOptions)
349 {
350 data.WriteUint32(static_cast<uint32_t>(downloadOptions.allowNetwork));
351 data.WriteUint32(static_cast<uint32_t>(downloadOptions.order));
352 return 0;
353 }
354
ReadPauseDownloadOptions(MessageParcel & reply,PauseDownloadOptions & pauseDownloadOptions)355 int32_t MessageParcelHelper::ReadPauseDownloadOptions(MessageParcel &reply, PauseDownloadOptions &pauseDownloadOptions)
356 {
357 pauseDownloadOptions.isAllowAutoResume = reply.ReadBool();
358 return 0;
359 }
360
WritePauseDownloadOptions(MessageParcel & data,const PauseDownloadOptions & pauseDownloadOptions)361 int32_t MessageParcelHelper::WritePauseDownloadOptions(
362 MessageParcel &data, const PauseDownloadOptions &pauseDownloadOptions)
363 {
364 data.WriteBool(pauseDownloadOptions.isAllowAutoResume);
365 return 0;
366 }
367
ReadResumeDownloadOptions(MessageParcel & reply,ResumeDownloadOptions & resumeDownloadOptions)368 int32_t MessageParcelHelper::ReadResumeDownloadOptions(
369 MessageParcel &reply, ResumeDownloadOptions &resumeDownloadOptions)
370 {
371 resumeDownloadOptions.allowNetwork = static_cast<NetType>(reply.ReadUint32());
372 return 0;
373 }
374
WriteResumeDownloadOptions(MessageParcel & data,const ResumeDownloadOptions & resumeDownloadOptions)375 int32_t MessageParcelHelper::WriteResumeDownloadOptions(
376 MessageParcel &data, const ResumeDownloadOptions &resumeDownloadOptions)
377 {
378 data.WriteUint32(static_cast<uint32_t>(resumeDownloadOptions.allowNetwork));
379 return 0;
380 }
381
ReadUpgradeOptions(MessageParcel & reply,UpgradeOptions & upgradeOptions)382 int32_t MessageParcelHelper::ReadUpgradeOptions(MessageParcel &reply, UpgradeOptions &upgradeOptions)
383 {
384 upgradeOptions.order = static_cast<Order>(reply.ReadUint32());
385 return 0;
386 }
387
WriteUpgradeOptions(MessageParcel & data,const UpgradeOptions & upgradeOptions)388 int32_t MessageParcelHelper::WriteUpgradeOptions(MessageParcel &data, const UpgradeOptions &upgradeOptions)
389 {
390 data.WriteUint32(static_cast<uint32_t>(upgradeOptions.order));
391 return 0;
392 }
393
ReadClearOptions(MessageParcel & reply,ClearOptions & clearOptions)394 int32_t MessageParcelHelper::ReadClearOptions(MessageParcel &reply, ClearOptions &clearOptions)
395 {
396 clearOptions.status = static_cast<UpgradeStatus>(reply.ReadUint32());
397 return 0;
398 }
399
WriteClearOptions(MessageParcel & data,const ClearOptions & clearOptions)400 int32_t MessageParcelHelper::WriteClearOptions(MessageParcel &data, const ClearOptions &clearOptions)
401 {
402 data.WriteUint32(static_cast<uint32_t>(clearOptions.status));
403 return 0;
404 }
405 } // namespace UpdateEngine
406 } // namespace OHOS
407