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