5 #include <openssl/err.h>
6 #include <openssl/lhash.h>
7 #include <openssl/objects.h>
8 #include <openssl/safestack.h>
9 #include <openssl/e_os2.h>
11 /* Later versions of DEC C has started to add lnkage information to certain
12 * functions, which makes it tricky to use them as values to regular function
13 * pointers. One way is to define a macro that takes care of casting them
16 #ifdef OPENSSL_SYS_VMS_DECC
17 # define OPENSSL_strcmp (int (*)(const char *,const char *))strcmp
19 # define OPENSSL_strcmp strcmp
22 /* I use the ex_data stuff to manage the identifiers for the obj_name_types
23 * that applications may define. I only really use the free function field.
25 DECLARE_LHASH_OF(OBJ_NAME);
26 static LHASH_OF(OBJ_NAME) *names_lh=NULL;
27 static int names_type_num=OBJ_NAME_TYPE_NUM;
29 typedef struct name_funcs_st
31 unsigned long (*hash_func)(const char *name);
32 int (*cmp_func)(const char *a,const char *b);
33 void (*free_func)(const char *, int, const char *);
36 DECLARE_STACK_OF(NAME_FUNCS)
37 IMPLEMENT_STACK_OF(NAME_FUNCS)
39 static STACK_OF(NAME_FUNCS) *name_funcs_stack;
41 /* The LHASH callbacks now use the raw "void *" prototypes and do per-variable
42 * casting in the functions. This prevents function pointer casting without the
43 * need for macro-generated wrapper functions. */
45 /* static unsigned long obj_name_hash(OBJ_NAME *a); */
46 static unsigned long obj_name_hash(const void *a_void);
47 /* static int obj_name_cmp(OBJ_NAME *a,OBJ_NAME *b); */
48 static int obj_name_cmp(const void *a_void,const void *b_void);
50 static IMPLEMENT_LHASH_HASH_FN(obj_name, OBJ_NAME)
51 static IMPLEMENT_LHASH_COMP_FN(obj_name, OBJ_NAME)
53 int OBJ_NAME_init(void)
55 if (names_lh != NULL) return(1);
57 names_lh=lh_OBJ_NAME_new();
59 return(names_lh != NULL);
62 int OBJ_NAME_new_index(unsigned long (*hash_func)(const char *),
63 int (*cmp_func)(const char *, const char *),
64 void (*free_func)(const char *, int, const char *))
68 NAME_FUNCS *name_funcs;
70 if (name_funcs_stack == NULL)
73 name_funcs_stack=sk_NAME_FUNCS_new_null();
76 if ((name_funcs_stack == NULL))
83 for (i=sk_NAME_FUNCS_num(name_funcs_stack); i<names_type_num; i++)
86 name_funcs = OPENSSL_malloc(sizeof(NAME_FUNCS));
90 OBJerr(OBJ_F_OBJ_NAME_NEW_INDEX,ERR_R_MALLOC_FAILURE);
93 name_funcs->hash_func = lh_strhash;
94 name_funcs->cmp_func = OPENSSL_strcmp;
95 name_funcs->free_func = 0; /* NULL is often declared to
96 * ((void *)0), which according
97 * to Compaq C is not really
98 * compatible with a function
99 * pointer. -- Richard Levitte*/
101 sk_NAME_FUNCS_push(name_funcs_stack,name_funcs);
104 name_funcs = sk_NAME_FUNCS_value(name_funcs_stack, ret);
105 if (hash_func != NULL)
106 name_funcs->hash_func = hash_func;
107 if (cmp_func != NULL)
108 name_funcs->cmp_func = cmp_func;
109 if (free_func != NULL)
110 name_funcs->free_func = free_func;
114 /* static int obj_name_cmp(OBJ_NAME *a, OBJ_NAME *b) */
115 static int obj_name_cmp(const void *a_void, const void *b_void)
118 const OBJ_NAME *a = (const OBJ_NAME *)a_void;
119 const OBJ_NAME *b = (const OBJ_NAME *)b_void;
124 if ((name_funcs_stack != NULL)
125 && (sk_NAME_FUNCS_num(name_funcs_stack) > a->type))
127 ret=sk_NAME_FUNCS_value(name_funcs_stack,
128 a->type)->cmp_func(a->name,b->name);
131 ret=strcmp(a->name,b->name);
136 /* static unsigned long obj_name_hash(OBJ_NAME *a) */
137 static unsigned long obj_name_hash(const void *a_void)
140 const OBJ_NAME *a = (const OBJ_NAME *)a_void;
142 if ((name_funcs_stack != NULL) && (sk_NAME_FUNCS_num(name_funcs_stack) > a->type))
144 ret=sk_NAME_FUNCS_value(name_funcs_stack,
145 a->type)->hash_func(a->name);
149 ret=lh_strhash(a->name);
155 const char *OBJ_NAME_get(const char *name, int type)
160 if (name == NULL) return(NULL);
161 if ((names_lh == NULL) && !OBJ_NAME_init()) return(NULL);
163 alias=type&OBJ_NAME_ALIAS;
164 type&= ~OBJ_NAME_ALIAS;
171 ret=lh_OBJ_NAME_retrieve(names_lh,&on);
172 if (ret == NULL) return(NULL);
173 if ((ret->alias) && !alias)
175 if (++num > 10) return(NULL);
185 int OBJ_NAME_add(const char *name, int type, const char *data)
190 if ((names_lh == NULL) && !OBJ_NAME_init()) return(0);
192 alias=type&OBJ_NAME_ALIAS;
193 type&= ~OBJ_NAME_ALIAS;
195 onp=(OBJ_NAME *)OPENSSL_malloc(sizeof(OBJ_NAME));
207 ret=lh_OBJ_NAME_insert(names_lh,onp);
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,
218 ret->type)->free_func(ret->name,ret->type,ret->data);
224 if (lh_OBJ_NAME_error(names_lh))
233 int OBJ_NAME_remove(const char *name, int type)
237 if (names_lh == NULL) return(0);
239 type&= ~OBJ_NAME_ALIAS;
242 ret=lh_OBJ_NAME_delete(names_lh,&on);
246 if ((name_funcs_stack != NULL) && (sk_NAME_FUNCS_num(name_funcs_stack) > ret->type))
248 /* XXX: I'm not sure I understand why the free
249 * function should get three arguments...
252 sk_NAME_FUNCS_value(name_funcs_stack,
253 ret->type)->free_func(ret->name,ret->type,ret->data);
265 void (*fn)(const OBJ_NAME *,void *arg);
269 static void do_all_fn_doall_arg(const OBJ_NAME *name,struct doall *d)
271 if(name->type == d->type)
275 static IMPLEMENT_LHASH_DOALL_ARG_FN(do_all_fn, const OBJ_NAME, struct doall)
277 void OBJ_NAME_do_all(int type,void (*fn)(const OBJ_NAME *,void *arg),void *arg)
285 lh_OBJ_NAME_doall_arg(names_lh, LHASH_DOALL_ARG_FN(do_all_fn),
293 const OBJ_NAME **names;
296 static void do_all_sorted_fn(const OBJ_NAME *name,void *d_)
298 struct doall_sorted *d=d_;
300 if(name->type != d->type)
303 d->names[d->n++]=name;
306 static int do_all_sorted_cmp(const void *n1_,const void *n2_)
308 const OBJ_NAME * const *n1=n1_;
309 const OBJ_NAME * const *n2=n2_;
311 return strcmp((*n1)->name,(*n2)->name);
314 void OBJ_NAME_do_all_sorted(int type,void (*fn)(const OBJ_NAME *,void *arg),
317 struct doall_sorted d;
321 d.names=OPENSSL_malloc(lh_OBJ_NAME_num_items(names_lh)*sizeof *d.names);
323 OBJ_NAME_do_all(type,do_all_sorted_fn,&d);
325 qsort((void *)d.names,d.n,sizeof *d.names,do_all_sorted_cmp);
327 for(n=0 ; n < d.n ; ++n)
330 OPENSSL_free((void *)d.names);
333 static int free_type;
335 static void names_lh_free_doall(OBJ_NAME *onp)
340 if (free_type < 0 || free_type == onp->type)
341 OBJ_NAME_remove(onp->name,onp->type);
344 static IMPLEMENT_LHASH_DOALL_FN(names_lh_free, OBJ_NAME)
346 static void name_funcs_free(NAME_FUNCS *ptr)
351 void OBJ_NAME_cleanup(int type)
353 unsigned long down_load;
355 if (names_lh == NULL) return;
358 down_load=lh_OBJ_NAME_down_load(names_lh);
359 lh_OBJ_NAME_down_load(names_lh)=0;
361 lh_OBJ_NAME_doall(names_lh,LHASH_DOALL_FN(names_lh_free));
364 lh_OBJ_NAME_free(names_lh);
365 sk_NAME_FUNCS_pop_free(name_funcs_stack,name_funcs_free);
367 name_funcs_stack = NULL;
370 lh_OBJ_NAME_down_load(names_lh)=down_load;