RT3548: Remove some obsolete platforms
[openssl.git] / crypto / dso / dso_win32.c
index 96ccd4b7ee63ffdda36be8d9a79dcc370cdf3dbf..1cab3142c6167a3478ee7f8aba6b59b11bcc5388 100644 (file)
@@ -68,6 +68,49 @@ DSO_METHOD *DSO_METHOD_win32(void)
        }
 #else
 
+#ifdef _WIN32_WCE
+# if _WIN32_WCE < 300
+static FARPROC GetProcAddressA(HMODULE hModule,LPCSTR lpProcName)
+       {
+       WCHAR lpProcNameW[64];
+       int i;
+
+       for (i=0;lpProcName[i] && i<64;i++)
+               lpProcNameW[i] = (WCHAR)lpProcName[i];
+       if (i==64) return NULL;
+       lpProcNameW[i] = 0;
+
+       return GetProcAddressW(hModule,lpProcNameW);
+       }
+# 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" ... */
 #define DSO_MAX_TRANSLATED_SIZE 256
 
@@ -86,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);
 
@@ -105,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)
@@ -143,7 +188,7 @@ static int win32_load(DSO *dso)
                goto err;
                }
        *p = h;
-       if(!sk_push(dso->meth_data, (char *)p))
+       if(!sk_void_push(dso->meth_data, p))
                {
                DSOerr(DSO_F_WIN32_LOAD,DSO_R_STACK_ERROR);
                goto err;
@@ -170,9 +215,9 @@ static int win32_unload(DSO *dso)
                DSOerr(DSO_F_WIN32_UNLOAD,ERR_R_PASSED_NULL_PARAMETER);
                return(0);
                }
-       if(sk_num(dso->meth_data) < 1)
+       if(sk_void_num(dso->meth_data) < 1)
                return(1);
-       p = (HINSTANCE *)sk_pop(dso->meth_data);
+       p = sk_void_pop(dso->meth_data);
        if(p == NULL)
                {
                DSOerr(DSO_F_WIN32_UNLOAD,DSO_R_NULL_HANDLE);
@@ -183,7 +228,7 @@ static int win32_unload(DSO *dso)
                DSOerr(DSO_F_WIN32_UNLOAD,DSO_R_UNLOAD_FAILED);
                /* We should push the value back onto the stack in
                 * case of a retry. */
-               sk_push(dso->meth_data, (char *)p);
+               sk_void_push(dso->meth_data, p);
                return(0);
                }
        /* Cleanup */
@@ -203,12 +248,12 @@ static void *win32_bind_var(DSO *dso, const char *symname)
                DSOerr(DSO_F_WIN32_BIND_VAR,ERR_R_PASSED_NULL_PARAMETER);
                return(NULL);
                }
-       if(sk_num(dso->meth_data) < 1)
+       if(sk_void_num(dso->meth_data) < 1)
                {
                DSOerr(DSO_F_WIN32_BIND_VAR,DSO_R_STACK_ERROR);
                return(NULL);
                }
-       ptr = (HINSTANCE *)sk_value(dso->meth_data, sk_num(dso->meth_data) - 1);
+       ptr = sk_void_value(dso->meth_data, sk_void_num(dso->meth_data) - 1);
        if(ptr == NULL)
                {
                DSOerr(DSO_F_WIN32_BIND_VAR,DSO_R_NULL_HANDLE);
@@ -234,12 +279,12 @@ static DSO_FUNC_TYPE win32_bind_func(DSO *dso, const char *symname)
                DSOerr(DSO_F_WIN32_BIND_FUNC,ERR_R_PASSED_NULL_PARAMETER);
                return(NULL);
                }
-       if(sk_num(dso->meth_data) < 1)
+       if(sk_void_num(dso->meth_data) < 1)
                {
                DSOerr(DSO_F_WIN32_BIND_FUNC,DSO_R_STACK_ERROR);
                return(NULL);
                }
-       ptr = (HINSTANCE *)sk_value(dso->meth_data, sk_num(dso->meth_data) - 1);
+       ptr = sk_void_value(dso->meth_data, sk_void_num(dso->meth_data) - 1);
        if(ptr == NULL)
                {
                DSOerr(DSO_F_WIN32_BIND_FUNC,DSO_R_NULL_HANDLE);
@@ -270,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)
                {
@@ -289,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;
@@ -300,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)
@@ -308,10 +355,11 @@ static struct file_st *win32_splitter(DSO *dso, const char *filename,
                                DSOerr(DSO_F_WIN32_SPLITTER,
                                        DSO_R_INCORRECT_FILE_SYNTAX);
                                /*goto err;*/
+                               OPENSSL_free(result);
                                return(NULL);
                                }
                        result->device = start;
-                       result->devicelen = filename - start;
+                       result->devicelen = (int)(filename - start);
                        position = IN_FILE;
                        start = ++filename;
                        result->dir = start;
