1 /*
2  * Copyright (c) 2022 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 #ifndef BASE_STARTUP_PARAM_INCLUDE_H
16 #define BASE_STARTUP_PARAM_INCLUDE_H
17 #include <stdio.h>
18 
19 #include "param_osadp.h"
20 #include "param_trie.h"
21 
22 #ifdef __cplusplus
23 #if __cplusplus
24 extern "C" {
25 #endif
26 #endif
27 
GetNextKey(const char ** remainingKey,char ** subKey,uint32_t * subKeyLen,const char * end)28 STATIC_INLINE void GetNextKey(const char **remainingKey, char **subKey, uint32_t *subKeyLen, const char *end)
29 {
30     *subKey = strchr(*remainingKey, '.');
31     if (*subKey != NULL) {
32         *subKeyLen = *subKey - *remainingKey;
33     } else {
34         *subKeyLen = end - *remainingKey;
35     }
36 }
37 
FindSubTrie(const WorkSpace * workSpace,ParamTrieNode * current,const char * key,uint32_t keyLen,uint32_t * matchLabel)38 STATIC_INLINE ParamTrieNode *FindSubTrie(const WorkSpace *workSpace,
39     ParamTrieNode *current, const char *key, uint32_t keyLen, uint32_t *matchLabel)
40 {
41     ParamTrieNode *subTrie = current;
42     int ret = 0;
43     while (subTrie != NULL) {
44         if (subTrie->length > keyLen) {
45             ret = -1;
46         } else if (subTrie->length < keyLen) {
47             ret = 1;
48         } else {
49             ret = memcmp(subTrie->key, key, keyLen);
50             if (ret == 0) {
51                 PARAM_CHECK(matchLabel != NULL, return NULL, "Invalid matchLabel");
52                 *matchLabel = (subTrie->labelIndex != 0) ? subTrie->labelIndex : *matchLabel;
53                 return subTrie;
54             }
55         }
56 
57         uint32_t offset = 0;
58         if (ret < 0) {
59             offset = subTrie->left;
60         } else {
61             offset = subTrie->right;
62         }
63         if (offset == 0 || offset > workSpace->area->dataSize) {
64             return NULL;
65         }
66         subTrie = (ParamTrieNode *)(workSpace->area->data + offset);
67     }
68     return NULL;
69 }
70 
FindTrieNode_(const WorkSpace * workSpace,const char * key,uint32_t keyLen,uint32_t * matchLabel)71 STATIC_INLINE ParamTrieNode *FindTrieNode_(
72     const WorkSpace *workSpace, const char *key, uint32_t keyLen, uint32_t *matchLabel)
73 {
74     const char *remainingKey = key;
75     ParamTrieNode *current = GetTrieRoot(workSpace);
76     PARAM_CHECK(current != NULL, return NULL, "Invalid current param %s", key);
77     *matchLabel = current->labelIndex;
78     const char *end = key + keyLen;
79     while (1) {
80         uint32_t subKeyLen = 0;
81         char *subKey = NULL;
82         GetNextKey(&remainingKey, &subKey, &subKeyLen, end);
83         if (!subKeyLen) {
84             return NULL;
85         }
86         if (current->child != 0) {
87             ParamTrieNode *next = GetTrieNode(workSpace, current->child);
88             current = FindSubTrie(workSpace, next, remainingKey, subKeyLen, matchLabel);
89         } else {
90             current = FindSubTrie(workSpace, current, remainingKey, subKeyLen, matchLabel);
91         }
92         if (current == NULL) {
93             return NULL;
94         } else if (current->labelIndex != 0) {
95             *matchLabel = current->labelIndex;
96         }
97         if (subKey == NULL || strcmp(subKey, ".") == 0) {
98             break;
99         }
100         remainingKey = subKey + 1;
101     }
102     return current;
103 }
104 
105 #ifdef __cplusplus
106 #if __cplusplus
107 }
108 #endif
109 #endif
110 #endif  // BASE_STARTUP_PARAM_INCLUDE_H