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.
 
 
 
 
 
 

425 lines
11 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. #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_ex(_STACK *sk, void (*call_free_func)(stack_free_func, void *),
  117. stack_free_func free_func) {
  118. if (sk == NULL) {
  119. return;
  120. }
  121. for (size_t i = 0; i < sk->num; i++) {
  122. if (sk->data[i] != NULL) {
  123. call_free_func(free_func, sk->data[i]);
  124. }
  125. }
  126. sk_free(sk);
  127. }
  128. // Historically, |sk_pop_free| called the function as |stack_free_func|
  129. // directly. This is undefined in C. Some callers called |sk_pop_free| directly,
  130. // so we must maintain a compatibility version for now.
  131. static void call_free_func_legacy(stack_free_func func, void *ptr) {
  132. func(ptr);
  133. }
  134. void sk_pop_free(_STACK *sk, stack_free_func free_func) {
  135. sk_pop_free_ex(sk, call_free_func_legacy, free_func);
  136. }
  137. size_t sk_insert(_STACK *sk, void *p, size_t where) {
  138. if (sk == NULL) {
  139. return 0;
  140. }
  141. if (sk->num_alloc <= sk->num + 1) {
  142. // Attempt to double the size of the array.
  143. size_t new_alloc = sk->num_alloc << 1;
  144. size_t alloc_size = new_alloc * sizeof(void *);
  145. void **data;
  146. // If the doubling overflowed, try to increment.
  147. if (new_alloc < sk->num_alloc || alloc_size / sizeof(void *) != new_alloc) {
  148. new_alloc = sk->num_alloc + 1;
  149. alloc_size = new_alloc * sizeof(void *);
  150. }
  151. // If the increment also overflowed, fail.
  152. if (new_alloc < sk->num_alloc || alloc_size / sizeof(void *) != new_alloc) {
  153. return 0;
  154. }
  155. data = OPENSSL_realloc(sk->data, alloc_size);
  156. if (data == NULL) {
  157. return 0;
  158. }
  159. sk->data = data;
  160. sk->num_alloc = new_alloc;
  161. }
  162. if (where >= sk->num) {
  163. sk->data[sk->num] = p;
  164. } else {
  165. OPENSSL_memmove(&sk->data[where + 1], &sk->data[where],
  166. sizeof(void *) * (sk->num - where));
  167. sk->data[where] = p;
  168. }
  169. sk->num++;
  170. sk->sorted = 0;
  171. return sk->num;
  172. }
  173. void *sk_delete(_STACK *sk, size_t where) {
  174. void *ret;
  175. if (!sk || where >= sk->num) {
  176. return NULL;
  177. }
  178. ret = sk->data[where];
  179. if (where != sk->num - 1) {
  180. OPENSSL_memmove(&sk->data[where], &sk->data[where + 1],
  181. sizeof(void *) * (sk->num - where - 1));
  182. }
  183. sk->num--;
  184. return ret;
  185. }
  186. void *sk_delete_ptr(_STACK *sk, const void *p) {
  187. if (sk == NULL) {
  188. return NULL;
  189. }
  190. for (size_t i = 0; i < sk->num; i++) {
  191. if (sk->data[i] == p) {
  192. return sk_delete(sk, i);
  193. }
  194. }
  195. return NULL;
  196. }
  197. int sk_find(const _STACK *sk, size_t *out_index, const void *p,
  198. int (*call_cmp_func)(stack_cmp_func, const void **,
  199. const void **)) {
  200. if (sk == NULL) {
  201. return 0;
  202. }
  203. if (sk->comp == NULL) {
  204. // Use pointer equality when no comparison function has been set.
  205. for (size_t i = 0; i < sk->num; i++) {
  206. if (sk->data[i] == p) {
  207. if (out_index) {
  208. *out_index = i;
  209. }
  210. return 1;
  211. }
  212. }
  213. return 0;
  214. }
  215. if (p == NULL) {
  216. return 0;
  217. }
  218. if (!sk_is_sorted(sk)) {
  219. for (size_t i = 0; i < sk->num; i++) {
  220. const void *elem = sk->data[i];
  221. if (call_cmp_func(sk->comp, &p, &elem) == 0) {
  222. if (out_index) {
  223. *out_index = i;
  224. }
  225. return 1;
  226. }
  227. }
  228. return 0;
  229. }
  230. // sk->comp is a function that takes pointers to pointers to elements, but
  231. // qsort and bsearch take a comparison function that just takes pointers to
  232. // elements. However, since we're passing an array of pointers to
  233. // qsort/bsearch, we can just cast the comparison function and everything
  234. // works.
  235. //
  236. // TODO(davidben): This is undefined behavior, but the call is in libc so,
  237. // e.g., CFI does not notice. Unfortunately, |bsearch| is missing a void*
  238. // parameter in its callback and |bsearch_s| is a mess of incompatibility.
  239. const void *const *r = bsearch(&p, sk->data, sk->num, sizeof(void *),
  240. (int (*)(const void *, const void *))sk->comp);
  241. if (r == NULL) {
  242. return 0;
  243. }
  244. size_t idx = ((void **)r) - sk->data;
  245. // This function always returns the first result. Note this logic is, in the
  246. // worst case, O(N) rather than O(log(N)). If this ever becomes a problem,
  247. // restore https://boringssl-review.googlesource.com/c/boringssl/+/32115/
  248. // which integrates the preference into the binary search.
  249. while (idx > 0) {
  250. const void *elem = sk->data[idx - 1];
  251. if (call_cmp_func(sk->comp, &p, &elem) != 0) {
  252. break;
  253. }
  254. idx--;
  255. }
  256. if (out_index) {
  257. *out_index = idx;
  258. }
  259. return 1;
  260. }
  261. void *sk_shift(_STACK *sk) {
  262. if (sk == NULL) {
  263. return NULL;
  264. }
  265. if (sk->num == 0) {
  266. return NULL;
  267. }
  268. return sk_delete(sk, 0);
  269. }
  270. size_t sk_push(_STACK *sk, void *p) { return (sk_insert(sk, p, sk->num)); }
  271. void *sk_pop(_STACK *sk) {
  272. if (sk == NULL) {
  273. return NULL;
  274. }
  275. if (sk->num == 0) {
  276. return NULL;
  277. }
  278. return sk_delete(sk, sk->num - 1);
  279. }
  280. _STACK *sk_dup(const _STACK *sk) {
  281. _STACK *ret;
  282. void **s;
  283. if (sk == NULL) {
  284. return NULL;
  285. }
  286. ret = sk_new(sk->comp);
  287. if (ret == NULL) {
  288. goto err;
  289. }
  290. s = (void **)OPENSSL_realloc(ret->data, sizeof(void *) * sk->num_alloc);
  291. if (s == NULL) {
  292. goto err;
  293. }
  294. ret->data = s;
  295. ret->num = sk->num;
  296. OPENSSL_memcpy(ret->data, sk->data, sizeof(void *) * sk->num);
  297. ret->sorted = sk->sorted;
  298. ret->num_alloc = sk->num_alloc;
  299. ret->comp = sk->comp;
  300. return ret;
  301. err:
  302. sk_free(ret);
  303. return NULL;
  304. }
  305. void sk_sort(_STACK *sk) {
  306. if (sk == NULL || sk->comp == NULL || sk->sorted) {
  307. return;
  308. }
  309. // See the comment in sk_find about this cast.
  310. //
  311. // TODO(davidben): This is undefined behavior, but the call is in libc so,
  312. // e.g., CFI does not notice. Unfortunately, |qsort| is missing a void*
  313. // parameter in its callback and |qsort_s| / |qsort_r| are a mess of
  314. // incompatibility.
  315. if (sk->num >= 2) {
  316. int (*comp_func)(const void *, const void *) =
  317. (int (*)(const void *, const void *))(sk->comp);
  318. qsort(sk->data, sk->num, sizeof(void *), comp_func);
  319. }
  320. sk->sorted = 1;
  321. }
  322. int sk_is_sorted(const _STACK *sk) {
  323. if (!sk) {
  324. return 1;
  325. }
  326. return sk->sorted;
  327. }
  328. stack_cmp_func sk_set_cmp_func(_STACK *sk, stack_cmp_func comp) {
  329. stack_cmp_func old = sk->comp;
  330. if (sk->comp != comp) {
  331. sk->sorted = 0;
  332. }
  333. sk->comp = comp;
  334. return old;
  335. }
  336. _STACK *sk_deep_copy(const _STACK *sk,
  337. void *(*call_copy_func)(stack_copy_func, void *),
  338. stack_copy_func copy_func,
  339. void (*call_free_func)(stack_free_func, void *),
  340. stack_free_func free_func) {
  341. _STACK *ret = sk_dup(sk);
  342. if (ret == NULL) {
  343. return NULL;
  344. }
  345. for (size_t i = 0; i < ret->num; i++) {
  346. if (ret->data[i] == NULL) {
  347. continue;
  348. }
  349. ret->data[i] = call_copy_func(copy_func, ret->data[i]);
  350. if (ret->data[i] == NULL) {
  351. for (size_t j = 0; j < i; j++) {
  352. if (ret->data[j] != NULL) {
  353. call_free_func(free_func, ret->data[j]);
  354. }
  355. }
  356. sk_free(ret);
  357. return NULL;
  358. }
  359. }
  360. return ret;
  361. }