• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025-2025 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 "params_trust_list.h"
17 #include <algorithm>
18 
19 #include "constant.h"
20 #include "params.h"
21 #include "string_utils.h"
22 #include "help.h"
23 
24 namespace OHOS {
25 namespace SignatureTools {
26 const std::string options = "[options]:";
27 
28 const std::vector<std::string> commands = {
29     SIGN_ELF + options,
30     VERIFY_ELF + options,
31 };
32 
GetInstance()33 ParamsTrustList ParamsTrustList::GetInstance()
34 {
35     static ParamsTrustList instance;
36     return instance;
37 }
38 
PutTrustMap(const std::string & cmdStandBy,const std::string & param)39 void ParamsTrustList::PutTrustMap(const std::string& cmdStandBy, const std::string& param)
40 {
41     if (param.at(0) == '-') {
42         size_t pos = param.find(':');
43         std::string subParam = param.substr(0, pos);
44         subParam = StringUtils::Trim(subParam);
45         bool isExists = false;
46         if (trustMap.find(cmdStandBy) != trustMap.end()) {
47             isExists = true;
48         }
49         std::vector<std::string> trustList = isExists ? trustMap[cmdStandBy] : std::vector<std::string>();
50         trustList.push_back(subParam);
51         trustMap[cmdStandBy] = trustList;
52     }
53 }
54 
ReadHelpParam(std::istringstream & fd)55 void ParamsTrustList::ReadHelpParam(std::istringstream& fd)
56 {
57     std::string str;
58     std::string cmdStandBy;
59     while (std::getline(fd, str)) {
60         bool isExists = false;
61         std::string params = StringUtils::Trim(str);
62         if (params.empty()) {
63             continue;
64         }
65         isExists = std::any_of(commands.begin(), commands.end(),
66                                [params](const std::string& cmd) {return cmd == params; });
67         if (isExists) {
68             cmdStandBy = params;
69         } else {
70             PutTrustMap(cmdStandBy, params);
71         }
72     }
73 }
74 
GenerateTrustList()75 void ParamsTrustList::GenerateTrustList()
76 {
77     std::istringstream iss(HELP_TXT);
78     ReadHelpParam(iss);
79 }
80 
GetTrustList(const std::string & command)81 std::vector<std::string> ParamsTrustList::GetTrustList(const std::string& command)
82 {
83     std::string keyParam = command + options;
84     GenerateTrustList();
85     if (trustMap.find(keyParam) != trustMap.end()) {
86         return trustMap[keyParam];
87     } else {
88         PrintErrorNumberMsg("COMMAND_ERROR", COMMAND_ERROR, "'" + command + " is not trust command");
89         trustMap[keyParam].clear();
90         return trustMap[keyParam];
91     }
92 }
93 } // namespace SignatureTools
94 } // namespace OHOS