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 int OBJ_NAME_init(void)
32 if (names_lh != NULL) return(1);
34 names_lh=lh_new((LHASH_HASH_FN_TYPE)obj_name_hash,
35 (LHASH_COMP_FN_TYPE)obj_name_cmp);
37 return(names_lh != NULL);
40 int OBJ_NAME_new_index(unsigned long (*hash_func)(const char *),
41 int (*cmp_func)(const char *, const char *),
42 void (*free_func)(const char *, int, const char *))
46 NAME_FUNCS *name_funcs;
48 if (name_funcs_stack == NULL)
51 name_funcs_stack=sk_NAME_FUNCS_new_null();
54 if ((name_funcs_stack == NULL))
61 for (i=sk_NAME_FUNCS_num(name_funcs_stack); i<names_type_num; i++)
64 name_funcs = OPENSSL_malloc(sizeof(NAME_FUNCS));
65 name_funcs->hash_func = lh_strhash;
66 name_funcs->cmp_func = strcmp;
67 name_funcs->free_func = 0; /* NULL is often declared to
68 * ((void *)0), which according
69 * to Compaq C is not really
70 * compatible with a function
71 * pointer. -- Richard Levitte*/
72 sk_NAME_FUNCS_push(name_funcs_stack,name_funcs);
75 name_funcs = sk_NAME_FUNCS_value(name_funcs_stack, ret);
76 if (hash_func != NULL)
77 name_funcs->hash_func = hash_func;
79 name_funcs->cmp_func = cmp_func;
80 if (free_func != NULL)
81 name_funcs->free_func = free_func;
85 static int obj_name_cmp(OBJ_NAME *a, OBJ_NAME *b)
92 if ((name_funcs_stack != NULL)
93 && (sk_NAME_FUNCS_num(name_funcs_stack) > a->type))
95 ret=sk_NAME_FUNCS_value(name_funcs_stack,a->type)
96 ->cmp_func(a->name,b->name);
99 ret=strcmp(a->name,b->name);
104 static unsigned long obj_name_hash(OBJ_NAME *a)
108 if ((name_funcs_stack != NULL) && (sk_NAME_FUNCS_num(name_funcs_stack) > a->type))
110 ret=sk_NAME_FUNCS_value(name_funcs_stack,a->type)
111 ->hash_func(a->name);
115 ret=lh_strhash(a->name);
121 const char *OBJ_NAME_get(const char *name, int type)
126 if (name == NULL) return(NULL);
127 if ((names_lh == NULL) && !OBJ_NAME_init()) return(NULL);
129 alias=type&OBJ_NAME_ALIAS;
130 type&= ~OBJ_NAME_ALIAS;
137 ret=(OBJ_NAME *)lh_retrieve(names_lh,&on);
138 if (ret == NULL) return(NULL);
139 if ((ret->alias) && !alias)
141 if (++num > 10) return(NULL);
151 int OBJ_NAME_add(const char *name, int type, const char *data)
156 if ((names_lh == NULL) && !OBJ_NAME_init()) return(0);
158 alias=type&OBJ_NAME_ALIAS;
159 type&= ~OBJ_NAME_ALIAS;
161 onp=(OBJ_NAME *)OPENSSL_malloc(sizeof(OBJ_NAME));
173 ret=(OBJ_NAME *)lh_insert(names_lh,onp);
177 if ((name_funcs_stack != NULL) && (sk_NAME_FUNCS_num(name_funcs_stack) > ret->type))
179 /* XXX: I'm not sure I understand why the free
180 * function should get three arguments...
183 sk_NAME_FUNCS_value(name_funcs_stack,ret->type)
184 ->free_func(ret->name,ret->type,ret->data);
190 if (lh_error(names_lh))
199 int OBJ_NAME_remove(const char *name, int type)
203 if (names_lh == NULL) return(0);
205 type&= ~OBJ_NAME_ALIAS;
208 ret=(OBJ_NAME *)lh_delete(names_lh,&on);
212 if ((name_funcs_stack != NULL) && (sk_NAME_FUNCS_num(name_funcs_stack) > ret->type))
214 /* XXX: I'm not sure I understand why the free
215 * function should get three arguments...
218 sk_NAME_FUNCS_value(name_funcs_stack,ret->type)
219 ->free_func(ret->name,ret->type,ret->data);
231 void (*fn)(const OBJ_NAME *,void *arg);
235 static void do_all_fn(const OBJ_NAME *name,struct doall *d)
237 if(name->type == d->type)
241 void OBJ_NAME_do_all(int type,void (*fn)(const OBJ_NAME *,void *arg),void *arg)
249 lh_doall_arg(names_lh,(LHASH_DOALL_ARG_FN_TYPE)do_all_fn,&d);
256 const OBJ_NAME **names;
259 static void do_all_sorted_fn(const OBJ_NAME *name,void *d_)
261 struct doall_sorted *d=d_;
263 if(name->type != d->type)
266 d->names[d->n++]=name;
269 static int do_all_sorted_cmp(const void *n1_,const void *n2_)
271 const OBJ_NAME * const *n1=n1_;
272 const OBJ_NAME * const *n2=n2_;
274 return strcmp((*n1)->name,(*n2)->name);
277 void OBJ_NAME_do_all_sorted(int type,void (*fn)(const OBJ_NAME *,void *arg),
280 struct doall_sorted d;
284 d.names=OPENSSL_malloc(lh_num_items(names_lh)*sizeof *d.names);
286 OBJ_NAME_do_all(type,do_all_sorted_fn,&d);
288 qsort(d.names,d.n,sizeof *d.names,do_all_sorted_cmp);
290 for(n=0 ; n < d.n ; ++n)
293 OPENSSL_free(d.names);
296 static int free_type;
298 static void names_lh_free(OBJ_NAME *onp, int type)
303 if ((free_type < 0) || (free_type == onp->type))
305 OBJ_NAME_remove(onp->name,onp->type);
309 static void name_funcs_free(NAME_FUNCS *ptr)
314 void OBJ_NAME_cleanup(int type)
316 unsigned long down_load;
318 if (names_lh == NULL) return;
321 down_load=names_lh->down_load;
322 names_lh->down_load=0;
324 lh_doall(names_lh,(LHASH_DOALL_FN_TYPE)names_lh_free);
328 sk_NAME_FUNCS_pop_free(name_funcs_stack,name_funcs_free);
330 name_funcs_stack = NULL;
333 names_lh->down_load=down_load;