5 #include <openssl/lhash.h>
6 #include <openssl/objects.h>
7 #include <openssl/safestack.h>
9 /* I use the ex_data stuff to manage the identifiers for the obj_name_types
10 * that applications may define. I only really use the free function field.
12 static LHASH *names_lh=NULL;
13 static int names_type_num=OBJ_NAME_TYPE_NUM;
15 typedef struct name_funcs_st
17 unsigned long (*hash_func)();
22 DECLARE_STACK_OF(NAME_FUNCS)
23 IMPLEMENT_STACK_OF(NAME_FUNCS)
25 static STACK_OF(NAME_FUNCS) *name_funcs_stack;
27 static unsigned long obj_name_hash(OBJ_NAME *a);
28 static int obj_name_cmp(OBJ_NAME *a,OBJ_NAME *b);
30 int OBJ_NAME_init(void)
32 if (names_lh != NULL) return(1);
34 names_lh=lh_new(obj_name_hash,obj_name_cmp);
36 return(names_lh != NULL);
39 int OBJ_NAME_new_index(unsigned long (*hash_func)(const char *),
40 int (*cmp_func)(const void *, const void *),
41 void (*free_func)(const char *, int, const char *))
45 NAME_FUNCS *name_funcs;
47 if (name_funcs_stack == NULL)
50 name_funcs_stack=sk_NAME_FUNCS_new_null();
53 if ((name_funcs_stack == NULL))
60 for (i=sk_NAME_FUNCS_num(name_funcs_stack); i<names_type_num; i++)
63 name_funcs = OPENSSL_malloc(sizeof(NAME_FUNCS));
64 name_funcs->hash_func = lh_strhash;
65 name_funcs->cmp_func = (int (*)())strcmp;
66 name_funcs->free_func = 0; /* NULL is often declared to
67 * ((void *)0), which according
68 * to Compaq C is not really
69 * compatible with a function
70 * pointer. -- Richard Levitte*/
71 sk_NAME_FUNCS_push(name_funcs_stack,name_funcs);
74 name_funcs = sk_NAME_FUNCS_value(name_funcs_stack, ret);
75 if (hash_func != NULL)
76 name_funcs->hash_func = hash_func;
78 name_funcs->cmp_func = cmp_func;
79 if (free_func != NULL)
80 name_funcs->free_func = free_func;
84 static int obj_name_cmp(OBJ_NAME *a, OBJ_NAME *b)
91 if ((name_funcs_stack != NULL)
92 && (sk_NAME_FUNCS_num(name_funcs_stack) > a->type))
94 ret=sk_NAME_FUNCS_value(name_funcs_stack,a->type)
95 ->cmp_func(a->name,b->name);
98 ret=strcmp(a->name,b->name);
103 static unsigned long obj_name_hash(OBJ_NAME *a)
107 if ((name_funcs_stack != NULL) && (sk_NAME_FUNCS_num(name_funcs_stack) > a->type))
109 ret=sk_NAME_FUNCS_value(name_funcs_stack,a->type)
110 ->hash_func(a->name);
114 ret=lh_strhash(a->name);
120 const char *OBJ_NAME_get(const char *name, int type)
125 if (name == NULL) return(NULL);
126 if ((names_lh == NULL) && !OBJ_NAME_init()) return(NULL);
128 alias=type&OBJ_NAME_ALIAS;
129 type&= ~OBJ_NAME_ALIAS;
136 ret=(OBJ_NAME *)lh_retrieve(names_lh,&on);
137 if (ret == NULL) return(NULL);
138 if ((ret->alias) && !alias)
140 if (++num > 10) return(NULL);
150 int OBJ_NAME_add(const char *name, int type, const char *data)
155 if ((names_lh == NULL) && !OBJ_NAME_init()) return(0);
157 alias=type&OBJ_NAME_ALIAS;
158 type&= ~OBJ_NAME_ALIAS;
160 onp=(OBJ_NAME *)OPENSSL_malloc(sizeof(OBJ_NAME));
172 ret=(OBJ_NAME *)lh_insert(names_lh,onp);
176 if ((name_funcs_stack != NULL) && (sk_NAME_FUNCS_num(name_funcs_stack) > ret->type))
178 /* XXX: I'm not sure I understand why the free
179 * function should get three arguments...
182 sk_NAME_FUNCS_value(name_funcs_stack,ret->type)
183 ->free_func(ret->name,ret->type,ret->data);
189 if (lh_error(names_lh))
198 int OBJ_NAME_remove(const char *name, int type)
202 if (names_lh == NULL) return(0);
204 type&= ~OBJ_NAME_ALIAS;
207 ret=(OBJ_NAME *)lh_delete(names_lh,&on);
211 if ((name_funcs_stack != NULL) && (sk_NAME_FUNCS_num(name_funcs_stack) > ret->type))
213 /* XXX: I'm not sure I understand why the free
214 * function should get three arguments...
217 sk_NAME_FUNCS_value(name_funcs_stack,ret->type)
218 ->free_func(ret->name,ret->type,ret->data);
227 static int free_type;
229 static void names_lh_free(OBJ_NAME *onp, int type)
234 if ((free_type < 0) || (free_type == onp->type))
236 OBJ_NAME_remove(onp->name,onp->type);
240 static void name_funcs_free(NAME_FUNCS *ptr)
245 void OBJ_NAME_cleanup(int type)
247 unsigned long down_load;
249 if (names_lh == NULL) return;
252 down_load=names_lh->down_load;
253 names_lh->down_load=0;
255 lh_doall(names_lh,names_lh_free);
259 sk_NAME_FUNCS_pop_free(name_funcs_stack,name_funcs_free);
261 name_funcs_stack = NULL;
264 names_lh->down_load=down_load;