Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 2 | * All rights reserved. |
| 3 | * |
| 4 | * This package is an SSL implementation written |
| 5 | * by Eric Young (eay@cryptsoft.com). |
| 6 | * The implementation was written so as to conform with Netscapes SSL. |
| 7 | * |
| 8 | * This library is free for commercial and non-commercial use as long as |
| 9 | * the following conditions are aheared to. The following conditions |
| 10 | * apply to all code found in this distribution, be it the RC4, RSA, |
| 11 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
| 12 | * included with this distribution is covered by the same copyright terms |
| 13 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
| 14 | * |
| 15 | * Copyright remains Eric Young's, and as such any Copyright notices in |
| 16 | * the code are not to be removed. |
| 17 | * If this package is used in a product, Eric Young should be given attribution |
| 18 | * as the author of the parts of the library used. |
| 19 | * This can be in the form of a textual message at program startup or |
| 20 | * in documentation (online or textual) provided with the package. |
| 21 | * |
| 22 | * Redistribution and use in source and binary forms, with or without |
| 23 | * modification, are permitted provided that the following conditions |
| 24 | * are met: |
| 25 | * 1. Redistributions of source code must retain the copyright |
| 26 | * notice, this list of conditions and the following disclaimer. |
| 27 | * 2. Redistributions in binary form must reproduce the above copyright |
| 28 | * notice, this list of conditions and the following disclaimer in the |
| 29 | * documentation and/or other materials provided with the distribution. |
| 30 | * 3. All advertising materials mentioning features or use of this software |
| 31 | * must display the following acknowledgement: |
| 32 | * "This product includes cryptographic software written by |
| 33 | * Eric Young (eay@cryptsoft.com)" |
| 34 | * The word 'cryptographic' can be left out if the rouines from the library |
| 35 | * being used are not cryptographic related :-). |
| 36 | * 4. If you include any Windows specific code (or a derivative thereof) from |
| 37 | * the apps directory (application code) you must include an acknowledgement: |
| 38 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
| 39 | * |
| 40 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
| 41 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 44 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 45 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 46 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 48 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 49 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 50 | * SUCH DAMAGE. |
| 51 | * |
| 52 | * The licence and distribution terms for any publically available version or |
| 53 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
| 54 | * copied and put under another distribution licence |
| 55 | * [including the GNU Public Licence.] */ |
| 56 | |
Adam Langley | 4c921e1 | 2014-07-14 15:28:14 -0700 | [diff] [blame] | 57 | #include <openssl/stack.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 58 | |
Adam Langley | 2b2d66d | 2015-01-30 17:08:37 -0800 | [diff] [blame] | 59 | #include <string.h> |
| 60 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 61 | #include <openssl/mem.h> |
| 62 | |
| 63 | /* kMinSize is the number of pointers that will be initially allocated in a new |
| 64 | * stack. */ |
| 65 | static const size_t kMinSize = 4; |
| 66 | |
| 67 | _STACK *sk_new(stack_cmp_func comp) { |
| 68 | _STACK *ret; |
| 69 | |
| 70 | ret = OPENSSL_malloc(sizeof(_STACK)); |
| 71 | if (ret == NULL) { |
| 72 | goto err; |
| 73 | } |
| 74 | memset(ret, 0, sizeof(_STACK)); |
| 75 | |
| 76 | ret->data = OPENSSL_malloc(sizeof(void *) * kMinSize); |
| 77 | if (ret->data == NULL) { |
| 78 | goto err; |
| 79 | } |
| 80 | |
| 81 | memset(ret->data, 0, sizeof(void *) * kMinSize); |
| 82 | |
| 83 | ret->comp = comp; |
| 84 | ret->num_alloc = kMinSize; |
| 85 | |
| 86 | return ret; |
| 87 | |
| 88 | err: |
David Benjamin | d8b65c8 | 2015-04-22 16:09:09 -0400 | [diff] [blame] | 89 | OPENSSL_free(ret); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 90 | return NULL; |
| 91 | } |
| 92 | |
| 93 | _STACK *sk_new_null(void) { return sk_new(NULL); } |
| 94 | |
| 95 | size_t sk_num(const _STACK *sk) { |
| 96 | if (sk == NULL) { |
| 97 | return 0; |
| 98 | } |
| 99 | return sk->num; |
| 100 | } |
| 101 | |
| 102 | void sk_zero(_STACK *sk) { |
| 103 | if (sk == NULL || sk->num == 0) { |
| 104 | return; |
| 105 | } |
| 106 | memset(sk->data, 0, sizeof(void*) * sk->num); |
| 107 | sk->num = 0; |
| 108 | sk->sorted = 0; |
| 109 | } |
| 110 | |
| 111 | void *sk_value(const _STACK *sk, size_t i) { |
| 112 | if (!sk || i >= sk->num) { |
| 113 | return NULL; |
| 114 | } |
| 115 | return sk->data[i]; |
| 116 | } |
| 117 | |
| 118 | void *sk_set(_STACK *sk, size_t i, void *value) { |
| 119 | if (!sk || i >= sk->num) { |
| 120 | return NULL; |
| 121 | } |
| 122 | return sk->data[i] = value; |
| 123 | } |
| 124 | |
| 125 | void sk_free(_STACK *sk) { |
| 126 | if (sk == NULL) { |
| 127 | return; |
| 128 | } |
| 129 | OPENSSL_free(sk->data); |
| 130 | OPENSSL_free(sk); |
| 131 | } |
| 132 | |
| 133 | void sk_pop_free(_STACK *sk, void (*func)(void *)) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 134 | if (sk == NULL) { |
| 135 | return; |
| 136 | } |
| 137 | |
David Benjamin | 5409123 | 2016-09-05 12:47:25 -0400 | [diff] [blame] | 138 | for (size_t i = 0; i < sk->num; i++) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 139 | if (sk->data[i] != NULL) { |
| 140 | func(sk->data[i]); |
| 141 | } |
| 142 | } |
| 143 | sk_free(sk); |
| 144 | } |
| 145 | |
| 146 | size_t sk_insert(_STACK *sk, void *p, size_t where) { |
| 147 | if (sk == NULL) { |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | if (sk->num_alloc <= sk->num + 1) { |
| 152 | /* Attempt to double the size of the array. */ |
| 153 | size_t new_alloc = sk->num_alloc << 1; |
| 154 | size_t alloc_size = new_alloc * sizeof(void *); |
| 155 | void **data; |
| 156 | |
| 157 | /* If the doubling overflowed, try to increment. */ |
| 158 | if (new_alloc < sk->num_alloc || alloc_size / sizeof(void *) != new_alloc) { |
| 159 | new_alloc = sk->num_alloc + 1; |
| 160 | alloc_size = new_alloc * sizeof(void *); |
| 161 | } |
| 162 | |
| 163 | /* If the increment also overflowed, fail. */ |
| 164 | if (new_alloc < sk->num_alloc || alloc_size / sizeof(void *) != new_alloc) { |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | data = OPENSSL_realloc(sk->data, alloc_size); |
| 169 | if (data == NULL) { |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | sk->data = data; |
| 174 | sk->num_alloc = new_alloc; |
| 175 | } |
| 176 | |
| 177 | if (where >= sk->num) { |
| 178 | sk->data[sk->num] = p; |
| 179 | } else { |
| 180 | memmove(&sk->data[where + 1], &sk->data[where], |
| 181 | sizeof(void *) * (sk->num - where)); |
| 182 | sk->data[where] = p; |
| 183 | } |
| 184 | |
| 185 | sk->num++; |
| 186 | sk->sorted = 0; |
| 187 | |
| 188 | return sk->num; |
| 189 | } |
| 190 | |
| 191 | void *sk_delete(_STACK *sk, size_t where) { |
| 192 | void *ret; |
| 193 | |
| 194 | if (!sk || where >= sk->num) { |
| 195 | return NULL; |
| 196 | } |
| 197 | |
| 198 | ret = sk->data[where]; |
| 199 | |
| 200 | if (where != sk->num - 1) { |
| 201 | memmove(&sk->data[where], &sk->data[where + 1], |
| 202 | sizeof(void *) * (sk->num - where - 1)); |
| 203 | } |
| 204 | |
| 205 | sk->num--; |
| 206 | return ret; |
| 207 | } |
| 208 | |
| 209 | void *sk_delete_ptr(_STACK *sk, void *p) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 210 | if (sk == NULL) { |
| 211 | return NULL; |
| 212 | } |
| 213 | |
David Benjamin | 5409123 | 2016-09-05 12:47:25 -0400 | [diff] [blame] | 214 | for (size_t i = 0; i < sk->num; i++) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 215 | if (sk->data[i] == p) { |
| 216 | return sk_delete(sk, i); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | return NULL; |
| 221 | } |
| 222 | |
| 223 | int sk_find(_STACK *sk, size_t *out_index, void *p) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 224 | if (sk == NULL) { |
Doug Hogan | 41846c7 | 2015-04-22 23:48:22 -0700 | [diff] [blame] | 225 | return 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | if (sk->comp == NULL) { |
| 229 | /* Use pointer equality when no comparison function has been set. */ |
David Benjamin | 5409123 | 2016-09-05 12:47:25 -0400 | [diff] [blame] | 230 | for (size_t i = 0; i < sk->num; i++) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 231 | if (sk->data[i] == p) { |
| 232 | if (out_index) { |
| 233 | *out_index = i; |
| 234 | } |
| 235 | return 1; |
| 236 | } |
| 237 | } |
| 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | if (p == NULL) { |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | sk_sort(sk); |
| 246 | |
| 247 | /* sk->comp is a function that takes pointers to pointers to elements, but |
| 248 | * qsort and bsearch take a comparison function that just takes pointers to |
| 249 | * elements. However, since we're passing an array of pointers to |
| 250 | * qsort/bsearch, we can just cast the comparison function and everything |
| 251 | * works. */ |
David Benjamin | 5409123 | 2016-09-05 12:47:25 -0400 | [diff] [blame] | 252 | const void *const *r = bsearch(&p, sk->data, sk->num, sizeof(void *), |
| 253 | (int (*)(const void *, const void *))sk->comp); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 254 | if (r == NULL) { |
| 255 | return 0; |
| 256 | } |
David Benjamin | 5409123 | 2016-09-05 12:47:25 -0400 | [diff] [blame] | 257 | size_t idx = ((void **)r) - sk->data; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 258 | /* This function always returns the first result. */ |
David Benjamin | 5409123 | 2016-09-05 12:47:25 -0400 | [diff] [blame] | 259 | while (idx > 0 && |
| 260 | sk->comp((const void **)&p, (const void **)&sk->data[idx - 1]) == 0) { |
| 261 | idx--; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 262 | } |
| 263 | if (out_index) { |
David Benjamin | 5409123 | 2016-09-05 12:47:25 -0400 | [diff] [blame] | 264 | *out_index = idx; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 265 | } |
| 266 | return 1; |
| 267 | } |
| 268 | |
| 269 | void *sk_shift(_STACK *sk) { |
| 270 | if (sk == NULL) { |
| 271 | return NULL; |
| 272 | } |
| 273 | if (sk->num == 0) { |
| 274 | return NULL; |
| 275 | } |
| 276 | return sk_delete(sk, 0); |
| 277 | } |
| 278 | |
| 279 | size_t sk_push(_STACK *sk, void *p) { return (sk_insert(sk, p, sk->num)); } |
| 280 | |
| 281 | void *sk_pop(_STACK *sk) { |
| 282 | if (sk == NULL) { |
| 283 | return NULL; |
| 284 | } |
| 285 | if (sk->num == 0) { |
| 286 | return NULL; |
| 287 | } |
| 288 | return sk_delete(sk, sk->num - 1); |
| 289 | } |
| 290 | |
| 291 | _STACK *sk_dup(const _STACK *sk) { |
| 292 | _STACK *ret; |
| 293 | void **s; |
| 294 | |
| 295 | if (sk == NULL) { |
| 296 | return NULL; |
| 297 | } |
| 298 | |
| 299 | ret = sk_new(sk->comp); |
| 300 | if (ret == NULL) { |
| 301 | goto err; |
| 302 | } |
| 303 | |
| 304 | s = (void **)OPENSSL_realloc(ret->data, sizeof(void *) * sk->num_alloc); |
| 305 | if (s == NULL) { |
| 306 | goto err; |
| 307 | } |
| 308 | ret->data = s; |
| 309 | |
| 310 | ret->num = sk->num; |
| 311 | memcpy(ret->data, sk->data, sizeof(void *) * sk->num); |
| 312 | ret->sorted = sk->sorted; |
| 313 | ret->num_alloc = sk->num_alloc; |
| 314 | ret->comp = sk->comp; |
| 315 | return ret; |
| 316 | |
| 317 | err: |
David Benjamin | d8b65c8 | 2015-04-22 16:09:09 -0400 | [diff] [blame] | 318 | sk_free(ret); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 319 | return NULL; |
| 320 | } |
| 321 | |
| 322 | void sk_sort(_STACK *sk) { |
| 323 | int (*comp_func)(const void *,const void *); |
| 324 | |
Steven Valdez | fd26b7a | 2016-02-25 13:49:45 -0500 | [diff] [blame] | 325 | if (sk == NULL || sk->comp == NULL || sk->sorted) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 326 | return; |
| 327 | } |
| 328 | |
| 329 | /* See the comment in sk_find about this cast. */ |
| 330 | comp_func = (int (*)(const void *, const void *))(sk->comp); |
| 331 | qsort(sk->data, sk->num, sizeof(void *), comp_func); |
| 332 | sk->sorted = 1; |
| 333 | } |
| 334 | |
| 335 | int sk_is_sorted(const _STACK *sk) { |
| 336 | if (!sk) { |
| 337 | return 1; |
| 338 | } |
| 339 | return sk->sorted; |
| 340 | } |
| 341 | |
| 342 | stack_cmp_func sk_set_cmp_func(_STACK *sk, stack_cmp_func comp) { |
| 343 | stack_cmp_func old = sk->comp; |
| 344 | |
| 345 | if (sk->comp != comp) { |
| 346 | sk->sorted = 0; |
| 347 | } |
| 348 | sk->comp = comp; |
| 349 | |
| 350 | return old; |
| 351 | } |
Adam Langley | a1048a7 | 2015-02-11 14:27:21 -0800 | [diff] [blame] | 352 | |
| 353 | _STACK *sk_deep_copy(const _STACK *sk, void *(*copy_func)(void *), |
| 354 | void (*free_func)(void *)) { |
| 355 | _STACK *ret = sk_dup(sk); |
| 356 | if (ret == NULL) { |
| 357 | return NULL; |
| 358 | } |
| 359 | |
David Benjamin | 5409123 | 2016-09-05 12:47:25 -0400 | [diff] [blame] | 360 | for (size_t i = 0; i < ret->num; i++) { |
Adam Langley | a1048a7 | 2015-02-11 14:27:21 -0800 | [diff] [blame] | 361 | if (ret->data[i] == NULL) { |
| 362 | continue; |
| 363 | } |
| 364 | ret->data[i] = copy_func(ret->data[i]); |
| 365 | if (ret->data[i] == NULL) { |
David Benjamin | 5409123 | 2016-09-05 12:47:25 -0400 | [diff] [blame] | 366 | for (size_t j = 0; j < i; j++) { |
Adam Langley | a1048a7 | 2015-02-11 14:27:21 -0800 | [diff] [blame] | 367 | if (ret->data[j] != NULL) { |
| 368 | free_func(ret->data[j]); |
| 369 | } |
| 370 | } |
| 371 | sk_free(ret); |
| 372 | return NULL; |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | return ret; |
| 377 | } |