Documentation 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         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(void)
263 {
264         WINE_MODREF *wm;
265         DWORD WinVersion = NB_WINDOWS_VERSIONS;
266         PIMAGE_OPTIONAL_HEADER ophd;
267
268         /* First check the native dlls provided. These have to be
269         from one windows version */
270         for ( wm = MODULE_modref_list; wm; wm=wm->next )
271         {
272           ophd = &(PE_HEADER(wm->module)->OptionalHeader);
273
274           TRACE("%s: %02x.%02x/%02x.%02x/%02x.%02x/%02x.%02x\n",
275             wm->modname,
276             ophd->MajorLinkerVersion, ophd->MinorLinkerVersion,
277             ophd->MajorOperatingSystemVersion, ophd->MinorOperatingSystemVersion,
278             ophd->MajorImageVersion, ophd->MinorImageVersion,
279             ophd->MajorSubsystemVersion, ophd->MinorSubsystemVersion);
280
281           /* test if it is an external (native) dll */
282           if (!(wm->flags & WINE_MODREF_INTERNAL))
283           {
284             int i;
285             for (i = 0; special_dlls[i]; i++)
286             {
287               /* test if it is a special dll */
288               if (!strncasecmp(wm->modname, special_dlls[i], strlen(special_dlls[i]) ))
289               {
290                 DWORD DllVersion = VERSION_GetSystemDLLVersion(wm->module);
291                 if (WinVersion == NB_WINDOWS_VERSIONS) 
292                   WinVersion = DllVersion;
293                 else {
294                   if (WinVersion != DllVersion) {
295                     ERR("You mixed system dlls from different windows versions! Expect a crash! (%s: expected version '%s', but is '%s')\n",
296                         wm->modname,
297                         VersionData[WinVersion].getVersionEx.szCSDVersion,
298                         VersionData[DllVersion].getVersionEx.szCSDVersion);
299                     return WIN31; /* this may let the exe exiting */
300                   }
301                 }
302                 break;
303               }
304             }
305           }
306         }
307         
308         if(WinVersion != NB_WINDOWS_VERSIONS) return WinVersion;
309         
310         /* we are using no external system dlls, look at the exe */
311         ophd = &(PE_HEADER(GetModuleHandleA(NULL))->OptionalHeader);
312         
313         TRACE("%02x.%02x/%02x.%02x/%02x.%02x/%02x.%02x\n",
314             ophd->MajorLinkerVersion, ophd->MinorLinkerVersion,
315             ophd->MajorOperatingSystemVersion, ophd->MinorOperatingSystemVersion,
316             ophd->MajorImageVersion, ophd->MinorImageVersion,
317             ophd->MajorSubsystemVersion, ophd->MinorSubsystemVersion);
318
319         /* special nt 3.51 */
320         if (3 == ophd->MajorOperatingSystemVersion && 51 == ophd->MinorOperatingSystemVersion)
321         {
322             return NT351;
323         }
324
325         /* the MajorSubsystemVersion is the only usable sign */
326         if (ophd->MajorSubsystemVersion < 4)
327         {
328           if ( ophd->MajorOperatingSystemVersion == 1 
329             && ophd->MinorOperatingSystemVersion == 0)
330           {
331             return WIN31; /* win32s */
332           }
333           
334           if (ophd->Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI)
335             return NT351; /* FIXME: NT 3.1, not tested */
336           else
337             return WIN95;
338         }       
339
340         return WIN95;
341 }
342
343 /**********************************************************************
344  *         VERSION_GetVersion
345  *
346  * WARNING !!!
347  * Don't call this function too early during the Wine init,
348  * as pdb->exe_modref (required by VERSION_GetImageVersion()) might still
349  * be NULL in such cases, which causes the winver to ALWAYS be detected
350  * as WIN31.
351  * And as we cache the winver once it has been determined, this is bad.
352  * This can happen much easier than you might think, as this function
353  * is called by EVERY GetVersion*() API !
354  *
355  */
356 static WINDOWS_VERSION VERSION_GetVersion(void)
357 {
358         static WORD winver = 0xffff;
359
360         if (versionForced) /* user has overridden any sensible checks */
361           return defaultWinVersion;
362
363         if (winver == 0xffff) /* to be determined */ {
364           WINDOWS_VERSION retver = VERSION_GetLinkedDllVersion();
365
366           if (retver != WIN31) winver = retver;
367           return retver;
368         }
369         return winver;
370 }
371
372
373 /***********************************************************************
374  *         GetVersion   (KERNEL.3)
375  */
376 LONG WINAPI GetVersion16(void)
377 {
378     WINDOWS_VERSION ver = VERSION_GetVersion();
379     return VersionData[ver].getVersion16;
380 }
381
382
383 /***********************************************************************
384  *         GetVersion   (KERNEL32.@)
385  */
386 LONG WINAPI GetVersion(void)
387 {
388     WINDOWS_VERSION ver = VERSION_GetVersion();
389     return VersionData[ver].getVersion32;
390 }
391
392
393 /***********************************************************************
394  *         GetVersionEx   (KERNEL.149)
395  */
396 BOOL16 WINAPI GetVersionEx16(OSVERSIONINFO16 *v)
397 {
398     WINDOWS_VERSION ver = VERSION_GetVersion();
399     if (v->dwOSVersionInfoSize < sizeof(OSVERSIONINFO16))
400     {
401         WARN("wrong OSVERSIONINFO size from app\n");
402         SetLastError(ERROR_INSUFFICIENT_BUFFER);
403         return FALSE;
404     }
405     v->dwMajorVersion = VersionData[ver].getVersionEx.dwMajorVersion;
406     v->dwMinorVersion = VersionData[ver].getVersionEx.dwMinorVersion;
407     v->dwBuildNumber  = VersionData[ver].getVersionEx.dwBuildNumber;
408     v->dwPlatformId   = VersionData[ver].getVersionEx.dwPlatformId;
409     strcpy( v->szCSDVersion, VersionData[ver].getVersionEx.szCSDVersion );
410     return TRUE;
411 }
412
413
414 /***********************************************************************
415  *         GetVersionExA   (KERNEL32.@)
416  */
417 BOOL WINAPI GetVersionExA(OSVERSIONINFOA *v)
418 {
419     WINDOWS_VERSION ver = VERSION_GetVersion();
420     if (v->dwOSVersionInfoSize < sizeof(OSVERSIONINFOA))
421     {
422         WARN("wrong OSVERSIONINFO size from app (got: %ld, expected: %d)\n",
423                         v->dwOSVersionInfoSize, sizeof(OSVERSIONINFOA));
424         SetLastError(ERROR_INSUFFICIENT_BUFFER);
425         return FALSE;
426     }
427     v->dwMajorVersion = VersionData[ver].getVersionEx.dwMajorVersion;
428     v->dwMinorVersion = VersionData[ver].getVersionEx.dwMinorVersion;
429     v->dwBuildNumber  = VersionData[ver].getVersionEx.dwBuildNumber;
430     v->dwPlatformId   = VersionData[ver].getVersionEx.dwPlatformId;
431     strcpy( v->szCSDVersion, VersionData[ver].getVersionEx.szCSDVersion );
432     return TRUE;
433 }
434
435
436 /***********************************************************************
437  *         GetVersionExW   (KERNEL32.@)
438  */
439 BOOL WINAPI GetVersionExW(OSVERSIONINFOW *v)
440 {
441     WINDOWS_VERSION ver = VERSION_GetVersion();
442
443     if (v->dwOSVersionInfoSize < sizeof(OSVERSIONINFOW))
444     {
445         WARN("wrong OSVERSIONINFO size from app (got: %ld, expected: %d)\n",
446                         v->dwOSVersionInfoSize, sizeof(OSVERSIONINFOW));
447         SetLastError(ERROR_INSUFFICIENT_BUFFER);
448         return FALSE;
449     }
450     v->dwMajorVersion = VersionData[ver].getVersionEx.dwMajorVersion;
451     v->dwMinorVersion = VersionData[ver].getVersionEx.dwMinorVersion;
452     v->dwBuildNumber  = VersionData[ver].getVersionEx.dwBuildNumber;
453     v->dwPlatformId   = VersionData[ver].getVersionEx.dwPlatformId;
454     MultiByteToWideChar( CP_ACP, 0, VersionData[ver].getVersionEx.szCSDVersion, -1,
455                          v->szCSDVersion, sizeof(v->szCSDVersion)/sizeof(WCHAR) );
456     return TRUE;
457 }
458
459
460 /***********************************************************************
461  *          GetWinFlags   (KERNEL.132)
462  */
463 DWORD WINAPI GetWinFlags16(void)
464 {
465   static const long cpuflags[5] =
466     { WF_CPU086, WF_CPU186, WF_CPU286, WF_CPU386, WF_CPU486 };
467   SYSTEM_INFO si;
468   OSVERSIONINFOA ovi;
469   DWORD result;
470
471   GetSystemInfo(&si);
472
473   /* There doesn't seem to be any Pentium flag.  */
474   result = cpuflags[min(si.wProcessorLevel, 4)] | WF_ENHANCED | WF_PMODE | WF_80x87 | WF_PAGING;
475   if (si.wProcessorLevel >= 4) result |= WF_HASCPUID;
476   ovi.dwOSVersionInfoSize = sizeof(ovi);
477   GetVersionExA(&ovi);
478   if (ovi.dwPlatformId == VER_PLATFORM_WIN32_NT)
479       result |= WF_WIN32WOW; /* undocumented WF_WINNT */
480   return result;
481 }
482
483
484 /***********************************************************************
485  *          GetWinDebugInfo   (KERNEL.355)
486  */
487 BOOL16 WINAPI GetWinDebugInfo16(WINDEBUGINFO *lpwdi, UINT16 flags)
488 {
489     FIXME("(%8lx,%d): stub returning 0\n",
490           (unsigned long)lpwdi, flags);
491     /* 0 means not in debugging mode/version */
492     /* Can this type of debugging be used in wine ? */
493     /* Constants: WDI_OPTIONS WDI_FILTER WDI_ALLOCBREAK */
494     return 0;
495 }
496
497
498 /***********************************************************************
499  *          SetWinDebugInfo   (KERNEL.356)
500  */
501 BOOL16 WINAPI SetWinDebugInfo16(WINDEBUGINFO *lpwdi)
502 {
503     FIXME("(%8lx): stub returning 0\n", (unsigned long)lpwdi);
504     /* 0 means not in debugging mode/version */
505     /* Can this type of debugging be used in wine ? */
506     /* Constants: WDI_OPTIONS WDI_FILTER WDI_ALLOCBREAK */
507     return 0;
508 }
509
510
511 /***********************************************************************
512  *           K329                    (KERNEL.329)
513  *
514  * TODO:
515  * Should fill lpBuffer only if DBO_BUFFERFILL has been set by SetWinDebugInfo()
516  */
517 void WINAPI DebugFillBuffer(LPSTR lpBuffer, WORD wBytes)
518 {
519         memset(lpBuffer, DBGFILL_BUFFER, wBytes);
520 }
521
522 /***********************************************************************
523  *           DiagQuery                          (KERNEL.339)
524  *
525  * returns TRUE if Win called with "/b" (bootlog.txt)
526  */
527 BOOL16 WINAPI DiagQuery16()
528 {
529         /* perhaps implement a Wine "/b" command line flag sometime ? */
530         return FALSE;
531 }
532
533 /***********************************************************************
534  *           DiagOutput                         (KERNEL.340)
535  *
536  * writes a debug string into <windir>\bootlog.txt
537  */
538 void WINAPI DiagOutput16(LPCSTR str)
539 {
540         /* FIXME */
541         DPRINTF("DIAGOUTPUT:%s\n", debugstr_a(str));
542 }