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