b44f25c5799360d3307f0a151417c4ad87010f6e
[openssl.git] / crypto / dso / dso_win32.c
1 /* dso_win32.c -*- mode:C; c-file-style: "eay" -*- */
2 /* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL
3  * project 2000.
4  */
5 /* ====================================================================
6  * Copyright (c) 2000 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer. 
14  *
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
18  *    distribution.
19  *
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/)"
24  *
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.
29  *
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.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
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  * ====================================================================
52  *
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).
56  *
57  */
58
59 #include <stdio.h>
60 #include <string.h>
61 #include "cryptlib.h"
62 #include <openssl/dso.h>
63
64 #if !defined(DSO_WIN32)
65 DSO_METHOD *DSO_METHOD_win32(void)
66         {
67         return NULL;
68         }
69 #else
70
71 #ifdef _WIN32_WCE
72 # if _WIN32_WCE < 300
73 static FARPROC GetProcAddressA(HMODULE hModule,LPCSTR lpProcName)
74         {
75         WCHAR lpProcNameW[64];
76         int i;
77
78         for (i=0;lpProcName[i] && i<64;i++)
79                 lpProcNameW[i] = (WCHAR)lpProcName[i];
80         if (i==64) return NULL;
81         lpProcNameW[i] = 0;
82
83         return GetProcAddressW(hModule,lpProcNameW);
84         }
85 # endif
86 # undef GetProcAddress
87 # define GetProcAddress GetProcAddressA
88
89 static HINSTANCE LoadLibraryA(LPCSTR lpLibFileName)
90         {
91         WCHAR *fnamw;
92         size_t len_0=strlen(lpLibFileName)+1,i;
93
94 #ifdef _MSC_VER
95         fnamw = (WCHAR *)_alloca (len_0*sizeof(WCHAR));
96 #else
97         fnamw = (WCHAR *)alloca (len_0*sizeof(WCHAR));
98 #endif
99         if (fnamw == NULL)
100                 {
101                 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
102                 return NULL;
103                 }
104
105 #if defined(_WIN32_WCE) && _WIN32_WCE>=101
106         if (!MultiByteToWideChar(CP_ACP,0,lpLibFileName,len_0,fnamw,len_0))
107 #endif
108                 for (i=0;i<len_0;i++) fnamw[i]=(WCHAR)lpLibFileName[i];
109
110         return LoadLibraryW(fnamw);
111         }
112 #endif
113
114 /* Part of the hack in "win32_load" ... */
115 #define DSO_MAX_TRANSLATED_SIZE 256
116
117 static int win32_load(DSO *dso);
118 static int win32_unload(DSO *dso);
119 static void *win32_bind_var(DSO *dso, const char *symname);
120 static DSO_FUNC_TYPE win32_bind_func(DSO *dso, const char *symname);
121 #if 0
122 static int win32_unbind_var(DSO *dso, char *symname, void *symptr);
123 static int win32_unbind_func(DSO *dso, char *symname, DSO_FUNC_TYPE symptr);
124 static int win32_init(DSO *dso);
125 static int win32_finish(DSO *dso);
126 static long win32_ctrl(DSO *dso, int cmd, long larg, void *parg);
127 #endif
128 static char *win32_name_converter(DSO *dso, const char *filename);
129 static char *win32_merger(DSO *dso, const char *filespec1,
130         const char *filespec2);
131 static int win32_pathbyaddr(void *addr,char *path,int sz);
132
133 static const char *openssl_strnchr(const char *string, int c, size_t len);
134
135 static DSO_METHOD dso_meth_win32 = {
136         "OpenSSL 'win32' shared library method",
137         win32_load,
138         win32_unload,
139         win32_bind_var,
140         win32_bind_func,
141 /* For now, "unbind" doesn't exist */
142 #if 0
143         NULL, /* unbind_var */
144         NULL, /* unbind_func */
145 #endif
146         NULL, /* ctrl */
147         win32_name_converter,
148         win32_merger,
149         NULL, /* init */
150         NULL, /* finish */
151         win32_pathbyaddr
152         };
153
154 DSO_METHOD *DSO_METHOD_win32(void)
155         {
156         return(&dso_meth_win32);
157         }
158
159 /* For this DSO_METHOD, our meth_data STACK will contain;
160  * (i) a pointer to the handle (HINSTANCE) returned from
161  *     LoadLibrary(), and copied.
162  */
163
164 static int win32_load(DSO *dso)
165         {
166         HINSTANCE h = NULL, *p = NULL;
167         /* See applicable comments from dso_dl.c */
168         char *filename = DSO_convert_filename(dso, NULL);
169
170         if(filename == NULL)
171                 {
172                 DSOerr(DSO_F_WIN32_LOAD,DSO_R_NO_FILENAME);
173                 goto err;
174                 }
175         h = LoadLibraryA(filename);
176         if(h == NULL)
177                 {
178                 DSOerr(DSO_F_WIN32_LOAD,DSO_R_LOAD_FAILED);
179                 ERR_add_error_data(3, "filename(", filename, ")");
180                 goto err;
181                 }
182         p = (HINSTANCE *)OPENSSL_malloc(sizeof(HINSTANCE));
183         if(p == NULL)
184                 {
185                 DSOerr(DSO_F_WIN32_LOAD,ERR_R_MALLOC_FAILURE);
186                 goto err;
187                 }
188         *p = h;
189         if(!sk_push(dso->meth_data, (char *)p))
190                 {
191                 DSOerr(DSO_F_WIN32_LOAD,DSO_R_STACK_ERROR);
192                 goto err;
193                 }
194         /* Success */
195         dso->loaded_filename = filename;
196         return(1);
197 err:
198         /* Cleanup !*/
199         if(filename != NULL)
200                 OPENSSL_free(filename);
201         if(p != NULL)
202                 OPENSSL_free(p);
203         if(h != NULL)
204                 FreeLibrary(h);
205         return(0);
206         }
207
208 static int win32_unload(DSO *dso)
209         {
210         HINSTANCE *p;
211         if(dso == NULL)
212                 {
213                 DSOerr(DSO_F_WIN32_UNLOAD,ERR_R_PASSED_NULL_PARAMETER);
214                 return(0);
215                 }
216         if(sk_num(dso->meth_data) < 1)
217                 return(1);
218         p = (HINSTANCE *)sk_pop(dso->meth_data);
219         if(p == NULL)
220                 {
221                 DSOerr(DSO_F_WIN32_UNLOAD,DSO_R_NULL_HANDLE);
222                 return(0);
223                 }
224         if(!FreeLibrary(*p))
225                 {
226                 DSOerr(DSO_F_WIN32_UNLOAD,DSO_R_UNLOAD_FAILED);
227                 /* We should push the value back onto the stack in
228                  * case of a retry. */
229                 sk_push(dso->meth_data, (char *)p);
230                 return(0);
231                 }
232         /* Cleanup */
233         OPENSSL_free(p);
234         return(1);
235         }
236
237 /* Using GetProcAddress for variables? TODO: Check this out in
238  * the Win32 API docs, there's probably a variant for variables. */
239 static void *win32_bind_var(DSO *dso, const char *symname)
240         {
241         HINSTANCE *ptr;
242         void *sym;
243
244         if((dso == NULL) || (symname == NULL))
245                 {
246                 DSOerr(DSO_F_WIN32_BIND_VAR,ERR_R_PASSED_NULL_PARAMETER);
247                 return(NULL);
248                 }
249         if(sk_num(dso->meth_data) < 1)
250                 {
251                 DSOerr(DSO_F_WIN32_BIND_VAR,DSO_R_STACK_ERROR);
252                 return(NULL);
253                 }
254         ptr = (HINSTANCE *)sk_value(dso->meth_data, sk_num(dso->meth_data) - 1);
255         if(ptr == NULL)
256                 {
257                 DSOerr(DSO_F_WIN32_BIND_VAR,DSO_R_NULL_HANDLE);
258                 return(NULL);
259                 }
260         sym = GetProcAddress(*ptr, symname);
261         if(sym == NULL)
262                 {
263                 DSOerr(DSO_F_WIN32_BIND_VAR,DSO_R_SYM_FAILURE);
264                 ERR_add_error_data(3, "symname(", symname, ")");
265                 return(NULL);
266                 }
267         return(sym);
268         }
269
270 static DSO_FUNC_TYPE win32_bind_func(DSO *dso, const char *symname)
271         {
272         HINSTANCE *ptr;
273         void *sym;
274
275         if((dso == NULL) || (symname == NULL))
276                 {
277                 DSOerr(DSO_F_WIN32_BIND_FUNC,ERR_R_PASSED_NULL_PARAMETER);
278                 return(NULL);
279                 }
280         if(sk_num(dso->meth_data) < 1)
281                 {
282                 DSOerr(DSO_F_WIN32_BIND_FUNC,DSO_R_STACK_ERROR);
283                 return(NULL);
284                 }
285         ptr = (HINSTANCE *)sk_value(dso->meth_data, sk_num(dso->meth_data) - 1);
286         if(ptr == NULL)
287                 {
288                 DSOerr(DSO_F_WIN32_BIND_FUNC,DSO_R_NULL_HANDLE);
289                 return(NULL);
290                 }
291         sym = GetProcAddress(*ptr, symname);
292         if(sym == NULL)
293                 {
294                 DSOerr(DSO_F_WIN32_BIND_FUNC,DSO_R_SYM_FAILURE);
295                 ERR_add_error_data(3, "symname(", symname, ")");
296                 return(NULL);
297                 }
298         return((DSO_FUNC_TYPE)sym);
299         }
300
301 struct file_st
302         {
303         const char *node; int nodelen;
304         const char *device; int devicelen;
305         const char *predir; int predirlen;
306         const char *dir; int dirlen;
307         const char *file; int filelen;
308         };
309
310 static struct file_st *win32_splitter(DSO *dso, const char *filename,
311         int assume_last_is_dir)
312         {
313         struct file_st *result = NULL;
314         enum { IN_NODE, IN_DEVICE, IN_FILE } position;
315         const char *start = filename;
316         char last;
317
318         if (!filename)
319                 {
320                 DSOerr(DSO_F_WIN32_SPLITTER,DSO_R_NO_FILENAME);
321                 /*goto err;*/
322                 return(NULL);
323                 }
324
325         result = OPENSSL_malloc(sizeof(struct file_st));
326         if(result == NULL)
327                 {
328                 DSOerr(DSO_F_WIN32_SPLITTER,
329                         ERR_R_MALLOC_FAILURE);
330                 return(NULL);
331                 }
332
333         memset(result, 0, sizeof(struct file_st));
334         position = IN_DEVICE;
335
336         if((filename[0] == '\\' && filename[1] == '\\')
337                 || (filename[0] == '/' && filename[1] == '/'))
338                 {
339                 position = IN_NODE;
340                 filename += 2;
341                 start = filename;
342                 result->node = start;
343                 }
344
345         do
346                 {
347                 last = filename[0];
348                 switch(last)
349                         {
350                 case ':':
351                         if(position != IN_DEVICE)
352                                 {
353                                 DSOerr(DSO_F_WIN32_SPLITTER,
354                                         DSO_R_INCORRECT_FILE_SYNTAX);
355                                 /*goto err;*/
356                                 return(NULL);
357                                 }
358                         result->device = start;
359                         result->devicelen = filename - start;
360                         position = IN_FILE;
361                         start = ++filename;
362                         result->dir = start;
363                         break;
364                 case '\\':
365                 case '/':
366                         if(position == IN_NODE)
367                                 {
368                                 result->nodelen = filename - start;
369                                 position = IN_FILE;
370                                 start = ++filename;
371                                 result->dir = start;
372                                 }
373                         else if(position == IN_DEVICE)
374                                 {
375                                 position = IN_FILE;
376                                 filename++;
377                                 result->dir = start;
378                                 result->dirlen = filename - start;
379                                 start = filename;
380                                 }
381                         else
382                                 {
383                                 filename++;
384                                 result->dirlen += filename - start;
385                                 start = filename;
386                                 }
387                         break;
388                 case '\0':
389                         if(position == IN_NODE)
390                                 {
391                                 result->nodelen = filename - start;
392                                 }
393                         else
394                                 {
395                                 if(filename - start > 0)
396                                         {
397                                         if (assume_last_is_dir)
398                                                 {
399                                                 if (position == IN_DEVICE)
400                                                         {
401                                                         result->dir = start;
402                                                         result->dirlen = 0;
403                                                         }
404                                                 result->dirlen +=
405                                                         filename - start;
406                                                 }
407                                         else
408                                                 {
409                                                 result->file = start;
410                                                 result->filelen =
411                                                         filename - start;
412                                                 }
413                                         }
414                                 }
415                         break;
416                 default:
417                         filename++;
418                         break;
419                         }
420                 }
421         while(last);
422
423         if(!result->nodelen) result->node = NULL;
424         if(!result->devicelen) result->device = NULL;
425         if(!result->dirlen) result->dir = NULL;
426         if(!result->filelen) result->file = NULL;
427
428         return(result);
429         }
430
431 static char *win32_joiner(DSO *dso, const struct file_st *file_split)
432         {
433         int len = 0, offset = 0;
434         char *result = NULL;
435         const char *start;
436
437         if(!file_split)
438                 {
439                 DSOerr(DSO_F_WIN32_JOINER,
440                                 ERR_R_PASSED_NULL_PARAMETER);
441                 return(NULL);
442                 }
443         if(file_split->node)
444                 {
445                 len += 2 + file_split->nodelen; /* 2 for starting \\ */
446                 if(file_split->predir || file_split->dir || file_split->file)
447                         len++;  /* 1 for ending \ */
448                 }
449         else if(file_split->device)
450                 {
451                 len += file_split->devicelen + 1; /* 1 for ending : */
452                 }
453         len += file_split->predirlen;
454         if(file_split->predir && (file_split->dir || file_split->file))
455                 {
456                 len++;  /* 1 for ending \ */
457                 }
458         len += file_split->dirlen;
459         if(file_split->dir && file_split->file)
460                 {
461                 len++;  /* 1 for ending \ */
462                 }
463         len += file_split->filelen;
464
465         if(!len)
466                 {
467                 DSOerr(DSO_F_WIN32_JOINER, DSO_R_EMPTY_FILE_STRUCTURE);
468                 return(NULL);
469                 }
470
471         result = OPENSSL_malloc(len + 1);
472         if (!result)
473                 {
474                 DSOerr(DSO_F_WIN32_JOINER,
475                         ERR_R_MALLOC_FAILURE);
476                 return(NULL);
477                 }
478
479         if(file_split->node)
480                 {
481                 strcpy(&result[offset], "\\\\"); offset += 2;
482                 strncpy(&result[offset], file_split->node,
483                         file_split->nodelen); offset += file_split->nodelen;
484                 if(file_split->predir || file_split->dir || file_split->file)
485                         {
486                         result[offset] = '\\'; offset++;
487                         }
488                 }
489         else if(file_split->device)
490                 {
491                 strncpy(&result[offset], file_split->device,
492                         file_split->devicelen); offset += file_split->devicelen;
493                 result[offset] = ':'; offset++;
494                 }
495         start = file_split->predir;
496         while(file_split->predirlen > (start - file_split->predir))
497                 {
498                 const char *end = openssl_strnchr(start, '/',
499                         file_split->predirlen - (start - file_split->predir));
500                 if(!end)
501                         end = start
502                                 + file_split->predirlen
503                                 - (start - file_split->predir);
504                 strncpy(&result[offset], start,
505                         end - start); offset += end - start;
506                 result[offset] = '\\'; offset++;
507                 start = end + 1;
508                 }
509 #if 0 /* Not needed, since the directory converter above already appeneded
510          a backslash */
511         if(file_split->predir && (file_split->dir || file_split->file))
512                 {
513                 result[offset] = '\\'; offset++;
514                 }
515 #endif
516         start = file_split->dir;
517         while(file_split->dirlen > (start - file_split->dir))
518                 {
519                 const char *end = openssl_strnchr(start, '/',
520                         file_split->dirlen - (start - file_split->dir));
521                 if(!end)
522                         end = start
523                                 + file_split->dirlen
524                                 - (start - file_split->dir);
525                 strncpy(&result[offset], start,
526                         end - start); offset += end - start;
527                 result[offset] = '\\'; offset++;
528                 start = end + 1;
529                 }
530 #if 0 /* Not needed, since the directory converter above already appeneded
531          a backslash */
532         if(file_split->dir && file_split->file)
533                 {
534                 result[offset] = '\\'; offset++;
535                 }
536 #endif
537         strncpy(&result[offset], file_split->file,
538                 file_split->filelen); offset += file_split->filelen;
539         result[offset] = '\0';
540         return(result);
541         }
542
543 static char *win32_merger(DSO *dso, const char *filespec1, const char *filespec2)
544         {
545         char *merged = NULL;
546         struct file_st *filespec1_split = NULL;
547         struct file_st *filespec2_split = NULL;
548
549         if(!filespec1 && !filespec2)
550                 {
551                 DSOerr(DSO_F_WIN32_MERGER,
552                                 ERR_R_PASSED_NULL_PARAMETER);
553                 return(NULL);
554                 }
555         if (!filespec2)
556                 {
557                 merged = OPENSSL_malloc(strlen(filespec1) + 1);
558                 if(!merged)
559                         {
560                         DSOerr(DSO_F_WIN32_MERGER,
561                                 ERR_R_MALLOC_FAILURE);
562                         return(NULL);
563                         }
564                 strcpy(merged, filespec1);
565                 }
566         else if (!filespec1)
567                 {
568                 merged = OPENSSL_malloc(strlen(filespec2) + 1);
569                 if(!merged)
570                         {
571                         DSOerr(DSO_F_WIN32_MERGER,
572                                 ERR_R_MALLOC_FAILURE);
573                         return(NULL);
574                         }
575                 strcpy(merged, filespec2);
576                 }
577         else
578                 {
579                 filespec1_split = win32_splitter(dso, filespec1, 1);
580                 if (!filespec1_split)
581                         {
582                         DSOerr(DSO_F_WIN32_MERGER,
583                                 ERR_R_MALLOC_FAILURE);
584                         return(NULL);
585                         }
586                 filespec2_split = win32_splitter(dso, filespec2, 0);
587                 if (!filespec2_split)
588                         {
589                         DSOerr(DSO_F_WIN32_MERGER,
590                                 ERR_R_MALLOC_FAILURE);
591                         OPENSSL_free(filespec1_split);
592                         return(NULL);
593                         }
594
595                 /* Fill in into filespec1_split */
596                 if (!filespec1_split->node && !filespec1_split->device)
597                         {
598                         filespec1_split->node = filespec2_split->node;
599                         filespec1_split->nodelen = filespec2_split->nodelen;
600                         filespec1_split->device = filespec2_split->device;
601                         filespec1_split->devicelen = filespec2_split->devicelen;
602                         }
603                 if (!filespec1_split->dir)
604                         {
605                         filespec1_split->dir = filespec2_split->dir;
606                         filespec1_split->dirlen = filespec2_split->dirlen;
607                         }
608                 else if (filespec1_split->dir[0] != '\\'
609                         && filespec1_split->dir[0] != '/')
610                         {
611                         filespec1_split->predir = filespec2_split->dir;
612                         filespec1_split->predirlen = filespec2_split->dirlen;
613                         }
614                 if (!filespec1_split->file)
615                         {
616                         filespec1_split->file = filespec2_split->file;
617                         filespec1_split->filelen = filespec2_split->filelen;
618                         }
619
620                 merged = win32_joiner(dso, filespec1_split);
621                 }
622         return(merged);
623         }
624
625 static char *win32_name_converter(DSO *dso, const char *filename)
626         {
627         char *translated;
628         int len, transform;
629
630         len = strlen(filename);
631         transform = ((strstr(filename, "/") == NULL) &&
632                         (strstr(filename, "\\") == NULL) &&
633                         (strstr(filename, ":") == NULL));
634         if(transform)
635                 /* We will convert this to "%s.dll" */
636                 translated = OPENSSL_malloc(len + 5);
637         else
638                 /* We will simply duplicate filename */
639                 translated = OPENSSL_malloc(len + 1);
640         if(translated == NULL)
641                 {
642                 DSOerr(DSO_F_WIN32_NAME_CONVERTER,
643                                 DSO_R_NAME_TRANSLATION_FAILED); 
644                 return(NULL);   
645                 }
646         if(transform)
647                 sprintf(translated, "%s.dll", filename);
648         else
649                 sprintf(translated, "%s", filename);
650         return(translated);
651         }
652
653 static const char *openssl_strnchr(const char *string, int c, size_t len)
654         {
655         size_t i;
656         const char *p;
657         for (i = 0, p = string; i < len && *p; i++, p++)
658                 {
659                 if (*p == c)
660                         return p;
661                 }
662         return NULL;
663         }
664
665 #include <tlhelp32.h>
666 #ifdef _WIN32_WCE
667 # define DLLNAME "TOOLHELP.DLL"
668 #else
669 # ifdef MODULEENTRY32
670 # undef MODULEENTRY32   /* unmask the ASCII version! */
671 # endif
672 # define DLLNAME "KERNEL32.DLL"
673 #endif
674
675 typedef HANDLE (WINAPI *CREATETOOLHELP32SNAPSHOT)(DWORD, DWORD);
676 typedef BOOL (WINAPI *CLOSETOOLHELP32SNAPSHOT)(HANDLE);
677 typedef BOOL (WINAPI *MODULE32)(HANDLE, MODULEENTRY32 *);
678
679 static int win32_pathbyaddr(void *addr,char *path,int sz)
680         {
681         HMODULE dll;
682         HANDLE hModuleSnap = INVALID_HANDLE_VALUE; 
683         MODULEENTRY32 me32; 
684         CREATETOOLHELP32SNAPSHOT create_snap;
685         CLOSETOOLHELP32SNAPSHOT  close_snap;
686         MODULE32 module_first, module_next;
687         int len;
688  
689         if (addr == NULL)
690                 {
691                 union   { int(*f)(void*,char*,int); void *p; } t =
692                         { win32_pathbyaddr };
693                 addr = t.p;
694                 }
695
696         dll = LoadLibrary(TEXT(DLLNAME));
697         if (dll == NULL)
698                 {
699                 DSOerr(DSO_F_PATHBYADDR,DSO_R_UNSUPPORTED);
700                 return -1;
701                 }
702
703         create_snap = (CREATETOOLHELP32SNAPSHOT)
704                 GetProcAddress(dll,"CreateToolhelp32Snapshot");
705         if (create_snap == NULL)
706                 {
707                 FreeLibrary(dll);
708                 DSOerr(DSO_F_PATHBYADDR,DSO_R_UNSUPPORTED);
709                 return -1;
710                 }
711         /* We take the rest for granted... */
712 #ifdef _WIN32_WCE
713         close_snap = (CLOSETOOLHELP32SNAPSHOT)
714                 GetProcAddress(dll,"CloseToolhelp32Snapshot");
715 #else
716         close_snap = (CLOSETOOLHELP32SNAPSHOT)CloseHandle;
717 #endif
718         module_first = (MODULE32)GetProcAddress(dll,"Module32First");
719         module_next  = (MODULE32)GetProcAddress(dll,"Module32Next");
720
721         hModuleSnap = (*create_snap)(TH32CS_SNAPMODULE,0); 
722         if( hModuleSnap == INVALID_HANDLE_VALUE ) 
723                 { 
724                 FreeLibrary(dll);
725                 DSOerr(DSO_F_PATHBYADDR,DSO_R_UNSUPPORTED);
726                 return -1;
727                 } 
728  
729         me32.dwSize = sizeof(me32); 
730  
731         if(!(*module_first)(hModuleSnap,&me32)) 
732                 { 
733                 (*close_snap)(hModuleSnap);
734                 FreeLibrary(dll);
735                 DSOerr(DSO_F_PATHBYADDR,DSO_R_FAILURE);
736                 return -1;
737                 }
738  
739         do      { 
740                 if ((BYTE *)addr >= me32.modBaseAddr &&
741                     (BYTE *)addr <  me32.modBaseAddr+me32.modBaseSize)
742                         {
743                         (*close_snap)(hModuleSnap);
744                         FreeLibrary(dll);
745 #ifdef _WIN32_WCE
746 # if _WIN32_WCE >= 101
747                         return WideCharToMultiByte(CP_ACP,0,me32.szExePath,-1,
748                                                         path,sz,NULL,NULL);
749 # else
750                         len = (int)wcslen(me32.szExePath);
751                         if (sz <= 0) return len+1;
752                         if (len >= sz) len=sz-1;
753                         for(i=0;i<len;i++)
754                                 path[i] = (char)me32.szExePath[i];
755                         path[len++] = 0;
756                         return len;
757 # endif
758 #else
759                         len = (int)strlen(me32.szExePath);
760                         if (sz <= 0) return len+1;
761                         if (len >= sz) len=sz-1;
762                         memcpy(path,me32.szExePath,len);
763                         path[len++] = 0;
764                         return len;
765 #endif
766                         } 
767                 } while((*module_next)(hModuleSnap, &me32)); 
768  
769         (*close_snap)(hModuleSnap); 
770         FreeLibrary(dll);
771         return 0;
772         }
773 #endif /* DSO_WIN32 */