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>
12 * Later versions of DEC C has started to add lnkage information to certain
13 * functions, which makes it tricky to use them as values to regular function
14 * pointers. One way is to define a macro that takes care of casting them
17 #ifdef OPENSSL_SYS_VMS_DECC
18 # define OPENSSL_strcmp (int (*)(const char *,const char *))strcmp
20 # define OPENSSL_strcmp strcmp
24 * I use the ex_data stuff to manage the identifiers for the obj_name_types
25 * that applications may define. I only really use the free function field.
27 static LHASH *names_lh = NULL;
28 static int names_type_num = OBJ_NAME_TYPE_NUM;
30 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;
42 * The LHASH callbacks now use the raw "void *" prototypes and do
43 * per-variable casting in the functions. This prevents function pointer
44 * casting without the need for macro-generated wrapper functions.
47 /* static unsigned long obj_name_hash(OBJ_NAME *a); */
48 static unsigned long obj_name_hash(const void *a_void);
49 /* static int obj_name_cmp(OBJ_NAME *a,OBJ_NAME *b); */
50 static int obj_name_cmp(const void *a_void, const void *b_void);
52 int OBJ_NAME_init(void)
57 names_lh = lh_new(obj_name_hash, obj_name_cmp);
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) {
72 name_funcs_stack = sk_NAME_FUNCS_new_null();
75 if ((name_funcs_stack == NULL)) {
81 for (i = sk_NAME_FUNCS_num(name_funcs_stack); i < names_type_num; i++) {
83 name_funcs = OPENSSL_malloc(sizeof(NAME_FUNCS));
86 OBJerr(OBJ_F_OBJ_NAME_NEW_INDEX, ERR_R_MALLOC_FAILURE);
89 name_funcs->hash_func = lh_strhash;
90 name_funcs->cmp_func = OPENSSL_strcmp;
91 name_funcs->free_func = 0; /* NULL is often declared to * ((void
92 * *)0), which according * to Compaq C is
93 * not really * compatible with a function
94 * * pointer. -- Richard Levitte */
96 sk_NAME_FUNCS_push(name_funcs_stack, name_funcs);
99 name_funcs = sk_NAME_FUNCS_value(name_funcs_stack, ret);
100 if (hash_func != NULL)
101 name_funcs->hash_func = hash_func;
102 if (cmp_func != NULL)
103 name_funcs->cmp_func = cmp_func;
104 if (free_func != NULL)
105 name_funcs->free_func = free_func;
109 /* static int obj_name_cmp(OBJ_NAME *a, OBJ_NAME *b) */
110 static int obj_name_cmp(const void *a_void, const void *b_void)
113 const OBJ_NAME *a = (const OBJ_NAME *)a_void;
114 const OBJ_NAME *b = (const OBJ_NAME *)b_void;
116 ret = a->type - b->type;
118 if ((name_funcs_stack != NULL)
119 && (sk_NAME_FUNCS_num(name_funcs_stack) > a->type)) {
120 ret = sk_NAME_FUNCS_value(name_funcs_stack,
121 a->type)->cmp_func(a->name, b->name);
123 ret = strcmp(a->name, b->name);
128 /* static unsigned long obj_name_hash(OBJ_NAME *a) */
129 static unsigned long obj_name_hash(const void *a_void)
132 const OBJ_NAME *a = (const OBJ_NAME *)a_void;
134 if ((name_funcs_stack != NULL)
135 && (sk_NAME_FUNCS_num(name_funcs_stack) > a->type)) {
137 sk_NAME_FUNCS_value(name_funcs_stack,
138 a->type)->hash_func(a->name);
140 ret = lh_strhash(a->name);
146 const char *OBJ_NAME_get(const char *name, int type)
153 if ((names_lh == NULL) && !OBJ_NAME_init())
156 alias = type & OBJ_NAME_ALIAS;
157 type &= ~OBJ_NAME_ALIAS;
163 ret = (OBJ_NAME *)lh_retrieve(names_lh, &on);
166 if ((ret->alias) && !alias) {
176 int OBJ_NAME_add(const char *name, int type, const char *data)
181 if ((names_lh == NULL) && !OBJ_NAME_init())
184 alias = type & OBJ_NAME_ALIAS;
185 type &= ~OBJ_NAME_ALIAS;
187 onp = (OBJ_NAME *)OPENSSL_malloc(sizeof(OBJ_NAME));
198 ret = (OBJ_NAME *)lh_insert(names_lh, onp);
201 if ((name_funcs_stack != NULL)
202 && (sk_NAME_FUNCS_num(name_funcs_stack) > ret->type)) {
204 * XXX: I'm not sure I understand why the free function should
205 * get three arguments... -- Richard Levitte
207 sk_NAME_FUNCS_value(name_funcs_stack,
208 ret->type)->free_func(ret->name, ret->type,
213 if (lh_error(names_lh)) {
221 int OBJ_NAME_remove(const char *name, int type)
225 if (names_lh == NULL)
228 type &= ~OBJ_NAME_ALIAS;
231 ret = (OBJ_NAME *)lh_delete(names_lh, &on);
234 if ((name_funcs_stack != NULL)
235 && (sk_NAME_FUNCS_num(name_funcs_stack) > ret->type)) {
237 * XXX: I'm not sure I understand why the free function should
238 * get three arguments... -- Richard Levitte
240 sk_NAME_FUNCS_value(name_funcs_stack,
241 ret->type)->free_func(ret->name, ret->type,
252 void (*fn) (const OBJ_NAME *, void *arg);
256 static void do_all_fn(const OBJ_NAME *name, struct doall *d)
258 if (name->type == d->type)
262 static IMPLEMENT_LHASH_DOALL_ARG_FN(do_all_fn, const OBJ_NAME *,
265 void OBJ_NAME_do_all(int type, void (*fn) (const OBJ_NAME *, void *arg),
274 lh_doall_arg(names_lh, LHASH_DOALL_ARG_FN(do_all_fn), &d);
277 struct doall_sorted {
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,
302 void (*fn) (const OBJ_NAME *, void *arg),
305 struct doall_sorted d;
309 d.names = OPENSSL_malloc(lh_num_items(names_lh) * sizeof *d.names);
311 OBJ_NAME_do_all(type, do_all_sorted_fn, &d);
313 qsort((void *)d.names, d.n, sizeof *d.names, do_all_sorted_cmp);
315 for (n = 0; n < d.n; ++n)
318 OPENSSL_free((void *)d.names);
321 static int free_type;
323 static void names_lh_free(OBJ_NAME *onp)
328 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)
348 down_load = names_lh->down_load;
349 names_lh->down_load = 0;
351 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;
358 names_lh->down_load = down_load;