os/ossrv/ssl/libcrypto/src/crypto/stack/stack.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/ssl/libcrypto/src/crypto/stack/stack.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,340 @@
     1.4 +/* crypto/stack/stack.c */
     1.5 +/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
     1.6 + * All rights reserved.
     1.7 + *
     1.8 + * This package is an SSL implementation written
     1.9 + * by Eric Young (eay@cryptsoft.com).
    1.10 + * The implementation was written so as to conform with Netscapes SSL.
    1.11 + * 
    1.12 + * This library is free for commercial and non-commercial use as long as
    1.13 + * the following conditions are aheared to.  The following conditions
    1.14 + * apply to all code found in this distribution, be it the RC4, RSA,
    1.15 + * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
    1.16 + * included with this distribution is covered by the same copyright terms
    1.17 + * except that the holder is Tim Hudson (tjh@cryptsoft.com).
    1.18 + * 
    1.19 + * Copyright remains Eric Young's, and as such any Copyright notices in
    1.20 + * the code are not to be removed.
    1.21 + * If this package is used in a product, Eric Young should be given attribution
    1.22 + * as the author of the parts of the library used.
    1.23 + * This can be in the form of a textual message at program startup or
    1.24 + * in documentation (online or textual) provided with the package.
    1.25 + * 
    1.26 + * Redistribution and use in source and binary forms, with or without
    1.27 + * modification, are permitted provided that the following conditions
    1.28 + * are met:
    1.29 + * 1. Redistributions of source code must retain the copyright
    1.30 + *    notice, this list of conditions and the following disclaimer.
    1.31 + * 2. Redistributions in binary form must reproduce the above copyright
    1.32 + *    notice, this list of conditions and the following disclaimer in the
    1.33 + *    documentation and/or other materials provided with the distribution.
    1.34 + * 3. All advertising materials mentioning features or use of this software
    1.35 + *    must display the following acknowledgement:
    1.36 + *    "This product includes cryptographic software written by
    1.37 + *     Eric Young (eay@cryptsoft.com)"
    1.38 + *    The word 'cryptographic' can be left out if the rouines from the library
    1.39 + *    being used are not cryptographic related :-).
    1.40 + * 4. If you include any Windows specific code (or a derivative thereof) from 
    1.41 + *    the apps directory (application code) you must include an acknowledgement:
    1.42 + *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
    1.43 + * 
    1.44 + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
    1.45 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.46 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.47 + * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
    1.48 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    1.49 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    1.50 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    1.51 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    1.52 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    1.53 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    1.54 + * SUCH DAMAGE.
    1.55 + * 
    1.56 + * The licence and distribution terms for any publically available version or
    1.57 + * derivative of this code cannot be changed.  i.e. this code cannot simply be
    1.58 + * copied and put under another distribution licence
    1.59 + * [including the GNU Public Licence.]
    1.60 + */
    1.61 +
    1.62 +/* Code for stacks
    1.63 + * Author - Eric Young v 1.0
    1.64 + * 1.2 eay 12-Mar-97 -	Modified sk_find so that it _DOES_ return the
    1.65 + *			lowest index for the searched item.
    1.66 + *
    1.67 + * 1.1 eay - Take from netdb and added to SSLeay
    1.68 + *
    1.69 + * 1.0 eay - First version 29/07/92
    1.70 + */
    1.71 +#include <stdio.h>
    1.72 +#include "cryptlib.h"
    1.73 +#include <openssl/stack.h>
    1.74 +#include <openssl/objects.h>
    1.75 +
    1.76 +#undef MIN_NODES
    1.77 +#define MIN_NODES	4
    1.78 +
    1.79 +const char STACK_version[]="Stack" OPENSSL_VERSION_PTEXT;
    1.80 +
    1.81 +#include <errno.h>
    1.82 +
    1.83 +EXPORT_C  int (*sk_set_cmp_func(STACK *sk, int (*c)(const char * const *,const char * const *)))
    1.84 +		(const char * const *, const char * const *)
    1.85 +	{
    1.86 +	int (*old)(const char * const *,const char * const *)=sk->comp;
    1.87 +
    1.88 +	if (sk->comp != c)
    1.89 +		sk->sorted=0;
    1.90 +	sk->comp=c;
    1.91 +
    1.92 +	return old;
    1.93 +	}
    1.94 +
    1.95 +EXPORT_C STACK *sk_dup(STACK *sk)
    1.96 +	{
    1.97 +	STACK *ret;
    1.98 +	char **s;
    1.99 +
   1.100 +	if ((ret=sk_new(sk->comp)) == NULL) goto err;
   1.101 +	s=(char **)OPENSSL_realloc((char *)ret->data,
   1.102 +		(unsigned int)sizeof(char *)*sk->num_alloc);
   1.103 +	if (s == NULL) goto err;
   1.104 +	ret->data=s;
   1.105 +
   1.106 +	ret->num=sk->num;
   1.107 +	memcpy(ret->data,sk->data,sizeof(char *)*sk->num);
   1.108 +	ret->sorted=sk->sorted;
   1.109 +	ret->num_alloc=sk->num_alloc;
   1.110 +	ret->comp=sk->comp;
   1.111 +	return(ret);
   1.112 +err:
   1.113 +	if(ret)
   1.114 +		sk_free(ret);
   1.115 +	return(NULL);
   1.116 +	}
   1.117 +
   1.118 +EXPORT_C STACK *sk_new_null(void)
   1.119 +	{
   1.120 +	return sk_new((int (*)(const char * const *, const char * const *))0);
   1.121 +	}
   1.122 +
   1.123 +EXPORT_C STACK *sk_new(int (*c)(const char * const *, const char * const *))
   1.124 +	{
   1.125 +	STACK *ret;
   1.126 +	int i;
   1.127 +
   1.128 +	if ((ret=(STACK *)OPENSSL_malloc(sizeof(STACK))) == NULL)
   1.129 +		goto err;
   1.130 +	if ((ret->data=(char **)OPENSSL_malloc(sizeof(char *)*MIN_NODES)) == NULL)
   1.131 +		goto err;
   1.132 +	for (i=0; i<MIN_NODES; i++)
   1.133 +		ret->data[i]=NULL;
   1.134 +	ret->comp=c;
   1.135 +	ret->num_alloc=MIN_NODES;
   1.136 +	ret->num=0;
   1.137 +	ret->sorted=0;
   1.138 +	return(ret);
   1.139 +err:
   1.140 +	if(ret)
   1.141 +		OPENSSL_free(ret);
   1.142 +	return(NULL);
   1.143 +	}
   1.144 +
   1.145 +EXPORT_C int sk_insert(STACK *st, char *data, int loc)
   1.146 +	{
   1.147 +	char **s;
   1.148 +
   1.149 +	if(st == NULL) return 0;
   1.150 +	if (st->num_alloc <= st->num+1)
   1.151 +		{
   1.152 +		s=(char **)OPENSSL_realloc((char *)st->data,
   1.153 +			(unsigned int)sizeof(char *)*st->num_alloc*2);
   1.154 +		if (s == NULL)
   1.155 +			return(0);
   1.156 +		st->data=s;
   1.157 +		st->num_alloc*=2;
   1.158 +		}
   1.159 +	if ((loc >= (int)st->num) || (loc < 0))
   1.160 +		st->data[st->num]=data;
   1.161 +	else
   1.162 +		{
   1.163 +		int i;
   1.164 +		char **f,**t;
   1.165 +
   1.166 +		f=(char **)st->data;
   1.167 +		t=(char **)&(st->data[1]);
   1.168 +		for (i=st->num; i>=loc; i--)
   1.169 +			t[i]=f[i];
   1.170 +			
   1.171 +#ifdef undef /* no memmove on sunos :-( */
   1.172 +		memmove( (char *)&(st->data[loc+1]),
   1.173 +			(char *)&(st->data[loc]),
   1.174 +			sizeof(char *)*(st->num-loc));
   1.175 +#endif
   1.176 +		st->data[loc]=data;
   1.177 +		}
   1.178 +	st->num++;
   1.179 +	st->sorted=0;
   1.180 +	return(st->num);
   1.181 +	}
   1.182 +
   1.183 +EXPORT_C char *sk_delete_ptr(STACK *st, char *p)
   1.184 +	{
   1.185 +	int i;
   1.186 +
   1.187 +	for (i=0; i<st->num; i++)
   1.188 +		if (st->data[i] == p)
   1.189 +			return(sk_delete(st,i));
   1.190 +	return(NULL);
   1.191 +	}
   1.192 +
   1.193 +EXPORT_C char *sk_delete(STACK *st, int loc)
   1.194 +	{
   1.195 +	char *ret;
   1.196 +	int i,j;
   1.197 +
   1.198 +	if(!st || (loc < 0) || (loc >= st->num)) return NULL;
   1.199 +
   1.200 +	ret=st->data[loc];
   1.201 +	if (loc != st->num-1)
   1.202 +		{
   1.203 +		j=st->num-1;
   1.204 +		for (i=loc; i<j; i++)
   1.205 +			st->data[i]=st->data[i+1];
   1.206 +		/* In theory memcpy is not safe for this
   1.207 +		 * memcpy( &(st->data[loc]),
   1.208 +		 *	&(st->data[loc+1]),
   1.209 +		 *	sizeof(char *)*(st->num-loc-1));
   1.210 +		 */
   1.211 +		}
   1.212 +	st->num--;
   1.213 +	return(ret);
   1.214 +	}
   1.215 +
   1.216 +static int internal_find(STACK *st, char *data, int ret_val_options)
   1.217 +	{
   1.218 +	char **r;
   1.219 +	int i;
   1.220 +	int (*comp_func)(const void *,const void *);
   1.221 +	if(st == NULL) return -1;
   1.222 +
   1.223 +	if (st->comp == NULL)
   1.224 +		{
   1.225 +		for (i=0; i<st->num; i++)
   1.226 +			if (st->data[i] == data)
   1.227 +				return(i);
   1.228 +		return(-1);
   1.229 +		}
   1.230 +	sk_sort(st);
   1.231 +	if (data == NULL) return(-1);
   1.232 +	/* This (and the "qsort" below) are the two places in OpenSSL
   1.233 +	 * where we need to convert from our standard (type **,type **)
   1.234 +	 * compare callback type to the (void *,void *) type required by
   1.235 +	 * bsearch. However, the "data" it is being called(back) with are
   1.236 +	 * not (type *) pointers, but the *pointers* to (type *) pointers,
   1.237 +	 * so we get our extra level of pointer dereferencing that way. */
   1.238 +	comp_func=(int (*)(const void *,const void *))(st->comp);
   1.239 +	r=(char **)OBJ_bsearch_ex((char *)&data,(char *)st->data,
   1.240 +		st->num,sizeof(char *),comp_func,ret_val_options);
   1.241 +	if (r == NULL) return(-1);
   1.242 +	return((int)(r-st->data));
   1.243 +	}
   1.244 +
   1.245 +EXPORT_C int sk_find(STACK *st, char *data)
   1.246 +	{
   1.247 +	return internal_find(st, data, OBJ_BSEARCH_FIRST_VALUE_ON_MATCH);
   1.248 +	}
   1.249 +EXPORT_C int sk_find_ex(STACK *st, char *data)
   1.250 +	{
   1.251 +	return internal_find(st, data, OBJ_BSEARCH_VALUE_ON_NOMATCH);
   1.252 +	}
   1.253 +
   1.254 +EXPORT_C int sk_push(STACK *st, char *data)
   1.255 +	{
   1.256 +	return(sk_insert(st,data,st->num));
   1.257 +	}
   1.258 +
   1.259 +EXPORT_C int sk_unshift(STACK *st, char *data)
   1.260 +	{
   1.261 +	return(sk_insert(st,data,0));
   1.262 +	}
   1.263 +
   1.264 +EXPORT_C char *sk_shift(STACK *st)
   1.265 +	{
   1.266 +	if (st == NULL) return(NULL);
   1.267 +	if (st->num <= 0) return(NULL);
   1.268 +	return(sk_delete(st,0));
   1.269 +	}
   1.270 +
   1.271 +EXPORT_C char *sk_pop(STACK *st)
   1.272 +	{
   1.273 +	if (st == NULL) return(NULL);
   1.274 +	if (st->num <= 0) return(NULL);
   1.275 +	return(sk_delete(st,st->num-1));
   1.276 +	}
   1.277 +
   1.278 +EXPORT_C void sk_zero(STACK *st)
   1.279 +	{
   1.280 +	if (st == NULL) return;
   1.281 +	if (st->num <= 0) return;
   1.282 +	memset((char *)st->data,0,sizeof(st->data)*st->num);
   1.283 +	st->num=0;
   1.284 +	}
   1.285 +
   1.286 +EXPORT_C void sk_pop_free(STACK *st, void (*func)(void *))
   1.287 +	{
   1.288 +	int i;
   1.289 +
   1.290 +	if (st == NULL) return;
   1.291 +	for (i=0; i<st->num; i++)
   1.292 +		if (st->data[i] != NULL)
   1.293 +			func(st->data[i]);
   1.294 +	sk_free(st);
   1.295 +	}
   1.296 +
   1.297 +EXPORT_C void sk_free(STACK *st)
   1.298 +	{
   1.299 +	if (st == NULL) return;
   1.300 +	if (st->data != NULL) OPENSSL_free(st->data);
   1.301 +	OPENSSL_free(st);
   1.302 +	}
   1.303 +
   1.304 +EXPORT_C int sk_num(const STACK *st)
   1.305 +{
   1.306 +	if(st == NULL) return -1;
   1.307 +	return st->num;
   1.308 +}
   1.309 +
   1.310 +EXPORT_C char *sk_value(const STACK *st, int i)
   1.311 +{
   1.312 +	if(!st || (i < 0) || (i >= st->num)) return NULL;
   1.313 +	return st->data[i];
   1.314 +}
   1.315 +
   1.316 +EXPORT_C char *sk_set(STACK *st, int i, char *value)
   1.317 +{
   1.318 +	if(!st || (i < 0) || (i >= st->num)) return NULL;
   1.319 +	return (st->data[i] = value);
   1.320 +}
   1.321 +
   1.322 +EXPORT_C void sk_sort(STACK *st)
   1.323 +	{
   1.324 +	if (st && !st->sorted)
   1.325 +		{
   1.326 +		int (*comp_func)(const void *,const void *);
   1.327 +
   1.328 +		/* same comment as in sk_find ... previously st->comp was declared
   1.329 +		 * as a (void*,void*) callback type, but this made the population
   1.330 +		 * of the callback pointer illogical - our callbacks compare
   1.331 +		 * type** with type**, so we leave the casting until absolutely
   1.332 +		 * necessary (ie. "now"). */
   1.333 +		comp_func=(int (*)(const void *,const void *))(st->comp);
   1.334 +		qsort(st->data,st->num,sizeof(char *), comp_func);
   1.335 +		st->sorted=1;
   1.336 +		}
   1.337 +	}
   1.338 +EXPORT_C int sk_is_sorted(const STACK *st)
   1.339 +	{
   1.340 +	if (!st)
   1.341 +		return 1;
   1.342 +	return st->sorted;
   1.343 +	}