Make the remaining LHASH macro changes. This should leave no remaining
[openssl.git] / crypto / objects / o_names.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 #include <openssl/lhash.h>
6 #include <openssl/objects.h>
7 #include <openssl/safestack.h>
8
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.
11  */
12 static LHASH *names_lh=NULL;
13 static int names_type_num=OBJ_NAME_TYPE_NUM;
14
15 typedef struct name_funcs_st
16         {
17         unsigned long (*hash_func)(const char *name);
18         int (*cmp_func)(const char *a,const char *b);
19         void (*free_func)(const char *, int, const char *);
20         } NAME_FUNCS;
21
22 DECLARE_STACK_OF(NAME_FUNCS)
23 IMPLEMENT_STACK_OF(NAME_FUNCS)
24
25 static STACK_OF(NAME_FUNCS) *name_funcs_stack;
26
27 static unsigned long obj_name_hash(OBJ_NAME *a);
28 static int obj_name_cmp(OBJ_NAME *a,OBJ_NAME *b);
29
30 static IMPLEMENT_LHASH_HASH_FN(obj_name_hash, OBJ_NAME *)
31 static IMPLEMENT_LHASH_COMP_FN(obj_name_cmp, OBJ_NAME *)
32
33 int OBJ_NAME_init(void)
34         {
35         if (names_lh != NULL) return(1);
36         MemCheck_off();
37         names_lh=lh_new(LHASH_HASH_FN(obj_name_hash),
38                         LHASH_COMP_FN(obj_name_cmp));
39         MemCheck_on();
40         return(names_lh != NULL);
41         }
42
43 int OBJ_NAME_new_index(unsigned long (*hash_func)(const char *),
44         int (*cmp_func)(const char *, const char *),
45         void (*free_func)(const char *, int, const char *))
46         {
47         int ret;
48         int i;
49         NAME_FUNCS *name_funcs;
50
51         if (name_funcs_stack == NULL)
52                 {
53                 MemCheck_off();
54                 name_funcs_stack=sk_NAME_FUNCS_new_null();
55                 MemCheck_on();
56                 }
57         if ((name_funcs_stack == NULL))
58                 {
59                 /* ERROR */
60                 return(0);
61                 }
62         ret=names_type_num;
63         names_type_num++;
64         for (i=sk_NAME_FUNCS_num(name_funcs_stack); i<names_type_num; i++)
65                 {
66                 MemCheck_off();
67                 name_funcs = OPENSSL_malloc(sizeof(NAME_FUNCS));
68                 name_funcs->hash_func = lh_strhash;
69                 name_funcs->cmp_func = strcmp;
70                 name_funcs->free_func = 0; /* NULL is often declared to
71                                                 * ((void *)0), which according
72                                                 * to Compaq C is not really
73                                                 * compatible with a function
74                                                 * pointer.      -- Richard Levitte*/
75                 sk_NAME_FUNCS_push(name_funcs_stack,name_funcs);
76                 MemCheck_on();
77                 }
78         name_funcs = sk_NAME_FUNCS_value(name_funcs_stack, ret);
79         if (hash_func != NULL)
80                 name_funcs->hash_func = hash_func;
81         if (cmp_func != NULL)
82                 name_funcs->cmp_func = cmp_func;
83         if (free_func != NULL)
84                 name_funcs->free_func = free_func;
85         return(ret);
86         }
87
88 static int obj_name_cmp(OBJ_NAME *a, OBJ_NAME *b)
89         {
90         int ret;
91
92         ret=a->type-b->type;
93         if (ret == 0)
94                 {
95                 if ((name_funcs_stack != NULL)
96                         && (sk_NAME_FUNCS_num(name_funcs_stack) > a->type))
97                         {
98                         ret=sk_NAME_FUNCS_value(name_funcs_stack,a->type)
99                                 ->cmp_func(a->name,b->name);
100                         }
101                 else
102                         ret=strcmp(a->name,b->name);
103                 }
104         return(ret);
105         }
106
107 static unsigned long obj_name_hash(OBJ_NAME *a)
108         {
109         unsigned long ret;
110
111         if ((name_funcs_stack != NULL) && (sk_NAME_FUNCS_num(name_funcs_stack) > a->type))
112                 {
113                 ret=sk_NAME_FUNCS_value(name_funcs_stack,a->type)
114                         ->hash_func(a->name);
115                 }
116         else
117                 {
118                 ret=lh_strhash(a->name);
119                 }
120         ret^=a->type;
121         return(ret);
122         }
123
124 const char *OBJ_NAME_get(const char *name, int type)
125         {
126         OBJ_NAME on,*ret;
127         int num=0,alias;
128
129         if (name == NULL) return(NULL);
130         if ((names_lh == NULL) && !OBJ_NAME_init()) return(NULL);
131
132         alias=type&OBJ_NAME_ALIAS;
133         type&= ~OBJ_NAME_ALIAS;
134
135         on.name=name;
136         on.type=type;
137
138         for (;;)
139         {
140                 ret=(OBJ_NAME *)lh_retrieve(names_lh,&on);
141                 if (ret == NULL) return(NULL);
142                 if ((ret->alias) && !alias)
143                         {
144                         if (++num > 10) return(NULL);
145                         on.name=ret->data;
146                         }
147                 else
148                         {
149                         return(ret->data);
150                         }
151                 }
152         }
153
154 int OBJ_NAME_add(const char *name, int type, const char *data)
155         {
156         OBJ_NAME *onp,*ret;
157         int alias;
158
159         if ((names_lh == NULL) && !OBJ_NAME_init()) return(0);
160
161         alias=type&OBJ_NAME_ALIAS;
162         type&= ~OBJ_NAME_ALIAS;
163
164         onp=(OBJ_NAME *)OPENSSL_malloc(sizeof(OBJ_NAME));
165         if (onp == NULL)
166                 {
167                 /* ERROR */
168                 return(0);
169                 }
170
171         onp->name=name;
172         onp->alias=alias;
173         onp->type=type;
174         onp->data=data;
175
176         ret=(OBJ_NAME *)lh_insert(names_lh,onp);
177         if (ret != NULL)
178                 {
179                 /* free things */
180                 if ((name_funcs_stack != NULL) && (sk_NAME_FUNCS_num(name_funcs_stack) > ret->type))
181                         {
182                         /* XXX: I'm not sure I understand why the free
183                          * function should get three arguments...
184                          * -- Richard Levitte
185                          */
186                         sk_NAME_FUNCS_value(name_funcs_stack,ret->type)
187                                 ->free_func(ret->name,ret->type,ret->data);
188                         }
189                 OPENSSL_free(ret);
190                 }
191         else
192                 {
193                 if (lh_error(names_lh))
194                         {
195                         /* ERROR */
196                         return(0);
197                         }
198                 }
199         return(1);
200         }
201
202 int OBJ_NAME_remove(const char *name, int type)
203         {
204         OBJ_NAME on,*ret;
205
206         if (names_lh == NULL) return(0);
207
208         type&= ~OBJ_NAME_ALIAS;
209         on.name=name;
210         on.type=type;
211         ret=(OBJ_NAME *)lh_delete(names_lh,&on);
212         if (ret != NULL)
213                 {
214                 /* free things */
215                 if ((name_funcs_stack != NULL) && (sk_NAME_FUNCS_num(name_funcs_stack) > ret->type))
216                         {
217                         /* XXX: I'm not sure I understand why the free
218                          * function should get three arguments...
219                          * -- Richard Levitte
220                          */
221                         sk_NAME_FUNCS_value(name_funcs_stack,ret->type)
222                                 ->free_func(ret->name,ret->type,ret->data);
223                         }
224                 OPENSSL_free(ret);
225                 return(1);
226                 }
227         else
228                 return(0);
229         }
230
231 struct doall
232         {
233         int type;
234         void (*fn)(const OBJ_NAME *,void *arg);
235         void *arg;
236         };
237
238 static void do_all_fn(const OBJ_NAME *name,struct doall *d)
239         {
240         if(name->type == d->type)
241                 d->fn(name,d->arg);
242         }
243
244 void OBJ_NAME_do_all(int type,void (*fn)(const OBJ_NAME *,void *arg),void *arg)
245         {
246         struct doall d;
247
248         d.type=type;
249         d.fn=fn;
250         d.arg=arg;
251
252         lh_doall_arg(names_lh,(LHASH_DOALL_ARG_FN_TYPE)do_all_fn,&d);
253         }
254
255 struct doall_sorted
256         {
257         int type;
258         int n;
259         const OBJ_NAME **names;
260         };
261
262 static void do_all_sorted_fn(const OBJ_NAME *name,void *d_)
263         {
264         struct doall_sorted *d=d_;
265
266         if(name->type != d->type)
267                 return;
268
269         d->names[d->n++]=name;
270         }
271
272 static int do_all_sorted_cmp(const void *n1_,const void *n2_)
273         {
274         const OBJ_NAME * const *n1=n1_;
275         const OBJ_NAME * const *n2=n2_;
276
277         return strcmp((*n1)->name,(*n2)->name);
278         }
279
280 void OBJ_NAME_do_all_sorted(int type,void (*fn)(const OBJ_NAME *,void *arg),
281                                 void *arg)
282         {
283         struct doall_sorted d;
284         int n;
285
286         d.type=type;
287         d.names=OPENSSL_malloc(lh_num_items(names_lh)*sizeof *d.names);
288         d.n=0;
289         OBJ_NAME_do_all(type,do_all_sorted_fn,&d);
290
291         qsort(d.names,d.n,sizeof *d.names,do_all_sorted_cmp);
292
293         for(n=0 ; n < d.n ; ++n)
294                 fn(d.names[n],arg);
295
296         OPENSSL_free(d.names);
297         }
298
299 static int free_type;
300
301 static void names_lh_free(OBJ_NAME *onp, int type)
302 {
303         if(onp == NULL)
304                 return;
305
306         if ((free_type < 0) || (free_type == onp->type))
307                 {
308                 OBJ_NAME_remove(onp->name,onp->type);
309                 }
310         }
311
312 static void name_funcs_free(NAME_FUNCS *ptr)
313         {
314         OPENSSL_free(ptr);
315         }
316
317 void OBJ_NAME_cleanup(int type)
318         {
319         unsigned long down_load;
320
321         if (names_lh == NULL) return;
322
323         free_type=type;
324         down_load=names_lh->down_load;
325         names_lh->down_load=0;
326
327         lh_doall(names_lh,(LHASH_DOALL_FN_TYPE)names_lh_free);
328         if (type < 0)
329                 {
330                 lh_free(names_lh);
331                 sk_NAME_FUNCS_pop_free(name_funcs_stack,name_funcs_free);
332                 names_lh=NULL;
333                 name_funcs_stack = NULL;
334                 }
335         else
336                 names_lh->down_load=down_load;
337         }
338