sl@0: /* crypto/stack/stack.c */ sl@0: /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) sl@0: * All rights reserved. sl@0: * sl@0: * This package is an SSL implementation written sl@0: * by Eric Young (eay@cryptsoft.com). sl@0: * The implementation was written so as to conform with Netscapes SSL. sl@0: * sl@0: * This library is free for commercial and non-commercial use as long as sl@0: * the following conditions are aheared to. The following conditions sl@0: * apply to all code found in this distribution, be it the RC4, RSA, sl@0: * lhash, DES, etc., code; not just the SSL code. The SSL documentation sl@0: * included with this distribution is covered by the same copyright terms sl@0: * except that the holder is Tim Hudson (tjh@cryptsoft.com). sl@0: * sl@0: * Copyright remains Eric Young's, and as such any Copyright notices in sl@0: * the code are not to be removed. sl@0: * If this package is used in a product, Eric Young should be given attribution sl@0: * as the author of the parts of the library used. sl@0: * This can be in the form of a textual message at program startup or sl@0: * in documentation (online or textual) provided with the package. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * 1. Redistributions of source code must retain the copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in the sl@0: * documentation and/or other materials provided with the distribution. sl@0: * 3. All advertising materials mentioning features or use of this software sl@0: * must display the following acknowledgement: sl@0: * "This product includes cryptographic software written by sl@0: * Eric Young (eay@cryptsoft.com)" sl@0: * The word 'cryptographic' can be left out if the rouines from the library sl@0: * being used are not cryptographic related :-). sl@0: * 4. If you include any Windows specific code (or a derivative thereof) from sl@0: * the apps directory (application code) you must include an acknowledgement: sl@0: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND sl@0: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE sl@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE sl@0: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL sl@0: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS sl@0: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT sl@0: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY sl@0: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF sl@0: * SUCH DAMAGE. sl@0: * sl@0: * The licence and distribution terms for any publically available version or sl@0: * derivative of this code cannot be changed. i.e. this code cannot simply be sl@0: * copied and put under another distribution licence sl@0: * [including the GNU Public Licence.] sl@0: */ sl@0: sl@0: /* Code for stacks sl@0: * Author - Eric Young v 1.0 sl@0: * 1.2 eay 12-Mar-97 - Modified sk_find so that it _DOES_ return the sl@0: * lowest index for the searched item. sl@0: * sl@0: * 1.1 eay - Take from netdb and added to SSLeay sl@0: * sl@0: * 1.0 eay - First version 29/07/92 sl@0: */ sl@0: #include sl@0: #include "cryptlib.h" sl@0: #include sl@0: #include sl@0: sl@0: #undef MIN_NODES sl@0: #define MIN_NODES 4 sl@0: sl@0: const char STACK_version[]="Stack" OPENSSL_VERSION_PTEXT; sl@0: sl@0: #include sl@0: sl@0: EXPORT_C int (*sk_set_cmp_func(STACK *sk, int (*c)(const char * const *,const char * const *))) sl@0: (const char * const *, const char * const *) sl@0: { sl@0: int (*old)(const char * const *,const char * const *)=sk->comp; sl@0: sl@0: if (sk->comp != c) sl@0: sk->sorted=0; sl@0: sk->comp=c; sl@0: sl@0: return old; sl@0: } sl@0: sl@0: EXPORT_C STACK *sk_dup(STACK *sk) sl@0: { sl@0: STACK *ret; sl@0: char **s; sl@0: sl@0: if ((ret=sk_new(sk->comp)) == NULL) goto err; sl@0: s=(char **)OPENSSL_realloc((char *)ret->data, sl@0: (unsigned int)sizeof(char *)*sk->num_alloc); sl@0: if (s == NULL) goto err; sl@0: ret->data=s; sl@0: sl@0: ret->num=sk->num; sl@0: memcpy(ret->data,sk->data,sizeof(char *)*sk->num); sl@0: ret->sorted=sk->sorted; sl@0: ret->num_alloc=sk->num_alloc; sl@0: ret->comp=sk->comp; sl@0: return(ret); sl@0: err: sl@0: if(ret) sl@0: sk_free(ret); sl@0: return(NULL); sl@0: } sl@0: sl@0: EXPORT_C STACK *sk_new_null(void) sl@0: { sl@0: return sk_new((int (*)(const char * const *, const char * const *))0); sl@0: } sl@0: sl@0: EXPORT_C STACK *sk_new(int (*c)(const char * const *, const char * const *)) sl@0: { sl@0: STACK *ret; sl@0: int i; sl@0: sl@0: if ((ret=(STACK *)OPENSSL_malloc(sizeof(STACK))) == NULL) sl@0: goto err; sl@0: if ((ret->data=(char **)OPENSSL_malloc(sizeof(char *)*MIN_NODES)) == NULL) sl@0: goto err; sl@0: for (i=0; idata[i]=NULL; sl@0: ret->comp=c; sl@0: ret->num_alloc=MIN_NODES; sl@0: ret->num=0; sl@0: ret->sorted=0; sl@0: return(ret); sl@0: err: sl@0: if(ret) sl@0: OPENSSL_free(ret); sl@0: return(NULL); sl@0: } sl@0: sl@0: EXPORT_C int sk_insert(STACK *st, char *data, int loc) sl@0: { sl@0: char **s; sl@0: sl@0: if(st == NULL) return 0; sl@0: if (st->num_alloc <= st->num+1) sl@0: { sl@0: s=(char **)OPENSSL_realloc((char *)st->data, sl@0: (unsigned int)sizeof(char *)*st->num_alloc*2); sl@0: if (s == NULL) sl@0: return(0); sl@0: st->data=s; sl@0: st->num_alloc*=2; sl@0: } sl@0: if ((loc >= (int)st->num) || (loc < 0)) sl@0: st->data[st->num]=data; sl@0: else sl@0: { sl@0: int i; sl@0: char **f,**t; sl@0: sl@0: f=(char **)st->data; sl@0: t=(char **)&(st->data[1]); sl@0: for (i=st->num; i>=loc; i--) sl@0: t[i]=f[i]; sl@0: sl@0: #ifdef undef /* no memmove on sunos :-( */ sl@0: memmove( (char *)&(st->data[loc+1]), sl@0: (char *)&(st->data[loc]), sl@0: sizeof(char *)*(st->num-loc)); sl@0: #endif sl@0: st->data[loc]=data; sl@0: } sl@0: st->num++; sl@0: st->sorted=0; sl@0: return(st->num); sl@0: } sl@0: sl@0: EXPORT_C char *sk_delete_ptr(STACK *st, char *p) sl@0: { sl@0: int i; sl@0: sl@0: for (i=0; inum; i++) sl@0: if (st->data[i] == p) sl@0: return(sk_delete(st,i)); sl@0: return(NULL); sl@0: } sl@0: sl@0: EXPORT_C char *sk_delete(STACK *st, int loc) sl@0: { sl@0: char *ret; sl@0: int i,j; sl@0: sl@0: if(!st || (loc < 0) || (loc >= st->num)) return NULL; sl@0: sl@0: ret=st->data[loc]; sl@0: if (loc != st->num-1) sl@0: { sl@0: j=st->num-1; sl@0: for (i=loc; idata[i]=st->data[i+1]; sl@0: /* In theory memcpy is not safe for this sl@0: * memcpy( &(st->data[loc]), sl@0: * &(st->data[loc+1]), sl@0: * sizeof(char *)*(st->num-loc-1)); sl@0: */ sl@0: } sl@0: st->num--; sl@0: return(ret); sl@0: } sl@0: sl@0: static int internal_find(STACK *st, char *data, int ret_val_options) sl@0: { sl@0: char **r; sl@0: int i; sl@0: int (*comp_func)(const void *,const void *); sl@0: if(st == NULL) return -1; sl@0: sl@0: if (st->comp == NULL) sl@0: { sl@0: for (i=0; inum; i++) sl@0: if (st->data[i] == data) sl@0: return(i); sl@0: return(-1); sl@0: } sl@0: sk_sort(st); sl@0: if (data == NULL) return(-1); sl@0: /* This (and the "qsort" below) are the two places in OpenSSL sl@0: * where we need to convert from our standard (type **,type **) sl@0: * compare callback type to the (void *,void *) type required by sl@0: * bsearch. However, the "data" it is being called(back) with are sl@0: * not (type *) pointers, but the *pointers* to (type *) pointers, sl@0: * so we get our extra level of pointer dereferencing that way. */ sl@0: comp_func=(int (*)(const void *,const void *))(st->comp); sl@0: r=(char **)OBJ_bsearch_ex((char *)&data,(char *)st->data, sl@0: st->num,sizeof(char *),comp_func,ret_val_options); sl@0: if (r == NULL) return(-1); sl@0: return((int)(r-st->data)); sl@0: } sl@0: sl@0: EXPORT_C int sk_find(STACK *st, char *data) sl@0: { sl@0: return internal_find(st, data, OBJ_BSEARCH_FIRST_VALUE_ON_MATCH); sl@0: } sl@0: EXPORT_C int sk_find_ex(STACK *st, char *data) sl@0: { sl@0: return internal_find(st, data, OBJ_BSEARCH_VALUE_ON_NOMATCH); sl@0: } sl@0: sl@0: EXPORT_C int sk_push(STACK *st, char *data) sl@0: { sl@0: return(sk_insert(st,data,st->num)); sl@0: } sl@0: sl@0: EXPORT_C int sk_unshift(STACK *st, char *data) sl@0: { sl@0: return(sk_insert(st,data,0)); sl@0: } sl@0: sl@0: EXPORT_C char *sk_shift(STACK *st) sl@0: { sl@0: if (st == NULL) return(NULL); sl@0: if (st->num <= 0) return(NULL); sl@0: return(sk_delete(st,0)); sl@0: } sl@0: sl@0: EXPORT_C char *sk_pop(STACK *st) sl@0: { sl@0: if (st == NULL) return(NULL); sl@0: if (st->num <= 0) return(NULL); sl@0: return(sk_delete(st,st->num-1)); sl@0: } sl@0: sl@0: EXPORT_C void sk_zero(STACK *st) sl@0: { sl@0: if (st == NULL) return; sl@0: if (st->num <= 0) return; sl@0: memset((char *)st->data,0,sizeof(st->data)*st->num); sl@0: st->num=0; sl@0: } sl@0: sl@0: EXPORT_C void sk_pop_free(STACK *st, void (*func)(void *)) sl@0: { sl@0: int i; sl@0: sl@0: if (st == NULL) return; sl@0: for (i=0; inum; i++) sl@0: if (st->data[i] != NULL) sl@0: func(st->data[i]); sl@0: sk_free(st); sl@0: } sl@0: sl@0: EXPORT_C void sk_free(STACK *st) sl@0: { sl@0: if (st == NULL) return; sl@0: if (st->data != NULL) OPENSSL_free(st->data); sl@0: OPENSSL_free(st); sl@0: } sl@0: sl@0: EXPORT_C int sk_num(const STACK *st) sl@0: { sl@0: if(st == NULL) return -1; sl@0: return st->num; sl@0: } sl@0: sl@0: EXPORT_C char *sk_value(const STACK *st, int i) sl@0: { sl@0: if(!st || (i < 0) || (i >= st->num)) return NULL; sl@0: return st->data[i]; sl@0: } sl@0: sl@0: EXPORT_C char *sk_set(STACK *st, int i, char *value) sl@0: { sl@0: if(!st || (i < 0) || (i >= st->num)) return NULL; sl@0: return (st->data[i] = value); sl@0: } sl@0: sl@0: EXPORT_C void sk_sort(STACK *st) sl@0: { sl@0: if (st && !st->sorted) sl@0: { sl@0: int (*comp_func)(const void *,const void *); sl@0: sl@0: /* same comment as in sk_find ... previously st->comp was declared sl@0: * as a (void*,void*) callback type, but this made the population sl@0: * of the callback pointer illogical - our callbacks compare sl@0: * type** with type**, so we leave the casting until absolutely sl@0: * necessary (ie. "now"). */ sl@0: comp_func=(int (*)(const void *,const void *))(st->comp); sl@0: qsort(st->data,st->num,sizeof(char *), comp_func); sl@0: st->sorted=1; sl@0: } sl@0: } sl@0: EXPORT_C int sk_is_sorted(const STACK *st) sl@0: { sl@0: if (!st) sl@0: return 1; sl@0: return st->sorted; sl@0: }