From 9fffb87f5d35d0387d2fef05868f79d200d8a912 Mon Sep 17 00:00:00 2001 From: tb <> Date: Sat, 13 Jan 2024 11:08:39 +0000 Subject: Reimplement {EVP_CIPHER,EVP_MD,OBJ_NAME}_do_all{,_sorted}(3) This implements the do_all API by simple loops over the tables of digests and ciphers. Since some ciphers are only available on some platforms, we need to skip them if necessary. We use loops in each of the functions rather the convoluted way of reducing some of the loops to others. Since the tables are sorted, as ensured by regress, both do_all() and do_all_sorted() walk the lists in order. In particular, we no longer need to allocate to be able to sort hash tables by name on the fly in a void function that may end up doing nothing because allocation failed. We still need to do an unchecked OPENSSL_init_crypto() call. But that's what prayer and clean living are there for (as beck put it). The OBJ_NAME API is completely misnamed. It has little to do with objects and a lot to do with EVP. Therefore we implement what will remain from its saner replacement in the evp directory, i.e., evp_names.c. ok jsing --- src/lib/libcrypto/objects/o_names.c | 81 +------------------------------------ 1 file changed, 1 insertion(+), 80 deletions(-) (limited to 'src/lib/libcrypto/objects') diff --git a/src/lib/libcrypto/objects/o_names.c b/src/lib/libcrypto/objects/o_names.c index 48b95d6767..1007c5e23a 100644 --- a/src/lib/libcrypto/objects/o_names.c +++ b/src/lib/libcrypto/objects/o_names.c @@ -1,4 +1,4 @@ -/* $OpenBSD: o_names.c,v 1.24 2023/07/08 12:27:51 beck Exp $ */ +/* $OpenBSD: o_names.c,v 1.25 2024/01/13 11:08:39 tb Exp $ */ #include #include #include @@ -240,85 +240,6 @@ OBJ_NAME_remove(const char *name, int type) } LCRYPTO_ALIAS(OBJ_NAME_remove); -struct doall { - int type; - void (*fn)(const OBJ_NAME *, void *arg); - void *arg; -}; - -static void -do_all_fn_doall_arg(const OBJ_NAME *name, struct doall *d) -{ - if (name->type == d->type) - d->fn(name, d->arg); -} - -static IMPLEMENT_LHASH_DOALL_ARG_FN(do_all_fn, const OBJ_NAME, struct doall) - -void -OBJ_NAME_do_all(int type, void (*fn)(const OBJ_NAME *, void *arg), void *arg) -{ - struct doall d; - - d.type = type; - d.fn = fn; - d.arg = arg; - - lh_OBJ_NAME_doall_arg(names_lh, LHASH_DOALL_ARG_FN(do_all_fn), - struct doall, &d); -} -LCRYPTO_ALIAS(OBJ_NAME_do_all); - -struct doall_sorted { - int type; - int n; - const OBJ_NAME **names; -}; - -static void -do_all_sorted_fn(const OBJ_NAME *name, void *d_) -{ - struct doall_sorted *d = d_; - - if (name->type != d->type) - return; - - d->names[d->n++] = name; -} - -static int -do_all_sorted_cmp(const void *n1_, const void *n2_) -{ - const OBJ_NAME * const *n1 = n1_; - const OBJ_NAME * const *n2 = n2_; - - return strcmp((*n1)->name, (*n2)->name); -} - -void -OBJ_NAME_do_all_sorted(int type, void (*fn)(const OBJ_NAME *, void *arg), - void *arg) -{ - struct doall_sorted d; - int n; - - d.type = type; - d.names = reallocarray(NULL, lh_OBJ_NAME_num_items(names_lh), - sizeof *d.names); - d.n = 0; - if (d.names != NULL) { - OBJ_NAME_do_all(type, do_all_sorted_fn, &d); - - qsort((void *)d.names, d.n, sizeof *d.names, do_all_sorted_cmp); - - for (n = 0; n < d.n; ++n) - fn(d.names[n], arg); - - free(d.names); - } -} -LCRYPTO_ALIAS(OBJ_NAME_do_all_sorted); - static int free_type; static void -- cgit v1.2.3-55-g6feb