summaryrefslogtreecommitdiff
path: root/legacy/evil/src/lib/dlfcn
diff options
context:
space:
mode:
Diffstat (limited to 'legacy/evil/src/lib/dlfcn')
-rw-r--r--legacy/evil/src/lib/dlfcn/dlfcn.c272
-rw-r--r--legacy/evil/src/lib/dlfcn/dlfcn.h258
2 files changed, 0 insertions, 530 deletions
diff --git a/legacy/evil/src/lib/dlfcn/dlfcn.c b/legacy/evil/src/lib/dlfcn/dlfcn.c
deleted file mode 100644
index 126b68c3ed..0000000000
--- a/legacy/evil/src/lib/dlfcn/dlfcn.c
+++ /dev/null
@@ -1,272 +0,0 @@
1#ifdef HAVE_CONFIG_H
2# include "config.h"
3#endif /* HAVE_CONFIG_H */
4
5#include <stdlib.h>
6
7#if defined(__MINGW32CE__) || defined(_MSC_VER)
8# include <limits.h>
9#endif /* __MINGW32CE__ || _MSC_VER */
10
11#ifndef WIN32_LEAN_AND_MEAN
12# define WIN32_LEAN_AND_MEAN
13#endif
14#include <windows.h>
15#undef WIN32_LEAN_AND_MEAN
16
17#ifdef _WIN32_WCE
18# include <tlhelp32.h> /* CreateToolhelp32Snapshot */
19#else
20# include <psapi.h> /* EnumProcessModules(Ex) */
21#endif
22
23#include "../Evil.h"
24
25#include "dlfcn.h"
26
27
28static char *dl_err = NULL;
29static int dl_err_viewed = 0;
30
31static void
32get_last_error(char *desc)
33{
34 char *str;
35 size_t l1;
36 size_t l2;
37
38 str = evil_last_error_get();
39
40 l1 = strlen(desc);
41 l2 = strlen(str);
42
43 if (dl_err)
44 free(dl_err);
45
46 dl_err = (char *)malloc(sizeof(char) * (l1 + l2 + 1));
47 if (!dl_err)
48 dl_err = strdup("not enough resource");
49 else
50 {
51 memcpy(dl_err, desc, l1);
52 memcpy(dl_err + l1, str, l2);
53 dl_err[l1 + l2] = '\0';
54 }
55 free(str);
56 dl_err_viewed = 0;
57}
58
59void *
60dlopen(const char* path, int mode __UNUSED__)
61{
62 HMODULE module = NULL;
63
64 if (!path)
65 {
66 module = GetModuleHandle(NULL);
67 if (!module)
68 get_last_error("GetModuleHandle returned: ");
69 }
70 else
71 {
72 char *new_path;
73 size_t l;
74 unsigned int i;
75
76 /* according to MSDN, we must change the slash to backslash */
77 l = strlen(path);
78 new_path = (char *)malloc(sizeof(char) * (l + 1));
79 if (!new_path)
80 {
81 if (dl_err)
82 free(dl_err);
83 dl_err = strdup("not enough resource");
84 dl_err_viewed = 0;
85 return NULL;
86 }
87 for (i = 0; i <= l; i++)
88 {
89 if (path[i] == '/')
90 new_path[i] = '\\';
91 else
92 new_path[i] = path[i];
93 }
94#ifdef UNICODE
95 {
96 wchar_t *wpath;
97
98 wpath = evil_char_to_wchar(new_path);
99 module = LoadLibrary(wpath);
100 free(wpath);
101 }
102#else
103 module = LoadLibraryEx(new_path, NULL,
104 LOAD_WITH_ALTERED_SEARCH_PATH);
105#endif /* ! UNICODE */
106 if (!module)
107 get_last_error("LoadLibraryEx returned: ");
108
109 free(new_path);
110 }
111
112 return module;
113}
114
115int
116dlclose(void* handle)
117{
118 if (FreeLibrary(handle))
119 return 0;
120 else
121 {
122 get_last_error("FreeLibrary returned: ");
123 return -1;
124 }
125}
126
127void *
128dlsym(void *handle, const char *symbol)
129{
130 FARPROC fp = NULL;
131 LPCTSTR new_symbol;
132
133 if (!symbol || !*symbol) return NULL;
134
135#ifdef UNICODE
136 new_symbol = evil_char_to_wchar(symbol);
137#else
138 new_symbol = symbol;
139#endif /* UNICODE */
140
141 if (handle == RTLD_DEFAULT)
142 {
143#ifdef _WIN32_WCE
144 HANDLE snapshot;
145 MODULEENTRY32 module;
146
147 snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS |
148 TH32CS_SNAPMODULE |
149 TH32CS_GETALLMODS,
150 0);
151 if (!snapshot)
152 return NULL;
153
154 module.dwSize = sizeof(module);
155 if (Module32First(snapshot, &module))
156 do {
157 fp = GetProcAddress(module.hModule, new_symbol);
158 if (fp) break;
159 } while (Module32Next(snapshot, &module));
160
161 CloseToolhelp32Snapshot(snapshot);
162#else
163 HMODULE modules[1024];
164 DWORD needed;
165 DWORD i;
166
167 /* TODO: use EnumProcessModulesEx() on Windows >= Vista */
168 if (!EnumProcessModules(GetCurrentProcess(),
169 modules, sizeof(modules), &needed))
170 return NULL;
171
172 for (i = 0; i < (needed / sizeof(HMODULE)); i++)
173 {
174 fp = GetProcAddress(modules[i], new_symbol);
175 if (fp) break;
176 }
177#endif
178 }
179 else
180 fp = GetProcAddress(handle, new_symbol);
181
182#ifdef UNICODE
183 free((void *)new_symbol);
184#endif /* UNICODE */
185
186 if (!fp)
187 get_last_error("GetProcAddress returned: ");
188
189 return fp;
190}
191
192int
193dladdr (const void *addr __UNUSED__, Dl_info *info)
194{
195 TCHAR tpath[PATH_MAX];
196 MEMORY_BASIC_INFORMATION mbi;
197 char *path;
198 char *tmp;
199 size_t length;
200 int ret = 0;
201
202 if (!info)
203 return 0;
204
205#ifdef _WIN32_WINNT
206 length = VirtualQuery(addr, &mbi, sizeof(mbi));
207 if (!length)
208 return 0;
209
210 if (mbi.State != MEM_COMMIT)
211 return 0;
212
213 if (!mbi.AllocationBase)
214 return 0;
215
216 ret = GetModuleFileName((HMODULE)mbi.AllocationBase, (LPTSTR)&tpath, PATH_MAX);
217 if (!ret)
218 return 0;
219#else
220 ret = GetModuleFileName(NULL, (LPTSTR)&tpath, PATH_MAX);
221 if (!ret)
222 return 0;
223#endif
224
225#ifdef UNICODE
226 path = evil_wchar_to_char(tpath);
227#else
228 path = tpath;
229#endif /* ! UNICODE */
230
231 length = strlen (path);
232 if (length >= PATH_MAX)
233 {
234 length = PATH_MAX - 1;
235 path[PATH_MAX - 1] = '\0';
236 }
237
238 /* replace '/' by '\' */
239 tmp = path;
240 while (*tmp)
241 {
242 if (*tmp == '/') *tmp = '\\';
243 tmp++;
244 }
245
246 memcpy (info->dli_fname, path, length + 1);
247 info->dli_fbase = NULL;
248 info->dli_sname = NULL;
249 info->dli_saddr = NULL;
250
251#ifdef UNICODE
252 free (path);
253#endif /* ! UNICODE */
254
255 return 1;
256}
257
258char *
259dlerror (void)
260{
261 if (!dl_err_viewed)
262 {
263 dl_err_viewed = 1;
264 return dl_err;
265 }
266 else
267 {
268 if (dl_err)
269 free(dl_err);
270 return NULL;
271 }
272}
diff --git a/legacy/evil/src/lib/dlfcn/dlfcn.h b/legacy/evil/src/lib/dlfcn/dlfcn.h
deleted file mode 100644
index 610331b702..0000000000
--- a/legacy/evil/src/lib/dlfcn/dlfcn.h
+++ /dev/null
@@ -1,258 +0,0 @@
1#ifndef __EVIL_DLFCN_H__
2#define __EVIL_DLFCN_H__
3
4
5#include <limits.h>
6
7
8#ifdef EAPI
9# undef EAPI
10#endif /* EAPI */
11
12#ifdef _WIN32
13# ifdef EFL_EVIL_DLFCN_BUILD
14# ifdef DLL_EXPORT
15# define EAPI __declspec(dllexport)
16# else
17# define EAPI
18# endif /* ! DLL_EXPORT */
19# else
20# define EAPI __declspec(dllimport)
21# endif /* ! EFL_EVIL_DLFCN_BUILD */
22#endif /* _WIN32 */
23
24
25#ifdef __cplusplus
26extern "C" {
27#endif
28
29#ifdef _WIN32_WCE
30# ifndef PATH_MAX
31# define PATH_MAX 260
32# endif
33#endif
34
35
36/**
37 * @file dlfcn.h
38 * @brief The file that provides functions to manage dynamic-link libraries
39 * @defgroup Dlfcn Functions that manage dynamic-link libraries.
40 *
41 * This header provides functions to load and unload dynamic-link
42 * libaries, to get the address of a symbol, and to get diagnostic
43 * information.
44 */
45
46
47/**
48 * @def RTLD_LAZY
49 * Lazy function call binding.
50 */
51# define RTLD_LAZY 0x00001 /* lazy function call binding */
52
53/**
54 * @def RTLD_NOW
55 * Immediate function call binding.
56 */
57# define RTLD_NOW 0x00002 /* immediate function call binding */
58
59/**
60 * @def RTLD_GLOBAL
61 * Symbols in this dlopen'ed obj are visible to other dlopen'ed objs.
62 */
63# define RTLD_GLOBAL 0x00100 /* symbols in this dlopen'ed obj are visible
64 to other dlopen'ed objs */
65
66/**
67 * @def RTLD_NODELETE
68 * Symbols are not deleted when closed.
69 */
70#define RTLD_NODELETE 0x01000 /* do not delete object when closed. */
71
72/**
73 * @def RTLD_DEFAULT
74 * Symbols are searched in all the DLL opened by the current process.
75 */
76#define RTLD_DEFAULT ((void*)1) /* search the symbol on all the DLL of the current process */
77
78/**
79 * @typedef Dl_info
80 * @brief A structure that stores infomation of a calling process.
81 */
82typedef struct Dl_info Dl_info;
83
84/**
85 * @struct Dl_info
86 * @brief A structure that stores infomation of a calling process.
87 */
88struct Dl_info
89{
90 char dli_fname[PATH_MAX]; /**< Filename of defining object */
91 void *dli_fbase; /**< Load address of that object */
92 const char *dli_sname; /**< Name of nearest lower symbol */
93 void *dli_saddr; /**< Exact value of nearest symbol */
94};
95
96/**
97 * @brief Map a specified executable module (either a .dll or .exe file)
98 * into the address space of the user process.
99 *
100 * @param path Name of the module.
101 * @param mode Unused.
102 * @return A pointer that represent the module, or @c NULL on failure.
103 *
104 * Map a specified executable module (either a .dll or .exe file)
105 * into the address space of the user process. If @p path is @c NULL,
106 * then the module corresponding to the current process is returned.
107 * Otherwise the module specified by @p path is loaded if it exists.
108 * If not, @c NULL is returned. The directory separators can be forward
109 * slash, or backward ones. Mapping a module can map other modules.
110 * @p mode is unused.
111 *
112 * If an error occurred, an error string can be retrived with dlerror().
113 *
114 * According to the OS, the search order of the module can change,
115 * according to the value of SafeDllSearchMode.
116 *
117 * - For Windows Vista, Windows Server 2003, and Windows XP SP2:
118 * SafeDLLSearchMode is enabled by default.
119 * - For Windows XP and Windows 2000 SP4: SafeDLLSearchMode is disabled
120 * by default.
121 *
122 * If SafeDllSearchMode is enabled
123 * - The directory from which the application loaded.
124 * - The system directory. Use the GetSystemDirectory() function
125 * to get the path of this directory.
126 * - The 16-bit system directory. There is no function that obtains
127 * the path of this directory, but it is searched.
128 * - The Windows directory. Use the GetWindowsDirectory() function
129 * to get the path of this directory.
130 * - The current directory.
131 * - The directories that are listed in the PATH environment variable.
132 * Note that this does not include the per-application path specified
133 * by the App Paths registry key.
134 *
135 * If SafeDllSearchMode is disabled
136 * - The directory from which the application loaded.
137 * - The current directory.
138 * - The system directory. Use the GetSystemDirectory() function
139 * to get the path of this directory.
140 * - The 16-bit system directory. There is no function that obtains
141 * the path of this directory, but it is searched.
142 * - The Windows directory. Use the GetWindowsDirectory() function
143 * to get the path of this directory.
144 * - The directories that are listed in the PATH environment variable.
145 * Note that this does not include the per-application path specified
146 * by the App Paths registry key.
147 *
148 * Conformity: None.
149 *
150 * Supported OS: Windows Vista, Windows XP or Windows 2000
151 * Professional.
152 *
153 * @ingroup Dlfcn
154 */
155EAPI void *dlopen(const char* path, int mode);
156
157/**
158 * @brief Close a dynamic-link library.
159 *
160 * @param handle Handle that references a dynamic-link library.
161 * @return O on sucess, -1 otherwise.
162 *
163 * Release a reference to the dynamic-link library referenced
164 * by @p handle. If the reference count drops to 0, the handle is
165 * removed from the address space and is rendered invalid. @p handle
166 * is the value returned by a previous call to dlopen().
167 *
168 * If no error occurred, the returned value is 0, otherwise the
169 * returned value is -1 and an error string can be retrived with
170 * dlerror().
171 *
172 * Conformity: None.
173 *
174 * Supported OS: Windows Vista, Windows XP or Windows 2000
175 * Professional.
176 *
177 * @ingroup Dlfcn
178 */
179EAPI int dlclose(void* handle);
180
181/**
182 * @brief Get the address of a symbol.
183 *
184 * @param handle Handle that references a dynamic-link library.
185 * @param symbol @c NULL-terminated string.
186 * @return O on sucess, NULL otherwise.
187 *
188 * Return the address of the code or data location specified by the
189 * string @p symbol. @p handle references a library that contains
190 * the function or variable @p symbol.
191 *
192 * If no error occurred, the returned value is the code or data
193 * location, otherwise the returned value is NULL and an error
194 * string can be retrived with dlerror().
195 *
196 * Conformity: None.
197 *
198 * Supported OS: Windows Vista, Windows XP or Windows 2000
199 * Professional.
200 *
201 * @ingroup Dlfcn
202 */
203EAPI void *dlsym(void* handle, const char* symbol);
204
205/**
206 * @brief Get the location of the current process (.exe)
207 *
208 * @param addr Unused.
209 * @param info Pointer to the Dl_info to fill.
210 * @return 1 on success, 0 otherwise.
211 *
212 * Fill the dli_fname member of @p info with the absolute name
213 * of the current calling process (.exe file that is executed).
214 * All other members are set to @c NULL.
215 *
216 * Contrary to the unix function, the full name of the shared
217 * library is not returned, but insted the full name of the current
218 * calling process (.exe file).
219 *
220 * Conformity: None.
221 *
222 * Supported OS: Windows Vista, Windows XP or Windows 2000
223 * Professional.
224 *
225 * @ingroup Dlfcn
226 */
227EAPI int dladdr (const void *addr, Dl_info *info);
228
229/**
230 * @brief Get diagnostic information
231 *
232 * @return A @c NULL-terminated string if an error occured, @c NULL
233 * otherwise.
234 *
235 * Return a @c NULL-terminated character string describing the last
236 * error that occurred on this thread during a call to dlopen(),
237 * dlsym(), or dlclose(). If no such error has occurred, dlerror()
238 * returns a null pointer. At each call to dlerror(), the error
239 * indication is reset. Thus in the case of two calls to dlerror(),
240 * where the second call follows the first immediately, the second
241 * call will always return a null pointer.
242 *
243 * Conformity: None.
244 *
245 * Supported OS: Windows Vista, Windows XP or Windows 2000
246 * Professional.
247 *
248 * @ingroup Dlfcn
249 */
250EAPI char *dlerror (void);
251
252
253#ifdef __cplusplus
254}
255#endif
256
257
258#endif /* __EVIL_DLFCN_H__ */