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 std::string::size_type mid = custVersion.find_last_of("(");
86 std::string::size_type end = custVersion.find_last_of(")");
87 std::string::size_type start = custVersion.find_last_of(".");
88 if ((start == std::string::npos) || (mid == std::string::npos) || (end == std::string::npos)) {
89 return "";
90 }
91 std::string cust = custVersion.substr(mid + 1, end - mid - 1);
92 std::string getEnum = custVersion.substr(start + 1, mid - start - 1);
93 cust.append("E").append(getEnum);
94 return cust;
95 }
96
HandlePreloadVersion(std::string & preloadVersion)97 std::string CombinePackageVersionUtils::HandlePreloadVersion(std::string &preloadVersion)
98 {
99 std::string::size_type start = preloadVersion.find_last_of("R");
100 std::string::size_type end = preloadVersion.find_last_of(")");
101 if ((start == std::string::npos) || (end == std::string::npos)) {
102 return "";
103 }
104 std::string preload = preloadVersion.substr(start, end - start);
105 start = preloadVersion.find_last_of(".");
106 end = preloadVersion.find_last_of("(");
107 if ((start == std::string::npos) || (end == std::string::npos)) {
108 return "";
109 }
110 std::string getPnum = preloadVersion.substr(start + 1, end - start - 1);
111 preload.append("P").append(getPnum);
112 return preload;
113 }
114 } // namespace UpdateEngine
115 } // namespace OHOS
116