miniroon_caveats.c (2212B)
1 #ifndef TRIE_HASH_PerfectHash 2 #define TRIE_HASH_PerfectHash 3 #include <stddef.h> 4 #include <stdint.h> 5 enum PerfectKey { 6 MINIROON_CAVEAT_ENV_MATCH = 2, 7 MINIROON_CAVEAT_ENV_REMOVE = 1, 8 MINIROON_CAVEAT_ENV_SET = 0, 9 Unknown = -1, 10 }; 11 static enum PerfectKey PerfectHash(const char *string, size_t length); 12 #ifdef __GNUC__ 13 typedef uint16_t __attribute__((aligned (1))) triehash_uu16; 14 typedef char static_assert16[__alignof__(triehash_uu16) == 1 ? 1 : -1]; 15 typedef uint32_t __attribute__((aligned (1))) triehash_uu32; 16 typedef char static_assert32[__alignof__(triehash_uu32) == 1 ? 1 : -1]; 17 typedef uint64_t __attribute__((aligned (1))) triehash_uu64; 18 typedef char static_assert64[__alignof__(triehash_uu64) == 1 ? 1 : -1]; 19 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ 20 #define onechar(c, s, l) (((uint64_t)(c)) << (s)) 21 #else 22 #define onechar(c, s, l) (((uint64_t)(c)) << (l-8-s)) 23 #endif 24 #if (!defined(__ARM_ARCH) || defined(__ARM_FEATURE_UNALIGNED)) && !defined(TRIE_HASH_NO_MULTI_BYTE) 25 #define TRIE_HASH_MULTI_BYTE 26 #endif 27 #endif /*GNUC */ 28 #ifdef TRIE_HASH_MULTI_BYTE 29 static enum PerfectKey PerfectHash2(const char *string) 30 { 31 switch(string[0]) { 32 case 0| onechar('E', 0, 8): 33 switch(string[1]) { 34 case 0| onechar('M', 0, 8): 35 return MINIROON_CAVEAT_ENV_MATCH; 36 break; 37 case 0| onechar('R', 0, 8): 38 return MINIROON_CAVEAT_ENV_REMOVE; 39 break; 40 case 0| onechar('S', 0, 8): 41 return MINIROON_CAVEAT_ENV_SET; 42 } 43 } 44 return Unknown; 45 } 46 #else 47 static enum PerfectKey PerfectHash2(const char *string) 48 { 49 switch(string[0]) { 50 case 'E': 51 switch(string[1]) { 52 case 'M': 53 return MINIROON_CAVEAT_ENV_MATCH; 54 break; 55 case 'R': 56 return MINIROON_CAVEAT_ENV_REMOVE; 57 break; 58 case 'S': 59 return MINIROON_CAVEAT_ENV_SET; 60 } 61 } 62 return Unknown; 63 } 64 #endif /* TRIE_HASH_MULTI_BYTE */ 65 static enum PerfectKey PerfectHash(const char *string, size_t length) 66 { 67 switch (length) { 68 case 2: 69 return PerfectHash2(string); 70 default: 71 return Unknown; 72 } 73 } 74 #endif /* TRIE_HASH_PerfectHash */