stack.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /*
  2. * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include "internal/numbers.h"
  12. #include <openssl/stack.h>
  13. #include <errno.h>
  14. #include <openssl/e_os2.h> /* For ossl_inline */
  15. /*
  16. * The initial number of nodes in the array.
  17. */
  18. static const int min_nodes = 4;
  19. static const int max_nodes = SIZE_MAX / sizeof(void *) < INT_MAX
  20. ? (int)(SIZE_MAX / sizeof(void *))
  21. : INT_MAX;
  22. struct stack_st {
  23. int num;
  24. const void **data;
  25. int sorted;
  26. int num_alloc;
  27. OPENSSL_sk_compfunc comp;
  28. };
  29. OPENSSL_sk_compfunc OPENSSL_sk_set_cmp_func(OPENSSL_STACK *sk, OPENSSL_sk_compfunc c)
  30. {
  31. OPENSSL_sk_compfunc old = sk->comp;
  32. if (sk->comp != c)
  33. sk->sorted = 0;
  34. sk->comp = c;
  35. return old;
  36. }
  37. OPENSSL_STACK *OPENSSL_sk_dup(const OPENSSL_STACK *sk)
  38. {
  39. OPENSSL_STACK *ret;
  40. if ((ret = OPENSSL_malloc(sizeof(*ret))) == NULL) {
  41. CRYPTOerr(CRYPTO_F_OPENSSL_SK_DUP, ERR_R_MALLOC_FAILURE);
  42. return NULL;
  43. }
  44. /* direct structure assignment */
  45. *ret = *sk;
  46. if (sk->num == 0) {
  47. /* postpone |ret->data| allocation */
  48. ret->data = NULL;
  49. ret->num_alloc = 0;
  50. return ret;
  51. }
  52. /* duplicate |sk->data| content */
  53. if ((ret->data = OPENSSL_malloc(sizeof(*ret->data) * sk->num_alloc)) == NULL)
  54. goto err;
  55. memcpy(ret->data, sk->data, sizeof(void *) * sk->num);
  56. return ret;
  57. err:
  58. OPENSSL_sk_free(ret);
  59. return NULL;
  60. }
  61. OPENSSL_STACK *OPENSSL_sk_deep_copy(const OPENSSL_STACK *sk,
  62. OPENSSL_sk_copyfunc copy_func,
  63. OPENSSL_sk_freefunc free_func)
  64. {
  65. OPENSSL_STACK *ret;
  66. int i;
  67. if ((ret = OPENSSL_malloc(sizeof(*ret))) == NULL) {
  68. CRYPTOerr(CRYPTO_F_OPENSSL_SK_DEEP_COPY, ERR_R_MALLOC_FAILURE);
  69. return NULL;
  70. }
  71. /* direct structure assignment */
  72. *ret = *sk;
  73. if (sk->num == 0) {
  74. /* postpone |ret| data allocation */
  75. ret->data = NULL;
  76. ret->num_alloc = 0;
  77. return ret;
  78. }
  79. ret->num_alloc = sk->num > min_nodes ? sk->num : min_nodes;
  80. ret->data = OPENSSL_zalloc(sizeof(*ret->data) * ret->num_alloc);
  81. if (ret->data == NULL) {
  82. OPENSSL_free(ret);
  83. return NULL;
  84. }
  85. for (i = 0; i < ret->num; ++i) {
  86. if (sk->data[i] == NULL)
  87. continue;
  88. if ((ret->data[i] = copy_func(sk->data[i])) == NULL) {
  89. while (--i >= 0)
  90. if (ret->data[i] != NULL)
  91. free_func((void *)ret->data[i]);
  92. OPENSSL_sk_free(ret);
  93. return NULL;
  94. }
  95. }
  96. return ret;
  97. }
  98. OPENSSL_STACK *OPENSSL_sk_new_null(void)
  99. {
  100. return OPENSSL_sk_new_reserve(NULL, 0);
  101. }
  102. OPENSSL_STACK *OPENSSL_sk_new(OPENSSL_sk_compfunc c)
  103. {
  104. return OPENSSL_sk_new_reserve(c, 0);
  105. }
  106. /*
  107. * Calculate the array growth based on the target size.
  108. *
  109. * The growth fraction is a rational number and is defined by a numerator
  110. * and a denominator. According to Andrew Koenig in his paper "Why Are
  111. * Vectors Efficient?" from JOOP 11(5) 1998, this factor should be less
  112. * than the golden ratio (1.618...).
  113. *
  114. * We use 3/2 = 1.5 for simplicity of calculation and overflow checking.
  115. * Another option 8/5 = 1.6 allows for slightly faster growth, although safe
  116. * computation is more difficult.
  117. *
  118. * The limit to avoid overflow is spot on. The modulo three correction term
  119. * ensures that the limit is the largest number than can be expanded by the
  120. * growth factor without exceeding the hard limit.
  121. *
  122. * Do not call it with |current| lower than 2, or it will infinitely loop.
  123. */
  124. static ossl_inline int compute_growth(int target, int current)
  125. {
  126. const int limit = (max_nodes / 3) * 2 + (max_nodes % 3 ? 1 : 0);
  127. while (current < target) {
  128. /* Check to see if we're at the hard limit */
  129. if (current >= max_nodes)
  130. return 0;
  131. /* Expand the size by a factor of 3/2 if it is within range */
  132. current = current < limit ? current + current / 2 : max_nodes;
  133. }
  134. return current;
  135. }
  136. /* internal STACK storage allocation */
  137. static int sk_reserve(OPENSSL_STACK *st, int n, int exact)
  138. {
  139. const void **tmpdata;
  140. int num_alloc;
  141. /* Check to see the reservation isn't exceeding the hard limit */
  142. if (n > max_nodes - st->num)
  143. return 0;
  144. /* Figure out the new size */
  145. num_alloc = st->num + n;
  146. if (num_alloc < min_nodes)
  147. num_alloc = min_nodes;
  148. /* If |st->data| allocation was postponed */
  149. if (st->data == NULL) {
  150. /*
  151. * At this point, |st->num_alloc| and |st->num| are 0;
  152. * so |num_alloc| value is |n| or |min_nodes| if greater than |n|.
  153. */
  154. if ((st->data = OPENSSL_zalloc(sizeof(void *) * num_alloc)) == NULL) {
  155. CRYPTOerr(CRYPTO_F_SK_RESERVE, ERR_R_MALLOC_FAILURE);
  156. return 0;
  157. }
  158. st->num_alloc = num_alloc;
  159. return 1;
  160. }
  161. if (!exact) {
  162. if (num_alloc <= st->num_alloc)
  163. return 1;
  164. num_alloc = compute_growth(num_alloc, st->num_alloc);
  165. if (num_alloc == 0)
  166. return 0;
  167. } else if (num_alloc == st->num_alloc) {
  168. return 1;
  169. }
  170. tmpdata = OPENSSL_realloc((void *)st->data, sizeof(void *) * num_alloc);
  171. if (tmpdata == NULL)
  172. return 0;
  173. st->data = tmpdata;
  174. st->num_alloc = num_alloc;
  175. return 1;
  176. }
  177. OPENSSL_STACK *OPENSSL_sk_new_reserve(OPENSSL_sk_compfunc c, int n)
  178. {
  179. OPENSSL_STACK *st = OPENSSL_zalloc(sizeof(OPENSSL_STACK));
  180. if (st == NULL)
  181. return NULL;
  182. st->comp = c;
  183. if (n <= 0)
  184. return st;
  185. if (!sk_reserve(st, n, 1)) {
  186. OPENSSL_sk_free(st);
  187. return NULL;
  188. }
  189. return st;
  190. }
  191. int OPENSSL_sk_reserve(OPENSSL_STACK *st, int n)
  192. {
  193. if (st == NULL)
  194. return 0;
  195. if (n < 0)
  196. return 1;
  197. return sk_reserve(st, n, 1);
  198. }
  199. int OPENSSL_sk_insert(OPENSSL_STACK *st, const void *data, int loc)
  200. {
  201. if (st == NULL || st->num == max_nodes)
  202. return 0;
  203. if (!sk_reserve(st, 1, 0))
  204. return 0;
  205. if ((loc >= st->num) || (loc < 0)) {
  206. st->data[st->num] = data;
  207. } else {
  208. memmove(&st->data[loc + 1], &st->data[loc],
  209. sizeof(st->data[0]) * (st->num - loc));
  210. st->data[loc] = data;
  211. }
  212. st->num++;
  213. st->sorted = 0;
  214. return st->num;
  215. }
  216. static ossl_inline void *internal_delete(OPENSSL_STACK *st, int loc)
  217. {
  218. const void *ret = st->data[loc];
  219. if (loc != st->num - 1)
  220. memmove(&st->data[loc], &st->data[loc + 1],
  221. sizeof(st->data[0]) * (st->num - loc - 1));
  222. st->num--;
  223. return (void *)ret;
  224. }
  225. void *OPENSSL_sk_delete_ptr(OPENSSL_STACK *st, const void *p)
  226. {
  227. int i;
  228. for (i = 0; i < st->num; i++)
  229. if (st->data[i] == p)
  230. return internal_delete(st, i);
  231. return NULL;
  232. }
  233. void *OPENSSL_sk_delete(OPENSSL_STACK *st, int loc)
  234. {
  235. if (st == NULL || loc < 0 || loc >= st->num)
  236. return NULL;
  237. return internal_delete(st, loc);
  238. }
  239. static int internal_find(OPENSSL_STACK *st, const void *data,
  240. int ret_val_options)
  241. {
  242. const void *r;
  243. int i;
  244. if (st == NULL || st->num == 0)
  245. return -1;
  246. if (st->comp == NULL) {
  247. for (i = 0; i < st->num; i++)
  248. if (st->data[i] == data)
  249. return i;
  250. return -1;
  251. }
  252. if (!st->sorted) {
  253. if (st->num > 1)
  254. qsort(st->data, st->num, sizeof(void *), st->comp);
  255. st->sorted = 1; /* empty or single-element stack is considered sorted */
  256. }
  257. if (data == NULL)
  258. return -1;
  259. r = ossl_bsearch(&data, st->data, st->num, sizeof(void *), st->comp,
  260. ret_val_options);
  261. return r == NULL ? -1 : (int)((const void **)r - st->data);
  262. }
  263. int OPENSSL_sk_find(OPENSSL_STACK *st, const void *data)
  264. {
  265. return internal_find(st, data, OSSL_BSEARCH_FIRST_VALUE_ON_MATCH);
  266. }
  267. int OPENSSL_sk_find_ex(OPENSSL_STACK *st, const void *data)
  268. {
  269. return internal_find(st, data, OSSL_BSEARCH_VALUE_ON_NOMATCH);
  270. }
  271. int OPENSSL_sk_push(OPENSSL_STACK *st, const void *data)
  272. {
  273. if (st == NULL)
  274. return -1;
  275. return OPENSSL_sk_insert(st, data, st->num);
  276. }
  277. int OPENSSL_sk_unshift(OPENSSL_STACK *st, const void *data)
  278. {
  279. return OPENSSL_sk_insert(st, data, 0);
  280. }
  281. void *OPENSSL_sk_shift(OPENSSL_STACK *st)
  282. {
  283. if (st == NULL || st->num == 0)
  284. return NULL;
  285. return internal_delete(st, 0);
  286. }
  287. void *OPENSSL_sk_pop(OPENSSL_STACK *st)
  288. {
  289. if (st == NULL || st->num == 0)
  290. return NULL;
  291. return internal_delete(st, st->num - 1);
  292. }
  293. void OPENSSL_sk_zero(OPENSSL_STACK *st)
  294. {
  295. if (st == NULL || st->num == 0)
  296. return;
  297. memset(st->data, 0, sizeof(*st->data) * st->num);
  298. st->num = 0;
  299. }
  300. void OPENSSL_sk_pop_free(OPENSSL_STACK *st, OPENSSL_sk_freefunc func)
  301. {
  302. int i;
  303. if (st == NULL)
  304. return;
  305. for (i = 0; i < st->num; i++)
  306. if (st->data[i] != NULL)
  307. func((char *)st->data[i]);
  308. OPENSSL_sk_free(st);
  309. }
  310. void OPENSSL_sk_free(OPENSSL_STACK *st)
  311. {
  312. if (st == NULL)
  313. return;
  314. OPENSSL_free(st->data);
  315. OPENSSL_free(st);
  316. }
  317. int OPENSSL_sk_num(const OPENSSL_STACK *st)
  318. {
  319. return st == NULL ? -1 : st->num;
  320. }
  321. void *OPENSSL_sk_value(const OPENSSL_STACK *st, int i)
  322. {
  323. if (st == NULL || i < 0 || i >= st->num)
  324. return NULL;
  325. return (void *)st->data[i];
  326. }
  327. void *OPENSSL_sk_set(OPENSSL_STACK *st, int i, const void *data)
  328. {
  329. if (st == NULL || i < 0 || i >= st->num)
  330. return NULL;
  331. st->data[i] = data;
  332. st->sorted = 0;
  333. return (void *)st->data[i];
  334. }
  335. void OPENSSL_sk_sort(OPENSSL_STACK *st)
  336. {
  337. if (st != NULL && !st->sorted && st->comp != NULL) {
  338. if (st->num > 1)
  339. qsort(st->data, st->num, sizeof(void *), st->comp);
  340. st->sorted = 1; /* empty or single-element stack is considered sorted */
  341. }
  342. }
  343. int OPENSSL_sk_is_sorted(const OPENSSL_STACK *st)
  344. {
  345. return st == NULL ? 1 : st->sorted;
  346. }