o_names.c 5.3 KB
Newer Older
1 2 3 4
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

5 6
#include <openssl/lhash.h>
#include <openssl/objects.h>
7

8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
union cmp_fn_to_char_u
	{
	char *char_p;
	int (*fn_p)(const char *, const char *);
	};

union hash_fn_to_char_u
	{
	char *char_p;
	unsigned long (*fn_p)(const char *);
	};

union int_fn_to_char_u
	{
	char *char_p;
	int (*fn_p)();
	};

union ulong_fn_to_char_u
	{
	char *char_p;
	unsigned long (*fn_p)();
	};

union void_fn_to_char_u
	{
	char *char_p;
	void (*fn_p)();
	};

38 39 40 41 42 43 44 45 46 47 48 49
/* I use the ex_data stuff to manage the identifiers for the obj_name_types
 * that applications may define.  I only really use the free function field.
 */
static LHASH *names_lh=NULL;
static int names_type_num=OBJ_NAME_TYPE_NUM;
static STACK *names_cmp=NULL;
static STACK *names_hash=NULL;
static STACK *names_free=NULL;

static unsigned long obj_name_hash(OBJ_NAME *a);
static int obj_name_cmp(OBJ_NAME *a,OBJ_NAME *b);

U
Ulf Möller 已提交
50
int OBJ_NAME_init(void)
51 52 53 54 55 56 57 58
	{
	if (names_lh != NULL) return(1);
	MemCheck_off();
	names_lh=lh_new(obj_name_hash,obj_name_cmp);
	MemCheck_on();
	return(names_lh != NULL);
	}

U
Ulf Möller 已提交
59 60
int OBJ_NAME_new_index(unsigned long (*hash_func)(), int (*cmp_func)(),
	     void (*free_func)())
61 62 63
	{
	int ret;
	int i;
64 65 66 67 68 69 70 71 72 73 74
	union ulong_fn_to_char_u tmp_hash_func;
	union int_fn_to_char_u tmp_cmp_func;
	union void_fn_to_char_u tmp_free_func;
	union cmp_fn_to_char_u tmp_strcmp;
	union hash_fn_to_char_u tmp_lh_strhash;

	tmp_hash_func.fn_p = hash_func;
	tmp_cmp_func.fn_p = cmp_func;
	tmp_free_func.fn_p = free_func;
	tmp_strcmp.fn_p = (int (*)(const char *, const char *))strcmp;
	tmp_lh_strhash.fn_p = lh_strhash;
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93

	if (names_free == NULL)
		{
		MemCheck_off();
		names_hash=sk_new_null();
		names_cmp=sk_new_null();
		names_free=sk_new_null();
		MemCheck_on();
		}
	if ((names_free == NULL) || (names_hash == NULL) || (names_cmp == NULL))
		{
		/* ERROR */
		return(0);
		}
	ret=names_type_num;
	names_type_num++;
	for (i=sk_num(names_free); i<names_type_num; i++)
		{
		MemCheck_off();
94 95
		sk_push(names_hash,tmp_strcmp.char_p);
		sk_push(names_cmp,tmp_lh_strhash.char_p);
96 97 98 99
		sk_push(names_free,NULL);
		MemCheck_on();
		}
	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);
105 106 107
	return(ret);
	}

U
Ulf Möller 已提交
108
static int obj_name_cmp(OBJ_NAME *a, OBJ_NAME *b)
109 110
	{
	int ret;
111
	union int_fn_to_char_u cmp;
112 113 114 115 116 117

	ret=a->type-b->type;
	if (ret == 0)
		{
		if ((names_cmp != NULL) && (sk_num(names_cmp) > a->type))
			{
118 119
			cmp.char_p=sk_value(names_cmp,a->type);
			ret=cmp.fn_p(a->name,b->name);
120 121 122 123 124 125 126
			}
		else
			ret=strcmp(a->name,b->name);
		}
	return(ret);
	}

U
Ulf Möller 已提交
127
static unsigned long obj_name_hash(OBJ_NAME *a)
128 129
	{
	unsigned long ret;
130
	union ulong_fn_to_char_u hash;
131 132 133

	if ((names_hash != NULL) && (sk_num(names_hash) > a->type))
		{
134 135
		hash.char_p=sk_value(names_hash,a->type);
		ret=hash.fn_p(a->name);
136 137 138 139 140 141 142 143 144
		}
	else
		{
		ret=lh_strhash(a->name);
		}
	ret^=a->type;
	return(ret);
	}

