START_TEST(test_dh_group_mapping)
{
- enum_name_t *e = key_exchange_method_names_short;
+ enum_name_elem_t *e = key_exchange_method_names_short->elem;
key_exchange_method_t ke;
const proposal_token_t *token;
char *name;
/*
+ * Copyright (C) 2022-2023 Tobias Brunner
* Copyright (C) 2006 Martin Willi
*
* Copyright (C) secunet Security Networks AG
*/
char *enum_to_name(enum_name_t *e, int val)
{
+ enum_name_elem_t *cur;
+
if (!e)
{
return NULL;
}
- do
+ for (cur = e->elem; cur; cur = cur->next)
{
- if (val >= e->first && val <= e->last)
+ if (val >= cur->first && val <= cur->last)
{
- return e->names[val - e->first];
+ return cur->names[val - cur->first];
}
}
- while ((e = e->next));
return NULL;
}
*/
bool enum_from_name_as_int(enum_name_t *e, const char *name, int *val)
{
- do
+ enum_name_elem_t *cur;
+
+ if (!e)
+ {
+ return FALSE;
+ }
+ for (cur = e->elem; cur; cur = cur->next)
{
- int i, count = e->last - e->first + 1;
+ int i, count = cur->last - cur->first + 1;
for (i = 0; i < count; i++)
{
- if (name && strcaseeq(name, e->names[i]))
+ if (name && strcaseeq(name, cur->names[i]))
{
- *val = e->first + i;
+ *val = cur->first + i;
return TRUE;
}
}
}
- while ((e = e->next));
return FALSE;
}
*/
char *enum_flags_to_string(enum_name_t *e, u_int val, char *buf, size_t len)
{
+ enum_name_elem_t *cur;
char *pos = buf, *delim = "";
int i, wr;
- if (e->next != ENUM_FLAG_MAGIC)
+ if (!e)
+ {
+ return NULL;
+ }
+ cur = e->elem;
+ if (cur->next != ENUM_FLAG_MAGIC)
{
if (snprintf(buf, len, "(%d)", (int)val) >= len)
{
return buf;
}
- if (snprintf(buf, len, "%s", e->names[0]) >= len)
+ if (snprintf(buf, len, "%s", cur->names[0]) >= len)
{
return NULL;
}
{
char *name = NULL, hex[32];
- if (flag >= (u_int)e->first && flag <= (u_int)e->last)
+ if (flag >= (u_int)cur->first && flag <= (u_int)cur->last)
{
- name = e->names[find_flag_pos(e->first, i)];
+ name = cur->names[find_flag_pos(cur->first, i)];
}
else
{
*/
bool enum_flags_from_string_as_int(enum_name_t *e, const char *str, u_int *val)
{
+ enum_name_elem_t *cur;
enumerator_t *enumerator;
char *name;
*val = 0;
- if (!str || !*str)
+ if (!e || !str || !*str)
{
return TRUE;
}
- else if (e->next != ENUM_FLAG_MAGIC)
+ cur = e->elem;
+ if (cur->next != ENUM_FLAG_MAGIC)
{
return enum_from_name_as_int(e, str, val);
}
u_int flag, i;
bool found = FALSE;
- if (strcaseeq(name, e->names[0]))
+ if (strcaseeq(name, cur->names[0]))
{ /* accept name used if no flags are set */
continue;
}
- for (i = 1, flag = e->first; flag <= e->last; i++, flag <<= 1)
+ for (i = 1, flag = cur->first; flag <= cur->last; i++, flag <<= 1)
{
- if (e->names[i] && strcaseeq(name, e->names[i]))
+ if (cur->names[i] && strcaseeq(name, cur->names[i]))
{
*val |= flag;
found = TRUE;
int val = *((int*)(args[1]));
char *name, buf[512];
- if (ed && ed->next == ENUM_FLAG_MAGIC)
+ if (ed && ed->elem->next == ENUM_FLAG_MAGIC)
{
name = enum_flags_to_string(ed, val, buf, sizeof(buf));
if (name == NULL)
/*
- * Copyright (C) 2009-2019 Tobias Brunner
+ * Copyright (C) 2009-2023 Tobias Brunner
* Copyright (C) 2006-2008 Martin Willi
*
* Copyright (C) secunet Security Networks AG
#include <utils/printf_hook/printf_hook.h>
typedef struct enum_name_t enum_name_t;
+typedef struct enum_name_elem_t enum_name_elem_t;
/**
* Magic enum_name_t pointer indicating this is an enum name for flags
*/
-#define ENUM_FLAG_MAGIC ((enum_name_t*)~(uintptr_t)0)
+#define ENUM_FLAG_MAGIC ((enum_name_elem_t*)~(uintptr_t)0)
/**
* Struct to store names for enums.
* by the numerical enum value.
*/
struct enum_name_t {
+ /** first enum_name_elem_t in chain */
+ enum_name_elem_t *elem;
+};
+
+/**
+ * Enum name element to chain to enum_name_t.
+ */
+struct enum_name_elem_t {
/** value of the first enum string, values are expected to be (u_)int, using
* int64_t here instead, however, avoids warnings for large unsigned ints */
int64_t first;
/** value of the last enum string */
int64_t last;
- /** next enum_name_t in list, or ENUM_FLAG_MAGIC */
- enum_name_t *next;
+ /** next enum_name_elem_t in list, or ENUM_FLAG_MAGIC */
+ enum_name_elem_t *next;
/** array of strings containing names from first to last */
char *names[];
};
* @param ... a list of strings
*/
#define ENUM_BEGIN(name, first, last, ...) \
- static enum_name_t name##last = {first, last + \
+ static enum_name_elem_t name##last = {first, last + \
BUILD_ASSERT(((last)-(first)+1) == countof(((char*[]){__VA_ARGS__}))), \
NULL, { __VA_ARGS__ }}
* @param ... a list of strings
*/
#define ENUM_NEXT(name, first, last, prev, ...) \
- static enum_name_t name##last = {first, last + \
+ static enum_name_elem_t name##last = {first, last + \
BUILD_ASSERT(((last)-(first)+1) == countof(((char*[]){__VA_ARGS__}))), \
&name##prev, { __VA_ARGS__ }}
* @param name name of the enum_name list
* @param prev enum value of the "last" defined in ENUM_BEGIN/previous ENUM_NEXT
*/
-#define ENUM_END(name, prev) enum_name_t *name = &name##prev;
+#define ENUM_END(name, prev) \
+ static enum_name_t name##head = { .elem = &name##prev }; \
+ enum_name_t *name = &name##head
/**
* Define a enum name with only one range.
* @param ... a list of strings
*/
#define ENUM_FLAGS(name, first, last, unset, ...) \
- static enum_name_t name##last = {first, last + \
+ static enum_name_elem_t name##last = {first, last + \
BUILD_ASSERT((__builtin_ffs(last)-__builtin_ffs(first)+1) == \
countof(((char*[]){__VA_ARGS__}))), \
ENUM_FLAG_MAGIC, { unset, __VA_ARGS__ }}; ENUM_END(name, last)