summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/objects/o_names.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/objects/o_names.c')
-rw-r--r--src/lib/libcrypto/objects/o_names.c421
1 files changed, 210 insertions, 211 deletions
diff --git a/src/lib/libcrypto/objects/o_names.c b/src/lib/libcrypto/objects/o_names.c
index c238a8b266..9ed1de92c9 100644
--- a/src/lib/libcrypto/objects/o_names.c
+++ b/src/lib/libcrypto/objects/o_names.c
@@ -12,15 +12,14 @@
12 * that applications may define. I only really use the free function field. 12 * that applications may define. I only really use the free function field.
13 */ 13 */
14DECLARE_LHASH_OF(OBJ_NAME); 14DECLARE_LHASH_OF(OBJ_NAME);
15static LHASH_OF(OBJ_NAME) *names_lh=NULL; 15static LHASH_OF(OBJ_NAME) *names_lh = NULL;
16static int names_type_num=OBJ_NAME_TYPE_NUM; 16static int names_type_num = OBJ_NAME_TYPE_NUM;
17 17
18typedef struct name_funcs_st 18typedef struct name_funcs_st {
19 {
20 unsigned long (*hash_func)(const char *name); 19 unsigned long (*hash_func)(const char *name);
21 int (*cmp_func)(const char *a,const char *b); 20 int (*cmp_func)(const char *a, const char *b);
22 void (*free_func)(const char *, int, const char *); 21 void (*free_func)(const char *, int, const char *);
23 } NAME_FUNCS; 22} NAME_FUNCS;
24 23
25DECLARE_STACK_OF(NAME_FUNCS) 24DECLARE_STACK_OF(NAME_FUNCS)
26IMPLEMENT_STACK_OF(NAME_FUNCS) 25IMPLEMENT_STACK_OF(NAME_FUNCS)
@@ -34,58 +33,57 @@ static STACK_OF(NAME_FUNCS) *name_funcs_stack;
34/* static unsigned long obj_name_hash(OBJ_NAME *a); */ 33/* static unsigned long obj_name_hash(OBJ_NAME *a); */
35static unsigned long obj_name_hash(const void *a_void); 34static unsigned long obj_name_hash(const void *a_void);
36/* static int obj_name_cmp(OBJ_NAME *a,OBJ_NAME *b); */ 35/* static int obj_name_cmp(OBJ_NAME *a,OBJ_NAME *b); */
37static int obj_name_cmp(const void *a_void,const void *b_void); 36static int obj_name_cmp(const void *a_void, const void *b_void);
38 37
39static IMPLEMENT_LHASH_HASH_FN(obj_name, OBJ_NAME) 38static IMPLEMENT_LHASH_HASH_FN(obj_name, OBJ_NAME)
40static IMPLEMENT_LHASH_COMP_FN(obj_name, OBJ_NAME) 39static IMPLEMENT_LHASH_COMP_FN(obj_name, OBJ_NAME)
41 40
42int OBJ_NAME_init(void) 41int
43 { 42OBJ_NAME_init(void)
44 if (names_lh != NULL) return(1); 43{
44 if (names_lh != NULL)
45 return (1);
45 MemCheck_off(); 46 MemCheck_off();
46 names_lh=lh_OBJ_NAME_new(); 47 names_lh = lh_OBJ_NAME_new();
47 MemCheck_on(); 48 MemCheck_on();
48 return(names_lh != NULL); 49 return (names_lh != NULL);
49 } 50}
50 51
51int OBJ_NAME_new_index(unsigned long (*hash_func)(const char *), 52int
52 int (*cmp_func)(const char *, const char *), 53OBJ_NAME_new_index(unsigned long (*hash_func)(const char *),
53 void (*free_func)(const char *, int, const char *)) 54 int (*cmp_func)(const char *, const char *),
54 { 55 void (*free_func)(const char *, int, const char *))
56{
55 int ret; 57 int ret;
56 int i; 58 int i;
57 NAME_FUNCS *name_funcs; 59 NAME_FUNCS *name_funcs;
58 60
59 if (name_funcs_stack == NULL) 61 if (name_funcs_stack == NULL) {
60 {
61 MemCheck_off(); 62 MemCheck_off();
62 name_funcs_stack=sk_NAME_FUNCS_new_null(); 63 name_funcs_stack = sk_NAME_FUNCS_new_null();
63 MemCheck_on(); 64 MemCheck_on();
64 } 65 }
65 if (name_funcs_stack == NULL) 66 if (name_funcs_stack == NULL) {
66 {
67 /* ERROR */ 67 /* ERROR */
68 return(0); 68 return (0);
69 } 69 }
70 ret=names_type_num; 70 ret = names_type_num;
71 names_type_num++; 71 names_type_num++;
72 for (i=sk_NAME_FUNCS_num(name_funcs_stack); i<names_type_num; i++) 72 for (i = sk_NAME_FUNCS_num(name_funcs_stack); i < names_type_num; i++) {
73 {
74 MemCheck_off(); 73 MemCheck_off();
75 name_funcs = malloc(sizeof(NAME_FUNCS)); 74 name_funcs = malloc(sizeof(NAME_FUNCS));
76 MemCheck_on(); 75 MemCheck_on();
77 if (!name_funcs) 76 if (!name_funcs) {
78 { 77 OBJerr(OBJ_F_OBJ_NAME_NEW_INDEX, ERR_R_MALLOC_FAILURE);
79 OBJerr(OBJ_F_OBJ_NAME_NEW_INDEX,ERR_R_MALLOC_FAILURE); 78 return (0);
80 return(0); 79 }
81 }
82 name_funcs->hash_func = lh_strhash; 80 name_funcs->hash_func = lh_strhash;
83 name_funcs->cmp_func = strcmp; 81 name_funcs->cmp_func = strcmp;
84 name_funcs->free_func = NULL; 82 name_funcs->free_func = NULL;
85 MemCheck_off(); 83 MemCheck_off();
86 sk_NAME_FUNCS_push(name_funcs_stack,name_funcs); 84 sk_NAME_FUNCS_push(name_funcs_stack, name_funcs);
87 MemCheck_on(); 85 MemCheck_on();
88 } 86 }
89 name_funcs = sk_NAME_FUNCS_value(name_funcs_stack, ret); 87 name_funcs = sk_NAME_FUNCS_value(name_funcs_stack, ret);
90 if (hash_func != NULL) 88 if (hash_func != NULL)
91 name_funcs->hash_func = hash_func; 89 name_funcs->hash_func = hash_func;
@@ -93,265 +91,266 @@ int OBJ_NAME_new_index(unsigned long (*hash_func)(const char *),
93 name_funcs->cmp_func = cmp_func; 91 name_funcs->cmp_func = cmp_func;
94 if (free_func != NULL) 92 if (free_func != NULL)
95 name_funcs->free_func = free_func; 93 name_funcs->free_func = free_func;
96 return(ret); 94 return (ret);
97 } 95}
98 96
99/* static int obj_name_cmp(OBJ_NAME *a, OBJ_NAME *b) */ 97/* static int obj_name_cmp(OBJ_NAME *a, OBJ_NAME *b) */
100static int obj_name_cmp(const void *a_void, const void *b_void) 98static int
101 { 99obj_name_cmp(const void *a_void, const void *b_void)
100{
102 int ret; 101 int ret;
103 const OBJ_NAME *a = (const OBJ_NAME *)a_void; 102 const OBJ_NAME *a = (const OBJ_NAME *)a_void;
104 const OBJ_NAME *b = (const OBJ_NAME *)b_void; 103 const OBJ_NAME *b = (const OBJ_NAME *)b_void;
105 104
106 ret=a->type-b->type; 105 ret = a->type - b->type;
107 if (ret == 0) 106 if (ret == 0) {
108 { 107 if ((name_funcs_stack != NULL) &&
109 if ((name_funcs_stack != NULL) 108 (sk_NAME_FUNCS_num(name_funcs_stack) > a->type)) {
110 && (sk_NAME_FUNCS_num(name_funcs_stack) > a->type)) 109 ret = sk_NAME_FUNCS_value(name_funcs_stack,
111 { 110 a->type)->cmp_func(a->name, b->name);
112 ret=sk_NAME_FUNCS_value(name_funcs_stack, 111 } else
113 a->type)->cmp_func(a->name,b->name); 112 ret = strcmp(a->name, b->name);
114 }
115 else
116 ret=strcmp(a->name,b->name);
117 }
118 return(ret);
119 } 113 }
114 return (ret);
115}
120 116
121/* static unsigned long obj_name_hash(OBJ_NAME *a) */ 117/* static unsigned long obj_name_hash(OBJ_NAME *a) */
122static unsigned long obj_name_hash(const void *a_void) 118static unsigned long
123 { 119obj_name_hash(const void *a_void)
120{
124 unsigned long ret; 121 unsigned long ret;
125 const OBJ_NAME *a = (const OBJ_NAME *)a_void; 122 const OBJ_NAME *a = (const OBJ_NAME *)a_void;
126 123
127 if ((name_funcs_stack != NULL) && (sk_NAME_FUNCS_num(name_funcs_stack) > a->type)) 124 if ((name_funcs_stack != NULL) &&
128 { 125 (sk_NAME_FUNCS_num(name_funcs_stack) > a->type)) {
129 ret=sk_NAME_FUNCS_value(name_funcs_stack, 126 ret = sk_NAME_FUNCS_value(name_funcs_stack,
130 a->type)->hash_func(a->name); 127 a->type)->hash_func(a->name);
131 } 128 } else {
132 else 129 ret = lh_strhash(a->name);
133 {
134 ret=lh_strhash(a->name);
135 }
136 ret^=a->type;
137 return(ret);
138 } 130 }
139 131 ret ^= a->type;
140const char *OBJ_NAME_get(const char *name, int type) 132 return (ret);
141 { 133}
142 OBJ_NAME on,*ret; 134
143 int num=0,alias; 135const char *
144 136OBJ_NAME_get(const char *name, int type)
145 if (name == NULL) return(NULL); 137{
146 if ((names_lh == NULL) && !OBJ_NAME_init()) return(NULL); 138 OBJ_NAME on, *ret;
147 139 int num = 0, alias;
148 alias=type&OBJ_NAME_ALIAS; 140
141 if (name == NULL)
142 return (NULL);
143 if ((names_lh == NULL) && !OBJ_NAME_init())
144 return (NULL);
145
146 alias = type&OBJ_NAME_ALIAS;
149 type&= ~OBJ_NAME_ALIAS; 147 type&= ~OBJ_NAME_ALIAS;
150 148
151 on.name=name; 149 on.name = name;
152 on.type=type; 150 on.type = type;
153 151
154 for (;;) 152 for (;;) {
155 { 153 ret = lh_OBJ_NAME_retrieve(names_lh, &on);
156 ret=lh_OBJ_NAME_retrieve(names_lh,&on); 154 if (ret == NULL)
157 if (ret == NULL) return(NULL); 155 return (NULL);
158 if ((ret->alias) && !alias) 156 if ((ret->alias) && !alias) {
159 { 157 if (++num > 10)
160 if (++num > 10) return(NULL); 158 return (NULL);
161 on.name=ret->data; 159 on.name = ret->data;
162 } 160 } else {
163 else 161 return (ret->data);
164 {
165 return(ret->data);
166 }
167 } 162 }
168 } 163 }
164}
169 165
170int OBJ_NAME_add(const char *name, int type, const char *data) 166int
171 { 167OBJ_NAME_add(const char *name, int type, const char *data)
172 OBJ_NAME *onp,*ret; 168{
169 OBJ_NAME *onp, *ret;
173 int alias; 170 int alias;
174 171
175 if ((names_lh == NULL) && !OBJ_NAME_init()) return(0); 172 if ((names_lh == NULL) && !OBJ_NAME_init())
173 return (0);
176 174
177 alias=type&OBJ_NAME_ALIAS; 175 alias = type & OBJ_NAME_ALIAS;
178 type&= ~OBJ_NAME_ALIAS; 176 type &= ~OBJ_NAME_ALIAS;
179 177
180 onp=(OBJ_NAME *)malloc(sizeof(OBJ_NAME)); 178 onp = (OBJ_NAME *)malloc(sizeof(OBJ_NAME));
181 if (onp == NULL) 179 if (onp == NULL) {
182 {
183 /* ERROR */ 180 /* ERROR */
184 return(0); 181 return (0);
185 } 182 }
186 183
187 onp->name=name; 184 onp->name = name;
188 onp->alias=alias; 185 onp->alias = alias;
189 onp->type=type; 186 onp->type = type;
190 onp->data=data; 187 onp->data = data;
191 188
192 ret=lh_OBJ_NAME_insert(names_lh,onp); 189 ret = lh_OBJ_NAME_insert(names_lh, onp);
193 if (ret != NULL) 190 if (ret != NULL) {
194 {
195 /* free things */ 191 /* free things */
196 if ((name_funcs_stack != NULL) && (sk_NAME_FUNCS_num(name_funcs_stack) > ret->type)) 192 if ((name_funcs_stack != NULL) &&
197 { 193 (sk_NAME_FUNCS_num(name_funcs_stack) > ret->type)) {
198 /* XXX: I'm not sure I understand why the free 194 /* XXX: I'm not sure I understand why the free
199 * function should get three arguments... 195 * function should get three arguments...
200 * -- Richard Levitte 196 * -- Richard Levitte
201 */ 197 */
202 sk_NAME_FUNCS_value(name_funcs_stack, 198 sk_NAME_FUNCS_value(
203 ret->type)->free_func(ret->name,ret->type,ret->data); 199 name_funcs_stack, ret->type)->free_func(
204 } 200 ret->name, ret->type, ret->data);
205 free(ret);
206 } 201 }
207 else 202 free(ret);
208 { 203 } else {
209 if (lh_OBJ_NAME_error(names_lh)) 204 if (lh_OBJ_NAME_error(names_lh)) {
210 {
211 /* ERROR */ 205 /* ERROR */
212 return(0); 206 return (0);
213 }
214 } 207 }
215 return(1);
216 } 208 }
217 209 return (1);
218int OBJ_NAME_remove(const char *name, int type) 210}
219 { 211
220 OBJ_NAME on,*ret; 212int
221 213OBJ_NAME_remove(const char *name, int type)
222 if (names_lh == NULL) return(0); 214{
223 215 OBJ_NAME on, *ret;
224 type&= ~OBJ_NAME_ALIAS; 216
225 on.name=name; 217 if (names_lh == NULL)
226 on.type=type; 218 return (0);
227 ret=lh_OBJ_NAME_delete(names_lh,&on); 219
228 if (ret != NULL) 220 type &= ~OBJ_NAME_ALIAS;
229 { 221 on.name = name;
222 on.type = type;
223 ret = lh_OBJ_NAME_delete(names_lh, &on);
224 if (ret != NULL) {
230 /* free things */ 225 /* free things */
231 if ((name_funcs_stack != NULL) && (sk_NAME_FUNCS_num(name_funcs_stack) > ret->type)) 226 if ((name_funcs_stack != NULL) &&
232 { 227 (sk_NAME_FUNCS_num(name_funcs_stack) > ret->type)) {
233 /* XXX: I'm not sure I understand why the free 228 /* XXX: I'm not sure I understand why the free
234 * function should get three arguments... 229 * function should get three arguments...
235 * -- Richard Levitte 230 * -- Richard Levitte
236 */ 231 */
237 sk_NAME_FUNCS_value(name_funcs_stack, 232 sk_NAME_FUNCS_value(
238 ret->type)->free_func(ret->name,ret->type,ret->data); 233 name_funcs_stack, ret->type)->free_func(
239 } 234 ret->name, ret->type, ret->data);
240 free(ret);
241 return(1);
242 } 235 }
243 else 236 free(ret);
244 return(0); 237 return (1);
245 } 238 } else
239 return (0);
240}
246 241
247struct doall 242struct doall {
248 {
249 int type; 243 int type;
250 void (*fn)(const OBJ_NAME *,void *arg); 244 void (*fn)(const OBJ_NAME *, void *arg);
251 void *arg; 245 void *arg;
252 }; 246};
253 247
254static void do_all_fn_doall_arg(const OBJ_NAME *name,struct doall *d) 248static void
255 { 249do_all_fn_doall_arg(const OBJ_NAME *name, struct doall *d)
256 if(name->type == d->type) 250{
257 d->fn(name,d->arg); 251 if (name->type == d->type)
258 } 252 d->fn(name, d->arg);
253}
259 254
260static IMPLEMENT_LHASH_DOALL_ARG_FN(do_all_fn, const OBJ_NAME, struct doall) 255static IMPLEMENT_LHASH_DOALL_ARG_FN(do_all_fn, const OBJ_NAME, struct doall)
261 256
262void OBJ_NAME_do_all(int type,void (*fn)(const OBJ_NAME *,void *arg),void *arg) 257void
263 { 258OBJ_NAME_do_all(int type, void (*fn)(const OBJ_NAME *, void *arg), void *arg)
259{
264 struct doall d; 260 struct doall d;
265 261
266 d.type=type; 262 d.type = type;
267 d.fn=fn; 263 d.fn = fn;
268 d.arg=arg; 264 d.arg = arg;
269 265
270 lh_OBJ_NAME_doall_arg(names_lh, LHASH_DOALL_ARG_FN(do_all_fn), 266 lh_OBJ_NAME_doall_arg(names_lh, LHASH_DOALL_ARG_FN(do_all_fn),
271 struct doall, &d); 267 struct doall, &d);
272 } 268}
273 269
274struct doall_sorted 270struct doall_sorted {
275 {
276 int type; 271 int type;
277 int n; 272 int n;
278 const OBJ_NAME **names; 273 const OBJ_NAME **names;
279 }; 274};
280 275
281static void do_all_sorted_fn(const OBJ_NAME *name,void *d_) 276static void
282 { 277do_all_sorted_fn(const OBJ_NAME *name, void *d_)
283 struct doall_sorted *d=d_; 278{
279 struct doall_sorted *d = d_;
284 280
285 if(name->type != d->type) 281 if (name->type != d->type)
286 return; 282 return;
287 283
288 d->names[d->n++]=name; 284 d->names[d->n++] = name;
289 } 285}
290 286
291static int do_all_sorted_cmp(const void *n1_,const void *n2_) 287static int
292 { 288do_all_sorted_cmp(const void *n1_, const void *n2_)
293 const OBJ_NAME * const *n1=n1_; 289{
294 const OBJ_NAME * const *n2=n2_; 290 const OBJ_NAME * const *n1 = n1_;
291 const OBJ_NAME * const *n2 = n2_;
295 292
296 return strcmp((*n1)->name,(*n2)->name); 293 return strcmp((*n1)->name, (*n2)->name);
297 } 294}
298 295
299void OBJ_NAME_do_all_sorted(int type,void (*fn)(const OBJ_NAME *,void *arg), 296void
300 void *arg) 297OBJ_NAME_do_all_sorted(int type, void (*fn)(const OBJ_NAME *, void *arg),
301 { 298 void *arg)
299{
302 struct doall_sorted d; 300 struct doall_sorted d;
303 int n; 301 int n;
304 302
305 d.type=type; 303 d.type = type;
306 d.names=malloc(lh_OBJ_NAME_num_items(names_lh)*sizeof *d.names); 304 d.names = malloc(lh_OBJ_NAME_num_items(names_lh)*sizeof *d.names);
307 d.n=0; 305 d.n = 0;
308 OBJ_NAME_do_all(type,do_all_sorted_fn,&d); 306 OBJ_NAME_do_all(type, do_all_sorted_fn, &d);
309 307
310 qsort((void *)d.names,d.n,sizeof *d.names,do_all_sorted_cmp); 308 qsort((void *)d.names, d.n, sizeof *d.names, do_all_sorted_cmp);
311 309
312 for(n=0 ; n < d.n ; ++n) 310 for (n = 0; n < d.n; ++n)
313 fn(d.names[n],arg); 311 fn(d.names[n], arg);
314 312
315 free((void *)d.names); 313 free((void *)d.names);
316 } 314}
317 315
318static int free_type; 316static int free_type;
319 317
320static void names_lh_free_doall(OBJ_NAME *onp) 318static void
321 { 319names_lh_free_doall(OBJ_NAME *onp)
320{
322 if (onp == NULL) 321 if (onp == NULL)
323 return; 322 return;
324 323
325 if (free_type < 0 || free_type == onp->type) 324 if (free_type < 0 || free_type == onp->type)
326 OBJ_NAME_remove(onp->name,onp->type); 325 OBJ_NAME_remove(onp->name, onp->type);
327 } 326}
328 327
329static IMPLEMENT_LHASH_DOALL_FN(names_lh_free, OBJ_NAME) 328static IMPLEMENT_LHASH_DOALL_FN(names_lh_free, OBJ_NAME)
330 329
331static void name_funcs_free(NAME_FUNCS *ptr) 330static void
332 { 331name_funcs_free(NAME_FUNCS *ptr)
332{
333 free(ptr); 333 free(ptr);
334 } 334}
335 335
336void OBJ_NAME_cleanup(int type) 336void
337 { 337OBJ_NAME_cleanup(int type)
338{
338 unsigned long down_load; 339 unsigned long down_load;
339 340
340 if (names_lh == NULL) return; 341 if (names_lh == NULL)
342 return;
341 343
342 free_type=type; 344 free_type = type;
343 down_load=lh_OBJ_NAME_down_load(names_lh); 345 down_load = lh_OBJ_NAME_down_load(names_lh);
344 lh_OBJ_NAME_down_load(names_lh)=0; 346 lh_OBJ_NAME_down_load(names_lh) = 0;
345 347
346 lh_OBJ_NAME_doall(names_lh,LHASH_DOALL_FN(names_lh_free)); 348 lh_OBJ_NAME_doall(names_lh, LHASH_DOALL_FN(names_lh_free));
347 if (type < 0) 349 if (type < 0) {
348 {
349 lh_OBJ_NAME_free(names_lh); 350 lh_OBJ_NAME_free(names_lh);
350 sk_NAME_FUNCS_pop_free(name_funcs_stack,name_funcs_free); 351 sk_NAME_FUNCS_pop_free(name_funcs_stack, name_funcs_free);
351 names_lh=NULL; 352 names_lh = NULL;
352 name_funcs_stack = NULL; 353 name_funcs_stack = NULL;
353 } 354 } else
354 else 355 lh_OBJ_NAME_down_load(names_lh) = down_load;
355 lh_OBJ_NAME_down_load(names_lh)=down_load; 356}
356 }
357