5 #include <openssl/lhash.h>
6 #include <openssl/objects.h>
11 int (*fn_p)(const char *, const char *);
14 union hash_fn_to_char_u
17 unsigned long (*fn_p)(const char *);
20 union int_fn_to_char_u
26 union ulong_fn_to_char_u
29 unsigned long (*fn_p)();
32 union void_fn_to_char_u
38 /* I use the ex_data stuff to manage the identifiers for the obj_name_types
39 * that applications may define. I only really use the free function field.
41 static LHASH *names_lh=NULL;
42 static int names_type_num=OBJ_NAME_TYPE_NUM;
43 static STACK *names_cmp=NULL;
44 static STACK *names_hash=NULL;
45 static STACK *names_free=NULL;
47 static unsigned long obj_name_hash(OBJ_NAME *a);
48 static int obj_name_cmp(OBJ_NAME *a,OBJ_NAME *b);
50 int OBJ_NAME_init(void)
52 if (names_lh != NULL) return(1);
54 names_lh=lh_new(obj_name_hash,obj_name_cmp);
56 return(names_lh != NULL);
59 int OBJ_NAME_new_index(unsigned long (*hash_func)(), int (*cmp_func)(),
64 union ulong_fn_to_char_u tmp_hash_func;
65 union int_fn_to_char_u tmp_cmp_func;
66 union void_fn_to_char_u tmp_free_func;
67 union cmp_fn_to_char_u tmp_strcmp;
68 union hash_fn_to_char_u tmp_lh_strhash;
70 tmp_hash_func.fn_p = hash_func;
71 tmp_cmp_func.fn_p = cmp_func;
72 tmp_free_func.fn_p = free_func;
73 tmp_strcmp.fn_p = (int (*)(const char *, const char *))strcmp;
74 tmp_lh_strhash.fn_p = lh_strhash;
76 if (names_free == NULL)
79 names_hash=sk_new_null();
80 names_cmp=sk_new_null();
81 names_free=sk_new_null();
84 if ((names_free == NULL) || (names_hash == NULL) || (names_cmp == NULL))
91 for (i=sk_num(names_free); i<names_type_num; i++)
94 sk_push(names_hash,tmp_strcmp.char_p);
95 sk_push(names_cmp,tmp_lh_strhash.char_p);
96 sk_push(names_free,NULL);
99 if (hash_func != NULL)
100 sk_set(names_hash,ret,tmp_hash_func.char_p);
101 if (cmp_func != NULL)
102 sk_set(names_cmp,ret,tmp_cmp_func.char_p);
103 if (free_func != NULL)
104 sk_set(names_free,ret,tmp_free_func.char_p);
108 static int obj_name_cmp(OBJ_NAME *a, OBJ_NAME *b)
111 union int_fn_to_char_u cmp;
116 if ((names_cmp != NULL) && (sk_num(names_cmp) > a->type))
118 cmp.char_p=sk_value(names_cmp,a->type);
119 ret=cmp.fn_p(a->name,b->name);
122 ret=strcmp(a->name,b->name);
127 static unsigned long obj_name_hash(OBJ_NAME *a)
130 union ulong_fn_to_char_u hash;
132 if ((names_hash != NULL) && (sk_num(names_hash) > a->type))
134 hash.char_p=sk_value(names_hash,a->type);
135 ret=hash.fn_p(a->name);
139 ret=lh_strhash(a->name);
145 const char *OBJ_NAME_get(const char *name, int type)
150 if (name == NULL) return(NULL);
151 if ((names_lh == NULL) && !OBJ_NAME_init()) return(NULL);
153 alias=type&OBJ_NAME_ALIAS;
154 type&= ~OBJ_NAME_ALIAS;
161 ret=(OBJ_NAME *)lh_retrieve(names_lh,(char *)&on);
162 if (ret == NULL) return(NULL);
163 if ((ret->alias) && !alias)
165 if (++num > 10) return(NULL);
175 int OBJ_NAME_add(const char *name, int type, const char *data)
177 union void_fn_to_char_u f;
181 if ((names_lh == NULL) && !OBJ_NAME_init()) return(0);
183 alias=type&OBJ_NAME_ALIAS;
184 type&= ~OBJ_NAME_ALIAS;
186 onp=(OBJ_NAME *)Malloc(sizeof(OBJ_NAME));
198 ret=(OBJ_NAME *)lh_insert(names_lh,(char *)onp);
202 if ((names_free != NULL) && (sk_num(names_free) > ret->type))
204 f.char_p=sk_value(names_free,ret->type);
205 f.fn_p(ret->name,ret->type,ret->data);
211 if (lh_error(names_lh))
220 int OBJ_NAME_remove(const char *name, int type)
223 union void_fn_to_char_u f;
225 if (names_lh == NULL) return(0);
227 type&= ~OBJ_NAME_ALIAS;
230 ret=(OBJ_NAME *)lh_delete(names_lh,(char *)&on);
234 if ((names_free != NULL) && (sk_num(names_free) > type))
236 f.char_p=sk_value(names_free,type);
237 f.fn_p(ret->name,ret->type,ret->data);
246 static int free_type;
248 static void names_lh_free(OBJ_NAME *onp, int type)
253 if ((free_type < 0) || (free_type == onp->type))
255 OBJ_NAME_remove(onp->name,onp->type);
259 void OBJ_NAME_cleanup(int type)
261 unsigned long down_load;
263 if (names_lh == NULL) return;
266 down_load=names_lh->down_load;
267 names_lh->down_load=0;
269 lh_doall(names_lh,names_lh_free);
282 names_lh->down_load=down_load;