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>
13 * Later versions of DEC C has started to add lnkage information to certain
14 * functions, which makes it tricky to use them as values to regular function
15 * pointers. One way is to define a macro that takes care of casting them
18 #ifdef OPENSSL_SYS_VMS_DECC
19 # define OPENSSL_strcmp (int (*)(const char *,const char *))strcmp
21 # define OPENSSL_strcmp strcmp
25 * I use the ex_data stuff to manage the identifiers for the obj_name_types
26 * that applications may define. I only really use the free function field.
28 DECLARE_LHASH_OF(OBJ_NAME);
29 static LHASH_OF(OBJ_NAME) *names_lh = NULL;
30 static int names_type_num = OBJ_NAME_TYPE_NUM;
32 struct name_funcs_st {
33 unsigned long (*hash_func) (const char *name);
34 int (*cmp_func) (const char *a, const char *b);
35 void (*free_func) (const char *, int, const char *);
38 static STACK_OF(NAME_FUNCS) *name_funcs_stack;
41 * The LHASH callbacks now use the raw "void *" prototypes and do
42 * per-variable casting in the functions. This prevents function pointer
43 * casting without the need for macro-generated wrapper functions.
46 /* static unsigned long obj_name_hash(OBJ_NAME *a); */
47 static unsigned long obj_name_hash(const void *a_void);
48 /* static int obj_name_cmp(OBJ_NAME *a,OBJ_NAME *b); */
49 static int obj_name_cmp(const void *a_void, const void *b_void);
51 static IMPLEMENT_LHASH_HASH_FN(obj_name, OBJ_NAME)
52 static IMPLEMENT_LHASH_COMP_FN(obj_name, OBJ_NAME)
54 int OBJ_NAME_init(void)
59 names_lh = lh_OBJ_NAME_new();
61 return (names_lh != NULL);
64 int OBJ_NAME_new_index(unsigned long (*hash_func) (const char *),
65 int (*cmp_func) (const char *, const char *),
66 void (*free_func) (const char *, int, const char *))
70 NAME_FUNCS *name_funcs;
72 if (name_funcs_stack == NULL) {
74 name_funcs_stack = sk_NAME_FUNCS_new_null();
77 if (name_funcs_stack == NULL) {
83 for (i = sk_NAME_FUNCS_num(name_funcs_stack); i < names_type_num; i++) {
85 name_funcs = OPENSSL_zalloc(sizeof(*name_funcs));
87 if (name_funcs == NULL) {
88 OBJerr(OBJ_F_OBJ_NAME_NEW_INDEX, ERR_R_MALLOC_FAILURE);
91 name_funcs->hash_func = lh_strhash;
92 name_funcs->cmp_func = OPENSSL_strcmp;
94 sk_NAME_FUNCS_push(name_funcs_stack, name_funcs);
97 name_funcs = sk_NAME_FUNCS_value(name_funcs_stack, ret);
98 if (hash_func != NULL)
99 name_funcs->hash_func = hash_func;
100 if (cmp_func != NULL)
101 name_funcs->cmp_func = cmp_func;
102 if (free_func != NULL)
103 name_funcs->free_func = free_func;
107 /* static int obj_name_cmp(OBJ_NAME *a, OBJ_NAME *b) */
108 static int obj_name_cmp(const void *a_void, const void *b_void)
111 const OBJ_NAME *a = (const OBJ_NAME *)a_void;
112 const OBJ_NAME *b = (const OBJ_NAME *)b_void;
114 ret = a->type - b->type;
116 if ((name_funcs_stack != NULL)
117 && (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);
121 ret = strcmp(a->name, b->name);
126 /* static unsigned long obj_name_hash(OBJ_NAME *a) */
127 static unsigned long obj_name_hash(const void *a_void)
130 const OBJ_NAME *a = (const OBJ_NAME *)a_void;
132 if ((name_funcs_stack != NULL)
133 && (sk_NAME_FUNCS_num(name_funcs_stack) > a->type)) {
135 sk_NAME_FUNCS_value(name_funcs_stack,
136 a->type)->hash_func(a->name);
138 ret = lh_strhash(a->name);
144 const char *OBJ_NAME_get(const char *name, int type)
151 if ((names_lh == NULL) && !OBJ_NAME_init())
154 alias = type & OBJ_NAME_ALIAS;
155 type &= ~OBJ_NAME_ALIAS;
161 ret = lh_OBJ_NAME_retrieve(names_lh, &on);
164 if ((ret->alias) && !alias) {
174 int OBJ_NAME_add(const char *name, int type, const char *data)
179 if ((names_lh == NULL) && !OBJ_NAME_init())
182 alias = type & OBJ_NAME_ALIAS;
183 type &= ~OBJ_NAME_ALIAS;
185 onp = OPENSSL_malloc(sizeof(*onp));
196 ret = lh_OBJ_NAME_insert(names_lh, onp);
199 if ((name_funcs_stack != NULL)
200 && (sk_NAME_FUNCS_num(name_funcs_stack) > ret->type)) {
202 * XXX: I'm not sure I understand why the free function should
203 * get three arguments... -- Richard Levitte
205 sk_NAME_FUNCS_value(name_funcs_stack,
206 ret->type)->free_func(ret->name, ret->type,
211 if (lh_OBJ_NAME_error(names_lh)) {
219 int OBJ_NAME_remove(const char *name, int type)
223 if (names_lh == NULL)
226 type &= ~OBJ_NAME_ALIAS;
229 ret = lh_OBJ_NAME_delete(names_lh, &on);
232 if ((name_funcs_stack != NULL)
233 && (sk_NAME_FUNCS_num(name_funcs_stack) > ret->type)) {
235 * XXX: I'm not sure I understand why the free function should
236 * get three arguments... -- Richard Levitte
238 sk_NAME_FUNCS_value(name_funcs_stack,
239 ret->type)->free_func(ret->name, ret->type,
250 void (*fn) (const OBJ_NAME *, void *arg);
254 static void do_all_fn_doall_arg(const OBJ_NAME *name, struct doall *d)
256 if (name->type == d->type)
260 static IMPLEMENT_LHASH_DOALL_ARG_FN(do_all_fn, const OBJ_NAME, struct doall)
262 void OBJ_NAME_do_all(int type, void (*fn) (const OBJ_NAME *, void *arg),
271 lh_OBJ_NAME_doall_arg(names_lh, LHASH_DOALL_ARG_FN(do_all_fn),
275 struct doall_sorted {
278 const OBJ_NAME **names;
281 static void do_all_sorted_fn(const OBJ_NAME *name, void *d_)
283 struct doall_sorted *d = d_;
285 if (name->type != d->type)
288 d->names[d->n++] = name;
291 static int do_all_sorted_cmp(const void *n1_, const void *n2_)
293 const OBJ_NAME *const *n1 = n1_;
294 const OBJ_NAME *const *n2 = n2_;
296 return strcmp((*n1)->name, (*n2)->name);
299 void OBJ_NAME_do_all_sorted(int type,
300 void (*fn) (const OBJ_NAME *, void *arg),
303 struct doall_sorted d;
308 OPENSSL_malloc(sizeof(*d.names) * lh_OBJ_NAME_num_items(names_lh));
309 /* Really should return an error if !d.names...but its a void function! */
310 if (d.names != NULL) {
312 OBJ_NAME_do_all(type, do_all_sorted_fn, &d);
314 qsort((void *)d.names, d.n, sizeof(*d.names), do_all_sorted_cmp);
316 for (n = 0; n < d.n; ++n)
319 OPENSSL_free((void *)d.names);
323 static int free_type;
325 static void names_lh_free_doall(OBJ_NAME *onp)
330 if (free_type < 0 || free_type == onp->type)
331 OBJ_NAME_remove(onp->name, onp->type);
334 static IMPLEMENT_LHASH_DOALL_FN(names_lh_free, OBJ_NAME)
336 static void name_funcs_free(NAME_FUNCS *ptr)
341 void OBJ_NAME_cleanup(int type)
343 unsigned long down_load;
345 if (names_lh == NULL)
349 down_load = lh_OBJ_NAME_down_load(names_lh);
350 lh_OBJ_NAME_down_load(names_lh) = 0;
352 lh_OBJ_NAME_doall(names_lh, LHASH_DOALL_FN(names_lh_free));
354 lh_OBJ_NAME_free(names_lh);
355 sk_NAME_FUNCS_pop_free(name_funcs_stack, name_funcs_free);
357 name_funcs_stack = NULL;
359 lh_OBJ_NAME_down_load(names_lh) = down_load;