Bug fix.
[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 "winbase.h"
13 #include "winuser.h"
14 #include "wine/winbase16.h"
15 #include "process.h"
16 #include "options.h"
17 #include "debug.h"
18 #include "neexe.h"
19 #include "winversion.h"
20
21 DEFAULT_DEBUG_CHANNEL(ver)
22
23 typedef struct
24 {
25     LONG             getVersion16; 
26     LONG             getVersion32;
27     OSVERSIONINFOA getVersionEx;
28 } VERSION_DATA;
29
30
31 /* FIXME: compare values below with original and fix */
32 static VERSION_DATA VersionData[NB_WINDOWS_VERSIONS] =
33 {
34     /* WIN31 */
35     {
36         MAKELONG( 0x0a03, 0x0616 ), /* DOS 6.22 */
37         MAKELONG( 0x0a03, 0x8000 ),
38         {
39             sizeof(OSVERSIONINFOA), 3, 10, 0,
40             VER_PLATFORM_WIN32s, "Win32s 1.3" 
41         }
42     },
43     /* WIN95 */
44     {
45         0x07005F03,
46         0xC0000004,
47         {
48             sizeof(OSVERSIONINFOA), 4, 0, 0x40003B6,
49             VER_PLATFORM_WIN32_WINDOWS, "Win95"
50         }
51     },
52     /* NT351 */
53     {
54         0x05000A03,
55         0x04213303,
56         {
57             sizeof(OSVERSIONINFOA), 3, 51, 0x421,
58             VER_PLATFORM_WIN32_NT, "Service Pack 2"
59         }
60     },
61     /* NT40 */
62     {
63         0x05000A03,
64         0x05650004,
65         {
66             sizeof(OSVERSIONINFOA), 4, 0, 0x565,
67             VER_PLATFORM_WIN32_NT, "Service Pack 3"
68         }
69     }
70 };
71
72 static const char *WinVersionNames[NB_WINDOWS_VERSIONS] =
73 {
74     "win31",
75     "win95",
76     "nt351",
77     "nt40"
78 };
79
80 /* the current version has not been autodetected but forced via cmdline */
81 static BOOL versionForced = FALSE;
82 static WINDOWS_VERSION defaultWinVersion = WIN31;
83
84
85 /**********************************************************************
86  *         VERSION_ParseWinVersion
87  */
88 void VERSION_ParseWinVersion( const char *arg )
89 {
90     int i;
91     for (i = 0; i < NB_WINDOWS_VERSIONS; i++)
92     {
93         if (!strcmp( WinVersionNames[i], arg ))
94         {
95             defaultWinVersion = (WINDOWS_VERSION)i;
96             versionForced = TRUE;
97             return;
98         }
99     }
100     MSG("Invalid winver value '%s' specified.\n", arg );
101     MSG("Valid versions are:" );
102     for (i = 0; i < NB_WINDOWS_VERSIONS; i++)
103         MSG(" '%s'%c", WinVersionNames[i],
104             (i == NB_WINDOWS_VERSIONS - 1) ? '\n' : ',' );
105 }
106
107
108 /**********************************************************************
109  *         VERSION_ParseDosVersion
110  */
111 void VERSION_ParseDosVersion( const char *arg )
112 {
113     int hi, lo;
114     if (sscanf( arg, "%d.%d", &hi, &lo ) == 2)
115     {
116         VersionData[WIN31].getVersion16 =
117             MAKELONG(LOWORD(VersionData[WIN31].getVersion16),
118                      (hi<<8) + lo);
119     }
120     else
121         fprintf( stderr, "-dosver: Wrong version format. Use \"-dosver x.xx\"\n");
122 }
123
124
125 WINDOWS_VERSION VERSION_GetImageVersion(PDB *pdb)
126 {
127     PIMAGE_NT_HEADERS peheader;
128
129         if (!pdb->exe_modref)
130         {
131                 /* HACK: if we have loaded a PE image into this address space,
132                  * we are probably using thunks, so Win95 is our best bet
133                  */
134                 if (pdb->modref_list)
135                         return WIN95;
136                 /* FIXME: hmm, do anything else ?
137                    TDB.version doesn't help here
138                    as it always holds version 3.10 */
139                         return WIN31;
140         }
141         else
142         {
143                 peheader = PE_HEADER(pdb->exe_modref->module);
144 #define OPTHD peheader->OptionalHeader
145
146                 TRACE(ver, "%02x.%02x/%02x.%02x/%02x.%02x/%02x.%02x\n",
147                           OPTHD.MajorLinkerVersion,
148                           OPTHD.MinorLinkerVersion,
149                           OPTHD.MajorOperatingSystemVersion,
150                           OPTHD.MinorOperatingSystemVersion,
151                           OPTHD.MajorImageVersion,
152                           OPTHD.MinorImageVersion,
153                           OPTHD.MajorSubsystemVersion,
154                           OPTHD.MinorSubsystemVersion);
155
156         switch (OPTHD.MajorSubsystemVersion)
157                 {
158                         case 4:
159                                 switch (OPTHD.MajorOperatingSystemVersion)
160                                 {
161                                         case 5:
162                                                 return NT40; /* FIXME: this is NT 5, isn't it ? */
163                                         case 4:
164                                                 if ((OPTHD.MajorImageVersion == 0) &&
165                                                         (OPTHD.SectionAlignment == 4096))
166                                                         return WIN95;
167                                                 else
168                                                         return NT40;
169                                         case 1:
170                                                 return WIN95;
171                                         default:
172                                                 return WIN95; /* FIXME ? */
173                                 }
174                         case 3:
175                                 /* 3.1x versions */
176                                 if (OPTHD.MinorSubsystemVersion <= 11)
177                                 {
178                                         if (OPTHD.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI)
179                                                 return NT351; /* FIXME: NT 3.1 */
180                                         else
181                                                 return WIN31;
182                                 }
183                                 else
184                                 {
185                                         /* NT 3.51 */
186                                         if (OPTHD.MinorSubsystemVersion == 50)
187                                                 return NT351;
188                                         else
189                                         if (OPTHD.MinorSubsystemVersion == 51)
190                                                 return NT351;
191                                 }
192                         default:
193                                 if (OPTHD.MajorSubsystemVersion)
194                                 ERR(ver,"unknown subsystem version: %04x.%04x, please report.\n",
195                                         OPTHD.MajorSubsystemVersion,
196                                         OPTHD.MinorSubsystemVersion );
197                                 return defaultWinVersion;
198                 }
199 #undef OPTHD
200         }
201 }
202
203
204 /**********************************************************************
205  *
206  * Check the version of COMDLG32, SHELL32, COMCTL32 and CTL3D32.
207  * Not functional yet.
208  */
209 DWORD VERSION_GetLinkedDllVersion(PDB *pdb)
210 {
211         WINE_MODREF *wm;
212     WORD VersionCounter[NB_WINDOWS_VERSIONS];
213
214     memset(VersionCounter, 0, sizeof(VersionCounter));
215
216         VersionCounter[WIN95] = 1;
217         for ( wm = PROCESS_Current()->modref_list; wm; wm=wm->next ) {
218             if (!(lstrncmpiA(wm->modname, "CTL3D32", 7)))
219                         VersionCounter[WIN95]++;
220         }
221
222         return MAKELONG(WIN95, VersionCounter[WIN95]);
223 }
224
225
226 /**********************************************************************
227  *         VERSION_GetVersion
228  *
229  * Some version data:
230  * linker/OS/image/subsys       Name                    Intended for
231  *
232  * 2.39/1.00/0.00/3.10          freecell.exe    Win32s (any version)
233  * 2.55/1.00/0.00/4.00          acrord32.exe    Win32s, Win95 supported (?)
234  * 
235  * 2.50/1.00/4.00/4.00          winhlp32.exe    Win32s 1.30
236  * 4.20/4.00/1.00/4.00          Asuslm.exe              Win95 (Aaargh !)
237  * 5.12/4.00/1.07/4.00      clikfixi.exe    NT 4 (service pack files)
238  * 3.10/4.00/4.00/4.00          PLUMBING.EXE    NT
239  * ?.??/4.00/97.01/4.00         sse.exe                 huh ?? (damn crackerz ;)
240  * 5.12/5.00/5.00/4.00          comctl32.dll    NT4 / IE 5.0
241  * 6.00/5.00/5.00/4.00                                          NT 4 driver update (strange numbers)
242  * 
243  * Common versions:
244  * x.xx/1.00/0.00/3.10                                          Win32s (any version ?)
245  * 2.xx/1.00/0.00/4.00                                          Win95 (Microsoft/system files) 
246  * x.xx/4.00/0.00/4.00                                          Win95 (most applications !)
247  * x.xx/4.00/4.00/4.00                                          NT 4 (most apps)
248  * x.xx/5.00/5.00/4.00                                          NT 4 newer files / NT 5 ??
249  */
250 WINDOWS_VERSION VERSION_GetVersion(void)
251 {
252     PDB *pdb = PROCESS_Current();
253     DWORD DllVer;
254
255     if (versionForced) /* user has overridden any sensible checks */
256         return defaultWinVersion;
257
258     if (pdb->winver == 0xffff) /* to be determined */
259         {
260                 DllVer = 0/*VERSION_GetLinkedDllVersion(pdb)*/;
261                 if (HIWORD(DllVer) > 1)
262                         pdb->winver = LOWORD(DllVer);
263             else
264                         pdb->winver = VERSION_GetImageVersion(pdb);
265         }
266
267         return pdb->winver;
268 }
269
270
271 /**********************************************************************
272  *         VERSION_GetVersionName
273  */
274 char *VERSION_GetVersionName()
275 {
276   WINDOWS_VERSION ver = VERSION_GetVersion();
277   switch(ver)
278     {
279     case WIN31:
280       return "Windows 3.1";
281     case WIN95:  
282       return "Windows 95";
283     case NT351:
284       return "Windows NT 3.51";
285     case NT40:
286       return "Windows NT 4.0";
287     default:
288       FIXME(ver,"Windows version %d not named",ver);
289       return "Windows <Unknown>";
290     }
291 }
292
293 /***********************************************************************
294  *         GetVersion16   (KERNEL.3)
295  */
296 LONG WINAPI GetVersion16(void)
297 {
298     WINDOWS_VERSION ver = VERSION_GetVersion();
299     return VersionData[ver].getVersion16;
300 }
301
302
303 /***********************************************************************
304  *         GetVersion32   (KERNEL32.427)
305  */
306 LONG WINAPI GetVersion(void)
307 {
308     WINDOWS_VERSION ver = VERSION_GetVersion();
309     return VersionData[ver].getVersion32;
310 }
311
312
313 /***********************************************************************
314  *         GetVersionEx16   (KERNEL.149)
315  */
316 BOOL16 WINAPI GetVersionEx16(OSVERSIONINFO16 *v)
317 {
318     WINDOWS_VERSION ver = VERSION_GetVersion();
319     if (v->dwOSVersionInfoSize != sizeof(OSVERSIONINFO16))
320     {
321         WARN(ver,"wrong OSVERSIONINFO size from app");
322         return FALSE;
323     }
324     v->dwMajorVersion = VersionData[ver].getVersionEx.dwMajorVersion;
325     v->dwMinorVersion = VersionData[ver].getVersionEx.dwMinorVersion;
326     v->dwBuildNumber  = VersionData[ver].getVersionEx.dwBuildNumber;
327     v->dwPlatformId   = VersionData[ver].getVersionEx.dwPlatformId;
328     strcpy( v->szCSDVersion, VersionData[ver].getVersionEx.szCSDVersion );
329     return TRUE;
330 }
331
332
333 /***********************************************************************
334  *         GetVersionEx32A   (KERNEL32.428)
335  */
336 BOOL WINAPI GetVersionExA(OSVERSIONINFOA *v)
337 {
338     WINDOWS_VERSION ver = VERSION_GetVersion();
339     if (v->dwOSVersionInfoSize != sizeof(OSVERSIONINFOA))
340     {
341         WARN(ver,"wrong OSVERSIONINFO size from app");
342         return FALSE;
343     }
344     v->dwMajorVersion = VersionData[ver].getVersionEx.dwMajorVersion;
345     v->dwMinorVersion = VersionData[ver].getVersionEx.dwMinorVersion;
346     v->dwBuildNumber  = VersionData[ver].getVersionEx.dwBuildNumber;
347     v->dwPlatformId   = VersionData[ver].getVersionEx.dwPlatformId;
348     strcpy( v->szCSDVersion, VersionData[ver].getVersionEx.szCSDVersion );
349     return TRUE;
350 }
351
352
353 /***********************************************************************
354  *         GetVersionEx32W   (KERNEL32.429)
355  */
356 BOOL WINAPI GetVersionExW(OSVERSIONINFOW *v)
357 {
358     WINDOWS_VERSION ver = VERSION_GetVersion();
359
360     if (v->dwOSVersionInfoSize!=sizeof(OSVERSIONINFOW))
361     {
362         WARN(ver,"wrong OSVERSIONINFO size from app");
363         return FALSE;
364     }
365     v->dwMajorVersion = VersionData[ver].getVersionEx.dwMajorVersion;
366     v->dwMinorVersion = VersionData[ver].getVersionEx.dwMinorVersion;
367     v->dwBuildNumber  = VersionData[ver].getVersionEx.dwBuildNumber;
368     v->dwPlatformId   = VersionData[ver].getVersionEx.dwPlatformId;
369     lstrcpyAtoW( v->szCSDVersion, VersionData[ver].getVersionEx.szCSDVersion );
370     return TRUE;
371 }
372
373
374 /***********************************************************************
375  *          GetWinFlags   (KERNEL.132)
376  */
377 DWORD WINAPI GetWinFlags16(void)
378 {
379   static const long cpuflags[5] =
380     { WF_CPU086, WF_CPU186, WF_CPU286, WF_CPU386, WF_CPU486 };
381   SYSTEM_INFO si;
382   OSVERSIONINFOA ovi;
383   DWORD result;
384
385   GetSystemInfo(&si);
386
387   /* There doesn't seem to be any Pentium flag.  */
388   result = cpuflags[MIN (si.wProcessorLevel, 4)];
389
390   switch(Options.mode)
391   {
392   case MODE_STANDARD:
393       result |= WF_STANDARD | WF_PMODE | WF_80x87;
394       break;
395
396   case MODE_ENHANCED:
397       result |= WF_ENHANCED | WF_PMODE | WF_80x87 | WF_PAGING;
398       break;
399
400   default:
401       ERR(ver, "Unknown mode set? This shouldn't happen. Check GetWinFlags()!\n");
402       break;
403   }
404   if (si.wProcessorLevel >= 4) result |= WF_HASCPUID;
405   ovi.dwOSVersionInfoSize = sizeof(ovi);
406   GetVersionExA(&ovi);
407   if (ovi.dwPlatformId == VER_PLATFORM_WIN32_NT)
408       result |= WF_WIN32WOW; /* undocumented WF_WINNT */
409   return result;
410 }
411
412
413 /***********************************************************************
414  *          GetWinDebugInfo   (KERNEL.355)
415  */
416 BOOL16 WINAPI GetWinDebugInfo16(WINDEBUGINFO *lpwdi, UINT16 flags)
417 {
418     FIXME(ver, "(%8lx,%d): stub returning 0\n",
419           (unsigned long)lpwdi, flags);
420     /* 0 means not in debugging mode/version */
421     /* Can this type of debugging be used in wine ? */
422     /* Constants: WDI_OPTIONS WDI_FILTER WDI_ALLOCBREAK */
423     return 0;
424 }
425
426
427 /***********************************************************************
428  *          SetWinDebugInfo   (KERNEL.356)
429  */
430 BOOL16 WINAPI SetWinDebugInfo16(WINDEBUGINFO *lpwdi)
431 {
432     FIXME(ver, "(%8lx): stub returning 0\n", (unsigned long)lpwdi);
433     /* 0 means not in debugging mode/version */
434     /* Can this type of debugging be used in wine ? */
435     /* Constants: WDI_OPTIONS WDI_FILTER WDI_ALLOCBREAK */
436     return 0;
437 }
438
439
440 /***********************************************************************
441  *           DebugFillBuffer                    (KERNEL.329)
442  *
443  * TODO:
444  * Should fill lpBuffer only if DBO_BUFFERFILL has been set by SetWinDebugInfo()
445  */
446 void WINAPI DebugFillBuffer(LPSTR lpBuffer, WORD wBytes)
447 {
448         memset(lpBuffer, DBGFILL_BUFFER, wBytes);
449 }
450
451 /***********************************************************************
452  *           DiagQuery                          (KERNEL.339)
453  *
454  * returns TRUE if Win called with "/b" (bootlog.txt)
455  */
456 BOOL16 WINAPI DiagQuery16()
457 {
458         /* perhaps implement a Wine "/b" command line flag sometime ? */
459         return FALSE;
460 }
461
462 /***********************************************************************
463  *           DiagOutput                         (KERNEL.340)
464  *
465  * writes a debug string into <windir>\bootlog.txt
466  */
467 void WINAPI DiagOutput16(LPCSTR str)
468 {
469         /* FIXME */
470         DPRINTF("DIAGOUTPUT:%s\n", debugstr_a(str));
471 }
472
473 /***********************************************************************
474  *           OaBuildVersion           [OLEAUT32.170]
475  */
476 UINT WINAPI OaBuildVersion()
477 {
478     WINDOWS_VERSION ver = VERSION_GetVersion();
479
480     FIXME(ver, "Please report to a.mohr@mailto.de if you get version error messages !\n");
481     switch(VersionData[ver].getVersion32)
482     {
483         case 0x80000a03: /* Win 3.1 */
484                 return 0x140fd1; /* from Win32s 1.1e */
485         case 0xc0000004: /* Win 95 */
486                 return 0x1e10a9; /* some older version: 0x0a0bd3 */
487         case 0x04213303: /* NT 3.51 */
488                 FIXME(ver, "NT 3.51 version value unknown !\n");
489                 return 0x1e10a9; /* value borrowed from Win95 */
490         case 0x05650004: /* NT 4.0 */
491                 return 0x141016;
492         default:
493                 return 0x0;
494     }
495 }
496 /***********************************************************************
497  *        VERSION_OsIsUnicode   [internal]
498  *
499  * NOTES
500  *   some functions getting sometimes LPSTR sometimes LPWSTR...
501  *
502  */
503 BOOL VERSION_OsIsUnicode(void)
504 {
505     switch(VERSION_GetVersion())
506     {
507     case NT351:
508     case NT40:
509         return TRUE;
510     default:
511         return FALSE;
512     }
513 }