[kdbus] Add SipHash algorithm
[platform/upstream/glib.git] / gmodule / gmodule-win32.c
1 /* GMODULE - GLIB wrapper code for dynamic module loading
2  * Copyright (C) 1998, 2000 Tim Janik
3  *
4  * Win32 GMODULE implementation
5  * Copyright (C) 1998 Tor Lillqvist
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19  */
20
21 /*
22  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
23  * file for a list of people on the GLib Team.  See the ChangeLog
24  * files for a list of changes.  These files are distributed with
25  * GLib at ftp://ftp.gtk.org/pub/gtk/. 
26  */
27
28 /* 
29  * MT safe
30  */
31 #include "config.h"
32
33 #include <stdio.h>
34 #include <windows.h>
35
36 #include <tlhelp32.h>
37
38 #ifdef G_WITH_CYGWIN
39 #include <sys/cygwin.h>
40 #endif
41
42 static void
43 set_error (const gchar *format,
44            ...)
45 {
46   gchar *error;
47   gchar *detail;
48   gchar *message;
49   va_list args;
50
51   error = g_win32_error_message (GetLastError ());
52
53   va_start (args, format);
54   detail = g_strdup_vprintf (format, args);
55   va_end (args);
56
57   message = g_strconcat (detail, error, NULL);
58
59   g_module_set_error (message);
60   g_free (message);
61   g_free (detail);
62   g_free (error);
63 }
64
65 /* --- functions --- */
66 static gpointer
67 _g_module_open (const gchar *file_name,
68                 gboolean     bind_lazy,
69                 gboolean     bind_local)
70 {
71   HINSTANCE handle;
72   wchar_t *wfilename;
73 #ifdef G_WITH_CYGWIN
74   gchar tmp[MAX_PATH];
75
76   cygwin_conv_to_win32_path(file_name, tmp);
77   file_name = tmp;
78 #endif
79   wfilename = g_utf8_to_utf16 (file_name, -1, NULL, NULL, NULL);
80
81   handle = LoadLibraryW (wfilename);
82   g_free (wfilename);
83       
84   if (!handle)
85     set_error ("'%s': ", file_name);
86
87   return handle;
88 }
89
90 static gint dummy;
91 static gpointer null_module_handle = &dummy;
92   
93 static gpointer
94 _g_module_self (void)
95 {
96   return null_module_handle;
97 }
98
99 static void
100 _g_module_close (gpointer handle,
101                  gboolean is_unref)
102 {
103   if (handle != null_module_handle)
104     if (!FreeLibrary (handle))
105       set_error ("");
106 }
107
108 static gpointer
109 find_in_any_module_using_toolhelp (const gchar *symbol_name)
110 {
111   HANDLE snapshot; 
112   MODULEENTRY32 me32;
113
114   gpointer p;
115
116   if ((snapshot = CreateToolhelp32Snapshot (TH32CS_SNAPMODULE, 0)) == (HANDLE) -1)
117     return NULL;
118
119   me32.dwSize = sizeof (me32);
120   p = NULL;
121   if (Module32First (snapshot, &me32))
122     {
123       do {
124         if ((p = GetProcAddress (me32.hModule, symbol_name)) != NULL)
125           break;
126       } while (Module32Next (snapshot, &me32));
127     }
128
129   CloseHandle (snapshot);
130
131   return p;
132 }
133
134 static gpointer
135 find_in_any_module (const gchar *symbol_name)
136 {
137   gpointer result;
138
139   if ((result = find_in_any_module_using_toolhelp (symbol_name)) == NULL)
140     return NULL;
141   else
142     return result;
143 }
144
145 static gpointer
146 _g_module_symbol (gpointer     handle,
147                   const gchar *symbol_name)
148 {
149   gpointer p;
150   
151   if (handle == null_module_handle)
152     {
153       if ((p = GetProcAddress (GetModuleHandle (NULL), symbol_name)) == NULL)
154         p = find_in_any_module (symbol_name);
155     }
156   else
157     p = GetProcAddress (handle, symbol_name);
158
159   if (!p)
160     set_error ("");
161
162   return p;
163 }
164
165 static gchar*
166 _g_module_build_path (const gchar *directory,
167                       const gchar *module_name)
168 {
169   gint k;
170
171   k = strlen (module_name);
172     
173   if (directory && *directory)
174     if (k > 4 && g_ascii_strcasecmp (module_name + k - 4, ".dll") == 0)
175       return g_strconcat (directory, G_DIR_SEPARATOR_S, module_name, NULL);
176 #ifdef G_WITH_CYGWIN
177     else if (strncmp (module_name, "lib", 3) == 0 || strncmp (module_name, "cyg", 3) == 0)
178       return g_strconcat (directory, G_DIR_SEPARATOR_S, module_name, ".dll", NULL);
179     else
180       return g_strconcat (directory, G_DIR_SEPARATOR_S, "cyg", module_name, ".dll", NULL);
181 #else
182     else if (strncmp (module_name, "lib", 3) == 0)
183       return g_strconcat (directory, G_DIR_SEPARATOR_S, module_name, ".dll", NULL);
184     else
185       return g_strconcat (directory, G_DIR_SEPARATOR_S, "lib", module_name, ".dll", NULL);
186 #endif
187   else if (k > 4 && g_ascii_strcasecmp (module_name + k - 4, ".dll") == 0)
188     return g_strdup (module_name);
189 #ifdef G_WITH_CYGWIN
190   else if (strncmp (module_name, "lib", 3) == 0 || strncmp (module_name, "cyg", 3) == 0)
191     return g_strconcat (module_name, ".dll", NULL);
192   else
193     return g_strconcat ("cyg", module_name, ".dll", NULL);
194 #else
195   else if (strncmp (module_name, "lib", 3) == 0)
196     return g_strconcat (module_name, ".dll", NULL);
197   else
198     return g_strconcat ("lib", module_name, ".dll", NULL);
199 #endif
200 }