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));
83 if (!name_funcs) return(0);
84 name_funcs->hash_func = lh_strhash;
85 name_funcs->cmp_func = OPENSSL_strcmp;
86 name_funcs->free_func = 0; /* NULL is often declared to
87 * ((void *)0), which according
88 * to Compaq C is not really
89 * compatible with a function
90 * pointer. -- Richard Levitte*/
92 sk_NAME_FUNCS_push(name_funcs_stack,name_funcs);
95 name_funcs = sk_NAME_FUNCS_value(name_funcs_stack, ret);
96 if (hash_func != NULL)
97 name_funcs->hash_func = hash_func;
99 name_funcs->cmp_func = cmp_func;
100 if (free_func != NULL)
101 name_funcs->free_func = free_func;
105 /* static int obj_name_cmp(OBJ_NAME *a, OBJ_NAME *b) */
106 static int obj_name_cmp(const void *a_void, const void *b_void)
109 OBJ_NAME *a = (OBJ_NAME *)a_void;
110 OBJ_NAME *b = (OBJ_NAME *)b_void;
115 if ((name_funcs_stack != NULL)
116 && (sk_NAME_FUNCS_num(name_funcs_stack) > a->type))
118 ret=sk_NAME_FUNCS_value(name_funcs_stack,
119 a->type)->cmp_func(a->name,b->name);
122 ret=strcmp(a->name,b->name);
127 /* static unsigned long obj_name_hash(OBJ_NAME *a) */
128 static unsigned long obj_name_hash(const void *a_void)
131 OBJ_NAME *a = (OBJ_NAME *)a_void;
133 if ((name_funcs_stack != NULL) && (sk_NAME_FUNCS_num(name_funcs_stack) > a->type))
135 ret=sk_NAME_FUNCS_value(name_funcs_stack,
136 a->type)->hash_func(a->name);
140 ret=lh_strhash(a->name);
146 const char *OBJ_NAME_get(const char *name, int type)
151 if (name == NULL) return(NULL);
152 if ((names_lh == NULL) && !OBJ_NAME_init()) return(NULL);
154 alias=type&OBJ_NAME_ALIAS;
155 type&= ~OBJ_NAME_ALIAS;
162 ret=(OBJ_NAME *)lh_retrieve(names_lh,&on);
163 if (ret == NULL) return(NULL);
164 if ((ret->alias) && !alias)
166 if (++num > 10) return(NULL);
176 int OBJ_NAME_add(const char *name, int type, const char *data)
181 if ((names_lh == NULL) && !OBJ_NAME_init()) return(0);
183 alias=type&OBJ_NAME_ALIAS;
184 type&= ~OBJ_NAME_ALIAS;
186 onp=(OBJ_NAME *)OPENSSL_malloc(sizeof(OBJ_NAME));
198 ret=(OBJ_NAME *)lh_insert(names_lh,onp);
202 if ((name_funcs_stack != NULL) && (sk_NAME_FUNCS_num(name_funcs_stack) > ret->type))
204 /* XXX: I'm not sure I understand why the free
205 * function should get three arguments...
208 sk_NAME_FUNCS_value(name_funcs_stack,
209 ret->type)->free_func(ret->name,ret->type,ret->data);
215 if (lh_error(names_lh))
224 int OBJ_NAME_remove(const char *name, int type)
228 if (names_lh == NULL) return(0);
230 type&= ~OBJ_NAME_ALIAS;
233 ret=(OBJ_NAME *)lh_delete(names_lh,&on);
237 if ((name_funcs_stack != NULL) && (sk_NAME_FUNCS_num(name_funcs_stack) > ret->type))
239 /* XXX: I'm not sure I understand why the free
240 * function should get three arguments...
243 sk_NAME_FUNCS_value(name_funcs_stack,
244 ret->type)->free_func(ret->name,ret->type,ret->data);
256 void (*fn)(const OBJ_NAME *,void *arg);
260 static void do_all_fn(const OBJ_NAME *name,struct doall *d)
262 if(name->type == d->type)
266 static IMPLEMENT_LHASH_DOALL_ARG_FN(do_all_fn, const OBJ_NAME *, struct doall *)
268 void OBJ_NAME_do_all(int type,void (*fn)(const OBJ_NAME *,void *arg),void *arg)
276 lh_doall_arg(names_lh,LHASH_DOALL_ARG_FN(do_all_fn),&d);
283 const OBJ_NAME **names;
286 static void do_all_sorted_fn(const OBJ_NAME *name,void *d_)
288 struct doall_sorted *d=d_;
290 if(name->type != d->type)
293 d->names[d->n++]=name;
296 static int do_all_sorted_cmp(const void *n1_,const void *n2_)
298 const OBJ_NAME * const *n1=n1_;
299 const OBJ_NAME * const *n2=n2_;
301 return strcmp((*n1)->name,(*n2)->name);
304 void OBJ_NAME_do_all_sorted(int type,void (*fn)(const OBJ_NAME *,void *arg),
307 struct doall_sorted d;
311 d.names=OPENSSL_malloc(lh_num_items(names_lh)*sizeof *d.names);
313 OBJ_NAME_do_all(type,do_all_sorted_fn,&d);
315 qsort((void *)d.names,d.n,sizeof *d.names,do_all_sorted_cmp);
317 for(n=0 ; n < d.n ; ++n)
320 OPENSSL_free((void *)d.names);
323 static int free_type;
325 static void names_lh_free(OBJ_NAME *onp)
330 if ((free_type < 0) || (free_type == onp->type))
332 OBJ_NAME_remove(onp->name,onp->type);
336 static IMPLEMENT_LHASH_DOALL_FN(names_lh_free, OBJ_NAME *)
338 static void name_funcs_free(NAME_FUNCS *ptr)
343 void OBJ_NAME_cleanup(int type)
345 unsigned long down_load;
347 if (names_lh == NULL) return;
350 down_load=names_lh->down_load;
351 names_lh->down_load=0;
353 lh_doall(names_lh,LHASH_DOALL_FN(names_lh_free));
357 sk_NAME_FUNCS_pop_free(name_funcs_stack,name_funcs_free);
359 name_funcs_stack = NULL;
362 names_lh->down_load=down_load;