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 SELINUX_MAP 16 #define SELINUX_MAP 17 18 #pragma once 19 20 #include <stddef.h> 21 #include <stdint.h> 22 23 #ifdef __cplusplus 24 #if __cplusplus 25 extern "C" { 26 #endif 27 #endif // __cplusplus 28 29 typedef struct HashNode { 30 struct HashNode *next; 31 } HashNode; 32 33 typedef struct { 34 HashNode *buckets[0]; 35 } HashTab; 36 37 typedef struct { 38 const char *prefixLabel; 39 const char *matchLabel; 40 uint32_t labeled; 41 HashTab *handle; 42 } ParamContextsTrie; 43 44 #define PREFIX_LABELED (1) 45 #define MATCH_LABELED (2) 46 #define UNLABELED (0) 47 48 typedef struct ParamHashNode { 49 HashNode hashNode; 50 ParamContextsTrie *childPtr; 51 uint32_t nameLen; 52 char* name; 53 } ParamHashNode; 54 55 #define HASHMAP_ENTRY(ptr, type, member) ((type *)((char *)(ptr)-offsetof(type, member))) 56 57 int32_t HashMapCreate(HashTab **handle); 58 void HashMapDestroy(HashTab *handle); 59 int32_t HashMapAdd(HashTab *handle, HashNode *hashNode); 60 void HashMapRemove(HashTab *handle, const char *key); 61 HashNode *HashMapGet(HashTab *handle, const char *key, uint32_t len); 62 HashNode *HashMapFind(HashTab *handle, int hashCode, const char *key); 63 #ifdef __cplusplus 64 #if __cplusplus 65 } 66 #endif 67 #endif // __cplusplus 68 #endif