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