sha1-armv4-large.pl: comply with ABI [from HEAD].
[mirror-openssl.git] / crypto / objects / o_names.c
blob8995869587b485dcdb0caa66dad675179388c7c0
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
5 #include "lhash.h"
6 #include "objects.h"
8 /* I use the ex_data stuff to manage the identifiers for the obj_name_types
9 * that applications may define. I only really use the free function field.
11 static LHASH *names_lh=NULL;
12 static int names_type_num=OBJ_NAME_TYPE_NUM;
13 static STACK *names_cmp=NULL;
14 static STACK *names_hash=NULL;
15 static STACK *names_free=NULL;
17 static unsigned long obj_name_hash(OBJ_NAME *a);
18 static int obj_name_cmp(OBJ_NAME *a,OBJ_NAME *b);
20 int OBJ_NAME_init()
22 if (names_lh != NULL) return(1);
23 MemCheck_off();
24 names_lh=lh_new(obj_name_hash,obj_name_cmp);
25 MemCheck_on();
26 return(names_lh != NULL);
29 int OBJ_NAME_new_index(hash_func,cmp_func,free_func)
30 unsigned long (*hash_func)();
31 int (*cmp_func)();
32 void (*free_func)();
34 int ret;
35 int i;
37 if (names_free == NULL)
39 MemCheck_off();
40 names_hash=sk_new_null();
41 names_cmp=sk_new_null();
42 names_free=sk_new_null();
43 MemCheck_on();
45 if ((names_free == NULL) || (names_hash == NULL) || (names_cmp == NULL))
47 /* ERROR */
48 return(0);
50 ret=names_type_num;
51 names_type_num++;
52 for (i=sk_num(names_free); i<names_type_num; i++)
54 MemCheck_off();
55 sk_push(names_hash,(char *)strcmp);
56 sk_push(names_cmp,(char *)lh_strhash);
57 sk_push(names_free,NULL);
58 MemCheck_on();
60 if (hash_func != NULL)
61 sk_value(names_hash,ret)=(char *)hash_func;
62 if (cmp_func != NULL)
63 sk_value(names_cmp,ret)= (char *)cmp_func;
64 if (free_func != NULL)
65 sk_value(names_free,ret)=(char *)free_func;
66 return(ret);
69 static int obj_name_cmp(a,b)
70 OBJ_NAME *a;
71 OBJ_NAME *b;
73 int ret;
74 int (*cmp)();
76 ret=a->type-b->type;
77 if (ret == 0)
79 if ((names_cmp != NULL) && (sk_num(names_cmp) > a->type))
81 cmp=(int (*)())sk_value(names_cmp,a->type);
82 ret=cmp(a->name,b->name);
84 else
85 ret=strcmp(a->name,b->name);
87 return(ret);
90 static unsigned long obj_name_hash(a)
91 OBJ_NAME *a;
93 unsigned long ret;
94 unsigned long (*hash)();
96 if ((names_hash != NULL) && (sk_num(names_hash) > a->type))
98 hash=(unsigned long (*)())sk_value(names_hash,a->type);
99 ret=hash(a->name);
101 else
103 ret=lh_strhash(a->name);
105 ret^=a->type;
106 return(ret);
109 char *OBJ_NAME_get(name,type)
110 char *name;
111 int type;
113 OBJ_NAME on,*ret;
114 int num=0,alias;
116 if (name == NULL) return(NULL);
117 if ((names_lh == NULL) && !OBJ_NAME_init()) return(NULL);
119 alias=type&OBJ_NAME_ALIAS;
120 type&= ~OBJ_NAME_ALIAS;
122 on.name=name;
123 on.type=type;
125 for (;;)
127 ret=(OBJ_NAME *)lh_retrieve(names_lh,(char *)&on);
128 if (ret == NULL) return(NULL);
129 if ((ret->alias) && !alias)
131 if (++num > 10) return(NULL);
132 on.name=ret->data;
134 else
136 return(ret->data);
141 int OBJ_NAME_add(name,type,data)
142 char *name;
143 int type;
144 char *data;
146 void (*f)();
147 OBJ_NAME *onp,*ret;
148 int alias;
150 if ((names_lh == NULL) && !OBJ_NAME_init()) return(0);
152 alias=type&OBJ_NAME_ALIAS;
153 type&= ~OBJ_NAME_ALIAS;
155 onp=(OBJ_NAME *)Malloc(sizeof(OBJ_NAME));
156 if (onp == NULL)
158 /* ERROR */
159 return(0);
162 onp->name=name;
163 onp->alias=alias;
164 onp->type=type;
165 onp->data=data;
167 ret=(OBJ_NAME *)lh_insert(names_lh,(char *)onp);
168 if (ret != NULL)
170 /* free things */
171 if ((names_free != NULL) && (sk_num(names_free) > ret->type))
173 f=(void (*)())sk_value(names_free,ret->type);
174 f(ret->name,ret->type,ret->data);
176 Free((char *)ret);
178 else
180 if (lh_error(names_lh))
182 /* ERROR */
183 return(0);
186 return(1);
189 int OBJ_NAME_remove(name,type)
190 char *name;
191 int type;
193 OBJ_NAME on,*ret;
194 void (*f)();
196 if (names_lh == NULL) return(0);
198 type&= ~OBJ_NAME_ALIAS;
199 on.name=name;
200 on.type=type;
201 ret=(OBJ_NAME *)lh_delete(names_lh,(char *)&on);
202 if (ret != NULL)
204 /* free things */
205 if ((names_free != NULL) && (sk_num(names_free) > type))
207 f=(void (*)())sk_value(names_free,type);
208 f(ret->name,ret->type,ret->data);
210 Free((char *)ret);
211 return(1);
213 else
214 return(0);
217 static int free_type;
219 static void names_lh_free(onp,type)
220 OBJ_NAME *onp;
222 if ((free_type < 0) || (free_type == onp->type))
224 OBJ_NAME_remove(onp->name,onp->type);
228 void OBJ_NAME_cleanup(type)
229 int type;
231 unsigned long down_load;
233 if (names_lh == NULL) return;
235 free_type=type;
236 down_load=names_lh->down_load;
237 names_lh->down_load=0;
239 lh_doall(names_lh,names_lh_free);
240 if (type < 0)
242 lh_free(names_lh);
243 sk_free(names_hash);
244 sk_free(names_cmp);
245 sk_free(names_free);
246 names_lh=NULL;
247 names_hash=NULL;
248 names_cmp=NULL;
249 names_free=NULL;
251 else
252 names_lh->down_load=down_load;