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