From 5fc61c757deaae46e25cc163ae9e92831528fbc8 Mon Sep 17 00:00:00 2001 From: tb <> Date: Wed, 29 Nov 2023 13:39:34 +0000 Subject: Convert ssl3_cipher_by_id() to bsearch() This was previously the only user of OBJ_bsearch_ssl_cipher_id(), which in turn is the one remaining user of OBJ_bsearch_() outside of libcrypto. OBJ_bsearch_() is OpenSSL's idiosyncratic reimplementation of ANSI C89's bsearch(). Since this used to be hidden behind macro insanity, the result was three inscrutable layers of comparison functions. It is much simpler and cleaner to use the standard API. Move all the code to s3_lib.c, since it's ony used there. In a few further diffs, OBJ_bsearch_() will be removed from libcrypto. Unfortunately, we'll need to keep OBJ_bsearch_ex(), because it is exposed via sk_find_ex(), which is exposed by M2Crypto... ok jsing --- src/lib/libssl/s3_lib.c | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) (limited to 'src/lib/libssl/s3_lib.c') diff --git a/src/lib/libssl/s3_lib.c b/src/lib/libssl/s3_lib.c index 9ac02f3e1b..1ae2d047bc 100644 --- a/src/lib/libssl/s3_lib.c +++ b/src/lib/libssl/s3_lib.c @@ -1,4 +1,4 @@ -/* $OpenBSD: s3_lib.c,v 1.247 2023/11/29 13:29:34 tb Exp $ */ +/* $OpenBSD: s3_lib.c,v 1.248 2023/11/29 13:39:34 tb Exp $ */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -150,6 +150,7 @@ #include #include +#include #include #include @@ -1413,18 +1414,26 @@ ssl3_get_cipher(unsigned int u) return (NULL); } +static int +ssl3_cipher_id_cmp(const void *id, const void *cipher) +{ + unsigned long a = *(const unsigned long *)id; + unsigned long b = ((const SSL_CIPHER *)cipher)->id; + + return a < b ? -1 : a > b; +} + const SSL_CIPHER * ssl3_get_cipher_by_id(unsigned long id) { - const SSL_CIPHER *cp; - SSL_CIPHER c; + const SSL_CIPHER *cipher; - c.id = id; - cp = OBJ_bsearch_ssl_cipher_id(&c, ssl3_ciphers, SSL3_NUM_CIPHERS); - if (cp != NULL && cp->valid == 1) - return (cp); + cipher = bsearch(&id, ssl3_ciphers, SSL3_NUM_CIPHERS, sizeof(*cipher), + ssl3_cipher_id_cmp); + if (cipher != NULL && cipher->valid == 1) + return cipher; - return (NULL); + return NULL; } const SSL_CIPHER * -- cgit v1.2.3-55-g6feb