1 /* dso_win32.c -*- mode:C; c-file-style: "eay" -*- */
3 * Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL project
6 /* ====================================================================
7 * Copyright (c) 2000 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * licensing@OpenSSL.org.
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
35 * 6. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
54 * This product includes cryptographic software written by Eric Young
55 * (eay@cryptsoft.com). This product includes software written by Tim
56 * Hudson (tjh@cryptsoft.com).
63 #include <openssl/dso.h>
65 #if !defined(DSO_WIN32)
66 DSO_METHOD *DSO_METHOD_win32(void)
74 static FARPROC GetProcAddressA(HMODULE hModule, LPCSTR lpProcName)
76 WCHAR lpProcNameW[64];
79 for (i = 0; lpProcName[i] && i < 64; i++)
80 lpProcNameW[i] = (WCHAR)lpProcName[i];
85 return GetProcAddressW(hModule, lpProcNameW);
88 # undef GetProcAddress
89 # define GetProcAddress GetProcAddressA
91 static HINSTANCE LoadLibraryA(LPCSTR lpLibFileName)
94 size_t len_0 = strlen(lpLibFileName) + 1, i;
97 fnamw = (WCHAR *)_alloca(len_0 * sizeof(WCHAR));
99 fnamw = (WCHAR *)alloca(len_0 * sizeof(WCHAR));
102 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
105 # if defined(_WIN32_WCE) && _WIN32_WCE>=101
106 if (!MultiByteToWideChar(CP_ACP, 0, lpLibFileName, len_0, fnamw, len_0))
108 for (i = 0; i < len_0; i++)
109 fnamw[i] = (WCHAR)lpLibFileName[i];
111 return LoadLibraryW(fnamw);
115 /* Part of the hack in "win32_load" ... */
116 # define DSO_MAX_TRANSLATED_SIZE 256
118 static int win32_load(DSO *dso);
119 static int win32_unload(DSO *dso);
120 static void *win32_bind_var(DSO *dso, const char *symname);
121 static DSO_FUNC_TYPE win32_bind_func(DSO *dso, const char *symname);
122 static char *win32_name_converter(DSO *dso, const char *filename);
123 static char *win32_merger(DSO *dso, const char *filespec1,
124 const char *filespec2);
125 static int win32_pathbyaddr(void *addr, char *path, int sz);
126 static void *win32_globallookup(const char *name);
128 static const char *openssl_strnchr(const char *string, int c, size_t len);
130 static DSO_METHOD dso_meth_win32 = {
131 "OpenSSL 'win32' shared library method",
137 win32_name_converter,
145 DSO_METHOD *DSO_METHOD_win32(void)
147 return (&dso_meth_win32);
151 * For this DSO_METHOD, our meth_data STACK will contain; (i) a pointer to
152 * the handle (HINSTANCE) returned from LoadLibrary(), and copied.
155 static int win32_load(DSO *dso)
157 HINSTANCE h = NULL, *p = NULL;
158 /* See applicable comments from dso_dl.c */
159 char *filename = DSO_convert_filename(dso, NULL);
161 if (filename == NULL) {
162 DSOerr(DSO_F_WIN32_LOAD, DSO_R_NO_FILENAME);
165 h = LoadLibraryA(filename);
167 DSOerr(DSO_F_WIN32_LOAD, DSO_R_LOAD_FAILED);
168 ERR_add_error_data(3, "filename(", filename, ")");
171 p = OPENSSL_malloc(sizeof(*p));
173 DSOerr(DSO_F_WIN32_LOAD, ERR_R_MALLOC_FAILURE);
177 if (!sk_void_push(dso->meth_data, p)) {
178 DSOerr(DSO_F_WIN32_LOAD, DSO_R_STACK_ERROR);
182 dso->loaded_filename = filename;
186 OPENSSL_free(filename);
193 static int win32_unload(DSO *dso)
197 DSOerr(DSO_F_WIN32_UNLOAD, ERR_R_PASSED_NULL_PARAMETER);
200 if (sk_void_num(dso->meth_data) < 1)
202 p = sk_void_pop(dso->meth_data);
204 DSOerr(DSO_F_WIN32_UNLOAD, DSO_R_NULL_HANDLE);
207 if (!FreeLibrary(*p)) {
208 DSOerr(DSO_F_WIN32_UNLOAD, DSO_R_UNLOAD_FAILED);
210 * We should push the value back onto the stack in case of a retry.
212 sk_void_push(dso->meth_data, p);
221 * Using GetProcAddress for variables? TODO: Check this out in the Win32 API
222 * docs, there's probably a variant for variables.
224 static void *win32_bind_var(DSO *dso, const char *symname)
229 if ((dso == NULL) || (symname == NULL)) {
230 DSOerr(DSO_F_WIN32_BIND_VAR, ERR_R_PASSED_NULL_PARAMETER);
233 if (sk_void_num(dso->meth_data) < 1) {
234 DSOerr(DSO_F_WIN32_BIND_VAR, DSO_R_STACK_ERROR);
237 ptr = sk_void_value(dso->meth_data, sk_void_num(dso->meth_data) - 1);
239 DSOerr(DSO_F_WIN32_BIND_VAR, DSO_R_NULL_HANDLE);
242 sym = GetProcAddress(*ptr, symname);
244 DSOerr(DSO_F_WIN32_BIND_VAR, DSO_R_SYM_FAILURE);
245 ERR_add_error_data(3, "symname(", symname, ")");
251 static DSO_FUNC_TYPE win32_bind_func(DSO *dso, const char *symname)
256 if ((dso == NULL) || (symname == NULL)) {
257 DSOerr(DSO_F_WIN32_BIND_FUNC, ERR_R_PASSED_NULL_PARAMETER);
260 if (sk_void_num(dso->meth_data) < 1) {
261 DSOerr(DSO_F_WIN32_BIND_FUNC, DSO_R_STACK_ERROR);
264 ptr = sk_void_value(dso->meth_data, sk_void_num(dso->meth_data) - 1);
266 DSOerr(DSO_F_WIN32_BIND_FUNC, DSO_R_NULL_HANDLE);
269 sym = GetProcAddress(*ptr, symname);
271 DSOerr(DSO_F_WIN32_BIND_FUNC, DSO_R_SYM_FAILURE);
272 ERR_add_error_data(3, "symname(", symname, ")");
275 return ((DSO_FUNC_TYPE)sym);
291 static struct file_st *win32_splitter(DSO *dso, const char *filename,
292 int assume_last_is_dir)
294 struct file_st *result = NULL;
295 enum { IN_NODE, IN_DEVICE, IN_FILE } position;
296 const char *start = filename;
300 DSOerr(DSO_F_WIN32_SPLITTER, DSO_R_NO_FILENAME);
307 result = OPENSSL_malloc(sizeof(*result));
308 if (result == NULL) {
309 DSOerr(DSO_F_WIN32_SPLITTER, ERR_R_MALLOC_FAILURE);
313 memset(result, 0, sizeof(struct file_st));
314 position = IN_DEVICE;
316 if ((filename[0] == '\\' && filename[1] == '\\')
317 || (filename[0] == '/' && filename[1] == '/')) {
321 result->node = start;
328 if (position != IN_DEVICE) {
329 DSOerr(DSO_F_WIN32_SPLITTER, DSO_R_INCORRECT_FILE_SYNTAX);
333 OPENSSL_free(result);
336 result->device = start;
337 result->devicelen = (int)(filename - start);
344 if (position == IN_NODE) {
345 result->nodelen = (int)(filename - start);
349 } else if (position == IN_DEVICE) {
353 result->dirlen = (int)(filename - start);
357 result->dirlen += (int)(filename - start);
362 if (position == IN_NODE) {
363 result->nodelen = (int)(filename - start);
365 if (filename - start > 0) {
366 if (assume_last_is_dir) {
367 if (position == IN_DEVICE) {
371 result->dirlen += (int)(filename - start);
373 result->file = start;
374 result->filelen = (int)(filename - start);
386 if (!result->nodelen)
388 if (!result->devicelen)
389 result->device = NULL;
392 if (!result->filelen)
398 static char *win32_joiner(DSO *dso, const struct file_st *file_split)
400 int len = 0, offset = 0;
405 DSOerr(DSO_F_WIN32_JOINER, ERR_R_PASSED_NULL_PARAMETER);
408 if (file_split->node) {
409 len += 2 + file_split->nodelen; /* 2 for starting \\ */
410 if (file_split->predir || file_split->dir || file_split->file)
411 len++; /* 1 for ending \ */
412 } else if (file_split->device) {
413 len += file_split->devicelen + 1; /* 1 for ending : */
415 len += file_split->predirlen;
416 if (file_split->predir && (file_split->dir || file_split->file)) {
417 len++; /* 1 for ending \ */
419 len += file_split->dirlen;
420 if (file_split->dir && file_split->file) {
421 len++; /* 1 for ending \ */
423 len += file_split->filelen;
426 DSOerr(DSO_F_WIN32_JOINER, DSO_R_EMPTY_FILE_STRUCTURE);
430 result = OPENSSL_malloc(len + 1);
432 DSOerr(DSO_F_WIN32_JOINER, ERR_R_MALLOC_FAILURE);
436 if (file_split->node) {
437 strcpy(&result[offset], "\\\\");
439 strncpy(&result[offset], file_split->node, file_split->nodelen);
440 offset += file_split->nodelen;
441 if (file_split->predir || file_split->dir || file_split->file) {
442 result[offset] = '\\';
445 } else if (file_split->device) {
446 strncpy(&result[offset], file_split->device, file_split->devicelen);
447 offset += file_split->devicelen;
448 result[offset] = ':';
451 start = file_split->predir;
452 while (file_split->predirlen > (start - file_split->predir)) {
453 const char *end = openssl_strnchr(start, '/',
454 file_split->predirlen - (start -
455 file_split->predir));
458 + file_split->predirlen - (start - file_split->predir);
459 strncpy(&result[offset], start, end - start);
460 offset += (int)(end - start);
461 result[offset] = '\\';
465 start = file_split->dir;
466 while (file_split->dirlen > (start - file_split->dir)) {
467 const char *end = openssl_strnchr(start, '/',
468 file_split->dirlen - (start -
471 end = start + file_split->dirlen - (start - file_split->dir);
472 strncpy(&result[offset], start, end - start);
473 offset += (int)(end - start);
474 result[offset] = '\\';
478 strncpy(&result[offset], file_split->file, file_split->filelen);
479 offset += file_split->filelen;
480 result[offset] = '\0';
484 static char *win32_merger(DSO *dso, const char *filespec1,
485 const char *filespec2)
488 struct file_st *filespec1_split = NULL;
489 struct file_st *filespec2_split = NULL;
491 if (!filespec1 && !filespec2) {
492 DSOerr(DSO_F_WIN32_MERGER, ERR_R_PASSED_NULL_PARAMETER);
496 merged = OPENSSL_malloc(strlen(filespec1) + 1);
498 DSOerr(DSO_F_WIN32_MERGER, ERR_R_MALLOC_FAILURE);
501 strcpy(merged, filespec1);
502 } else if (!filespec1) {
503 merged = OPENSSL_malloc(strlen(filespec2) + 1);
505 DSOerr(DSO_F_WIN32_MERGER, ERR_R_MALLOC_FAILURE);
508 strcpy(merged, filespec2);
510 filespec1_split = win32_splitter(dso, filespec1, 0);
511 if (!filespec1_split) {
512 DSOerr(DSO_F_WIN32_MERGER, ERR_R_MALLOC_FAILURE);
515 filespec2_split = win32_splitter(dso, filespec2, 1);
516 if (!filespec2_split) {
517 DSOerr(DSO_F_WIN32_MERGER, ERR_R_MALLOC_FAILURE);
518 OPENSSL_free(filespec1_split);
522 /* Fill in into filespec1_split */
523 if (!filespec1_split->node && !filespec1_split->device) {
524 filespec1_split->node = filespec2_split->node;
525 filespec1_split->nodelen = filespec2_split->nodelen;
526 filespec1_split->device = filespec2_split->device;
527 filespec1_split->devicelen = filespec2_split->devicelen;
529 if (!filespec1_split->dir) {
530 filespec1_split->dir = filespec2_split->dir;
531 filespec1_split->dirlen = filespec2_split->dirlen;
532 } else if (filespec1_split->dir[0] != '\\'
533 && filespec1_split->dir[0] != '/') {
534 filespec1_split->predir = filespec2_split->dir;
535 filespec1_split->predirlen = filespec2_split->dirlen;
537 if (!filespec1_split->file) {
538 filespec1_split->file = filespec2_split->file;
539 filespec1_split->filelen = filespec2_split->filelen;
542 merged = win32_joiner(dso, filespec1_split);
544 OPENSSL_free(filespec1_split);
545 OPENSSL_free(filespec2_split);
549 static char *win32_name_converter(DSO *dso, const char *filename)
554 len = strlen(filename);
555 transform = ((strstr(filename, "/") == NULL) &&
556 (strstr(filename, "\\") == NULL) &&
557 (strstr(filename, ":") == NULL));
559 /* We will convert this to "%s.dll" */
560 translated = OPENSSL_malloc(len + 5);
562 /* We will simply duplicate filename */
563 translated = OPENSSL_malloc(len + 1);
564 if (translated == NULL) {
565 DSOerr(DSO_F_WIN32_NAME_CONVERTER, DSO_R_NAME_TRANSLATION_FAILED);
569 sprintf(translated, "%s.dll", filename);
571 sprintf(translated, "%s", filename);
575 static const char *openssl_strnchr(const char *string, int c, size_t len)
579 for (i = 0, p = string; i < len && *p; i++, p++) {
586 # include <tlhelp32.h>
588 # define DLLNAME "TOOLHELP.DLL"
590 # ifdef MODULEENTRY32
591 # undef MODULEENTRY32 /* unmask the ASCII version! */
593 # define DLLNAME "KERNEL32.DLL"
596 typedef HANDLE(WINAPI *CREATETOOLHELP32SNAPSHOT) (DWORD, DWORD);
597 typedef BOOL(WINAPI *CLOSETOOLHELP32SNAPSHOT) (HANDLE);
598 typedef BOOL(WINAPI *MODULE32) (HANDLE, MODULEENTRY32 *);
600 static int win32_pathbyaddr(void *addr, char *path, int sz)
603 HANDLE hModuleSnap = INVALID_HANDLE_VALUE;
605 CREATETOOLHELP32SNAPSHOT create_snap;
606 CLOSETOOLHELP32SNAPSHOT close_snap;
607 MODULE32 module_first, module_next;
611 int (*f) (void *, char *, int);
619 dll = LoadLibrary(TEXT(DLLNAME));
621 DSOerr(DSO_F_WIN32_PATHBYADDR, DSO_R_UNSUPPORTED);
625 create_snap = (CREATETOOLHELP32SNAPSHOT)
626 GetProcAddress(dll, "CreateToolhelp32Snapshot");
627 if (create_snap == NULL) {
629 DSOerr(DSO_F_WIN32_PATHBYADDR, DSO_R_UNSUPPORTED);
632 /* We take the rest for granted... */
634 close_snap = (CLOSETOOLHELP32SNAPSHOT)
635 GetProcAddress(dll, "CloseToolhelp32Snapshot");
637 close_snap = (CLOSETOOLHELP32SNAPSHOT) CloseHandle;
639 module_first = (MODULE32) GetProcAddress(dll, "Module32First");
640 module_next = (MODULE32) GetProcAddress(dll, "Module32Next");
642 hModuleSnap = (*create_snap) (TH32CS_SNAPMODULE, 0);
643 if (hModuleSnap == INVALID_HANDLE_VALUE) {
645 DSOerr(DSO_F_WIN32_PATHBYADDR, DSO_R_UNSUPPORTED);
649 me32.dwSize = sizeof(me32);
651 if (!(*module_first) (hModuleSnap, &me32)) {
652 (*close_snap) (hModuleSnap);
654 DSOerr(DSO_F_WIN32_PATHBYADDR, DSO_R_FAILURE);
659 if ((BYTE *) addr >= me32.modBaseAddr &&
660 (BYTE *) addr < me32.modBaseAddr + me32.modBaseSize) {
661 (*close_snap) (hModuleSnap);
664 # if _WIN32_WCE >= 101
665 return WideCharToMultiByte(CP_ACP, 0, me32.szExePath, -1,
666 path, sz, NULL, NULL);
669 int i, len = (int)wcslen(me32.szExePath);
674 for (i = 0; i < len; i++)
675 path[i] = (char)me32.szExePath[i];
682 int len = (int)strlen(me32.szExePath);
687 memcpy(path, me32.szExePath, len);
693 } while ((*module_next) (hModuleSnap, &me32));
695 (*close_snap) (hModuleSnap);
700 static void *win32_globallookup(const char *name)
703 HANDLE hModuleSnap = INVALID_HANDLE_VALUE;
705 CREATETOOLHELP32SNAPSHOT create_snap;
706 CLOSETOOLHELP32SNAPSHOT close_snap;
707 MODULE32 module_first, module_next;
710 dll = LoadLibrary(TEXT(DLLNAME));
712 DSOerr(DSO_F_WIN32_GLOBALLOOKUP, DSO_R_UNSUPPORTED);
716 create_snap = (CREATETOOLHELP32SNAPSHOT)
717 GetProcAddress(dll, "CreateToolhelp32Snapshot");
718 if (create_snap == NULL) {
720 DSOerr(DSO_F_WIN32_GLOBALLOOKUP, DSO_R_UNSUPPORTED);
723 /* We take the rest for granted... */
725 close_snap = (CLOSETOOLHELP32SNAPSHOT)
726 GetProcAddress(dll, "CloseToolhelp32Snapshot");
728 close_snap = (CLOSETOOLHELP32SNAPSHOT) CloseHandle;
730 module_first = (MODULE32) GetProcAddress(dll, "Module32First");
731 module_next = (MODULE32) GetProcAddress(dll, "Module32Next");
733 hModuleSnap = (*create_snap) (TH32CS_SNAPMODULE, 0);
734 if (hModuleSnap == INVALID_HANDLE_VALUE) {
736 DSOerr(DSO_F_WIN32_GLOBALLOOKUP, DSO_R_UNSUPPORTED);
740 me32.dwSize = sizeof(me32);
742 if (!(*module_first) (hModuleSnap, &me32)) {
743 (*close_snap) (hModuleSnap);
749 if ((ret = GetProcAddress(me32.hModule, name))) {
750 (*close_snap) (hModuleSnap);
754 } while ((*module_next) (hModuleSnap, &me32));
756 (*close_snap) (hModuleSnap);
760 #endif /* DSO_WIN32 */