1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/* GATE PROJECT LICENSE:
+----------------------------------------------------------------------------+
| Copyright(c) 2018-2025, Stefan Meislinger <sm@opengate.at>                 |
| All rights reserved.                                                       |
|                                                                            |
| Redistribution and use in source and binary forms, with or without         |
| modification, are permitted provided that the following conditions are met:|
|                                                                            |
| 1. Redistributions of source code must retain the above copyright notice,  |
|    this list of conditions and the following disclaimer.                   |
| 2. Redistributions in binary form must reproduce the above copyright       |
|    notice, this list of conditions and the following disclaimer in the     |
|    documentation and/or other materials provided with the distribution.    |
|                                                                            |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE  |
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE    |
| LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR        |
| CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF       |
| SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS   |
| INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN    |
| CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)    |
| ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF     |
| THE POSSIBILITY OF SUCH DAMAGE.                                            |
+----------------------------------------------------------------------------+
*/

#include "gate/libraries.h"
#include "gate/results.h"
#include "gate/platforms.h"

#if defined(GATE_SYS_WIN)
#	define GATE_CORE_LIBRARIES_USE_WINAPI 1
#elif defined(GATE_SYS_POSIX)
#	define GATE_CORE_LIBRARIES_USE_POSIX 1
#else
#	define GATE_CORE_LIBRARIES_NO_IMPL 1
#endif


gate_result_t gate_library_get_function_name(gate_library_t lib, char const* funcname, void* ptrfunc)
{
    gate_library_function_t func;
    gate_string_t name;
    gate_result_t res;
    gate_string_create_static(&name, funcname);
    res = gate_library_get_function(lib, &name, &func);
    if (GATE_SUCCEEDED(res))
    {
        *((gate_library_function_t*)ptrfunc) = func;
    }
    else
    {
        *((gate_library_function_t*)ptrfunc) = NULL;
    }
    return res;
}

gate_result_t gate_library_get_data_name(gate_library_t lib, char const* entryname, void* ptr_data)
{
    void* data;
    gate_string_t name;
    gate_result_t res;
    gate_string_create_static(&name, entryname);
    res = gate_library_get_data(lib, &name, &data);
    if (GATE_SUCCEEDED(res))
    {
        *((void**)ptr_data) = data;
    }
    else
    {
        *((void**)ptr_data) = NULL;
    }
    return res;
}



#if defined(GATE_CORE_LIBRARIES_USE_WINAPI)

#ifndef DONT_RESOLVE_DLL_REFERENCES
#define DONT_RESOLVE_DLL_REFERENCES         0x00000001
#endif

static gate_bool_t is_hmodule_valid(HMODULE hmod)
{
#if defined(GATE_SYS_WIN16)
    return (hmod > HINSTANCE_ERROR);
#else
    return (hmod != NULL);
#endif
}

gate_bool_t gate_library_support()
{
    return true;
}

gate_result_t gate_library_open(gate_string_t const* path, gate_library_t* lib, gate_enumint_t flags)
{
    gate_result_t ret;
    TCHAR libpath[GATE_MAX_FILEPATH_LENGTH];
    HMODULE hmod;
    DWORD dwflags = 0;
    gate_size_t libpathlen = gate_win32_utf8_2_winstr(path->str, path->length, libpath, sizeof(libpath) / sizeof(libpath[0]));
    if (libpathlen == 0)
    {
        return GATE_RESULT_INVALIDDATA;
    }
    if (GATE_FLAG_ENABLED(flags, GATE_LIBRARY_FLAG_DATA))
    {
        dwflags |= DONT_RESOLVE_DLL_REFERENCES;
    }
    hmod = gate_win32_load_library(libpath, dwflags);
    if (is_hmodule_valid(hmod))
    {
        *lib = (gate_library_t)hmod;
        ret = GATE_RESULT_OK;
    }
    else
    {
        gate_win32_print_lasterror(&ret, NULL, 0);
    }
    return ret;
}
gate_result_t gate_library_close(gate_library_t lib)
{
    gate_result_t ret;
    HMODULE hmod = (HMODULE)lib;
    BOOL succeeded = true;
#if !defined(GATE_SYS_WIN16)
    succeeded =
#endif
        FreeLibrary(hmod);
    if (succeeded)
    {
        ret = GATE_RESULT_OK;
    }
    else
    {
        gate_win32_print_lasterror(&ret, NULL, 0);
    }
    return ret;
}
gate_result_t gate_library_get_function(gate_library_t lib, gate_string_t const* entryname, gate_library_function_t* func)
{
    gate_result_t ret;
    HMODULE hmod = (HMODULE)lib;
    gate_size_t procnamelen;
    FARPROC proc;
#if defined(GATE_SYS_WINCE)
    TCHAR procname[GATE_MAX_FILENAME_LENGTH];
    procnamelen = gate_win32_utf8_2_winstr(entryname->str, entryname->length, procname, sizeof(procname) / sizeof(procname[0]));
#else
    char procname[GATE_MAX_FILENAME_LENGTH];
    procnamelen = gate_str_print_text(procname, sizeof(procname) / sizeof(procname[0]), entryname->str, entryname->length);
#endif
    if (procnamelen == 0)
    {
        return GATE_RESULT_INVALIDDATA;
    }
    proc = GetProcAddress(hmod, procname);
    if (proc == NULL)
    {
        gate_win32_print_lasterror(&ret, NULL, 0);
    }
    else
    {
        *func = (gate_library_function_t)proc;
        ret = GATE_RESULT_OK;
    }
    return ret;
}
gate_result_t gate_library_get_data(gate_library_t lib, gate_string_t const* entryname, void** data)
{
    gate_library_function_t ptr;
    gate_result_t ret = gate_library_get_function(lib, entryname, &ptr);
    if (GATE_SUCCEEDED(ret))
    {
        *data = (void*)ptr;
    }
    return ret;
}

