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)(const char *name);
18 int (*cmp_func)(const char *a,const char *b);
19 void (*free_func)(const char *, int, const char *);
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 static IMPLEMENT_LHASH_HASH_FN(obj_name_hash, OBJ_NAME *)
31 static IMPLEMENT_LHASH_COMP_FN(obj_name_cmp, OBJ_NAME *)
33 int OBJ_NAME_init(void)
35 if (names_lh != NULL) return(1);
37 names_lh=lh_new(LHASH_HASH_FN(obj_name_hash),
38 LHASH_COMP_FN(obj_name_cmp));
40 return(names_lh != NULL);
43 int OBJ_NAME_new_index(unsigned long (*hash_func)(const char *),
44 int (*cmp_func)(const char *, const char *),
45 void (*free_func)(const char *, int, const char *))
49 NAME_FUNCS *name_funcs;
51 if (name_funcs_stack == NULL)
54 name_funcs_stack=sk_NAME_FUNCS_new_null();
57 if ((name_funcs_stack == NULL))
64 for (i=sk_NAME_FUNCS_num(name_funcs_stack); i<names_type_num; i++)
67 name_funcs = OPENSSL_malloc(sizeof(NAME_FUNCS));
68 name_funcs->hash_func = lh_strhash;
69 name_funcs->cmp_func = strcmp;
70 name_funcs->free_func = 0; /* NULL is often declared to
71 * ((void *)0), which according
72 * to Compaq C is not really
73 * compatible with a function
74 * pointer. -- Richard Levitte*/
75 sk_NAME_FUNCS_push(name_funcs_stack,name_funcs);
78 name_funcs = sk_NAME_FUNCS_value(name_funcs_stack, ret);
79 if (hash_func != NULL)
80 name_funcs->hash_func = hash_func;
82 name_funcs->cmp_func = cmp_func;
83 if (free_func != NULL)
84 name_funcs->free_func = free_func;
88 static int obj_name_cmp(OBJ_NAME *a, OBJ_NAME *b)
95 if ((name_funcs_stack != NULL)
96 && (sk_NAME_FUNCS_num(name_funcs_stack) > a->type))
98 ret=sk_NAME_FUNCS_value(name_funcs_stack,a->type)
99 ->cmp_func(a->name,b->name);
102 ret=strcmp(a->name,b->name);
107 static unsigned long obj_name_hash(OBJ_NAME *a)
111 if ((name_funcs_stack != NULL) && (sk_NAME_FUNCS_num(name_funcs_stack) > a->type))
113 ret=sk_NAME_FUNCS_value(name_funcs_stack,a->type)
114 ->hash_func(a->name);
118 ret=lh_strhash(a->name);
124 const char *OBJ_NAME_get(const char *name, int type)
129 if (name == NULL) return(NULL);
130 if ((names_lh == NULL) && !OBJ_NAME_init()) return(NULL);
132 alias=type&OBJ_NAME_ALIAS;
133 type&= ~OBJ_NAME_ALIAS;
140 ret=(OBJ_NAME *)lh_retrieve(names_lh,&on);
141 if (ret == NULL) return(NULL);
142 if ((ret->alias) && !alias)
144 if (++num > 10) return(NULL);
154 int OBJ_NAME_add(const char *name, int type, const char *data)
159 if ((names_lh == NULL) && !OBJ_NAME_init()) return(0);
161 alias=type&OBJ_NAME_ALIAS;
162 type&= ~OBJ_NAME_ALIAS;
164 onp=(OBJ_NAME *)OPENSSL_malloc(sizeof(OBJ_NAME));
176 ret=(OBJ_NAME *)lh_insert(names_lh,onp);
180 if ((name_funcs_stack != NULL) && (sk_NAME_FUNCS_num(name_funcs_stack) > ret->type))
182 /* XXX: I'm not sure I understand why the free
183 * function should get three arguments...
186 sk_NAME_FUNCS_value(name_funcs_stack,ret->type)
187 ->free_func(ret->name,ret->type,ret->data);
193 if (lh_error(names_lh))
202 int OBJ_NAME_remove(const char *name, int type)
206 if (names_lh == NULL) return(0);
208 type&= ~OBJ_NAME_ALIAS;
211 ret=(OBJ_NAME *)lh_delete(names_lh,&on);
215 if ((name_funcs_stack != NULL) && (sk_NAME_FUNCS_num(name_funcs_stack) > ret->type))
217 /* XXX: I'm not sure I understand why the free
218 * function should get three arguments...
221 sk_NAME_FUNCS_value(name_funcs_stack,ret->type)
222 ->free_func(ret->name,ret->type,ret->data);
234 void (*fn)(const OBJ_NAME *,void *arg);
238 static void do_all_fn(const OBJ_NAME *name,struct doall *d)
240 if(name->type == d->type)
244 void OBJ_NAME_do_all(int type,void (*fn)(const OBJ_NAME *,void *arg),void *arg)
252 lh_doall_arg(names_lh,(LHASH_DOALL_ARG_FN_TYPE)do_all_fn,&d);
259 const OBJ_NAME **names;
262 static void do_all_sorted_fn(const OBJ_NAME *name,void *d_)
264 struct doall_sorted *d=d_;
266 if(name->type != d->type)
269 d->names[d->n++]=name;
272 static int do_all_sorted_cmp(const void *n1_,const void *n2_)
274 const OBJ_NAME * const *n1=n1_;
275 const OBJ_NAME * const *n2=n2_;
277 return strcmp((*n1)->name,(*n2)->name);
280 void OBJ_NAME_do_all_sorted(int type,void (*fn)(const OBJ_NAME *,void *arg),
283 struct doall_sorted d;
287 d.names=OPENSSL_malloc(lh_num_items(names_lh)*sizeof *d.names);
289 OBJ_NAME_do_all(type,do_all_sorted_fn,&d);
291 qsort(d.names,d.n,sizeof *d.names,do_all_sorted_cmp);
293 for(n=0 ; n < d.n ; ++n)
296 OPENSSL_free(d.names);
299 static int free_type;
301 static void names_lh_free(OBJ_NAME *onp, int type)
306 if ((free_type < 0) || (free_type == onp->type))
308 OBJ_NAME_remove(onp->name,onp->type);
312 static void name_funcs_free(NAME_FUNCS *ptr)
317 void OBJ_NAME_cleanup(int type)
319 unsigned long down_load;
321 if (names_lh == NULL) return;
324 down_load=names_lh->down_load;
325 names_lh->down_load=0;
327 lh_doall(names_lh,(LHASH_DOALL_FN_TYPE)names_lh_free);
331 sk_NAME_FUNCS_pop_free(name_funcs_stack,name_funcs_free);
333 name_funcs_stack = NULL;
336 names_lh->down_load=down_load;