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 "firmware_combine_version_utils.h"
17
18 #include "firmware_log.h"
19 #include "device_adapter.h"
20
21 namespace OHOS {
22 namespace UpdateEngine {
GetPackageVersion(std::string & baseVersion,std::string & custVersion,std::string & preloadVersion)23 std::string CombinePackageVersionUtils::GetPackageVersion(std::string &baseVersion, std::string &custVersion,
24 std::string &preloadVersion)
25 {
26 std::string version;
27 std::string base = HandleBaseVersion(baseVersion);
28 std::string baseLog;
29 std::string log;
30 HandleBaseVersionLog(baseVersion, baseLog, log);
31 std::string cust = HandleCustVersion(custVersion);
32 std::string preload = HandlePreloadVersion(preloadVersion);
33 version.append(DeviceAdapter::GetDeviceName()).append(base).append("(")
34 .append(baseLog).append(cust).append(preload).append(log).append(")");
35 return version;
36 }
37
HandleBaseVersion(std::string & baseVersion)38 std::string CombinePackageVersionUtils::HandleBaseVersion(std::string &baseVersion)
39 {
40 std::string::size_type start = baseVersion.find(" ");
41 if (start == std::string::npos) {
42 return "";
43 }
44 std::string::size_type end = baseVersion.find("(");
45 std::string base;
46 if (end == std::string::npos) {
47 base = baseVersion.substr(start);
48 } else {
49 base = baseVersion.substr(start, end - start);
50 }
51 return base;
52 }
53
HandleBaseVersionLog(std::string & baseVersion,std::string & base,std::string & log)54 void CombinePackageVersionUtils::HandleBaseVersionLog(std::string &baseVersion, std::string &base, std::string &log)
55 {
56 std::string::size_type start = baseVersion.find_last_of("(");
57 std::string::size_type end = baseVersion.find_last_of(")");
58 if ((start == std::string::npos) || (end == std::string::npos)) {
59 return;
60 }
61 start++;
62 std::string::size_type mid = start;
63 bool isNumbers = false;
64 while (mid < end) {
65 mid++;
66 if (isdigit(baseVersion[mid])) {
67 isNumbers = true;
68 } else {
69 if (isNumbers) {
70 break;
71 }
72 }
73 }
74 if ((mid == end) && (!isNumbers)) {
75 log = baseVersion.substr(start, mid - start);
76 base = "";
77 return;
78 }
79 base = baseVersion.substr(start, mid - start);
80 log = baseVersion.substr(mid, end - mid);
81 }
82
HandleCustVersion(std::string & custVersion)83 std::string CombinePackageVersionUtils::HandleCustVersion(std::string &custVersion)
84 {
85 if (custVersion.empty()) {
86 return "";
87 }
88 std::string::size_type mid = custVersion.find_last_of("(");
89 std::string::size_type end = custVersion.find_last_of(")");
90 std::string::size_type start = custVersion.find_last_of(".");
91 if ((start == std::string::npos) || (mid == std::string::npos) || (end == std::string::npos) ||
92 (end <= mid + 1) || (mid <= start + 1) || (end >= INT_MAX) || (mid >= INT_MAX)) {
93 return "";
94 }
95 std::string cust = custVersion.substr(mid + 1, end - mid - 1);
96 std::string getEnum = custVersion.substr(start + 1, mid - start - 1);
97 cust.append("E").append(getEnum);
98 return cust;
99 }
100
HandlePreloadVersion(std::string & preloadVersion)101 std::string CombinePackageVersionUtils::HandlePreloadVersion(std::string &preloadVersion)
102 {
103 if (preloadVersion.empty()) {
104 return "";
105 }
106 std::string::size_type start = preloadVersion.find_last_of("R");
107 std::string::size_type end = preloadVersion.find_last_of(")");
108 if ((start == std::string::npos) || (end == std::string::npos) || (end <= start) || (end >= INT_MAX)) {
109 return "";
110 }
111 std::string preload = preloadVersion.substr(start, end - start);
112 start = preloadVersion.find_last_of(".");
113 end = preloadVersion.find_last_of("(");
114 if ((start == std::string::npos) || (end == std::string::npos) || (end <= start + 1) || (end >= INT_MAX)) {
115 return "";
116 }
117 std::string getPnum = preloadVersion.substr(start + 1, end - start - 1);
118 preload.append("P").append(getPnum);
119 return preload;
120 }
121 } // namespace UpdateEngine
122 } // namespace OHOS
123