@@ -320,21 +368,30 @@ static struct file_st *win32_splitter(DSO *dso, const char *filename,
                case '/':
                        if(position == IN_NODE)
                                {
-                               result->nodelen = filename - start;
+                               result->nodelen = (int)(filename - start);
                                position = IN_FILE;
                                start = ++filename;
                                result->dir = start;
                                }
+                       else if(position == IN_DEVICE)
+                               {
+                               position = IN_FILE;
+                               filename++;
+                               result->dir = start;
+                               result->dirlen = (int)(filename - start);
+                               start = filename;
+                               }
                        else
                                {
                                filename++;
-                               result->dirlen += filename - start;
+                               result->dirlen += (int)(filename - start);
+                               start = filename;
                                }
                        break;
                case '\0':
                        if(position == IN_NODE)
                                {
-                               result->nodelen = filename - start;
+                               result->nodelen = (int)(filename - start);
                                }
                        else
                                {
@@ -342,12 +399,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 +=
+                                                       (int)(filename - start);
                                                }
                                        else
                                                {
                                                result->file = start;
-                                               result->filelen = filename - start;
+                                               result->filelen =
+                                                       (int)(filename - start);
                                                }
                                        }
                                }
@@ -357,7 +421,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;
@@ -441,14 +505,17 @@ static char *win32_joiner(DSO *dso, const struct file_st *file_split)
                                + file_split->predirlen
                                - (start - file_split->predir);
                strncpy(&result[offset], start,
-                       end - start); offset += end - start;
+                       end - start); offset += (int)(end - start);
                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))
                {
@@ -459,14 +526,17 @@ static char *win32_joiner(DSO *dso, const struct file_st *file_split)
                                + file_split->dirlen
                                - (start - file_split->dir);
                strncpy(&result[offset], start,
-                       end - start); offset += end - start;
+                       end - start); offset += (int)(end - start);
                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';
@@ -509,15 +579,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);
@@ -552,6 +622,8 @@ static char *win32_merger(DSO *dso, const char *filespec1, const char *filespec2
 
                merged = win32_joiner(dso, filespec1_split);
                }
+       OPENSSL_free(filespec1_split);
+       OPENSSL_free(filespec2_split);
        return(merged);
        }
 
@@ -597,22 +669,6 @@ static const char *openssl_strnchr(const char *string, int c, size_t len)
 
 #include <tlhelp32.h>
 #ifdef _WIN32_WCE
-# if _WIN32_WCE < 300
-static FARPROC GetProcAddressA(HMODULE hModule,LPCSTR lpProcName)
-       {
-       WCHAR lpProcNameW[64];
-       int i;
-
-       for (i=0;lpProcName[i] && i<64;i++)
-               lpProcNameW[i] = (WCHAR)lpProcName[i];
-       if (i==64) return NULL;
-       lpProcNameW[i] = 0;
-
-       return GetProcAddressW(hModule,lpProcNameW);
-       }
-# endif
-# undef GetProcAddress
-# define GetProcAddress GetProcAddressA
 # define DLLNAME "TOOLHELP.DLL"
 #else
 # ifdef MODULEENTRY32
@@ -633,14 +689,18 @@ static int win32_pathbyaddr(void *addr,char *path,int sz)
        CREATETOOLHELP32SNAPSHOT create_snap;
        CLOSETOOLHELP32SNAPSHOT  close_snap;
        MODULE32 module_first, module_next;
-       int len;
  
-       if (addr == NULL) addr = win32_pathbyaddr;
+       if (addr == NULL)
+               {
+               union   { int(*f)(void*,char*,int); void *p; } t =
+                       { win32_pathbyaddr };
+               addr = t.p;
+               }
 
        dll = LoadLibrary(TEXT(DLLNAME));
        if (dll == NULL)
                {
-               DSOerr(DSO_F_PATHBYADDR,DSO_R_UNSUPPORTED);
+               DSOerr(DSO_F_WIN32_PATHBYADDR,DSO_R_UNSUPPORTED);
                return -1;
                }
 
@@ -648,7 +708,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... */
@@ -665,7 +726,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;
                } 
  
@@ -675,7 +736,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;
                }
  
@@ -690,21 +751,25 @@ static int win32_pathbyaddr(void *addr,char *path,int sz)
                        return WideCharToMultiByte(CP_ACP,0,me32.szExePath,-1,
                                                        path,sz,NULL,NULL);
 # else
-                       len = (int)wcslen(me32.szExePath);
+                       {
+                       int i,len = (int)wcslen(me32.szExePath);
                        if (sz <= 0) return len+1;
                        if (len >= sz) len=sz-1;
                        for(i=0;i<len;i++)
                                path[i] = (char)me32.szExePath[i];
                        path[len++] = 0;
                        return len;
+                       }
 # endif
 #else
-                       len = (int)strlen(me32.szExePath);
+                       {
+                       int len = (int)strlen(me32.szExePath);
                        if (sz <= 0) return len+1;
                        if (len >= sz) len=sz-1;
                        memcpy(path,me32.szExePath,len);
                        path[len++] = 0;
                        return len;
+                       }
 #endif
                        } 
                } while((*module_next)(hModuleSnap, &me32)); 
@@ -713,4 +778,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 */