5 #include <openssl/lhash.h>
6 #include <openssl/objects.h>
7 #include <openssl/safestack.h>
8 #include <openssl/e_os2.h>
10 /* Later versions of DEC C has started to add lnkage information to certain
11 * functions, which makes it tricky to use them as values to regular function
12 * pointers. One way is to define a macro that takes care of casting them
15 #ifdef OPENSSL_SYS_VMS_DECC
16 # define OPENSSL_strcmp (int (*)(const char *,const char *))strcmp
18 # define OPENSSL_strcmp strcmp
21 /* I use the ex_data stuff to manage the identifiers for the obj_name_types
22 * that applications may define. I only really use the free function field.
24 static LHASH *names_lh=NULL;
25 static int names_type_num=OBJ_NAME_TYPE_NUM;
27 typedef struct name_funcs_st
29 unsigned long (*hash_func)(const char *name);
30 int (*cmp_func)(const char *a,const char *b);
31 void (*free_func)(const char *, int, const char *);
34 DECLARE_STACK_OF(NAME_FUNCS)
35 IMPLEMENT_STACK_OF(NAME_FUNCS)
37 static STACK_OF(NAME_FUNCS) *name_funcs_stack;
39 /* The LHASH callbacks now use the raw "void *" prototypes and do per-variable
40 * casting in the functions. This prevents function pointer casting without the
41 * need for macro-generated wrapper functions. */
43 /* static unsigned long obj_name_hash(OBJ_NAME *a); */
44 static unsigned long obj_name_hash(const void *a_void);
45 /* static int obj_name_cmp(OBJ_NAME *a,OBJ_NAME *b); */
46 static int obj_name_cmp(const void *a_void,const void *b_void);
48 int OBJ_NAME_init(void)
50 if (names_lh != NULL) return(1);
52 names_lh=lh_new(obj_name_hash, obj_name_cmp);
54 return(names_lh != NULL);
57 int OBJ_NAME_new_index(unsigned long (*hash_func)(const char *),
58 int (*cmp_func)(const char *, const char *),
59 void (*free_func)(const char *, int, const char *))
63 NAME_FUNCS *name_funcs;
65 if (name_funcs_stack == NULL)
68 name_funcs_stack=sk_NAME_FUNCS_new_null();
71 if ((name_funcs_stack == NULL))
78 for (i=sk_NAME_FUNCS_num(name_funcs_stack); i<names_type_num; i++)
81 name_funcs = OPENSSL_malloc(sizeof(NAME_FUNCS));
82 name_funcs->hash_func = lh_strhash;
83 name_funcs->cmp_func = OPENSSL_strcmp;
84 name_funcs->free_func = 0; /* NULL is often declared to
85 * ((void *)0), which according
86 * to Compaq C is not really
87 * compatible with a function
88 * pointer. -- Richard Levitte*/
89 sk_NAME_FUNCS_push(name_funcs_stack,name_funcs);
92 name_funcs = sk_NAME_FUNCS_value(name_funcs_stack, ret);
93 if (hash_func != NULL)
94 name_funcs->hash_func = hash_func;
96 name_funcs->cmp_func = cmp_func;
97 if (free_func != NULL)
98 name_funcs->free_func = free_func;
102 /* static int obj_name_cmp(OBJ_NAME *a, OBJ_NAME *b) */
103 static int obj_name_cmp(const void *a_void, const void *b_void)
106 OBJ_NAME *a = (OBJ_NAME *)a_void;
107 OBJ_NAME *b = (OBJ_NAME *)b_void;
112 if ((name_funcs_stack != NULL)
113 && (sk_NAME_FUNCS_num(name_funcs_stack) > a->type))
115 ret=sk_NAME_FUNCS_value(name_funcs_stack,
116 a->type)->cmp_func(a->name,b->name);
119 ret=strcmp(a->name,b->name);
124 /* static unsigned long obj_name_hash(OBJ_NAME *a) */
125 static unsigned long obj_name_hash(const void *a_void)
128 OBJ_NAME *a = (OBJ_NAME *)a_void;
130 if ((name_funcs_stack != NULL) && (sk_NAME_FUNCS_num(name_funcs_stack) > a->type))
132 ret=sk_NAME_FUNCS_value(name_funcs_stack,
133 a->type)->hash_func(a->name);
137 ret=lh_strhash(a->name);
143 const char *OBJ_NAME_get(const char *name, int type)
148 if (name == NULL) return(NULL);
149 if ((names_lh == NULL) && !OBJ_NAME_init()) return(NULL);
151 alias=type&OBJ_NAME_ALIAS;
152 type&= ~OBJ_NAME_ALIAS;
159 ret=(OBJ_NAME *)lh_retrieve(names_lh,&on);
160 if (ret == NULL) return(NULL);
161 if ((ret->alias) && !alias)
163 if (++num > 10) return(NULL);
173 int OBJ_NAME_add(const char *name, int type, const char *data)
178 if ((names_lh == NULL) && !OBJ_NAME_init()) return(0);
180 alias=type&OBJ_NAME_ALIAS;
181 type&= ~OBJ_NAME_ALIAS;
183 onp=(OBJ_NAME *)OPENSSL_malloc(sizeof(OBJ_NAME));
195 ret=(OBJ_NAME *)lh_insert(names_lh,onp);
199 if ((name_funcs_stack != NULL) && (sk_NAME_FUNCS_num(name_funcs_stack) > ret->type))
201 /* XXX: I'm not sure I understand why the free
202 * function should get three arguments...
205 sk_NAME_FUNCS_value(name_funcs_stack,
206 ret->type)->free_func(ret->name,ret->type,ret->data);
212 if (lh_error(names_lh))
221 int OBJ_NAME_remove(const char *name, int type)
225 if (names_lh == NULL) return(0);
227 type&= ~OBJ_NAME_ALIAS;
230 ret=(OBJ_NAME *)lh_delete(names_lh,&on);
234 if ((name_funcs_stack != NULL) && (sk_NAME_FUNCS_num(name_funcs_stack) > ret->type))
236 /* XXX: I'm not sure I understand why the free
237 * function should get three arguments...
240 sk_NAME_FUNCS_value(name_funcs_stack,
241 ret->type)->free_func(ret->name,ret->type,ret->data);
253 void (*fn)(const OBJ_NAME *,void *arg);
257 static void do_all_fn(const OBJ_NAME *name,struct doall *d)
259 if(name->type == d->type)
263 static IMPLEMENT_LHASH_DOALL_ARG_FN(do_all_fn, const OBJ_NAME *, struct doall *)
265 void OBJ_NAME_do_all(int type,void (*fn)(const OBJ_NAME *,void *arg),void *arg)
273 lh_doall_arg(names_lh,LHASH_DOALL_ARG_FN(do_all_fn),&d);
280 const OBJ_NAME **names;
283 static void do_all_sorted_fn(const OBJ_NAME *name,void *d_)
285 struct doall_sorted *d=d_;
287 if(name->type != d->type)
290 d->names[d->n++]=name;
293 static int do_all_sorted_cmp(const void *n1_,const void *n2_)
295 const OBJ_NAME * const *n1=n1_;
296 const OBJ_NAME * const *n2=n2_;
298 return strcmp((*n1)->name,(*n2)->name);
301 void OBJ_NAME_do_all_sorted(int type,void (*fn)(const OBJ_NAME *,void *arg),
304 struct doall_sorted d;
308 d.names=OPENSSL_malloc(lh_num_items(names_lh)*sizeof *d.names);
310 OBJ_NAME_do_all(type,do_all_sorted_fn,&d);
312 qsort((void *)d.names,d.n,sizeof *d.names,do_all_sorted_cmp);
314 for(n=0 ; n < d.n ; ++n)
317 OPENSSL_free((void *)d.names);
320 static int free_type;
322 static void names_lh_free(OBJ_NAME *onp)
327 if ((free_type < 0) || (free_type == onp->type))
329 OBJ_NAME_remove(onp->name,onp->type);
333 static IMPLEMENT_LHASH_DOALL_FN(names_lh_free, OBJ_NAME *)
335 static void name_funcs_free(NAME_FUNCS *ptr)
340 void OBJ_NAME_cleanup(int type)
342 unsigned long down_load;
344 if (names_lh == NULL) return;
347 down_load=names_lh->down_load;
348 names_lh->down_load=0;
350 lh_doall(names_lh,LHASH_DOALL_FN(names_lh_free));
354 sk_NAME_FUNCS_pop_free(name_funcs_stack,name_funcs_free);
356 name_funcs_stack = NULL;
359 names_lh->down_load=down_load;