• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef SEHAP_CONTEXTS_TRIE_H
17 #define SEHAP_CONTEXTS_TRIE_H
18 
19 #include <string>
20 #include <unordered_map>
21 
22 #ifdef MCS_ENABLE
23 typedef enum LevelFrom {
24     LEVELFROM_NONE = 0,
25     LEVELFROM_APP,
26     LEVELFROM_USER,
27     LEVELFROM_ALL,
28 } LevelFrom;
29 #endif
30 
31 typedef struct SehapContextInfo {
32     std::string context = "";
33     bool isDomain = false;
34 #ifdef MCS_ENABLE
35     LevelFrom levelFrom = LEVELFROM_NONE;
36     std::string user = "u";
37 #endif
38 } SehapContextInfo;
39 
40 typedef struct SehapInsertParamInfo {
41 #ifdef MCS_ENABLE
42     LevelFrom levelFrom = LEVELFROM_NONE;
43     std::string user = "u";
44 #endif
45     std::string domain = "";
46     std::string type = "";
47     std::string extension = "";
48 } SehapInsertParamInfo;
49 
50 typedef struct ExtensionInfo {
51     std::string domain;
52 #ifdef MCS_ENABLE
53     LevelFrom levelFrom = LEVELFROM_NONE;
54     std::string user;
55 #endif
56 } ExtensionInfo;
57 
58 typedef struct NodeTypeInfo {
59     bool isEnd = false;
60     std::string domain;
61     std::string type;
62 #ifdef MCS_ENABLE
63     LevelFrom levelFrom = LEVELFROM_NONE;
64     std::string user;
65 #endif
66     std::unordered_map<std::string, ExtensionInfo> extensionMap;
67 
68     void Insert(const SehapInsertParamInfo &paramInfo);
69     SehapContextInfo Search(bool isDomain, const std::string& extension) const;
70 } NodeTypeInfo;
71 
72 class SehapContextsTrie {
73 public:
SehapContextsTrie()74     SehapContextsTrie() {};
~SehapContextsTrie()75     ~SehapContextsTrie() {};
76 
77     bool Insert(const std::string &paraName, const SehapInsertParamInfo &paramInfo);
78     SehapContextInfo Search(const std::string &paraName, bool isDomain, const std::string &extension = "");
79     void Clear();
80 
81     NodeTypeInfo prefixInfo;
82     NodeTypeInfo matchedInfo;
83 
84 private:
85     std::vector<std::string> SplitString(const std::string &paraName);
86     SehapContextsTrie* FindChild(const std::string &element);
87     std::unordered_map<std::string, SehapContextsTrie *> children;
88 };
89 #endif // SEHAP_CONTEXTS_TRIE_H
90