Compiler warnings fix.
[wine] / dlls / version / info.c
1 /* 
2  * Implementation of VERSION.DLL - Version Info access
3  * 
4  * Copyright 1996,1997 Marcus Meissner
5  * Copyright 1997 David Cuthbert
6  * Copyright 1999 Ulrich Weigand
7  */
8
9 #include <stdlib.h>
10 #include <string.h>
11
12 #include "winreg.h"
13 #include "winver.h"
14 #include "wine/winestring.h"
15 #include "winerror.h"
16 #include "heap.h"
17 #include "debugtools.h"
18
19 DEFAULT_DEBUG_CHANNEL(ver)
20
21
22 /******************************************************************************
23  *
24  *   This function will print via dprintf[_]ver to stddeb debug info regarding
25  *   the file info structure vffi.
26  *      15-Feb-1998 Dimitrie Paun (dimi@cs.toronto.edu)
27  *      Added this function to clean up the code.
28  *
29  *****************************************************************************/
30 static void print_vffi_debug(VS_FIXEDFILEINFO *vffi)
31 {
32         dbg_decl_str(ver, 1024);
33
34         TRACE(" structversion=%u.%u, fileversion=%u.%u.%u.%u, productversion=%u.%u.%u.%u, flagmask=0x%lx, flags=%s%s%s%s%s%s\n",
35                     HIWORD(vffi->dwStrucVersion),LOWORD(vffi->dwStrucVersion),
36                     HIWORD(vffi->dwFileVersionMS),LOWORD(vffi->dwFileVersionMS),
37                     HIWORD(vffi->dwFileVersionLS),LOWORD(vffi->dwFileVersionLS),
38                     HIWORD(vffi->dwProductVersionMS),LOWORD(vffi->dwProductVersionMS),
39                     HIWORD(vffi->dwProductVersionLS),LOWORD(vffi->dwProductVersionLS),
40                     vffi->dwFileFlagsMask,
41                     (vffi->dwFileFlags & VS_FF_DEBUG) ? "DEBUG," : "",
42                     (vffi->dwFileFlags & VS_FF_PRERELEASE) ? "PRERELEASE," : "",
43                     (vffi->dwFileFlags & VS_FF_PATCHED) ? "PATCHED," : "",
44                     (vffi->dwFileFlags & VS_FF_PRIVATEBUILD) ? "PRIVATEBUILD," : "",
45                     (vffi->dwFileFlags & VS_FF_INFOINFERRED) ? "INFOINFERRED," : "",
46                     (vffi->dwFileFlags & VS_FF_SPECIALBUILD) ? "SPECIALBUILD," : ""
47                     );
48
49         dsprintf(ver," OS=0x%x.0x%x ",
50                 HIWORD(vffi->dwFileOS),
51                 LOWORD(vffi->dwFileOS)
52         );
53         switch (vffi->dwFileOS&0xFFFF0000) {
54         case VOS_DOS:dsprintf(ver,"DOS,");break;
55         case VOS_OS216:dsprintf(ver,"OS/2-16,");break;
56         case VOS_OS232:dsprintf(ver,"OS/2-32,");break;
57         case VOS_NT:dsprintf(ver,"NT,");break;
58         case VOS_UNKNOWN:
59         default:
60                 dsprintf(ver,"UNKNOWN(0x%lx),",vffi->dwFileOS&0xFFFF0000);break;
61         }
62         switch (LOWORD(vffi->dwFileOS)) {
63         case VOS__BASE:dsprintf(ver,"BASE");break;
64         case VOS__WINDOWS16:dsprintf(ver,"WIN16");break;
65         case VOS__WINDOWS32:dsprintf(ver,"WIN32");break;
66         case VOS__PM16:dsprintf(ver,"PM16");break;
67         case VOS__PM32:dsprintf(ver,"PM32");break;
68         default:dsprintf(ver,"UNKNOWN(0x%x)",LOWORD(vffi->dwFileOS));break;
69         }
70         TRACE("(%s)\n", dbg_str(ver));
71
72         dbg_reset_str(ver);
73         switch (vffi->dwFileType) {
74         default:
75         case VFT_UNKNOWN:
76                 dsprintf(ver,"filetype=Unknown(0x%lx)",vffi->dwFileType);
77                 break;
78         case VFT_APP:dsprintf(ver,"filetype=APP,");break;
79         case VFT_DLL:dsprintf(ver,"filetype=DLL,");break;
80         case VFT_DRV:
81                 dsprintf(ver,"filetype=DRV,");
82                 switch(vffi->dwFileSubtype) {
83                 default:
84                 case VFT2_UNKNOWN:
85                         dsprintf(ver,"UNKNOWN(0x%lx)",vffi->dwFileSubtype);
86                         break;
87                 case VFT2_DRV_PRINTER:
88                         dsprintf(ver,"PRINTER");
89                         break;
90                 case VFT2_DRV_KEYBOARD:
91                         dsprintf(ver,"KEYBOARD");
92                         break;
93                 case VFT2_DRV_LANGUAGE:
94                         dsprintf(ver,"LANGUAGE");
95                         break;
96                 case VFT2_DRV_DISPLAY:
97                         dsprintf(ver,"DISPLAY");
98                         break;
99                 case VFT2_DRV_MOUSE:
100                         dsprintf(ver,"MOUSE");
101                         break;
102                 case VFT2_DRV_NETWORK:
103                         dsprintf(ver,"NETWORK");
104                         break;
105                 case VFT2_DRV_SYSTEM:
106                         dsprintf(ver,"SYSTEM");
107                         break;
108                 case VFT2_DRV_INSTALLABLE:
109                         dsprintf(ver,"INSTALLABLE");
110                         break;
111                 case VFT2_DRV_SOUND:
112                         dsprintf(ver,"SOUND");
113                         break;
114                 case VFT2_DRV_COMM:
115                         dsprintf(ver,"COMM");
116                         break;
117                 case VFT2_DRV_INPUTMETHOD:
118                         dsprintf(ver,"INPUTMETHOD");
119                         break;
120                 }
121                 break;
122         case VFT_FONT:
123                 dsprintf(ver,"filetype=FONT.");
124                 switch (vffi->dwFileSubtype) {
125                 default:
126                         dsprintf(ver,"UNKNOWN(0x%lx)",vffi->dwFileSubtype);
127                         break;
128                 case VFT2_FONT_RASTER:dsprintf(ver,"RASTER");break;
129                 case VFT2_FONT_VECTOR:dsprintf(ver,"VECTOR");break;
130                 case VFT2_FONT_TRUETYPE:dsprintf(ver,"TRUETYPE");break;
131                 }
132                 break;
133         case VFT_VXD:dsprintf(ver,"filetype=VXD");break;
134         case VFT_STATIC_LIB:dsprintf(ver,"filetype=STATIC_LIB");break;
135         }
136         TRACE("%s\n", dbg_str(ver));
137
138         TRACE("  filedata=0x%lx.0x%lx\n",
139                     vffi->dwFileDateMS,vffi->dwFileDateLS);
140 }
141
142
143 /***********************************************************************
144  * Version Info Structure
145  */
146
147 typedef struct
148 {
149     WORD  wLength;
150     WORD  wValueLength;
151     CHAR  szKey[1];
152 #if 0   /* variable length structure */
153     /* DWORD aligned */
154     BYTE  Value[];
155     /* DWORD aligned */
156     VS_VERSION_INFO_STRUCT16 Children[];
157 #endif
158 } VS_VERSION_INFO_STRUCT16;
159
160 typedef struct
161 {
162     WORD  wLength;
163     WORD  wValueLength;
164     WORD  bText;
165     WCHAR szKey[1];
166 #if 0   /* variable length structure */
167     /* DWORD aligned */
168     BYTE  Value[];
169     /* DWORD aligned */
170     VS_VERSION_INFO_STRUCT32 Children[];
171 #endif
172 } VS_VERSION_INFO_STRUCT32;
173
174 #define VersionInfoIs16( ver ) \
175     ( ((VS_VERSION_INFO_STRUCT16 *)ver)->szKey[0] >= ' ' )
176
177 #define VersionInfo16_Value( ver )  \
178     (LPBYTE)( ((DWORD)((ver)->szKey) + (lstrlenA((ver)->szKey)+1) + 3) & ~3 )
179 #define VersionInfo32_Value( ver )  \
180     (LPBYTE)( ((DWORD)((ver)->szKey) + 2*(lstrlenW((ver)->szKey)+1) + 3) & ~3 )
181
182 #define VersionInfo16_Children( ver )  \
183     (VS_VERSION_INFO_STRUCT16 *)( VersionInfo16_Value( ver ) + \
184                            ( ( (ver)->wValueLength + 3 ) & ~3 ) )
185 #define VersionInfo32_Children( ver )  \
186     (VS_VERSION_INFO_STRUCT32 *)( VersionInfo32_Value( ver ) + \
187                            ( ( (ver)->wValueLength * \
188                                ((ver)->bText? 2 : 1) + 3 ) & ~3 ) )
189
190 #define VersionInfo16_Next( ver ) \
191     (VS_VERSION_INFO_STRUCT16 *)( (LPBYTE)ver + (((ver)->wLength + 3) & ~3) )
192 #define VersionInfo32_Next( ver ) \
193     (VS_VERSION_INFO_STRUCT32 *)( (LPBYTE)ver + (((ver)->wLength + 3) & ~3) )
194
195 /***********************************************************************
196  *           ConvertVersionInfo32To16        [internal]
197  */
198 void ConvertVersionInfo32To16( VS_VERSION_INFO_STRUCT32 *info32, 
199                                VS_VERSION_INFO_STRUCT16 *info16 )
200 {
201     /* Copy data onto local stack to prevent overwrites */
202     WORD wLength = info32->wLength;
203     WORD wValueLength = info32->wValueLength;
204     WORD bText = info32->bText;
205     LPBYTE lpValue = VersionInfo32_Value( info32 );
206     VS_VERSION_INFO_STRUCT32 *child32 = VersionInfo32_Children( info32 );
207     VS_VERSION_INFO_STRUCT16 *child16;
208
209     TRACE("Converting %p to %p\n", info32, info16 );
210     TRACE("wLength %d, wValueLength %d, bText %d, value %p, child %p\n",
211                 wLength, wValueLength, bText, lpValue, child32 );
212
213     /* Convert key */
214     lstrcpyWtoA( info16->szKey, info32->szKey );
215
216     TRACE("Copied key from %p to %p: %s\n", info32->szKey, info16->szKey,
217                 debugstr_a(info16->szKey) );
218
219     /* Convert value */
220     if ( wValueLength == 0 )
221     {
222         info16->wValueLength = 0;
223         TRACE("No value present\n" );
224     }
225     else if ( bText )
226     {
227         info16->wValueLength = lstrlenW( (LPCWSTR)lpValue ) + 1;
228         lstrcpyWtoA( VersionInfo16_Value( info16 ), (LPCWSTR)lpValue );
229
230         TRACE("Copied value from %p to %p: %s\n", lpValue, 
231                     VersionInfo16_Value( info16 ), 
232                     debugstr_a(VersionInfo16_Value( info16 )) );
233     }
234     else
235     {
236         info16->wValueLength = wValueLength;
237         memmove( VersionInfo16_Value( info16 ), lpValue, wValueLength );
238
239         TRACE("Copied value from %p to %p: %d bytes\n", lpValue, 
240                      VersionInfo16_Value( info16 ), wValueLength );
241     }
242
243     /* Convert children */
244     child16 = VersionInfo16_Children( info16 );
245     while ( (DWORD)child32 < (DWORD)info32 + wLength )
246     {
247         VS_VERSION_INFO_STRUCT32 *nextChild = VersionInfo32_Next( child32 );
248
249         ConvertVersionInfo32To16( child32, child16 );
250
251         child16 = VersionInfo16_Next( child16 );
252         child32 = nextChild;
253     }
254
255     /* Fixup length */
256     info16->wLength = (DWORD)child16 - (DWORD)info16;
257
258     TRACE("Finished, length is %d (%p - %p)\n", 
259                 info16->wLength, info16, child16 );
260 }
261
262
263 /***********************************************************************
264  *           GetFileVersionInfoSize32A         [VERSION.2]
265  */
266 DWORD WINAPI GetFileVersionInfoSizeA( LPCSTR filename, LPDWORD handle )
267 {
268     VS_FIXEDFILEINFO *vffi;
269     DWORD len, ret, offset;
270     BYTE buf[144];
271
272     TRACE("(%s,%p)\n", debugstr_a(filename), handle );
273
274     len = GetFileResourceSize( filename,
275                                  MAKEINTRESOURCEA(VS_FILE_INFO),
276                                  MAKEINTRESOURCEA(VS_VERSION_INFO),
277                                  &offset );
278     if (!len) return 0;
279
280     ret = GetFileResource( filename,
281                              MAKEINTRESOURCEA(VS_FILE_INFO),
282                              MAKEINTRESOURCEA(VS_VERSION_INFO),
283                              offset, sizeof( buf ), buf );
284     if (!ret) return 0;
285
286     if ( handle ) *handle = offset;
287     
288     if ( VersionInfoIs16( buf ) )
289         vffi = (VS_FIXEDFILEINFO *)VersionInfo16_Value( (VS_VERSION_INFO_STRUCT16 *)buf );
290     else
291         vffi = (VS_FIXEDFILEINFO *)VersionInfo32_Value( (VS_VERSION_INFO_STRUCT32 *)buf );
292
293     if ( vffi->dwSignature != VS_FFI_SIGNATURE )
294     {
295         WARN("vffi->dwSignature is 0x%08lx, but not 0x%08lx!\n",
296                    vffi->dwSignature, VS_FFI_SIGNATURE );
297         return 0;
298     }
299
300     if ( ((VS_VERSION_INFO_STRUCT16 *)buf)->wLength < len )
301         len = ((VS_VERSION_INFO_STRUCT16 *)buf)->wLength;
302
303     if ( TRACE_ON(ver) )
304         print_vffi_debug( vffi );
305
306     return len;
307 }
308
309 /***********************************************************************
310  *           GetFileVersionInfoSize32W         [VERSION.3]
311  */
312 DWORD WINAPI GetFileVersionInfoSizeW( LPCWSTR filename, LPDWORD handle )
313 {
314     LPSTR fn = HEAP_strdupWtoA( GetProcessHeap(), 0, filename );
315     DWORD ret = GetFileVersionInfoSizeA( fn, handle );
316     HeapFree( GetProcessHeap(), 0, fn );
317     return ret;
318 }
319
320 /***********************************************************************
321  *           GetFileVersionInfo32A             [VERSION.1]
322  */
323 DWORD WINAPI GetFileVersionInfoA( LPCSTR filename, DWORD handle,
324                                     DWORD datasize, LPVOID data )
325 {
326     TRACE("(%s,%ld,size=%ld,data=%p)\n",
327                 debugstr_a(filename), handle, datasize, data );
328
329     if ( !GetFileResource( filename, MAKEINTRESOURCEA(VS_FILE_INFO),
330                                        MAKEINTRESOURCEA(VS_VERSION_INFO),
331                                        handle, datasize, data ) )
332         return FALSE;
333
334     if (    datasize >= sizeof(VS_VERSION_INFO_STRUCT16)
335          && datasize >= ((VS_VERSION_INFO_STRUCT16 *)data)->wLength  
336          && !VersionInfoIs16( data ) )
337     {
338         /* convert resource from PE format to NE format */
339         ConvertVersionInfo32To16( (VS_VERSION_INFO_STRUCT32 *)data, 
340                                   (VS_VERSION_INFO_STRUCT16 *)data );
341     }
342
343     return TRUE;
344 }
345
346 /***********************************************************************
347  *           GetFileVersionInfo32W             [VERSION.4]
348  */
349 DWORD WINAPI GetFileVersionInfoW( LPCWSTR filename, DWORD handle,
350                                     DWORD datasize, LPVOID data )
351 {
352     LPSTR fn = HEAP_strdupWtoA( GetProcessHeap(), 0, filename );
353     DWORD retv = TRUE;
354
355     TRACE("(%s,%ld,size=%ld,data=%p)\n",
356                 debugstr_a(fn), handle, datasize, data );
357
358     if ( !GetFileResource( fn, MAKEINTRESOURCEA(VS_FILE_INFO),
359                                  MAKEINTRESOURCEA(VS_VERSION_INFO),
360                                  handle, datasize, data ) )
361         retv = FALSE;
362
363     else if (    datasize >= sizeof(VS_VERSION_INFO_STRUCT16)
364               && datasize >= ((VS_VERSION_INFO_STRUCT16 *)data)->wLength 
365               && VersionInfoIs16( data ) )
366     {
367         ERR("Cannot access NE resource in %s\n", debugstr_a(fn) );
368         retv =  FALSE;
369     }
370
371     HeapFree( GetProcessHeap(), 0, fn );
372     return retv;
373 }
374
375
376 /***********************************************************************
377  *           VersionInfo16_FindChild             [internal]
378  */
379 VS_VERSION_INFO_STRUCT16 *VersionInfo16_FindChild( VS_VERSION_INFO_STRUCT16 *info, 
380                                             LPCSTR szKey, UINT cbKey )
381 {
382     VS_VERSION_INFO_STRUCT16 *child = VersionInfo16_Children( info );
383
384     while ( (DWORD)child < (DWORD)info + info->wLength )
385     {
386         if ( !lstrncmpiA( child->szKey, szKey, cbKey ) )
387             return child;
388
389         if (!(child->wLength)) return NULL;
390         child = VersionInfo16_Next( child );
391     }
392
393     return NULL;
394 }
395
396 /***********************************************************************
397  *           VersionInfo32_FindChild             [internal]
398  */
399 VS_VERSION_INFO_STRUCT32 *VersionInfo32_FindChild( VS_VERSION_INFO_STRUCT32 *info, 
400                                             LPCWSTR szKey, UINT cbKey )
401 {
402     VS_VERSION_INFO_STRUCT32 *child = VersionInfo32_Children( info );
403
404     while ( (DWORD)child < (DWORD)info + info->wLength )
405     {
406         if ( !lstrncmpiW( child->szKey, szKey, cbKey ) )
407             return child;
408
409         child = VersionInfo32_Next( child );
410     }
411
412     return NULL;
413 }
414
415 /***********************************************************************
416  *           VerQueryValue32A              [VERSION.12]
417  */
418 DWORD WINAPI VerQueryValueA( LPVOID pBlock, LPCSTR lpSubBlock,
419                                LPVOID *lplpBuffer, UINT *puLen )
420 {
421     VS_VERSION_INFO_STRUCT16 *info = (VS_VERSION_INFO_STRUCT16 *)pBlock;
422     if ( !VersionInfoIs16( info ) )
423     {
424         ERR("called on PE resource!\n" );
425         return FALSE;
426     }
427
428     TRACE("(%p,%s,%p,%p)\n",
429                 pBlock, debugstr_a(lpSubBlock), lplpBuffer, puLen );
430
431     while ( *lpSubBlock )
432     {
433         /* Find next path component */
434         LPCSTR lpNextSlash;
435         for ( lpNextSlash = lpSubBlock; *lpNextSlash; lpNextSlash++ )
436             if ( *lpNextSlash == '\\' )
437                 break;
438
439         /* Skip empty components */
440         if ( lpNextSlash == lpSubBlock )
441         {
442             lpSubBlock++;
443             continue;
444         }
445
446         /* We have a non-empty component: search info for key */
447         info = VersionInfo16_FindChild( info, lpSubBlock, lpNextSlash-lpSubBlock );
448         if ( !info ) return FALSE;
449
450         /* Skip path component */
451         lpSubBlock = lpNextSlash;
452     }
453
454     /* Return value */
455     *lplpBuffer = VersionInfo16_Value( info );
456     *puLen = info->wValueLength;
457
458     return TRUE;
459 }
460
461 /***********************************************************************
462  *           VerQueryValue32W              [VERSION.13]
463  */
464 DWORD WINAPI VerQueryValueW( LPVOID pBlock, LPCWSTR lpSubBlock,
465                                LPVOID *lplpBuffer, UINT *puLen )
466 {
467     VS_VERSION_INFO_STRUCT32 *info = (VS_VERSION_INFO_STRUCT32 *)pBlock;
468     if ( VersionInfoIs16( info ) )
469     {
470         ERR("called on NE resource!\n" );
471         return FALSE;
472     }
473
474     TRACE("(%p,%s,%p,%p)\n",
475                 pBlock, debugstr_w(lpSubBlock), lplpBuffer, puLen );
476
477     while ( *lpSubBlock )
478     {
479         /* Find next path component */
480         LPCWSTR lpNextSlash;
481         for ( lpNextSlash = lpSubBlock; *lpNextSlash; lpNextSlash++ )
482             if ( *lpNextSlash == '\\' )
483                 break;
484
485         /* Skip empty components */
486         if ( lpNextSlash == lpSubBlock )
487         {
488             lpSubBlock++;
489             continue;
490         }
491
492         /* We have a non-empty component: search info for key */
493         info = VersionInfo32_FindChild( info, lpSubBlock, lpNextSlash-lpSubBlock );
494         if ( !info ) return FALSE;
495
496         /* Skip path component */
497         lpSubBlock = lpNextSlash;
498     }
499
500     /* Return value */
501     *lplpBuffer = VersionInfo32_Value( info );
502     *puLen = info->wValueLength;
503
504     return TRUE;
505 }
506
507 extern LPCSTR WINE_GetLanguageName( UINT langid );
508
509 /***********************************************************************
510  *           VerLanguageName32A              [VERSION.9]
511  */
512 DWORD WINAPI VerLanguageNameA( UINT wLang, LPSTR szLang, UINT nSize )
513 {
514     char    buffer[80];
515     LPCSTR  name;
516     DWORD   result;
517
518     TRACE("(%d,%p,%d)\n", wLang, szLang, nSize );
519
520     /* 
521      * First, check \System\CurrentControlSet\control\Nls\Locale\<langid>
522      * from the registry.
523      */
524
525     sprintf( buffer,
526              "\\System\\CurrentControlSet\\control\\Nls\\Locale\\%08x",
527              wLang );
528
529     result = RegQueryValueA( HKEY_LOCAL_MACHINE, buffer, szLang, (LPDWORD)&nSize );
530     if ( result == ERROR_SUCCESS || result == ERROR_MORE_DATA ) 
531         return nSize;
532
533     /* 
534      * If that fails, use the internal table 
535      * (actually, Windows stores the names in a string table resource ...)
536      */
537
538     name = WINE_GetLanguageName( wLang );
539     lstrcpynA( szLang, name, nSize );
540     return lstrlenA( name );
541 }
542
543 /***********************************************************************
544  *           VerLanguageName32W              [VERSION.10]
545  */
546 DWORD WINAPI VerLanguageNameW( UINT wLang, LPWSTR szLang, UINT nSize )
547 {
548     char    buffer[80];
549     LPWSTR  keyname;
550     LPCSTR  name;
551     DWORD   result;
552
553     TRACE("(%d,%p,%d)\n", wLang, szLang, nSize );
554
555     /* 
556      * First, check \System\CurrentControlSet\control\Nls\Locale\<langid>
557      * from the registry.
558      */
559
560     sprintf( buffer,
561              "\\System\\CurrentControlSet\\control\\Nls\\Locale\\%08x",
562              wLang );
563
564     keyname = HEAP_strdupAtoW( GetProcessHeap(), 0, buffer );
565     result = RegQueryValueW( HKEY_LOCAL_MACHINE, keyname, szLang, (LPDWORD)&nSize );
566     HeapFree( GetProcessHeap(), 0, keyname );
567
568     if ( result == ERROR_SUCCESS || result == ERROR_MORE_DATA ) 
569         return nSize;
570
571     /* 
572      * If that fails, use the internal table 
573      * (actually, Windows stores the names in a string table resource ...)
574      */
575
576     name = WINE_GetLanguageName( wLang );
577     lstrcpynAtoW( szLang, name, nSize );
578     return lstrlenA( name );
579 }
580
581