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 *matchLabel = (subTrie->labelIndex != 0) ? subTrie->labelIndex : *matchLabel;
52 return subTrie;
53 }
54 }
55
56 uint32_t offset = 0;
57 if (ret < 0) {
58 offset = subTrie->left;
59 } else {
60 offset = subTrie->right;
61 }
62 if (offset == 0 || offset > workSpace->area->dataSize) {
63 return NULL;
64 }
65 subTrie = (ParamTrieNode *)(workSpace->area->data + offset);
66 }
67 return NULL;
68 }
69
FindTrieNode_(const WorkSpace * workSpace,const char * key,uint32_t keyLen,uint32_t * matchLabel)70 STATIC_INLINE ParamTrieNode *FindTrieNode_(
71 const WorkSpace *workSpace, const char *key, uint32_t keyLen, uint32_t *matchLabel)
72 {
73 const char *remainingKey = key;
74 ParamTrieNode *current = GetTrieRoot(workSpace);
75 PARAM_CHECK(current != NULL, return NULL, "Invalid current param %s", key);
76 *matchLabel = current->labelIndex;
77 const char *end = key + keyLen;
78 while (1) {
79 uint32_t subKeyLen = 0;
80 char *subKey = NULL;
81 GetNextKey(&remainingKey, &subKey, &subKeyLen, end);
82 if (!subKeyLen) {
83 return NULL;
84 }
85 if (current->child != 0) {
86 ParamTrieNode *next = GetTrieNode(workSpace, current->child);
87 current = FindSubTrie(workSpace, next, remainingKey, subKeyLen, matchLabel);
88 } else {
89 current = FindSubTrie(workSpace, current, remainingKey, subKeyLen, matchLabel);
90 }
91 if (current == NULL) {
92 return NULL;
93 } else if (current->labelIndex != 0) {
94 *matchLabel = current->labelIndex;
95 }
96 if (subKey == NULL || strcmp(subKey, ".") == 0) {
97 break;
98 }
99 remainingKey = subKey + 1;
100 }
101 return current;
102 }
103
104 #ifdef __cplusplus
105 #if __cplusplus
106 }
107 #endif
108 #endif
109 #endif // BASE_STARTUP_PARAM_INCLUDE_H