Enhance uxtheme to store the themed system metrics in the registry and
[wine] / dlls / ntdll / 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 "wine/unicode.h"
34 #include "wine/debug.h"
35 #include "ntdll_misc.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(ver);
38
39 typedef enum
40 {
41     WIN20,   /* Windows 2.0 */
42     WIN30,   /* Windows 3.0 */
43     WIN31,   /* Windows 3.1 */
44     WIN95,   /* Windows 95 */
45     WIN98,   /* Windows 98 */
46     WINME,   /* Windows Me */
47     NT351,   /* Windows NT 3.51 */
48     NT40,    /* Windows NT 4.0 */
49     NT2K,    /* Windows 2000 */
50     WINXP,   /* Windows XP */
51     WIN2K3,  /* Windows 2003 */
52     NB_WINDOWS_VERSIONS
53 } WINDOWS_VERSION;
54
55 /* FIXME: compare values below with original and fix.
56  * An *excellent* win9x version page (ALL versions !)
57  * can be found at members.aol.com/axcel216/ver.htm */
58 static const RTL_OSVERSIONINFOEXW VersionData[NB_WINDOWS_VERSIONS] =
59 {
60     /* WIN20 FIXME: verify values */
61     {
62         sizeof(RTL_OSVERSIONINFOEXW), 2, 0, 0, VER_PLATFORM_WIN32s,
63         {'W','i','n','3','2','s',' ','1','.','3',0},
64         0, 0, 0, 0, 0
65     },
66     /* WIN30 FIXME: verify values */
67     {
68         sizeof(RTL_OSVERSIONINFOEXW), 3, 0, 0, VER_PLATFORM_WIN32s,
69         {'W','i','n','3','2','s',' ','1','.','3',0},
70         0, 0, 0, 0, 0
71     },
72     /* WIN31 */
73     {
74         sizeof(RTL_OSVERSIONINFOEXW), 3, 10, 0, VER_PLATFORM_WIN32s,
75         {'W','i','n','3','2','s',' ','1','.','3',0},
76         0, 0, 0, 0, 0
77     },
78     /* WIN95 */
79     {
80         /* Win95:       4, 0, 0x40003B6, ""
81          * Win95sp1:    4, 0, 0x40003B6, " A " (according to doc)
82          * Win95osr2:   4, 0, 0x4000457, " B " (according to doc)
83          * Win95osr2.1: 4, 3, 0x40304BC, " B " (according to doc)
84          * Win95osr2.5: 4, 3, 0x40304BE, " C " (according to doc)
85          * Win95a/b can be discerned via regkey SubVersionNumber
86          * See also:
87          * http://support.microsoft.com/support/kb/articles/q158/2/38.asp
88          */
89         sizeof(RTL_OSVERSIONINFOEXW), 4, 0, 0x40003B6, VER_PLATFORM_WIN32_WINDOWS,
90         {0},
91         0, 0, 0, 0, 0
92     },
93     /* WIN98 (second edition) */
94     {
95         /* Win98:   4, 10, 0x40A07CE, " "   4.10.1998
96          * Win98SE: 4, 10, 0x40A08AE, " A " 4.10.2222
97          */
98         sizeof(RTL_OSVERSIONINFOEXW), 4, 10, 0x40A08AE, VER_PLATFORM_WIN32_WINDOWS,
99         {' ','A',' ',0},
100         0, 0, 0, 0, 0
101     },
102     /* WINME */
103     {
104         sizeof(RTL_OSVERSIONINFOEXW), 4, 90, 0x45A0BB8, VER_PLATFORM_WIN32_WINDOWS,
105         {' ',0},
106         0, 0, 0, 0, 0
107     },
108     /* NT351 */
109     {
110         sizeof(RTL_OSVERSIONINFOEXW), 3, 51, 0x421, VER_PLATFORM_WIN32_NT,
111         {'S','e','r','v','i','c','e',' ','P','a','c','k',' ','2',0},
112         0, 0, 0, 0, 0
113     },
114     /* NT40 */
115     {
116         sizeof(RTL_OSVERSIONINFOEXW), 4, 0, 0x565, VER_PLATFORM_WIN32_NT,
117         {'S','e','r','v','i','c','e',' ','P','a','c','k',' ','6','a',0},
118         6, 0, 0, VER_NT_WORKSTATION, 0
119     },
120     /* NT2K */
121     {
122         sizeof(RTL_OSVERSIONINFOEXW), 5, 0, 0x893, VER_PLATFORM_WIN32_NT,
123         {'S','e','r','v','i','c','e',' ','P','a','c','k',' ','4',0},
124         4, 0, 0, VER_NT_WORKSTATION, 30 /* FIXME: Great, a reserved field with a value! */
125     },
126     /* WINXP */
127     {
128         sizeof(RTL_OSVERSIONINFOEXW), 5, 1, 0xA28, VER_PLATFORM_WIN32_NT,
129         {'S','e','r','v','i','c','e',' ','P','a','c','k',' ','2',0},
130         2, 0, VER_SUITE_SINGLEUSERTS, VER_NT_WORKSTATION, 30 /* FIXME: Great, a reserved field with a value! */
131     },
132     /* WIN2K3 */
133     {
134         sizeof(RTL_OSVERSIONINFOEXW), 5, 2, 0xECE, VER_PLATFORM_WIN32_NT,
135         {'S','e','r','v','i','c','e',' ','P','a','c','k',' ','1',0},
136         1, 0, VER_SUITE_SINGLEUSERTS, VER_NT_SERVER, 0
137     }
138 };
139
140 static const char * const WinVersionNames[NB_WINDOWS_VERSIONS] =
141 { /* no spaces in here ! */
142     "win20",                      /* WIN20 */
143     "win30",                      /* WIN30 */
144     "win31",                      /* WIN31 */
145     "win95",                      /* WIN95 */
146     "win98",                      /* WIN98 */
147     "winme",                      /* WINME */
148     "nt351",                      /* NT351 */
149     "nt40",                       /* NT40 */
150     "win2000,win2k,nt2k,nt2000",  /* NT2K */
151     "winxp",                      /* WINXP */
152     "win2003,win2k3"              /* WIN2K3 */
153 };
154
155
156 /* initialized to null so that we crash if we try to retrieve the version too early at startup */
157 static const RTL_OSVERSIONINFOEXW *current_version;
158
159 /**********************************************************************
160  *         parse_win_version
161  *
162  * Parse the contents of the Version key.
163  */
164 static BOOL parse_win_version( HANDLE hkey )
165 {
166     static const WCHAR VersionW[] = {'V','e','r','s','i','o','n',0};
167
168     UNICODE_STRING valueW;
169     char tmp[64], buffer[50];
170     KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)tmp;
171     DWORD count, len;
172     int i;
173
174     RtlInitUnicodeString( &valueW, VersionW );
175     if (NtQueryValueKey( hkey, &valueW, KeyValuePartialInformation, tmp, sizeof(tmp), &count ))
176         return FALSE;
177
178     RtlUnicodeToMultiByteN( buffer, sizeof(buffer)-1, &len, (WCHAR *)info->Data, info->DataLength );
179     buffer[len] = 0;
180
181     for (i = 0; i < NB_WINDOWS_VERSIONS; i++)
182     {
183         const char *p, *pCurr = WinVersionNames[i];
184         /* iterate through all winver aliases separated by comma */
185         do {
186             p = strchr(pCurr, ',');
187             len = p ? p - pCurr : strlen(pCurr);
188             if ( (!strncmp( pCurr, buffer, len )) && (buffer[len] == 0) )
189             {
190                 current_version = &VersionData[i];
191                 TRACE( "got win version %s\n", WinVersionNames[i] );
192                 return TRUE;
193             }
194             pCurr = p+1;
195         } while (p);
196     }
197
198     MESSAGE("Invalid Windows version value '%s' specified in config file.\n", buffer );
199     MESSAGE("Valid versions are:" );
200     for (i = 0; i < NB_WINDOWS_VERSIONS; i++)
201     {
202         /* only list the first, "official" alias in case of aliases */
203         const char *pCurr = WinVersionNames[i];
204         const char *p = strchr(pCurr, ',');
205         len = (p) ? p - pCurr : strlen(pCurr);
206
207         MESSAGE(" '%.*s'%c", (int)len, pCurr, (i == NB_WINDOWS_VERSIONS - 1) ? '\n' : ',' );
208     }
209     return FALSE;
210 }
211
212
213 /**********************************************************************
214  *         version_init
215  */
216 void version_init( const WCHAR *appname )
217 {
218     static const WCHAR configW[] = {'S','o','f','t','w','a','r','e','\\','W','i','n','e',0};
219     static const WCHAR appdefaultsW[] = {'A','p','p','D','e','f','a','u','l','t','s','\\',0};
220
221     OBJECT_ATTRIBUTES attr;
222     UNICODE_STRING nameW;
223     HANDLE root, hkey, config_key;
224     BOOL got_win_ver = FALSE;
225
226     current_version = &VersionData[WIN98];  /* default if nothing else is specified */
227
228     RtlOpenCurrentUser( KEY_ALL_ACCESS, &root );
229     attr.Length = sizeof(attr);
230     attr.RootDirectory = root;
231     attr.ObjectName = &nameW;
232     attr.Attributes = 0;
233     attr.SecurityDescriptor = NULL;
234     attr.SecurityQualityOfService = NULL;
235     RtlInitUnicodeString( &nameW, configW );
236
237     /* @@ Wine registry key: HKCU\Software\Wine */
238     if (NtOpenKey( &config_key, KEY_ALL_ACCESS, &attr )) config_key = 0;
239     NtClose( root );
240     if (!config_key) goto done;
241
242     /* open AppDefaults\\appname key */
243     if (appname && *appname)
244     {
245         const WCHAR *p;
246         WCHAR appversion[MAX_PATH+20];
247
248         if ((p = strrchrW( appname, '/' ))) appname = p + 1;
249         if ((p = strrchrW( appname, '\\' ))) appname = p + 1;
250
251         strcpyW( appversion, appdefaultsW );
252         strcatW( appversion, appname );
253         RtlInitUnicodeString( &nameW, appversion );
254         attr.RootDirectory = config_key;
255
256         /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe */
257         if (!NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr ))
258         {
259             TRACE( "getting version from %s\n", debugstr_w(appversion) );
260             got_win_ver = parse_win_version( hkey );
261             NtClose( hkey );
262         }
263     }
264
265     if (!got_win_ver)
266     {
267         TRACE( "getting default version\n" );
268         parse_win_version( config_key );
269     }
270     NtClose( config_key );
271
272 done:
273     NtCurrentTeb()->Peb->OSMajorVersion = current_version->dwMajorVersion;
274     NtCurrentTeb()->Peb->OSMinorVersion = current_version->dwMinorVersion;
275     NtCurrentTeb()->Peb->OSBuildNumber  = current_version->dwBuildNumber;
276     NtCurrentTeb()->Peb->OSPlatformId   = current_version->dwPlatformId;
277
278     TRACE( "got %ld.%ld plaform %ld build %lx name %s\n",
279            current_version->dwMajorVersion, current_version->dwMinorVersion,
280            current_version->dwPlatformId, current_version->dwBuildNumber,
281            debugstr_w( current_version->szCSDVersion ));
282 }
283
284
285 /***********************************************************************
286  *         RtlGetVersion   (NTDLL.@)
287  */
288 NTSTATUS WINAPI RtlGetVersion( RTL_OSVERSIONINFOEXW *info )
289 {
290     info->dwMajorVersion = current_version->dwMajorVersion;
291     info->dwMinorVersion = current_version->dwMinorVersion;
292     info->dwBuildNumber  = current_version->dwBuildNumber;
293     info->dwPlatformId   = current_version->dwPlatformId;
294     strcpyW( info->szCSDVersion, current_version->szCSDVersion );
295     if(info->dwOSVersionInfoSize == sizeof(RTL_OSVERSIONINFOEXW))
296     {
297         info->wServicePackMajor = current_version->wServicePackMajor;
298         info->wServicePackMinor = current_version->wServicePackMinor;
299         info->wSuiteMask        = current_version->wSuiteMask;
300         info->wProductType      = current_version->wProductType;
301     }
302     return STATUS_SUCCESS;
303 }
304
305
306 /******************************************************************************
307  *  RtlGetNtVersionNumbers   (NTDLL.@)
308  *
309  * Get the version numbers of the run time library.
310  *
311  * PARAMS
312  *  major [O] Destination for the Major version
313  *  minor [O] Destination for the Minor version
314  *  build [O] Destination for the Build version
315  *
316  * RETURNS
317  *  Nothing.
318  *
319  * NOTES
320  * Introduced in Windows XP (NT5.1)
321  */
322 void WINAPI RtlGetNtVersionNumbers( LPDWORD major, LPDWORD minor, LPDWORD build )
323 {
324     if (major) *major = current_version->dwMajorVersion;
325     if (minor) *minor = current_version->dwMinorVersion;
326     /* FIXME: Does anybody know the real formula? */
327     if (build) *build = (0xF0000000 | current_version->dwBuildNumber);
328 }
329
330
331 /******************************************************************************
332  *  RtlGetNtProductType   (NTDLL.@)
333  */
334 BOOLEAN WINAPI RtlGetNtProductType( LPDWORD type )
335 {
336     if (type) *type = current_version->wProductType;
337     return TRUE;
338 }
339
340
341 /******************************************************************************
342  *        VerifyVersionInfoW   (KERNEL32.@)
343  */
344 NTSTATUS WINAPI RtlVerifyVersionInfo( const RTL_OSVERSIONINFOEXW *info,
345                                       DWORD dwTypeMask, DWORDLONG dwlConditionMask )
346 {
347     RTL_OSVERSIONINFOEXW ver;
348     NTSTATUS status;
349
350     FIXME("(%p,%lu,%llx): Not all cases correctly implemented yet\n",
351           info, dwTypeMask, dwlConditionMask);
352
353     /* FIXME:
354         - Check the following special case on Windows (various versions):
355           o lp->wSuiteMask == 0 and ver.wSuiteMask != 0 and VER_AND/VER_OR
356           o lp->dwOSVersionInfoSize != sizeof(OSVERSIONINFOEXW)
357         - MSDN talks about some tests being impossible. Check what really happens.
358      */
359
360     ver.dwOSVersionInfoSize = sizeof(ver);
361     if ((status = RtlGetVersion( &ver )) != STATUS_SUCCESS) return status;
362
363     if(!(dwTypeMask && dwlConditionMask)) return STATUS_INVALID_PARAMETER;
364
365     if(dwTypeMask & VER_PRODUCT_TYPE)
366         switch(dwlConditionMask >> 7*3 & 0x07) {
367             case VER_EQUAL:
368                 if(ver.wProductType != info->wProductType) return STATUS_REVISION_MISMATCH;
369                 break;
370             case VER_GREATER:
371                 if(ver.wProductType <= info->wProductType) return STATUS_REVISION_MISMATCH;
372                 break;
373             case VER_GREATER_EQUAL:
374                 if(ver.wProductType < info->wProductType) return STATUS_REVISION_MISMATCH;
375                 break;
376             case VER_LESS:
377                 if(ver.wProductType >= info->wProductType) return STATUS_REVISION_MISMATCH;
378                 break;
379             case VER_LESS_EQUAL:
380                 if(ver.wProductType > info->wProductType) return STATUS_REVISION_MISMATCH;
381                 break;
382             default:
383                 return STATUS_INVALID_PARAMETER;
384         }
385     if(dwTypeMask & VER_SUITENAME)
386         switch(dwlConditionMask >> 6*3 & 0x07)
387         {
388             case VER_AND:
389                 if((info->wSuiteMask & ver.wSuiteMask) != info->wSuiteMask)
390                     return STATUS_REVISION_MISMATCH;
391                 break;
392             case VER_OR:
393                 if(!(info->wSuiteMask & ver.wSuiteMask) && info->wSuiteMask)
394                     return STATUS_REVISION_MISMATCH;
395                 break;
396             default:
397                 return STATUS_INVALID_PARAMETER;
398         }
399     if(dwTypeMask & VER_PLATFORMID)
400         switch(dwlConditionMask >> 3*3 & 0x07)
401         {
402             case VER_EQUAL:
403                 if(ver.dwPlatformId != info->dwPlatformId) return STATUS_REVISION_MISMATCH;
404                 break;
405             case VER_GREATER:
406                 if(ver.dwPlatformId <= info->dwPlatformId) return STATUS_REVISION_MISMATCH;
407                 break;
408             case VER_GREATER_EQUAL:
409                 if(ver.dwPlatformId < info->dwPlatformId) return STATUS_REVISION_MISMATCH;
410                 break;
411             case VER_LESS:
412                 if(ver.dwPlatformId >= info->dwPlatformId) return STATUS_REVISION_MISMATCH;
413                 break;
414             case VER_LESS_EQUAL:
415                 if(ver.dwPlatformId > info->dwPlatformId) return STATUS_REVISION_MISMATCH;
416                 break;
417             default:
418                 return STATUS_INVALID_PARAMETER;
419         }
420     if(dwTypeMask & VER_BUILDNUMBER)
421         switch(dwlConditionMask >> 2*3 & 0x07)
422         {
423             case VER_EQUAL:
424                 if(ver.dwBuildNumber != info->dwBuildNumber) return STATUS_REVISION_MISMATCH;
425                 break;
426             case VER_GREATER:
427                 if(ver.dwBuildNumber <= info->dwBuildNumber) return STATUS_REVISION_MISMATCH;
428                 break;
429             case VER_GREATER_EQUAL:
430                 if(ver.dwBuildNumber < info->dwBuildNumber) return STATUS_REVISION_MISMATCH;
431                 break;
432             case VER_LESS:
433                 if(ver.dwBuildNumber >= info->dwBuildNumber) return STATUS_REVISION_MISMATCH;
434                 break;
435             case VER_LESS_EQUAL:
436                 if(ver.dwBuildNumber > info->dwBuildNumber) return STATUS_REVISION_MISMATCH;
437                 break;
438             default:
439                 return STATUS_INVALID_PARAMETER;
440         }
441     if(dwTypeMask & VER_MAJORVERSION)
442         switch(dwlConditionMask >> 1*3 & 0x07)
443         {
444             case VER_EQUAL:
445                 if(ver.dwMajorVersion != info->dwMajorVersion) return STATUS_REVISION_MISMATCH;
446                 break;
447             case VER_GREATER:
448                 if(ver.dwMajorVersion <= info->dwMajorVersion) return STATUS_REVISION_MISMATCH;
449                 break;
450             case VER_GREATER_EQUAL:
451                 if(ver.dwMajorVersion < info->dwMajorVersion) return STATUS_REVISION_MISMATCH;
452                 break;
453             case VER_LESS:
454                 if(ver.dwMajorVersion >= info->dwMajorVersion) return STATUS_REVISION_MISMATCH;
455                 break;
456             case VER_LESS_EQUAL:
457                 if(ver.dwMajorVersion > info->dwMajorVersion) return STATUS_REVISION_MISMATCH;
458                 break;
459             default:
460                 return STATUS_INVALID_PARAMETER;
461         }
462     if(dwTypeMask & VER_MINORVERSION)
463         switch(dwlConditionMask >> 0*3 & 0x07)
464         {
465             case VER_EQUAL:
466                 if(ver.dwMinorVersion != info->dwMinorVersion) return STATUS_REVISION_MISMATCH;
467                 break;
468             case VER_GREATER:
469                 if(ver.dwMinorVersion <= info->dwMinorVersion) return STATUS_REVISION_MISMATCH;
470                 break;
471             case VER_GREATER_EQUAL:
472                 if(ver.dwMinorVersion < info->dwMinorVersion) return STATUS_REVISION_MISMATCH;
473                 break;
474             case VER_LESS:
475                 if(ver.dwMinorVersion >= info->dwMinorVersion) return STATUS_REVISION_MISMATCH;
476                 break;
477             case VER_LESS_EQUAL:
478                 if(ver.dwMinorVersion > info->dwMinorVersion) return STATUS_REVISION_MISMATCH;
479                 break;
480             default:
481                 return STATUS_INVALID_PARAMETER;
482         }
483     if(dwTypeMask & VER_SERVICEPACKMAJOR)
484         switch(dwlConditionMask >> 5*3 & 0x07)
485         {
486             case VER_EQUAL:
487                 if(ver.wServicePackMajor != info->wServicePackMajor) return STATUS_REVISION_MISMATCH;
488                 break;
489             case VER_GREATER:
490                 if(ver.wServicePackMajor <= info->wServicePackMajor) return STATUS_REVISION_MISMATCH;
491                 break;
492             case VER_GREATER_EQUAL:
493                 if(ver.wServicePackMajor < info->wServicePackMajor) return STATUS_REVISION_MISMATCH;
494                 break;
495             case VER_LESS:
496                 if(ver.wServicePackMajor >= info->wServicePackMajor) return STATUS_REVISION_MISMATCH;
497                 break;
498             case VER_LESS_EQUAL:
499                 if(ver.wServicePackMajor > info->wServicePackMajor) return STATUS_REVISION_MISMATCH;
500                 break;
501             default:
502                 return STATUS_INVALID_PARAMETER;
503         }
504     if(dwTypeMask & VER_SERVICEPACKMINOR)
505         switch(dwlConditionMask >> 4*3 & 0x07)
506         {
507             case VER_EQUAL:
508                 if(ver.wServicePackMinor != info->wServicePackMinor) return STATUS_REVISION_MISMATCH;
509                 break;
510             case VER_GREATER:
511                 if(ver.wServicePackMinor <= info->wServicePackMinor) return STATUS_REVISION_MISMATCH;
512                 break;
513             case VER_GREATER_EQUAL:
514                 if(ver.wServicePackMinor < info->wServicePackMinor) return STATUS_REVISION_MISMATCH;
515                 break;
516             case VER_LESS:
517                 if(ver.wServicePackMinor >= info->wServicePackMinor) return STATUS_REVISION_MISMATCH;
518                 break;
519             case VER_LESS_EQUAL:
520                 if(ver.wServicePackMinor > info->wServicePackMinor) return STATUS_REVISION_MISMATCH;
521                 break;
522             default:
523                 return STATUS_INVALID_PARAMETER;
524         }
525
526     return STATUS_SUCCESS;
527 }