/** * Filter loaded test suites, either remove suites listed (exclude=TRUE), or all * that are not listed (exclude=FALSE). */ static void apply_filter(array_t *loaded, char *filter, bool exclude) { enumerator_t *enumerator, *names; hashtable_t *listed; test_suite_t *suite; char *name; listed = hashtable_create(hashtable_hash_str, hashtable_equals_str, 8); names = enumerator_create_token(filter, ",", " "); while (names->enumerate(names, &name)) { listed->put(listed, name, name); } enumerator = array_create_enumerator(loaded); while (enumerator->enumerate(enumerator, &suite)) { if ((exclude && listed->get(listed, suite->name)) || (!exclude && !listed->get(listed, suite->name))) { array_remove_at(loaded, enumerator); destroy_suite(suite); } } enumerator->destroy(enumerator); listed->destroy(listed); names->destroy(names); }
/** * Add a vici list from a comma separated string value */ static void add_list_key(vici_req_t *req, char *key, char *value) { enumerator_t *enumerator; char *token; vici_begin_list(req, key); enumerator = enumerator_create_token(value, ",", " "); while (enumerator->enumerate(enumerator, &token)) { vici_add_list_itemf(req, "%s", token); } enumerator->destroy(enumerator); vici_end_list(req); }
/* * See header */ eap_vendor_type_t *eap_vendor_type_from_string(char *str) { enumerator_t *enumerator; eap_vendor_type_t *result = NULL; eap_type_t type = 0; u_int32_t vendor = 0; char *part, *end; /* parse EAP method string of the form: [eap-]type[-vendor] */ enumerator = enumerator_create_token(str, "-", " "); while (enumerator->enumerate(enumerator, &part)) { if (!type) { if (streq(part, "eap")) { /* skip 'eap' at the beginning */ continue; } type = eap_type_from_string(part); if (!type) { type = strtoul(part, &end, 0); if (*end != '\0' || errno) { DBG1(DBG_LIB, "unknown or invalid EAP method: %s", part); break; } } continue; } vendor = strtoul(part, &end, 0); if (*end != '\0' || errno) { DBG1(DBG_LIB, "invalid EAP vendor: %s", part); type = 0; } break; } enumerator->destroy(enumerator); if (type) { INIT(result, .type = type, .vendor = vendor, ); } return result; }
/** * Load plugins from builddir */ static bool load_plugins() { enumerator_t *enumerator; char *name, path[PATH_MAX], dir[64]; enumerator = enumerator_create_token(PLUGINS, " ", ""); while (enumerator->enumerate(enumerator, &name)) { snprintf(dir, sizeof(dir), "%s", name); translate(dir, "-", "_"); snprintf(path, sizeof(path), "%s/%s/.libs", PLUGINDIR, dir); lib->plugins->add_path(lib->plugins, path); } enumerator->destroy(enumerator); return lib->plugins->load(lib->plugins, PLUGINS); }
/** * Check if the given string is contained in the filter string. */ static bool is_in_filter(const char *find, char *filter) { enumerator_t *names; bool found = FALSE; char *name; names = enumerator_create_token(filter, ",", " "); while (names->enumerate(names, &name)) { if (streq(name, find)) { found = TRUE; break; } } names->destroy(names); return found; }
/** * Build checksums for a set of plugins */ static void build_plugin_checksums(char *plugins) { enumerator_t *enumerator; char *plugin, path[256], under[128], sname[128], name[128]; enumerator = enumerator_create_token(plugins, " ", " "); while (enumerator->enumerate(enumerator, &plugin)) { snprintf(under, sizeof(under), "%s", plugin); translate(under, "-", "_"); snprintf(path, sizeof(path), "%s/libstrongswan-%s.so", PLUGINDIR, plugin); snprintf(sname, sizeof(sname), "%s_plugin_create", under); snprintf(name, sizeof(name), "%s\",", plugin); build_checksum(path, name, sname); } enumerator->destroy(enumerator); }
/* * Defined in header. */ chunk_t asn1_oid_from_string(char *str) { enumerator_t *enumerator; u_char buf[64]; char *end; int i = 0, pos = 0, shift; u_int val, shifted_val, first = 0; enumerator = enumerator_create_token(str, ".", ""); while (enumerator->enumerate(enumerator, &str)) { val = strtoul(str, &end, 10); if (end == str || pos > countof(buf)) { pos = 0; break; } switch (i++) { case 0: first = val; break; case 1: buf[pos++] = first * 40 + val; break; default: shift = 28; /* sufficient to handle 32 bit node numbers */ while (shift) { shifted_val = val >> shift; shift -= 7; if (shifted_val) /* do not encode leading zeroes */ { buf[pos++] = 0x80 | (shifted_val & 0x7F); } } buf[pos++] = val & 0x7F; } } enumerator->destroy(enumerator); return chunk_clone(chunk_create(buf, pos)); }
static bool get_next_test_vector(test_vector_t *test) { param_t param = PARAM_UNKNOWN; char line[512]; memset(test, 0, sizeof(test_vector_t)); while (fgets(line, sizeof(line), ctx.in)) { enumerator_t *enumerator; chunk_t value = chunk_empty; char *token; int i; switch (line[0]) { case '\n': case '\r': case '#': case '\0': /* copy comments, empty lines etc. directly to the output */ if (param != PARAM_UNKNOWN) { /* seems we got a complete test vector */ return TRUE; } fputs(line, ctx.out); continue; case '[': /* control directives */ fputs(line, ctx.out); if (strpfx(line, "[ENCRYPT]")) { ctx.decrypt = FALSE; } else if (strpfx(line, "[DECRYPT]")) { ctx.decrypt = TRUE; } else if (strcasepfx(line, "[IVlen = ")) { ctx.ivlen = atoi(line + strlen("[IVlen = ")); } else if (strcasepfx(line, "[Taglen = ")) { ctx.icvlen = atoi(line + strlen("[Taglen = ")); } continue; default: /* we assume the rest of the lines are PARAM = VALUE pairs*/ fputs(line, ctx.out); break; } i = 0; enumerator = enumerator_create_token(line, "=", " \n\r"); while (enumerator->enumerate(enumerator, &token)) { switch (i++) { case 0: /* PARAM */ param = parse_parameter(token); continue; case 1: /* VALUE */ if (param != PARAM_UNKNOWN && param != PARAM_COUNT) { value = chunk_from_hex(chunk_from_str(token), NULL); } else { value = chunk_empty; } continue; default: break; } break; } enumerator->destroy(enumerator); if (i < 2) { value = chunk_empty; } switch (param) { case PARAM_KEY: test->key = value; break; case PARAM_IV: test->iv = value; test->external_iv = TRUE; break; case PARAM_PLAINTEXT: test->plain = value; break; case PARAM_CIPHERTEXT: test->cipher = value; break; case PARAM_AAD: test->aad = value; break; case PARAM_ICV: test->icv = value; break; default: chunk_free(&value); break; } } if (param != PARAM_UNKNOWN) { /* could be that the file ended with a complete test vector */ return TRUE; } return FALSE; }