dso_beos.c 7.3 KB

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