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 <vector>
21
22 #include "update_helper.h"
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 componentDescriptions.push_back(componentDescription);
67 }
68 }
69
WriteComponentDescriptions(MessageParcel & data,const std::vector<ComponentDescription> & componentDescriptions)70 void WriteComponentDescriptions(MessageParcel &data, const std::vector<ComponentDescription> &componentDescriptions)
71 {
72 data.WriteInt32(static_cast<int32_t>(componentDescriptions.size()));
73 for (size_t i = 0; i < componentDescriptions.size(); i++) {
74 data.WriteString16(Str8ToStr16(componentDescriptions[i].componentId));
75 data.WriteUint32(static_cast<uint32_t>(componentDescriptions[i].descriptionInfo.descriptionType));
76 data.WriteString16(Str8ToStr16(componentDescriptions[i].descriptionInfo.content));
77 }
78 }
79
ReadUpgradeInfo(MessageParcel & reply,UpgradeInfo & info)80 int32_t MessageParcelHelper::ReadUpgradeInfo(MessageParcel &reply, UpgradeInfo &info)
81 {
82 info.upgradeApp = Str16ToStr8(reply.ReadString16());
83 info.businessType.vendor = Str16ToStr8(reply.ReadString16());
84 info.businessType.subType = static_cast<BusinessSubType>(reply.ReadInt32());
85 info.upgradeDevId = Str16ToStr8(reply.ReadString16());
86 info.controlDevId = Str16ToStr8(reply.ReadString16());
87 return 0;
88 }
89
WriteUpgradeInfo(MessageParcel & data,const UpgradeInfo & info)90 int32_t MessageParcelHelper::WriteUpgradeInfo(MessageParcel &data, const UpgradeInfo &info)
91 {
92 data.WriteString16(Str8ToStr16(info.upgradeApp));
93 data.WriteString16(Str8ToStr16(info.businessType.vendor));
94 data.WriteInt32(static_cast<int32_t>(info.businessType.subType));
95 data.WriteString16(Str8ToStr16(info.upgradeDevId));
96 data.WriteString16(Str8ToStr16(info.controlDevId));
97 return 0;
98 }
99
ReadVersionDescriptionInfo(MessageParcel & reply,VersionDescriptionInfo & versionDescriptionInfo)100 int32_t MessageParcelHelper::ReadVersionDescriptionInfo(
101 MessageParcel &reply, VersionDescriptionInfo &versionDescriptionInfo)
102 {
103 ReadComponentDescriptions(reply, versionDescriptionInfo.componentDescriptions);
104 return 0;
105 }
106
WriteVersionDescriptionInfo(MessageParcel & data,const VersionDescriptionInfo & versionDescriptionInfo)107 int32_t MessageParcelHelper::WriteVersionDescriptionInfo(
108 MessageParcel &data, const VersionDescriptionInfo &versionDescriptionInfo)
109 {
110 WriteComponentDescriptions(data, versionDescriptionInfo.componentDescriptions);
111 return 0;
112 }
113
ReadBusinessError(MessageParcel & reply,BusinessError & businessError)114 int32_t MessageParcelHelper::ReadBusinessError(MessageParcel &reply, BusinessError &businessError)
115 {
116 businessError.message = Str16ToStr8(reply.ReadString16());
117 businessError.errorNum = static_cast<CallResult>(reply.ReadInt32());
118 ReadErrorMessages(reply, businessError.data);
119 return 0;
120 }
121
WriteBusinessError(MessageParcel & data,const BusinessError & businessError)122 int32_t MessageParcelHelper::WriteBusinessError(MessageParcel &data, const BusinessError &businessError)
123 {
124 data.WriteString16(Str8ToStr16(businessError.message));
125 data.WriteInt32(static_cast<int32_t>(businessError.errorNum));
126 WriteErrorMessages(data, businessError.data);
127 return 0;
128 }
129
ReadVersionComponents(MessageParcel & reply,std::vector<VersionComponent> & versionComponents)130 void ReadVersionComponents(MessageParcel &reply, std::vector<VersionComponent> &versionComponents)
131 {
132 int32_t size = reply.ReadInt32();
133 if (size > MAX_VECTOR_SIZE) {
134 ENGINE_LOGE("ReadVersionComponents size is over MAX_VECTOR_SIZE, size=%{public}d", size);
135 return;
136 }
137 for (size_t i = 0; i < static_cast<size_t>(size); i++) {
138 VersionComponent versionComponent;
139 versionComponent.componentId = Str16ToStr8(reply.ReadString16());
140 versionComponent.componentType = reply.ReadInt32();
141 versionComponent.upgradeAction = Str16ToStr8(reply.ReadString16());
142 versionComponent.displayVersion = Str16ToStr8(reply.ReadString16());
143 versionComponent.innerVersion = Str16ToStr8(reply.ReadString16());
144 versionComponent.size = static_cast<size_t>(reply.ReadUint32());
145 versionComponent.effectiveMode = static_cast<size_t>(reply.ReadUint32());
146
147 versionComponent.descriptionInfo.descriptionType = static_cast<DescriptionType>(reply.ReadUint32());
148 versionComponent.descriptionInfo.content = Str16ToStr8(reply.ReadString16());
149 versionComponent.componentExtra = Str16ToStr8(reply.ReadString16());
150 versionComponents.push_back(versionComponent);
151 }
152 }
153
WriteVersionComponents(MessageParcel & data,const std::vector<VersionComponent> & versionComponents)154 void WriteVersionComponents(MessageParcel &data, const std::vector<VersionComponent> &versionComponents)
155 {
156 data.WriteInt32(static_cast<int32_t>(versionComponents.size()));
157 for (size_t i = 0; i < versionComponents.size(); i++) {
158 const VersionComponent *versionComponent = &versionComponents[i];
159 data.WriteString16(Str8ToStr16(versionComponent->componentId));
160 data.WriteInt32(versionComponent->componentType);
161 data.WriteString16(Str8ToStr16(versionComponent->upgradeAction));
162 data.WriteString16(Str8ToStr16(versionComponent->displayVersion));
163 data.WriteString16(Str8ToStr16(versionComponent->innerVersion));
164 data.WriteUint32(static_cast<uint32_t>(versionComponent->size));
165 data.WriteUint32(static_cast<uint32_t>(versionComponent->effectiveMode));
166
167 data.WriteUint32(static_cast<uint32_t>(versionComponent->descriptionInfo.descriptionType));
168 data.WriteString16(Str8ToStr16(versionComponent->descriptionInfo.content));
169 data.WriteString16(Str8ToStr16(versionComponent->componentExtra));
170 }
171 }
172
ReadNewVersionInfoEx(MessageParcel & reply,NewVersionInfo & newVersionInfo)173 void ReadNewVersionInfoEx(MessageParcel &reply, NewVersionInfo &newVersionInfo)
174 {
175 newVersionInfo.versionDigestInfo.versionDigest = Str16ToStr8(reply.ReadString16());
176 ReadVersionComponents(reply, newVersionInfo.versionComponents);
177 }
178
WriteNewVersionInfoEx(MessageParcel & data,const NewVersionInfo & newVersionInfo)179 void WriteNewVersionInfoEx(MessageParcel &data, const NewVersionInfo &newVersionInfo)
180 {
181 data.WriteString16(Str8ToStr16(newVersionInfo.versionDigestInfo.versionDigest));
182 WriteVersionComponents(data, newVersionInfo.versionComponents);
183 }
184
ReadCheckResult(MessageParcel & reply,CheckResult & checkResult)185 int32_t MessageParcelHelper::ReadCheckResult(MessageParcel &reply, CheckResult &checkResult)
186 {
187 checkResult.isExistNewVersion = reply.ReadBool();
188 ReadNewVersionInfoEx(reply, checkResult.newVersionInfo);
189 return 0;
190 }
191
WriteCheckResult(MessageParcel & data,const CheckResult & checkResult)192 int32_t MessageParcelHelper::WriteCheckResult(MessageParcel &data, const CheckResult &checkResult)
193 {
194 data.WriteBool(checkResult.isExistNewVersion);
195 WriteNewVersionInfoEx(data, checkResult.newVersionInfo);
196 return 0;
197 }
198
ReadNewVersionInfo(MessageParcel & reply,NewVersionInfo & newVersionInfo)199 int32_t MessageParcelHelper::ReadNewVersionInfo(MessageParcel &reply, NewVersionInfo &newVersionInfo)
200 {
201 ReadNewVersionInfoEx(reply, newVersionInfo);
202 return 0;
203 }
204
WriteNewVersionInfo(MessageParcel & data,const NewVersionInfo & newVersionInfo)205 int32_t MessageParcelHelper::WriteNewVersionInfo(MessageParcel &data, const NewVersionInfo &newVersionInfo)
206 {
207 WriteNewVersionInfoEx(data, newVersionInfo);
208 return 0;
209 }
210
ReadCurrentVersionInfo(MessageParcel & reply,CurrentVersionInfo & info)211 int32_t MessageParcelHelper::ReadCurrentVersionInfo(MessageParcel &reply, CurrentVersionInfo &info)
212 {
213 info.osVersion = Str16ToStr8(reply.ReadString16());
214 info.deviceName = Str16ToStr8(reply.ReadString16());
215 ReadVersionComponents(reply, info.versionComponents);
216 return 0;
217 }
218
WriteCurrentVersionInfo(MessageParcel & data,const CurrentVersionInfo & info)219 int32_t MessageParcelHelper::WriteCurrentVersionInfo(MessageParcel &data, const CurrentVersionInfo &info)
220 {
221 data.WriteString16(Str8ToStr16(info.osVersion));
222 data.WriteString16(Str8ToStr16(info.deviceName));
223 WriteVersionComponents(data, info.versionComponents);
224 return 0;
225 }
226
ReadTaskBody(MessageParcel & reply,TaskBody & taskBody)227 void ReadTaskBody(MessageParcel &reply, TaskBody &taskBody)
228 {
229 taskBody.versionDigestInfo.versionDigest = Str16ToStr8(reply.ReadString16());
230 taskBody.status = static_cast<UpgradeStatus>(reply.ReadInt32());
231 taskBody.subStatus = reply.ReadInt32();
232 taskBody.progress = reply.ReadInt32();
233 taskBody.installMode = reply.ReadInt32();
234 ReadErrorMessages(reply, taskBody.errorMessages);
235 ReadVersionComponents(reply, taskBody.versionComponents);
236 }
237
WriteTaskBody(MessageParcel & data,const TaskBody & taskBody)238 void WriteTaskBody(MessageParcel &data, const TaskBody &taskBody)
239 {
240 data.WriteString16(Str8ToStr16(taskBody.versionDigestInfo.versionDigest));
241 data.WriteInt32(static_cast<int32_t>(taskBody.status));
242 data.WriteInt32(taskBody.subStatus);
243 data.WriteInt32(taskBody.progress);
244 data.WriteInt32(taskBody.installMode);
245 WriteErrorMessages(data, taskBody.errorMessages);
246 WriteVersionComponents(data, taskBody.versionComponents);
247 }
248
ReadTaskInfo(MessageParcel & reply,TaskInfo & info)249 int32_t MessageParcelHelper::ReadTaskInfo(MessageParcel &reply, TaskInfo &info)
250 {
251 info.existTask = reply.ReadBool();
252 ReadTaskBody(reply, info.taskBody);
253 return 0;
254 }
255
WriteTaskInfo(MessageParcel & data,const TaskInfo & info)256 int32_t MessageParcelHelper::WriteTaskInfo(MessageParcel &data, const TaskInfo &info)
257 {
258 data.WriteBool(info.existTask);
259 WriteTaskBody(data, info.taskBody);
260 return 0;
261 }
262
ReadUpgradePolicy(MessageParcel & reply,UpgradePolicy & policy)263 int32_t MessageParcelHelper::ReadUpgradePolicy(MessageParcel &reply, UpgradePolicy &policy)
264 {
265 policy.downloadStrategy = static_cast<bool>(reply.ReadBool());
266 policy.autoUpgradeStrategy = static_cast<bool>(reply.ReadBool());
267 size_t size = static_cast<size_t>(reply.ReadInt32());
268 size_t arraySize = COUNT_OF(policy.autoUpgradePeriods);
269 if (size > MAX_VECTOR_SIZE) {
270 ENGINE_LOGE("ReadUpgradePolicy size is over MAX_VECTOR_SIZE, size=%{public}zu", size);
271 return -1;
272 }
273 for (size_t i = 0; (i < size) && (i < arraySize); i++) {
274 policy.autoUpgradePeriods[i].start = reply.ReadUint32();
275 policy.autoUpgradePeriods[i].end = reply.ReadUint32();
276 }
277 return 0;
278 }
279
WriteUpgradePolicy(MessageParcel & data,const UpgradePolicy & policy)280 int32_t MessageParcelHelper::WriteUpgradePolicy(MessageParcel &data, const UpgradePolicy &policy)
281 {
282 data.WriteBool(policy.downloadStrategy);
283 data.WriteBool(policy.autoUpgradeStrategy);
284 int32_t size = static_cast<int32_t>(COUNT_OF(policy.autoUpgradePeriods));
285 data.WriteInt32(size);
286 for (int32_t i = 0; i < size; i++) {
287 data.WriteUint32(policy.autoUpgradePeriods[i].start);
288 data.WriteUint32(policy.autoUpgradePeriods[i].end);
289 }
290 return 0;
291 }
292
ReadEventInfo(MessageParcel & reply,EventInfo & eventInfo)293 int32_t MessageParcelHelper::ReadEventInfo(MessageParcel &reply, EventInfo &eventInfo)
294 {
295 eventInfo.eventId = static_cast<EventId>(reply.ReadUint32());
296 ReadTaskBody(reply, eventInfo.taskBody);
297 return 0;
298 }
299
WriteEventInfo(MessageParcel & data,const EventInfo & eventInfo)300 int32_t MessageParcelHelper::WriteEventInfo(MessageParcel &data, const EventInfo &eventInfo)
301 {
302 data.WriteUint32(static_cast<uint32_t>(eventInfo.eventId));
303 WriteTaskBody(data, eventInfo.taskBody);
304 return 0;
305 }
306
ReadVersionDigestInfo(MessageParcel & reply,VersionDigestInfo & versionDigestInfo)307 int32_t MessageParcelHelper::ReadVersionDigestInfo(MessageParcel &reply, VersionDigestInfo &versionDigestInfo)
308 {
309 versionDigestInfo.versionDigest = Str16ToStr8(reply.ReadString16());
310 return 0;
311 }
312
WriteVersionDigestInfo(MessageParcel & data,const VersionDigestInfo & versionDigestInfo)313 int32_t MessageParcelHelper::WriteVersionDigestInfo(MessageParcel &data, const VersionDigestInfo &versionDigestInfo)
314 {
315 data.WriteString16(Str8ToStr16(versionDigestInfo.versionDigest));
316 return 0;
317 }
318
ReadDescriptionOptions(MessageParcel & reply,DescriptionOptions & descriptionOptions)319 int32_t MessageParcelHelper::ReadDescriptionOptions(MessageParcel &reply, DescriptionOptions &descriptionOptions)
320 {
321 descriptionOptions.format = static_cast<DescriptionFormat>(reply.ReadUint32());
322 descriptionOptions.language = Str16ToStr8(reply.ReadString16());
323 return 0;
324 }
325
WriteDescriptionOptions(MessageParcel & data,const DescriptionOptions & descriptionOptions)326 int32_t MessageParcelHelper::WriteDescriptionOptions(MessageParcel &data, const DescriptionOptions &descriptionOptions)
327 {
328 data.WriteUint32(static_cast<uint32_t>(descriptionOptions.format));
329 data.WriteString16(Str8ToStr16(descriptionOptions.language));
330 return 0;
331 }
332
ReadDownloadOptions(MessageParcel & reply,DownloadOptions & downloadOptions)333 int32_t MessageParcelHelper::ReadDownloadOptions(MessageParcel &reply, DownloadOptions &downloadOptions)
334 {
335 downloadOptions.allowNetwork = static_cast<NetType>(reply.ReadUint32());
336 downloadOptions.order = static_cast<Order>(reply.ReadUint32());
337 return 0;
338 }
339
WriteDownloadOptions(MessageParcel & data,const DownloadOptions & downloadOptions)340 int32_t MessageParcelHelper::WriteDownloadOptions(MessageParcel &data, const DownloadOptions &downloadOptions)
341 {
342 data.WriteUint32(static_cast<uint32_t>(downloadOptions.allowNetwork));
343 data.WriteUint32(static_cast<uint32_t>(downloadOptions.order));
344 return 0;
345 }
346
ReadPauseDownloadOptions(MessageParcel & reply,PauseDownloadOptions & pauseDownloadOptions)347 int32_t MessageParcelHelper::ReadPauseDownloadOptions(MessageParcel &reply, PauseDownloadOptions &pauseDownloadOptions)
348 {
349 pauseDownloadOptions.isAllowAutoResume = reply.ReadBool();
350 return 0;
351 }
352
WritePauseDownloadOptions(MessageParcel & data,const PauseDownloadOptions & pauseDownloadOptions)353 int32_t MessageParcelHelper::WritePauseDownloadOptions(
354 MessageParcel &data, const PauseDownloadOptions &pauseDownloadOptions)
355 {
356 data.WriteBool(pauseDownloadOptions.isAllowAutoResume);
357 return 0;
358 }
359
ReadResumeDownloadOptions(MessageParcel & reply,ResumeDownloadOptions & resumeDownloadOptions)360 int32_t MessageParcelHelper::ReadResumeDownloadOptions(
361 MessageParcel &reply, ResumeDownloadOptions &resumeDownloadOptions)
362 {
363 resumeDownloadOptions.allowNetwork = static_cast<NetType>(reply.ReadUint32());
364 return 0;
365 }
366
WriteResumeDownloadOptions(MessageParcel & data,const ResumeDownloadOptions & resumeDownloadOptions)367 int32_t MessageParcelHelper::WriteResumeDownloadOptions(
368 MessageParcel &data, const ResumeDownloadOptions &resumeDownloadOptions)
369 {
370 data.WriteUint32(static_cast<uint32_t>(resumeDownloadOptions.allowNetwork));
371 return 0;
372 }
373
ReadUpgradeOptions(MessageParcel & reply,UpgradeOptions & upgradeOptions)374 int32_t MessageParcelHelper::ReadUpgradeOptions(MessageParcel &reply, UpgradeOptions &upgradeOptions)
375 {
376 upgradeOptions.order = static_cast<Order>(reply.ReadUint32());
377 return 0;
378 }
379
WriteUpgradeOptions(MessageParcel & data,const UpgradeOptions & upgradeOptions)380 int32_t MessageParcelHelper::WriteUpgradeOptions(MessageParcel &data, const UpgradeOptions &upgradeOptions)
381 {
382 data.WriteUint32(static_cast<uint32_t>(upgradeOptions.order));
383 return 0;
384 }
385
ReadClearOptions(MessageParcel & reply,ClearOptions & clearOptions)386 int32_t MessageParcelHelper::ReadClearOptions(MessageParcel &reply, ClearOptions &clearOptions)
387 {
388 clearOptions.status = static_cast<UpgradeStatus>(reply.ReadUint32());
389 return 0;
390 }
391
WriteClearOptions(MessageParcel & data,const ClearOptions & clearOptions)392 int32_t MessageParcelHelper::WriteClearOptions(MessageParcel &data, const ClearOptions &clearOptions)
393 {
394 data.WriteUint32(static_cast<uint32_t>(clearOptions.status));
395 return 0;
396 }
397 } // namespace UpdateEngine
398 } // namespace OHOS
399