]>
git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/objects/o_names.c
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 static LHASH_OF(OBJ_NAME
) *names_lh
= NULL
;
29 static int names_type_num
= OBJ_NAME_TYPE_NUM
;
31 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 static STACK_OF(NAME_FUNCS
) *name_funcs_stack
;
40 * The LHASH callbacks now use the raw "void *" prototypes and do
41 * per-variable casting in the functions. This prevents function pointer
42 * casting without the need for macro-generated wrapper functions.
45 static unsigned long obj_name_hash(const OBJ_NAME
*a
);
46 static int obj_name_cmp(const OBJ_NAME
*a
, const OBJ_NAME
*b
);
48 int OBJ_NAME_init(void)
52 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE
);
53 names_lh
= lh_OBJ_NAME_new(obj_name_hash
, obj_name_cmp
);
54 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE
);
55 return (names_lh
!= NULL
);
58 int OBJ_NAME_new_index(unsigned long (*hash_func
) (const char *),
59 int (*cmp_func
) (const char *, const char *),
60 void (*free_func
) (const char *, int, const char *))
64 NAME_FUNCS
*name_funcs
;
66 if (name_funcs_stack
== NULL
) {
67 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE
);
68 name_funcs_stack
= sk_NAME_FUNCS_new_null();
69 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE
);
71 if (name_funcs_stack
== NULL
) {
77 for (i
= sk_NAME_FUNCS_num(name_funcs_stack
); i
< names_type_num
; i
++) {
78 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE
);
79 name_funcs
= OPENSSL_zalloc(sizeof(*name_funcs
));
80 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE
);
81 if (name_funcs
== NULL
) {
82 OBJerr(OBJ_F_OBJ_NAME_NEW_INDEX
, ERR_R_MALLOC_FAILURE
);
85 name_funcs
->hash_func
= lh_strhash
;
86 name_funcs
->cmp_func
= OPENSSL_strcmp
;
87 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE
);
88 sk_NAME_FUNCS_push(name_funcs_stack
, name_funcs
);
89 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE
);
91 name_funcs
= sk_NAME_FUNCS_value(name_funcs_stack
, ret
);
92 if (hash_func
!= NULL
)
93 name_funcs
->hash_func
= hash_func
;
95 name_funcs
->cmp_func
= cmp_func
;
96 if (free_func
!= NULL
)
97 name_funcs
->free_func
= free_func
;
101 static int obj_name_cmp(const OBJ_NAME
*a
, const OBJ_NAME
*b
)
105 ret
= a
->type
- b
->type
;
107 if ((name_funcs_stack
!= NULL
)
108 && (sk_NAME_FUNCS_num(name_funcs_stack
) > a
->type
)) {
109 ret
= sk_NAME_FUNCS_value(name_funcs_stack
,
110 a
->type
)->cmp_func(a
->name
, b
->name
);
112 ret
= strcmp(a
->name
, b
->name
);
117 static unsigned long obj_name_hash(const OBJ_NAME
*a
)
121 if ((name_funcs_stack
!= NULL
)
122 && (sk_NAME_FUNCS_num(name_funcs_stack
) > a
->type
)) {
124 sk_NAME_FUNCS_value(name_funcs_stack
,
125 a
->type
)->hash_func(a
->name
);
127 ret
= lh_strhash(a
->name
);
133 const char *OBJ_NAME_get(const char *name
, int type
)
140 if ((names_lh
== NULL
) && !OBJ_NAME_init())
143 alias
= type
& OBJ_NAME_ALIAS
;
144 type
&= ~OBJ_NAME_ALIAS
;
150 ret
= lh_OBJ_NAME_retrieve(names_lh
, &on
);
153 if ((ret
->alias
) && !alias
) {
163 int OBJ_NAME_add(const char *name
, int type
, const char *data
)
168 if ((names_lh
== NULL
) && !OBJ_NAME_init())
171 alias
= type
& OBJ_NAME_ALIAS
;
172 type
&= ~OBJ_NAME_ALIAS
;
174 onp
= OPENSSL_malloc(sizeof(*onp
));
185 ret
= lh_OBJ_NAME_insert(names_lh
, onp
);
188 if ((name_funcs_stack
!= NULL
)
189 && (sk_NAME_FUNCS_num(name_funcs_stack
) > ret
->type
)) {
191 * XXX: I'm not sure I understand why the free function should
192 * get three arguments... -- Richard Levitte
194 sk_NAME_FUNCS_value(name_funcs_stack
,
195 ret
->type
)->free_func(ret
->name
, ret
->type
,
200 if (lh_OBJ_NAME_error(names_lh
)) {
208 int OBJ_NAME_remove(const char *name
, int type
)
212 if (names_lh
== NULL
)
215 type
&= ~OBJ_NAME_ALIAS
;
218 ret
= lh_OBJ_NAME_delete(names_lh
, &on
);
221 if ((name_funcs_stack
!= NULL
)
222 && (sk_NAME_FUNCS_num(name_funcs_stack
) > ret
->type
)) {
224 * XXX: I'm not sure I understand why the free function should
225 * get three arguments... -- Richard Levitte
227 sk_NAME_FUNCS_value(name_funcs_stack
,
228 ret
->type
)->free_func(ret
->name
, ret
->type
,
239 void (*fn
) (const OBJ_NAME
*, void *arg
);
243 static void do_all_fn_doall_arg(const OBJ_NAME
*name
, struct doall
*d
)
245 if (name
->type
== d
->type
)
249 static IMPLEMENT_LHASH_DOALL_ARG_FN(do_all_fn
, const OBJ_NAME
, struct doall
)
251 void OBJ_NAME_do_all(int type
, void (*fn
) (const OBJ_NAME
*, void *arg
),
260 lh_OBJ_NAME_doall_arg(names_lh
, LHASH_DOALL_ARG_FN(do_all_fn
),
264 struct doall_sorted
{
267 const OBJ_NAME
**names
;
270 static void do_all_sorted_fn(const OBJ_NAME
*name
, void *d_
)
272 struct doall_sorted
*d
= d_
;
274 if (name
->type
!= d
->type
)
277 d
->names
[d
->n
++] = name
;
280 static int do_all_sorted_cmp(const void *n1_
, const void *n2_
)
282 const OBJ_NAME
*const *n1
= n1_
;
283 const OBJ_NAME
*const *n2
= n2_
;
285 return strcmp((*n1
)->name
, (*n2
)->name
);
288 void OBJ_NAME_do_all_sorted(int type
,
289 void (*fn
) (const OBJ_NAME
*, void *arg
),
292 struct doall_sorted d
;
297 OPENSSL_malloc(sizeof(*d
.names
) * lh_OBJ_NAME_num_items(names_lh
));
298 /* Really should return an error if !d.names...but its a void function! */
299 if (d
.names
!= NULL
) {
301 OBJ_NAME_do_all(type
, do_all_sorted_fn
, &d
);
303 qsort((void *)d
.names
, d
.n
, sizeof(*d
.names
), do_all_sorted_cmp
);
305 for (n
= 0; n
< d
.n
; ++n
)
308 OPENSSL_free((void *)d
.names
);
312 static int free_type
;
314 static void names_lh_free_doall(OBJ_NAME
*onp
)
319 if (free_type
< 0 || free_type
== onp
->type
)
320 OBJ_NAME_remove(onp
->name
, onp
->type
);
323 static void name_funcs_free(NAME_FUNCS
*ptr
)
328 void OBJ_NAME_cleanup(int type
)
330 unsigned long down_load
;
332 if (names_lh
== NULL
)
336 down_load
= lh_OBJ_NAME_get_down_load(names_lh
);
337 lh_OBJ_NAME_set_down_load(names_lh
, 0);
339 lh_OBJ_NAME_doall(names_lh
, names_lh_free_doall
);
341 lh_OBJ_NAME_free(names_lh
);
342 sk_NAME_FUNCS_pop_free(name_funcs_stack
, name_funcs_free
);
344 name_funcs_stack
= NULL
;
346 lh_OBJ_NAME_set_down_load(names_lh
, down_load
);