diff options
author | beck <> | 2002-05-15 02:29:21 +0000 |
---|---|---|
committer | beck <> | 2002-05-15 02:29:21 +0000 |
commit | b64270d1e45fe7f3241e4c9b6ce60d5ac89bc2e9 (patch) | |
tree | fa27cf82a1250b64ed3bf5f4a18c7354d470bbcc /src/lib/libcrypto/objects/o_names.c | |
parent | e471e1ea98d673597b182ea85f29e30c97cd08b5 (diff) | |
download | openbsd-b64270d1e45fe7f3241e4c9b6ce60d5ac89bc2e9.tar.gz openbsd-b64270d1e45fe7f3241e4c9b6ce60d5ac89bc2e9.tar.bz2 openbsd-b64270d1e45fe7f3241e4c9b6ce60d5ac89bc2e9.zip |
OpenSSL 0.9.7 stable 2002 05 08 merge
Diffstat (limited to 'src/lib/libcrypto/objects/o_names.c')
-rw-r--r-- | src/lib/libcrypto/objects/o_names.c | 147 |
1 files changed, 121 insertions, 26 deletions
diff --git a/src/lib/libcrypto/objects/o_names.c b/src/lib/libcrypto/objects/o_names.c index dca988230e..2b80243256 100644 --- a/src/lib/libcrypto/objects/o_names.c +++ b/src/lib/libcrypto/objects/o_names.c | |||
@@ -5,6 +5,18 @@ | |||
5 | #include <openssl/lhash.h> | 5 | #include <openssl/lhash.h> |
6 | #include <openssl/objects.h> | 6 | #include <openssl/objects.h> |
7 | #include <openssl/safestack.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 | ||
8 | 20 | ||
9 | /* I use the ex_data stuff to manage the identifiers for the obj_name_types | 21 | /* 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. | 22 | * that applications may define. I only really use the free function field. |
@@ -14,9 +26,9 @@ static int names_type_num=OBJ_NAME_TYPE_NUM; | |||
14 | 26 | ||
15 | typedef struct name_funcs_st | 27 | typedef struct name_funcs_st |
16 | { | 28 | { |
17 | unsigned long (*hash_func)(); | 29 | unsigned long (*hash_func)(const char *name); |
18 | int (*cmp_func)(); | 30 | int (*cmp_func)(const char *a,const char *b); |
19 | void (*free_func)(); | 31 | void (*free_func)(const char *, int, const char *); |
20 | } NAME_FUNCS; | 32 | } NAME_FUNCS; |
21 | 33 | ||
22 | DECLARE_STACK_OF(NAME_FUNCS) | 34 | DECLARE_STACK_OF(NAME_FUNCS) |
@@ -24,20 +36,26 @@ IMPLEMENT_STACK_OF(NAME_FUNCS) | |||
24 | 36 | ||
25 | static STACK_OF(NAME_FUNCS) *name_funcs_stack; | 37 | static STACK_OF(NAME_FUNCS) *name_funcs_stack; |
26 | 38 | ||
27 | static unsigned long obj_name_hash(OBJ_NAME *a); | 39 | /* The LHASH callbacks now use the raw "void *" prototypes and do per-variable |
28 | static int obj_name_cmp(OBJ_NAME *a,OBJ_NAME *b); | 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); | ||
29 | 47 | ||
30 | int OBJ_NAME_init(void) | 48 | int OBJ_NAME_init(void) |
31 | { | 49 | { |
32 | if (names_lh != NULL) return(1); | 50 | if (names_lh != NULL) return(1); |
33 | MemCheck_off(); | 51 | MemCheck_off(); |
34 | names_lh=lh_new(obj_name_hash,obj_name_cmp); | 52 | names_lh=lh_new(obj_name_hash, obj_name_cmp); |
35 | MemCheck_on(); | 53 | MemCheck_on(); |
36 | return(names_lh != NULL); | 54 | return(names_lh != NULL); |
37 | } | 55 | } |
38 | 56 | ||
39 | int OBJ_NAME_new_index(unsigned long (*hash_func)(const char *), | 57 | int OBJ_NAME_new_index(unsigned long (*hash_func)(const char *), |
40 | int (*cmp_func)(const void *, const void *), | 58 | int (*cmp_func)(const char *, const char *), |
41 | void (*free_func)(const char *, int, const char *)) | 59 | void (*free_func)(const char *, int, const char *)) |
42 | { | 60 | { |
43 | int ret; | 61 | int ret; |
@@ -62,12 +80,12 @@ int OBJ_NAME_new_index(unsigned long (*hash_func)(const char *), | |||
62 | MemCheck_off(); | 80 | MemCheck_off(); |
63 | name_funcs = OPENSSL_malloc(sizeof(NAME_FUNCS)); | 81 | name_funcs = OPENSSL_malloc(sizeof(NAME_FUNCS)); |
64 | name_funcs->hash_func = lh_strhash; | 82 | name_funcs->hash_func = lh_strhash; |
65 | name_funcs->cmp_func = (int (*)())strcmp; | 83 | name_funcs->cmp_func = OPENSSL_strcmp; |
66 | name_funcs->free_func = 0; /* NULL is often declared to | 84 | name_funcs->free_func = 0; /* NULL is often declared to |
67 | * ((void *)0), which according | 85 | * ((void *)0), which according |
68 | * to Compaq C is not really | 86 | * to Compaq C is not really |
69 | * compatible with a function | 87 | * compatible with a function |
70 | * pointer. -- Richard Levitte*/ | 88 | * pointer. -- Richard Levitte*/ |
71 | sk_NAME_FUNCS_push(name_funcs_stack,name_funcs); | 89 | sk_NAME_FUNCS_push(name_funcs_stack,name_funcs); |
72 | MemCheck_on(); | 90 | MemCheck_on(); |
73 | } | 91 | } |
@@ -81,9 +99,12 @@ int OBJ_NAME_new_index(unsigned long (*hash_func)(const char *), | |||
81 | return(ret); | 99 | return(ret); |
82 | } | 100 | } |
83 | 101 | ||
84 | static int obj_name_cmp(OBJ_NAME *a, OBJ_NAME *b) | 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) | ||
85 | { | 104 | { |
86 | int ret; | 105 | int ret; |
106 | OBJ_NAME *a = (OBJ_NAME *)a_void; | ||
107 | OBJ_NAME *b = (OBJ_NAME *)b_void; | ||
87 | 108 | ||
88 | ret=a->type-b->type; | 109 | ret=a->type-b->type; |
89 | if (ret == 0) | 110 | if (ret == 0) |
@@ -91,8 +112,8 @@ static int obj_name_cmp(OBJ_NAME *a, OBJ_NAME *b) | |||
91 | if ((name_funcs_stack != NULL) | 112 | if ((name_funcs_stack != NULL) |
92 | && (sk_NAME_FUNCS_num(name_funcs_stack) > a->type)) | 113 | && (sk_NAME_FUNCS_num(name_funcs_stack) > a->type)) |
93 | { | 114 | { |
94 | ret=sk_NAME_FUNCS_value(name_funcs_stack,a->type) | 115 | ret=sk_NAME_FUNCS_value(name_funcs_stack, |
95 | ->cmp_func(a->name,b->name); | 116 | a->type)->cmp_func(a->name,b->name); |
96 | } | 117 | } |
97 | else | 118 | else |
98 | ret=strcmp(a->name,b->name); | 119 | ret=strcmp(a->name,b->name); |
@@ -100,14 +121,16 @@ static int obj_name_cmp(OBJ_NAME *a, OBJ_NAME *b) | |||
100 | return(ret); | 121 | return(ret); |
101 | } | 122 | } |
102 | 123 | ||
103 | static unsigned long obj_name_hash(OBJ_NAME *a) | 124 | /* static unsigned long obj_name_hash(OBJ_NAME *a) */ |
125 | static unsigned long obj_name_hash(const void *a_void) | ||
104 | { | 126 | { |
105 | unsigned long ret; | 127 | unsigned long ret; |
128 | OBJ_NAME *a = (OBJ_NAME *)a_void; | ||
106 | 129 | ||
107 | if ((name_funcs_stack != NULL) && (sk_NAME_FUNCS_num(name_funcs_stack) > a->type)) | 130 | if ((name_funcs_stack != NULL) && (sk_NAME_FUNCS_num(name_funcs_stack) > a->type)) |
108 | { | 131 | { |
109 | ret=sk_NAME_FUNCS_value(name_funcs_stack,a->type) | 132 | ret=sk_NAME_FUNCS_value(name_funcs_stack, |
110 | ->hash_func(a->name); | 133 | a->type)->hash_func(a->name); |
111 | } | 134 | } |
112 | else | 135 | else |
113 | { | 136 | { |
@@ -132,7 +155,7 @@ const char *OBJ_NAME_get(const char *name, int type) | |||
132 | on.type=type; | 155 | on.type=type; |
133 | 156 | ||
134 | for (;;) | 157 | for (;;) |
135 | { | 158 | { |
136 | ret=(OBJ_NAME *)lh_retrieve(names_lh,&on); | 159 | ret=(OBJ_NAME *)lh_retrieve(names_lh,&on); |
137 | if (ret == NULL) return(NULL); | 160 | if (ret == NULL) return(NULL); |
138 | if ((ret->alias) && !alias) | 161 | if ((ret->alias) && !alias) |
@@ -179,8 +202,8 @@ int OBJ_NAME_add(const char *name, int type, const char *data) | |||
179 | * function should get three arguments... | 202 | * function should get three arguments... |
180 | * -- Richard Levitte | 203 | * -- Richard Levitte |
181 | */ | 204 | */ |
182 | sk_NAME_FUNCS_value(name_funcs_stack,ret->type) | 205 | sk_NAME_FUNCS_value(name_funcs_stack, |
183 | ->free_func(ret->name,ret->type,ret->data); | 206 | ret->type)->free_func(ret->name,ret->type,ret->data); |
184 | } | 207 | } |
185 | OPENSSL_free(ret); | 208 | OPENSSL_free(ret); |
186 | } | 209 | } |
@@ -214,8 +237,8 @@ int OBJ_NAME_remove(const char *name, int type) | |||
214 | * function should get three arguments... | 237 | * function should get three arguments... |
215 | * -- Richard Levitte | 238 | * -- Richard Levitte |
216 | */ | 239 | */ |
217 | sk_NAME_FUNCS_value(name_funcs_stack,ret->type) | 240 | sk_NAME_FUNCS_value(name_funcs_stack, |
218 | ->free_func(ret->name,ret->type,ret->data); | 241 | ret->type)->free_func(ret->name,ret->type,ret->data); |
219 | } | 242 | } |
220 | OPENSSL_free(ret); | 243 | OPENSSL_free(ret); |
221 | return(1); | 244 | return(1); |
@@ -224,12 +247,82 @@ int OBJ_NAME_remove(const char *name, int type) | |||
224 | return(0); | 247 | return(0); |
225 | } | 248 | } |
226 | 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 | |||
227 | static int free_type; | 320 | static int free_type; |
228 | 321 | ||
229 | static void names_lh_free(OBJ_NAME *onp, int type) | 322 | static void names_lh_free(OBJ_NAME *onp) |
230 | { | 323 | { |
231 | if(onp == NULL) | 324 | if(onp == NULL) |
232 | return; | 325 | return; |
233 | 326 | ||
234 | if ((free_type < 0) || (free_type == onp->type)) | 327 | if ((free_type < 0) || (free_type == onp->type)) |
235 | { | 328 | { |
@@ -237,6 +330,8 @@ static void names_lh_free(OBJ_NAME *onp, int type) | |||
237 | } | 330 | } |
238 | } | 331 | } |
239 | 332 | ||
333 | static IMPLEMENT_LHASH_DOALL_FN(names_lh_free, OBJ_NAME *) | ||
334 | |||
240 | static void name_funcs_free(NAME_FUNCS *ptr) | 335 | static void name_funcs_free(NAME_FUNCS *ptr) |
241 | { | 336 | { |
242 | OPENSSL_free(ptr); | 337 | OPENSSL_free(ptr); |
@@ -252,7 +347,7 @@ void OBJ_NAME_cleanup(int type) | |||
252 | down_load=names_lh->down_load; | 347 | down_load=names_lh->down_load; |
253 | names_lh->down_load=0; | 348 | names_lh->down_load=0; |
254 | 349 | ||
255 | lh_doall(names_lh,names_lh_free); | 350 | lh_doall(names_lh,LHASH_DOALL_FN(names_lh_free)); |
256 | if (type < 0) | 351 | if (type < 0) |
257 | { | 352 | { |
258 | lh_free(names_lh); | 353 | lh_free(names_lh); |