Fix mingw warnings.
[openssl.git] / crypto / dso / dso_win32.c
index c28184099af8af6d5f85f3264a6260d54f802eb1..eea0b07c3c6bb23b8e84ab3e69caf3ded94a0584 100644 (file)
@@ -85,6 +85,30 @@ static FARPROC GetProcAddressA(HMODULE hModule,LPCSTR lpProcName)
 # endif
 # undef GetProcAddress
 # define GetProcAddress GetProcAddressA
+
+static HINSTANCE LoadLibraryA(LPCSTR lpLibFileName)
+       {
+       WCHAR *fnamw;
+       size_t len_0=strlen(lpLibFileName)+1,i;
+
+#ifdef _MSC_VER
+       fnamw = (WCHAR *)_alloca (len_0*sizeof(WCHAR));
+#else
+       fnamw = (WCHAR *)alloca (len_0*sizeof(WCHAR));
+#endif
+       if (fnamw == NULL)
+               {
+               SetLastError(ERROR_NOT_ENOUGH_MEMORY);
+               return NULL;
+               }
+
+#if defined(_WIN32_WCE) && _WIN32_WCE>=101
+       if (!MultiByteToWideChar(CP_ACP,0,lpLibFileName,len_0,fnamw,len_0))
+#endif
+               for (i=0;i<len_0;i++) fnamw[i]=(WCHAR)lpLibFileName[i];
+
+       return LoadLibraryW(fnamw);
+       }
 #endif
 
 /* Part of the hack in "win32_load" ... */
@@ -105,6 +129,7 @@ static char *win32_name_converter(DSO *dso, const char *filename);
 static char *win32_merger(DSO *dso, const char *filespec1,
        const char *filespec2);
 static int win32_pathbyaddr(void *addr,char *path,int sz);
+static void *win32_globallookup(const char *name);
 
 static const char *openssl_strnchr(const char *string, int c, size_t len);
 
@@ -124,7 +149,8 @@ static DSO_METHOD dso_meth_win32 = {
        win32_merger,
        NULL, /* init */
        NULL, /* finish */
-       win32_pathbyaddr
+       win32_pathbyaddr,
+       win32_globallookup
        };
 
 DSO_METHOD *DSO_METHOD_win32(void)
