Fix the case of product and company names.
[wine] / relay32 / builtin32.c
1 /*
2  * Win32 builtin functions
3  *
4  * Copyright 1997 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <assert.h>
25 #include <string.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <ctype.h>
29 #include <sys/types.h>
30 #ifdef HAVE_SYS_MMAN_H
31 #include <sys/mman.h>
32 #endif
33
34 #include "ntstatus.h"
35 #include "windef.h"
36 #include "winbase.h"
37 #include "wine/winbase16.h"
38 #include "wine/library.h"
39 #include "module.h"
40 #include "file.h"
41 #include "ntdll_misc.h"
42 #include "wine/server.h"
43 #include "wine/debug.h"
44
45 WINE_DEFAULT_DEBUG_CHANNEL(module);
46 WINE_DECLARE_DEBUG_CHANNEL(relay);
47
48 static HMODULE main_module;
49 static NTSTATUS last_status; /* use to gather all errors in callback */
50
51
52 /***********************************************************************
53  *           load_library
54  *
55  * Load a library in memory; callback function for wine_dll_register
56  */
57 static void load_library( void *base, const char *filename )
58 {
59     UNICODE_STRING      wstr;
60     HMODULE module = (HMODULE)base, ret;
61     IMAGE_NT_HEADERS *nt;
62     WINE_MODREF *wm;
63     char *fullname;
64     DWORD len;
65
66     if (!base)
67     {
68         ERR("could not map image for %s\n", filename ? filename : "main exe" );
69         return;
70     }
71     if (!(nt = RtlImageNtHeader( module )))
72     {
73         ERR( "bad module for %s\n", filename ? filename : "main exe" );
74         last_status = STATUS_INVALID_IMAGE_FORMAT;
75         return;
76     }
77
78     if (!(nt->FileHeader.Characteristics & IMAGE_FILE_DLL))
79     {
80         /* if we already have an executable, ignore this one */
81         if (!main_module) main_module = module;
82         return; /* don't create the modref here, will be done later on */
83     }
84
85     RtlCreateUnicodeStringFromAsciiz(&wstr, filename);
86     if (LdrGetDllHandle(0, 0, &wstr, &ret) == STATUS_SUCCESS)
87         MESSAGE( "Warning: loading builtin %s, but native version already present. "
88                  "Expect trouble.\n", filename );
89     RtlFreeUnicodeString( &wstr );
90
91     len = GetSystemDirectoryA( NULL, 0 );
92     if (!(fullname = RtlAllocateHeap( ntdll_get_process_heap(), 0, len + strlen(filename) + 1 )))
93     {
94         ERR( "can't load %s\n", filename );
95         last_status = STATUS_NO_MEMORY;
96         return;
97     }
98     GetSystemDirectoryA( fullname, len );
99     strcat( fullname, "\\" );
100     strcat( fullname, filename );
101
102     /* Create 32-bit MODREF */
103     if (!(wm = PE_CreateModule( module, fullname, 0, 0, TRUE )))
104     {
105         ERR( "can't load %s\n", filename );
106         RtlFreeHeap( ntdll_get_process_heap(), 0, fullname );
107         last_status = STATUS_NO_MEMORY;
108         return;
109     }
110     TRACE( "loaded %s %p %p\n", fullname, wm, module );
111     RtlFreeHeap( ntdll_get_process_heap(), 0, fullname );
112
113     /* setup relay debugging entry points */
114     if (TRACE_ON(relay)) RELAY_SetupDLL( (void *)module );
115 }
116
117
118 /***********************************************************************
119  *           BUILTIN32_LoadLibraryExA
120  *
121  * Partly copied from the original PE_ version.
122  *
123  */
124 NTSTATUS BUILTIN32_LoadLibraryExA(LPCSTR path, DWORD flags, WINE_MODREF** pwm)
125 {
126     char error[256], dllname[20], *p;
127     int file_exists;
128     LPCSTR name;
129     void *handle;
130
131     /* Fix the name in case we have a full path and extension */
132     name = path;
133     if ((p = strrchr( name, '\\' ))) name = p + 1;
134     if ((p = strrchr( name, '/' ))) name = p + 1;
135
136     if (strlen(name) >= sizeof(dllname)-4) return STATUS_NO_SUCH_FILE;
137
138     strcpy( dllname, name );
139     p = strrchr( dllname, '.' );
140     if (!p) strcat( dllname, ".dll" );
141     for (p = dllname; *p; p++) *p = FILE_tolower(*p);
142
143     last_status = STATUS_SUCCESS;
144     /* load_library will modify last_status. Note also that load_library can be
145      * called several times, if the .so file we're loading has dependencies.
146      * last_status will gather all the errors we may get while loading all these
147      * libraries
148      */
149     if (!(handle = wine_dll_load( dllname, error, sizeof(error), &file_exists )))
150     {
151         if (!file_exists)
152         {
153             /* The file does not exist -> WARN() */
154             WARN("cannot open .so lib for builtin %s: %s\n", name, error);
155             return STATUS_NO_SUCH_FILE;
156         }
157         /* ERR() for all other errors (missing functions, ...) */
158         ERR("failed to load .so lib for builtin %s: %s\n", name, error );
159         return STATUS_PROCEDURE_NOT_FOUND;
160     }
161     if (last_status != STATUS_SUCCESS) return last_status;
162
163     if (!((*pwm) = MODULE_FindModule( path ))) *pwm = MODULE_FindModule( dllname );
164     if (!*pwm)
165     {
166         ERR( "loaded .so but dll %s still not found - 16-bit dll or version conflict.\n", dllname );
167         /* wine_dll_unload( handle );*/
168         return STATUS_INVALID_IMAGE_FORMAT;
169     }
170     (*pwm)->dlhandle = handle;
171     return STATUS_SUCCESS;
172 }
173
174 /***********************************************************************
175  *           BUILTIN32_Init
176  *
177  * Initialize loading callbacks and return HMODULE of main exe.
178  * 'main' is the main exe in case it was already loaded from a PE file.
179  */
180 HMODULE BUILTIN32_LoadExeModule( HMODULE main )
181 {
182     main_module = main;
183     last_status = STATUS_SUCCESS;
184     wine_dll_set_callback( load_library );
185     if (!main_module)
186         MESSAGE( "No built-in EXE module loaded!  Did you create a .spec file?\n" );
187     if (last_status != STATUS_SUCCESS)
188         MESSAGE( "Error while processing initial modules\n");
189
190     return main_module;
191 }