1 /* dso_win32.c -*- mode:C; c-file-style: "eay" -*- */
2 /* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL
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).
62 #include <openssl/dso.h>
64 #ifndef OPENSSL_SYS_WIN32
65 DSO_METHOD *DSO_METHOD_win32(void)
71 /* Part of the hack in "win32_load" ... */
72 #define DSO_MAX_TRANSLATED_SIZE 256
74 static int win32_load(DSO *dso);
75 static int win32_unload(DSO *dso);
76 static void *win32_bind_var(DSO *dso, const char *symname);
77 static DSO_FUNC_TYPE win32_bind_func(DSO *dso, const char *symname);
79 static int win32_unbind_var(DSO *dso, char *symname, void *symptr);
80 static int win32_unbind_func(DSO *dso, char *symname, DSO_FUNC_TYPE symptr);
81 static int win32_init(DSO *dso);
82 static int win32_finish(DSO *dso);
83 static long win32_ctrl(DSO *dso, int cmd, long larg, void *parg);
85 static char *win32_name_converter(DSO *dso, const char *filename);
86 static char *win32_merger(DSO *dso, const char *filespec1,
87 const char *filespec2);
89 static DSO_METHOD dso_meth_win32 = {
90 "OpenSSL 'win32' shared library method",
95 /* For now, "unbind" doesn't exist */
97 NULL, /* unbind_var */
98 NULL, /* unbind_func */
101 win32_name_converter,
107 DSO_METHOD *DSO_METHOD_win32(void)
109 return(&dso_meth_win32);
112 /* For this DSO_METHOD, our meth_data STACK will contain;
113 * (i) a pointer to the handle (HINSTANCE) returned from
114 * LoadLibrary(), and copied.
117 static int win32_load(DSO *dso)
119 HINSTANCE h = NULL, *p = NULL;
120 /* See applicable comments from dso_dl.c */
121 char *filename = DSO_convert_filename(dso, NULL);
125 DSOerr(DSO_F_WIN32_LOAD,DSO_R_NO_FILENAME);
128 h = LoadLibrary(filename);
131 DSOerr(DSO_F_WIN32_LOAD,DSO_R_LOAD_FAILED);
132 ERR_add_error_data(3, "filename(", filename, ")");
135 p = (HINSTANCE *)OPENSSL_malloc(sizeof(HINSTANCE));
138 DSOerr(DSO_F_WIN32_LOAD,ERR_R_MALLOC_FAILURE);
142 if(!sk_push(dso->meth_data, (char *)p))
144 DSOerr(DSO_F_WIN32_LOAD,DSO_R_STACK_ERROR);
148 dso->loaded_filename = filename;
153 OPENSSL_free(filename);
161 static int win32_unload(DSO *dso)
166 DSOerr(DSO_F_WIN32_UNLOAD,ERR_R_PASSED_NULL_PARAMETER);
169 if(sk_num(dso->meth_data) < 1)
171 p = (HINSTANCE *)sk_pop(dso->meth_data);
174 DSOerr(DSO_F_WIN32_UNLOAD,DSO_R_NULL_HANDLE);
179 DSOerr(DSO_F_WIN32_UNLOAD,DSO_R_UNLOAD_FAILED);
180 /* We should push the value back onto the stack in
181 * case of a retry. */
182 sk_push(dso->meth_data, (char *)p);
190 /* Using GetProcAddress for variables? TODO: Check this out in
191 * the Win32 API docs, there's probably a variant for variables. */
192 static void *win32_bind_var(DSO *dso, const char *symname)
197 if((dso == NULL) || (symname == NULL))
199 DSOerr(DSO_F_WIN32_BIND_VAR,ERR_R_PASSED_NULL_PARAMETER);
202 if(sk_num(dso->meth_data) < 1)
204 DSOerr(DSO_F_WIN32_BIND_VAR,DSO_R_STACK_ERROR);
207 ptr = (HINSTANCE *)sk_value(dso->meth_data, sk_num(dso->meth_data) - 1);
210 DSOerr(DSO_F_WIN32_BIND_VAR,DSO_R_NULL_HANDLE);
213 sym = GetProcAddress(*ptr, symname);
216 DSOerr(DSO_F_WIN32_BIND_VAR,DSO_R_SYM_FAILURE);
217 ERR_add_error_data(3, "symname(", symname, ")");
223 static DSO_FUNC_TYPE win32_bind_func(DSO *dso, const char *symname)
228 if((dso == NULL) || (symname == NULL))
230 DSOerr(DSO_F_WIN32_BIND_FUNC,ERR_R_PASSED_NULL_PARAMETER);
233 if(sk_num(dso->meth_data) < 1)
235 DSOerr(DSO_F_WIN32_BIND_FUNC,DSO_R_STACK_ERROR);
238 ptr = (HINSTANCE *)sk_value(dso->meth_data, sk_num(dso->meth_data) - 1);
241 DSOerr(DSO_F_WIN32_BIND_FUNC,DSO_R_NULL_HANDLE);
244 sym = GetProcAddress(*ptr, symname);
247 DSOerr(DSO_F_WIN32_BIND_FUNC,DSO_R_SYM_FAILURE);
248 ERR_add_error_data(3, "symname(", symname, ")");
251 return((DSO_FUNC_TYPE)sym);
256 const char *node; int nodelen;
257 const char *device; int devicelen;
258 const char *predir; int predirlen;
259 const char *dir; int dirlen;
260 const char *file; int filelen;
263 static struct file_st *win32_splitter(DSO *dso, const char *filename,
264 int assume_last_is_dir)
266 struct file_st *result = NULL;
267 enum { IN_NODE, IN_DEVICE, IN_FILE } position;
268 const char *start = filename;
272 DSOerr(DSO_F_WIN32_MERGER,DSO_R_NO_FILENAME);
276 result = OPENSSL_malloc(sizeof(struct file_st));
279 DSOerr(DSO_F_WIN32_MERGER,
280 ERR_R_MALLOC_FAILURE);
284 memset(result, 0, sizeof(struct file_st));
285 position = IN_DEVICE;
287 if(filename[0] == '\\' && filename[1] == '\\'
288 || filename[0] == '/' && filename[1] == '/')
293 result->node = start;
301 if(position != IN_DEVICE)
303 DSOerr(DSO_F_WIN32_MERGER,
304 DSO_R_INCORRECT_FILE_SYNTAX);
307 result->device = start;
308 result->devicelen = filename - start;
315 if(position == IN_NODE)
317 result->nodelen = filename - start;
325 result->dirlen += filename - start;
329 if(position == IN_NODE)
331 result->nodelen = filename - start;
335 if(filename - start > 0)
337 if (assume_last_is_dir)
339 result->devicelen += filename - start;
343 result->file = start;
344 result->filelen = filename - start;
356 if(!result->nodelen) result->node = NULL;
357 if(!result->devicelen) result->devicce = NULL;
358 if(!result->dirlen) result->dir = NULL;
359 if(!result->filelen) result->file = NULL;
364 static char *win32_joiner(DSO *dso, const file_st *file_split)
366 int len = 0, offset = 0;
372 DSOerr(DSO_F_WIN32_MERGER,
373 ERR_R_PASSED_NULL_PARAMETER);
378 len += 2 + file_split->nodelen; /* 2 for starting \\ */
379 if(file_split->predir || file_split->dir || file_split->file)
380 len++; /* 1 for ending \ */
382 else if(file_split->device)
384 len += file_split->devicelen + 1; /* 1 for ending : */
386 len += file_split->predirlen;
387 if(file_split->predir && (file_split->dir || file_split->file))
389 len++; /* 1 for ending \ */
391 len += file_split->dirlen;
392 if(file_split->dir && file_split->file)
394 len++; /* 1 for ending \ */
396 len += file_split->filelen;
400 DSOerr(DSO_F_WIN32_MERGER, DSO_R_EMPTY_FILE_STRUCTURE);
404 result = OPENSSL_malloc(len + 1);
407 DSOerr(DSO_F_WIN32_MERGER,
408 ERR_R_MALLOC_FAILURE);
414 strcpy(&result[offset], "\\\\"); offset += 2;
415 strncpy(&result[offset], file_split->node,
416 file_split->nodelen); offset += file_split->nodelen;
417 if(file_split->predir || file_split->dir || file_split->file)
419 result[offset] = '\\'; offset++;
422 else if(file_split->device)
424 strncpy(&result[offset], file_split->device,
425 file_split->devicelen); offset += file_split->devicelen;
426 result[offset] = ':'; offset++;
428 start = file_split->predir;
429 while(file_split->predirlen > (start - file_split->predir))
431 const char *end = strnchr(start, '/',
432 file_split->predirlen - (start - file_split->predir));
435 + file_split->predirlen
436 - (start - file_split->predir);
437 strncpy(&result[offset], start,
438 end - start); offset += end - start;
439 result[offset] = '\\'; offset++;
442 if(file_split->predir && (file_split->dir || file_split->file))
444 result[offset] = '\\'; offset++;
446 start = file_split->dir;
447 while(file_split->dirlen > (start - file_split->dir))
449 const char *end = strnchr(start, '/',
450 file_split->dirlen - (start - file_split->dir));
454 - (start - file_split->dir);
455 strncpy(&result[offset], start,
456 end - start); offset += end - start;
457 result[offset] = '\\'; offset++;
460 if(file_split->dir && file_split->file)
462 result[offset] = '\\'; offset++;
464 strncpy(&result[offset], file_split->file,
465 file_split->filelen); offset += file_split->filelen;
466 result[offset] = '\0';
470 static char *win32_merger(DSO *dso, const char *filespec1, const char *filespec2)
473 struct file_st *filespec1_split = NULL;
474 struct file_st *filespec2_split = NULL;
476 if(!filespec1 && !filespec2)
478 DSOerr(DSO_F_WIN32_MERGER,
479 ERR_R_PASSED_NULL_PARAMETER);
484 merged = OPENSSL_malloc(strlen(filespec1) + 1);
487 DSOerr(DSO_F_WIN32_MERGER,
488 ERR_R_MALLOC_FAILURE);
491 strcpy(merged, filespec1);
495 merged = OPENSSL_malloc(strlen(filespec2) + 1);
498 DSOerr(DSO_F_WIN32_MERGER,
499 ERR_R_MALLOC_FAILURE);
502 strcpy(merged, filespec2);
506 filespec1_split = win32_splitter(dso, filespec1, 1);
507 if (!filespec1_split)
509 DSOerr(DSO_F_WIN32_MERGER,
510 ERR_R_MALLOC_FAILURE);
513 filespec2_split = win32_splitter(dso, filespec2, 0);
514 if (!filespec1_split)
516 DSOerr(DSO_F_WIN32_MERGER,
517 ERR_R_MALLOC_FAILURE);
518 OPENSSL_free(filespec1_split);
522 /* Fill in into filespec1_split */
523 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)
532 filespec1_split->dir = filespec2_split->dir;
533 filespec1_split->dirlen = filespec2_split->dirlen;
535 else if (filespec1_split->dir[0] != '\\'
536 && filespec1_split->dir[0] != '/')
538 filespec1_split->predir = filespec2_split->dir;
539 filespec1_split->predirlen = filespec2_split->dirlen;
541 if (!filespec1_split->file)
543 filespec1_split->file = filespec2_split->file;
544 filespec1_split->filelen = filespec2_split->filelen;
547 merged = win32_joiner(dso, filespec1_split);
552 static char *win32_name_converter(DSO *dso, const char *filename)
557 len = strlen(filename);
558 transform = ((strstr(filename, "/") == NULL) &&
559 (strstr(filename, "\\") == NULL) &&
560 (strstr(filename, ":") == NULL));
562 /* We will convert this to "%s.dll" */
563 translated = OPENSSL_malloc(len + 5);
565 /* We will simply duplicate filename */
566 translated = OPENSSL_malloc(len + 1);
567 if(translated == NULL)
569 DSOerr(DSO_F_WIN32_NAME_CONVERTER,
570 DSO_R_NAME_TRANSLATION_FAILED);
574 sprintf(translated, "%s.dll", filename);
576 sprintf(translated, "%s", filename);
580 #endif /* OPENSSL_SYS_WIN32 */