libkafs: derivation from non-DES key (rxkad-kdf)
[heimdal.git] / lib / roken / dlfcn_w32.c
blob96cea138241e933529396020e8fee920adc515ff
1 /***********************************************************************
2 * Copyright (c) 2009, Secure Endpoints Inc.
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28 * OF THE POSSIBILITY OF SUCH DAMAGE.
30 **********************************************************************/
32 #include <config.h>
33 #include <windows.h>
34 #include <dlfcn.h>
35 #include <strsafe.h>
37 #define ERR_STR_LEN 256
39 static volatile LONG dlfcn_tls = TLS_OUT_OF_INDEXES;
41 static DWORD get_tl_error_slot(void)
43 if (dlfcn_tls == TLS_OUT_OF_INDEXES) {
44 DWORD slot = TlsAlloc();
45 DWORD old_slot;
47 if (slot == TLS_OUT_OF_INDEXES)
48 return dlfcn_tls;
50 if ((old_slot = InterlockedCompareExchange(&dlfcn_tls, slot,
51 TLS_OUT_OF_INDEXES)) !=
52 TLS_OUT_OF_INDEXES) {
54 /* Lost a race */
55 TlsFree(slot);
56 return old_slot;
57 } else {
58 return slot;
62 return dlfcn_tls;
65 static void set_error(const char * e)
67 char * s;
68 char * old_s;
69 size_t len;
71 DWORD slot = get_tl_error_slot();
73 if (slot == TLS_OUT_OF_INDEXES)
74 return;
76 len = strlen(e) * sizeof(char) + sizeof(char);
77 s = LocalAlloc(LMEM_FIXED, len);
78 if (s == NULL)
79 return;
81 old_s = (char *) TlsGetValue(slot);
82 TlsSetValue(slot, (LPVOID) s);
84 if (old_s != NULL)
85 LocalFree(old_s);
88 static void set_error_from_last(void) {
89 DWORD slot = get_tl_error_slot();
90 char * s = NULL;
91 char * old_s;
93 if (slot == TLS_OUT_OF_INDEXES)
94 return;
96 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER,
97 0, GetLastError(), 0,
98 (LPTSTR) &s, 0,
99 NULL);
100 if (s == NULL)
101 return;
103 old_s = (char *) TlsGetValue(slot);
104 TlsSetValue(slot, (LPVOID) s);
106 if (old_s != NULL)
107 LocalFree(old_s);
110 ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
111 dlclose(void * vhm)
113 BOOL brv;
115 brv = FreeLibrary((HMODULE) vhm);
116 if (!brv) {
117 set_error_from_last();
119 return !brv;
122 ROKEN_LIB_FUNCTION char * ROKEN_LIB_CALL
123 dlerror(void)
125 DWORD slot = get_tl_error_slot();
127 if (slot == TLS_OUT_OF_INDEXES)
128 return NULL;
130 return (char *) TlsGetValue(slot);
133 ROKEN_LIB_FUNCTION void * ROKEN_LIB_CALL
134 dlopen(const char *fn, int flags)
136 HMODULE hm;
137 UINT old_error_mode;
139 /* We don't support dlopen(0, ...) on Windows.*/
140 if ( fn == NULL ) {
141 set_error("Not implemented");
142 return NULL;
145 old_error_mode = SetErrorMode(SEM_FAILCRITICALERRORS);
147 hm = LoadLibraryEx(fn, 0, LOAD_WITH_ALTERED_SEARCH_PATH);
149 if (hm == NULL) {
150 set_error_from_last();
153 SetErrorMode(old_error_mode);
155 return (void *) hm;
158 ROKEN_LIB_FUNCTION DLSYM_RET_TYPE ROKEN_LIB_CALL
159 dlsym(void * vhm, const char * func_name)
161 HMODULE hm = (HMODULE) vhm;
163 return (DLSYM_RET_TYPE)(ULONG_PTR)GetProcAddress(hm, func_name);
166 ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
167 dladdr(void *addr, Dl_info *dli)
169 HMODULE hm;
170 DWORD nsize;
172 memset(dli, 0, sizeof(*dli));
173 dli->dli_fname = dli->_dli_buf;
175 if (!GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
176 GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
177 (LPCTSTR)(ULONG_PTR)addr, &hm))
178 return 0;
180 nsize = GetModuleFileName(hm, dli->_dli_buf, sizeof(dli->_dli_buf));
181 dli->_dli_buf[sizeof(dli->_dli_buf) - 1] = '\0';
182 if (nsize >= sizeof(dli->_dli_buf))
183 return 0; /* truncated? can't be... */
184 return 1;