2 * Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL project
5 /* ====================================================================
6 * Copyright (c) 2000 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
61 #if !defined(DSO_WIN32)
62 DSO_METHOD *DSO_METHOD_win32(void)
70 static FARPROC GetProcAddressA(HMODULE hModule, LPCSTR lpProcName)
72 WCHAR lpProcNameW[64];
75 for (i = 0; lpProcName[i] && i < 64; i++)
76 lpProcNameW[i] = (WCHAR)lpProcName[i];
81 return GetProcAddressW(hModule, lpProcNameW);
84 # undef GetProcAddress
85 # define GetProcAddress GetProcAddressA
87 static HINSTANCE LoadLibraryA(LPCSTR lpLibFileName)
90 size_t len_0 = strlen(lpLibFileName) + 1, i;
93 fnamw = (WCHAR *)_alloca(len_0 * sizeof(WCHAR));
95 fnamw = (WCHAR *)alloca(len_0 * sizeof(WCHAR));
98 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
101 # if defined(_WIN32_WCE) && _WIN32_WCE>=101
102 if (!MultiByteToWideChar(CP_ACP, 0, lpLibFileName, len_0, fnamw, len_0))
104 for (i = 0; i < len_0; i++)
105 fnamw[i] = (WCHAR)lpLibFileName[i];
107 return LoadLibraryW(fnamw);
111 /* Part of the hack in "win32_load" ... */
112 # define DSO_MAX_TRANSLATED_SIZE 256
114 static int win32_load(DSO *dso);
115 static int win32_unload(DSO *dso);
116 static void *win32_bind_var(DSO *dso, const char *symname);
117 static DSO_FUNC_TYPE win32_bind_func(DSO *dso, const char *symname);
118 static char *win32_name_converter(DSO *dso, const char *filename);
119 static char *win32_merger(DSO *dso, const char *filespec1,
120 const char *filespec2);
121 static int win32_pathbyaddr(void *addr, char *path, int sz);
122 static void *win32_globallookup(const char *name);
124 static const char *openssl_strnchr(const char *string, int c, size_t len);
126 static DSO_METHOD dso_meth_win32 = {
127 "OpenSSL 'win32' shared library method",
133 win32_name_converter,
141 DSO_METHOD *DSO_METHOD_win32(void)
143 return (&dso_meth_win32);
147 * For this DSO_METHOD, our meth_data STACK will contain; (i) a pointer to
148 * the handle (HINSTANCE) returned from LoadLibrary(), and copied.
151 static int win32_load(DSO *dso)
153 HINSTANCE h = NULL, *p = NULL;
154 /* See applicable comments from dso_dl.c */
155 char *filename = DSO_convert_filename(dso, NULL);
157 if (filename == NULL) {
158 DSOerr(DSO_F_WIN32_LOAD, DSO_R_NO_FILENAME);
161 h = LoadLibraryA(filename);
163 DSOerr(DSO_F_WIN32_LOAD, DSO_R_LOAD_FAILED);
164 ERR_add_error_data(3, "filename(", filename, ")");
167 p = OPENSSL_malloc(sizeof(*p));
169 DSOerr(DSO_F_WIN32_LOAD, ERR_R_MALLOC_FAILURE);
173 if (!sk_void_push(dso->meth_data, p)) {
174 DSOerr(DSO_F_WIN32_LOAD, DSO_R_STACK_ERROR);
178 dso->loaded_filename = filename;
182 OPENSSL_free(filename);
189 static int win32_unload(DSO *dso)
193 DSOerr(DSO_F_WIN32_UNLOAD, ERR_R_PASSED_NULL_PARAMETER);
196 if (sk_void_num(dso->meth_data) < 1)
198 p = sk_void_pop(dso->meth_data);
200 DSOerr(DSO_F_WIN32_UNLOAD, DSO_R_NULL_HANDLE);
203 if (!FreeLibrary(*p)) {
204 DSOerr(DSO_F_WIN32_UNLOAD, DSO_R_UNLOAD_FAILED);
206 * We should push the value back onto the stack in case of a retry.
208 sk_void_push(dso->meth_data, p);
217 * Using GetProcAddress for variables? TODO: Check this out in the Win32 API
218 * docs, there's probably a variant for variables.
220 static void *win32_bind_var(DSO *dso, const char *symname)
228 if ((dso == NULL) || (symname == NULL)) {
229 DSOerr(DSO_F_WIN32_BIND_VAR, ERR_R_PASSED_NULL_PARAMETER);
232 if (sk_void_num(dso->meth_data) < 1) {
233 DSOerr(DSO_F_WIN32_BIND_VAR, DSO_R_STACK_ERROR);
236 ptr = sk_void_value(dso->meth_data, sk_void_num(dso->meth_data) - 1);
238 DSOerr(DSO_F_WIN32_BIND_VAR, DSO_R_NULL_HANDLE);
241 sym.f = GetProcAddress(*ptr, symname);
243 DSOerr(DSO_F_WIN32_BIND_VAR, DSO_R_SYM_FAILURE);
244 ERR_add_error_data(3, "symname(", symname, ")");
250 static DSO_FUNC_TYPE win32_bind_func(DSO *dso, const char *symname)
258 if ((dso == NULL) || (symname == NULL)) {
259 DSOerr(DSO_F_WIN32_BIND_FUNC, ERR_R_PASSED_NULL_PARAMETER);
262 if (sk_void_num(dso->meth_data) < 1) {
263 DSOerr(DSO_F_WIN32_BIND_FUNC, DSO_R_STACK_ERROR);
266 ptr = sk_void_value(dso->meth_data, sk_void_num(dso->meth_data) - 1);
268 DSOerr(DSO_F_WIN32_BIND_FUNC, DSO_R_NULL_HANDLE);
271 sym.f = GetProcAddress(*ptr, symname);
273 DSOerr(DSO_F_WIN32_BIND_FUNC, DSO_R_SYM_FAILURE);
274 ERR_add_error_data(3, "symname(", symname, ")");
277 return ((DSO_FUNC_TYPE)sym.f);
293 static struct file_st *win32_splitter(DSO *dso, const char *filename,
294 int assume_last_is_dir)
296 struct file_st *result = NULL;
297 enum { IN_NODE, IN_DEVICE, IN_FILE } position;
298 const char *start = filename;
302 DSOerr(DSO_F_WIN32_SPLITTER, DSO_R_NO_FILENAME);
309 result = OPENSSL_zalloc(sizeof(*result));
310 if (result == NULL) {
311 DSOerr(DSO_F_WIN32_SPLITTER, ERR_R_MALLOC_FAILURE);
315 position = IN_DEVICE;
317 if ((filename[0] == '\\' && filename[1] == '\\')
318 || (filename[0] == '/' && filename[1] == '/')) {
322 result->node = start;
329 if (position != IN_DEVICE) {
330 DSOerr(DSO_F_WIN32_SPLITTER, DSO_R_INCORRECT_FILE_SYNTAX);
334 OPENSSL_free(result);
337 result->device = start;
338 result->devicelen = (int)(filename - start);
345 if (position == IN_NODE) {
346 result->nodelen = (int)(filename - start);
350 } else if (position == IN_DEVICE) {
354 result->dirlen = (int)(filename - start);
358 result->dirlen += (int)(filename - start);
363 if (position == IN_NODE) {
364 result->nodelen = (int)(filename - start);
366 if (filename - start > 0) {
367 if (assume_last_is_dir) {
368 if (position == IN_DEVICE) {
372 result->dirlen += (int)(filename - start);
374 result->file = start;
375 result->filelen = (int)(filename - start);
387 if (!result->nodelen)
389 if (!result->devicelen)
390 result->device = NULL;
393 if (!result->filelen)
399 static char *win32_joiner(DSO *dso, const struct file_st *file_split)
401 int len = 0, offset = 0;
406 DSOerr(DSO_F_WIN32_JOINER, ERR_R_PASSED_NULL_PARAMETER);
409 if (file_split->node) {
410 len += 2 + file_split->nodelen; /* 2 for starting \\ */
411 if (file_split->predir || file_split->dir || file_split->file)
412 len++; /* 1 for ending \ */
413 } else if (file_split->device) {
414 len += file_split->devicelen + 1; /* 1 for ending : */
416 len += file_split->predirlen;
417 if (file_split->predir && (file_split->dir || file_split->file)) {
418 len++; /* 1 for ending \ */
420 len += file_split->dirlen;
421 if (file_split->dir && file_split->file) {
422 len++; /* 1 for ending \ */
424 len += file_split->filelen;
427 DSOerr(DSO_F_WIN32_JOINER, DSO_R_EMPTY_FILE_STRUCTURE);
431 result = OPENSSL_malloc(len + 1);
432 if (result == NULL) {
433 DSOerr(DSO_F_WIN32_JOINER, ERR_R_MALLOC_FAILURE);
437 if (file_split->node) {
438 strcpy(&result[offset], "\\\\");
440 strncpy(&result[offset], file_split->node, file_split->nodelen);
441 offset += file_split->nodelen;
442 if (file_split->predir || file_split->dir || file_split->file) {
443 result[offset] = '\\';
446 } else if (file_split->device) {
447 strncpy(&result[offset], file_split->device, file_split->devicelen);
448 offset += file_split->devicelen;
449 result[offset] = ':';
452 start = file_split->predir;
453 while (file_split->predirlen > (start - file_split->predir)) {
454 const char *end = openssl_strnchr(start, '/',
455 file_split->predirlen - (start -
456 file_split->predir));
459 + file_split->predirlen - (start - file_split->predir);
460 strncpy(&result[offset], start, end - start);
461 offset += (int)(end - start);
462 result[offset] = '\\';
466 start = file_split->dir;
467 while (file_split->dirlen > (start - file_split->dir)) {
468 const char *end = openssl_strnchr(start, '/',
469 file_split->dirlen - (start -
472 end = start + file_split->dirlen - (start - file_split->dir);
473 strncpy(&result[offset], start, end - start);
474 offset += (int)(end - start);
475 result[offset] = '\\';
479 strncpy(&result[offset], file_split->file, file_split->filelen);
480 offset += file_split->filelen;
481 result[offset] = '\0';
485 static char *win32_merger(DSO *dso, const char *filespec1,
486 const char *filespec2)
489 struct file_st *filespec1_split = NULL;
490 struct file_st *filespec2_split = NULL;
492 if (!filespec1 && !filespec2) {
493 DSOerr(DSO_F_WIN32_MERGER, ERR_R_PASSED_NULL_PARAMETER);
497 merged = OPENSSL_malloc(strlen(filespec1) + 1);
498 if (merged == NULL) {
499 DSOerr(DSO_F_WIN32_MERGER, ERR_R_MALLOC_FAILURE);
502 strcpy(merged, filespec1);
503 } else if (!filespec1) {
504 merged = OPENSSL_malloc(strlen(filespec2) + 1);
505 if (merged == NULL) {
506 DSOerr(DSO_F_WIN32_MERGER, ERR_R_MALLOC_FAILURE);
509 strcpy(merged, filespec2);
511 filespec1_split = win32_splitter(dso, filespec1, 0);
512 if (!filespec1_split) {
513 DSOerr(DSO_F_WIN32_MERGER, ERR_R_MALLOC_FAILURE);
516 filespec2_split = win32_splitter(dso, filespec2, 1);
517 if (!filespec2_split) {
518 DSOerr(DSO_F_WIN32_MERGER, ERR_R_MALLOC_FAILURE);
519 OPENSSL_free(filespec1_split);
523 /* Fill in into filespec1_split */
524 if (!filespec1_split->node && !filespec1_split->device) {
525 filespec1_split->node = filespec2_split->node;
526 filespec1_split->nodelen = filespec2_split->nodelen;
527 filespec1_split->device = filespec2_split->device;
528 filespec1_split->devicelen = filespec2_split->devicelen;
530 if (!filespec1_split->dir) {
531 filespec1_split->dir = filespec2_split->dir;
532 filespec1_split->dirlen = filespec2_split->dirlen;
533 } else if (filespec1_split->dir[0] != '\\'
534 && filespec1_split->dir[0] != '/') {
535 filespec1_split->predir = filespec2_split->dir;
536 filespec1_split->predirlen = filespec2_split->dirlen;
538 if (!filespec1_split->file) {
539 filespec1_split->file = filespec2_split->file;
540 filespec1_split->filelen = filespec2_split->filelen;
543 merged = win32_joiner(dso, filespec1_split);
545 OPENSSL_free(filespec1_split);
546 OPENSSL_free(filespec2_split);
550 static char *win32_name_converter(DSO *dso, const char *filename)
555 len = strlen(filename);
556 transform = ((strstr(filename, "/") == NULL) &&
557 (strstr(filename, "\\") == NULL) &&
558 (strstr(filename, ":") == NULL));
560 /* We will convert this to "%s.dll" */
561 translated = OPENSSL_malloc(len + 5);
563 /* We will simply duplicate filename */
564 translated = OPENSSL_malloc(len + 1);
565 if (translated == NULL) {
566 DSOerr(DSO_F_WIN32_NAME_CONVERTER, DSO_R_NAME_TRANSLATION_FAILED);
570 sprintf(translated, "%s.dll", filename);
572 sprintf(translated, "%s", filename);
576 static const char *openssl_strnchr(const char *string, int c, size_t len)
580 for (i = 0, p = string; i < len && *p; i++, p++) {
587 # include <tlhelp32.h>
589 # define DLLNAME "TOOLHELP.DLL"
591 # ifdef MODULEENTRY32
592 # undef MODULEENTRY32 /* unmask the ASCII version! */
594 # define DLLNAME "KERNEL32.DLL"
597 typedef HANDLE(WINAPI *CREATETOOLHELP32SNAPSHOT) (DWORD, DWORD);
598 typedef BOOL(WINAPI *CLOSETOOLHELP32SNAPSHOT) (HANDLE);
599 typedef BOOL(WINAPI *MODULE32) (HANDLE, MODULEENTRY32 *);
601 static int win32_pathbyaddr(void *addr, char *path, int sz)
604 HANDLE hModuleSnap = INVALID_HANDLE_VALUE;
606 CREATETOOLHELP32SNAPSHOT create_snap;
607 CLOSETOOLHELP32SNAPSHOT close_snap;
608 MODULE32 module_first, module_next;
612 int (*f) (void *, char *, int);
620 dll = LoadLibrary(TEXT(DLLNAME));
622 DSOerr(DSO_F_WIN32_PATHBYADDR, DSO_R_UNSUPPORTED);
626 create_snap = (CREATETOOLHELP32SNAPSHOT)
627 GetProcAddress(dll, "CreateToolhelp32Snapshot");
628 if (create_snap == NULL) {
630 DSOerr(DSO_F_WIN32_PATHBYADDR, DSO_R_UNSUPPORTED);
633 /* We take the rest for granted... */
635 close_snap = (CLOSETOOLHELP32SNAPSHOT)
636 GetProcAddress(dll, "CloseToolhelp32Snapshot");
638 close_snap = (CLOSETOOLHELP32SNAPSHOT) CloseHandle;
640 module_first = (MODULE32) GetProcAddress(dll, "Module32First");
641 module_next = (MODULE32) GetProcAddress(dll, "Module32Next");
643 hModuleSnap = (*create_snap) (TH32CS_SNAPMODULE, 0);
644 if (hModuleSnap == INVALID_HANDLE_VALUE) {
646 DSOerr(DSO_F_WIN32_PATHBYADDR, DSO_R_UNSUPPORTED);
650 me32.dwSize = sizeof(me32);
652 if (!(*module_first) (hModuleSnap, &me32)) {
653 (*close_snap) (hModuleSnap);
655 DSOerr(DSO_F_WIN32_PATHBYADDR, DSO_R_FAILURE);
660 if ((BYTE *) addr >= me32.modBaseAddr &&
661 (BYTE *) addr < me32.modBaseAddr + me32.modBaseSize) {
662 (*close_snap) (hModuleSnap);
665 # if _WIN32_WCE >= 101
666 return WideCharToMultiByte(CP_ACP, 0, me32.szExePath, -1,
667 path, sz, NULL, NULL);
670 int i, len = (int)wcslen(me32.szExePath);
675 for (i = 0; i < len; i++)
676 path[i] = (char)me32.szExePath[i];
683 int len = (int)strlen(me32.szExePath);
688 memcpy(path, me32.szExePath, len);
694 } while ((*module_next) (hModuleSnap, &me32));
696 (*close_snap) (hModuleSnap);
701 static void *win32_globallookup(const char *name)
704 HANDLE hModuleSnap = INVALID_HANDLE_VALUE;
706 CREATETOOLHELP32SNAPSHOT create_snap;
707 CLOSETOOLHELP32SNAPSHOT close_snap;
708 MODULE32 module_first, module_next;
714 dll = LoadLibrary(TEXT(DLLNAME));
716 DSOerr(DSO_F_WIN32_GLOBALLOOKUP, DSO_R_UNSUPPORTED);
720 create_snap = (CREATETOOLHELP32SNAPSHOT)
721 GetProcAddress(dll, "CreateToolhelp32Snapshot");
722 if (create_snap == NULL) {
724 DSOerr(DSO_F_WIN32_GLOBALLOOKUP, DSO_R_UNSUPPORTED);
727 /* We take the rest for granted... */
729 close_snap = (CLOSETOOLHELP32SNAPSHOT)
730 GetProcAddress(dll, "CloseToolhelp32Snapshot");
732 close_snap = (CLOSETOOLHELP32SNAPSHOT) CloseHandle;
734 module_first = (MODULE32) GetProcAddress(dll, "Module32First");
735 module_next = (MODULE32) GetProcAddress(dll, "Module32Next");
737 hModuleSnap = (*create_snap) (TH32CS_SNAPMODULE, 0);
738 if (hModuleSnap == INVALID_HANDLE_VALUE) {
740 DSOerr(DSO_F_WIN32_GLOBALLOOKUP, DSO_R_UNSUPPORTED);
744 me32.dwSize = sizeof(me32);
746 if (!(*module_first) (hModuleSnap, &me32)) {
747 (*close_snap) (hModuleSnap);
753 if ((ret.f = GetProcAddress(me32.hModule, name))) {
754 (*close_snap) (hModuleSnap);
758 } while ((*module_next) (hModuleSnap, &me32));
760 (*close_snap) (hModuleSnap);
764 #endif /* DSO_WIN32 */