Removed some unnecessary includes.
[wine] / misc / version.c
1 /*
2  * Windows and DOS version functions
3  *
4  * Copyright 1997 Alexandre Julliard
5  * Copyright 1997 Marcus Meissner
6  * Copyright 1998 Patrik Stridvall
7  * Copyright 1998 Andreas Mohr
8  */
9
10 #include <string.h>
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include "windef.h"
14 #include "winbase.h"
15 #include "wingdi.h"
16 #include "winuser.h"
17 #include "wine/winbase16.h"
18 #include "process.h"
19 #include "options.h"
20 #include "debugtools.h"
21 #include "winerror.h"
22
23 DEFAULT_DEBUG_CHANNEL(ver);
24
25 typedef enum
26 {
27     WIN20,   /* Windows 2.0 */
28     WIN30,   /* Windows 3.0 */
29     WIN31,   /* Windows 3.1 */
30     WIN95,   /* Windows 95 */
31     WIN98,   /* Windows 98 */
32  /* insert Windows ME here as WINME if needed */
33     NT351,   /* Windows NT 3.51 */
34     NT40,    /* Windows NT 4.0 */  
35     NT2K,    /* Windows 2000 */
36     NB_WINDOWS_VERSIONS
37 } WINDOWS_VERSION;
38
39 typedef struct
40 {
41     LONG             getVersion16; 
42     LONG             getVersion32;
43     OSVERSIONINFOA getVersionEx;
44 } VERSION_DATA;
45
46 /* FIXME: compare values below with original and fix */
47 static VERSION_DATA VersionData[NB_WINDOWS_VERSIONS] =
48 {
49     /* WIN20 FIXME: verify values */
50     {
51         MAKELONG( 0x0002, 0x0303 ), /* assume DOS 3.3 */
52         MAKELONG( 0x0003, 0x8000 ),
53         {
54             sizeof(OSVERSIONINFOA), 2, 0, 0,
55             VER_PLATFORM_WIN32s, "Win32s 1.3" 
56         }
57     },
58     /* WIN30 FIXME: verify values */
59     {
60         MAKELONG( 0x0003, 0x0500 ), /* assume DOS 5.00 */
61         MAKELONG( 0x0003, 0x8000 ),
62         {
63             sizeof(OSVERSIONINFOA), 3, 0, 0,
64             VER_PLATFORM_WIN32s, "Win32s 1.3" 
65         }
66     },
67     /* WIN31 */
68     {
69         MAKELONG( 0x0a03, 0x0616 ), /* DOS 6.22 */
70         MAKELONG( 0x0a03, 0x8000 ),
71         {
72             sizeof(OSVERSIONINFOA), 3, 10, 0,
73             VER_PLATFORM_WIN32s, "Win32s 1.3" 
74         }
75     },
76     /* WIN95 */
77     {
78         0x07005F03,
79         0xC0000004,
80         {
81             sizeof(OSVERSIONINFOA), 4, 0, 0x40003B6,
82             VER_PLATFORM_WIN32_WINDOWS, "Win95"
83         }
84     },
85     /* WIN98 */
86     {
87         0x070A5F03,
88         0xC0000A04,
89         {
90             sizeof(OSVERSIONINFOA), 4, 10, 0x40A07CE,
91             VER_PLATFORM_WIN32_WINDOWS, "Win98"
92         }
93     },
94     /* NT351 */
95     {
96         0x05000A03,
97         0x04213303,
98         {
99             sizeof(OSVERSIONINFOA), 3, 51, 0x421,
100             VER_PLATFORM_WIN32_NT, "Service Pack 2"
101         }
102     },
103     /* NT40 */
104     {
105         0x05000A03,
106         0x05650004,
107         {
108             sizeof(OSVERSIONINFOA), 4, 0, 0x565,
109             VER_PLATFORM_WIN32_NT, "Service Pack 6"
110         }
111     },
112     /* NT2K FIXME: verify values */
113     {
114         0x05000A03, /* ? */
115         0x08930005, /* better build ? using 2195 (final) for now */
116         {
117             sizeof(OSVERSIONINFOA), 5, 0, 0x893,
118             VER_PLATFORM_WIN32_NT, "FIXME: OS version string not known yet"
119         }
120     }
121 };
122
123 static const char *WinVersionNames[NB_WINDOWS_VERSIONS] =
124 { /* no spaces in here ! */
125     "win20",
126     "win30",
127     "win31",
128     "win95",
129     "win98",
130     "nt351",
131     "nt40",
132     "win2000,win2k,nt2k,nt2000"
133 };
134
135 /* if one of the following dlls is importing ntdll the windows
136 version autodetection switches wine to unicode (nt 3.51 or 4.0) */
137 static char * special_dlls[] =
138 {
139         "COMDLG32",
140         "COMCTL32",
141         "SHELL32",
142         "OLE32",
143         "RPCRT4",
144         NULL
145 };
146
147 /* the current version has not been autodetected but forced via cmdline */
148 static BOOL versionForced = FALSE;
149 static WINDOWS_VERSION defaultWinVersion = WIN31;
150
151 /**********************************************************************
152  *         VERSION_ParseWinVersion
153  */
154 void VERSION_ParseWinVersion( const char *arg )
155 {
156     int i, len;
157     const char *pCurr, *p;
158     for (i = 0; i < NB_WINDOWS_VERSIONS; i++)
159     {
160         pCurr = WinVersionNames[i];
161         /* iterate through all winver aliases separated by comma */
162         do {
163             p = strchr(pCurr, ',');
164             len = p ? (int)p - (int)pCurr : strlen(pCurr);
165             if ( (!strncmp( pCurr, arg, len )) && (arg[len] == '\0') )
166             {
167                 defaultWinVersion = (WINDOWS_VERSION)i;
168                 versionForced = TRUE;
169                 return;
170             }
171             pCurr = p+1;
172         } while (p);
173     }
174     MESSAGE("Invalid winver value '%s' specified.\n", arg );
175     MESSAGE("Valid versions are:" );
176     for (i = 0; i < NB_WINDOWS_VERSIONS; i++)
177     {
178         /* only list the first, "official" alias in case of aliases */
179         pCurr = WinVersionNames[i];
180         p = strchr(pCurr, ',');
181         len = (p) ? (int)p - (int)pCurr : strlen(pCurr);
182
183         MESSAGE(" '%.*s'%c", len, pCurr,
184                 (i == NB_WINDOWS_VERSIONS - 1) ? '\n' : ',' );
185     }
186     ExitProcess(1);
187 }
188
189
190 /**********************************************************************
191  *         VERSION_ParseDosVersion
192  */
193 void VERSION_ParseDosVersion( const char *arg )
194 {
195     int hi, lo;
196     if (sscanf( arg, "%d.%d", &hi, &lo ) == 2)
197     {
198         VersionData[WIN31].getVersion16 =
199             MAKELONG(LOWORD(VersionData[WIN31].getVersion16),
200                      (hi<<8) + lo);
201     }
202     else
203     {
204         MESSAGE("--dosver: Wrong version format. Use \"--dosver x.xx\"\n");
205         ExitProcess(1);
206     }
207 }
208
209 /**********************************************************************
210  *      VERSION_GetSystemDLLVersion
211  *
212  * This function tries to figure out if a given (native) dll comes from
213  * win95/98 or winnt. Since all values in the OptionalHeader are not a 
214  * usable hint, we test if a dll imports the ntdll.
215  * This is at least working for all system dlls like comctl32, comdlg32 and
216  * shell32.
217  * If you have a better idea to figure this out...
218  */
219 static DWORD VERSION_GetSystemDLLVersion( HMODULE hmod )
220 {
221     IMAGE_DATA_DIRECTORY *dir = PE_HEADER(hmod)->OptionalHeader.DataDirectory
222                                 + IMAGE_DIRECTORY_ENTRY_IMPORT;
223     if (dir->Size && dir->VirtualAddress)
224     {
225         IMAGE_IMPORT_DESCRIPTOR *pe_imp = (IMAGE_IMPORT_DESCRIPTOR *)((char *)hmod + dir->VirtualAddress);
226         for ( ; pe_imp->Name; pe_imp++)
227         {
228             char * name = (char *)hmod + (unsigned int)pe_imp->Name;
229             TRACE("%s\n", name);
230             
231             if (!strncasecmp(name, "ntdll", 5))
232             {
233               if (3 == PE_HEADER(hmod)->OptionalHeader.MajorOperatingSystemVersion)
234                 return NT351;
235               else
236                 return NT40;
237             }
238         }
239     }
240     return WIN95;
241 }
242 /**********************************************************************
243  *      VERSION_GetLinkedDllVersion
244  *
245  * Some version data (not reliable!):
246  * linker/OS/image/subsys
247  *
248  * x.xx/1.00/0.00/3.10  Win32s          (any version ?)
249  * 2.39/1.00/0.00/3.10  Win32s          freecell.exe (any version)
250  * 2.50/1.00/4.00/4.00  Win32s 1.30     winhlp32.exe    
251  * 2.60/3.51/3.51/3.51  NT351SP5        system dlls 
252  * 2.60/3.51/3.51/4.00  NT351SP5        comctl32 dll
253  * 2.xx/1.00/0.00/4.00  Win95           system files
254  * x.xx/4.00/0.00/4.00  Win95           most applications
255  * 3.10/4.00/0.00/4.00  Win98           notepad
256  * x.xx/5.00/5.00/4.00  Win98           system dlls
257  * x.xx/4.00/4.00/4.00  NT 4            most apps
258  * 5.12/5.00/5.00/4.00  NT4+IE5         comctl32.dll
259  * 5.12/5.00/5.00/4.00  Win98           calc
260  * x.xx/5.00/5.00/4.00  win95/win98/NT4 IE5 files
261  */
262 DWORD VERSION_GetLinkedDllVersion(PDB *pdb)
263 {
264         WINE_MODREF *wm;
265         DWORD WinVersion = NB_WINDOWS_VERSIONS;
266         PIMAGE_OPTIONAL_HEADER ophd;
267
268         if (!pdb->exe_modref)
269         {
270           if (!pdb->modref_list)
271             return WIN31;
272
273           /* FIXME: The above condition will never trigger, since all our
274            * standard dlls load their win32 equivalents. We have usually at
275            * this point: kernel32.dll and ntdll.dll.
276            */
277           return WIN95;
278         }
279         /* First check the native dlls provided. These have to be
280         from one windows version */
281         for ( wm = pdb->modref_list; wm; wm=wm->next )
282         {
283           ophd = &(PE_HEADER(wm->module)->OptionalHeader);
284
285           TRACE("%s: %02x.%02x/%02x.%02x/%02x.%02x/%02x.%02x\n",
286             wm->modname,
287             ophd->MajorLinkerVersion, ophd->MinorLinkerVersion,
288             ophd->MajorOperatingSystemVersion, ophd->MinorOperatingSystemVersion,
289             ophd->MajorImageVersion, ophd->MinorImageVersion,
290             ophd->MajorSubsystemVersion, ophd->MinorSubsystemVersion);
291
292           /* test if it is an external (native) dll */
293           if (!(wm->flags & WINE_MODREF_INTERNAL))
294           {
295             int i;
296             for (i = 0; special_dlls[i]; i++)
297             {
298               /* test if it is a special dll */
299               if (!strncasecmp(wm->modname, special_dlls[i], strlen(special_dlls[i]) ))
300               {
301                 DWORD DllVersion = VERSION_GetSystemDLLVersion(wm->module);
302                 if (WinVersion == NB_WINDOWS_VERSIONS) 
303                   WinVersion = DllVersion;
304                 else {
305                   if (WinVersion != DllVersion) {
306                     ERR("You mixed system dlls from different windows versions! Expect a crash! (%s: expected version '%s', but is '%s')\n",
307                         wm->modname,
308                         VersionData[WinVersion].getVersionEx.szCSDVersion,
309                         VersionData[DllVersion].getVersionEx.szCSDVersion);
310                     return WIN31; /* this may let the exe exiting */
311                   }
312                 }
313                 break;
314               }
315             }
316           }
317         }
318         
319         if(WinVersion != NB_WINDOWS_VERSIONS) return WinVersion;
320         
321         /* we are using no external system dlls, look at the exe */
322         ophd = &(PE_HEADER(pdb->exe_modref->module)->OptionalHeader);
323         
324         TRACE("-%s: %02x.%02x/%02x.%02x/%02x.%02x/%02x.%02x\n",
325             pdb->exe_modref->modname,
326             ophd->MajorLinkerVersion, ophd->MinorLinkerVersion,
327             ophd->MajorOperatingSystemVersion, ophd->MinorOperatingSystemVersion,
328             ophd->MajorImageVersion, ophd->MinorImageVersion,
329             ophd->MajorSubsystemVersion, ophd->MinorSubsystemVersion);
330
331         /* special nt 3.51 */
332         if (3 == ophd->MajorOperatingSystemVersion && 51 == ophd->MinorOperatingSystemVersion)
333         {
334             return NT351;
335         }
336
337         /* the MajorSubsystemVersion is the only usable sign */
338         if (ophd->MajorSubsystemVersion < 4)
339         {
340           if ( ophd->MajorOperatingSystemVersion == 1 
341             && ophd->MinorOperatingSystemVersion == 0)
342           {
343             return WIN31; /* win32s */
344           }
345           
346           if (ophd->Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI)
347             return NT351; /* FIXME: NT 3.1, not tested */
348           else
349             return WIN95;
350         }       
351
352         return WIN95;
353 }
354
355 /**********************************************************************
356  *         VERSION_GetVersion
357  *
358  * WARNING !!!
359  * Don't call this function too early during the Wine init,
360  * as pdb->exe_modref (required by VERSION_GetImageVersion()) might still
361  * be NULL in such cases, which causes the winver to ALWAYS be detected
362  * as WIN31.
363  * And as we cache the winver once it has been determined, this is bad.
364  * This can happen much easier than you might think, as this function
365  * is called by EVERY GetVersion*() API !
366  *
367  */
368 static WINDOWS_VERSION VERSION_GetVersion(void)
369 {
370         static WORD winver = 0xffff;
371
372         if (versionForced) /* user has overridden any sensible checks */
373           return defaultWinVersion;
374
375         if (winver == 0xffff) /* to be determined */ {
376           WINDOWS_VERSION retver = VERSION_GetLinkedDllVersion( PROCESS_Current() );
377
378           if (retver != WIN31) winver = retver;
379           return retver;
380         }
381         return winver;
382 }
383
384
385 /***********************************************************************
386  *         GetVersion16   (KERNEL.3)
387  */
388 LONG WINAPI GetVersion16(void)
389 {
390     WINDOWS_VERSION ver = VERSION_GetVersion();
391     return VersionData[ver].getVersion16;
392 }
393
394
395 /***********************************************************************
396  *         GetVersion   (KERNEL32.427)
397  */
398 LONG WINAPI GetVersion(void)
399 {
400     WINDOWS_VERSION ver = VERSION_GetVersion();
401     return VersionData[ver].getVersion32;
402 }
403
404
405 /***********************************************************************
406  *         GetVersionEx16   (KERNEL.149)
407  */
408 BOOL16 WINAPI GetVersionEx16(OSVERSIONINFO16 *v)
409 {
410     WINDOWS_VERSION ver = VERSION_GetVersion();
411     if (v->dwOSVersionInfoSize != sizeof(OSVERSIONINFO16))
412     {
413         WARN("wrong OSVERSIONINFO size from app");
414         SetLastError(ERROR_INSUFFICIENT_BUFFER);
415         return FALSE;
416     }
417     v->dwMajorVersion = VersionData[ver].getVersionEx.dwMajorVersion;
418     v->dwMinorVersion = VersionData[ver].getVersionEx.dwMinorVersion;
419     v->dwBuildNumber  = VersionData[ver].getVersionEx.dwBuildNumber;
420     v->dwPlatformId   = VersionData[ver].getVersionEx.dwPlatformId;
421     strcpy( v->szCSDVersion, VersionData[ver].getVersionEx.szCSDVersion );
422     return TRUE;
423 }
424
425
426 /***********************************************************************
427  *         GetVersionExA   (KERNEL32.428)
428  */
429 BOOL WINAPI GetVersionExA(OSVERSIONINFOA *v)
430 {
431     WINDOWS_VERSION ver = VERSION_GetVersion();
432     if (v->dwOSVersionInfoSize != sizeof(OSVERSIONINFOA))
433     {
434         WARN("wrong OSVERSIONINFO size from app (got: %ld, expected: %d)",
435                         v->dwOSVersionInfoSize, sizeof(OSVERSIONINFOA));
436         SetLastError(ERROR_INSUFFICIENT_BUFFER);
437         return FALSE;
438     }
439     v->dwMajorVersion = VersionData[ver].getVersionEx.dwMajorVersion;
440     v->dwMinorVersion = VersionData[ver].getVersionEx.dwMinorVersion;
441     v->dwBuildNumber  = VersionData[ver].getVersionEx.dwBuildNumber;
442     v->dwPlatformId   = VersionData[ver].getVersionEx.dwPlatformId;
443     strcpy( v->szCSDVersion, VersionData[ver].getVersionEx.szCSDVersion );
444     return TRUE;
445 }
446
447
448 /***********************************************************************
449  *         GetVersionExW   (KERNEL32.429)
450  */
451 BOOL WINAPI GetVersionExW(OSVERSIONINFOW *v)
452 {
453     WINDOWS_VERSION ver = VERSION_GetVersion();
454
455     if (v->dwOSVersionInfoSize!=sizeof(OSVERSIONINFOW))
456     {
457         WARN("wrong OSVERSIONINFO size from app (got: %ld, expected: %d)",
458                         v->dwOSVersionInfoSize, sizeof(OSVERSIONINFOW));
459         SetLastError(ERROR_INSUFFICIENT_BUFFER);
460         return FALSE;
461     }
462     v->dwMajorVersion = VersionData[ver].getVersionEx.dwMajorVersion;
463     v->dwMinorVersion = VersionData[ver].getVersionEx.dwMinorVersion;
464     v->dwBuildNumber  = VersionData[ver].getVersionEx.dwBuildNumber;
465     v->dwPlatformId   = VersionData[ver].getVersionEx.dwPlatformId;
466     MultiByteToWideChar( CP_ACP, 0, VersionData[ver].getVersionEx.szCSDVersion, -1,
467                          v->szCSDVersion, sizeof(v->szCSDVersion)/sizeof(WCHAR) );
468     return TRUE;
469 }
470
471
472 /***********************************************************************
473  *          GetWinFlags   (KERNEL.132)
474  */
475 DWORD WINAPI GetWinFlags16(void)
476 {
477   static const long cpuflags[5] =
478     { WF_CPU086, WF_CPU186, WF_CPU286, WF_CPU386, WF_CPU486 };
479   SYSTEM_INFO si;
480   OSVERSIONINFOA ovi;
481   DWORD result;
482
483   GetSystemInfo(&si);
484
485   /* There doesn't seem to be any Pentium flag.  */
486   result = cpuflags[min(si.wProcessorLevel, 4)] | WF_ENHANCED | WF_PMODE | WF_80x87 | WF_PAGING;
487   if (si.wProcessorLevel >= 4) result |= WF_HASCPUID;
488   ovi.dwOSVersionInfoSize = sizeof(ovi);
489   GetVersionExA(&ovi);
490   if (ovi.dwPlatformId == VER_PLATFORM_WIN32_NT)
491       result |= WF_WIN32WOW; /* undocumented WF_WINNT */
492   return result;
493 }
494
495
496 /***********************************************************************
497  *          GetWinDebugInfo   (KERNEL.355)
498  */
499 BOOL16 WINAPI GetWinDebugInfo16(WINDEBUGINFO *lpwdi, UINT16 flags)
500 {
501     FIXME("(%8lx,%d): stub returning 0\n",
502           (unsigned long)lpwdi, flags);
503     /* 0 means not in debugging mode/version */
504     /* Can this type of debugging be used in wine ? */
505     /* Constants: WDI_OPTIONS WDI_FILTER WDI_ALLOCBREAK */
506     return 0;
507 }
508
509
510 /***********************************************************************
511  *          SetWinDebugInfo   (KERNEL.356)
512  */
513 BOOL16 WINAPI SetWinDebugInfo16(WINDEBUGINFO *lpwdi)
514 {
515     FIXME("(%8lx): stub returning 0\n", (unsigned long)lpwdi);
516     /* 0 means not in debugging mode/version */
517     /* Can this type of debugging be used in wine ? */
518     /* Constants: WDI_OPTIONS WDI_FILTER WDI_ALLOCBREAK */
519     return 0;
520 }
521
522
523 /***********************************************************************
524  *           DebugFillBuffer                    (KERNEL.329)
525  *
526  * TODO:
527  * Should fill lpBuffer only if DBO_BUFFERFILL has been set by SetWinDebugInfo()
528  */
529 void WINAPI DebugFillBuffer(LPSTR lpBuffer, WORD wBytes)
530 {
531         memset(lpBuffer, DBGFILL_BUFFER, wBytes);
532 }
533
534 /***********************************************************************
535  *           DiagQuery                          (KERNEL.339)
536  *
537  * returns TRUE if Win called with "/b" (bootlog.txt)
538  */
539 BOOL16 WINAPI DiagQuery16()
540 {
541         /* perhaps implement a Wine "/b" command line flag sometime ? */
542         return FALSE;
543 }
544
545 /***********************************************************************
546  *           DiagOutput                         (KERNEL.340)
547  *
548  * writes a debug string into <windir>\bootlog.txt
549  */
550 void WINAPI DiagOutput16(LPCSTR str)
551 {
552         /* FIXME */
553         DPRINTF("DIAGOUTPUT:%s\n", debugstr_a(str));
554 }