1998-12-21 11:00:56 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.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>
|
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.
|
|
|
|
*/
|
|
|
|
static LHASH *names_lh=NULL;
|
|
|
|
static int names_type_num=OBJ_NAME_TYPE_NUM;
|
2000-01-29 01:22:03 +00:00
|
|
|
|
|
|
|
typedef struct name_funcs_st
|
|
|
|
{
|
|
|
|
unsigned long (*hash_func)();
|
|
|
|
int (*cmp_func)();
|
|
|
|
void (*free_func)();
|
|
|
|
} 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
|
|
|
|
|
|
|
static unsigned long obj_name_hash(OBJ_NAME *a);
|
|
|
|
static int obj_name_cmp(OBJ_NAME *a,OBJ_NAME *b);
|
|
|
|
|
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();
|
|
|
|
names_lh=lh_new(obj_name_hash,obj_name_cmp);
|
|
|
|
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 *),
|
|
|
|
int (*cmp_func)(const void *, const void *),
|
|
|
|
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();
|
|
|
|
}
|
2000-01-29 01:22:03 +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));
|
2000-01-29 01:22:03 +00:00
|
|
|
name_funcs->hash_func = lh_strhash;
|
2000-02-25 20:40:12 +00:00
|
|
|
name_funcs->cmp_func = (int (*)())strcmp;
|
2000-01-29 01:22:03 +00:00
|
|
|
name_funcs->free_func = 0; /* NULL is often declared to
|
|
|
|
* ((void *)0), which according
|
|
|
|
* to Compaq C is not really
|
|
|
|
* compatible with a function
|
|
|
|
* pointer. -- Richard Levitte*/
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
1999-04-19 21:31:43 +00:00
|
|
|
static int obj_name_cmp(OBJ_NAME *a, OBJ_NAME *b)
|
1998-12-21 11:00:56 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2000-01-29 01:22:03 +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);
|
|
|
|
}
|
|
|
|
|
1999-04-19 21:31:43 +00:00
|
|
|
static unsigned long obj_name_hash(OBJ_NAME *a)
|
1998-12-21 11:00:56 +00:00
|
|
|
{
|
|
|
|
unsigned long ret;
|
|
|
|
|
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
|
|
|
{
|
2000-01-29 01:22:03 +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-01-30 22:20:28 +00:00
|
|
|
ret=(OBJ_NAME *)lh_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;
|
|
|
|
|
2000-01-30 22:20:28 +00:00
|
|
|
ret=(OBJ_NAME *)lh_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
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
{
|
|
|
|
if (lh_error(names_lh))
|
|
|
|
{
|
|
|
|
/* 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;
|
2000-01-30 22:20:28 +00:00
|
|
|
ret=(OBJ_NAME *)lh_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
|
|
|
|
*/
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int free_type;
|
|
|
|
|
1999-04-19 21:31:43 +00:00
|
|
|
static void names_lh_free(OBJ_NAME *onp, int type)
|
|
|
|
{
|
1999-01-17 14:19:31 +00:00
|
|
|
if(onp == NULL)
|
|
|
|
return;
|
|
|
|
|
1998-12-21 11:00:56 +00:00
|
|
|
if ((free_type < 0) || (free_type == onp->type))
|
|
|
|
{
|
|
|
|
OBJ_NAME_remove(onp->name,onp->type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
down_load=names_lh->down_load;
|
|
|
|
names_lh->down_load=0;
|
|
|
|
|
|
|
|
lh_doall(names_lh,names_lh_free);
|
|
|
|
if (type < 0)
|
|
|
|
{
|
|
|
|
lh_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
|
|
|
|
names_lh->down_load=down_load;
|
|
|
|
}
|
|
|
|
|