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