make update
[openssl.git] / crypto / dso / dso_win32.c
index b44f25c5799360d3307f0a151417c4ad87010f6e..6fb6c54181c1cabb3663810aea3a47f6ccdf4a0f 100644 (file)
@@ -129,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);
 
@@ -148,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)
@@ -186,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;
@@ -213,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);
@@ -226,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 */
@@ -246,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);
@@ -277,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);
@@ -353,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;
@@ -365,7 +368,7 @@ 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;
@@ -375,20 +378,20 @@ static struct file_st *win32_splitter(DSO *dso, const char *filename,
                                position = IN_FILE;
                                filename++;
                                result->dir = start;
-                               result->dirlen = filename - 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
                                {
@@ -402,13 +405,13 @@ static struct file_st *win32_splitter(DSO *dso, const char *filename,
                                                        result->dirlen = 0;
                                                        }
                                                result->dirlen +=
-                                                       filename - start;
+                                                       (int)(filename - start);
                                                }
                                        else
                                                {
                                                result->file = start;
                                                result->filelen =
-                                                       filename - start;
+                                                       (int)(filename - start);
                                                }
                                        }
                                }
@@ -502,7 +505,7 @@ 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;
                }
@@ -523,7 +526,7 @@ 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;
                }
@@ -576,14 +579,14 @@ 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);
+               filespec2_split = win32_splitter(dso, filespec2, 1);
                if (!filespec2_split)
                        {
                        DSOerr(DSO_F_WIN32_MERGER,
@@ -619,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);
        }
 
@@ -696,7 +701,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;
                }
 
@@ -705,7 +710,7 @@ static int win32_pathbyaddr(void *addr,char *path,int sz)
        if (create_snap == NULL)
                {
                FreeLibrary(dll);
-               DSOerr(DSO_F_PATHBYADDR,DSO_R_UNSUPPORTED);
+               DSOerr(DSO_F_WIN32_PATHBYADDR,DSO_R_UNSUPPORTED);
                return -1;
                }
        /* We take the rest for granted... */
@@ -722,7 +727,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;
                } 
  
@@ -732,7 +737,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;
                }
  
@@ -770,4 +775,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 */