]>
git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/objects/o_names.c
5 #include <openssl/lhash.h>
6 #include <openssl/objects.h>
7 #include <openssl/safestack.h>
9 /* I use the ex_data stuff to manage the identifiers for the obj_name_types
10 * that applications may define. I only really use the free function field.
12 static LHASH
*names_lh
=NULL
;
13 static int names_type_num
=OBJ_NAME_TYPE_NUM
;
15 typedef struct name_funcs_st
17 unsigned long (*hash_func
)();
22 DECLARE_STACK_OF(NAME_FUNCS
)
23 IMPLEMENT_STACK_OF(NAME_FUNCS
)
25 static STACK_OF(NAME_FUNCS
) *name_funcs_stack
;
27 static unsigned long obj_name_hash(OBJ_NAME
*a
);
28 static int obj_name_cmp(OBJ_NAME
*a
,OBJ_NAME
*b
);
30 int OBJ_NAME_init(void)
32 if (names_lh
!= NULL
) return(1);
34 names_lh
=lh_new(obj_name_hash
,obj_name_cmp
);
36 return(names_lh
!= NULL
);
39 int OBJ_NAME_new_index(unsigned long (*hash_func
)(), int (*cmp_func
)(),
44 NAME_FUNCS
*name_funcs
;
46 if (name_funcs_stack
== NULL
)
49 name_funcs_stack
=sk_NAME_FUNCS_new_null();
52 if ((name_funcs_stack
== NULL
))
59 for (i
=sk_NAME_FUNCS_num(name_funcs_stack
); i
<names_type_num
; i
++)
62 name_funcs
= OPENSSL_malloc(sizeof(NAME_FUNCS
));
63 name_funcs
->hash_func
= lh_strhash
;
64 name_funcs
->cmp_func
= (int (*)())strcmp
;
65 name_funcs
->free_func
= 0; /* NULL is often declared to
66 * ((void *)0), which according
67 * to Compaq C is not really
68 * compatible with a function
69 * pointer. -- Richard Levitte*/
70 sk_NAME_FUNCS_push(name_funcs_stack
,name_funcs
);
73 name_funcs
= sk_NAME_FUNCS_value(name_funcs_stack
, ret
);
74 if (hash_func
!= NULL
)
75 name_funcs
->hash_func
= hash_func
;
77 name_funcs
->cmp_func
= cmp_func
;
78 if (free_func
!= NULL
)
79 name_funcs
->free_func
= free_func
;
83 static int obj_name_cmp(OBJ_NAME
*a
, OBJ_NAME
*b
)
90 if ((name_funcs_stack
!= NULL
)
91 && (sk_NAME_FUNCS_num(name_funcs_stack
) > a
->type
))
93 ret
=sk_NAME_FUNCS_value(name_funcs_stack
,a
->type
)
94 ->cmp_func(a
->name
,b
->name
);
97 ret
=strcmp(a
->name
,b
->name
);
102 static unsigned long obj_name_hash(OBJ_NAME
*a
)
106 if ((name_funcs_stack
!= NULL
) && (sk_NAME_FUNCS_num(name_funcs_stack
) > a
->type
))
108 ret
=sk_NAME_FUNCS_value(name_funcs_stack
,a
->type
)
109 ->hash_func(a
->name
);
113 ret
=lh_strhash(a
->name
);
119 const char *OBJ_NAME_get(const char *name
, int type
)
124 if (name
== NULL
) return(NULL
);
125 if ((names_lh
== NULL
) && !OBJ_NAME_init()) return(NULL
);
127 alias
=type
&OBJ_NAME_ALIAS
;
128 type
&= ~OBJ_NAME_ALIAS
;
135 ret
=(OBJ_NAME
*)lh_retrieve(names_lh
,&on
);
136 if (ret
== NULL
) return(NULL
);
137 if ((ret
->alias
) && !alias
)
139 if (++num
> 10) return(NULL
);
149 int OBJ_NAME_add(const char *name
, int type
, const char *data
)
154 if ((names_lh
== NULL
) && !OBJ_NAME_init()) return(0);
156 alias
=type
&OBJ_NAME_ALIAS
;
157 type
&= ~OBJ_NAME_ALIAS
;
159 onp
=(OBJ_NAME
*)OPENSSL_malloc(sizeof(OBJ_NAME
));
171 ret
=(OBJ_NAME
*)lh_insert(names_lh
,onp
);
175 if ((name_funcs_stack
!= NULL
) && (sk_NAME_FUNCS_num(name_funcs_stack
) > ret
->type
))
177 /* XXX: I'm not sure I understand why the free
178 * function should get three arguments...
181 sk_NAME_FUNCS_value(name_funcs_stack
,ret
->type
)
182 ->free_func(ret
->name
,ret
->type
,ret
->data
);
188 if (lh_error(names_lh
))
197 int OBJ_NAME_remove(const char *name
, int type
)
201 if (names_lh
== NULL
) return(0);
203 type
&= ~OBJ_NAME_ALIAS
;
206 ret
=(OBJ_NAME
*)lh_delete(names_lh
,&on
);
210 if ((name_funcs_stack
!= NULL
) && (sk_NAME_FUNCS_num(name_funcs_stack
) > ret
->type
))
212 /* XXX: I'm not sure I understand why the free
213 * function should get three arguments...
216 sk_NAME_FUNCS_value(name_funcs_stack
,ret
->type
)
217 ->free_func(ret
->name
,ret
->type
,ret
->data
);
226 static int free_type
;
228 static void names_lh_free(OBJ_NAME
*onp
, int type
)
233 if ((free_type
< 0) || (free_type
== onp
->type
))
235 OBJ_NAME_remove(onp
->name
,onp
->type
);
239 static void name_funcs_free(NAME_FUNCS
*ptr
)
244 void OBJ_NAME_cleanup(int type
)
246 unsigned long down_load
;
248 if (names_lh
== NULL
) return;
251 down_load
=names_lh
->down_load
;
252 names_lh
->down_load
=0;
254 lh_doall(names_lh
,names_lh_free
);
258 sk_NAME_FUNCS_pop_free(name_funcs_stack
,name_funcs_free
);
260 name_funcs_stack
= NULL
;
263 names_lh
->down_load
=down_load
;