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