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