diff options
Diffstat (limited to 'src/lib/libcrypto/objects/o_names.c')
-rw-r--r-- | src/lib/libcrypto/objects/o_names.c | 106 |
1 files changed, 64 insertions, 42 deletions
diff --git a/src/lib/libcrypto/objects/o_names.c b/src/lib/libcrypto/objects/o_names.c index 4da5e45b9c..d654eb220e 100644 --- a/src/lib/libcrypto/objects/o_names.c +++ b/src/lib/libcrypto/objects/o_names.c | |||
@@ -4,15 +4,25 @@ | |||
4 | 4 | ||
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 | 8 | ||
8 | /* I use the ex_data stuff to manage the identifiers for the obj_name_types | 9 | /* 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 | * that applications may define. I only really use the free function field. |
10 | */ | 11 | */ |
11 | static LHASH *names_lh=NULL; | 12 | static LHASH *names_lh=NULL; |
12 | static int names_type_num=OBJ_NAME_TYPE_NUM; | 13 | static int names_type_num=OBJ_NAME_TYPE_NUM; |
13 | static STACK *names_cmp=NULL; | 14 | |
14 | static STACK *names_hash=NULL; | 15 | typedef struct name_funcs_st |
15 | static STACK *names_free=NULL; | 16 | { |
17 | unsigned long (*hash_func)(); | ||
18 | int (*cmp_func)(); | ||
19 | void (*free_func)(); | ||
20 | } NAME_FUNCS; | ||
21 | |||
22 | DECLARE_STACK_OF(NAME_FUNCS) | ||
23 | IMPLEMENT_STACK_OF(NAME_FUNCS) | ||
24 | |||
25 | STACK_OF(NAME_FUNCS) *name_funcs_stack; | ||
16 | 26 | ||
17 | static unsigned long obj_name_hash(OBJ_NAME *a); | 27 | static unsigned long obj_name_hash(OBJ_NAME *a); |
18 | static int obj_name_cmp(OBJ_NAME *a,OBJ_NAME *b); | 28 | static int obj_name_cmp(OBJ_NAME *a,OBJ_NAME *b); |
@@ -31,51 +41,57 @@ int OBJ_NAME_new_index(unsigned long (*hash_func)(), int (*cmp_func)(), | |||
31 | { | 41 | { |
32 | int ret; | 42 | int ret; |
33 | int i; | 43 | int i; |
44 | NAME_FUNCS *name_funcs; | ||
34 | 45 | ||
35 | if (names_free == NULL) | 46 | if (name_funcs_stack == NULL) |
36 | { | 47 | { |
37 | MemCheck_off(); | 48 | MemCheck_off(); |
38 | names_hash=sk_new_null(); | 49 | name_funcs_stack=sk_NAME_FUNCS_new_null(); |
39 | names_cmp=sk_new_null(); | ||
40 | names_free=sk_new_null(); | ||
41 | MemCheck_on(); | 50 | MemCheck_on(); |
42 | } | 51 | } |
43 | if ((names_free == NULL) || (names_hash == NULL) || (names_cmp == NULL)) | 52 | if ((name_funcs_stack == NULL)) |
44 | { | 53 | { |
45 | /* ERROR */ | 54 | /* ERROR */ |
46 | return(0); | 55 | return(0); |
47 | } | 56 | } |
48 | ret=names_type_num; | 57 | ret=names_type_num; |
49 | names_type_num++; | 58 | names_type_num++; |
50 | for (i=sk_num(names_free); i<names_type_num; i++) | 59 | for (i=sk_NAME_FUNCS_num(name_funcs_stack); i<names_type_num; i++) |
51 | { | 60 | { |
52 | MemCheck_off(); | 61 | MemCheck_off(); |
53 | sk_push(names_hash,(char *)strcmp); | 62 | name_funcs = Malloc(sizeof(NAME_FUNCS)); |
54 | sk_push(names_cmp,(char *)lh_strhash); | 63 | name_funcs->hash_func = lh_strhash; |
55 | sk_push(names_free,NULL); | 64 | name_funcs->cmp_func = (int (*)())strcmp; |
65 | name_funcs->free_func = 0; /* NULL is often declared to | ||
66 | * ((void *)0), which according | ||
67 | * to Compaq C is not really | ||
68 | * compatible with a function | ||
69 | * pointer. -- Richard Levitte*/ | ||
70 | sk_NAME_FUNCS_push(name_funcs_stack,name_funcs); | ||
56 | MemCheck_on(); | 71 | MemCheck_on(); |
57 | } | 72 | } |
73 | name_funcs = sk_NAME_FUNCS_value(name_funcs_stack, ret); | ||
58 | if (hash_func != NULL) | 74 | if (hash_func != NULL) |
59 | sk_set(names_hash,ret,(char *)hash_func); | 75 | name_funcs->hash_func = hash_func; |
60 | if (cmp_func != NULL) | 76 | if (cmp_func != NULL) |
61 | sk_set(names_cmp,ret,(char *)cmp_func); | 77 | name_funcs->cmp_func = cmp_func; |
62 | if (free_func != NULL) | 78 | if (free_func != NULL) |
63 | sk_set(names_free,ret,(char *)free_func); | 79 | name_funcs->free_func = free_func; |
64 | return(ret); | 80 | return(ret); |
65 | } | 81 | } |
66 | 82 | ||
67 | static int obj_name_cmp(OBJ_NAME *a, OBJ_NAME *b) | 83 | static int obj_name_cmp(OBJ_NAME *a, OBJ_NAME *b) |
68 | { | 84 | { |
69 | int ret; | 85 | int ret; |
70 | int (*cmp)(); | ||
71 | 86 | ||
72 | ret=a->type-b->type; | 87 | ret=a->type-b->type; |
73 | if (ret == 0) | 88 | if (ret == 0) |
74 | { | 89 | { |
75 | if ((names_cmp != NULL) && (sk_num(names_cmp) > a->type)) | 90 | if ((name_funcs_stack != NULL) |
91 | && (sk_NAME_FUNCS_num(name_funcs_stack) > a->type)) | ||
76 | { | 92 | { |
77 | cmp=(int (*)())sk_value(names_cmp,a->type); | 93 | ret=sk_NAME_FUNCS_value(name_funcs_stack,a->type) |
78 | ret=cmp(a->name,b->name); | 94 | ->cmp_func(a->name,b->name); |
79 | } | 95 | } |
80 | else | 96 | else |
81 | ret=strcmp(a->name,b->name); | 97 | ret=strcmp(a->name,b->name); |
@@ -86,12 +102,11 @@ static int obj_name_cmp(OBJ_NAME *a, OBJ_NAME *b) | |||
86 | static unsigned long obj_name_hash(OBJ_NAME *a) | 102 | static unsigned long obj_name_hash(OBJ_NAME *a) |
87 | { | 103 | { |
88 | unsigned long ret; | 104 | unsigned long ret; |
89 | unsigned long (*hash)(); | ||
90 | 105 | ||
91 | if ((names_hash != NULL) && (sk_num(names_hash) > a->type)) | 106 | if ((name_funcs_stack != NULL) && (sk_NAME_FUNCS_num(name_funcs_stack) > a->type)) |
92 | { | 107 | { |
93 | hash=(unsigned long (*)())sk_value(names_hash,a->type); | 108 | ret=sk_NAME_FUNCS_value(name_funcs_stack,a->type) |
94 | ret=hash(a->name); | 109 | ->hash_func(a->name); |
95 | } | 110 | } |
96 | else | 111 | else |
97 | { | 112 | { |
@@ -117,7 +132,7 @@ const char *OBJ_NAME_get(const char *name, int type) | |||
117 | 132 | ||
118 | for (;;) | 133 | for (;;) |
119 | { | 134 | { |
120 | ret=(OBJ_NAME *)lh_retrieve(names_lh,(char *)&on); | 135 | ret=(OBJ_NAME *)lh_retrieve(names_lh,&on); |
121 | if (ret == NULL) return(NULL); | 136 | if (ret == NULL) return(NULL); |
122 | if ((ret->alias) && !alias) | 137 | if ((ret->alias) && !alias) |
123 | { | 138 | { |
@@ -133,7 +148,6 @@ const char *OBJ_NAME_get(const char *name, int type) | |||
133 | 148 | ||
134 | int OBJ_NAME_add(const char *name, int type, const char *data) | 149 | int OBJ_NAME_add(const char *name, int type, const char *data) |
135 | { | 150 | { |
136 | void (*f)(); | ||
137 | OBJ_NAME *onp,*ret; | 151 | OBJ_NAME *onp,*ret; |
138 | int alias; | 152 | int alias; |
139 | 153 | ||
@@ -154,16 +168,20 @@ int OBJ_NAME_add(const char *name, int type, const char *data) | |||
154 | onp->type=type; | 168 | onp->type=type; |
155 | onp->data=data; | 169 | onp->data=data; |
156 | 170 | ||
157 | ret=(OBJ_NAME *)lh_insert(names_lh,(char *)onp); | 171 | ret=(OBJ_NAME *)lh_insert(names_lh,onp); |
158 | if (ret != NULL) | 172 | if (ret != NULL) |
159 | { | 173 | { |
160 | /* free things */ | 174 | /* free things */ |
161 | if ((names_free != NULL) && (sk_num(names_free) > ret->type)) | 175 | if ((name_funcs_stack != NULL) && (sk_NAME_FUNCS_num(name_funcs_stack) > ret->type)) |
162 | { | 176 | { |
163 | f=(void (*)())sk_value(names_free,ret->type); | 177 | /* XXX: I'm not sure I understand why the free |
164 | f(ret->name,ret->type,ret->data); | 178 | * function should get three arguments... |
179 | * -- Richard Levitte | ||
180 | */ | ||
181 | sk_NAME_FUNCS_value(name_funcs_stack,ret->type) | ||
182 | ->free_func(ret->name,ret->type,ret->data); | ||
165 | } | 183 | } |
166 | Free((char *)ret); | 184 | Free(ret); |
167 | } | 185 | } |
168 | else | 186 | else |
169 | { | 187 | { |
@@ -179,23 +197,26 @@ int OBJ_NAME_add(const char *name, int type, const char *data) | |||
179 | int OBJ_NAME_remove(const char *name, int type) | 197 | int OBJ_NAME_remove(const char *name, int type) |
180 | { | 198 | { |
181 | OBJ_NAME on,*ret; | 199 | OBJ_NAME on,*ret; |
182 | void (*f)(); | ||
183 | 200 | ||
184 | if (names_lh == NULL) return(0); | 201 | if (names_lh == NULL) return(0); |
185 | 202 | ||
186 | type&= ~OBJ_NAME_ALIAS; | 203 | type&= ~OBJ_NAME_ALIAS; |
187 | on.name=name; | 204 | on.name=name; |
188 | on.type=type; | 205 | on.type=type; |
189 | ret=(OBJ_NAME *)lh_delete(names_lh,(char *)&on); | 206 | ret=(OBJ_NAME *)lh_delete(names_lh,&on); |
190 | if (ret != NULL) | 207 | if (ret != NULL) |
191 | { | 208 | { |
192 | /* free things */ | 209 | /* free things */ |
193 | if ((names_free != NULL) && (sk_num(names_free) > type)) | 210 | if ((name_funcs_stack != NULL) && (sk_NAME_FUNCS_num(name_funcs_stack) > ret->type)) |
194 | { | 211 | { |
195 | f=(void (*)())sk_value(names_free,type); | 212 | /* XXX: I'm not sure I understand why the free |
196 | f(ret->name,ret->type,ret->data); | 213 | * function should get three arguments... |
214 | * -- Richard Levitte | ||
215 | */ | ||
216 | sk_NAME_FUNCS_value(name_funcs_stack,ret->type) | ||
217 | ->free_func(ret->name,ret->type,ret->data); | ||
197 | } | 218 | } |
198 | Free((char *)ret); | 219 | Free(ret); |
199 | return(1); | 220 | return(1); |
200 | } | 221 | } |
201 | else | 222 | else |
@@ -215,6 +236,11 @@ static void names_lh_free(OBJ_NAME *onp, int type) | |||
215 | } | 236 | } |
216 | } | 237 | } |
217 | 238 | ||
239 | static void name_funcs_free(NAME_FUNCS *ptr) | ||
240 | { | ||
241 | Free(ptr); | ||
242 | } | ||
243 | |||
218 | void OBJ_NAME_cleanup(int type) | 244 | void OBJ_NAME_cleanup(int type) |
219 | { | 245 | { |
220 | unsigned long down_load; | 246 | unsigned long down_load; |
@@ -229,13 +255,9 @@ void OBJ_NAME_cleanup(int type) | |||
229 | if (type < 0) | 255 | if (type < 0) |
230 | { | 256 | { |
231 | lh_free(names_lh); | 257 | lh_free(names_lh); |
232 | sk_free(names_hash); | 258 | sk_NAME_FUNCS_pop_free(name_funcs_stack,name_funcs_free); |
233 | sk_free(names_cmp); | ||
234 | sk_free(names_free); | ||
235 | names_lh=NULL; | 259 | names_lh=NULL; |
236 | names_hash=NULL; | 260 | name_funcs_stack = NULL; |
237 | names_cmp=NULL; | ||
238 | names_free=NULL; | ||
239 | } | 261 | } |
240 | else | 262 | else |
241 | names_lh->down_load=down_load; | 263 | names_lh->down_load=down_load; |