dso_beos.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /* dso_beos.c */
  2. /*
  3. * Written by Marcin Konicki (ahwayakchih@neoni.net) for the OpenSSL project
  4. * 2000.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 2000 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * licensing@OpenSSL.org.
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. *
  54. * This product includes cryptographic software written by Eric Young
  55. * (eay@cryptsoft.com). This product includes software written by Tim
  56. * Hudson (tjh@cryptsoft.com).
  57. *
  58. */
  59. #include <stdio.h>
  60. #include <string.h>
  61. #include "cryptlib.h"
  62. #include <openssl/dso.h>
  63. #if !defined(OPENSSL_SYS_BEOS)
  64. DSO_METHOD *DSO_METHOD_beos(void)
  65. {
  66. return NULL;
  67. }
  68. #else
  69. # include <kernel/image.h>
  70. static int beos_load(DSO *dso);
  71. static int beos_unload(DSO *dso);
  72. static void *beos_bind_var(DSO *dso, const char *symname);
  73. static DSO_FUNC_TYPE beos_bind_func(DSO *dso, const char *symname);
  74. # if 0
  75. static int beos_unbind_var(DSO *dso, char *symname, void *symptr);
  76. static int beos_unbind_func(DSO *dso, char *symname, DSO_FUNC_TYPE symptr);
  77. static int beos_init(DSO *dso);
  78. static int beos_finish(DSO *dso);
  79. static long beos_ctrl(DSO *dso, int cmd, long larg, void *parg);
  80. # endif
  81. static char *beos_name_converter(DSO *dso, const char *filename);
  82. static DSO_METHOD dso_meth_beos = {
  83. "OpenSSL 'beos' shared library method",
  84. beos_load,
  85. beos_unload,
  86. beos_bind_var,
  87. beos_bind_func,
  88. /* For now, "unbind" doesn't exist */
  89. # if 0
  90. NULL, /* unbind_var */
  91. NULL, /* unbind_func */
  92. # endif
  93. NULL, /* ctrl */
  94. beos_name_converter,
  95. NULL, /* init */
  96. NULL /* finish */
  97. };
  98. DSO_METHOD *DSO_METHOD_beos(void)
  99. {
  100. return (&dso_meth_beos);
  101. }
  102. /*
  103. * For this DSO_METHOD, our meth_data STACK will contain; (i) a pointer to
  104. * the handle (image_id) returned from load_add_on().
  105. */
  106. static int beos_load(DSO *dso)
  107. {
  108. image_id id;
  109. /* See applicable comments from dso_dl.c */
  110. char *filename = DSO_convert_filename(dso, NULL);
  111. if (filename == NULL) {
  112. DSOerr(DSO_F_BEOS_LOAD, DSO_R_NO_FILENAME);
  113. goto err;
  114. }
  115. id = load_add_on(filename);
  116. if (id < 1) {
  117. DSOerr(DSO_F_BEOS_LOAD, DSO_R_LOAD_FAILED);
  118. ERR_add_error_data(3, "filename(", filename, ")");
  119. goto err;
  120. }
  121. if (!sk_push(dso->meth_data, (char *)id)) {
  122. DSOerr(DSO_F_BEOS_LOAD, DSO_R_STACK_ERROR);
  123. goto err;
  124. }
  125. /* Success */
  126. dso->loaded_filename = filename;
  127. return (1);
  128. err:
  129. /* Cleanup ! */
  130. if (filename != NULL)
  131. OPENSSL_free(filename);
  132. if (id > 0)
  133. unload_add_on(id);
  134. return (0);
  135. }
  136. static int beos_unload(DSO *dso)
  137. {
  138. image_id id;
  139. if (dso == NULL) {
  140. DSOerr(DSO_F_BEOS_UNLOAD, ERR_R_PASSED_NULL_PARAMETER);
  141. return (0);
  142. }
  143. if (sk_num(dso->meth_data) < 1)
  144. return (1);
  145. id = (image_id) sk_pop(dso->meth_data);
  146. if (id < 1) {
  147. DSOerr(DSO_F_BEOS_UNLOAD, DSO_R_NULL_HANDLE);
  148. return (0);
  149. }
  150. if (unload_add_on(id) != B_OK) {
  151. DSOerr(DSO_F_BEOS_UNLOAD, DSO_R_UNLOAD_FAILED);
  152. /*
  153. * We should push the value back onto the stack in case of a retry.
  154. */
  155. sk_push(dso->meth_data, (char *)id);
  156. return (0);
  157. }
  158. return (1);
  159. }
  160. static void *beos_bind_var(DSO *dso, const char *symname)
  161. {
  162. image_id id;
  163. void *sym;
  164. if ((dso == NULL) || (symname == NULL)) {
  165. DSOerr(DSO_F_BEOS_BIND_VAR, ERR_R_PASSED_NULL_PARAMETER);
  166. return (NULL);
  167. }
  168. if (sk_num(dso->meth_data) < 1) {
  169. DSOerr(DSO_F_BEOS_BIND_VAR, DSO_R_STACK_ERROR);
  170. return (NULL);
  171. }
  172. id = (image_id) sk_value(dso->meth_data, sk_num(dso->meth_data) - 1);
  173. if (id < 1) {
  174. DSOerr(DSO_F_BEOS_BIND_VAR, DSO_R_NULL_HANDLE);
  175. return (NULL);
  176. }
  177. if (get_image_symbol(id, symname, B_SYMBOL_TYPE_DATA, &sym) != B_OK) {
  178. DSOerr(DSO_F_BEOS_BIND_VAR, DSO_R_SYM_FAILURE);
  179. ERR_add_error_data(3, "symname(", symname, ")");
  180. return (NULL);
  181. }
  182. return (sym);
  183. }
  184. static DSO_FUNC_TYPE beos_bind_func(DSO *dso, const char *symname)
  185. {
  186. image_id id;
  187. void *sym;
  188. if ((dso == NULL) || (symname == NULL)) {
  189. DSOerr(DSO_F_BEOS_BIND_FUNC, ERR_R_PASSED_NULL_PARAMETER);
  190. return (NULL);
  191. }
  192. if (sk_num(dso->meth_data) < 1) {
  193. DSOerr(DSO_F_BEOS_BIND_FUNC, DSO_R_STACK_ERROR);
  194. return (NULL);
  195. }
  196. id = (image_id) sk_value(dso->meth_data, sk_num(dso->meth_data) - 1);
  197. if (id < 1) {
  198. DSOerr(DSO_F_BEOS_BIND_FUNC, DSO_R_NULL_HANDLE);
  199. return (NULL);
  200. }
  201. if (get_image_symbol(id, symname, B_SYMBOL_TYPE_TEXT, &sym) != B_OK) {
  202. DSOerr(DSO_F_BEOS_BIND_FUNC, DSO_R_SYM_FAILURE);
  203. ERR_add_error_data(3, "symname(", symname, ")");
  204. return (NULL);
  205. }
  206. return ((DSO_FUNC_TYPE)sym);
  207. }
  208. /* This one is the same as the one in dlfcn */
  209. static char *beos_name_converter(DSO *dso, const char *filename)
  210. {
  211. char *translated;
  212. int len, rsize, transform;
  213. len = strlen(filename);
  214. rsize = len + 1;
  215. transform = (strstr(filename, "/") == NULL);
  216. if (transform) {
  217. /* We will convert this to "%s.so" or "lib%s.so" */
  218. rsize += 3; /* The length of ".so" */
  219. if ((DSO_flags(dso) & DSO_FLAG_NAME_TRANSLATION_EXT_ONLY) == 0)
  220. rsize += 3; /* The length of "lib" */
  221. }
  222. translated = OPENSSL_malloc(rsize);
  223. if (translated == NULL) {
  224. DSOerr(DSO_F_BEOS_NAME_CONVERTER, DSO_R_NAME_TRANSLATION_FAILED);
  225. return (NULL);
  226. }
  227. if (transform) {
  228. if ((DSO_flags(dso) & DSO_FLAG_NAME_TRANSLATION_EXT_ONLY) == 0)
  229. sprintf(translated, "lib%s.so", filename);
  230. else
  231. sprintf(translated, "%s.so", filename);
  232. } else
  233. sprintf(translated, "%s", filename);
  234. return (translated);
  235. }
  236. #endif