• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "update_helper.h"
17 
18 #include <cstring>
19 #include <string>
20 
21 namespace OHOS {
22 namespace UpdateEngine {
23 #ifdef UPDATE_SERVICE
24 const std::string LOG_LABEL = "update_engine";
25 const std::string LOG_NAME = "/data/ota_package/update_service_log.txt";
26 #else
27 const std::string LOG_LABEL = "update_engine";
28 const std::string LOG_NAME = "/data/ota_package/update_client.log.txt";
29 #endif
30 constexpr int HEX_DIGEST_NUM = 2;
31 constexpr int HEX_DIGEST_BASE = 16;
32 constexpr int DATA_ANONYMIZATION_LEN = 4;
33 
34 UpdateLogLevel UpdateHelper::level_ = UpdateLogLevel::UPDATE_INFO;
35 
ToString() const36 std::string UpgradeInfo::ToString() const
37 {
38     std::string output = "upgradeApp:" + upgradeApp;
39     output += ",businessType(vender:" + businessType.vendor;
40     output += ",subType:" + std::to_string(CAST_INT(businessType.subType));
41     output += "),upgradeDevId:" + UpdateHelper::Anonymization(upgradeDevId);
42     output += ",controlDevId:" + UpdateHelper::Anonymization(controlDevId);
43     return output;
44 }
45 
ReadErrorMessages(MessageParcel & reply,ErrorMessage errorMessages[],size_t arraySize)46 void ReadErrorMessages(MessageParcel &reply, ErrorMessage errorMessages[], size_t arraySize)
47 {
48     int32_t size = reply.ReadInt32();
49     for (size_t i = 0; (i < static_cast<size_t>(size)) && (i < arraySize); i++) {
50         errorMessages[i].errorCode = reply.ReadInt32();
51         errorMessages[i].errorMessage = Str16ToStr8(reply.ReadString16());
52     }
53 }
54 
WriteErrorMessages(MessageParcel & data,const ErrorMessage errorMessages[],size_t arraySize)55 void WriteErrorMessages(MessageParcel &data, const ErrorMessage errorMessages[], size_t arraySize)
56 {
57     data.WriteInt32(static_cast<int32_t>(arraySize));
58     for (size_t i = 0; i < arraySize; i++) {
59         data.WriteInt32(errorMessages[i].errorCode);
60         data.WriteString16(Str8ToStr16(errorMessages[i].errorMessage));
61     }
62 }
63 
ReadComponentDescriptions(MessageParcel & reply,ComponentDescription componentDescriptions[],size_t arraySize)64 void ReadComponentDescriptions(MessageParcel &reply, ComponentDescription componentDescriptions[], size_t arraySize)
65 {
66     int32_t size = reply.ReadInt32();
67     for (size_t i = 0; (i < static_cast<size_t>(size)) && (i < arraySize); i++) {
68         componentDescriptions[i].componentId = Str16ToStr8(reply.ReadString16());
69         componentDescriptions[i].descriptionInfo.descriptionType = static_cast<DescriptionType>(reply.ReadUint32());
70         componentDescriptions[i].descriptionInfo.content = Str16ToStr8(reply.ReadString16());
71     }
72 }
73 
WriteComponentDescriptions(MessageParcel & data,const ComponentDescription componentDescriptions[],size_t arraySize)74 void WriteComponentDescriptions(MessageParcel &data, const ComponentDescription componentDescriptions[],
75     size_t arraySize)
76 {
77     data.WriteInt32(static_cast<int32_t>(arraySize));
78     for (size_t i = 0; i < arraySize; i++) {
79         data.WriteString16(Str8ToStr16(componentDescriptions[i].componentId));
80         data.WriteUint32(static_cast<uint32_t>(componentDescriptions[i].descriptionInfo.descriptionType));
81         data.WriteString16(Str8ToStr16(componentDescriptions[i].descriptionInfo.content));
82     }
83 }
84 
ReadUpgradeInfo(MessageParcel & reply,UpgradeInfo & info)85 int32_t UpdateHelper::ReadUpgradeInfo(MessageParcel &reply, UpgradeInfo &info)
86 {
87     info.upgradeApp = Str16ToStr8(reply.ReadString16());
88     info.businessType.vendor = Str16ToStr8(reply.ReadString16());
89     info.businessType.subType = static_cast<BusinessSubType>(reply.ReadInt32());
90     info.upgradeDevId = Str16ToStr8(reply.ReadString16());
91     info.controlDevId = Str16ToStr8(reply.ReadString16());
92     return 0;
93 }
94 
WriteUpgradeInfo(MessageParcel & data,const UpgradeInfo & info)95 int32_t UpdateHelper::WriteUpgradeInfo(MessageParcel &data, const UpgradeInfo &info)
96 {
97     data.WriteString16(Str8ToStr16(info.upgradeApp));
98     data.WriteString16(Str8ToStr16(info.businessType.vendor));
99     data.WriteInt32(static_cast<int32_t>(info.businessType.subType));
100     data.WriteString16(Str8ToStr16(info.upgradeDevId));
101     data.WriteString16(Str8ToStr16(info.controlDevId));
102     return 0;
103 }
104 
ReadVersionDescriptionInfo(MessageParcel & reply,VersionDescriptionInfo & versionDescriptionInfo)105 int32_t UpdateHelper::ReadVersionDescriptionInfo(MessageParcel &reply,
106     VersionDescriptionInfo &versionDescriptionInfo)
107 {
108     ReadComponentDescriptions(reply, versionDescriptionInfo.componentDescriptions,
109         COUNT_OF(versionDescriptionInfo.componentDescriptions));
110     return 0;
111 }
112 
WriteVersionDescriptionInfo(MessageParcel & data,const VersionDescriptionInfo & versionDescriptionInfo)113 int32_t UpdateHelper::WriteVersionDescriptionInfo(MessageParcel &data,
114     const VersionDescriptionInfo &versionDescriptionInfo)
115 {
116     WriteComponentDescriptions(data, versionDescriptionInfo.componentDescriptions,
117         COUNT_OF(versionDescriptionInfo.componentDescriptions));
118     return 0;
119 }
120 
ReadBusinessError(MessageParcel & reply,BusinessError & businessError)121 int32_t UpdateHelper::ReadBusinessError(MessageParcel &reply, BusinessError &businessError)
122 {
123     businessError.message = Str16ToStr8(reply.ReadString16());
124     businessError.errorNum = static_cast<CallResult>(reply.ReadInt32());
125     ReadErrorMessages(reply, businessError.data, COUNT_OF(businessError.data));
126     return 0;
127 }
128 
WriteBusinessError(MessageParcel & data,const BusinessError & businessError)129 int32_t UpdateHelper::WriteBusinessError(MessageParcel &data, const BusinessError &businessError)
130 {
131     data.WriteString16(Str8ToStr16(businessError.message));
132     data.WriteInt32(static_cast<int32_t>(businessError.errorNum));
133     WriteErrorMessages(data, businessError.data, COUNT_OF(businessError.data));
134     return 0;
135 }
136 
ReadVersionComponents(MessageParcel & reply,VersionComponent versionComponents[],size_t arraySize)137 void ReadVersionComponents(MessageParcel &reply, VersionComponent versionComponents[], size_t arraySize)
138 {
139     int32_t size = reply.ReadInt32();
140     for (size_t i = 0; (i < static_cast<size_t>(size)) && (i < arraySize); i++) {
141         VersionComponent *versionComponent = &versionComponents[i];
142         versionComponent->componentId = Str16ToStr8(reply.ReadString16());
143         versionComponent->componentType = reply.ReadUint32();
144         versionComponent->upgradeAction = Str16ToStr8(reply.ReadString16());
145         versionComponent->displayVersion = Str16ToStr8(reply.ReadString16());
146         versionComponent->innerVersion = Str16ToStr8(reply.ReadString16());
147         versionComponent->size = static_cast<size_t>(reply.ReadUint32());
148         versionComponent->effectiveMode = static_cast<size_t>(reply.ReadUint32());
149 
150         versionComponent->descriptionInfo.descriptionType = static_cast<DescriptionType>(reply.ReadUint32());
151         versionComponent->descriptionInfo.content = Str16ToStr8(reply.ReadString16());
152     }
153 }
154 
WriteVersionComponents(MessageParcel & data,const VersionComponent versionComponents[],size_t arraySize)155 void WriteVersionComponents(MessageParcel &data, const VersionComponent versionComponents[], size_t arraySize)
156 {
157     data.WriteInt32(static_cast<int32_t>(arraySize));
158     for (size_t i = 0; i < arraySize; i++) {
159         const VersionComponent *versionComponent = &versionComponents[i];
160         data.WriteString16(Str8ToStr16(versionComponent->componentId));
161         data.WriteUint32(versionComponent->componentType);
162         data.WriteString16(Str8ToStr16(versionComponent->upgradeAction));
163         data.WriteString16(Str8ToStr16(versionComponent->displayVersion));
164         data.WriteString16(Str8ToStr16(versionComponent->innerVersion));
165         data.WriteUint32(static_cast<uint32_t>(versionComponent->size));
166         data.WriteUint32(static_cast<uint32_t>(versionComponent->effectiveMode));
167 
168         data.WriteUint32(static_cast<uint32_t>(versionComponent->descriptionInfo.descriptionType));
169         data.WriteString16(Str8ToStr16(versionComponent->descriptionInfo.content));
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, COUNT_OF(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, COUNT_OF(newVersionInfo.versionComponents));
183 }
184 
ReadCheckResult(MessageParcel & reply,CheckResultEx & checkResultEx)185 int32_t UpdateHelper::ReadCheckResult(MessageParcel &reply, CheckResultEx &checkResultEx)
186 {
187     checkResultEx.isExistNewVersion = reply.ReadBool();
188     ReadNewVersionInfoEx(reply, checkResultEx.newVersionInfo);
189     return 0;
190 }
191 
WriteCheckResult(MessageParcel & data,const CheckResultEx & checkResultEx)192 int32_t UpdateHelper::WriteCheckResult(MessageParcel &data, const CheckResultEx &checkResultEx)
193 {
194     data.WriteBool(checkResultEx.isExistNewVersion);
195     WriteNewVersionInfoEx(data, checkResultEx.newVersionInfo);
196     return 0;
197 }
198 
ReadNewVersionInfo(MessageParcel & reply,NewVersionInfo & newVersionInfo)199 int32_t UpdateHelper::ReadNewVersionInfo(MessageParcel &reply, NewVersionInfo &newVersionInfo)
200 {
201     ReadNewVersionInfoEx(reply, newVersionInfo);
202     return 0;
203 }
204 
WriteNewVersionInfo(MessageParcel & data,const NewVersionInfo & newVersionInfo)205 int32_t UpdateHelper::WriteNewVersionInfo(MessageParcel &data, const NewVersionInfo &newVersionInfo)
206 {
207     WriteNewVersionInfoEx(data, newVersionInfo);
208     return 0;
209 }
210 
ReadCurrentVersionInfo(MessageParcel & reply,CurrentVersionInfo & info)211 int32_t UpdateHelper::ReadCurrentVersionInfo(MessageParcel &reply, CurrentVersionInfo &info)
212 {
213     ENGINE_LOGI("ReadCurrentVersionInfo");
214     info.osVersion = Str16ToStr8(reply.ReadString16());
215     info.deviceName = Str16ToStr8(reply.ReadString16());
216     ReadVersionComponents(reply, info.versionComponents, COUNT_OF(info.versionComponents));
217     return 0;
218 }
219 
WriteCurrentVersionInfo(MessageParcel & data,const CurrentVersionInfo & info)220 int32_t UpdateHelper::WriteCurrentVersionInfo(MessageParcel &data, const CurrentVersionInfo &info)
221 {
222     ENGINE_LOGI("WriteCurrentVersionInfo");
223     data.WriteString16(Str8ToStr16(info.osVersion));
224     data.WriteString16(Str8ToStr16(info.deviceName));
225     WriteVersionComponents(data, info.versionComponents, COUNT_OF(info.versionComponents));
226     return 0;
227 }
228 
ReadTaskBody(MessageParcel & reply,TaskBody & taskBody)229 void ReadTaskBody(MessageParcel &reply, TaskBody &taskBody)
230 {
231     taskBody.versionDigestInfo.versionDigest = Str16ToStr8(reply.ReadString16());
232     taskBody.status = static_cast<UpgradeStatus>(reply.ReadInt32());
233     taskBody.subStatus = reply.ReadInt32();
234     taskBody.progress = reply.ReadUint32();
235     taskBody.installMode = reply.ReadInt32();
236     ReadErrorMessages(reply, taskBody.errorMessages, COUNT_OF(taskBody.errorMessages));
237     ReadVersionComponents(reply, taskBody.versionComponents, COUNT_OF(taskBody.versionComponents));
238 }
239 
WriteTaskBody(MessageParcel & data,const TaskBody & taskBody)240 void WriteTaskBody(MessageParcel &data, const TaskBody &taskBody)
241 {
242     data.WriteString16(Str8ToStr16(taskBody.versionDigestInfo.versionDigest));
243     data.WriteInt32(static_cast<int32_t>(taskBody.status));
244     data.WriteInt32(taskBody.subStatus);
245     data.WriteUint32(taskBody.progress);
246     data.WriteInt32(taskBody.installMode);
247     WriteErrorMessages(data, taskBody.errorMessages, COUNT_OF(taskBody.errorMessages));
248     WriteVersionComponents(data, taskBody.versionComponents, COUNT_OF(taskBody.versionComponents));
249 }
250 
ReadTaskInfo(MessageParcel & reply,TaskInfo & info)251 int32_t UpdateHelper::ReadTaskInfo(MessageParcel &reply, TaskInfo &info)
252 {
253     ENGINE_LOGI("ReadTaskInfo");
254     info.existTask = reply.ReadBool();
255     ReadTaskBody(reply, info.taskBody);
256     return 0;
257 }
258 
WriteTaskInfo(MessageParcel & data,const TaskInfo & info)259 int32_t UpdateHelper::WriteTaskInfo(MessageParcel &data, const TaskInfo &info)
260 {
261     ENGINE_LOGI("WriteTaskInfo");
262     data.WriteBool(info.existTask);
263     WriteTaskBody(data, info.taskBody);
264     return 0;
265 }
266 
ReadUpgradePolicy(MessageParcel & reply,UpgradePolicy & policy)267 int32_t UpdateHelper::ReadUpgradePolicy(MessageParcel &reply, UpgradePolicy &policy)
268 {
269     policy.downloadStrategy = static_cast<bool>(reply.ReadBool());
270     policy.autoUpgradeStrategy = static_cast<bool>(reply.ReadBool());
271     size_t size = static_cast<size_t>(reply.ReadInt32());
272     size_t arraySize = COUNT_OF(policy.autoUpgradePeriods);
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 UpdateHelper::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 UpdateHelper::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 UpdateHelper::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 UpdateHelper::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 UpdateHelper::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 UpdateHelper::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 UpdateHelper::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 UpdateHelper::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 UpdateHelper::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 UpdateHelper::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 UpdateHelper::WritePauseDownloadOptions(MessageParcel &data, const PauseDownloadOptions &pauseDownloadOptions)
354 {
355     data.WriteBool(pauseDownloadOptions.isAllowAutoResume);
356     return 0;
357 }
358 
ReadResumeDownloadOptions(MessageParcel & reply,ResumeDownloadOptions & resumeDownloadOptions)359 int32_t UpdateHelper::ReadResumeDownloadOptions(MessageParcel &reply, ResumeDownloadOptions &resumeDownloadOptions)
360 {
361     resumeDownloadOptions.allowNetwork = static_cast<NetType>(reply.ReadUint32());
362     return 0;
363 }
364 
WriteResumeDownloadOptions(MessageParcel & data,const ResumeDownloadOptions & resumeDownloadOptions)365 int32_t UpdateHelper::WriteResumeDownloadOptions(MessageParcel &data,
366     const ResumeDownloadOptions &resumeDownloadOptions)
367 {
368     data.WriteUint32(static_cast<uint32_t>(resumeDownloadOptions.allowNetwork));
369     return 0;
370 }
371 
ReadUpgradeOptions(MessageParcel & reply,UpgradeOptions & upgradeOptions)372 int32_t UpdateHelper::ReadUpgradeOptions(MessageParcel &reply, UpgradeOptions &upgradeOptions)
373 {
374     upgradeOptions.order = static_cast<Order>(reply.ReadUint32());
375     return 0;
376 }
377 
WriteUpgradeOptions(MessageParcel & data,const UpgradeOptions & upgradeOptions)378 int32_t UpdateHelper::WriteUpgradeOptions(MessageParcel &data, const UpgradeOptions &upgradeOptions)
379 {
380     data.WriteUint32(static_cast<uint32_t>(upgradeOptions.order));
381     return 0;
382 }
383 
ReadClearOptions(MessageParcel & reply,ClearOptions & clearOptions)384 int32_t UpdateHelper::ReadClearOptions(MessageParcel &reply, ClearOptions &clearOptions)
385 {
386     clearOptions.status = static_cast<UpgradeStatus>(reply.ReadUint32());
387     return 0;
388 }
389 
WriteClearOptions(MessageParcel & data,const ClearOptions & clearOptions)390 int32_t UpdateHelper::WriteClearOptions(MessageParcel &data, const ClearOptions &clearOptions)
391 {
392     data.WriteUint32(static_cast<uint32_t>(clearOptions.status));
393     return 0;
394 }
395 
JudgeLevel(const UpdateLogLevel & level)396 bool UpdateHelper::JudgeLevel(const UpdateLogLevel& level)
397 {
398     const UpdateLogLevel& curLevel = UpdateHelper::GetLogLevel();
399     if (level <= curLevel) {
400         return true;
401     }
402     return true;
403 }
404 
GetBriefFileName(const std::string & file)405 std::string UpdateHelper::GetBriefFileName(const std::string &file)
406 {
407     auto pos = file.find_last_of("/");
408     if (pos != std::string::npos) {
409         return file.substr(pos + 1);
410     }
411 
412     pos = file.find_last_of("\\");
413     if (pos != std::string::npos) {
414         return file.substr(pos + 1);
415     }
416 
417     return file;
418 }
419 
SplitString(const std::string & str,const std::string & delimiter)420 std::vector<std::string> UpdateHelper::SplitString(const std::string &str, const std::string &delimiter)
421 {
422     std::vector<std::string> result;
423     ENGINE_CHECK(!str.empty(), return result, "string is empty");
424 
425     size_t found = std::string::npos;
426     size_t start = 0;
427     while (true) {
428         found = str.find_first_of(delimiter, start);
429         result.push_back(str.substr(start, found - start));
430         if (found == std::string::npos) {
431             break;
432         }
433         start = found + 1;
434     }
435     return result;
436 }
437 
IsErrorExist(const BusinessError & businessError)438 bool UpdateHelper::IsErrorExist(const BusinessError &businessError)
439 {
440     return businessError.errorNum != CallResult::SUCCESS;
441 }
442 
CompareVersion(const std::string & version1,const std::string & version2)443 int32_t UpdateHelper::CompareVersion(const std::string &version1, const std::string &version2)
444 {
445     std::vector<std::string> result1 = SplitString(version1, ".");
446     std::vector<std::string> result2 = SplitString(version2, ".");
447     if (result1.size() != result2.size()) {
448         return ((result1.size() > result2.size()) ? -1 : 1);
449     }
450 
451     for (size_t i = 1; i < result1.size(); i++) {
452         long long ver1 = std::stoll(result1[i]);
453         long long ver2 = std::stoll(result2[i]);
454         if (ver1 == ver2) {
455             continue;
456         }
457         return ((ver1 > ver2) ? 1 : -1);
458     }
459     return 0;
460 }
461 
HexToDegist(const std::string & str)462 std::vector<uint8_t> UpdateHelper::HexToDegist(const std::string &str)
463 {
464     std::vector<uint8_t> result;
465     for (size_t i = 0; i < str.length(); i += HEX_DIGEST_NUM) {
466         std::string byte = str.substr(i, HEX_DIGEST_NUM);
467         long byteToLong = strtol(byte.c_str(), nullptr, HEX_DIGEST_BASE);
468         if (byteToLong == 0) {
469             return result;
470         }
471         auto chr = static_cast<uint8_t>(static_cast<int>(byteToLong));
472         result.push_back(chr);
473     }
474     return result;
475 }
476 
Anonymization(const std::string & src)477 std::string UpdateHelper::Anonymization(const std::string &src)
478 {
479     int len = static_cast<int>(src.length());
480     if (len <= DATA_ANONYMIZATION_LEN) {
481         return std::string("");
482     }
483     return std::string("*****") + src.substr(0, DATA_ANONYMIZATION_LEN);
484 }
485 
BuildEventVersionInfo(const VersionInfo & ver)486 std::string UpdateHelper::BuildEventVersionInfo(const VersionInfo &ver)
487 {
488     return std::string("{") + std::string("pkgSize: ") + std::to_string(ver.result[0].size)
489         + std::string(", packageType: ") + std::to_string(ver.result[0].packageType)
490         + std::string(", versionCode: ") + ver.result[0].versionCode + std::string(" }");
491 }
492 
BuildEventDevId(const UpgradeInfo & info)493 std::string UpdateHelper::BuildEventDevId(const UpgradeInfo &info)
494 {
495     return std::string("{") + std::string("upgradeDevId: ") + info.upgradeDevId
496         + std::string(", controlDevId: ") + info.controlDevId + std::string(" }");
497 }
498 } // namespace UpdateEngine
499 } // namespace OHOS
500