24 RCSID(
"$Id: c9bbf86f4f402f0bffa10f354d44a21012246079 $")
26 #include <freeradius-devel/util/table.h>
27 #include <freeradius-devel/util/misc.h>
31 #define TABLE_IDX(_table, _idx, _element_size) (((uint8_t const *)(_table)) + ((_idx) * (_element_size)))
32 #define ELEM_STR(_offset) (*((fr_table_elem_t const *)(_offset))).str
33 #define ELEM_LEN(_offset) (*((fr_table_elem_t const *)(_offset))).len
46 if (!str_val)
return NULL;
61 #define TABLE_TYPE_STR_FUNC(_func, _our_table_type, _our_name, _our_def_type, _our_return_type) \
62 _our_return_type _our_name(_our_table_type table, size_t table_len, char const *name, _our_def_type def) \
64 _our_return_type ret; \
65 _our_table_type found; \
66 found = (_our_table_type)_func(table, table_len, sizeof(((_our_table_type)0)[0]), name); \
68 memcpy(&ret, &def, sizeof(ret)); \
71 memcpy(&ret, &found->value, sizeof(ret)); \
83 #define TABLE_TYPE_STR_LEN_FUNC(_func, _our_table_type, _our_name, _our_def_type, _our_return_type) \
84 _our_return_type _our_name(_our_table_type table, size_t table_len, char const *name, ssize_t name_len, _our_def_type def) \
86 _our_return_type ret; \
87 _our_table_type found; \
88 found = (_our_table_type)_func(table, table_len, sizeof(((_our_table_type)0)[0]), name, name_len); \
90 memcpy(&ret, &def, sizeof(ret)); \
93 memcpy(&ret, &found->value, sizeof(ret)); \
97 #define TABLE_TYPE_STR_MATCH_LEN_FUNC(_func, _our_table_type, _our_name, _our_def_type, _our_return_type) \
98 _our_return_type _our_name(size_t *match_len, _our_table_type table, size_t table_len, char const *name, ssize_t name_len, _our_def_type def) \
100 _our_return_type ret; \
101 _our_table_type found; \
102 found = (_our_table_type)_func(match_len, table, table_len, sizeof(((_our_table_type)0)[0]), name, name_len); \
104 memcpy(&ret, &def, sizeof(ret)); \
107 memcpy(&ret, &found->value, sizeof(ret)); \
112 #define TABLE_TYPE_VALUE_FUNC(_our_table_type, _our_name, _our_value_type) \
113 char const *_our_name(_our_table_type table, size_t table_len, _our_value_type value, char const *def) \
116 for (i = 0; i < table_len; i++) if (table[i].value == value) return table[i].name.str; \
120 #define TABLE_TYPE_VALUE_INDEX_BIT_FIELD_FUNC(_our_table_type, _our_name, _our_value_type) \
121 char const *_our_name(_our_table_type table, size_t table_len, _our_value_type value, char const *def) \
123 uint8_t idx = fr_high_bit_pos(value); \
124 if (idx >= table_len) return def; \
125 return table[idx].name.str; \
128 #define TABLE_TYPE_VALUE_INDEX_FUNC(_our_table_type, _our_name, _our_value_type) \
129 char const *_our_name(_our_table_type table, size_t table_len, _our_value_type value, char const *def) \
131 if (value >= table_len) return def; \
132 return table[value].name.str; \
154 if (!
name)
return NULL;
156 while (start <= end) {
159 mid = start + ((end - start) / 2);
161 offset =
TABLE_IDX(table, mid, element_size);
163 if (ret == 0)
return offset;
189 static void const *table_ordered_value_by_str(
void const *table,
size_t table_len,
size_t element_size,
194 if (!
name)
return NULL;
196 for (i = 0; i < table_len; i++) {
197 void const *offset =
TABLE_IDX(table, i, element_size);
222 static void const *table_sorted_value_by_substr(
void const *table,
size_t table_len,
size_t element_size,
231 if (!
name)
return NULL;
233 while (start <= end) {
236 mid = start + ((end - start) / 2);
238 offset =
TABLE_IDX(table, mid, element_size);
244 (name_len < 0) ?
ELEM_LEN(offset) : (
size_t)name_len);
245 if (ret == 0)
return offset;
275 static void const *table_ordered_value_by_substr(
void const *table,
size_t table_len,
size_t element_size,
280 if (!
name)
return NULL;
282 for (i = 0; i < table_len; i++) {
286 offset =
TABLE_IDX(table, i, element_size);
293 if ((name_len > 0) && (name_len < (
int) tlen))
continue;
299 (name_len < 0) ? tlen : (
size_t)name_len) == 0)
return offset;
329 static void const *table_sorted_value_by_longest_prefix(
size_t *match_len,
330 void const *table,
size_t table_len,
size_t element_size,
338 void const *found = NULL;
340 if (!
name)
return NULL;
341 if (name_len < 0) name_len = strlen(
name);
343 while (start <= end) {
348 mid = start + ((end - start) / 2);
350 offset =
TABLE_IDX(table, mid, element_size);
354 ret =
strncasecmp(
name, elem, tlen < (
size_t)name_len ? tlen : (
size_t)name_len);
359 if (tlen == (
size_t)name_len) {
360 if (match_len) *match_len = tlen;
370 if (tlen < (
size_t)name_len) {
372 if (match_len) *match_len = tlen;
386 if (!found && match_len) *match_len = 0;
410 static void const *table_ordered_value_by_longest_prefix(
size_t *match_len,
411 void const *table,
size_t table_len,
size_t element_size,
415 size_t found_len = 0;
416 void const *found = NULL;
418 if (!
name)
return NULL;
419 if (name_len < 0) name_len = strlen(
name);
421 for (i = 0; i < table_len; i++) {
425 offset =
TABLE_IDX(table, i, element_size);
428 (tolower(
name[j]) == tolower((
ELEM_STR(offset))[j])); j++);
434 if ((
ELEM_STR(offset))[j] !=
'\0')
continue;
439 if (j == (
size_t)name_len) {
440 if (match_len) *match_len = name_len;
456 if (match_len) *match_len = found_len;
int strncasecmp(char *s1, char *s2, int n)
int strcasecmp(char *s1, char *s2)
#define TABLE_TYPE_VALUE_FUNC(_our_table_type, _our_name, _our_value_type)
#define TABLE_TYPE_VALUE_INDEX_BIT_FIELD_FUNC(_our_table_type, _our_name, _our_value_type)
#define ELEM_STR(_offset)
#define TABLE_TYPE_STR_FUNC(_func, _our_table_type, _our_name, _our_def_type, _our_return_type)
Create type specific string to value functions.
#define TABLE_TYPE_VALUE_INDEX_FUNC(_our_table_type, _our_name, _our_value_type)
#define TABLE_TYPE_STR_LEN_FUNC(_func, _our_table_type, _our_name, _our_def_type, _our_return_type)
Create type specific string to value functions with an input string length argument.
#define TABLE_IDX(_table, _idx, _element_size)
char const * _fr_table_ptr_by_str_value(fr_table_ptr_sorted_t const *table, size_t table_len, char const *str_val, char const *def)
Brute force search a sorted or ordered ptr table, assuming the pointers are strings.
static void const * table_sorted_value_by_str(void const *table, size_t table_len, size_t element_size, char const *name)
Convert a string to a value using a lexicographically sorted table.
#define ELEM_LEN(_offset)
#define TABLE_TYPE_STR_MATCH_LEN_FUNC(_func, _our_table_type, _our_name, _our_def_type, _our_return_type)
int fr_table_ordered_num_by_substr(fr_table_num_ordered_t const *table, size_t table_len, char const *name, ssize_t name_len, int def)
char const * fr_table_sorted_str_by_ptr(fr_table_ptr_sorted_t const *table, size_t table_len, void const *ptr, char const *def)
int fr_table_ordered_num_by_longest_prefix(size_t *match_len, fr_table_num_ordered_t const *table, size_t table_len, char const *name, ssize_t name_len, int def)
void * fr_table_ordered_ptr_by_str(fr_table_ptr_ordered_t const *table, size_t table_len, char const *name, void const *def)
void * fr_table_ordered_ptr_by_longest_prefix(size_t *match_len, fr_table_ptr_ordered_t const *table, size_t table_len, char const *name, ssize_t name_len, void const *def)
char const * str
Literal string.
int fr_table_sorted_num_by_substr(fr_table_num_sorted_t const *table, size_t table_len, char const *name, ssize_t name_len, int def)
char const * fr_table_ordered_str_by_num(fr_table_num_ordered_t const *table, size_t table_len, int number, char const *def)
void * fr_table_sorted_ptr_by_str(fr_table_ptr_sorted_t const *table, size_t table_len, char const *name, void const *def)
char const * fr_table_ordered_str_by_ptr(fr_table_ptr_ordered_t const *table, size_t table_len, void const *ptr, char const *def)
void * fr_table_sorted_ptr_by_substr(fr_table_ptr_sorted_t const *table, size_t table_len, char const *name, ssize_t name_len, void const *def)
char const * fr_table_sorted_str_by_num(fr_table_num_sorted_t const *table, size_t table_len, int number, char const *def)
int fr_table_sorted_num_by_str(fr_table_num_sorted_t const *table, size_t table_len, char const *name, int def)
char const * fr_table_indexed_str_by_num(fr_table_num_indexed_t const *table, size_t table_len, unsigned int number, char const *def)
void * fr_table_ordered_ptr_by_substr(fr_table_ptr_ordered_t const *table, size_t table_len, char const *name, ssize_t name_len, void const *def)
int fr_table_ordered_num_by_str(fr_table_num_ordered_t const *table, size_t table_len, char const *name, int def)
void * fr_table_sorted_ptr_by_longest_prefix(size_t *match_len, fr_table_ptr_sorted_t const *table, size_t table_len, char const *name, ssize_t name_len, void const *def)
char const * fr_table_indexed_str_by_bit_field(fr_table_num_indexed_bit_pos_t const *table, size_t table_len, uint64_t number, char const *def)
int fr_table_sorted_num_by_longest_prefix(size_t *match_len, fr_table_num_sorted_t const *table, size_t table_len, char const *name, ssize_t name_len, int def)
An element in a table indexed by bit position.
An element in a table indexed by numeric value.
An element in an arbitrarily ordered array of name to num mappings.
An element in a lexicographically sorted array of name to num mappings.
An element in an arbitrarily ordered array of name to ptr mappings.
An element in a lexicographically sorted array of name to ptr mappings.