GetModuleFileName[AW] doesn't terminate the string if the buffer is
[wine] / dlls / kernel / version.c
1 /*
2  * Windows and DOS version functions
3  *
4  * Copyright 1997 Marcus Meissner
5  * Copyright 1998 Patrik Stridvall
6  * Copyright 1998, 2003 Andreas Mohr
7  * Copyright 1997, 2003 Alexandre Julliard
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24 #include "config.h"
25 #include "wine/port.h"
26
27 #include <string.h>
28 #include <stdlib.h>
29 #include <stdarg.h>
30 #include <stdio.h>
31 #include "ntstatus.h"
32 #include "windef.h"
33 #include "winbase.h"
34 #include "wingdi.h"
35 #include "winuser.h"
36 #include "winreg.h"
37 #include "winternl.h"
38 #include "winerror.h"
39 #include "wine/winbase16.h"
40 #include "wine/unicode.h"
41 #include "wine/debug.h"
42
43 WINE_DEFAULT_DEBUG_CHANNEL(ver);
44
45 /**********************************************************************
46  *         parse_dos_version
47  *
48  * Parse the contents of the Version key.
49  */
50 static WORD parse_dos_version( HKEY hkey )
51 {
52     static const WCHAR DosW[] = {'D','O','S',0};
53
54     UNICODE_STRING valueW;
55     int hi, lo;
56     char tmp[64], buffer[50];
57     KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)tmp;
58     DWORD count, len;
59     WORD ret = 0;
60
61     RtlInitUnicodeString( &valueW, DosW );
62     if (!NtQueryValueKey( hkey, &valueW, KeyValuePartialInformation, tmp, sizeof(tmp), &count ))
63     {
64         RtlUnicodeToMultiByteN( buffer, sizeof(buffer)-1, &len,
65                                 (WCHAR *)info->Data, info->DataLength );
66         buffer[len] = 0;
67
68         if (sscanf( buffer, "%d.%d", &hi, &lo ) == 2) ret = MAKEWORD( lo, hi );
69         else MESSAGE("Wrong format for DOS version in config file. Use \"x.xx\"\n");
70     }
71     return ret;
72 }
73
74
75 /**********************************************************************
76  *         get_dos_version
77  */
78 WORD get_dos_version(void)
79 {
80     OBJECT_ATTRIBUTES attr;
81     UNICODE_STRING nameW;
82     HKEY hkey, config_key;
83     WCHAR buffer[MAX_PATH];
84     WORD ret = 0;
85     DWORD len;
86
87     static const WCHAR configW[] = {'M','a','c','h','i','n','e','\\',
88                                     'S','o','f','t','w','a','r','e','\\',
89                                     'W','i','n','e','\\',
90                                     'W','i','n','e','\\',
91                                     'C','o','n','f','i','g',0};
92     static const WCHAR appdefaultsW[] = {'A','p','p','D','e','f','a','u','l','t','s','\\',0};
93     static const WCHAR versionW[] = {'\\','V','e','r','s','i','o','n',0};
94
95     attr.Length = sizeof(attr);
96     attr.RootDirectory = 0;
97     attr.ObjectName = &nameW;
98     attr.Attributes = 0;
99     attr.SecurityDescriptor = NULL;
100     attr.SecurityQualityOfService = NULL;
101     RtlInitUnicodeString( &nameW, configW );
102
103     if (NtOpenKey( &config_key, KEY_ALL_ACCESS, &attr )) return 0;
104     attr.RootDirectory = config_key;
105
106     /* open AppDefaults\\appname\\Version key */
107     len = GetModuleFileNameW( 0, buffer, sizeof(buffer)/sizeof(WCHAR) );
108     if (len && len < sizeof(buffer)/sizeof(WCHAR))
109     {
110         WCHAR *p, *appname, appversion[MAX_PATH+20];
111
112         appname = buffer;
113         if ((p = strrchrW( appname, '/' ))) appname = p + 1;
114         if ((p = strrchrW( appname, '\\' ))) appname = p + 1;
115
116         strcpyW( appversion, appdefaultsW );
117         strcatW( appversion, appname );
118         strcatW( appversion, versionW );
119
120         TRACE( "getting version from %s\n", debugstr_w(appversion) );
121         RtlInitUnicodeString( &nameW, appversion );
122
123         if (!NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr ))
124         {
125             ret = parse_dos_version( hkey );
126             NtClose( hkey );
127         }
128     }
129
130     if (!ret)
131     {
132         TRACE( "getting default version\n" );
133         RtlInitUnicodeString( &nameW, versionW + 1 );
134         if (!NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr ))
135         {
136             ret = parse_dos_version( hkey );
137             NtClose( hkey );
138         }
139     }
140
141     NtClose( config_key );
142     return ret;
143 }
144
145
146 /***********************************************************************
147  *         GetVersion   (KERNEL.3)
148  */
149 DWORD WINAPI GetVersion16(void)
150 {
151     static WORD dosver, winver;
152
153     if (!dosver)  /* not determined yet */
154     {
155         RTL_OSVERSIONINFOEXW info;
156
157         info.dwOSVersionInfoSize = sizeof(info);
158         if (RtlGetVersion( &info ) != STATUS_SUCCESS) return 0;
159
160         if (info.dwMajorVersion <= 3)
161             winver = MAKEWORD( info.dwMajorVersion, info.dwMinorVersion );
162         else
163             winver = MAKEWORD( 3, 95 );
164
165         switch(info.dwPlatformId)
166         {
167         case VER_PLATFORM_WIN32s:
168             if ((dosver = get_dos_version())) break;  /* got the configured version */
169
170             switch(MAKELONG( info.dwMinorVersion, info.dwMajorVersion ))
171             {
172             case 0x0200:
173                 dosver = 0x0303;  /* DOS 3.3 for Windows 2.0 */
174                 break;
175             case 0x0300:
176                 dosver = 0x0500;  /* DOS 5.0 for Windows 3.0 */
177                 break;
178             default:
179                 dosver = 0x0616;  /* DOS 6.22 for Windows 3.1 and later */
180                 break;
181             }
182             break;
183         case VER_PLATFORM_WIN32_WINDOWS:
184             /* DOS 8.0 for WinME, 7.0 for Win95/98 */
185             if (info.dwMinorVersion >= 90) dosver = 0x0800;
186             else dosver = 0x0700;
187             break;
188         case VER_PLATFORM_WIN32_NT:
189             dosver = 0x0500;  /* always DOS 5.0 for NT */
190             break;
191         }
192         TRACE( "DOS %d.%02d Win %d.%02d\n",
193                HIBYTE(dosver), LOBYTE(dosver), LOBYTE(winver), HIBYTE(winver) );
194     }
195     return MAKELONG( winver, dosver );
196 }
197
198
199 /***********************************************************************
200  *         GetVersion   (KERNEL32.@)
201  *
202  * Win31   0x80000a03
203  * Win95   0xc0000004
204  * Win98   0xc0000a04
205  * WinME   0xc0005a04
206  * NT351   0x04213303
207  * NT4     0x05650004
208  * Win2000 0x08930005
209  * WinXP   0x0a280105
210  */
211 DWORD WINAPI GetVersion(void)
212 {
213     RTL_OSVERSIONINFOEXW info;
214     DWORD result;
215
216     info.dwOSVersionInfoSize = sizeof(info);
217     if (RtlGetVersion( &info ) != STATUS_SUCCESS) return 0;
218
219     result = MAKELONG( MAKEWORD( info.dwMajorVersion, info.dwMinorVersion ),
220                        (info.dwPlatformId ^ 2) << 14 );
221     if (info.dwPlatformId == VER_PLATFORM_WIN32_NT) result |= LOWORD(info.dwBuildNumber) << 16;
222     return result;
223 }
224
225
226 /***********************************************************************
227  *         GetVersionEx   (KERNEL.149)
228  */
229 BOOL16 WINAPI GetVersionEx16(OSVERSIONINFO16 *v)
230 {
231     OSVERSIONINFOA info;
232
233     if (v->dwOSVersionInfoSize < sizeof(OSVERSIONINFO16))
234     {
235         WARN("wrong OSVERSIONINFO size from app\n");
236         return FALSE;
237     }
238
239     info.dwOSVersionInfoSize = sizeof(info);
240     if (!GetVersionExA( &info )) return FALSE;
241
242     v->dwMajorVersion = info.dwMajorVersion;
243     v->dwMinorVersion = info.dwMinorVersion;
244     v->dwBuildNumber  = info.dwBuildNumber;
245     v->dwPlatformId   = info.dwPlatformId;
246     strcpy( v->szCSDVersion, info.szCSDVersion );
247     return TRUE;
248 }
249
250
251 /***********************************************************************
252  *         GetVersionExA   (KERNEL32.@)
253  */
254 BOOL WINAPI GetVersionExA(OSVERSIONINFOA *v)
255 {
256     RTL_OSVERSIONINFOEXW infoW;
257
258     if (v->dwOSVersionInfoSize != sizeof(OSVERSIONINFOA) &&
259         v->dwOSVersionInfoSize != sizeof(OSVERSIONINFOEXA))
260     {
261         WARN("wrong OSVERSIONINFO size from app (got: %ld, expected: %d or %d)\n",
262                         v->dwOSVersionInfoSize, sizeof(OSVERSIONINFOA),
263                         sizeof(OSVERSIONINFOEXA));
264         return FALSE;
265     }
266
267     infoW.dwOSVersionInfoSize = sizeof(infoW);
268     if (RtlGetVersion( &infoW ) != STATUS_SUCCESS) return FALSE;
269
270     v->dwMajorVersion = infoW.dwMajorVersion;
271     v->dwMinorVersion = infoW.dwMinorVersion;
272     v->dwBuildNumber  = infoW.dwBuildNumber;
273     v->dwPlatformId   = infoW.dwPlatformId;
274     WideCharToMultiByte( CP_ACP, 0, infoW.szCSDVersion, -1,
275                          v->szCSDVersion, sizeof(v->szCSDVersion), NULL, NULL );
276
277     if(v->dwOSVersionInfoSize == sizeof(OSVERSIONINFOEXA))
278     {
279         LPOSVERSIONINFOEXA vex = (LPOSVERSIONINFOEXA) v;
280         vex->wServicePackMajor = infoW.wServicePackMajor;
281         vex->wServicePackMinor = infoW.wServicePackMinor;
282         vex->wSuiteMask        = infoW.wSuiteMask;
283         vex->wProductType      = infoW.wProductType;
284     }
285     return TRUE;
286 }
287
288
289 /***********************************************************************
290  *         GetVersionExW   (KERNEL32.@)
291  */
292 BOOL WINAPI GetVersionExW( OSVERSIONINFOW *info )
293 {
294     if (info->dwOSVersionInfoSize != sizeof(OSVERSIONINFOW) &&
295         info->dwOSVersionInfoSize != sizeof(OSVERSIONINFOEXW))
296     {
297         WARN("wrong OSVERSIONINFO size from app (got: %ld, expected: %d or %d)\n",
298                         info->dwOSVersionInfoSize, sizeof(OSVERSIONINFOW),
299                         sizeof(OSVERSIONINFOEXW));
300         return FALSE;
301     }
302     return (RtlGetVersion( (RTL_OSVERSIONINFOEXW *)info ) == STATUS_SUCCESS);
303 }
304
305
306 /******************************************************************************
307  *        VerifyVersionInfoA   (KERNEL32.@)
308  */
309 BOOL WINAPI VerifyVersionInfoA( LPOSVERSIONINFOEXA lpVersionInfo, DWORD dwTypeMask,
310                                 DWORDLONG dwlConditionMask)
311 {
312     OSVERSIONINFOEXW verW;
313
314     verW.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEXW);
315     verW.dwMajorVersion = lpVersionInfo->dwMajorVersion;
316     verW.dwMinorVersion = lpVersionInfo->dwMinorVersion;
317     verW.dwBuildNumber = lpVersionInfo->dwBuildNumber;
318     verW.dwPlatformId = lpVersionInfo->dwPlatformId;
319     verW.wServicePackMajor = lpVersionInfo->wServicePackMajor;
320     verW.wServicePackMinor = lpVersionInfo->wServicePackMinor;
321     verW.wSuiteMask = lpVersionInfo->wSuiteMask;
322     verW.wProductType = lpVersionInfo->wProductType;
323     verW.wReserved = lpVersionInfo->wReserved;
324
325     return VerifyVersionInfoW(&verW, dwTypeMask, dwlConditionMask);
326 }
327
328
329 /******************************************************************************
330  *        VerifyVersionInfoW   (KERNEL32.@)
331  */
332 BOOL WINAPI VerifyVersionInfoW( LPOSVERSIONINFOEXW lpVersionInfo, DWORD dwTypeMask,
333                                 DWORDLONG dwlConditionMask)
334 {
335     switch(RtlVerifyVersionInfo( lpVersionInfo, dwTypeMask, dwlConditionMask ))
336     {
337     case STATUS_INVALID_PARAMETER:
338         SetLastError( ERROR_BAD_ARGUMENTS );
339         return FALSE;
340     case STATUS_REVISION_MISMATCH:
341         SetLastError( ERROR_OLD_WIN_VERSION );
342         return FALSE;
343     }
344     return TRUE;
345 }
346
347
348 /***********************************************************************
349  *          GetWinFlags   (KERNEL.132)
350  */
351 DWORD WINAPI GetWinFlags16(void)
352 {
353   static const long cpuflags[5] =
354     { WF_CPU086, WF_CPU186, WF_CPU286, WF_CPU386, WF_CPU486 };
355   SYSTEM_INFO si;
356   OSVERSIONINFOA ovi;
357   DWORD result;
358
359   GetSystemInfo(&si);
360
361   /* There doesn't seem to be any Pentium flag.  */
362   result = cpuflags[min(si.wProcessorLevel, 4)] | WF_ENHANCED | WF_PMODE | WF_80x87 | WF_PAGING;
363   if (si.wProcessorLevel >= 4) result |= WF_HASCPUID;
364   ovi.dwOSVersionInfoSize = sizeof(ovi);
365   GetVersionExA(&ovi);
366   if (ovi.dwPlatformId == VER_PLATFORM_WIN32_NT)
367       result |= WF_WIN32WOW; /* undocumented WF_WINNT */
368   return result;
369 }
370
371
372 #if 0
373 /* Not used at this time. This is here for documentation only */
374
375 /* WINDEBUGINFO flags values */
376 #define WDI_OPTIONS         0x0001
377 #define WDI_FILTER          0x0002
378 #define WDI_ALLOCBREAK      0x0004
379
380 /* dwOptions values */
381 #define DBO_CHECKHEAP       0x0001
382 #define DBO_BUFFERFILL      0x0004
383 #define DBO_DISABLEGPTRAPPING 0x0010
384 #define DBO_CHECKFREE       0x0020
385
386 #define DBO_SILENT          0x8000
387
388 #define DBO_TRACEBREAK      0x2000
389 #define DBO_WARNINGBREAK    0x1000
390 #define DBO_NOERRORBREAK    0x0800
391 #define DBO_NOFATALBREAK    0x0400
392 #define DBO_INT3BREAK       0x0100
393
394 /* DebugOutput flags values */
395 #define DBF_TRACE           0x0000
396 #define DBF_WARNING         0x4000
397 #define DBF_ERROR           0x8000
398 #define DBF_FATAL           0xc000
399
400 /* dwFilter values */
401 #define DBF_KERNEL          0x1000
402 #define DBF_KRN_MEMMAN      0x0001
403 #define DBF_KRN_LOADMODULE  0x0002
404 #define DBF_KRN_SEGMENTLOAD 0x0004
405 #define DBF_USER            0x0800
406 #define DBF_GDI             0x0400
407 #define DBF_MMSYSTEM        0x0040
408 #define DBF_PENWIN          0x0020
409 #define DBF_APPLICATION     0x0008
410 #define DBF_DRIVER          0x0010
411
412 #endif /* NOLOGERROR */
413
414
415 /***********************************************************************
416  *          GetWinDebugInfo   (KERNEL.355)
417  */
418 BOOL16 WINAPI GetWinDebugInfo16(WINDEBUGINFO16 *lpwdi, UINT16 flags)
419 {
420     FIXME("(%8lx,%d): stub returning 0\n",
421           (unsigned long)lpwdi, flags);
422     /* 0 means not in debugging mode/version */
423     /* Can this type of debugging be used in wine ? */
424     /* Constants: WDI_OPTIONS WDI_FILTER WDI_ALLOCBREAK */
425     return 0;
426 }
427
428
429 /***********************************************************************
430  *          SetWinDebugInfo   (KERNEL.356)
431  */
432 BOOL16 WINAPI SetWinDebugInfo16(WINDEBUGINFO16 *lpwdi)
433 {
434     FIXME("(%8lx): stub returning 0\n", (unsigned long)lpwdi);
435     /* 0 means not in debugging mode/version */
436     /* Can this type of debugging be used in wine ? */
437     /* Constants: WDI_OPTIONS WDI_FILTER WDI_ALLOCBREAK */
438     return 0;
439 }
440
441
442 /***********************************************************************
443  *           K329                    (KERNEL.329)
444  *
445  * TODO:
446  * Should fill lpBuffer only if DBO_BUFFERFILL has been set by SetWinDebugInfo()
447  */
448 void WINAPI DebugFillBuffer(LPSTR lpBuffer, WORD wBytes)
449 {
450         memset(lpBuffer, DBGFILL_BUFFER, wBytes);
451 }
452
453 /***********************************************************************
454  *           DiagQuery                          (KERNEL.339)
455  *
456  * returns TRUE if Win called with "/b" (bootlog.txt)
457  */
458 BOOL16 WINAPI DiagQuery16(void)
459 {
460         /* perhaps implement a Wine "/b" command line flag sometime ? */
461         return FALSE;
462 }
463
464 /***********************************************************************
465  *           DiagOutput                         (KERNEL.340)
466  *
467  * writes a debug string into <windir>\bootlog.txt
468  */
469 void WINAPI DiagOutput16(LPCSTR str)
470 {
471         /* FIXME */
472         DPRINTF("DIAGOUTPUT:%s\n", debugstr_a(str));
473 }