U
Ulf Möller 已提交
145
const char *OBJ_NAME_get(const char *name, int type)
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
	{
	OBJ_NAME on,*ret;
	int num=0,alias;

	if (name == NULL) return(NULL);
	if ((names_lh == NULL) && !OBJ_NAME_init()) return(NULL);

	alias=type&OBJ_NAME_ALIAS;
	type&= ~OBJ_NAME_ALIAS;

	on.name=name;
	on.type=type;

	for (;;)
		{
		ret=(OBJ_NAME *)lh_retrieve(names_lh,(char *)&on);
		if (ret == NULL) return(NULL);
		if ((ret->alias) && !alias)
			{
			if (++num > 10) return(NULL);
			on.name=ret->data;
			}
		else
			{
			return(ret->data);
			}
		}
	}

U
Ulf Möller 已提交
175
int OBJ_NAME_add(const char *name, int type, const char *data)
176
	{
177
	union void_fn_to_char_u f;
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
	OBJ_NAME *onp,*ret;
	int alias;

	if ((names_lh == NULL) && !OBJ_NAME_init()) return(0);

	alias=type&OBJ_NAME_ALIAS;
	type&= ~OBJ_NAME_ALIAS;

	onp=(OBJ_NAME *)Malloc(sizeof(OBJ_NAME));
	if (onp == NULL)
		{
		/* ERROR */
		return(0);
		}

	onp->name=name;
	onp->alias=alias;
	onp->type=type;
	onp->data=data;

	ret=(OBJ_NAME *)lh_insert(names_lh,(char *)onp);
	if (ret != NULL)
		{
		/* free things */
		if ((names_free != NULL) && (sk_num(names_free) > ret->type))
			{
204 205
			f.char_p=sk_value(names_free,ret->type);
			f.fn_p(ret->name,ret->type,ret->data);
206 207 208 209 210 211 212 213 214 215 216 217 218 219
			}
		Free((char *)ret);
		}
	else
		{
		if (lh_error(names_lh))
			{
			/* ERROR */
			return(0);
			}
		}
	return(1);
	}

U
Ulf Möller 已提交
220
int OBJ_NAME_remove(const char *name, int type)
221 222
	{
	OBJ_NAME on,*ret;
223
	union void_fn_to_char_u f;
224 225 226 227 228 229 230 231 232 233 234 235

	if (names_lh == NULL) return(0);

	type&= ~OBJ_NAME_ALIAS;
	on.name=name;
	on.type=type;
	ret=(OBJ_NAME *)lh_delete(names_lh,(char *)&on);
	if (ret != NULL)
		{
		/* free things */
		if ((names_free != NULL) && (sk_num(names_free) > type))
			{
236 237
			f.char_p=sk_value(names_free,type);
			f.fn_p(ret->name,ret->type,ret->data);
238 239 240 241 242 243 244 245 246 247
			}
		Free((char *)ret);
		return(1);
		}
	else
		return(0);
	}

static int free_type;

U
Ulf Möller 已提交
248 249
static void names_lh_free(OBJ_NAME *onp, int type)
{
B
Ben Laurie 已提交
250 251 252
	if(onp == NULL)
	    return;

253 254 255 256 257 258
	if ((free_type < 0) || (free_type == onp->type))
		{
		OBJ_NAME_remove(onp->name,onp->type);
		}
	}

U
Ulf Möller 已提交
259
void OBJ_NAME_cleanup(int type)
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
	{
	unsigned long down_load;

	if (names_lh == NULL) return;

	free_type=type;
	down_load=names_lh->down_load;
	names_lh->down_load=0;

	lh_doall(names_lh,names_lh_free);
	if (type < 0)
		{
		lh_free(names_lh);
		sk_free(names_hash);
		sk_free(names_cmp);
		sk_free(names_free);
		names_lh=NULL;
		names_hash=NULL;
		names_cmp=NULL;
		names_free=NULL;
		}
	else
		names_lh->down_load=down_load;
	}