Continuing adding X509 V3 support. This starts to integrate the code with
[openssl.git] / crypto / objects / o_names.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 #include "lhash.h"
6 #include "objects.h"
7
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.
10  */
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;
16
17 static unsigned long obj_name_hash(OBJ_NAME *a);
18 static int obj_name_cmp(OBJ_NAME *a,OBJ_NAME *b);
19
20 int OBJ_NAME_init()
21         {
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);
27         }
28
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)();
33         {
34         int ret;
35         int i;
36
37         if (names_free == NULL)
38                 {
39                 MemCheck_off();
40                 names_hash=sk_new_null();
41                 names_cmp=sk_new_null();
42                 names_free=sk_new_null();
43                 MemCheck_on();
44                 }
45         if ((names_free == NULL) || (names_hash == NULL) || (names_cmp == NULL))
46                 {
47                 /* ERROR */
48                 return(0);
49                 }
50         ret=names_type_num;
51         names_type_num++;
52         for (i=sk_num(names_free); i<names_type_num; i++)
53                 {
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();
59                 }
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);
67         }
68
69 static int obj_name_cmp(a,b)
70 OBJ_NAME *a;
71 OBJ_NAME *b;
72         {
73         int ret;
74         int (*cmp)();
75
76         ret=a->type-b->type;
77         if (ret == 0)
78                 {
79                 if ((names_cmp != NULL) && (sk_num(names_cmp) > a->type))
80                         {
81                         cmp=(int (*)())sk_value(names_cmp,a->type);
82                         ret=cmp(a->name,b->name);
83                         }
84                 else
85                         ret=strcmp(a->name,b->name);
86                 }
87         return(ret);
88         }
89
90 static unsigned long obj_name_hash(a)
91 OBJ_NAME *a;
92         {
93         unsigned long ret;
94         unsigned long (*hash)();
95
96         if ((names_hash != NULL) && (sk_num(names_hash) > a->type))
97                 {
98                 hash=(unsigned long (*)())sk_value(names_hash,a->type);
99                 ret=hash(a->name);
100                 }
101         else
102                 {
103                 ret=lh_strhash(a->name);
104                 }
105         ret^=a->type;
106         return(ret);
107         }
108
109 char *OBJ_NAME_get(name,type)
110 char *name;
111 int type;
112         {
113         OBJ_NAME on,*ret;
114         int num=0,alias;
115
116         if (name == NULL) return(NULL);
117         if ((names_lh == NULL) && !OBJ_NAME_init()) return(NULL);
118
119         alias=type&OBJ_NAME_ALIAS;
120         type&= ~OBJ_NAME_ALIAS;
121
122         on.name=name;
123         on.type=type;
124
125         for (;;)
126                 {
127                 ret=(OBJ_NAME *)lh_retrieve(names_lh,(char *)&on);
128                 if (ret == NULL) return(NULL);
129                 if ((ret->alias) && !alias)
130                         {
131                         if (++num > 10) return(NULL);
132                         on.name=ret->data;
133                         }
134                 else
135                         {
136                         return(ret->data);
137                         }
138                 }
139         }
140
141 int OBJ_NAME_add(name,type,data)
142 char *name;
143 int type;
144 char *data;
145         {
146         void (*f)();
147         OBJ_NAME *onp,*ret;
148         int alias;
149
150         if ((names_lh == NULL) && !OBJ_NAME_init()) return(0);
151
152         alias=type&OBJ_NAME_ALIAS;
153         type&= ~OBJ_NAME_ALIAS;
154
155         onp=(OBJ_NAME *)Malloc(sizeof(OBJ_NAME));
156         if (onp == NULL)
157                 {
158                 /* ERROR */
159                 return(0);
160                 }
161
162         onp->name=name;
163         onp->alias=alias;
164         onp->type=type;
165         onp->data=data;
166
167         ret=(OBJ_NAME *)lh_insert(names_lh,(char *)onp);
168         if (ret != NULL)
169                 {
170                 /* free things */
171                 if ((names_free != NULL) && (sk_num(names_free) > ret->type))
172                         {
173                         f=(void (*)())sk_value(names_free,ret->type);
174                         f(ret->name,ret->type,ret->data);
175                         }
176                 Free((char *)ret);
177                 }
178         else
179                 {
180                 if (lh_error(names_lh))
181                         {
182                         /* ERROR */
183                         return(0);
184                         }
185                 }
186         return(1);
187         }
188
189 int OBJ_NAME_remove(name,type)
190 char *name;
191 int type;
192         {
193         OBJ_NAME on,*ret;
194         void (*f)();
195
196         if (names_lh == NULL) return(0);
197
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)
203                 {
204                 /* free things */
205                 if ((names_free != NULL) && (sk_num(names_free) > type))
206                         {
207                         f=(void (*)())sk_value(names_free,type);
208                         f(ret->name,ret->type,ret->data);
209                         }
210                 Free((char *)ret);
211                 return(1);
212                 }
213         else
214                 return(0);
215         }
216
217 static int free_type;
218
219 static void names_lh_free(onp,type)
220 OBJ_NAME *onp;
221         {
222         if(onp == NULL)
223             return;
224
225         if ((free_type < 0) || (free_type == onp->type))
226                 {
227                 OBJ_NAME_remove(onp->name,onp->type);
228                 }
229         }
230
231 void OBJ_NAME_cleanup(type)
232 int type;
233         {
234         unsigned long down_load;
235
236         if (names_lh == NULL) return;
237
238         free_type=type;
239         down_load=names_lh->down_load;
240         names_lh->down_load=0;
241
242         lh_doall(names_lh,names_lh_free);
243         if (type < 0)
244                 {
245                 lh_free(names_lh);
246                 sk_free(names_hash);
247                 sk_free(names_cmp);
248                 sk_free(names_free);
249                 names_lh=NULL;
250                 names_hash=NULL;
251                 names_cmp=NULL;
252                 names_free=NULL;
253                 }
254         else
255                 names_lh->down_load=down_load;
256         }
257