1998-12-21 11:00:56 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2004-12-05 01:03:15 +00:00
|
|
|
#include <openssl/err.h>
|
1999-04-23 22:13:45 +00:00
|
|
|
#include <openssl/lhash.h>
|
|
|
|
#include <openssl/objects.h>
|
2000-01-29 01:22:03 +00:00
|
|
|
#include <openssl/safestack.h>
|
2001-02-20 19:13:46 +00:00
|
|
|
#include <openssl/e_os2.h>
|
|
|
|
|
|
|
|
/* Later versions of DEC C has started to add lnkage information to certain
|
|
|
|
* functions, which makes it tricky to use them as values to regular function
|
|
|
|
* pointers. One way is to define a macro that takes care of casting them
|
|
|
|
* correctly.
|
|
|
|
*/
|
|
|
|
#ifdef OPENSSL_SYS_VMS_DECC
|
|
|
|
# define OPENSSL_strcmp (int (*)(const char *,const char *))strcmp
|
|
|
|
#else
|
|
|
|
# define OPENSSL_strcmp strcmp
|
|
|
|
#endif
|
2000-01-18 09:30:51 +00:00
|
|
|
|
1998-12-21 11:00:56 +00:00
|
|
|
/* I use the ex_data stuff to manage the identifiers for the obj_name_types
|
|
|
|
* that applications may define. I only really use the free function field.
|
|
|
|
*/
|
2008-05-26 11:24:29 +00:00
|
|
|
DECLARE_LHASH_OF(OBJ_NAME);
|
|
|
|
static LHASH_OF(OBJ_NAME) *names_lh=NULL;
|
1998-12-21 11:00:56 +00:00
|
|
|
static int names_type_num=OBJ_NAME_TYPE_NUM;
|
2000-01-29 01:22:03 +00:00
|
|
|
|
|
|
|
typedef struct name_funcs_st
|
|
|
|
{
|
2000-11-20 04:14:19 +00:00
|
|
|
unsigned long (*hash_func)(const char *name);
|
|
|
|
int (*cmp_func)(const char *a,const char *b);
|
|
|
|
void (*free_func)(const char *, int, const char *);
|
2000-01-29 01:22:03 +00:00
|
|
|
} NAME_FUNCS;
|
|
|
|
|
|
|
|
DECLARE_STACK_OF(NAME_FUNCS)
|
|
|
|
IMPLEMENT_STACK_OF(NAME_FUNCS)
|
|
|
|
|
2000-03-03 01:37:11 +00:00
|
|
|
static STACK_OF(NAME_FUNCS) *name_funcs_stack;
|
1998-12-21 11:00:56 +00:00
|
|
|
|
2000-12-08 20:02:01 +00:00
|
|
|
/* The LHASH callbacks now use the raw "void *" prototypes and do per-variable
|
|
|
|
* casting in the functions. This prevents function pointer casting without the
|
|
|
|
* need for macro-generated wrapper functions. */
|
1998-12-21 11:00:56 +00:00
|
|
|
|
2000-12-08 20:02:01 +00:00
|
|
|
/* static unsigned long obj_name_hash(OBJ_NAME *a); */
|
2000-12-13 17:15:03 +00:00
|
|
|
static unsigned long obj_name_hash(const void *a_void);
|
2000-12-08 20:02:01 +00:00
|
|
|
/* static int obj_name_cmp(OBJ_NAME *a,OBJ_NAME *b); */
|
2000-12-13 17:15:03 +00:00
|
|
|
static int obj_name_cmp(const void *a_void,const void *b_void);
|
2000-12-04 03:02:44 +00:00
|
|
|
|
2008-05-26 11:24:29 +00:00
|
|
|
static IMPLEMENT_LHASH_HASH_FN(obj_name, OBJ_NAME)
|
|
|
|
static IMPLEMENT_LHASH_COMP_FN(obj_name, OBJ_NAME)
|
|
|
|
|
1999-04-19 21:31:43 +00:00
|
|
|
int OBJ_NAME_init(void)
|
1998-12-21 11:00:56 +00:00
|
|
|
{
|
|
|
|
if (names_lh != NULL) return(1);
|
|
|
|
MemCheck_off();
|
2008-05-26 11:24:29 +00:00
|
|
|
names_lh=lh_OBJ_NAME_new();
|
1998-12-21 11:00:56 +00:00
|
|
|
MemCheck_on();
|
|
|
|
return(names_lh != NULL);
|
|
|
|
}
|
|
|
|
|
2000-06-21 02:25:30 +00:00
|
|
|
int OBJ_NAME_new_index(unsigned long (*hash_func)(const char *),
|
2000-11-20 04:14:19 +00:00
|
|
|
int (*cmp_func)(const char *, const char *),
|
2000-06-21 02:25:30 +00:00
|
|
|
void (*free_func)(const char *, int, const char *))
|
1998-12-21 11:00:56 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
int i;
|
2000-01-29 01:22:03 +00:00
|
|
|
NAME_FUNCS *name_funcs;
|
|
|
|
|
|
|
|
if (name_funcs_stack == NULL)
|
1998-12-21 11:00:56 +00:00
|
|
|
{
|
|
|
|
MemCheck_off();
|
2000-01-29 01:22:03 +00:00
|
|
|
name_funcs_stack=sk_NAME_FUNCS_new_null();
|
1998-12-21 11:00:56 +00:00
|
|
|
MemCheck_on();
|
|
|
|
}
|
2013-01-13 21:04:39 +00:00
|
|
|
if (name_funcs_stack == NULL)
|
1998-12-21 11:00:56 +00:00
|
|
|
{
|
|
|
|
/* ERROR */
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
ret=names_type_num;
|
|
|
|
names_type_num++;
|
2000-01-29 01:22:03 +00:00
|
|
|
for (i=sk_NAME_FUNCS_num(name_funcs_stack); i<names_type_num; i++)
|
1998-12-21 11:00:56 +00:00
|
|
|
{
|
|
|
|
MemCheck_off();
|
2000-06-01 22:19:21 +00:00
|
|
|
name_funcs = OPENSSL_malloc(sizeof(NAME_FUNCS));
|
2002-05-30 16:47:45 +00:00
|
|
|
MemCheck_on();
|
2004-12-05 01:03:15 +00:00
|
|
|
if (!name_funcs)
|
|
|
|
{
|
|
|
|
OBJerr(OBJ_F_OBJ_NAME_NEW_INDEX,ERR_R_MALLOC_FAILURE);
|
|
|
|
return(0);
|
|
|
|
}
|
2001-02-20 16:31:15 +00:00
|
|
|
name_funcs->hash_func = lh_strhash;
|
2001-02-20 19:13:46 +00:00
|
|
|
name_funcs->cmp_func = OPENSSL_strcmp;
|
2000-01-29 01:22:03 +00:00
|
|
|
name_funcs->free_func = 0; /* NULL is often declared to
|
2000-11-20 04:14:19 +00:00
|
|
|
* ((void *)0), which according
|
|
|
|
* to Compaq C is not really
|
|
|
|
* compatible with a function
|
|
|
|
* pointer. -- Richard Levitte*/
|
2002-05-30 16:47:45 +00:00
|
|
|
MemCheck_off();
|
2000-01-29 01:22:03 +00:00
|
|
|
sk_NAME_FUNCS_push(name_funcs_stack,name_funcs);
|
1998-12-21 11:00:56 +00:00
|
|
|
MemCheck_on();
|
|
|
|
}
|
2000-01-29 01:22:03 +00:00
|
|
|
name_funcs = sk_NAME_FUNCS_value(name_funcs_stack, ret);
|
1998-12-21 11:00:56 +00:00
|
|
|
if (hash_func != NULL)
|
2000-01-29 01:22:03 +00:00
|
|
|
name_funcs->hash_func = hash_func;
|
1998-12-21 11:00:56 +00:00
|
|
|
if (cmp_func != NULL)
|
2000-01-29 01:22:03 +00:00
|
|
|
name_funcs->cmp_func = cmp_func;
|
1998-12-21 11:00:56 +00:00
|
|
|
if (free_func != NULL)
|
2000-01-29 01:22:03 +00:00
|
|
|
name_funcs->free_func = free_func;
|
1998-12-21 11:00:56 +00:00
|
|
|
return(ret);
|
|
|
|
}
|
|
|
|
|
2000-12-08 20:02:01 +00:00
|
|
|
/* static int obj_name_cmp(OBJ_NAME *a, OBJ_NAME *b) */
|
2000-12-13 17:15:03 +00:00
|
|
|
static int obj_name_cmp(const void *a_void, const void *b_void)
|
1998-12-21 11:00:56 +00:00
|
|
|
{
|
|
|
|
int ret;
|
2005-04-05 10:29:43 +00:00
|
|
|
const OBJ_NAME *a = (const OBJ_NAME *)a_void;
|
|
|
|
const OBJ_NAME *b = (const OBJ_NAME *)b_void;
|
1998-12-21 11:00:56 +00:00
|
|
|
|
|
|
|
ret=a->type-b->type;
|
|
|
|
if (ret == 0)
|
|
|
|
{
|
2000-01-29 01:22:03 +00:00
|
|
|
if ((name_funcs_stack != NULL)
|
|
|
|
&& (sk_NAME_FUNCS_num(name_funcs_stack) > a->type))
|
1998-12-21 11:00:56 +00:00
|
|
|
{
|
2001-02-20 16:31:15 +00:00
|
|
|
ret=sk_NAME_FUNCS_value(name_funcs_stack,
|
|
|
|
a->type)->cmp_func(a->name,b->name);
|
1998-12-21 11:00:56 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
ret=strcmp(a->name,b->name);
|
|
|
|
}
|
|
|
|
return(ret);
|
|
|
|
}
|
|
|
|
|
2000-12-08 20:02:01 +00:00
|
|
|
/* static unsigned long obj_name_hash(OBJ_NAME *a) */
|
2000-12-13 17:15:03 +00:00
|
|
|
static unsigned long obj_name_hash(const void *a_void)
|
1998-12-21 11:00:56 +00:00
|
|
|
{
|
|
|
|
unsigned long ret;
|
2005-04-05 10:29:43 +00:00
|
|
|
const OBJ_NAME *a = (const OBJ_NAME *)a_void;
|
1998-12-21 11:00:56 +00:00
|
|
|
|
2000-01-29 01:22:03 +00:00
|
|
|
if ((name_funcs_stack != NULL) && (sk_NAME_FUNCS_num(name_funcs_stack) > a->type))
|
1998-12-21 11:00:56 +00:00
|
|
|
{
|
2001-02-20 16:31:15 +00:00
|
|
|
ret=sk_NAME_FUNCS_value(name_funcs_stack,
|
|
|
|
a->type)->hash_func(a->name);
|
1998-12-21 11:00:56 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret=lh_strhash(a->name);
|
|
|
|
}
|
|
|
|
ret^=a->type;
|
|
|
|
return(ret);
|
|
|
|
}
|
|
|
|
|
1999-04-19 21:31:43 +00:00
|
|
|
const char *OBJ_NAME_get(const char *name, int type)
|
1998-12-21 11:00:56 +00:00
|
|
|
{
|
|
|
|
OBJ_NAME on,*ret;
|
|
|
|
int num=0,alias;
|
|
|
|
|
|
|
|
if (name == NULL) return(NULL);
|
|
|
|
if ((names_lh == NULL) && !OBJ_NAME_init()) return(NULL);
|
|
|
|
|
|
|
|
alias=type&OBJ_NAME_ALIAS;
|
|
|
|
type&= ~OBJ_NAME_ALIAS;
|
|
|
|
|
|
|
|
on.name=name;
|
|
|
|
on.type=type;
|
|
|
|
|
|
|
|
for (;;)
|
2000-11-20 04:14:19 +00:00
|
|
|
{
|
2008-05-26 11:24:29 +00:00
|
|
|
ret=lh_OBJ_NAME_retrieve(names_lh,&on);
|
1998-12-21 11:00:56 +00:00
|
|
|
if (ret == NULL) return(NULL);
|
|
|
|
if ((ret->alias) && !alias)
|
|
|
|
{
|
|
|
|
if (++num > 10) return(NULL);
|
|
|
|
on.name=ret->data;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return(ret->data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-04-19 21:31:43 +00:00
|
|
|
int OBJ_NAME_add(const char *name, int type, const char *data)
|
1998-12-21 11:00:56 +00:00
|
|
|
{
|
|
|
|
OBJ_NAME *onp,*ret;
|
|
|
|
int alias;
|
|
|
|
|
|
|
|
if ((names_lh == NULL) && !OBJ_NAME_init()) return(0);
|
|
|
|
|
|
|
|
alias=type&OBJ_NAME_ALIAS;
|
|
|
|
type&= ~OBJ_NAME_ALIAS;
|
|
|
|
|
2000-06-01 22:19:21 +00:00
|
|
|
onp=(OBJ_NAME *)OPENSSL_malloc(sizeof(OBJ_NAME));
|
1998-12-21 11:00:56 +00:00
|
|
|
if (onp == NULL)
|
|
|
|
{
|
|
|
|
/* ERROR */
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
onp->name=name;
|
|
|
|
onp->alias=alias;
|
|
|
|
onp->type=type;
|
|
|
|
onp->data=data;
|
|
|
|
|
2008-05-26 11:24:29 +00:00
|
|
|
ret=lh_OBJ_NAME_insert(names_lh,onp);
|
1998-12-21 11:00:56 +00:00
|
|
|
if (ret != NULL)
|
|
|
|
{
|
|
|
|
/* free things */
|
2000-01-29 01:22:03 +00:00
|
|
|
if ((name_funcs_stack != NULL) && (sk_NAME_FUNCS_num(name_funcs_stack) > ret->type))
|
1998-12-21 11:00:56 +00:00
|
|
|
{
|
2000-01-29 01:22:03 +00:00
|
|
|
/* XXX: I'm not sure I understand why the free
|
|
|
|
* function should get three arguments...
|
|
|
|
* -- Richard Levitte
|
|
|
|
*/
|
2001-02-20 16:31:15 +00:00
|
|
|
sk_NAME_FUNCS_value(name_funcs_stack,
|
|
|
|
ret->type)->free_func(ret->name,ret->type,ret->data);
|
1998-12-21 11:00:56 +00:00
|
|
|
}
|
2000-06-01 22:19:21 +00:00
|
|
|
OPENSSL_free(ret);
|
1998-12-21 11:00:56 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-05-26 11:24:29 +00:00
|
|
|
if (lh_OBJ_NAME_error(names_lh))
|
1998-12-21 11:00:56 +00:00
|
|
|
{
|
|
|
|
/* ERROR */
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
|
1999-04-19 21:31:43 +00:00
|
|
|
int OBJ_NAME_remove(const char *name, int type)
|
1998-12-21 11:00:56 +00:00
|
|
|
{
|
|
|
|
OBJ_NAME on,*ret;
|
|
|
|
|
|
|
|
if (names_lh == NULL) return(0);
|
|
|
|
|
|
|
|
type&= ~OBJ_NAME_ALIAS;
|
|
|
|
on.name=name;
|
|
|
|
on.type=type;
|
2008-05-26 11:24:29 +00:00
|
|
|
ret=lh_OBJ_NAME_delete(names_lh,&on);
|
1998-12-21 11:00:56 +00:00
|
|
|
if (ret != NULL)
|
|
|
|
{
|
|
|
|
/* free things */
|
2000-01-29 01:22:03 +00:00
|
|
|
if ((name_funcs_stack != NULL) && (sk_NAME_FUNCS_num(name_funcs_stack) > ret->type))
|
1998-12-21 11:00:56 +00:00
|
|
|
{
|
2000-01-29 01:22:03 +00:00
|
|
|
/* XXX: I'm not sure I understand why the free
|
|
|
|
* function should get three arguments...
|
|
|
|
* -- Richard Levitte
|
|
|
|
*/
|
2001-02-20 16:31:15 +00:00
|
|
|
sk_NAME_FUNCS_value(name_funcs_stack,
|
|
|
|
ret->type)->free_func(ret->name,ret->type,ret->data);
|
1998-12-21 11:00:56 +00:00
|
|
|
}
|
2000-06-01 22:19:21 +00:00
|
|
|
OPENSSL_free(ret);
|
1998-12-21 11:00:56 +00:00
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
2000-11-20 04:14:19 +00:00
|
|
|
struct doall
|
|
|
|
{
|
|
|
|
int type;
|
|
|
|
void (*fn)(const OBJ_NAME *,void *arg);
|
|
|
|
void *arg;
|
|
|
|
};
|
|
|
|
|
2008-05-26 11:24:29 +00:00
|
|
|
static void do_all_fn_doall_arg(const OBJ_NAME *name,struct doall *d)
|
2000-11-20 04:14:19 +00:00
|
|
|
{
|
|
|
|
if(name->type == d->type)
|
|
|
|
d->fn(name,d->arg);
|
|
|
|
}
|
|
|
|
|
2008-05-26 11:24:29 +00:00
|
|
|
static IMPLEMENT_LHASH_DOALL_ARG_FN(do_all_fn, const OBJ_NAME, struct doall)
|
2001-01-09 00:24:38 +00:00
|
|
|
|
2000-11-20 04:14:19 +00:00
|
|
|
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;
|
|
|
|
|
2008-05-26 11:24:29 +00:00
|
|
|
lh_OBJ_NAME_doall_arg(names_lh, LHASH_DOALL_ARG_FN(do_all_fn),
|
|
|
|
struct doall, &d);
|
2000-11-20 04:14:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct doall_sorted
|
|
|
|
{
|
|
|
|
int type;
|
2008-11-12 03:58:08 +00:00
|
|
|
int n;
|
2000-11-20 04:14:19 +00:00
|
|
|
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;
|
2008-05-26 11:24:29 +00:00
|
|
|
d.names=OPENSSL_malloc(lh_OBJ_NAME_num_items(names_lh)*sizeof *d.names);
|
2000-11-20 04:14:19 +00:00
|
|
|
d.n=0;
|
|
|
|
OBJ_NAME_do_all(type,do_all_sorted_fn,&d);
|
|
|
|
|
2008-11-12 03:58:08 +00:00
|
|
|
qsort((void *)d.names,d.n,sizeof *d.names,do_all_sorted_cmp);
|
2000-11-20 04:14:19 +00:00
|
|
|
|
|
|
|
for(n=0 ; n < d.n ; ++n)
|
|
|
|
fn(d.names[n],arg);
|
|
|
|
|
2000-12-21 01:38:55 +00:00
|
|
|
OPENSSL_free((void *)d.names);
|
2000-11-20 04:14:19 +00:00
|
|
|
}
|
|
|
|
|
1998-12-21 11:00:56 +00:00
|
|
|
static int free_type;
|
|
|
|
|
2008-05-26 11:24:29 +00:00
|
|
|
static void names_lh_free_doall(OBJ_NAME *onp)
|
|
|
|
{
|
|
|
|
if (onp == NULL)
|
2000-11-20 04:14:19 +00:00
|
|
|
return;
|
1999-01-17 14:19:31 +00:00
|
|
|
|
2008-05-26 11:24:29 +00:00
|
|
|
if (free_type < 0 || free_type == onp->type)
|
1998-12-21 11:00:56 +00:00
|
|
|
OBJ_NAME_remove(onp->name,onp->type);
|
|
|
|
}
|
|
|
|
|
2008-05-26 11:24:29 +00:00
|
|
|
static IMPLEMENT_LHASH_DOALL_FN(names_lh_free, OBJ_NAME)
|
2001-01-09 00:24:38 +00:00
|
|
|
|
2000-01-29 01:22:03 +00:00
|
|
|
static void name_funcs_free(NAME_FUNCS *ptr)
|
|
|
|
{
|
2000-06-01 22:19:21 +00:00
|
|
|
OPENSSL_free(ptr);
|
2000-01-29 01:22:03 +00:00
|
|
|
}
|
|
|
|
|
1999-04-19 21:31:43 +00:00
|
|
|
void OBJ_NAME_cleanup(int type)
|
1998-12-21 11:00:56 +00:00
|
|
|
{
|
|
|
|
unsigned long down_load;
|
|
|
|
|
|
|
|
if (names_lh == NULL) return;
|
|
|
|
|
|
|
|
free_type=type;
|
2008-05-26 11:24:29 +00:00
|
|
|
down_load=lh_OBJ_NAME_down_load(names_lh);
|
|
|
|
lh_OBJ_NAME_down_load(names_lh)=0;
|
1998-12-21 11:00:56 +00:00
|
|
|
|
2008-05-26 11:24:29 +00:00
|
|
|
lh_OBJ_NAME_doall(names_lh,LHASH_DOALL_FN(names_lh_free));
|
1998-12-21 11:00:56 +00:00
|
|
|
if (type < 0)
|
|
|
|
{
|
2008-05-26 11:24:29 +00:00
|
|
|
lh_OBJ_NAME_free(names_lh);
|
2000-01-29 01:22:03 +00:00
|
|
|
sk_NAME_FUNCS_pop_free(name_funcs_stack,name_funcs_free);
|
1998-12-21 11:00:56 +00:00
|
|
|
names_lh=NULL;
|
2000-01-29 01:22:03 +00:00
|
|
|
name_funcs_stack = NULL;
|
1998-12-21 11:00:56 +00:00
|
|
|
}
|
|
|
|
else
|
2008-05-26 11:24:29 +00:00
|
|
|
lh_OBJ_NAME_down_load(names_lh)=down_load;
|
1998-12-21 11:00:56 +00:00
|
|
|
}
|
|
|
|
|