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 DECLARE_LHASH_OF(OBJ_NAME);
28 static LHASH_OF(OBJ_NAME) *names_lh = NULL;
29 static int names_type_num = OBJ_NAME_TYPE_NUM;
31 typedef struct name_funcs_st {
32 unsigned long (*hash_func) (const char *name);
33 int (*cmp_func) (const char *a, const char *b);
34 void (*free_func) (const char *, int, const char *);
37 DEFINE_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 static IMPLEMENT_LHASH_HASH_FN(obj_name, OBJ_NAME)
53 static IMPLEMENT_LHASH_COMP_FN(obj_name, OBJ_NAME)
55 int OBJ_NAME_init(void)
60 names_lh = lh_OBJ_NAME_new();
62 return (names_lh != NULL);
65 int OBJ_NAME_new_index(unsigned long (*hash_func) (const char *),
66 int (*cmp_func) (const char *, const char *),
67 void (*free_func) (const char *, int, const char *))
71 NAME_FUNCS *name_funcs;
73 if (name_funcs_stack == NULL) {
75 name_funcs_stack = sk_NAME_FUNCS_new_null();
78 if (name_funcs_stack == NULL) {
84 for (i = sk_NAME_FUNCS_num(name_funcs_stack); i < names_type_num; i++) {
86 name_funcs = OPENSSL_zalloc(sizeof(*name_funcs));
88 if (name_funcs == NULL) {
89 OBJerr(OBJ_F_OBJ_NAME_NEW_INDEX, ERR_R_MALLOC_FAILURE);
92 name_funcs->hash_func = lh_strhash;
93 name_funcs->cmp_func = OPENSSL_strcmp;
95 sk_NAME_FUNCS_push(name_funcs_stack, name_funcs);
98 name_funcs = sk_NAME_FUNCS_value(name_funcs_stack, ret);
99 if (hash_func != NULL)
100 name_funcs->hash_func = hash_func;
101 if (cmp_func != NULL)
102 name_funcs->cmp_func = cmp_func;
103 if (free_func != NULL)
104 name_funcs->free_func = free_func;
108 /* static int obj_name_cmp(OBJ_NAME *a, OBJ_NAME *b) */
109 static int obj_name_cmp(const void *a_void, const void *b_void)
112 const OBJ_NAME *a = (const OBJ_NAME *)a_void;
113 const OBJ_NAME *b = (const OBJ_NAME *)b_void;
115 ret = a->type - b->type;
117 if ((name_funcs_stack != NULL)
118 && (sk_NAME_FUNCS_num(name_funcs_stack) > a->type)) {
119 ret = sk_NAME_FUNCS_value(name_funcs_stack,
120 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 const OBJ_NAME *a = (const OBJ_NAME *)a_void;
133 if ((name_funcs_stack != NULL)
134 && (sk_NAME_FUNCS_num(name_funcs_stack) > a->type)) {
136 sk_NAME_FUNCS_value(name_funcs_stack,
137 a->type)->hash_func(a->name);
139 ret = lh_strhash(a->name);
145 const char *OBJ_NAME_get(const char *name, int type)
152 if ((names_lh == NULL) && !OBJ_NAME_init())
155 alias = type & OBJ_NAME_ALIAS;
156 type &= ~OBJ_NAME_ALIAS;
162 ret = lh_OBJ_NAME_retrieve(names_lh, &on);
165 if ((ret->alias) && !alias) {
175 int OBJ_NAME_add(const char *name, int type, const char *data)
180 if ((names_lh == NULL) && !OBJ_NAME_init())
183 alias = type & OBJ_NAME_ALIAS;
184 type &= ~OBJ_NAME_ALIAS;
186 onp = OPENSSL_malloc(sizeof(*onp));
197 ret = lh_OBJ_NAME_insert(names_lh, onp);
200 if ((name_funcs_stack != NULL)
201 && (sk_NAME_FUNCS_num(name_funcs_stack) > ret->type)) {
203 * XXX: I'm not sure I understand why the free function should
204 * get three arguments... -- Richard Levitte
206 sk_NAME_FUNCS_value(name_funcs_stack,
207 ret->type)->free_func(ret->name, ret->type,
212 if (lh_OBJ_NAME_error(names_lh)) {
220 int OBJ_NAME_remove(const char *name, int type)
224 if (names_lh == NULL)
227 type &= ~OBJ_NAME_ALIAS;
230 ret = lh_OBJ_NAME_delete(names_lh, &on);
233 if ((name_funcs_stack != NULL)
234 && (sk_NAME_FUNCS_num(name_funcs_stack) > ret->type)) {
236 * XXX: I'm not sure I understand why the free function should
237 * get three arguments... -- Richard Levitte
239 sk_NAME_FUNCS_value(name_funcs_stack,
240 ret->type)->free_func(ret->name, ret->type,
251 void (*fn) (const OBJ_NAME *, void *arg);
255 static void do_all_fn_doall_arg(const OBJ_NAME *name, struct doall *d)
257 if (name->type == d->type)
261 static IMPLEMENT_LHASH_DOALL_ARG_FN(do_all_fn, const OBJ_NAME, struct doall)
263 void OBJ_NAME_do_all(int type, void (*fn) (const OBJ_NAME *, void *arg),
272 lh_OBJ_NAME_doall_arg(names_lh, LHASH_DOALL_ARG_FN(do_all_fn),
276 struct doall_sorted {
279 const OBJ_NAME **names;
282 static void do_all_sorted_fn(const OBJ_NAME *name, void *d_)
284 struct doall_sorted *d = d_;
286 if (name->type != d->type)
289 d->names[d->n++] = name;
292 static int do_all_sorted_cmp(const void *n1_, const void *n2_)
294 const OBJ_NAME *const *n1 = n1_;
295 const OBJ_NAME *const *n2 = n2_;
297 return strcmp((*n1)->name, (*n2)->name);
300 void OBJ_NAME_do_all_sorted(int type,
301 void (*fn) (const OBJ_NAME *, void *arg),
304 struct doall_sorted d;
309 OPENSSL_malloc(sizeof(*d.names) * lh_OBJ_NAME_num_items(names_lh));
310 /* Really should return an error if !d.names...but its a void function! */
311 if (d.names != NULL) {
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);
324 static int free_type;
326 static void names_lh_free_doall(OBJ_NAME *onp)
331 if (free_type < 0 || free_type == onp->type)
332 OBJ_NAME_remove(onp->name, onp->type);
335 static IMPLEMENT_LHASH_DOALL_FN(names_lh_free, OBJ_NAME)
337 static void name_funcs_free(NAME_FUNCS *ptr)
342 void OBJ_NAME_cleanup(int type)
344 unsigned long down_load;
346 if (names_lh == NULL)
350 down_load = lh_OBJ_NAME_down_load(names_lh);
351 lh_OBJ_NAME_down_load(names_lh) = 0;
353 lh_OBJ_NAME_doall(names_lh, LHASH_DOALL_FN(names_lh_free));
355 lh_OBJ_NAME_free(names_lh);
356 sk_NAME_FUNCS_pop_free(name_funcs_stack, name_funcs_free);
358 name_funcs_stack = NULL;
360 lh_OBJ_NAME_down_load(names_lh) = down_load;