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