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)
65 static FARPROC
GetProcAddressA(HMODULE hModule
, LPCSTR lpProcName
)
67 WCHAR lpProcNameW
[64];
70 for (i
= 0; lpProcName
[i
] && i
< 64; i
++)
71 lpProcNameW
[i
] = (WCHAR
)lpProcName
[i
];
76 return GetProcAddressW(hModule
, lpProcNameW
);
79 # undef GetProcAddress
80 # define GetProcAddress GetProcAddressA
82 static HINSTANCE
LoadLibraryA(LPCSTR lpLibFileName
)
85 size_t len_0
= strlen(lpLibFileName
) + 1, i
;
88 fnamw
= (WCHAR
*)_alloca(len_0
* sizeof(WCHAR
));
90 fnamw
= (WCHAR
*)alloca(len_0
* sizeof(WCHAR
));
93 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
96 # if defined(_WIN32_WCE) && _WIN32_WCE>=101
97 if (!MultiByteToWideChar(CP_ACP
, 0, lpLibFileName
, len_0
, fnamw
, len_0
))
99 for (i
= 0; i
< len_0
; i
++)
100 fnamw
[i
] = (WCHAR
)lpLibFileName
[i
];
102 return LoadLibraryW(fnamw
);
106 /* Part of the hack in "win32_load" ... */
107 # define DSO_MAX_TRANSLATED_SIZE 256
109 static int win32_load(DSO
*dso
);
110 static int win32_unload(DSO
*dso
);
111 static DSO_FUNC_TYPE
win32_bind_func(DSO
*dso
, const char *symname
);
112 static char *win32_name_converter(DSO
*dso
, const char *filename
);
113 static char *win32_merger(DSO
*dso
, const char *filespec1
,
114 const char *filespec2
);
115 static void *win32_globallookup(const char *name
);
117 static const char *openssl_strnchr(const char *string
, int c
, size_t len
);
119 static DSO_METHOD dso_meth_win32
= {
120 "OpenSSL 'win32' shared library method",
125 win32_name_converter
,
132 DSO_METHOD
*DSO_METHOD_openssl(void)
134 return &dso_meth_win32
;
138 * For this DSO_METHOD, our meth_data STACK will contain; (i) a pointer to
139 * the handle (HINSTANCE) returned from LoadLibrary(), and copied.
142 static int win32_load(DSO
*dso
)
144 HINSTANCE h
= NULL
, *p
= NULL
;
145 /* See applicable comments from dso_dl.c */
146 char *filename
= DSO_convert_filename(dso
, NULL
);
148 if (filename
== NULL
) {
149 DSOerr(DSO_F_WIN32_LOAD
, DSO_R_NO_FILENAME
);
152 h
= LoadLibraryA(filename
);
154 DSOerr(DSO_F_WIN32_LOAD
, DSO_R_LOAD_FAILED
);
155 ERR_add_error_data(3, "filename(", filename
, ")");
158 p
= OPENSSL_malloc(sizeof(*p
));
160 DSOerr(DSO_F_WIN32_LOAD
, ERR_R_MALLOC_FAILURE
);
164 if (!sk_void_push(dso
->meth_data
, p
)) {
165 DSOerr(DSO_F_WIN32_LOAD
, DSO_R_STACK_ERROR
);
169 dso
->loaded_filename
= filename
;
173 OPENSSL_free(filename
);
180 static int win32_unload(DSO
*dso
)
184 DSOerr(DSO_F_WIN32_UNLOAD
, ERR_R_PASSED_NULL_PARAMETER
);
187 if (sk_void_num(dso
->meth_data
) < 1)
189 p
= sk_void_pop(dso
->meth_data
);
191 DSOerr(DSO_F_WIN32_UNLOAD
, DSO_R_NULL_HANDLE
);
194 if (!FreeLibrary(*p
)) {
195 DSOerr(DSO_F_WIN32_UNLOAD
, DSO_R_UNLOAD_FAILED
);
197 * We should push the value back onto the stack in case of a retry.
199 sk_void_push(dso
->meth_data
, p
);
207 static DSO_FUNC_TYPE
win32_bind_func(DSO
*dso
, const char *symname
)
215 if ((dso
== NULL
) || (symname
== NULL
)) {
216 DSOerr(DSO_F_WIN32_BIND_FUNC
, ERR_R_PASSED_NULL_PARAMETER
);
219 if (sk_void_num(dso
->meth_data
) < 1) {
220 DSOerr(DSO_F_WIN32_BIND_FUNC
, DSO_R_STACK_ERROR
);
223 ptr
= sk_void_value(dso
->meth_data
, sk_void_num(dso
->meth_data
) - 1);
225 DSOerr(DSO_F_WIN32_BIND_FUNC
, DSO_R_NULL_HANDLE
);
228 sym
.f
= GetProcAddress(*ptr
, symname
);
230 DSOerr(DSO_F_WIN32_BIND_FUNC
, DSO_R_SYM_FAILURE
);
231 ERR_add_error_data(3, "symname(", symname
, ")");
234 return ((DSO_FUNC_TYPE
)sym
.f
);
250 static struct file_st
*win32_splitter(DSO
*dso
, const char *filename
,
251 int assume_last_is_dir
)
253 struct file_st
*result
= NULL
;
254 enum { IN_NODE
, IN_DEVICE
, IN_FILE
} position
;
255 const char *start
= filename
;
259 DSOerr(DSO_F_WIN32_SPLITTER
, DSO_R_NO_FILENAME
);
266 result
= OPENSSL_zalloc(sizeof(*result
));
267 if (result
== NULL
) {
268 DSOerr(DSO_F_WIN32_SPLITTER
, ERR_R_MALLOC_FAILURE
);
272 position
= IN_DEVICE
;
274 if ((filename
[0] == '\\' && filename
[1] == '\\')
275 || (filename
[0] == '/' && filename
[1] == '/')) {
279 result
->node
= start
;
286 if (position
!= IN_DEVICE
) {
287 DSOerr(DSO_F_WIN32_SPLITTER
, DSO_R_INCORRECT_FILE_SYNTAX
);
291 OPENSSL_free(result
);
294 result
->device
= start
;
295 result
->devicelen
= (int)(filename
- start
);
302 if (position
== IN_NODE
) {
303 result
->nodelen
= (int)(filename
- start
);
307 } else if (position
== IN_DEVICE
) {
311 result
->dirlen
= (int)(filename
- start
);
315 result
->dirlen
+= (int)(filename
- start
);
320 if (position
== IN_NODE
) {
321 result
->nodelen
= (int)(filename
- start
);
323 if (filename
- start
> 0) {
324 if (assume_last_is_dir
) {
325 if (position
== IN_DEVICE
) {
329 result
->dirlen
+= (int)(filename
- start
);
331 result
->file
= start
;
332 result
->filelen
= (int)(filename
- start
);
344 if (!result
->nodelen
)
346 if (!result
->devicelen
)
347 result
->device
= NULL
;
350 if (!result
->filelen
)
356 static char *win32_joiner(DSO
*dso
, const struct file_st
*file_split
)
358 int len
= 0, offset
= 0;
363 DSOerr(DSO_F_WIN32_JOINER
, ERR_R_PASSED_NULL_PARAMETER
);
366 if (file_split
->node
) {
367 len
+= 2 + file_split
->nodelen
; /* 2 for starting \\ */
368 if (file_split
->predir
|| file_split
->dir
|| file_split
->file
)
369 len
++; /* 1 for ending \ */
370 } else if (file_split
->device
) {
371 len
+= file_split
->devicelen
+ 1; /* 1 for ending : */
373 len
+= file_split
->predirlen
;
374 if (file_split
->predir
&& (file_split
->dir
|| file_split
->file
)) {
375 len
++; /* 1 for ending \ */
377 len
+= file_split
->dirlen
;
378 if (file_split
->dir
&& file_split
->file
) {
379 len
++; /* 1 for ending \ */
381 len
+= file_split
->filelen
;
384 DSOerr(DSO_F_WIN32_JOINER
, DSO_R_EMPTY_FILE_STRUCTURE
);
388 result
= OPENSSL_malloc(len
+ 1);
389 if (result
== NULL
) {
390 DSOerr(DSO_F_WIN32_JOINER
, ERR_R_MALLOC_FAILURE
);
394 if (file_split
->node
) {
395 strcpy(&result
[offset
], "\\\\");
397 strncpy(&result
[offset
], file_split
->node
, file_split
->nodelen
);
398 offset
+= file_split
->nodelen
;
399 if (file_split
->predir
|| file_split
->dir
|| file_split
->file
) {
400 result
[offset
] = '\\';
403 } else if (file_split
->device
) {
404 strncpy(&result
[offset
], file_split
->device
, file_split
->devicelen
);
405 offset
+= file_split
->devicelen
;
406 result
[offset
] = ':';
409 start
= file_split
->predir
;
410 while (file_split
->predirlen
> (start
- file_split
->predir
)) {
411 const char *end
= openssl_strnchr(start
, '/',
412 file_split
->predirlen
- (start
-
413 file_split
->predir
));
416 + file_split
->predirlen
- (start
- file_split
->predir
);
417 strncpy(&result
[offset
], start
, end
- start
);
418 offset
+= (int)(end
- start
);
419 result
[offset
] = '\\';
423 start
= file_split
->dir
;
424 while (file_split
->dirlen
> (start
- file_split
->dir
)) {
425 const char *end
= openssl_strnchr(start
, '/',
426 file_split
->dirlen
- (start
-
429 end
= start
+ file_split
->dirlen
- (start
- file_split
->dir
);
430 strncpy(&result
[offset
], start
, end
- start
);
431 offset
+= (int)(end
- start
);
432 result
[offset
] = '\\';
436 strncpy(&result
[offset
], file_split
->file
, file_split
->filelen
);
437 offset
+= file_split
->filelen
;
438 result
[offset
] = '\0';
442 static char *win32_merger(DSO
*dso
, const char *filespec1
,
443 const char *filespec2
)
446 struct file_st
*filespec1_split
= NULL
;
447 struct file_st
*filespec2_split
= NULL
;
449 if (!filespec1
&& !filespec2
) {
450 DSOerr(DSO_F_WIN32_MERGER
, ERR_R_PASSED_NULL_PARAMETER
);
454 merged
= OPENSSL_malloc(strlen(filespec1
) + 1);
455 if (merged
== NULL
) {
456 DSOerr(DSO_F_WIN32_MERGER
, ERR_R_MALLOC_FAILURE
);
459 strcpy(merged
, filespec1
);
460 } else if (!filespec1
) {
461 merged
= OPENSSL_malloc(strlen(filespec2
) + 1);
462 if (merged
== NULL
) {
463 DSOerr(DSO_F_WIN32_MERGER
, ERR_R_MALLOC_FAILURE
);
466 strcpy(merged
, filespec2
);
468 filespec1_split
= win32_splitter(dso
, filespec1
, 0);
469 if (!filespec1_split
) {
470 DSOerr(DSO_F_WIN32_MERGER
, ERR_R_MALLOC_FAILURE
);
473 filespec2_split
= win32_splitter(dso
, filespec2
, 1);
474 if (!filespec2_split
) {
475 DSOerr(DSO_F_WIN32_MERGER
, ERR_R_MALLOC_FAILURE
);
476 OPENSSL_free(filespec1_split
);
480 /* Fill in into filespec1_split */
481 if (!filespec1_split
->node
&& !filespec1_split
->device
) {
482 filespec1_split
->node
= filespec2_split
->node
;
483 filespec1_split
->nodelen
= filespec2_split
->nodelen
;
484 filespec1_split
->device
= filespec2_split
->device
;
485 filespec1_split
->devicelen
= filespec2_split
->devicelen
;
487 if (!filespec1_split
->dir
) {
488 filespec1_split
->dir
= filespec2_split
->dir
;
489 filespec1_split
->dirlen
= filespec2_split
->dirlen
;
490 } else if (filespec1_split
->dir
[0] != '\\'
491 && filespec1_split
->dir
[0] != '/') {
492 filespec1_split
->predir
= filespec2_split
->dir
;
493 filespec1_split
->predirlen
= filespec2_split
->dirlen
;
495 if (!filespec1_split
->file
) {
496 filespec1_split
->file
= filespec2_split
->file
;
497 filespec1_split
->filelen
= filespec2_split
->filelen
;
500 merged
= win32_joiner(dso
, filespec1_split
);
502 OPENSSL_free(filespec1_split
);
503 OPENSSL_free(filespec2_split
);
507 static char *win32_name_converter(DSO
*dso
, const char *filename
)
512 len
= strlen(filename
);
513 transform
= ((strstr(filename
, "/") == NULL
) &&
514 (strstr(filename
, "\\") == NULL
) &&
515 (strstr(filename
, ":") == NULL
));
517 /* We will convert this to "%s.dll" */
518 translated
= OPENSSL_malloc(len
+ 5);
520 /* We will simply duplicate filename */
521 translated
= OPENSSL_malloc(len
+ 1);
522 if (translated
== NULL
) {
523 DSOerr(DSO_F_WIN32_NAME_CONVERTER
, DSO_R_NAME_TRANSLATION_FAILED
);
527 sprintf(translated
, "%s.dll", filename
);
529 sprintf(translated
, "%s", filename
);
533 static const char *openssl_strnchr(const char *string
, int c
, size_t len
)
537 for (i
= 0, p
= string
; i
< len
&& *p
; i
++, p
++) {
544 # include <tlhelp32.h>
546 # define DLLNAME "TOOLHELP.DLL"
548 # ifdef MODULEENTRY32
549 # undef MODULEENTRY32 /* unmask the ASCII version! */
551 # define DLLNAME "KERNEL32.DLL"
554 typedef HANDLE(WINAPI
*CREATETOOLHELP32SNAPSHOT
) (DWORD
, DWORD
);
555 typedef BOOL(WINAPI
*CLOSETOOLHELP32SNAPSHOT
) (HANDLE
);
556 typedef BOOL(WINAPI
*MODULE32
) (HANDLE
, MODULEENTRY32
*);
558 static void *win32_globallookup(const char *name
)
561 HANDLE hModuleSnap
= INVALID_HANDLE_VALUE
;
563 CREATETOOLHELP32SNAPSHOT create_snap
;
564 CLOSETOOLHELP32SNAPSHOT close_snap
;
565 MODULE32 module_first
, module_next
;
571 dll
= LoadLibrary(TEXT(DLLNAME
));
573 DSOerr(DSO_F_WIN32_GLOBALLOOKUP
, DSO_R_UNSUPPORTED
);
577 create_snap
= (CREATETOOLHELP32SNAPSHOT
)
578 GetProcAddress(dll
, "CreateToolhelp32Snapshot");
579 if (create_snap
== NULL
) {
581 DSOerr(DSO_F_WIN32_GLOBALLOOKUP
, DSO_R_UNSUPPORTED
);
584 /* We take the rest for granted... */
586 close_snap
= (CLOSETOOLHELP32SNAPSHOT
)
587 GetProcAddress(dll
, "CloseToolhelp32Snapshot");
589 close_snap
= (CLOSETOOLHELP32SNAPSHOT
) CloseHandle
;
591 module_first
= (MODULE32
) GetProcAddress(dll
, "Module32First");
592 module_next
= (MODULE32
) GetProcAddress(dll
, "Module32Next");
594 hModuleSnap
= (*create_snap
) (TH32CS_SNAPMODULE
, 0);
595 if (hModuleSnap
== INVALID_HANDLE_VALUE
) {
597 DSOerr(DSO_F_WIN32_GLOBALLOOKUP
, DSO_R_UNSUPPORTED
);
601 me32
.dwSize
= sizeof(me32
);
603 if (!(*module_first
) (hModuleSnap
, &me32
)) {
604 (*close_snap
) (hModuleSnap
);
610 if ((ret
.f
= GetProcAddress(me32
.hModule
, name
))) {
611 (*close_snap
) (hModuleSnap
);
615 } while ((*module_next
) (hModuleSnap
, &me32
));
617 (*close_snap
) (hModuleSnap
);
621 #endif /* DSO_WIN32 */