You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

391 lines
9.4 KiB

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