@@ -289,6 +315,7 @@ static struct file_st *win32_splitter(DSO *dso, const char *filename,
        struct file_st *result = NULL;
        enum { IN_NODE, IN_DEVICE, IN_FILE } position;
        const char *start = filename;
+       char last;
 
        if (!filename)
                {
@@ -308,8 +335,8 @@ static struct file_st *win32_splitter(DSO *dso, const char *filename,
        memset(result, 0, sizeof(struct file_st));
        position = IN_DEVICE;
 
-       if(filename[0] == '\\' && filename[1] == '\\'
-               || filename[0] == '/' && filename[1] == '/')
+       if((filename[0] == '\\' && filename[1] == '\\')
+               || (filename[0] == '/' && filename[1] == '/'))
                {
                position = IN_NODE;
                filename += 2;
@@ -319,7 +346,8 @@ static struct file_st *win32_splitter(DSO *dso, const char *filename,
 
        do
                {
-               switch(filename[0])
+               last = filename[0];
+               switch(last)
                        {
                case ':':
                        if(position != IN_DEVICE)
@@ -344,10 +372,19 @@ static struct file_st *win32_splitter(DSO *dso, const char *filename,
                                start = ++filename;
                                result->dir = start;
                                }
+                       else if(position == IN_DEVICE)
+                               {
+                               position = IN_FILE;
+                               filename++;
+                               result->dir = start;
+                               result->dirlen = filename - start;
+                               start = filename;
+                               }
                        else
                                {
                                filename++;
                                result->dirlen += filename - start;
+                               start = filename;
                                }
                        break;
                case '\0':
@@ -361,12 +398,19 @@ static struct file_st *win32_splitter(DSO *dso, const char *filename,
                                        {
                                        if (assume_last_is_dir)
                                                {
-                                               result->devicelen += filename - start;
+                                               if (position == IN_DEVICE)
+                                                       {
+                                                       result->dir = start;
+                                                       result->dirlen = 0;
+                                                       }
+                                               result->dirlen +=
+                                                       filename - start;
                                                }
                                        else
                                                {
                                                result->file = start;
-                                               result->filelen = filename - start;
+                                               result->filelen =
+                                                       filename - start;
                                                }
                                        }
                                }
@@ -376,7 +420,7 @@ static struct file_st *win32_splitter(DSO *dso, const char *filename,
                        break;
                        }
                }
-       while(*filename);
+       while(last);
 
        if(!result->nodelen) result->node = NULL;
        if(!result->devicelen) result->device = NULL;
@@ -464,10 +508,13 @@ static char *win32_joiner(DSO *dso, const struct file_st *file_split)
                result[offset] = '\\'; offset++;
                start = end + 1;
                }
+#if 0 /* Not needed, since the directory converter above already appeneded
+        a backslash */
        if(file_split->predir && (file_split->dir || file_split->file))
                {
                result[offset] = '\\'; offset++;
                }
+#endif
        start = file_split->dir;
        while(file_split->dirlen > (start - file_split->dir))
                {
@@ -482,10 +529,13 @@ static char *win32_joiner(DSO *dso, const struct file_st *file_split)
                result[offset] = '\\'; offset++;
                start = end + 1;
                }
+#if 0 /* Not needed, since the directory converter above already appeneded
+        a backslash */
        if(file_split->dir && file_split->file)
                {
                result[offset] = '\\'; offset++;
                }
+#endif
        strncpy(&result[offset], file_split->file,
                file_split->filelen); offset += file_split->filelen;
        result[offset] = '\0';
@@ -528,15 +578,15 @@ static char *win32_merger(DSO *dso, const char *filespec1, const char *filespec2
                }
        else
                {
-               filespec1_split = win32_splitter(dso, filespec1, 1);
+               filespec1_split = win32_splitter(dso, filespec1, 0);
                if (!filespec1_split)
                        {
                        DSOerr(DSO_F_WIN32_MERGER,
                                ERR_R_MALLOC_FAILURE);
                        return(NULL);
                        }
-               filespec2_split = win32_splitter(dso, filespec2, 0);
-               if (!filespec1_split)
+               filespec2_split = win32_splitter(dso, filespec2, 1);
+               if (!filespec2_split)
                        {
                        DSOerr(DSO_F_WIN32_MERGER,
                                ERR_R_MALLOC_FAILURE);
@@ -648,7 +698,7 @@ static int win32_pathbyaddr(void *addr,char *path,int sz)
        dll = LoadLibrary(TEXT(DLLNAME));
        if (dll == NULL)
                {
-               DSOerr(DSO_F_PATHBYADDR,DSO_R_UNSUPPORTED);
+               DSOerr(DSO_F_WIN32_PATHBYADDR,DSO_R_UNSUPPORTED);
                return -1;
                }
 
@@ -656,7 +706,8 @@ static int win32_pathbyaddr(void *addr,char *path,int sz)
                GetProcAddress(dll,"CreateToolhelp32Snapshot");
        if (create_snap == NULL)
                {
-               DSOerr(DSO_F_PATHBYADDR,DSO_R_UNSUPPORTED);
+               FreeLibrary(dll);
+               DSOerr(DSO_F_WIN32_PATHBYADDR,DSO_R_UNSUPPORTED);
                return -1;
                }
        /* We take the rest for granted... */
@@ -673,7 +724,7 @@ static int win32_pathbyaddr(void *addr,char *path,int sz)
        if( hModuleSnap == INVALID_HANDLE_VALUE ) 
                { 
                FreeLibrary(dll);
-               DSOerr(DSO_F_PATHBYADDR,DSO_R_UNSUPPORTED);
+               DSOerr(DSO_F_WIN32_PATHBYADDR,DSO_R_UNSUPPORTED);
                return -1;
                } 
  
@@ -683,7 +734,7 @@ static int win32_pathbyaddr(void *addr,char *path,int sz)
                { 
                (*close_snap)(hModuleSnap);
                FreeLibrary(dll);
-               DSOerr(DSO_F_PATHBYADDR,DSO_R_FAILURE);
+               DSOerr(DSO_F_WIN32_PATHBYADDR,DSO_R_FAILURE);
                return -1;
                }
  
@@ -721,4 +772,70 @@ static int win32_pathbyaddr(void *addr,char *path,int sz)
        FreeLibrary(dll);
        return 0;
        }
+
+static void *win32_globallookup(const char *name)
+       {
+       HMODULE dll;
+       HANDLE hModuleSnap = INVALID_HANDLE_VALUE;
+       MODULEENTRY32 me32;
+       CREATETOOLHELP32SNAPSHOT create_snap;
+       CLOSETOOLHELP32SNAPSHOT  close_snap;
+       MODULE32 module_first, module_next;
+       FARPROC ret=NULL;
+
+       dll = LoadLibrary(TEXT(DLLNAME));
+       if (dll == NULL)
+               {
+               DSOerr(DSO_F_WIN32_GLOBALLOOKUP,DSO_R_UNSUPPORTED);
+               return NULL;
+               }
+
+       create_snap = (CREATETOOLHELP32SNAPSHOT)
+               GetProcAddress(dll,"CreateToolhelp32Snapshot");
+       if (create_snap == NULL)
+               {
+               FreeLibrary(dll);
+               DSOerr(DSO_F_WIN32_GLOBALLOOKUP,DSO_R_UNSUPPORTED);
+               return NULL;
+               }
+       /* We take the rest for granted... */
+#ifdef _WIN32_WCE
+       close_snap = (CLOSETOOLHELP32SNAPSHOT)
+               GetProcAddress(dll,"CloseToolhelp32Snapshot");
+#else
+       close_snap = (CLOSETOOLHELP32SNAPSHOT)CloseHandle;
+#endif
+       module_first = (MODULE32)GetProcAddress(dll,"Module32First");
+       module_next  = (MODULE32)GetProcAddress(dll,"Module32Next");
+
+       hModuleSnap = (*create_snap)(TH32CS_SNAPMODULE,0);
+       if( hModuleSnap == INVALID_HANDLE_VALUE )
+               {
+               FreeLibrary(dll);
+               DSOerr(DSO_F_WIN32_GLOBALLOOKUP,DSO_R_UNSUPPORTED);
+               return NULL;
+               }
+
+       me32.dwSize = sizeof(me32);
+
+       if (!(*module_first)(hModuleSnap,&me32))
+               {
+               (*close_snap)(hModuleSnap);
+               FreeLibrary(dll);
+               return NULL;
+               }
+
+       do      {
+               if ((ret = GetProcAddress(me32.hModule,name)))
+                       {
+                       (*close_snap)(hModuleSnap);
+                       FreeLibrary(dll);
+                       return ret;
+                       }
+               } while((*module_next)(hModuleSnap,&me32));
+
+       (*close_snap)(hModuleSnap); 
+       FreeLibrary(dll);
+       return NULL;
+       }
 #endif /* DSO_WIN32 */