1bf215546Sopenharmony_ci/**************************************************************************
2bf215546Sopenharmony_ci *
3bf215546Sopenharmony_ci * Copyright 2009 VMware, Inc.
4bf215546Sopenharmony_ci * Copyright 1999-2008  Brian Paul
5bf215546Sopenharmony_ci * All Rights Reserved.
6bf215546Sopenharmony_ci *
7bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
8bf215546Sopenharmony_ci * copy of this software and associated documentation files (the
9bf215546Sopenharmony_ci * "Software"), to deal in the Software without restriction, including
10bf215546Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish,
11bf215546Sopenharmony_ci * distribute, sub license, and/or sell copies of the Software, and to
12bf215546Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to
13bf215546Sopenharmony_ci * the following conditions:
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
19bf215546Sopenharmony_ci * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20bf215546Sopenharmony_ci * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21bf215546Sopenharmony_ci * USE OR OTHER DEALINGS IN THE SOFTWARE.
22bf215546Sopenharmony_ci *
23bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the
24bf215546Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions
25bf215546Sopenharmony_ci * of the Software.
26bf215546Sopenharmony_ci *
27bf215546Sopenharmony_ci **************************************************************************/
28bf215546Sopenharmony_ci
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_ci#include "detect_os.h"
31bf215546Sopenharmony_ci
32bf215546Sopenharmony_ci#if DETECT_OS_UNIX
33bf215546Sopenharmony_ci#include <dlfcn.h>
34bf215546Sopenharmony_ci#endif
35bf215546Sopenharmony_ci#if DETECT_OS_WINDOWS
36bf215546Sopenharmony_ci#include <windows.h>
37bf215546Sopenharmony_ci#endif
38bf215546Sopenharmony_ci
39bf215546Sopenharmony_ci#include "u_dl.h"
40bf215546Sopenharmony_ci#include "u_pointer.h"
41bf215546Sopenharmony_ci
42bf215546Sopenharmony_ci
43bf215546Sopenharmony_cistruct util_dl_library *
44bf215546Sopenharmony_ciutil_dl_open(const char *filename)
45bf215546Sopenharmony_ci{
46bf215546Sopenharmony_ci#if DETECT_OS_UNIX
47bf215546Sopenharmony_ci   return (struct util_dl_library *)dlopen(filename, RTLD_LAZY | RTLD_LOCAL);
48bf215546Sopenharmony_ci#elif DETECT_OS_WINDOWS
49bf215546Sopenharmony_ci   return (struct util_dl_library *)LoadLibraryA(filename);
50bf215546Sopenharmony_ci#else
51bf215546Sopenharmony_ci   return NULL;
52bf215546Sopenharmony_ci#endif
53bf215546Sopenharmony_ci}
54bf215546Sopenharmony_ci
55bf215546Sopenharmony_ci
56bf215546Sopenharmony_ciutil_dl_proc
57bf215546Sopenharmony_ciutil_dl_get_proc_address(struct util_dl_library *library,
58bf215546Sopenharmony_ci                         const char *procname)
59bf215546Sopenharmony_ci{
60bf215546Sopenharmony_ci#if DETECT_OS_UNIX
61bf215546Sopenharmony_ci   return (util_dl_proc) pointer_to_func(dlsym((void *)library, procname));
62bf215546Sopenharmony_ci#elif DETECT_OS_WINDOWS
63bf215546Sopenharmony_ci   return (util_dl_proc)GetProcAddress((HMODULE)library, procname);
64bf215546Sopenharmony_ci#else
65bf215546Sopenharmony_ci   return (util_dl_proc)NULL;
66bf215546Sopenharmony_ci#endif
67bf215546Sopenharmony_ci}
68bf215546Sopenharmony_ci
69bf215546Sopenharmony_ci
70bf215546Sopenharmony_civoid
71bf215546Sopenharmony_ciutil_dl_close(struct util_dl_library *library)
72bf215546Sopenharmony_ci{
73bf215546Sopenharmony_ci#if DETECT_OS_UNIX
74bf215546Sopenharmony_ci   dlclose((void *)library);
75bf215546Sopenharmony_ci#elif DETECT_OS_WINDOWS
76bf215546Sopenharmony_ci   FreeLibrary((HMODULE)library);
77bf215546Sopenharmony_ci#else
78bf215546Sopenharmony_ci   (void)library;
79bf215546Sopenharmony_ci#endif
80bf215546Sopenharmony_ci}
81bf215546Sopenharmony_ci
82bf215546Sopenharmony_ci
83bf215546Sopenharmony_ciconst char *
84bf215546Sopenharmony_ciutil_dl_error(void)
85bf215546Sopenharmony_ci{
86bf215546Sopenharmony_ci#if DETECT_OS_UNIX
87bf215546Sopenharmony_ci   return dlerror();
88bf215546Sopenharmony_ci#elif DETECT_OS_WINDOWS
89bf215546Sopenharmony_ci   return "unknown error";
90bf215546Sopenharmony_ci#else
91bf215546Sopenharmony_ci   return "unknown error";
92bf215546Sopenharmony_ci#endif
93bf215546Sopenharmony_ci}
94