Add Traditional Chinese Big5 and Simplified Chinese GBK mappings.
[wine] / dlls / kernel / vxd.c
1 /*
2  * Win32 VxD functions
3  *
4  * Copyright 1998 Marcus Meissner
5  * Copyright 1998 Ulrich Weigand
6  * Copyright 1998 Patrik Stridvall
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include "config.h"
24 #include "wine/port.h"
25
26 #include <stdlib.h>
27 #ifdef HAVE_UNISTD_H
28 # include <unistd.h>
29 #endif
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <string.h>
33 #include <stdarg.h>
34
35 #include "windef.h"
36 #include "winbase.h"
37 #include "winreg.h"
38 #include "winerror.h"
39 #include "file.h"
40 #include "kernel_private.h"
41 #include "wine/library.h"
42 #include "wine/unicode.h"
43 #include "wine/server.h"
44 #include "wine/debug.h"
45
46 WINE_DEFAULT_DEBUG_CHANNEL(vxd);
47
48 typedef BOOL (WINAPI *DeviceIoProc)(DWORD, LPVOID, DWORD, LPVOID, DWORD, LPDWORD, LPOVERLAPPED);
49 typedef DWORD (WINAPI *VxDCallProc)(DWORD, CONTEXT86 *);
50
51 struct vxd_module
52 {
53     dev_t        dev;
54     ino_t        ino;
55     HANDLE       handle;
56     HMODULE      module;
57     DeviceIoProc proc;
58 };
59
60 struct vxdcall_service
61 {
62     WCHAR       name[12];
63     DWORD       service;
64     HMODULE     module;
65     VxDCallProc proc;
66 };
67
68 #define MAX_VXD_MODULES 32
69
70 static struct vxd_module vxd_modules[MAX_VXD_MODULES];
71
72 static struct vxdcall_service vxd_services[] =
73 {
74     { {'v','m','m','.','v','x','d',0},             0x0001, NULL, NULL },
75     { {'v','w','i','n','3','2','.','v','x','d',0}, 0x002a, NULL, NULL }
76 };
77
78 #define NB_VXD_SERVICES  (sizeof(vxd_services)/sizeof(vxd_services[0]))
79
80 static CRITICAL_SECTION vxd_section;
81 static CRITICAL_SECTION_DEBUG critsect_debug =
82 {
83     0, 0, &vxd_section,
84     { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
85       0, 0, { 0, (DWORD)(__FILE__ ": vxd_section") }
86 };
87 static CRITICAL_SECTION vxd_section = { &critsect_debug, -1, 0, 0, 0, 0 };
88
89
90 /* create a file handle to represent a VxD, by opening a dummy file in the wineserver directory */
91 static HANDLE open_vxd_handle( LPCWSTR name )
92 {
93     const char *dir = wine_get_server_dir();
94     char *unix_name;
95     int len1, len2;
96     HANDLE ret;
97
98     len1 = strlen( dir );
99     len2 = WideCharToMultiByte( CP_UNIXCP, 0, name, -1, NULL, 0, NULL, NULL);
100     if (!(unix_name = HeapAlloc( GetProcessHeap(), 0, len1 + len2 + 1 )))
101     {
102         SetLastError( ERROR_NOT_ENOUGH_MEMORY );
103         return 0;
104     }
105     strcpy( unix_name, dir );
106     unix_name[len1] = '/';
107     WideCharToMultiByte( CP_UNIXCP, 0, name, -1, unix_name + len1 + 1, len2, NULL, NULL);
108     ret = FILE_CreateFile( unix_name, 0, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
109                            OPEN_ALWAYS, 0, 0, TRUE, DRIVE_FIXED );
110     HeapFree( GetProcessHeap(), 0, unix_name );
111     return ret;
112 }
113
114 /* retrieve the DeviceIoControl function for a Vxd given a file handle */
115 static DeviceIoProc get_vxd_proc( HANDLE handle )
116 {
117     struct stat st;
118     DeviceIoProc ret = NULL;
119     int status, i, fd;
120
121     status = wine_server_handle_to_fd( handle, 0, &fd, NULL, NULL );
122     if (status)
123     {
124         SetLastError( RtlNtStatusToDosError(status) );
125         return NULL;
126     }
127     if (fstat( fd, &st ) == -1)
128     {
129         wine_server_release_fd( handle, fd );
130         SetLastError( ERROR_INVALID_HANDLE );
131         return NULL;
132     }
133     wine_server_release_fd( handle, fd );
134
135     RtlEnterCriticalSection( &vxd_section );
136
137     for (i = 0; i < MAX_VXD_MODULES; i++)
138     {
139         if (!vxd_modules[i].module) break;
140         if (vxd_modules[i].dev == st.st_dev && vxd_modules[i].ino == st.st_ino)
141         {
142             if (!(ret = vxd_modules[i].proc)) SetLastError( ERROR_INVALID_FUNCTION );
143             goto done;
144         }
145     }
146     /* FIXME: Here we could go through the directory to find the VxD name and load it. */
147     /* Let's wait to find out if there are actually apps out there that try to share   */
148     /* VxD handles between processes, before we go to the trouble of implementing it.  */
149     ERR( "handle %p not found in module list, inherited from another process?\n", handle );
150
151 done:
152     RtlLeaveCriticalSection( &vxd_section );
153     return ret;
154 }
155
156
157 /* load a VxD and return a file handle to it */
158 HANDLE VXD_Open( LPCWSTR filenameW, DWORD access, SECURITY_ATTRIBUTES *sa )
159 {
160     static const WCHAR dotVxDW[] = {'.','v','x','d',0};
161     int i;
162     HANDLE handle;
163     HMODULE module;
164     WCHAR *p, name[16];
165
166     if (!(GetVersion() & 0x80000000))  /* there are no VxDs on NT */
167     {
168         SetLastError( ERROR_INVALID_PARAMETER );
169         return 0;
170     }
171
172     /* normalize the filename */
173
174     if (strlenW( filenameW ) >= sizeof(name)/sizeof(WCHAR) - 4 ||
175         strchrW( filenameW, '/' ) || strchrW( filenameW, '\\' ))
176     {
177         SetLastError( ERROR_FILE_NOT_FOUND );
178         return 0;
179     }
180     strcpyW( name, filenameW );
181     strlwrW( name );
182     p = strchrW( name, '.' );
183     if (!p) strcatW( name, dotVxDW );
184     else if (strcmpW( p, dotVxDW ))  /* existing extension has to be .vxd */
185     {
186         SetLastError( ERROR_FILE_NOT_FOUND );
187         return 0;
188     }
189
190     /* try to load the module first */
191
192     if (!(module = LoadLibraryW( name )))
193     {
194         FIXME( "Unknown/unsupported VxD %s. Try setting Windows version to 'nt40' or 'win31'.\n",
195                debugstr_w(name) );
196         SetLastError( ERROR_FILE_NOT_FOUND );
197         return 0;
198     }
199
200     /* register the module in the global list if necessary */
201
202     RtlEnterCriticalSection( &vxd_section );
203
204     for (i = 0; i < MAX_VXD_MODULES; i++)
205     {
206         if (vxd_modules[i].module == module)
207         {
208             handle = vxd_modules[i].handle;
209             goto done;  /* already registered */
210         }
211         if (!vxd_modules[i].module)  /* new one, register it */
212         {
213             struct stat st;
214             int fd;
215
216             /* get a file handle to the dummy file */
217             if (!(handle = open_vxd_handle( name )))
218             {
219                 FreeLibrary( module );
220                 goto done;
221             }
222             wine_server_handle_to_fd( handle, 0, &fd, NULL, NULL );
223             if (fstat( fd, &st ) != -1)
224             {
225                 vxd_modules[i].dev = st.st_dev;
226                 vxd_modules[i].ino = st.st_ino;
227             }
228             vxd_modules[i].module = module;
229             vxd_modules[i].handle = handle;
230             vxd_modules[i].proc = (DeviceIoProc)GetProcAddress( module, "DeviceIoControl" );
231             wine_server_release_fd( handle, fd );
232             goto done;
233         }
234     }
235
236     ERR("too many open VxD modules, please report\n" );
237     CloseHandle( handle );
238     FreeLibrary( module );
239     handle = 0;
240
241 done:
242     RtlLeaveCriticalSection( &vxd_section );
243     if (!DuplicateHandle( GetCurrentProcess(), handle, GetCurrentProcess(), &handle, 0,
244                           (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle),
245                           DUP_HANDLE_SAME_ACCESS ))
246         handle = 0;
247     return handle;
248 }
249
250
251 /***********************************************************************
252  *              VxDCall0 (KERNEL32.1)
253  *              VxDCall1 (KERNEL32.2)
254  *              VxDCall2 (KERNEL32.3)
255  *              VxDCall3 (KERNEL32.4)
256  *              VxDCall4 (KERNEL32.5)
257  *              VxDCall5 (KERNEL32.6)
258  *              VxDCall6 (KERNEL32.7)
259  *              VxDCall7 (KERNEL32.8)
260  *              VxDCall8 (KERNEL32.9)
261  */
262 void VxDCall( DWORD service, CONTEXT86 *context )
263 {
264     int i;
265     VxDCallProc proc = NULL;
266
267     RtlEnterCriticalSection( &vxd_section );
268     for (i = 0; i < NB_VXD_SERVICES; i++)
269     {
270         if (HIWORD(service) != vxd_services[i].service) continue;
271         if (!vxd_services[i].module)  /* need to load it */
272         {
273             if ((vxd_services[i].module = LoadLibraryW( vxd_services[i].name )))
274                 vxd_services[i].proc = (VxDCallProc)GetProcAddress( vxd_services[i].module, "VxDCall" );
275         }
276         proc = vxd_services[i].proc;
277         break;
278     }
279     RtlLeaveCriticalSection( &vxd_section );
280
281     if (proc) context->Eax = proc( service, context );
282     else
283     {
284         FIXME( "Unknown/unimplemented VxD (%08lx)\n", service);
285         context->Eax = 0xffffffff; /* FIXME */
286     }
287 }
288
289
290 /***********************************************************************
291  *              OpenVxDHandle (KERNEL32.@)
292  *
293  *      This function is supposed to return the corresponding Ring 0
294  *      ("kernel") handle for a Ring 3 handle in Win9x.
295  *      Evidently, Wine will have problems with this. But we try anyway,
296  *      maybe it helps...
297  */
298 HANDLE WINAPI OpenVxDHandle(HANDLE hHandleRing3)
299 {
300     FIXME( "(%p), stub! (returning Ring 3 handle instead of Ring 0)\n", hHandleRing3);
301     return hHandleRing3;
302 }
303
304
305 /****************************************************************************
306  *              DeviceIoControl (KERNEL32.@)
307  * This is one of those big ugly nasty procedure which can do
308  * a million and one things when it comes to devices. It can also be
309  * used for VxD communication.
310  *
311  * A return value of FALSE indicates that something has gone wrong which
312  * GetLastError can decipher.
313  */
314 BOOL WINAPI DeviceIoControl(HANDLE hDevice, DWORD dwIoControlCode,
315                             LPVOID lpvInBuffer, DWORD cbInBuffer,
316                             LPVOID lpvOutBuffer, DWORD cbOutBuffer,
317                             LPDWORD lpcbBytesReturned,
318                             LPOVERLAPPED lpOverlapped)
319 {
320     NTSTATUS status;
321
322     TRACE( "(%p,%lx,%p,%ld,%p,%ld,%p,%p)\n",
323            hDevice,dwIoControlCode,lpvInBuffer,cbInBuffer,
324            lpvOutBuffer,cbOutBuffer,lpcbBytesReturned,lpOverlapped );
325
326     /* Check if this is a user defined control code for a VxD */
327
328     if( HIWORD( dwIoControlCode ) == 0 )
329     {
330         DeviceIoProc proc = get_vxd_proc( hDevice );
331         if (proc) return proc( dwIoControlCode, lpvInBuffer, cbInBuffer,
332                                lpvOutBuffer, cbOutBuffer, lpcbBytesReturned, lpOverlapped );
333         return FALSE;
334     }
335
336     /* Not a VxD, let ntdll handle it */
337
338     if (lpOverlapped)
339     {
340         status = NtDeviceIoControlFile(hDevice, lpOverlapped->hEvent,
341                                        NULL, NULL, (PIO_STATUS_BLOCK)lpOverlapped,
342                                        dwIoControlCode, lpvInBuffer, cbInBuffer,
343                                        lpvOutBuffer, cbOutBuffer);
344         if (lpcbBytesReturned) *lpcbBytesReturned = lpOverlapped->InternalHigh;
345     }
346     else
347     {
348         IO_STATUS_BLOCK iosb;
349
350         status = NtDeviceIoControlFile(hDevice, NULL, NULL, NULL, &iosb,
351                                        dwIoControlCode, lpvInBuffer, cbInBuffer,
352                                        lpvOutBuffer, cbOutBuffer);
353         if (lpcbBytesReturned) *lpcbBytesReturned = iosb.Information;
354     }
355     if (status) SetLastError( RtlNtStatusToDosError(status) );
356     return !status;
357 }