Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

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