10Sstevel@tonic-gate /* crypto/stack/stack.c */
20Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
30Sstevel@tonic-gate * All rights reserved.
40Sstevel@tonic-gate *
50Sstevel@tonic-gate * This package is an SSL implementation written
60Sstevel@tonic-gate * by Eric Young (eay@cryptsoft.com).
70Sstevel@tonic-gate * The implementation was written so as to conform with Netscapes SSL.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * This library is free for commercial and non-commercial use as long as
100Sstevel@tonic-gate * the following conditions are aheared to. The following conditions
110Sstevel@tonic-gate * apply to all code found in this distribution, be it the RC4, RSA,
120Sstevel@tonic-gate * lhash, DES, etc., code; not just the SSL code. The SSL documentation
130Sstevel@tonic-gate * included with this distribution is covered by the same copyright terms
140Sstevel@tonic-gate * except that the holder is Tim Hudson (tjh@cryptsoft.com).
150Sstevel@tonic-gate *
160Sstevel@tonic-gate * Copyright remains Eric Young's, and as such any Copyright notices in
170Sstevel@tonic-gate * the code are not to be removed.
180Sstevel@tonic-gate * If this package is used in a product, Eric Young should be given attribution
190Sstevel@tonic-gate * as the author of the parts of the library used.
200Sstevel@tonic-gate * This can be in the form of a textual message at program startup or
210Sstevel@tonic-gate * in documentation (online or textual) provided with the package.
220Sstevel@tonic-gate *
230Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
240Sstevel@tonic-gate * modification, are permitted provided that the following conditions
250Sstevel@tonic-gate * are met:
260Sstevel@tonic-gate * 1. Redistributions of source code must retain the copyright
270Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
280Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
290Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
300Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
310Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software
320Sstevel@tonic-gate * must display the following acknowledgement:
330Sstevel@tonic-gate * "This product includes cryptographic software written by
340Sstevel@tonic-gate * Eric Young (eay@cryptsoft.com)"
350Sstevel@tonic-gate * The word 'cryptographic' can be left out if the rouines from the library
360Sstevel@tonic-gate * being used are not cryptographic related :-).
370Sstevel@tonic-gate * 4. If you include any Windows specific code (or a derivative thereof) from
380Sstevel@tonic-gate * the apps directory (application code) you must include an acknowledgement:
390Sstevel@tonic-gate * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
400Sstevel@tonic-gate *
410Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
420Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
430Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
440Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
450Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
460Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
470Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
480Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
490Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
500Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
510Sstevel@tonic-gate * SUCH DAMAGE.
520Sstevel@tonic-gate *
530Sstevel@tonic-gate * The licence and distribution terms for any publically available version or
540Sstevel@tonic-gate * derivative of this code cannot be changed. i.e. this code cannot simply be
550Sstevel@tonic-gate * copied and put under another distribution licence
560Sstevel@tonic-gate * [including the GNU Public Licence.]
570Sstevel@tonic-gate */
580Sstevel@tonic-gate
590Sstevel@tonic-gate /* Code for stacks
600Sstevel@tonic-gate * Author - Eric Young v 1.0
610Sstevel@tonic-gate * 1.2 eay 12-Mar-97 - Modified sk_find so that it _DOES_ return the
620Sstevel@tonic-gate * lowest index for the searched item.
630Sstevel@tonic-gate *
640Sstevel@tonic-gate * 1.1 eay - Take from netdb and added to SSLeay
650Sstevel@tonic-gate *
660Sstevel@tonic-gate * 1.0 eay - First version 29/07/92
670Sstevel@tonic-gate */
680Sstevel@tonic-gate #include <stdio.h>
690Sstevel@tonic-gate #include "cryptlib.h"
700Sstevel@tonic-gate #include <openssl/stack.h>
71*2139Sjp161948 #include <openssl/objects.h>
720Sstevel@tonic-gate
730Sstevel@tonic-gate #undef MIN_NODES
740Sstevel@tonic-gate #define MIN_NODES 4
750Sstevel@tonic-gate
760Sstevel@tonic-gate const char *STACK_version="Stack" OPENSSL_VERSION_PTEXT;
770Sstevel@tonic-gate
780Sstevel@tonic-gate #include <errno.h>
790Sstevel@tonic-gate
sk_set_cmp_func(STACK * sk,int (* c)(const char * const *,const char * const *))800Sstevel@tonic-gate int (*sk_set_cmp_func(STACK *sk, int (*c)(const char * const *,const char * const *)))
810Sstevel@tonic-gate (const char * const *, const char * const *)
820Sstevel@tonic-gate {
830Sstevel@tonic-gate int (*old)(const char * const *,const char * const *)=sk->comp;
840Sstevel@tonic-gate
850Sstevel@tonic-gate if (sk->comp != c)
860Sstevel@tonic-gate sk->sorted=0;
870Sstevel@tonic-gate sk->comp=c;
880Sstevel@tonic-gate
890Sstevel@tonic-gate return old;
900Sstevel@tonic-gate }
910Sstevel@tonic-gate
sk_dup(STACK * sk)920Sstevel@tonic-gate STACK *sk_dup(STACK *sk)
930Sstevel@tonic-gate {
940Sstevel@tonic-gate STACK *ret;
950Sstevel@tonic-gate char **s;
960Sstevel@tonic-gate
970Sstevel@tonic-gate if ((ret=sk_new(sk->comp)) == NULL) goto err;
980Sstevel@tonic-gate s=(char **)OPENSSL_realloc((char *)ret->data,
990Sstevel@tonic-gate (unsigned int)sizeof(char *)*sk->num_alloc);
1000Sstevel@tonic-gate if (s == NULL) goto err;
1010Sstevel@tonic-gate ret->data=s;
1020Sstevel@tonic-gate
1030Sstevel@tonic-gate ret->num=sk->num;
1040Sstevel@tonic-gate memcpy(ret->data,sk->data,sizeof(char *)*sk->num);
1050Sstevel@tonic-gate ret->sorted=sk->sorted;
1060Sstevel@tonic-gate ret->num_alloc=sk->num_alloc;
1070Sstevel@tonic-gate ret->comp=sk->comp;
1080Sstevel@tonic-gate return(ret);
1090Sstevel@tonic-gate err:
1100Sstevel@tonic-gate if(ret)
1110Sstevel@tonic-gate sk_free(ret);
1120Sstevel@tonic-gate return(NULL);
1130Sstevel@tonic-gate }
1140Sstevel@tonic-gate
sk_new_null(void)1150Sstevel@tonic-gate STACK *sk_new_null(void)
1160Sstevel@tonic-gate {
1170Sstevel@tonic-gate return sk_new((int (*)(const char * const *, const char * const *))0);
1180Sstevel@tonic-gate }
1190Sstevel@tonic-gate
sk_new(int (* c)(const char * const *,const char * const *))1200Sstevel@tonic-gate STACK *sk_new(int (*c)(const char * const *, const char * const *))
1210Sstevel@tonic-gate {
1220Sstevel@tonic-gate STACK *ret;
1230Sstevel@tonic-gate int i;
1240Sstevel@tonic-gate
1250Sstevel@tonic-gate if ((ret=(STACK *)OPENSSL_malloc(sizeof(STACK))) == NULL)
1260Sstevel@tonic-gate goto err;
1270Sstevel@tonic-gate if ((ret->data=(char **)OPENSSL_malloc(sizeof(char *)*MIN_NODES)) == NULL)
1280Sstevel@tonic-gate goto err;
1290Sstevel@tonic-gate for (i=0; i<MIN_NODES; i++)
1300Sstevel@tonic-gate ret->data[i]=NULL;
1310Sstevel@tonic-gate ret->comp=c;
1320Sstevel@tonic-gate ret->num_alloc=MIN_NODES;
1330Sstevel@tonic-gate ret->num=0;
1340Sstevel@tonic-gate ret->sorted=0;
1350Sstevel@tonic-gate return(ret);
1360Sstevel@tonic-gate err:
1370Sstevel@tonic-gate if(ret)
1380Sstevel@tonic-gate OPENSSL_free(ret);
1390Sstevel@tonic-gate return(NULL);
1400Sstevel@tonic-gate }
1410Sstevel@tonic-gate
sk_insert(STACK * st,char * data,int loc)1420Sstevel@tonic-gate int sk_insert(STACK *st, char *data, int loc)
1430Sstevel@tonic-gate {
1440Sstevel@tonic-gate char **s;
1450Sstevel@tonic-gate
1460Sstevel@tonic-gate if(st == NULL) return 0;
1470Sstevel@tonic-gate if (st->num_alloc <= st->num+1)
1480Sstevel@tonic-gate {
1490Sstevel@tonic-gate s=(char **)OPENSSL_realloc((char *)st->data,
1500Sstevel@tonic-gate (unsigned int)sizeof(char *)*st->num_alloc*2);
1510Sstevel@tonic-gate if (s == NULL)
1520Sstevel@tonic-gate return(0);
1530Sstevel@tonic-gate st->data=s;
1540Sstevel@tonic-gate st->num_alloc*=2;
1550Sstevel@tonic-gate }
1560Sstevel@tonic-gate if ((loc >= (int)st->num) || (loc < 0))
1570Sstevel@tonic-gate st->data[st->num]=data;
1580Sstevel@tonic-gate else
1590Sstevel@tonic-gate {
1600Sstevel@tonic-gate int i;
1610Sstevel@tonic-gate char **f,**t;
1620Sstevel@tonic-gate
1630Sstevel@tonic-gate f=(char **)st->data;
1640Sstevel@tonic-gate t=(char **)&(st->data[1]);
1650Sstevel@tonic-gate for (i=st->num; i>=loc; i--)
1660Sstevel@tonic-gate t[i]=f[i];
1670Sstevel@tonic-gate
1680Sstevel@tonic-gate #ifdef undef /* no memmove on sunos :-( */
1690Sstevel@tonic-gate memmove( (char *)&(st->data[loc+1]),
1700Sstevel@tonic-gate (char *)&(st->data[loc]),
1710Sstevel@tonic-gate sizeof(char *)*(st->num-loc));
1720Sstevel@tonic-gate #endif
1730Sstevel@tonic-gate st->data[loc]=data;
1740Sstevel@tonic-gate }
1750Sstevel@tonic-gate st->num++;
1760Sstevel@tonic-gate st->sorted=0;
1770Sstevel@tonic-gate return(st->num);
1780Sstevel@tonic-gate }
1790Sstevel@tonic-gate
sk_delete_ptr(STACK * st,char * p)1800Sstevel@tonic-gate char *sk_delete_ptr(STACK *st, char *p)
1810Sstevel@tonic-gate {
1820Sstevel@tonic-gate int i;
1830Sstevel@tonic-gate
1840Sstevel@tonic-gate for (i=0; i<st->num; i++)
1850Sstevel@tonic-gate if (st->data[i] == p)
1860Sstevel@tonic-gate return(sk_delete(st,i));
1870Sstevel@tonic-gate return(NULL);
1880Sstevel@tonic-gate }
1890Sstevel@tonic-gate
sk_delete(STACK * st,int loc)1900Sstevel@tonic-gate char *sk_delete(STACK *st, int loc)
1910Sstevel@tonic-gate {
1920Sstevel@tonic-gate char *ret;
1930Sstevel@tonic-gate int i,j;
1940Sstevel@tonic-gate
195*2139Sjp161948 if(!st || (loc < 0) || (loc >= st->num)) return NULL;
1960Sstevel@tonic-gate
1970Sstevel@tonic-gate ret=st->data[loc];
1980Sstevel@tonic-gate if (loc != st->num-1)
1990Sstevel@tonic-gate {
2000Sstevel@tonic-gate j=st->num-1;
2010Sstevel@tonic-gate for (i=loc; i<j; i++)
2020Sstevel@tonic-gate st->data[i]=st->data[i+1];
2030Sstevel@tonic-gate /* In theory memcpy is not safe for this
2040Sstevel@tonic-gate * memcpy( &(st->data[loc]),
2050Sstevel@tonic-gate * &(st->data[loc+1]),
2060Sstevel@tonic-gate * sizeof(char *)*(st->num-loc-1));
2070Sstevel@tonic-gate */
2080Sstevel@tonic-gate }
2090Sstevel@tonic-gate st->num--;
2100Sstevel@tonic-gate return(ret);
2110Sstevel@tonic-gate }
2120Sstevel@tonic-gate
internal_find(STACK * st,char * data,int ret_val_options)213*2139Sjp161948 static int internal_find(STACK *st, char *data, int ret_val_options)
2140Sstevel@tonic-gate {
2150Sstevel@tonic-gate char **r;
2160Sstevel@tonic-gate int i;
2170Sstevel@tonic-gate int (*comp_func)(const void *,const void *);
2180Sstevel@tonic-gate if(st == NULL) return -1;
2190Sstevel@tonic-gate
2200Sstevel@tonic-gate if (st->comp == NULL)
2210Sstevel@tonic-gate {
2220Sstevel@tonic-gate for (i=0; i<st->num; i++)
2230Sstevel@tonic-gate if (st->data[i] == data)
2240Sstevel@tonic-gate return(i);
2250Sstevel@tonic-gate return(-1);
2260Sstevel@tonic-gate }
2270Sstevel@tonic-gate sk_sort(st);
2280Sstevel@tonic-gate if (data == NULL) return(-1);
2290Sstevel@tonic-gate /* This (and the "qsort" below) are the two places in OpenSSL
2300Sstevel@tonic-gate * where we need to convert from our standard (type **,type **)
2310Sstevel@tonic-gate * compare callback type to the (void *,void *) type required by
2320Sstevel@tonic-gate * bsearch. However, the "data" it is being called(back) with are
2330Sstevel@tonic-gate * not (type *) pointers, but the *pointers* to (type *) pointers,
2340Sstevel@tonic-gate * so we get our extra level of pointer dereferencing that way. */
2350Sstevel@tonic-gate comp_func=(int (*)(const void *,const void *))(st->comp);
236*2139Sjp161948 r=(char **)OBJ_bsearch_ex((char *)&data,(char *)st->data,
237*2139Sjp161948 st->num,sizeof(char *),comp_func,ret_val_options);
2380Sstevel@tonic-gate if (r == NULL) return(-1);
239*2139Sjp161948 return((int)(r-st->data));
240*2139Sjp161948 }
241*2139Sjp161948
sk_find(STACK * st,char * data)242*2139Sjp161948 int sk_find(STACK *st, char *data)
243*2139Sjp161948 {
244*2139Sjp161948 return internal_find(st, data, OBJ_BSEARCH_FIRST_VALUE_ON_MATCH);
245*2139Sjp161948 }
sk_find_ex(STACK * st,char * data)246*2139Sjp161948 int sk_find_ex(STACK *st, char *data)
247*2139Sjp161948 {
248*2139Sjp161948 return internal_find(st, data, OBJ_BSEARCH_VALUE_ON_NOMATCH);
2490Sstevel@tonic-gate }
2500Sstevel@tonic-gate
sk_push(STACK * st,char * data)2510Sstevel@tonic-gate int sk_push(STACK *st, char *data)
2520Sstevel@tonic-gate {
2530Sstevel@tonic-gate return(sk_insert(st,data,st->num));
2540Sstevel@tonic-gate }
2550Sstevel@tonic-gate
sk_unshift(STACK * st,char * data)2560Sstevel@tonic-gate int sk_unshift(STACK *st, char *data)
2570Sstevel@tonic-gate {
2580Sstevel@tonic-gate return(sk_insert(st,data,0));
2590Sstevel@tonic-gate }
2600Sstevel@tonic-gate
sk_shift(STACK * st)2610Sstevel@tonic-gate char *sk_shift(STACK *st)
2620Sstevel@tonic-gate {
2630Sstevel@tonic-gate if (st == NULL) return(NULL);
2640Sstevel@tonic-gate if (st->num <= 0) return(NULL);
2650Sstevel@tonic-gate return(sk_delete(st,0));
2660Sstevel@tonic-gate }
2670Sstevel@tonic-gate
sk_pop(STACK * st)2680Sstevel@tonic-gate char *sk_pop(STACK *st)
2690Sstevel@tonic-gate {
2700Sstevel@tonic-gate if (st == NULL) return(NULL);
2710Sstevel@tonic-gate if (st->num <= 0) return(NULL);
2720Sstevel@tonic-gate return(sk_delete(st,st->num-1));
2730Sstevel@tonic-gate }
2740Sstevel@tonic-gate
sk_zero(STACK * st)2750Sstevel@tonic-gate void sk_zero(STACK *st)
2760Sstevel@tonic-gate {
2770Sstevel@tonic-gate if (st == NULL) return;
2780Sstevel@tonic-gate if (st->num <= 0) return;
2790Sstevel@tonic-gate memset((char *)st->data,0,sizeof(st->data)*st->num);
2800Sstevel@tonic-gate st->num=0;
2810Sstevel@tonic-gate }
2820Sstevel@tonic-gate
sk_pop_free(STACK * st,void (* func)(void *))2830Sstevel@tonic-gate void sk_pop_free(STACK *st, void (*func)(void *))
2840Sstevel@tonic-gate {
2850Sstevel@tonic-gate int i;
2860Sstevel@tonic-gate
2870Sstevel@tonic-gate if (st == NULL) return;
2880Sstevel@tonic-gate for (i=0; i<st->num; i++)
2890Sstevel@tonic-gate if (st->data[i] != NULL)
2900Sstevel@tonic-gate func(st->data[i]);
2910Sstevel@tonic-gate sk_free(st);
2920Sstevel@tonic-gate }
2930Sstevel@tonic-gate
sk_free(STACK * st)2940Sstevel@tonic-gate void sk_free(STACK *st)
2950Sstevel@tonic-gate {
2960Sstevel@tonic-gate if (st == NULL) return;
2970Sstevel@tonic-gate if (st->data != NULL) OPENSSL_free(st->data);
2980Sstevel@tonic-gate OPENSSL_free(st);
2990Sstevel@tonic-gate }
3000Sstevel@tonic-gate
sk_num(const STACK * st)3010Sstevel@tonic-gate int sk_num(const STACK *st)
3020Sstevel@tonic-gate {
3030Sstevel@tonic-gate if(st == NULL) return -1;
3040Sstevel@tonic-gate return st->num;
3050Sstevel@tonic-gate }
3060Sstevel@tonic-gate
sk_value(const STACK * st,int i)3070Sstevel@tonic-gate char *sk_value(const STACK *st, int i)
3080Sstevel@tonic-gate {
309*2139Sjp161948 if(!st || (i < 0) || (i >= st->num)) return NULL;
3100Sstevel@tonic-gate return st->data[i];
3110Sstevel@tonic-gate }
3120Sstevel@tonic-gate
sk_set(STACK * st,int i,char * value)3130Sstevel@tonic-gate char *sk_set(STACK *st, int i, char *value)
3140Sstevel@tonic-gate {
315*2139Sjp161948 if(!st || (i < 0) || (i >= st->num)) return NULL;
3160Sstevel@tonic-gate return (st->data[i] = value);
3170Sstevel@tonic-gate }
3180Sstevel@tonic-gate
sk_sort(STACK * st)3190Sstevel@tonic-gate void sk_sort(STACK *st)
3200Sstevel@tonic-gate {
3210Sstevel@tonic-gate if (st && !st->sorted)
3220Sstevel@tonic-gate {
3230Sstevel@tonic-gate int (*comp_func)(const void *,const void *);
3240Sstevel@tonic-gate
3250Sstevel@tonic-gate /* same comment as in sk_find ... previously st->comp was declared
3260Sstevel@tonic-gate * as a (void*,void*) callback type, but this made the population
3270Sstevel@tonic-gate * of the callback pointer illogical - our callbacks compare
3280Sstevel@tonic-gate * type** with type**, so we leave the casting until absolutely
3290Sstevel@tonic-gate * necessary (ie. "now"). */
3300Sstevel@tonic-gate comp_func=(int (*)(const void *,const void *))(st->comp);
3310Sstevel@tonic-gate qsort(st->data,st->num,sizeof(char *), comp_func);
3320Sstevel@tonic-gate st->sorted=1;
3330Sstevel@tonic-gate }
3340Sstevel@tonic-gate }
335*2139Sjp161948
sk_is_sorted(const STACK * st)336*2139Sjp161948 int sk_is_sorted(const STACK *st)
337*2139Sjp161948 {
338*2139Sjp161948 if (!st)
339*2139Sjp161948 return 1;
340*2139Sjp161948 return st->sorted;
341*2139Sjp161948 }
342