stack.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. #include <openssl/stack.h>
  57. #include <string.h>
  58. #include <openssl/mem.h>
  59. #include "../internal.h"
  60. // kMinSize is the number of pointers that will be initially allocated in a new
  61. // stack.
  62. static const size_t kMinSize = 4;
  63. _STACK *sk_new(stack_cmp_func comp) {
  64. _STACK *ret;
  65. ret = OPENSSL_malloc(sizeof(_STACK));
  66. if (ret == NULL) {
  67. goto err;
  68. }
  69. OPENSSL_memset(ret, 0, sizeof(_STACK));
  70. ret->data = OPENSSL_malloc(sizeof(void *) * kMinSize);
  71. if (ret->data == NULL) {
  72. goto err;
  73. }
  74. OPENSSL_memset(ret->data, 0, sizeof(void *) * kMinSize);
  75. ret->comp = comp;
  76. ret->num_alloc = kMinSize;
  77. return ret;
  78. err:
  79. OPENSSL_free(ret);
  80. return NULL;
  81. }
  82. _STACK *sk_new_null(void) { return sk_new(NULL); }
  83. size_t sk_num(const _STACK *sk) {
  84. if (sk == NULL) {
  85. return 0;
  86. }
  87. return sk->num;
  88. }
  89. void sk_zero(_STACK *sk) {
  90. if (sk == NULL || sk->num == 0) {
  91. return;
  92. }
  93. OPENSSL_memset(sk->data, 0, sizeof(void*) * sk->num);
  94. sk->num = 0;
  95. sk->sorted = 0;
  96. }
  97. void *sk_value(const _STACK *sk, size_t i) {
  98. if (!sk || i >= sk->num) {
  99. return NULL;
  100. }
  101. return sk->data[i];
  102. }
  103. void *sk_set(_STACK *sk, size_t i, void *value) {
  104. if (!sk || i >= sk->num) {
  105. return NULL;
  106. }
  107. return sk->data[i] = value;
  108. }
  109. void sk_free(_STACK *sk) {
  110. if (sk == NULL) {
  111. return;
  112. }
  113. OPENSSL_free(sk->data);
  114. OPENSSL_free(sk);
  115. }
  116. void sk_pop_free(_STACK *sk, void (*func)(void *)) {
  117. if (sk == NULL) {
  118. return;
  119. }
  120. for (size_t i = 0; i < sk->num; i++) {
  121. if (sk->data[i] != NULL) {
  122. func(sk->data[i]);
  123. }
  124. }
  125. sk_free(sk);
  126. }
  127. size_t sk_insert(_STACK *sk, void *p, size_t where) {
  128. if (sk == NULL) {
  129. return 0;
  130. }
  131. if (sk->num_alloc <= sk->num + 1) {
  132. // Attempt to double the size of the array.
  133. size_t new_alloc = sk->num_alloc << 1;
  134. size_t alloc_size = new_alloc * sizeof(void *);
  135. void **data;
  136. // If the doubling overflowed, try to increment.
  137. if (new_alloc < sk->num_alloc || alloc_size / sizeof(void *) != new_alloc) {
  138. new_alloc = sk->num_alloc + 1;
  139. alloc_size = new_alloc * sizeof(void *);
  140. }
  141. // If the increment also overflowed, fail.
  142. if (new_alloc < sk->num_alloc || alloc_size / sizeof(void *) != new_alloc) {
  143. return 0;
  144. }
  145. data = OPENSSL_realloc(sk->data, alloc_size);
  146. if (data == NULL) {
  147. return 0;
  148. }
  149. sk->data = data;
  150. sk->num_alloc = new_alloc;
  151. }
  152. if (where >= sk->num) {
  153. sk->data[sk->num] = p;
  154. } else {
  155. OPENSSL_memmove(&sk->data[where + 1], &sk->data[where],
  156. sizeof(void *) * (sk->num - where));
  157. sk->data[where] = p;
  158. }
  159. sk->num++;
  160. sk->sorted = 0;
  161. return sk->num;
  162. }
  163. void *sk_delete(_STACK *sk, size_t where) {
  164. void *ret;
  165. if (!sk || where >= sk->num) {
  166. return NULL;
  167. }
  168. ret = sk->data[where];
  169. if (where != sk->num - 1) {
  170. OPENSSL_memmove(&sk->data[where], &sk->data[where + 1],
  171. sizeof(void *) * (sk->num - where - 1));
  172. }
  173. sk->num--;
  174. return ret;
  175. }
  176. void *sk_delete_ptr(_STACK *sk, void *p) {
  177. if (sk == NULL) {
  178. return NULL;
  179. }
  180. for (size_t i = 0; i < sk->num; i++) {
  181. if (sk->data[i] == p) {
  182. return sk_delete(sk, i);
  183. }
  184. }
  185. return NULL;
  186. }
  187. int sk_find(_STACK *sk, size_t *out_index, void *p) {
  188. if (sk == NULL) {
  189. return 0;
  190. }
  191. if (sk->comp == NULL) {
  192. // Use pointer equality when no comparison function has been set.
  193. for (size_t i = 0; i < sk->num; i++) {
  194. if (sk->data[i] == p) {
  195. if (out_index) {
  196. *out_index = i;
  197. }
  198. return 1;
  199. }
  200. }
  201. return 0;
  202. }
  203. if (p == NULL) {
  204. return 0;
  205. }
  206. sk_sort(sk);
  207. // sk->comp is a function that takes pointers to pointers to elements, but
  208. // qsort and bsearch take a comparison function that just takes pointers to
  209. // elements. However, since we're passing an array of pointers to
  210. // qsort/bsearch, we can just cast the comparison function and everything
  211. // works.
  212. const void *const *r = bsearch(&p, sk->data, sk->num, sizeof(void *),
  213. (int (*)(const void *, const void *))sk->comp);
  214. if (r == NULL) {
  215. return 0;
  216. }
  217. size_t idx = ((void **)r) - sk->data;
  218. // This function always returns the first result.
  219. while (idx > 0 &&
  220. sk->comp((const void **)&p, (const void **)&sk->data[idx - 1]) == 0) {
  221. idx--;
  222. }
  223. if (out_index) {
  224. *out_index = idx;
  225. }
  226. return 1;
  227. }
  228. void *sk_shift(_STACK *sk) {
  229. if (sk == NULL) {
  230. return NULL;
  231. }
  232. if (sk->num == 0) {
  233. return NULL;
  234. }
  235. return sk_delete(sk, 0);
  236. }
  237. size_t sk_push(_STACK *sk, void *p) { return (sk_insert(sk, p, sk->num)); }
  238. void *sk_pop(_STACK *sk) {
  239. if (sk == NULL) {
  240. return NULL;
  241. }
  242. if (sk->num == 0) {
  243. return NULL;
  244. }
  245. return sk_delete(sk, sk->num - 1);
  246. }
  247. _STACK *sk_dup(const _STACK *sk) {
  248. _STACK *ret;
  249. void **s;
  250. if (sk == NULL) {
  251. return NULL;
  252. }
  253. ret = sk_new(sk->comp);
  254. if (ret == NULL) {
  255. goto err;
  256. }
  257. s = (void **)OPENSSL_realloc(ret->data, sizeof(void *) * sk->num_alloc);
  258. if (s == NULL) {
  259. goto err;
  260. }
  261. ret->data = s;
  262. ret->num = sk->num;
  263. OPENSSL_memcpy(ret->data, sk->data, sizeof(void *) * sk->num);
  264. ret->sorted = sk->sorted;
  265. ret->num_alloc = sk->num_alloc;
  266. ret->comp = sk->comp;
  267. return ret;
  268. err:
  269. sk_free(ret);
  270. return NULL;
  271. }
  272. void sk_sort(_STACK *sk) {
  273. int (*comp_func)(const void *,const void *);
  274. if (sk == NULL || sk->comp == NULL || sk->sorted) {
  275. return;
  276. }
  277. // See the comment in sk_find about this cast.
  278. comp_func = (int (*)(const void *, const void *))(sk->comp);
  279. qsort(sk->data, sk->num, sizeof(void *), comp_func);
  280. sk->sorted = 1;
  281. }
  282. int sk_is_sorted(const _STACK *sk) {
  283. if (!sk) {
  284. return 1;
  285. }
  286. return sk->sorted;
  287. }
  288. stack_cmp_func sk_set_cmp_func(_STACK *sk, stack_cmp_func comp) {
  289. stack_cmp_func old = sk->comp;
  290. if (sk->comp != comp) {
  291. sk->sorted = 0;
  292. }
  293. sk->comp = comp;
  294. return old;
  295. }
  296. _STACK *sk_deep_copy(const _STACK *sk, void *(*copy_func)(void *),
  297. void (*free_func)(void *)) {
  298. _STACK *ret = sk_dup(sk);
  299. if (ret == NULL) {
  300. return NULL;
  301. }
  302. for (size_t i = 0; i < ret->num; i++) {
  303. if (ret->data[i] == NULL) {
  304. continue;
  305. }
  306. ret->data[i] = copy_func(ret->data[i]);
  307. if (ret->data[i] == NULL) {
  308. for (size_t j = 0; j < i; j++) {
  309. if (ret->data[j] != NULL) {
  310. free_func(ret->data[j]);
  311. }
  312. }
  313. sk_free(ret);
  314. return NULL;
  315. }
  316. }
  317. return ret;
  318. }