stack.c 10 KB

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