From e19106f5fb7da7db15449a9a50f9be9047800757 Mon Sep 17 00:00:00 2001 From: "Dr. Stephen Henson" Date: Wed, 22 Oct 2008 15:43:01 +0000 Subject: Create function of the form OBJ_bsearch_xxx() in bsearch typesafe macros with the appropriate parameters which calls OBJ_bsearch(). A compiler will typically inline this. This avoids the need for cmp_xxx variables and fixes unchecked const issues with CHECKED_PTR_OF() --- crypto/objects/obj_dat.c | 21 +++++++++------------ crypto/objects/obj_xref.c | 31 ++++++++++++++----------------- crypto/objects/objects.h | 24 ++++++++++++++---------- 3 files changed, 37 insertions(+), 39 deletions(-) (limited to 'crypto/objects') diff --git a/crypto/objects/obj_dat.c b/crypto/objects/obj_dat.c index 3623e152b1..e999ef7831 100644 --- a/crypto/objects/obj_dat.c +++ b/crypto/objects/obj_dat.c @@ -81,9 +81,9 @@ static const unsigned int ln_objs[1]; static const unsigned int obj_objs[1]; #endif -DECLARE_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, sn_cmp); -DECLARE_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, ln_cmp); -DECLARE_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, obj_cmp); +DECLARE_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, sn); +DECLARE_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, ln); +DECLARE_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, obj); #define ADDED_DATA 0 #define ADDED_SNAME 1 @@ -103,12 +103,12 @@ static LHASH_OF(ADDED_OBJ) *added=NULL; static int sn_cmp(const ASN1_OBJECT * const *a, const unsigned int *b) { return(strcmp((*a)->sn,nid_objs[*b].sn)); } -IMPLEMENT_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, sn_cmp); +IMPLEMENT_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, sn); static int ln_cmp(const ASN1_OBJECT * const *a, const unsigned int *b) { return(strcmp((*a)->ln,nid_objs[*b].ln)); } -IMPLEMENT_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, ln_cmp); +IMPLEMENT_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, ln); static unsigned long added_obj_hash(const ADDED_OBJ *ca) { @@ -393,7 +393,7 @@ static int obj_cmp(const ASN1_OBJECT * const *ap, const unsigned int *bp) return(memcmp(a->data,b->data,a->length)); } -IMPLEMENT_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, obj_cmp); +IMPLEMENT_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, obj); int OBJ_obj2nid(const ASN1_OBJECT *a) { @@ -412,8 +412,7 @@ int OBJ_obj2nid(const ASN1_OBJECT *a) adp=lh_ADDED_OBJ_retrieve(added,&ad); if (adp != NULL) return (adp->obj->nid); } - op=OBJ_bsearch(const ASN1_OBJECT *, &a, const unsigned int, obj_objs, - NUM_OBJ, obj_cmp); + op=OBJ_bsearch_obj(&a, obj_objs, NUM_OBJ); if (op == NULL) return(NID_undef); return(nid_objs[*op].nid); @@ -648,8 +647,7 @@ int OBJ_ln2nid(const char *s) adp=lh_ADDED_OBJ_retrieve(added,&ad); if (adp != NULL) return (adp->obj->nid); } - op=OBJ_bsearch(const ASN1_OBJECT *, &oo, const unsigned int, ln_objs, - NUM_LN, ln_cmp); + op=OBJ_bsearch_ln(&oo, ln_objs, NUM_LN); if (op == NULL) return(NID_undef); return(nid_objs[*op].nid); } @@ -669,8 +667,7 @@ int OBJ_sn2nid(const char *s) adp=lh_ADDED_OBJ_retrieve(added,&ad); if (adp != NULL) return (adp->obj->nid); } - op=OBJ_bsearch(const ASN1_OBJECT *, &oo, const unsigned int, sn_objs, - NUM_SN, sn_cmp); + op=OBJ_bsearch_sn(&oo, sn_objs, NUM_SN); if (op == NULL) return(NID_undef); return(nid_objs[*op].nid); } diff --git a/crypto/objects/obj_xref.c b/crypto/objects/obj_xref.c index 30d544b1a4..6230c365a1 100644 --- a/crypto/objects/obj_xref.c +++ b/crypto/objects/obj_xref.c @@ -62,22 +62,22 @@ DECLARE_STACK_OF(nid_triple) STACK_OF(nid_triple) *sig_app, *sigx_app; -static int cmp_sig(const nid_triple *a, const nid_triple *b) +static int sig_cmp(const nid_triple *a, const nid_triple *b) { return a->sign_id - b->sign_id; } -DECLARE_OBJ_BSEARCH_CMP_FN(nid_triple, nid_triple, cmp_sig); -IMPLEMENT_OBJ_BSEARCH_CMP_FN(nid_triple, nid_triple, cmp_sig); +DECLARE_OBJ_BSEARCH_CMP_FN(nid_triple, nid_triple, sig); +IMPLEMENT_OBJ_BSEARCH_CMP_FN(nid_triple, nid_triple, sig); -static int cmp_sig_sk(const nid_triple * const *a, const nid_triple * const *b) +static int sig_sk_cmp(const nid_triple * const *a, const nid_triple * const *b) { return (*a)->sign_id - (*b)->sign_id; } -DECLARE_OBJ_BSEARCH_CMP_FN(const nid_triple *, const nid_triple *, cmp_sigx); +DECLARE_OBJ_BSEARCH_CMP_FN(const nid_triple *, const nid_triple *, sigx); -static int cmp_sigx(const nid_triple * const *a, const nid_triple * const *b) +static int sigx_cmp(const nid_triple * const *a, const nid_triple * const *b) { int ret; ret = (*a)->hash_id - (*b)->hash_id; @@ -86,7 +86,7 @@ static int cmp_sigx(const nid_triple * const *a, const nid_triple * const *b) return (*a)->pkey_id - (*b)->pkey_id; } -IMPLEMENT_OBJ_BSEARCH_CMP_FN(const nid_triple *, const nid_triple *, cmp_sigx); +IMPLEMENT_OBJ_BSEARCH_CMP_FN(const nid_triple *, const nid_triple *, sigx); int OBJ_find_sigid_algs(int signid, int *pdig_nid, int *ppkey_nid) { @@ -104,10 +104,8 @@ int OBJ_find_sigid_algs(int signid, int *pdig_nid, int *ppkey_nid) #ifndef OBJ_XREF_TEST2 if (rv == NULL) { - rv = OBJ_bsearch(const nid_triple,&tmp, - const nid_triple,sigoid_srt, - sizeof(sigoid_srt) / sizeof(nid_triple), - cmp_sig); + rv = OBJ_bsearch_sig(&tmp, sigoid_srt, + sizeof(sigoid_srt) / sizeof(nid_triple)); } #endif if (rv == NULL) @@ -139,10 +137,9 @@ int OBJ_find_sigid_by_algs(int *psignid, int dig_nid, int pkey_nid) #ifndef OBJ_XREF_TEST2 if (rv == NULL) { - rv = OBJ_bsearch(const nid_triple *,&t, - const nid_triple *,sigoid_srt_xref, - sizeof(sigoid_srt_xref) / sizeof(nid_triple *), - cmp_sigx); + rv = OBJ_bsearch_sigx(&t, sigoid_srt_xref, + sizeof(sigoid_srt_xref) / sizeof(nid_triple *) + ); } #endif if (rv == NULL) @@ -155,11 +152,11 @@ int OBJ_add_sigid(int signid, int dig_id, int pkey_id) { nid_triple *ntr; if (!sig_app) - sig_app = sk_nid_triple_new(cmp_sig_sk); + sig_app = sk_nid_triple_new(sig_sk_cmp); if (!sig_app) return 0; if (!sigx_app) - sigx_app = sk_nid_triple_new(cmp_sigx); + sigx_app = sk_nid_triple_new(sigx_cmp); if (!sigx_app) return 0; ntr = OPENSSL_malloc(sizeof(int) * 3); diff --git a/crypto/objects/objects.h b/crypto/objects/objects.h index 3e6d71021b..65b6f01358 100644 --- a/crypto/objects/objects.h +++ b/crypto/objects/objects.h @@ -1018,16 +1018,15 @@ const void * OBJ_bsearch_ex_(const void *key,const void *base,int num, int (*cmp)(const void *, const void *), int flags); -#define _DECLARE_OBJ_BSEARCH_CMP_FN(scope, type1, type2, cmp) \ - scope type1 *cmp##_type_1; \ - scope type2 *cmp##_type_2; \ - scope int cmp##_BSEARCH_CMP_FN(const void *, const void *); \ - scope int cmp(type1 const *, type2 const *) +#define _DECLARE_OBJ_BSEARCH_CMP_FN(scope, type1, type2, nm) \ + static int nm##_cmp_BSEARCH_CMP_FN(const void *, const void *); \ + static int nm##_cmp(type1 const *, type2 const *); \ + scope type2 * OBJ_bsearch_##nm(type1 *key, type2 const *base, int num) #define DECLARE_OBJ_BSEARCH_CMP_FN(type1, type2, cmp) \ _DECLARE_OBJ_BSEARCH_CMP_FN(static, type1, type2, cmp) -#define DECLARE_OBJ_BSEARCH_GLOBAL_CMP_FN(type1, type2, cmp) \ - _DECLARE_OBJ_BSEARCH_CMP_FN(, type1, type2, cmp) +#define DECLARE_OBJ_BSEARCH_GLOBAL_CMP_FN(type1, type2, nm) \ + type2 * OBJ_bsearch_##nm(type1 *key, type2 const *base, int num) /* * Unsolved problem: if a type is actually a pointer type, like @@ -1055,12 +1054,17 @@ const void * OBJ_bsearch_ex_(const void *key,const void *base,int num, * the non-constness means a lot of complication, and in practice * comparison routines do always not touch their arguments. */ -#define _IMPLEMENT_OBJ_BSEARCH_CMP_FN(scope, type1, type2, cmp) \ - scope int cmp##_BSEARCH_CMP_FN(const void *a_, const void *b_) \ +#define _IMPLEMENT_OBJ_BSEARCH_CMP_FN(scope, type1, type2, nm) \ + static int nm##_cmp_BSEARCH_CMP_FN(const void *a_, const void *b_) \ { \ type1 const *a = a_; \ type2 const *b = b_; \ - return cmp(a,b); \ + return nm##_cmp(a,b); \ + } \ + scope type2 *OBJ_bsearch_##nm(type1 *key, type2 const *base, int num) \ + { \ + return (type2 *)OBJ_bsearch_(key, base, num, sizeof(type2), \ + nm##_cmp_BSEARCH_CMP_FN); \ } \ extern void dummy_prototype(void) -- cgit v1.2.3