2
0

o_names.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <openssl/err.h>
  5. #include <openssl/lhash.h>
  6. #include <openssl/objects.h>
  7. #include <openssl/safestack.h>
  8. #include <openssl/e_os2.h>
  9. /*
  10. * Later versions of DEC C has started to add lnkage information to certain
  11. * functions, which makes it tricky to use them as values to regular function
  12. * pointers. One way is to define a macro that takes care of casting them
  13. * correctly.
  14. */
  15. #ifdef OPENSSL_SYS_VMS_DECC
  16. # define OPENSSL_strcmp (int (*)(const char *,const char *))strcmp
  17. #else
  18. # define OPENSSL_strcmp strcmp
  19. #endif
  20. /*
  21. * I use the ex_data stuff to manage the identifiers for the obj_name_types
  22. * that applications may define. I only really use the free function field.
  23. */
  24. static LHASH *names_lh = NULL;
  25. static int names_type_num = OBJ_NAME_TYPE_NUM;
  26. typedef struct name_funcs_st {
  27. unsigned long (*hash_func) (const char *name);
  28. int (*cmp_func) (const char *a, const char *b);
  29. void (*free_func) (const char *, int, const char *);
  30. } NAME_FUNCS;
  31. DECLARE_STACK_OF(NAME_FUNCS)
  32. IMPLEMENT_STACK_OF(NAME_FUNCS)
  33. static STACK_OF(NAME_FUNCS) *name_funcs_stack;
  34. /*
  35. * The LHASH callbacks now use the raw "void *" prototypes and do
  36. * per-variable casting in the functions. This prevents function pointer
  37. * casting without the need for macro-generated wrapper functions.
  38. */
  39. /* static unsigned long obj_name_hash(OBJ_NAME *a); */
  40. static unsigned long obj_name_hash(const void *a_void);
  41. /* static int obj_name_cmp(OBJ_NAME *a,OBJ_NAME *b); */
  42. static int obj_name_cmp(const void *a_void, const void *b_void);
  43. int OBJ_NAME_init(void)
  44. {
  45. if (names_lh != NULL)
  46. return (1);
  47. MemCheck_off();
  48. names_lh = lh_new(obj_name_hash, obj_name_cmp);
  49. MemCheck_on();
  50. return (names_lh != NULL);
  51. }
  52. int OBJ_NAME_new_index(unsigned long (*hash_func) (const char *),
  53. int (*cmp_func) (const char *, const char *),
  54. void (*free_func) (const char *, int, const char *))
  55. {
  56. int ret;
  57. int i;
  58. NAME_FUNCS *name_funcs;
  59. if (name_funcs_stack == NULL) {
  60. MemCheck_off();
  61. name_funcs_stack = sk_NAME_FUNCS_new_null();
  62. MemCheck_on();
  63. }
  64. if ((name_funcs_stack == NULL)) {
  65. /* ERROR */
  66. return (0);
  67. }
  68. ret = names_type_num;
  69. names_type_num++;
  70. for (i = sk_NAME_FUNCS_num(name_funcs_stack); i < names_type_num; i++) {
  71. MemCheck_off();
  72. name_funcs = OPENSSL_malloc(sizeof(NAME_FUNCS));
  73. MemCheck_on();
  74. if (!name_funcs) {
  75. OBJerr(OBJ_F_OBJ_NAME_NEW_INDEX, ERR_R_MALLOC_FAILURE);
  76. return (0);
  77. }
  78. name_funcs->hash_func = lh_strhash;
  79. name_funcs->cmp_func = OPENSSL_strcmp;
  80. name_funcs->free_func = 0; /* NULL is often declared to * ((void
  81. * *)0), which according * to Compaq C is
  82. * not really * compatible with a function
  83. * * pointer. -- Richard Levitte */
  84. MemCheck_off();
  85. sk_NAME_FUNCS_push(name_funcs_stack, name_funcs);
  86. MemCheck_on();
  87. }
  88. name_funcs = sk_NAME_FUNCS_value(name_funcs_stack, ret);
  89. if (hash_func != NULL)
  90. name_funcs->hash_func = hash_func;
  91. if (cmp_func != NULL)
  92. name_funcs->cmp_func = cmp_func;
  93. if (free_func != NULL)
  94. name_funcs->free_func = free_func;
  95. return (ret);
  96. }
  97. /* static int obj_name_cmp(OBJ_NAME *a, OBJ_NAME *b) */
  98. static int obj_name_cmp(const void *a_void, const void *b_void)
  99. {
  100. int ret;
  101. const OBJ_NAME *a = (const OBJ_NAME *)a_void;
  102. const OBJ_NAME *b = (const OBJ_NAME *)b_void;
  103. ret = a->type - b->type;
  104. if (ret == 0) {
  105. if ((name_funcs_stack != NULL)
  106. && (sk_NAME_FUNCS_num(name_funcs_stack) > a->type)) {
  107. ret = sk_NAME_FUNCS_value(name_funcs_stack,
  108. a->type)->cmp_func(a->name, b->name);
  109. } else
  110. ret = strcmp(a->name, b->name);
  111. }
  112. return (ret);
  113. }
  114. /* static unsigned long obj_name_hash(OBJ_NAME *a) */
  115. static unsigned long obj_name_hash(const void *a_void)
  116. {
  117. unsigned long ret;
  118. const OBJ_NAME *a = (const OBJ_NAME *)a_void;
  119. if ((name_funcs_stack != NULL)
  120. && (sk_NAME_FUNCS_num(name_funcs_stack) > a->type)) {
  121. ret =
  122. sk_NAME_FUNCS_value(name_funcs_stack,
  123. a->type)->hash_func(a->name);
  124. } else {
  125. ret = lh_strhash(a->name);
  126. }
  127. ret ^= a->type;
  128. return (ret);
  129. }
  130. const char *OBJ_NAME_get(const char *name, int type)
  131. {
  132. OBJ_NAME on, *ret;
  133. int num = 0, alias;
  134. if (name == NULL)
  135. return (NULL);
  136. if ((names_lh == NULL) && !OBJ_NAME_init())
  137. return (NULL);
  138. alias = type & OBJ_NAME_ALIAS;
  139. type &= ~OBJ_NAME_ALIAS;
  140. on.name = name;
  141. on.type = type;
  142. for (;;) {
  143. ret = (OBJ_NAME *)lh_retrieve(names_lh, &on);
  144. if (ret == NULL)
  145. return (NULL);
  146. if ((ret->alias) && !alias) {
  147. if (++num > 10)
  148. return (NULL);
  149. on.name = ret->data;
  150. } else {
  151. return (ret->data);
  152. }
  153. }
  154. }
  155. int OBJ_NAME_add(const char *name, int type, const char *data)
  156. {
  157. OBJ_NAME *onp, *ret;
  158. int alias;
  159. if ((names_lh == NULL) && !OBJ_NAME_init())
  160. return (0);
  161. alias = type & OBJ_NAME_ALIAS;
  162. type &= ~OBJ_NAME_ALIAS;
  163. onp = (OBJ_NAME *)OPENSSL_malloc(sizeof(OBJ_NAME));
  164. if (onp == NULL) {
  165. /* ERROR */
  166. return (0);
  167. }
  168. onp->name = name;
  169. onp->alias = alias;
  170. onp->type = type;
  171. onp->data = data;
  172. ret = (OBJ_NAME *)lh_insert(names_lh, onp);
  173. if (ret != NULL) {
  174. /* free things */
  175. if ((name_funcs_stack != NULL)
  176. && (sk_NAME_FUNCS_num(name_funcs_stack) > ret->type)) {
  177. /*
  178. * XXX: I'm not sure I understand why the free function should
  179. * get three arguments... -- Richard Levitte
  180. */
  181. sk_NAME_FUNCS_value(name_funcs_stack,
  182. ret->type)->free_func(ret->name, ret->type,
  183. ret->data);
  184. }
  185. OPENSSL_free(ret);
  186. } else {
  187. if (lh_error(names_lh)) {
  188. /* ERROR */
  189. return (0);
  190. }
  191. }
  192. return (1);
  193. }
  194. int OBJ_NAME_remove(const char *name, int type)
  195. {
  196. OBJ_NAME on, *ret;
  197. if (names_lh == NULL)
  198. return (0);
  199. type &= ~OBJ_NAME_ALIAS;
  200. on.name = name;
  201. on.type = type;
  202. ret = (OBJ_NAME *)lh_delete(names_lh, &on);
  203. if (ret != NULL) {
  204. /* free things */
  205. if ((name_funcs_stack != NULL)
  206. && (sk_NAME_FUNCS_num(name_funcs_stack) > ret->type)) {
  207. /*
  208. * XXX: I'm not sure I understand why the free function should
  209. * get three arguments... -- Richard Levitte
  210. */
  211. sk_NAME_FUNCS_value(name_funcs_stack,
  212. ret->type)->free_func(ret->name, ret->type,
  213. ret->data);
  214. }
  215. OPENSSL_free(ret);
  216. return (1);
  217. } else
  218. return (0);
  219. }
  220. struct doall {
  221. int type;
  222. void (*fn) (const OBJ_NAME *, void *arg);
  223. void *arg;
  224. };
  225. static void do_all_fn(const OBJ_NAME *name, struct doall *d)
  226. {
  227. if (name->type == d->type)
  228. d->fn(name, d->arg);
  229. }
  230. static IMPLEMENT_LHASH_DOALL_ARG_FN(do_all_fn, const OBJ_NAME *,
  231. struct doall *)
  232. void OBJ_NAME_do_all(int type, void (*fn) (const OBJ_NAME *, void *arg),
  233. void *arg)
  234. {
  235. struct doall d;
  236. d.type = type;
  237. d.fn = fn;
  238. d.arg = arg;
  239. lh_doall_arg(names_lh, LHASH_DOALL_ARG_FN(do_all_fn), &d);
  240. }
  241. struct doall_sorted {
  242. int type;
  243. int n;
  244. const OBJ_NAME **names;
  245. };
  246. static void do_all_sorted_fn(const OBJ_NAME *name, void *d_)
  247. {
  248. struct doall_sorted *d = d_;
  249. if (name->type != d->type)
  250. return;
  251. d->names[d->n++] = name;
  252. }
  253. static int do_all_sorted_cmp(const void *n1_, const void *n2_)
  254. {
  255. const OBJ_NAME *const *n1 = n1_;
  256. const OBJ_NAME *const *n2 = n2_;
  257. return strcmp((*n1)->name, (*n2)->name);
  258. }
  259. void OBJ_NAME_do_all_sorted(int type,
  260. void (*fn) (const OBJ_NAME *, void *arg),
  261. void *arg)
  262. {
  263. struct doall_sorted d;
  264. int n;
  265. d.type = type;
  266. d.names = OPENSSL_malloc(lh_num_items(names_lh) * sizeof *d.names);
  267. d.n = 0;
  268. OBJ_NAME_do_all(type, do_all_sorted_fn, &d);
  269. qsort((void *)d.names, d.n, sizeof *d.names, do_all_sorted_cmp);
  270. for (n = 0; n < d.n; ++n)
  271. fn(d.names[n], arg);
  272. OPENSSL_free((void *)d.names);
  273. }
  274. static int free_type;
  275. static void names_lh_free(OBJ_NAME *onp)
  276. {
  277. if (onp == NULL)
  278. return;
  279. if ((free_type < 0) || (free_type == onp->type)) {
  280. OBJ_NAME_remove(onp->name, onp->type);
  281. }
  282. }
  283. static IMPLEMENT_LHASH_DOALL_FN(names_lh_free, OBJ_NAME *)
  284. static void name_funcs_free(NAME_FUNCS *ptr)
  285. {
  286. OPENSSL_free(ptr);
  287. }
  288. void OBJ_NAME_cleanup(int type)
  289. {
  290. unsigned long down_load;
  291. if (names_lh == NULL)
  292. return;
  293. free_type = type;
  294. down_load = names_lh->down_load;
  295. names_lh->down_load = 0;
  296. lh_doall(names_lh, LHASH_DOALL_FN(names_lh_free));
  297. if (type < 0) {
  298. lh_free(names_lh);
  299. sk_NAME_FUNCS_pop_free(name_funcs_stack, name_funcs_free);
  300. names_lh = NULL;
  301. name_funcs_stack = NULL;
  302. } else
  303. names_lh->down_load = down_load;
  304. }