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