• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         versionComponent.otaMode = static_cast<size_t>(reply.ReadUint32());
155 
156         versionComponent.descriptionInfo.descriptionType = static_cast<DescriptionType>(reply.ReadUint32());
157         versionComponent.descriptionInfo.content = Str16ToStr8(reply.ReadString16());
158         versionComponent.componentExtra = Str16ToStr8(reply.ReadString16());
159         versionComponents.push_back(versionComponent);
160     }
161 }
162 
WriteVersionComponents(MessageParcel & data,const std::vector<VersionComponent> & versionComponents)163 void WriteVersionComponents(MessageParcel &data, const std::vector<VersionComponent> &versionComponents)
164 {
165     data.WriteInt32(static_cast<int32_t>(versionComponents.size()));
166     for (size_t i = 0; i < versionComponents.size(); i++) {
167         const VersionComponent *versionComponent = &versionComponents[i];
168         data.WriteString16(Str8ToStr16(versionComponent->componentId));
169         data.WriteInt32(versionComponent->componentType);
170         data.WriteString16(Str8ToStr16(versionComponent->upgradeAction));
171         data.WriteString16(Str8ToStr16(versionComponent->displayVersion));
172         data.WriteString16(Str8ToStr16(versionComponent->innerVersion));
173         data.WriteUint64(static_cast<uint64_t>(versionComponent->size));
174         data.WriteUint32(static_cast<uint32_t>(versionComponent->effectiveMode));
175         data.WriteUint32(static_cast<uint32_t>(versionComponent->otaMode));
176 
177         data.WriteUint32(static_cast<uint32_t>(versionComponent->descriptionInfo.descriptionType));
178         data.WriteString16(Str8ToStr16(versionComponent->descriptionInfo.content));
179         data.WriteString16(Str8ToStr16(versionComponent->componentExtra));
180     }
181 }
182 
ReadNewVersionInfoEx(MessageParcel & reply,NewVersionInfo & newVersionInfo)183 void ReadNewVersionInfoEx(MessageParcel &reply, NewVersionInfo &newVersionInfo)
184 {
185     newVersionInfo.versionDigestInfo.versionDigest = Str16ToStr8(reply.ReadString16());
186     ReadVersionComponents(reply, newVersionInfo.versionComponents);
187 }
188 
WriteNewVersionInfoEx(MessageParcel & data,const NewVersionInfo & newVersionInfo)189 void WriteNewVersionInfoEx(MessageParcel &data, const NewVersionInfo &newVersionInfo)
190 {
191     data.WriteString16(Str8ToStr16(newVersionInfo.versionDigestInfo.versionDigest));
192     WriteVersionComponents(data, newVersionInfo.versionComponents);
193 }
194 
ReadCheckResult(MessageParcel & reply,CheckResult & checkResult)195 int32_t MessageParcelHelper::ReadCheckResult(MessageParcel &reply, CheckResult &checkResult)
196 {
197     checkResult.isExistNewVersion = reply.ReadBool();
198     ReadNewVersionInfoEx(reply, checkResult.newVersionInfo);
199     return 0;
200 }
201 
WriteCheckResult(MessageParcel & data,const CheckResult & checkResult)202 int32_t MessageParcelHelper::WriteCheckResult(MessageParcel &data, const CheckResult &checkResult)
203 {
204     data.WriteBool(checkResult.isExistNewVersion);
205     WriteNewVersionInfoEx(data, checkResult.newVersionInfo);
206     return 0;
207 }
208 
ReadNewVersionInfo(MessageParcel & reply,NewVersionInfo & newVersionInfo)209 int32_t MessageParcelHelper::ReadNewVersionInfo(MessageParcel &reply, NewVersionInfo &newVersionInfo)
210 {
211     ReadNewVersionInfoEx(reply, newVersionInfo);
212     return 0;
213 }
214 
WriteNewVersionInfo(MessageParcel & data,const NewVersionInfo & newVersionInfo)215 int32_t MessageParcelHelper::WriteNewVersionInfo(MessageParcel &data, const NewVersionInfo &newVersionInfo)
216 {
217     WriteNewVersionInfoEx(data, newVersionInfo);
218     return 0;
219 }
220 
ReadCurrentVersionInfo(MessageParcel & reply,CurrentVersionInfo & info)221 int32_t MessageParcelHelper::ReadCurrentVersionInfo(MessageParcel &reply, CurrentVersionInfo &info)
222 {
223     info.osVersion = Str16ToStr8(reply.ReadString16());
224     info.deviceName = Str16ToStr8(reply.ReadString16());
225     ReadVersionComponents(reply, info.versionComponents);
226     return 0;
227 }
228 
WriteCurrentVersionInfo(MessageParcel & data,const CurrentVersionInfo & info)229 int32_t MessageParcelHelper::WriteCurrentVersionInfo(MessageParcel &data, const CurrentVersionInfo &info)
230 {
231     data.WriteString16(Str8ToStr16(info.osVersion));
232     data.WriteString16(Str8ToStr16(info.deviceName));
233     WriteVersionComponents(data, info.versionComponents);
234     return 0;
235 }
236 
ReadTaskBody(MessageParcel & reply,TaskBody & taskBody)237 void ReadTaskBody(MessageParcel &reply, TaskBody &taskBody)
238 {
239     taskBody.versionDigestInfo.versionDigest = Str16ToStr8(reply.ReadString16());
240     taskBody.status = static_cast<UpgradeStatus>(reply.ReadInt32());
241     taskBody.subStatus = reply.ReadInt32();
242     taskBody.progress = reply.ReadInt32();
243     taskBody.installMode = reply.ReadInt32();
244     ReadErrorMessages(reply, taskBody.errorMessages);
245     ReadVersionComponents(reply, taskBody.versionComponents);
246 }
247 
WriteTaskBody(MessageParcel & data,const TaskBody & taskBody)248 void WriteTaskBody(MessageParcel &data, const TaskBody &taskBody)
249 {
250     data.WriteString16(Str8ToStr16(taskBody.versionDigestInfo.versionDigest));
251     data.WriteInt32(static_cast<int32_t>(taskBody.status));
252     data.WriteInt32(taskBody.subStatus);
253     data.WriteInt32(taskBody.progress);
254     data.WriteInt32(taskBody.installMode);
255     WriteErrorMessages(data, taskBody.errorMessages);
256     WriteVersionComponents(data, taskBody.versionComponents);
257 }
258 
ReadTaskInfo(MessageParcel & reply,TaskInfo & info)259 int32_t MessageParcelHelper::ReadTaskInfo(MessageParcel &reply, TaskInfo &info)
260 {
261     info.existTask = reply.ReadBool();
262     ReadTaskBody(reply, info.taskBody);
263     return 0;
264 }
265 
WriteTaskInfo(MessageParcel & data,const TaskInfo & info)266 int32_t MessageParcelHelper::WriteTaskInfo(MessageParcel &data, const TaskInfo &info)
267 {
268     data.WriteBool(info.existTask);
269     WriteTaskBody(data, info.taskBody);
270     return 0;
271 }
272 
ReadUpgradePolicy(MessageParcel & reply,UpgradePolicy & policy)273 int32_t MessageParcelHelper::ReadUpgradePolicy(MessageParcel &reply, UpgradePolicy &policy)
274 {
275     policy.downloadStrategy = static_cast<bool>(reply.ReadBool());
276     policy.autoUpgradeStrategy = static_cast<bool>(reply.ReadBool());
277     size_t size = static_cast<size_t>(reply.ReadInt32());
278     size_t arraySize = COUNT_OF(policy.autoUpgradePeriods);
279     if (size > MAX_VECTOR_SIZE) {
280         ENGINE_LOGE("ReadUpgradePolicy size is over MAX_VECTOR_SIZE, size=%{public}zu", size);
281         return -1;
282     }
283     for (size_t i = 0; (i < size) && (i < arraySize); i++) {
284         policy.autoUpgradePeriods[i].start = reply.ReadUint32();
285         policy.autoUpgradePeriods[i].end = reply.ReadUint32();
286     }
287     return 0;
288 }
289 
WriteUpgradePolicy(MessageParcel & data,const UpgradePolicy & policy)290 int32_t MessageParcelHelper::WriteUpgradePolicy(MessageParcel &data, const UpgradePolicy &policy)
291 {
292     data.WriteBool(policy.downloadStrategy);
293     data.WriteBool(policy.autoUpgradeStrategy);
294     int32_t size = static_cast<int32_t>(COUNT_OF(policy.autoUpgradePeriods));
295     data.WriteInt32(size);
296     for (int32_t i = 0; i < size; i++) {
297         data.WriteUint32(policy.autoUpgradePeriods[i].start);
298         data.WriteUint32(policy.autoUpgradePeriods[i].end);
299     }
300     return 0;
301 }
302 
ReadEventInfo(MessageParcel & reply,EventInfo & eventInfo)303 int32_t MessageParcelHelper::ReadEventInfo(MessageParcel &reply, EventInfo &eventInfo)
304 {
305     eventInfo.eventId = static_cast<EventId>(reply.ReadUint32());
306     ReadTaskBody(reply, eventInfo.taskBody);
307     return 0;
308 }
309 
WriteEventInfo(MessageParcel & data,const EventInfo & eventInfo)310 int32_t MessageParcelHelper::WriteEventInfo(MessageParcel &data, const EventInfo &eventInfo)
311 {
312     data.WriteUint32(static_cast<uint32_t>(eventInfo.eventId));
313     WriteTaskBody(data, eventInfo.taskBody);
314     return 0;
315 }
316 
ReadVersionDigestInfo(MessageParcel & reply,VersionDigestInfo & versionDigestInfo)317 int32_t MessageParcelHelper::ReadVersionDigestInfo(MessageParcel &reply, VersionDigestInfo &versionDigestInfo)
318 {
319     versionDigestInfo.versionDigest = Str16ToStr8(reply.ReadString16());
320     return 0;
321 }
322 
WriteVersionDigestInfo(MessageParcel & data,const VersionDigestInfo & versionDigestInfo)323 int32_t MessageParcelHelper::WriteVersionDigestInfo(MessageParcel &data, const VersionDigestInfo &versionDigestInfo)
324 {
325     data.WriteString16(Str8ToStr16(versionDigestInfo.versionDigest));
326     return 0;
327 }
328 
ReadDescriptionOptions(MessageParcel & reply,DescriptionOptions & descriptionOptions)329 int32_t MessageParcelHelper::ReadDescriptionOptions(MessageParcel &reply, DescriptionOptions &descriptionOptions)
330 {
331     descriptionOptions.format = static_cast<DescriptionFormat>(reply.ReadUint32());
332     descriptionOptions.language = Str16ToStr8(reply.ReadString16());
333     return 0;
334 }
335 
WriteDescriptionOptions(MessageParcel & data,const DescriptionOptions & descriptionOptions)336 int32_t MessageParcelHelper::WriteDescriptionOptions(MessageParcel &data, const DescriptionOptions &descriptionOptions)
337 {
338     data.WriteUint32(static_cast<uint32_t>(descriptionOptions.format));
339     data.WriteString16(Str8ToStr16(descriptionOptions.language));
340     return 0;
341 }
342 
ReadDownloadOptions(MessageParcel & reply,DownloadOptions & downloadOptions)343 int32_t MessageParcelHelper::ReadDownloadOptions(MessageParcel &reply, DownloadOptions &downloadOptions)
344 {
345     downloadOptions.allowNetwork = static_cast<NetType>(reply.ReadUint32());
346     downloadOptions.order = static_cast<Order>(reply.ReadUint32());
347     return 0;
348 }
349 
WriteDownloadOptions(MessageParcel & data,const DownloadOptions & downloadOptions)350 int32_t MessageParcelHelper::WriteDownloadOptions(MessageParcel &data, const DownloadOptions &downloadOptions)
351 {
352     data.WriteUint32(static_cast<uint32_t>(downloadOptions.allowNetwork));
353     data.WriteUint32(static_cast<uint32_t>(downloadOptions.order));
354     return 0;
355 }
356 
ReadPauseDownloadOptions(MessageParcel & reply,PauseDownloadOptions & pauseDownloadOptions)357 int32_t MessageParcelHelper::ReadPauseDownloadOptions(MessageParcel &reply, PauseDownloadOptions &pauseDownloadOptions)
358 {
359     pauseDownloadOptions.isAllowAutoResume = reply.ReadBool();
360     return 0;
361 }
362 
WritePauseDownloadOptions(MessageParcel & data,const PauseDownloadOptions & pauseDownloadOptions)363 int32_t MessageParcelHelper::WritePauseDownloadOptions(
364     MessageParcel &data, const PauseDownloadOptions &pauseDownloadOptions)
365 {
366     data.WriteBool(pauseDownloadOptions.isAllowAutoResume);
367     return 0;
368 }
369 
ReadResumeDownloadOptions(MessageParcel & reply,ResumeDownloadOptions & resumeDownloadOptions)370 int32_t MessageParcelHelper::ReadResumeDownloadOptions(
371     MessageParcel &reply, ResumeDownloadOptions &resumeDownloadOptions)
372 {
373     resumeDownloadOptions.allowNetwork = static_cast<NetType>(reply.ReadUint32());
374     return 0;
375 }
376 
WriteResumeDownloadOptions(MessageParcel & data,const ResumeDownloadOptions & resumeDownloadOptions)377 int32_t MessageParcelHelper::WriteResumeDownloadOptions(
378     MessageParcel &data, const ResumeDownloadOptions &resumeDownloadOptions)
379 {
380     data.WriteUint32(static_cast<uint32_t>(resumeDownloadOptions.allowNetwork));
381     return 0;
382 }
383 
ReadUpgradeOptions(MessageParcel & reply,UpgradeOptions & upgradeOptions)384 int32_t MessageParcelHelper::ReadUpgradeOptions(MessageParcel &reply, UpgradeOptions &upgradeOptions)
385 {
386     upgradeOptions.order = static_cast<Order>(reply.ReadUint32());
387     return 0;
388 }
389 
WriteUpgradeOptions(MessageParcel & data,const UpgradeOptions & upgradeOptions)390 int32_t MessageParcelHelper::WriteUpgradeOptions(MessageParcel &data, const UpgradeOptions &upgradeOptions)
391 {
392     data.WriteUint32(static_cast<uint32_t>(upgradeOptions.order));
393     return 0;
394 }
395 
ReadClearOptions(MessageParcel & reply,ClearOptions & clearOptions)396 int32_t MessageParcelHelper::ReadClearOptions(MessageParcel &reply, ClearOptions &clearOptions)
397 {
398     clearOptions.status = static_cast<UpgradeStatus>(reply.ReadUint32());
399     return 0;
400 }
401 
WriteClearOptions(MessageParcel & data,const ClearOptions & clearOptions)402 int32_t MessageParcelHelper::WriteClearOptions(MessageParcel &data, const ClearOptions &clearOptions)
403 {
404     data.WriteUint32(static_cast<uint32_t>(clearOptions.status));
405     return 0;
406 }
407 } // namespace UpdateEngine
408 } // namespace OHOS
409