• 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 <fstream>
20 #include <iostream>
21 #include <string>
22 #include <unistd.h>
23 
24 #include "hilog/log.h"
25 #include "parcel.h"
26 #include "securec.h"
27 
28 namespace OHOS {
29 namespace update_engine {
30 #ifdef UPDATE_SERVICE
31 const std::string LOG_LABEL = "update_engine";
32 const std::string LOG_NAME = "/data/update_service_log.txt";
33 #else
34 const std::string LOG_LABEL = "update_engine";
35 const std::string LOG_NAME = "/data/update_client.log.txt";
36 #endif
37 constexpr int HEX_DIGEST_NUM = 2;
38 constexpr int HEX_DIGEST_BASE = 16;
39 
40 UpdateLogLevel UpdateHelper::level_ = UpdateLogLevel::UPDATE_INFO;
41 
WriteUpdateContext(MessageParcel & data,const UpdateContext & info)42 int32_t UpdateHelper::WriteUpdateContext(MessageParcel &data, const UpdateContext &info)
43 {
44     data.WriteString16(Str8ToStr16(info.upgradeDevId));
45     data.WriteString16(Str8ToStr16(info.controlDevId));
46     data.WriteString16(Str8ToStr16(info.upgradeApp));
47     data.WriteString16(Str8ToStr16(info.upgradeFile));
48     data.WriteString16(Str8ToStr16(info.type));
49     return 0;
50 }
51 
ReadUpdateContext(MessageParcel & reply,UpdateContext & info)52 int32_t UpdateHelper::ReadUpdateContext(MessageParcel &reply, UpdateContext &info)
53 {
54     info.upgradeDevId = Str16ToStr8(reply.ReadString16());
55     info.controlDevId = Str16ToStr8(reply.ReadString16());
56     info.upgradeApp = Str16ToStr8(reply.ReadString16());
57     info.upgradeFile = Str16ToStr8(reply.ReadString16());
58     info.type = Str16ToStr8(reply.ReadString16());
59     return 0;
60 }
61 
WriteVersionInfo(MessageParcel & data,const VersionInfo & info)62 int32_t UpdateHelper::WriteVersionInfo(MessageParcel &data, const VersionInfo &info)
63 {
64     data.WriteInt32(static_cast<int32_t>(info.status));
65     data.WriteString16(Str8ToStr16(info.errMsg));
66     data.WriteInt32(static_cast<int32_t>(sizeof(info.result) / sizeof(info.result[0])));
67     for (size_t i = 0; i < sizeof(info.result) / sizeof(info.result[0]); i++) {
68         data.WriteUint64(static_cast<uint64_t>(info.result[i].size));
69         data.WriteInt32(static_cast<int32_t>(info.result[i].packageType));
70 
71         data.WriteString16(Str8ToStr16(info.result[i].versionName));
72         data.WriteString16(Str8ToStr16(info.result[i].versionCode));
73         data.WriteString16(Str8ToStr16(info.result[i].verifyInfo));
74         data.WriteString16(Str8ToStr16(info.result[i].descriptPackageId));
75     }
76     data.WriteInt32(static_cast<int32_t>(sizeof(info.descriptInfo) / sizeof(info.descriptInfo[0])));
77     for (size_t i = 0; i < sizeof(info.descriptInfo) / sizeof(info.descriptInfo[0]); i++) {
78         data.WriteString16(Str8ToStr16(info.descriptInfo[i].descriptPackageId));
79         data.WriteString16(Str8ToStr16(info.descriptInfo[i].content));
80     }
81     return 0;
82 }
83 
ReadVersionInfo(MessageParcel & reply,VersionInfo & info)84 int32_t UpdateHelper::ReadVersionInfo(MessageParcel &reply, VersionInfo &info)
85 {
86     info.status = static_cast<SearchStatus>(reply.ReadInt32());
87     info.errMsg = Str16ToStr8(reply.ReadString16());
88 
89     int32_t count = reply.ReadInt32();
90     for (size_t i = 0; (i < static_cast<size_t>(count)) && (i < sizeof(info.result) / sizeof(info.result[0])); i++) {
91         uint64_t uint64 = reply.ReadUint64();
92         info.result[i].size = static_cast<size_t>(uint64);
93         info.result[i].packageType = static_cast<PackageType>(reply.ReadInt32());
94 
95         info.result[i].versionName = Str16ToStr8(reply.ReadString16());
96         info.result[i].versionCode = Str16ToStr8(reply.ReadString16());
97         info.result[i].verifyInfo = Str16ToStr8(reply.ReadString16());
98         info.result[i].descriptPackageId = Str16ToStr8(reply.ReadString16());
99     }
100     count = reply.ReadInt32();
101     for (size_t i = 0; (i < static_cast<size_t>(count))
102         && (i < sizeof(info.descriptInfo) / sizeof(info.descriptInfo[0])); i++) {
103         info.descriptInfo[i].descriptPackageId = Str16ToStr8(reply.ReadString16());
104         info.descriptInfo[i].content = Str16ToStr8(reply.ReadString16());
105     }
106     return 0;
107 }
108 
WriteUpdatePolicy(MessageParcel & data,const UpdatePolicy & policy)109 int32_t UpdateHelper::WriteUpdatePolicy(MessageParcel &data, const UpdatePolicy &policy)
110 {
111     data.WriteBool(policy.autoDownload);
112     data.WriteBool(policy.autoDownloadNet);
113     data.WriteInt32(static_cast<int32_t>(policy.mode));
114     data.WriteInt32(static_cast<int32_t>(policy.autoUpgradeCondition));
115     data.WriteInt32(static_cast<int32_t>(sizeof(policy.autoUpgradeInterval) / sizeof(policy.autoUpgradeInterval[0])));
116     for (size_t i = 0; i < sizeof(policy.autoUpgradeInterval) / sizeof(policy.autoUpgradeInterval[0]); i++) {
117         data.WriteUint32(policy.autoUpgradeInterval[i]);
118     }
119     return 0;
120 }
121 
ReadUpdatePolicy(MessageParcel & data,UpdatePolicy & policy)122 int32_t UpdateHelper::ReadUpdatePolicy(MessageParcel &data, UpdatePolicy &policy)
123 {
124     policy.autoDownload = static_cast<bool>(data.ReadBool());
125     policy.autoDownloadNet = static_cast<bool>(data.ReadBool());
126     policy.mode = static_cast<InstallMode>(data.ReadInt32());
127     policy.autoUpgradeCondition = static_cast<AutoUpgradeCondition>(data.ReadInt32());
128     int32_t count = data.ReadInt32();
129     for (size_t i = 0; (i < static_cast<size_t>(count)) && (i <
130         sizeof(policy.autoUpgradeInterval) / sizeof(policy.autoUpgradeInterval[0])); i++) {
131         policy.autoUpgradeInterval[i] = data.ReadUint32();
132     }
133     return 0;
134 }
135 
ReadUpgradeInfo(MessageParcel & reply,UpgradeInfo & info)136 int32_t UpdateHelper::ReadUpgradeInfo(MessageParcel &reply, UpgradeInfo &info)
137 {
138     info.status = static_cast<UpgradeStatus>(reply.ReadInt32());
139     return 0;
140 }
141 
WriteUpgradeInfo(MessageParcel & data,const UpgradeInfo & info)142 int32_t UpdateHelper::WriteUpgradeInfo(MessageParcel &data, const UpgradeInfo &info)
143 {
144     data.WriteInt32(info.status);
145     return 0;
146 }
147 
ReadUpdateProgress(MessageParcel & reply,Progress & info)148 int32_t UpdateHelper::ReadUpdateProgress(MessageParcel &reply, Progress &info)
149 {
150     info.percent = static_cast<uint32_t>(reply.ReadUint32());
151     info.status = static_cast<UpgradeStatus>(reply.ReadInt32());
152     info.endReason = Str16ToStr8(reply.ReadString16());
153     return 0;
154 }
155 
WriteUpdateProgress(MessageParcel & data,const Progress & info)156 int32_t UpdateHelper::WriteUpdateProgress(MessageParcel &data, const Progress &info)
157 {
158     data.WriteUint32(info.percent);
159     data.WriteInt32(static_cast<int32_t>(info.status));
160     data.WriteString16(Str8ToStr16(info.endReason));
161     return 0;
162 }
163 
CopyVersionInfo(const VersionInfo & srcInfo,VersionInfo & dstInfo)164 int32_t UpdateHelper::CopyVersionInfo(const VersionInfo &srcInfo, VersionInfo &dstInfo)
165 {
166     dstInfo.status = srcInfo.status;
167     dstInfo.errMsg = srcInfo.errMsg;
168     for (size_t i = 0; i < sizeof(dstInfo.result) / sizeof(dstInfo.result[0]); i++) {
169         dstInfo.result[i].size = srcInfo.result[i].size;
170         dstInfo.result[i].packageType = srcInfo.result[i].packageType;
171         dstInfo.result[i].versionName = srcInfo.result[i].versionName;
172         dstInfo.result[i].versionCode = srcInfo.result[i].versionCode;
173         dstInfo.result[i].verifyInfo = srcInfo.result[i].verifyInfo;
174         dstInfo.result[i].descriptPackageId = srcInfo.result[i].descriptPackageId;
175     }
176     for (size_t i = 0; i < sizeof(dstInfo.descriptInfo) / sizeof(dstInfo.descriptInfo[0]); i++) {
177         dstInfo.descriptInfo[i].content = srcInfo.descriptInfo[i].content;
178         dstInfo.descriptInfo[i].descriptPackageId = srcInfo.descriptInfo[i].descriptPackageId;
179     }
180     return 0;
181 }
182 
CopyUpdatePolicy(const UpdatePolicy & srcInfo,UpdatePolicy & dstInfo)183 int32_t UpdateHelper::CopyUpdatePolicy(const UpdatePolicy &srcInfo, UpdatePolicy &dstInfo)
184 {
185     dstInfo.autoDownload = srcInfo.autoDownload;
186     dstInfo.autoDownloadNet = srcInfo.autoDownloadNet;
187     dstInfo.mode = srcInfo.mode;
188     dstInfo.autoUpgradeCondition = srcInfo.autoUpgradeCondition;
189     for (size_t i = 0; i < sizeof(dstInfo.autoUpgradeInterval) / sizeof(dstInfo.autoUpgradeInterval[0]); i++) {
190         dstInfo.autoUpgradeInterval[i] = srcInfo.autoUpgradeInterval[i];
191     }
192     return 0;
193 }
194 
JudgeLevel(const UpdateLogLevel & level)195 bool UpdateHelper::JudgeLevel(const UpdateLogLevel& level)
196 {
197     const UpdateLogLevel& curLevel = UpdateHelper::GetLogLevel();
198     if (level <= curLevel) {
199         return true;
200     }
201     return true;
202 }
203 
GetBriefFileName(const std::string & file)204 std::string UpdateHelper::GetBriefFileName(const std::string &file)
205 {
206     auto pos = file.find_last_of("/");
207     if (pos != std::string::npos) {
208         return file.substr(pos + 1);
209     }
210 
211     pos = file.find_last_of("\\");
212     if (pos != std::string::npos) {
213         return file.substr(pos + 1);
214     }
215 
216     return file;
217 }
218 
SplitString(const std::string & str,const std::string & delimiter)219 std::vector<std::string> UpdateHelper::SplitString(const std::string &str, const std::string &delimiter)
220 {
221     std::vector<std::string> result;
222     ENGINE_CHECK(!str.empty(), return result, "string is empty");
223 
224     size_t found = std::string::npos;
225     size_t start = 0;
226     while (true) {
227         found = str.find_first_of(delimiter, start);
228         result.push_back(str.substr(start, found - start));
229         if (found == std::string::npos) {
230             break;
231         }
232         start = found + 1;
233     }
234     return result;
235 }
236 
CompareVersion(const std::string & version1,const std::string & version2)237 int32_t UpdateHelper::CompareVersion(const std::string &version1, const std::string &version2)
238 {
239     std::vector<std::string> result1 = SplitString(version1, ".");
240     std::vector<std::string> result2 = SplitString(version2, ".");
241     if (result1.size() != result2.size()) {
242         return ((result1.size() > result2.size()) ? -1 : 1);
243     }
244 
245     for (size_t i = 1; i < result1.size(); i++) {
246         long long ver1 = std::stoll(result1[i]);
247         long long ver2 = std::stoll(result2[i]);
248         if (ver1 == ver2) {
249             continue;
250         }
251         return ((ver1 > ver2) ? 1 : -1);
252     }
253     return 0;
254 }
255 
HexToDegist(const std::string & str)256 std::vector<uint8_t> UpdateHelper::HexToDegist(const std::string &str)
257 {
258     std::vector<uint8_t> result;
259     for (size_t i = 0; i < str.length(); i += HEX_DIGEST_NUM) {
260         std::string byte = str.substr(i, HEX_DIGEST_NUM);
261         auto chr = static_cast<uint8_t>(static_cast<int>(strtol(byte.c_str(), nullptr, HEX_DIGEST_BASE)));
262         result.push_back(chr);
263     }
264     return result;
265 }
266 }
267 } // namespace OHOS
268