1/* crypto/stack/stack.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to.  The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 *    notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 *    notice, this list of conditions and the following disclaimer in the
30 *    documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 *    must display the following acknowledgement:
33 *    "This product includes cryptographic software written by
34 *     Eric Young (eay@cryptsoft.com)"
35 *    The word 'cryptographic' can be left out if the rouines from the library
36 *    being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 *    the apps directory (application code) you must include an acknowledgement:
39 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed.  i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59/* Code for stacks
60 * Author - Eric Young v 1.0
61 * 1.2 eay 12-Mar-97 -	Modified sk_find so that it _DOES_ return the
62 *			lowest index for the searched item.
63 *
64 * 1.1 eay - Take from netdb and added to SSLeay
65 *
66 * 1.0 eay - First version 29/07/92
67 */
68#include <stdio.h>
69#include <string.h>
70#include <stdlib.h>
71#include <time.h>
72
73#if 0
74#include "cryptlib.h"
75#include <openssl/stack.h>
76#include <openssl/objects.h>
77#else
78#include "cs-stack.h"
79#include "cs-objects.h"
80#endif
81
82#undef MIN_NODES
83#define MIN_NODES	4
84
85const char STACK_version[]="Stack" /* OPENSSL_VERSION_PTEXT */;
86
87#include <errno.h>
88
89int (*sk_set_cmp_func(STACK *sk, int (*c)(const char * const *,const char * const *)))
90		(const char * const *, const char * const *)
91	{
92	int (*old)(const char * const *,const char * const *)=sk->comp;
93
94	if (sk->comp != c)
95		sk->sorted=0;
96	sk->comp=c;
97
98	return old;
99	}
100
101STACK *sk_dup(STACK *sk)
102	{
103	STACK *ret;
104	char **s;
105
106	if ((ret=sk_new(sk->comp)) == NULL) goto err;
107	s=(char **)realloc((char *)ret->data,
108		(unsigned int)sizeof(char *)*sk->num_alloc);
109	if (s == NULL) goto err;
110	ret->data=s;
111
112	ret->num=sk->num;
113	memcpy(ret->data,sk->data,sizeof(char *)*sk->num);
114	ret->sorted=sk->sorted;
115	ret->num_alloc=sk->num_alloc;
116	ret->comp=sk->comp;
117	return(ret);
118err:
119	if(ret)
120		sk_free(ret);
121	return(NULL);
122	}
123
124STACK *sk_new_null(void)
125	{
126	return sk_new((int (*)(const char * const *, const char * const *))0);
127	}
128
129STACK *sk_new(int (*c)(const char * const *, const char * const *))
130	{
131	STACK *ret;
132	int i;
133
134	if ((ret=(STACK *)malloc(sizeof(STACK))) == NULL)
135		goto err;
136	if ((ret->data=(char **)malloc(sizeof(char *)*MIN_NODES)) == NULL)
137		goto err;
138	for (i=0; i<MIN_NODES; i++)
139		ret->data[i]=NULL;
140	ret->comp=c;
141	ret->num_alloc=MIN_NODES;
142	ret->num=0;
143	ret->sorted=0;
144	return(ret);
145err:
146	if(ret)
147		free(ret);
148	return(NULL);
149	}
150
151int sk_insert(STACK *st, char *data, int loc)
152	{
153	char **s;
154
155	if(st == NULL) return 0;
156	if (st->num_alloc <= st->num+1)
157		{
158		s=(char **)realloc((char *)st->data,
159			(unsigned int)sizeof(char *)*st->num_alloc*2);
160		if (s == NULL)
161			return(0);
162		st->data=s;
163		st->num_alloc*=2;
164		}
165	if ((loc >= (int)st->num) || (loc < 0))
166		st->data[st->num]=data;
167	else
168		{
169		int i;
170		char **f,**t;
171
172		f=(char **)st->data;
173		t=(char **)&(st->data[1]);
174		for (i=st->num; i>=loc; i--)
175			t[i]=f[i];
176
177#ifdef undef /* no memmove on sunos :-( */
178		memmove( (char *)&(st->data[loc+1]),
179			(char *)&(st->data[loc]),
180			sizeof(char *)*(st->num-loc));
181#endif
182		st->data[loc]=data;
183		}
184	st->num++;
185	st->sorted=0;
186	return(st->num);
187	}
188
189char *sk_delete_ptr(STACK *st, char *p)
190	{
191	int i;
192
193	for (i=0; i<st->num; i++)
194		if (st->data[i] == p)
195			return(sk_delete(st,i));
196	return(NULL);
197	}
198
199char *sk_delete(STACK *st, int loc)
200	{
201	char *ret;
202	int i,j;
203
204	if(!st || (loc < 0) || (loc >= st->num)) return NULL;
205
206	ret=st->data[loc];
207	if (loc != st->num-1)
208		{
209		j=st->num-1;
210		for (i=loc; i<j; i++)
211			st->data[i]=st->data[i+1];
212		/* In theory memcpy is not safe for this
213		 * memcpy( &(st->data[loc]),
214		 *	&(st->data[loc+1]),
215		 *	sizeof(char *)*(st->num-loc-1));
216		 */
217		}
218	st->num--;
219	return(ret);
220	}
221
222static int internal_find(STACK *st, char *data, int ret_val_options)
223	{
224	char **r;
225	int i;
226	int (*comp_func)(const void *,const void *);
227	if(st == NULL) return -1;
228
229	if (st->comp == NULL)
230		{
231		for (i=0; i<st->num; i++)
232			if (st->data[i] == data)
233				return(i);
234		return(-1);
235		}
236	sk_sort(st);
237	if (data == NULL) return(-1);
238	/* This (and the "qsort" below) are the two places in OpenSSL
239	 * where we need to convert from our standard (type **,type **)
240	 * compare callback type to the (void *,void *) type required by
241	 * bsearch. However, the "data" it is being called(back) with are
242	 * not (type *) pointers, but the *pointers* to (type *) pointers,
243	 * so we get our extra level of pointer dereferencing that way. */
244	comp_func=(int (*)(const void *,const void *))(st->comp);
245	r=(char **)OBJ_bsearch_ex((char *)&data,(char *)st->data,
246		st->num,sizeof(char *),comp_func,ret_val_options);
247	if (r == NULL) return(-1);
248	return((int)(r-st->data));
249	}
250
251int sk_find(STACK *st, char *data)
252	{
253	return internal_find(st, data, OBJ_BSEARCH_FIRST_VALUE_ON_MATCH);
254	}
255int sk_find_ex(STACK *st, char *data)
256	{
257	return internal_find(st, data, OBJ_BSEARCH_VALUE_ON_NOMATCH);
258	}
259
260int sk_push(STACK *st, char *data)
261	{
262	return(sk_insert(st,data,st->num));
263	}
264
265int sk_unshift(STACK *st, char *data)
266	{
267	return(sk_insert(st,data,0));
268	}
269
270char *sk_shift(STACK *st)
271	{
272	if (st == NULL) return(NULL);
273	if (st->num <= 0) return(NULL);
274	return(sk_delete(st,0));
275	}
276
277char *sk_pop(STACK *st)
278	{
279	if (st == NULL) return(NULL);
280	if (st->num <= 0) return(NULL);
281	return(sk_delete(st,st->num-1));
282	}
283
284void sk_zero(STACK *st)
285	{
286	if (st == NULL) return;
287	if (st->num <= 0) return;
288	memset((char *)st->data,0,sizeof(st->data)*st->num);
289	st->num=0;
290	}
291
292void sk_pop_free(STACK *st, void (*func)(void *))
293	{
294	int i;
295
296	if (st == NULL) return;
297	for (i=0; i<st->num; i++)
298		if (st->data[i] != NULL)
299			func(st->data[i]);
300	sk_free(st);
301	}
302
303void sk_free(STACK *st)
304	{
305	if (st == NULL) return;
306	if (st->data != NULL) free(st->data);
307	free(st);
308	}
309
310int sk_num(const STACK *st)
311{
312	if(st == NULL) return -1;
313	return st->num;
314}
315
316char *sk_value(const STACK *st, int i)
317{
318	if(!st || (i < 0) || (i >= st->num)) return NULL;
319	return st->data[i];
320}
321
322char *sk_set(STACK *st, int i, char *value)
323{
324	if(!st || (i < 0) || (i >= st->num)) return NULL;
325	return (st->data[i] = value);
326}
327
328void sk_sort(STACK *st)
329	{
330	if (st && !st->sorted)
331		{
332		int (*comp_func)(const void *,const void *);
333
334		/* same comment as in sk_find ... previously st->comp was declared
335		 * as a (void*,void*) callback type, but this made the population
336		 * of the callback pointer illogical - our callbacks compare
337		 * type** with type**, so we leave the casting until absolutely
338		 * necessary (ie. "now"). */
339		comp_func=(int (*)(const void *,const void *))(st->comp);
340		qsort(st->data,st->num,sizeof(char *), comp_func);
341		st->sorted=1;
342		}
343	}
344
345int sk_is_sorted(const STACK *st)
346	{
347	if (!st)
348		return 1;
349	return st->sorted;
350	}
351