dso_beos.c revision 296341
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
60#include <stdio.h>
61#include <string.h>
62#include "cryptlib.h"
63#include <openssl/dso.h>
64
65#if !defined(OPENSSL_SYS_BEOS)
66DSO_METHOD *DSO_METHOD_beos(void)
67{
68    return NULL;
69}
70#else
71
72# include <kernel/image.h>
73
74static int beos_load(DSO *dso);
75static int beos_unload(DSO *dso);
76static void *beos_bind_var(DSO *dso, const char *symname);
77static DSO_FUNC_TYPE beos_bind_func(DSO *dso, const char *symname);
78# if 0
79static int beos_unbind_var(DSO *dso, char *symname, void *symptr);
80static int beos_unbind_func(DSO *dso, char *symname, DSO_FUNC_TYPE symptr);
81static int beos_init(DSO *dso);
82static int beos_finish(DSO *dso);
83static long beos_ctrl(DSO *dso, int cmd, long larg, void *parg);
84# endif
85static char *beos_name_converter(DSO *dso, const char *filename);
86
87static DSO_METHOD dso_meth_beos = {
88    "OpenSSL 'beos' shared library method",
89    beos_load,
90    beos_unload,
91    beos_bind_var,
92    beos_bind_func,
93/* For now, "unbind" doesn't exist */
94# if 0
95    NULL,                       /* unbind_var */
96    NULL,                       /* unbind_func */
97# endif
98    NULL,                       /* ctrl */
99    beos_name_converter,
100    NULL,                       /* init */
101    NULL                        /* finish */
102};
103
104DSO_METHOD *DSO_METHOD_beos(void)
105{
106    return (&dso_meth_beos);
107}
108
109/*
110 * For this DSO_METHOD, our meth_data STACK will contain; (i) a pointer to
111 * the handle (image_id) returned from load_add_on().
112 */
113
114static int beos_load(DSO *dso)
115{
116    image_id id;
117    /* See applicable comments from dso_dl.c */
118    char *filename = DSO_convert_filename(dso, NULL);
119
120    if (filename == NULL) {
121        DSOerr(DSO_F_BEOS_LOAD, DSO_R_NO_FILENAME);
122        goto err;
123    }
124    id = load_add_on(filename);
125    if (id < 1) {
126        DSOerr(DSO_F_BEOS_LOAD, DSO_R_LOAD_FAILED);
127        ERR_add_error_data(3, "filename(", filename, ")");
128        goto err;
129    }
130    if (!sk_push(dso->meth_data, (char *)id)) {
131        DSOerr(DSO_F_BEOS_LOAD, DSO_R_STACK_ERROR);
132        goto err;
133    }
134    /* Success */
135    dso->loaded_filename = filename;
136    return (1);
137 err:
138    /* Cleanup ! */
139    if (filename != NULL)
140        OPENSSL_free(filename);
141    if (id > 0)
142        unload_add_on(id);
143    return (0);
144}
145
146static int beos_unload(DSO *dso)
147{
148    image_id id;
149    if (dso == NULL) {
150        DSOerr(DSO_F_BEOS_UNLOAD, ERR_R_PASSED_NULL_PARAMETER);
151        return (0);
152    }
153    if (sk_num(dso->meth_data) < 1)
154        return (1);
155    id = (image_id) sk_pop(dso->meth_data);
156    if (id < 1) {
157        DSOerr(DSO_F_BEOS_UNLOAD, DSO_R_NULL_HANDLE);
158        return (0);
159    }
160    if (unload_add_on(id) != B_OK) {
161        DSOerr(DSO_F_BEOS_UNLOAD, DSO_R_UNLOAD_FAILED);
162        /*
163         * We should push the value back onto the stack in case of a retry.
164         */
165        sk_push(dso->meth_data, (char *)id);
166        return (0);
167    }
168    return (1);
169}
170
171static void *beos_bind_var(DSO *dso, const char *symname)
172{
173    image_id id;
174    void *sym;
175
176    if ((dso == NULL) || (symname == NULL)) {
177        DSOerr(DSO_F_BEOS_BIND_VAR, ERR_R_PASSED_NULL_PARAMETER);
178        return (NULL);
179    }
180    if (sk_num(dso->meth_data) < 1) {
181        DSOerr(DSO_F_BEOS_BIND_VAR, DSO_R_STACK_ERROR);
182        return (NULL);
183    }
184    id = (image_id) sk_value(dso->meth_data, sk_num(dso->meth_data) - 1);
185    if (id < 1) {
186        DSOerr(DSO_F_BEOS_BIND_VAR, DSO_R_NULL_HANDLE);
187        return (NULL);
188    }
189    if (get_image_symbol(id, symname, B_SYMBOL_TYPE_DATA, &sym) != B_OK) {
190        DSOerr(DSO_F_BEOS_BIND_VAR, DSO_R_SYM_FAILURE);
191        ERR_add_error_data(3, "symname(", symname, ")");
192        return (NULL);
193    }
194    return (sym);
195}
196
197static DSO_FUNC_TYPE beos_bind_func(DSO *dso, const char *symname)
198{
199    image_id id;
200    void *sym;
201
202    if ((dso == NULL) || (symname == NULL)) {
203        DSOerr(DSO_F_BEOS_BIND_FUNC, ERR_R_PASSED_NULL_PARAMETER);
204        return (NULL);
205    }
206    if (sk_num(dso->meth_data) < 1) {
207        DSOerr(DSO_F_BEOS_BIND_FUNC, DSO_R_STACK_ERROR);
208        return (NULL);
209    }
210    id = (image_id) sk_value(dso->meth_data, sk_num(dso->meth_data) - 1);
211    if (id < 1) {
212        DSOerr(DSO_F_BEOS_BIND_FUNC, DSO_R_NULL_HANDLE);
213        return (NULL);
214    }
215    if (get_image_symbol(id, symname, B_SYMBOL_TYPE_TEXT, &sym) != B_OK) {
216        DSOerr(DSO_F_BEOS_BIND_FUNC, DSO_R_SYM_FAILURE);
217        ERR_add_error_data(3, "symname(", symname, ")");
218        return (NULL);
219    }
220    return ((DSO_FUNC_TYPE)sym);
221}
222
223/* This one is the same as the one in dlfcn */
224static char *beos_name_converter(DSO *dso, const char *filename)
225{
226    char *translated;
227    int len, rsize, transform;
228
229    len = strlen(filename);
230    rsize = len + 1;
231    transform = (strstr(filename, "/") == NULL);
232    if (transform) {
233        /* We will convert this to "%s.so" or "lib%s.so" */
234        rsize += 3;             /* The length of ".so" */
235        if ((DSO_flags(dso) & DSO_FLAG_NAME_TRANSLATION_EXT_ONLY) == 0)
236            rsize += 3;         /* The length of "lib" */
237    }
238    translated = OPENSSL_malloc(rsize);
239    if (translated == NULL) {
240        DSOerr(DSO_F_BEOS_NAME_CONVERTER, DSO_R_NAME_TRANSLATION_FAILED);
241        return (NULL);
242    }
243    if (transform) {
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    } else
249        sprintf(translated, "%s", filename);
250    return (translated);
251}
252
253#endif
254