#endif /* GATE_CORE_LIBRARIES_USE_WINAPI */



#if defined(GATE_CORE_LIBRARIES_USE_POSIX)

#	include "dlfcn.h"

gate_bool_t gate_library_support()
{
    static gate_bool_t volatile test_completed = false;
    static gate_bool_t volatile test_result = false;

    if (!test_completed)
    {
        void* proc_handle = gate_posix_dlopen(NULL, RTLD_LAZY);
        if (NULL == proc_handle)
        {
            /* dlopen not supported (e.g. in a static binary) */
            test_result = false;
        }
        else
        {
            test_result = true;
            gate_posix_dlclose(proc_handle);
        }
        test_completed = true;
    }
    return test_result;
}

gate_result_t gate_library_open(gate_string_t const* path, gate_library_t* lib, gate_enumint_t flags)
{
    gate_result_t ret;
    void* handle;
    int dlflags = 0;
    char libpath[GATE_MAX_FILEPATH_LENGTH];
    gate_size_t libpathlen = gate_str_print_text(libpath, sizeof(libpath) / sizeof(libpath[0]), path->str, path->length);

    do
    {
        if (libpathlen == 0)
        {
            ret = GATE_RESULT_INVALIDARG;
            break;
        }
        if (GATE_FLAG_ENABLED(flags, GATE_LIBRARY_FLAG_DATA))
        {
            dlflags |= RTLD_LAZY;
        }
        else
        {
            dlflags |= RTLD_NOW;
        }

        if (GATE_FLAG_ENABLED(flags, GATE_LIBRARY_FLAG_LOCAL))
        {
            dlflags |= RTLD_LOCAL;
        }
        else
        {
            dlflags |= RTLD_GLOBAL;
        }

        handle = gate_posix_dlopen(libpath, dlflags);
        if (handle == NULL)
        {
            ret = GATE_RESULT_FAILED;
            break;
        }

        /* success: */
        *lib = (gate_library_t)handle;
        ret = GATE_RESULT_OK;

    } while (0);

    return ret;
}
gate_result_t gate_library_close(gate_library_t lib)
{
    void* handle = (void*)lib;
    int errcode = gate_posix_dlclose(handle);
    return ((errcode == 0) ? GATE_RESULT_OK : GATE_RESULT_FAILED);
}
gate_result_t gate_library_get_function(gate_library_t lib, gate_string_t const* entryname, gate_library_function_t* func)
{
    gate_result_t ret;
    void* dataptr;
    ret = gate_library_get_data(lib, entryname, &dataptr);
    if (GATE_SUCCEEDED(ret) && (func != NULL))
    {
        *func = (gate_library_function_t)dataptr;
    }
    return ret;
}
gate_result_t gate_library_get_data(gate_library_t lib, gate_string_t const* entryname, void** data)
{
    gate_result_t ret;
    void* handle = (void*)lib;
    void* dataptr;
    char name[GATE_MAX_FILENAME_LENGTH];
    gate_size_t entrylen = gate_str_print_text(name, sizeof(name) / sizeof(name[0]), entryname->str, entryname->length);
    do
    {
        if ((handle == NULL) || (entrylen == 0))
        {
            ret = GATE_RESULT_INVALIDARG;
            break;
        }

        dataptr = gate_posix_dlsym(handle, name);
        if (dataptr == NULL)
        {
            ret = GATE_RESULT_FAILED;
            break;
        }

        /*success case*/
        if (data != NULL)
        {
            *data = dataptr;
        }
        ret = GATE_RESULT_OK;

    } while (0);

    return ret;
}

#endif	/* GATE_SYS_POSIX */



#if defined(GATE_CORE_LIBRARIES_NO_IMPL)

gate_bool_t gate_library_support()
{
    return false;
}

gate_result_t gate_library_open(gate_string_t const* path, gate_library_t* lib, gate_enumint_t flags)
{
    (void)path;
    (void)lib;
    (void)flags;
    return GATE_RESULT_NOTSUPPORTED;
}
gate_result_t gate_library_close(gate_library_t lib)
{
    (void)lib;
    return GATE_RESULT_NOTSUPPORTED;
}
gate_result_t gate_library_get_function(gate_library_t lib, gate_string_t const* entryname, gate_library_function_t* ptr_func)
{
    (void)lib;
    (void)entryname;
    (void)ptr_func;
    return GATE_RESULT_NOTSUPPORTED;
}
gate_result_t gate_library_get_data(gate_library_t lib, gate_string_t const* entryname, void** ptr_data)
{
    (void)lib;
    (void)entryname;
    (void)ptr_data;
    return GATE_RESULT_NOTSUPPORTED;
}

#endif /* GATE_CORE_LIBRARIES_NO_IMPL */