A local copy of OpenSSL from GitHub
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.

313 lines
6.9 KiB

  1. /*
  2. * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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 <openssl/stack.h>
  12. #include <openssl/objects.h>
  13. struct stack_st {
  14. int num;
  15. char **data;
  16. int sorted;
  17. int num_alloc;
  18. int (*comp) (const void *, const void *);
  19. };
  20. #undef MIN_NODES
  21. #define MIN_NODES 4
  22. #include <errno.h>
  23. int (*sk_set_cmp_func(_STACK *sk, int (*c) (const void *, const void *)))
  24. (const void *, const void *) {
  25. int (*old) (const void *, const void *) = sk->comp;
  26. if (sk->comp != c)
  27. sk->sorted = 0;
  28. sk->comp = c;
  29. return old;
  30. }
  31. _STACK *sk_dup(_STACK *sk)
  32. {
  33. _STACK *ret;
  34. char **s;
  35. if ((ret = sk_new(sk->comp)) == NULL)
  36. goto err;
  37. s = OPENSSL_realloc((char *)ret->data,
  38. (unsigned int)sizeof(char *) * sk->num_alloc);
  39. if (s == NULL)
  40. goto err;
  41. ret->data = s;
  42. ret->num = sk->num;
  43. memcpy(ret->data, sk->data, sizeof(char *) * sk->num);
  44. ret->sorted = sk->sorted;
  45. ret->num_alloc = sk->num_alloc;
  46. ret->comp = sk->comp;
  47. return (ret);
  48. err:
  49. sk_free(ret);
  50. return (NULL);
  51. }
  52. _STACK *sk_deep_copy(_STACK *sk, void *(*copy_func) (void *),
  53. void (*free_func) (void *))
  54. {
  55. _STACK *ret;
  56. int i;
  57. if ((ret = OPENSSL_malloc(sizeof(_STACK))) == NULL)
  58. return ret;
  59. ret->comp = sk->comp;
  60. ret->sorted = sk->sorted;
  61. ret->num = sk->num;
  62. ret->num_alloc = sk->num > MIN_NODES ? sk->num : MIN_NODES;
  63. ret->data = OPENSSL_malloc(sizeof(*ret->data) * ret->num_alloc);
  64. if (ret->data == NULL) {
  65. OPENSSL_free(ret);
  66. return NULL;
  67. }
  68. for (i = 0; i < ret->num_alloc; i++)
  69. ret->data[i] = NULL;
  70. for (i = 0; i < ret->num; ++i) {
  71. if (sk->data[i] == NULL)
  72. continue;
  73. if ((ret->data[i] = copy_func(sk->data[i])) == NULL) {
  74. while (--i >= 0)
  75. if (ret->data[i] != NULL)
  76. free_func(ret->data[i]);
  77. sk_free(ret);
  78. return NULL;
  79. }
  80. }
  81. return ret;
  82. }
  83. _STACK *sk_new_null(void)
  84. {
  85. return sk_new((int (*)(const void *, const void *))0);
  86. }
  87. _STACK *sk_new(int (*c) (const void *, const void *))
  88. {
  89. _STACK *ret;
  90. if ((ret = OPENSSL_zalloc(sizeof(_STACK))) == NULL)
  91. goto err;
  92. if ((ret->data = OPENSSL_zalloc(sizeof(*ret->data) * MIN_NODES)) == NULL)
  93. goto err;
  94. ret->comp = c;
  95. ret->num_alloc = MIN_NODES;
  96. return (ret);
  97. err:
  98. OPENSSL_free(ret);
  99. return (NULL);
  100. }
  101. int sk_insert(_STACK *st, void *data, int loc)
  102. {
  103. char **s;
  104. if (st == NULL)
  105. return 0;
  106. if (st->num_alloc <= st->num + 1) {
  107. s = OPENSSL_realloc((char *)st->data,
  108. (unsigned int)sizeof(char *) * st->num_alloc * 2);
  109. if (s == NULL)
  110. return (0);
  111. st->data = s;
  112. st->num_alloc *= 2;
  113. }
  114. if ((loc >= (int)st->num) || (loc < 0))
  115. st->data[st->num] = data;
  116. else {
  117. memmove(&(st->data[loc + 1]),
  118. &(st->data[loc]), sizeof(char *) * (st->num - loc));
  119. st->data[loc] = data;
  120. }
  121. st->num++;
  122. st->sorted = 0;
  123. return (st->num);
  124. }
  125. void *sk_delete_ptr(_STACK *st, void *p)
  126. {
  127. int i;
  128. for (i = 0; i < st->num; i++)
  129. if (st->data[i] == p)
  130. return (sk_delete(st, i));
  131. return (NULL);
  132. }
  133. void *sk_delete(_STACK *st, int loc)
  134. {
  135. char *ret;
  136. int i, j;
  137. if (!st || (loc < 0) || (loc >= st->num))
  138. return NULL;
  139. ret = st->data[loc];
  140. if (loc != st->num - 1) {
  141. j = st->num - 1;
  142. for (i = loc; i < j; i++)
  143. st->data[i] = st->data[i + 1];
  144. /*
  145. * In theory memcpy is not safe for this memcpy( &(st->data[loc]),
  146. * &(st->data[loc+1]), sizeof(char *)*(st->num-loc-1));
  147. */
  148. }
  149. st->num--;
  150. return (ret);
  151. }
  152. static int internal_find(_STACK *st, void *data, int ret_val_options)
  153. {
  154. const void *const *r;
  155. int i;
  156. if (st == NULL)
  157. return -1;
  158. if (st->comp == NULL) {
  159. for (i = 0; i < st->num; i++)
  160. if (st->data[i] == data)
  161. return (i);
  162. return (-1);
  163. }
  164. sk_sort(st);
  165. if (data == NULL)
  166. return (-1);
  167. r = OBJ_bsearch_ex_(&data, st->data, st->num, sizeof(void *), st->comp,
  168. ret_val_options);
  169. if (r == NULL)
  170. return (-1);
  171. return (int)((char **)r - st->data);
  172. }
  173. int sk_find(_STACK *st, void *data)
  174. {
  175. return internal_find(st, data, OBJ_BSEARCH_FIRST_VALUE_ON_MATCH);
  176. }
  177. int sk_find_ex(_STACK *st, void *data)
  178. {
  179. return internal_find(st, data, OBJ_BSEARCH_VALUE_ON_NOMATCH);
  180. }
  181. int sk_push(_STACK *st, void *data)
  182. {
  183. return (sk_insert(st, data, st->num));
  184. }
  185. int sk_unshift(_STACK *st, void *data)
  186. {
  187. return (sk_insert(st, data, 0));
  188. }
  189. void *sk_shift(_STACK *st)
  190. {
  191. if (st == NULL)
  192. return (NULL);
  193. if (st->num <= 0)
  194. return (NULL);
  195. return (sk_delete(st, 0));
  196. }
  197. void *sk_pop(_STACK *st)
  198. {
  199. if (st == NULL)
  200. return (NULL);
  201. if (st->num <= 0)
  202. return (NULL);
  203. return (sk_delete(st, st->num - 1));
  204. }
  205. void sk_zero(_STACK *st)
  206. {
  207. if (st == NULL)
  208. return;
  209. if (st->num <= 0)
  210. return;
  211. memset(st->data, 0, sizeof(*st->data) * st->num);
  212. st->num = 0;
  213. }
  214. void sk_pop_free(_STACK *st, void (*func) (void *))
  215. {
  216. int i;
  217. if (st == NULL)
  218. return;
  219. for (i = 0; i < st->num; i++)
  220. if (st->data[i] != NULL)
  221. func(st->data[i]);
  222. sk_free(st);
  223. }
  224. void sk_free(_STACK *st)
  225. {
  226. if (st == NULL)
  227. return;
  228. OPENSSL_free(st->data);
  229. OPENSSL_free(st);
  230. }
  231. int sk_num(const _STACK *st)
  232. {
  233. if (st == NULL)
  234. return -1;
  235. return st->num;
  236. }
  237. void *sk_value(const _STACK *st, int i)
  238. {
  239. if (!st || (i < 0) || (i >= st->num))
  240. return NULL;
  241. return st->data[i];
  242. }
  243. void *sk_set(_STACK *st, int i, void *value)
  244. {
  245. if (!st || (i < 0) || (i >= st->num))
  246. return NULL;
  247. return (st->data[i] = value);
  248. }
  249. void sk_sort(_STACK *st)
  250. {
  251. if (st && !st->sorted && st->comp != NULL) {
  252. int (*comp_func) (const void *, const void *);
  253. /*
  254. * same comment as in sk_find ... previously st->comp was declared as
  255. * a (void*,void*) callback type, but this made the population of the
  256. * callback pointer illogical - our callbacks compare type** with
  257. * type**, so we leave the casting until absolutely necessary (ie.
  258. * "now").
  259. */
  260. comp_func = (int (*)(const void *, const void *))(st->comp);
  261. qsort(st->data, st->num, sizeof(char *), comp_func);
  262. st->sorted = 1;
  263. }
  264. }
  265. int sk_is_sorted(const _STACK *st)
  266. {
  267. if (!st)
  268. return 1;
  269. return st->sorted;
  270. }