limit = atoi(argv[2]);
}
- alg = enum_from_name(hash_algorithm_short_names, argv[1]);
- if (alg == -1)
+ if (!enum_from_name(hash_algorithm_short_names, argv[1], &alg))
{
fprintf(stderr, "unknown hash algorthm: %s\n", argv[1]);
return 1;
*/
static void set_profile(private_cmd_connection_t *this, char *name)
{
- int profile;
+ profile_t profile;
- profile = enum_from_name(profile_names, name);
- if (profile == -1)
+ if (!enum_from_name(profile_names, name, &profile))
{
DBG1(DBG_CFG, "unknown connection profile: %s", name);
exit(1);
type = atoi(this->type);
if (!type)
{
- type = enum_from_name(notify_type_names, this->type);
- if (type == -1)
+ if (!enum_from_name(notify_type_names, this->type, &type))
{
DBG1(DBG_CFG, "unknown notify: '%s', skipped", this->type);
return TRUE;
type = atoi(this->type);
if (!type)
{
- type = enum_from_name(payload_type_short_names, this->type);
- if (type == -1)
+ if (!enum_from_name(payload_type_short_names, this->type, &type))
{
DBG1(DBG_CFG, "unknown payload: '%s', skipped", this->type);
return TRUE;
type = strtoul(key, &end, 10);
if (end == key || errno)
{
- type = enum_from_name(transform_type_names, key);
- if (type == -1)
+ if (!enum_from_name(transform_type_names, key, &type))
{
DBG1(DBG_CFG, "unknown transform: '%s', skipped", key);
continue;
type = atoi(name);
if (!type)
{
- type = enum_from_name(payload_type_short_names, name);
- if (type == -1)
+ if (!enum_from_name(payload_type_short_names, name, &type))
{
DBG1(DBG_CFG, "invalid payload name '%s'", name);
break;
type = atoi(this->type);
if (!type)
{
- type = enum_from_name(payload_type_short_names, this->type);
- if (type == -1)
+ if (!enum_from_name(payload_type_short_names, this->type, &type))
{
DBG1(DBG_CFG, "unknown payload: '%s', skipped", this->type);
return TRUE;
type = atoi(name);
if (!type)
{
- type = enum_from_name(payload_type_short_names, name);
- if (type == -1)
+ if (!enum_from_name(payload_type_short_names, name, &type))
{
DBG1(DBG_CFG, "invalid payload name '%s'", name);
break;
type = atoi(this->type);
if (!type)
{
- type = enum_from_name(notify_type_names, this->type);
- if (type == -1)
+ if (!enum_from_name(notify_type_names, this->type, &type))
{
DBG1(DBG_CFG, "unknown notify: '%s', skipped", this->type);
return TRUE;
order = enumerator_create_token(this->order, ", ", " ");
while (order->enumerate(order, &name))
{
- type = enum_from_name(payload_type_short_names, name);
- if (type != -1)
+ if (enum_from_name(payload_type_short_names, name, &type))
{
enumerator = list->create_enumerator(list);
while (enumerator->enumerate(enumerator, &payload))
coupling_validator_t *coupling_validator_create()
{
private_coupling_validator_t *this;
+ hash_algorithm_t alg;
char *path, *hash;
INIT(this,
hash = lib->settings->get_str(lib->settings,
"%s.plugins.coupling.hash", "sha1", lib->ns);
- this->hasher = lib->crypto->create_hasher(lib->crypto,
- enum_from_name(hash_algorithm_short_names, hash));
+ if (!enum_from_name(hash_algorithm_short_names, hash, &alg))
+ {
+ DBG1(DBG_CFG, "unknown coupling hash algorithm: %s", hash);
+ destroy(this);
+ return NULL;
+ }
+ this->hasher = lib->crypto->create_hasher(lib->crypto, alg);
if (!this->hasher)
{
DBG1(DBG_CFG, "unsupported coupling hash algorithm: %s", hash);
vendor = atoi(token);
token = pos;
}
- type = enum_from_name(radius_attribute_type_names, token);
- if (type == -1)
+ if (!enum_from_name(radius_attribute_type_names, token, &type))
{
type = atoi(token);
}
.private = load_issuer_key(),
.ca = load_issuer_cert(),
.cas = linked_list_create(),
- .digest = enum_from_name(hash_algorithm_short_names, digest),
.psk = shared_key_create(SHARED_IKE,
chunk_clone(chunk_create(psk, strlen(psk)))),
.pwd = shared_key_create(SHARED_EAP,
this->cas->insert_last(this->cas, this->ca->get_ref(this->ca));
}
- if (this->digest == -1)
+ if (!enum_from_name(hash_algorithm_short_names, digest, &this->digest))
{
DBG1(DBG_CFG, "invalid load-tester digest: '%s', using sha1", digest);
this->digest = HASH_SHA1;
return &this->public;
}
-
}
else
{
- group = enum_from_name(debug_names, msg->loglevel.type);
- if ((int)group < 0)
+ if (!enum_from_name(debug_names, msg->loglevel.type, &group))
{
fprintf(out, "unknown type '%s'!\n", msg->loglevel.type);
return;
char *str;
str = request->get_str(request, "ANY", "type");
- type = enum_from_name(certificate_type_names, str);
- if (type == -1)
+ if (!enum_from_name(certificate_type_names, str, &type))
{
b = vici_builder_create();
return b->finalize(b);
*/
static struct {
+ bool found;
int val;
char *str;
} enum_tests_cont[] = {
- {CONT1, "CONT1"},
- {CONT2, "CONT2"},
- {CONT2, "CoNt2"},
- {CONT3, "CONT3"},
- {CONT4, "CONT4"},
- {CONT5, "CONT5"},
- {-1, "asdf"},
- {-1, ""},
- {-1, NULL},
+ {TRUE, CONT1, "CONT1"},
+ {TRUE, CONT2, "CONT2"},
+ {TRUE, CONT2, "CoNt2"},
+ {TRUE, CONT3, "CONT3"},
+ {TRUE, CONT4, "CONT4"},
+ {TRUE, CONT5, "CONT5"},
+ {FALSE, 0, "asdf"},
+ {FALSE, 0, ""},
+ {FALSE, 0, NULL},
}, enum_tests_split[] = {
- {SPLIT1, "SPLIT1"},
- {SPLIT1, "split1"},
- {SPLIT2, "SPLIT2"},
- {SPLIT2, "SpLiT2"},
- {SPLIT3, "SPLIT3"},
- {SPLIT4, "SPLIT4"},
- {SPLIT5, "SPLIT5"},
- {-1, "asdf"},
- {-1, ""},
- {-1, NULL},
+ {TRUE, SPLIT1, "SPLIT1"},
+ {TRUE, SPLIT1, "split1"},
+ {TRUE, SPLIT2, "SPLIT2"},
+ {TRUE, SPLIT2, "SpLiT2"},
+ {TRUE, SPLIT3, "SPLIT3"},
+ {TRUE, SPLIT4, "SPLIT4"},
+ {TRUE, SPLIT5, "SPLIT5"},
+ {FALSE, 0, "asdf"},
+ {FALSE, 0, ""},
+ {FALSE, 0, NULL},
};
START_TEST(test_enum_from_name_cont)
{
- int val = enum_from_name(test_enum_cont_names, enum_tests_cont[_i].str);
+ int val = 0;
+ bool found;
+
+ found = enum_from_name(test_enum_cont_names, enum_tests_cont[_i].str, &val);
+ ck_assert(enum_tests_cont[_i].found == found);
ck_assert_int_eq(val, enum_tests_cont[_i].val);
}
END_TEST
START_TEST(test_enum_from_name_split)
{
- int val = enum_from_name(test_enum_split_names, enum_tests_split[_i].str);
+ int val = 0;
+ bool found;
+
+ found = enum_from_name(test_enum_split_names, enum_tests_split[_i].str, &val);
+ ck_assert(enum_tests_split[_i].found == found);
ck_assert_int_eq(val, enum_tests_split[_i].val);
}
END_TEST
/**
* See header.
*/
-int enum_from_name(enum_name_t *e, char *name)
+bool enum_from_name_as_int(enum_name_t *e, const char *name, int *val)
{
do
{
{
if (name && strcaseeq(name, e->names[i]))
{
- return e->first + i;
+ *val = e->first + i;
+ return TRUE;
}
}
}
while ((e = e->next));
- return -1;
+ return FALSE;
}
/**
*
* @param e enum names for this enum value
* @param name name to get enum value for
- * @return enum value, -1 if not found
+ * @þaram valp variable sized pointer receiving value
+ * @return TRUE if enum name found, FALSE otherwise
*/
-int enum_from_name(enum_name_t *e, char *name);
+#define enum_from_name(e, name, valp) ({ \
+ int _val; \
+ int _found = enum_from_name_as_int(e, name, &_val); \
+ if (_found) \
+ { \
+ *(valp) = _val; \
+ } \
+ _found; })
+
+/**
+ * Convert a enum string back to its enum value, integer pointer variant.
+ *
+ * This variant takes integer pointer only, use enum_from_name() to pass
+ * enum type pointers for the result.
+ *
+ * @param e enum names for this enum value
+ * @param name name to get enum value for
+ * @þaram val integer pointer receiving value
+ * @return TRUE if enum name found, FALSE otherwise
+ */
+bool enum_from_name_as_int(enum_name_t *e, const char *name, int *val);
/**
* printf hook function for enum_names_t.
enumerator = enumerator_create_token(config, ",", " ");
while (enumerator->enumerate(enumerator, &token))
{
- suite = enum_from_name(tls_cipher_suite_names, token);
- if (suite == suites[i].suite)
+ if (enum_from_name(tls_cipher_suite_names, token, &suite) &&
+ suite == suites[i].suite)
{
suites[remaining++] = suites[i];
break;
imv_manager_t* tnc_imv_manager_create(void)
{
private_tnc_imv_manager_t *this;
- recommendation_policy_t policy;
+ char *polname;
INIT(this,
.public = {
.next_imv_id = 1,
);
- policy = enum_from_name(recommendation_policy_names,
- lib->settings->get_str(lib->settings,
- "%s.plugins.tnc-imv.recommendation_policy",
- "default", lib->ns));
- this->policy = (policy != -1) ? policy : RECOMMENDATION_POLICY_DEFAULT;
+ polname = lib->settings->get_str(lib->settings,
+ "%s.plugins.tnc-imv.recommendation_policy", "default", lib->ns);
+ if (!enum_from_name(recommendation_policy_names, polname, &this->policy))
+ {
+ this->policy = RECOMMENDATION_POLICY_DEFAULT;
+ }
DBG1(DBG_TNC, "TNC recommendation policy is '%N'",
recommendation_policy_names, this->policy);
error_type_name = xmlGetProp(node, "type");
if (error_type_name)
{
- this->error_type = enum_from_name(tnccs_error_type_names,
- error_type_name);
- if (this->error_type == -1)
+ if (!enum_from_name(tnccs_error_type_names, error_type_name,
+ &this->error_type))
{
this->error_type = TNCCS_ERROR_OTHER;
}
char *error_msg, buf[BUF_LEN];
tnccs_error_type_t error_type = TNCCS_ERROR_MALFORMED_BATCH;
tnccs_msg_t *msg;
- tnccs_msg_type_t type = IMC_IMV_MSG;
+ tnccs_msg_type_t type = IMC_IMV_MSG, nametype;
if (streq((char*)node->name, "IMC-IMV-Message"))
{
error_msg = "node is not in the TNCCS message namespace";
goto fatal;
}
- if (type != enum_from_name(tnccs_msg_type_names, (char*)cur->name))
+ if (!enum_from_name(tnccs_msg_type_names, cur->name, &nametype) ||
+ type != nametype)
{
error_msg = buf;
snprintf(buf, BUF_LEN, "expected '%N' node but was '%s'",
errors->insert_last(errors, msg);
return NULL;
}
-
case 'h':
goto usage;
case 'g':
- digest = enum_from_name(hash_algorithm_short_names, arg);
- if (digest == -1)
+ if (!enum_from_name(hash_algorithm_short_names, arg, &digest))
{
error = "invalid --digest type";
goto usage;
}
continue;
case 'g':
- digest = enum_from_name(hash_algorithm_short_names, arg);
- if (digest == -1)
+ if (!enum_from_name(hash_algorithm_short_names, arg, &digest))
{
error = "invalid --digest type";
goto usage;
}
continue;
case 'g':
- digest = enum_from_name(hash_algorithm_short_names, arg);
- if (digest == -1)
+ if (!enum_from_name(hash_algorithm_short_names, arg, &digest))
{
error = "invalid --digest type";
goto usage;
}
continue;
case 'g':
- digest = enum_from_name(hash_algorithm_short_names, arg);
- if (digest == -1)
+ if (!enum_from_name(hash_algorithm_short_names, arg, &digest))
{
error = "invalid --digest type";
goto usage;
case 'h':
goto usage;
case 'g':
- digest = enum_from_name(hash_algorithm_short_names, arg);
- if (digest == -1)
+ if (!enum_from_name(hash_algorithm_short_names, arg, &digest))
{
error = "invalid --digest type";
goto usage;
bool has_privkey;
buf = vici_find(res, &len, "data");
- type = enum_from_name(certificate_type_names,
- vici_find_str(res, "ANY", "type"));
has_privkey = streq(vici_find_str(res, "no", "has_privkey"), "yes");
- if (type != -1 && type != CERT_ANY && buf)
+ if (enum_from_name(certificate_type_names,
+ vici_find_str(res, "ANY", "type"), &type) &&
+ type != CERT_ANY && buf)
{
cert = lib->creds->create(lib->creds, CRED_CERTIFICATE, type,
BUILD_BLOB_ASN1_DER, chunk_create(buf, len),