1 /* $OpenBSD: stack.c,v 1.18 2014/07/11 08:44:49 jsing Exp $ */ 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 69 #include <stdio.h> 70 #include <string.h> 71 72 #include <openssl/objects.h> 73 #include <openssl/stack.h> 74 75 #undef MIN_NODES 76 #define MIN_NODES 4 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 free(ret); 145 return (NULL); 146 } 147 148 int 149 sk_insert(_STACK *st, void *data, int loc) 150 { 151 char **s; 152 153 if (st == NULL) 154 return 0; 155 if (st->num_alloc <= st->num + 1) { 156 s = reallocarray(st->data, st->num_alloc, 2 * sizeof(char *)); 157 if (s == NULL) 158 return (0); 159 st->data = s; 160 st->num_alloc *= 2; 161 } 162 if ((loc >= (int)st->num) || (loc < 0)) 163 st->data[st->num] = data; 164 else { 165 memmove(&(st->data[loc + 1]), &(st->data[loc]), 166 sizeof(char *)*(st->num - loc)); 167 st->data[loc] = data; 168 } 169 st->num++; 170 st->sorted = 0; 171 return (st->num); 172 } 173 174 void * 175 sk_delete_ptr(_STACK *st, void *p) 176 { 177 int i; 178 179 for (i = 0; i < st->num; i++) 180 if (st->data[i] == p) 181 return (sk_delete(st, i)); 182 return (NULL); 183 } 184 185 void * 186 sk_delete(_STACK *st, int loc) 187 { 188 char *ret; 189 190 if (!st || (loc < 0) || (loc >= st->num)) 191 return NULL; 192 193 ret = st->data[loc]; 194 if (loc != st->num - 1) { 195 memmove(&(st->data[loc]), &(st->data[loc + 1]), 196 sizeof(char *)*(st->num - 1 - loc)); 197 } 198 st->num--; 199 return (ret); 200 } 201 202 static int 203 internal_find(_STACK *st, void *data, int ret_val_options) 204 { 205 const void * const *r; 206 int i; 207 208 if (st == NULL) 209 return -1; 210 211 if (st->comp == NULL) { 212 for (i = 0; i < st->num; i++) 213 if (st->data[i] == data) 214 return (i); 215 return (-1); 216 } 217 sk_sort(st); 218 if (data == NULL) 219 return (-1); 220 r = OBJ_bsearch_ex_(&data, st->data, st->num, sizeof(void *), st->comp, 221 ret_val_options); 222 if (r == NULL) 223 return (-1); 224 return (int)((char **)r - st->data); 225 } 226 227 int 228 sk_find(_STACK *st, void *data) 229 { 230 return internal_find(st, data, OBJ_BSEARCH_FIRST_VALUE_ON_MATCH); 231 } 232 233 int 234 sk_find_ex(_STACK *st, void *data) 235 { 236 return internal_find(st, data, OBJ_BSEARCH_VALUE_ON_NOMATCH); 237 } 238 239 int 240 sk_push(_STACK *st, void *data) 241 { 242 return (sk_insert(st, data, st->num)); 243 } 244 245 int 246 sk_unshift(_STACK *st, void *data) 247 { 248 return (sk_insert(st, data, 0)); 249 } 250 251 void * 252 sk_shift(_STACK *st) 253 { 254 if (st == NULL) 255 return (NULL); 256 if (st->num <= 0) 257 return (NULL); 258 return (sk_delete(st, 0)); 259 } 260 261 void * 262 sk_pop(_STACK *st) 263 { 264 if (st == NULL) 265 return (NULL); 266 if (st->num <= 0) 267 return (NULL); 268 return (sk_delete(st, st->num - 1)); 269 } 270 271 void 272 sk_zero(_STACK *st) 273 { 274 if (st == NULL) 275 return; 276 if (st->num <= 0) 277 return; 278 memset(st->data, 0, sizeof(st->data)*st->num); 279 st->num = 0; 280 } 281 282 void 283 sk_pop_free(_STACK *st, void (*func)(void *)) 284 { 285 int i; 286 287 if (st == NULL) 288 return; 289 for (i = 0; i < st->num; i++) 290 if (st->data[i] != NULL) 291 func(st->data[i]); 292 sk_free(st); 293 } 294 295 void 296 sk_free(_STACK *st) 297 { 298 if (st == NULL) 299 return; 300 free(st->data); 301 free(st); 302 } 303 304 int 305 sk_num(const _STACK *st) 306 { 307 if (st == NULL) 308 return -1; 309 return st->num; 310 } 311 312 void * 313 sk_value(const _STACK *st, int i) 314 { 315 if (!st || (i < 0) || (i >= st->num)) 316 return NULL; 317 return st->data[i]; 318 } 319 320 void * 321 sk_set(_STACK *st, int i, void *value) 322 { 323 if (!st || (i < 0) || (i >= st->num)) 324 return NULL; 325 return (st->data[i] = value); 326 } 327 328 void 329 sk_sort(_STACK *st) 330 { 331 if (st && !st->sorted) { 332 int (*comp_func)(const void *, const void *); 333 334 /* same comment as in sk_find ... previously st->comp was declared 335 * as a (void*,void*) callback type, but this made the population 336 * of the callback pointer illogical - our callbacks compare 337 * type** with type**, so we leave the casting until absolutely 338 * necessary (ie. "now"). */ 339 comp_func = (int (*)(const void *, const void *))(st->comp); 340 qsort(st->data, st->num, sizeof(char *), comp_func); 341 st->sorted = 1; 342 } 343 } 344 345 int 346 sk_is_sorted(const _STACK *st) 347 { 348 if (!st) 349 return 1; 350 return st->sorted; 351 } 352