(Merged by Marcus Meissner <marcus@jet.franken.de>)
[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 "wine/unicode.h"
16 #include "winerror.h"
17 #include "heap.h"
18 #include "debugtools.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         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",
34                     HIWORD(vffi->dwStrucVersion),LOWORD(vffi->dwStrucVersion),
35                     HIWORD(vffi->dwFileVersionMS),LOWORD(vffi->dwFileVersionMS),
36                     HIWORD(vffi->dwFileVersionLS),LOWORD(vffi->dwFileVersionLS),
37                     HIWORD(vffi->dwProductVersionMS),LOWORD(vffi->dwProductVersionMS),
38                     HIWORD(vffi->dwProductVersionLS),LOWORD(vffi->dwProductVersionLS),
39                     vffi->dwFileFlagsMask,
40                     (vffi->dwFileFlags & VS_FF_DEBUG) ? "DEBUG," : "",
41                     (vffi->dwFileFlags & VS_FF_PRERELEASE) ? "PRERELEASE," : "",
42                     (vffi->dwFileFlags & VS_FF_PATCHED) ? "PATCHED," : "",
43                     (vffi->dwFileFlags & VS_FF_PRIVATEBUILD) ? "PRIVATEBUILD," : "",
44                     (vffi->dwFileFlags & VS_FF_INFOINFERRED) ? "INFOINFERRED," : "",
45                     (vffi->dwFileFlags & VS_FF_SPECIALBUILD) ? "SPECIALBUILD," : ""
46                     );
47
48         TRACE("(");
49         DPRINTF(" OS=0x%x.0x%x ",
50                 HIWORD(vffi->dwFileOS),
51                 LOWORD(vffi->dwFileOS)
52         );
53         switch (vffi->dwFileOS&0xFFFF0000) {
54         case VOS_DOS:DPRINTF("DOS,");break;
55         case VOS_OS216:DPRINTF("OS/2-16,");break;
56         case VOS_OS232:DPRINTF("OS/2-32,");break;
57         case VOS_NT:DPRINTF("NT,");break;
58         case VOS_UNKNOWN:
59         default:
60                 DPRINTF("UNKNOWN(0x%lx),",vffi->dwFileOS&0xFFFF0000);break;
61         }
62         switch (LOWORD(vffi->dwFileOS)) {
63         case VOS__BASE:DPRINTF("BASE");break;
64         case VOS__WINDOWS16:DPRINTF("WIN16");break;
65         case VOS__WINDOWS32:DPRINTF("WIN32");break;
66         case VOS__PM16:DPRINTF("PM16");break;
67         case VOS__PM32:DPRINTF("PM32");break;
68         default:DPRINTF("UNKNOWN(0x%x)",LOWORD(vffi->dwFileOS));break;
69         }
70         DPRINTF(")\n");
71
72         switch (vffi->dwFileType) {
73         default:
74         case VFT_UNKNOWN:
75                 TRACE("filetype=Unknown(0x%lx)",vffi->dwFileType);
76                 break;
77         case VFT_APP:TRACE("filetype=APP,");break;
78         case VFT_DLL:TRACE("filetype=DLL,");break;
79         case VFT_DRV:
80                 TRACE("filetype=DRV,");
81                 switch(vffi->dwFileSubtype) {
82                 default:
83                 case VFT2_UNKNOWN:
84                         DPRINTF("UNKNOWN(0x%lx)",vffi->dwFileSubtype);
85                         break;
86                 case VFT2_DRV_PRINTER:
87                         DPRINTF("PRINTER");
88                         break;
89                 case VFT2_DRV_KEYBOARD:
90                         DPRINTF("KEYBOARD");
91                         break;
92                 case VFT2_DRV_LANGUAGE:
93                         DPRINTF("LANGUAGE");
94                         break;
95                 case VFT2_DRV_DISPLAY:
96                         DPRINTF("DISPLAY");
97                         break;
98                 case VFT2_DRV_MOUSE:
99                         DPRINTF("MOUSE");
100                         break;
101                 case VFT2_DRV_NETWORK:
102                         DPRINTF("NETWORK");
103                         break;
104                 case VFT2_DRV_SYSTEM:
105                         DPRINTF("SYSTEM");
106                         break;
107                 case VFT2_DRV_INSTALLABLE:
108                         DPRINTF("INSTALLABLE");
109                         break;
110                 case VFT2_DRV_SOUND:
111                         DPRINTF("SOUND");
112                         break;
113                 case VFT2_DRV_COMM:
114                         DPRINTF("COMM");
115                         break;
116                 case VFT2_DRV_INPUTMETHOD:
117                         DPRINTF("INPUTMETHOD");
118                         break;
119                 }
120                 break;
121         case VFT_FONT:
122                 TRACE("filetype=FONT.");
123                 switch (vffi->dwFileSubtype) {
124                 default:
125                         DPRINTF("UNKNOWN(0x%lx)",vffi->dwFileSubtype);
126                         break;
127                 case VFT2_FONT_RASTER:DPRINTF("RASTER");break;
128                 case VFT2_FONT_VECTOR:DPRINTF("VECTOR");break;
129                 case VFT2_FONT_TRUETYPE:DPRINTF("TRUETYPE");break;
130                 }
131                 break;
132         case VFT_VXD:TRACE("filetype=VXD");break;
133         case VFT_STATIC_LIB:TRACE("filetype=STATIC_LIB");break;
134         }
135         DPRINTF("\n");
136         TRACE("  filedata=0x%lx.0x%lx\n",
137                     vffi->dwFileDateMS,vffi->dwFileDateLS);
138 }
139
140
141 /***********************************************************************
142  * Version Info Structure
143  */
144
145 typedef struct
146 {
147     WORD  wLength;
148     WORD  wValueLength;
149     CHAR  szKey[1];
150 #if 0   /* variable length structure */
151     /* DWORD aligned */
152     BYTE  Value[];
153     /* DWORD aligned */
154     VS_VERSION_INFO_STRUCT16 Children[];
155 #endif
156 } VS_VERSION_INFO_STRUCT16;
157
158 typedef struct
159 {
160     WORD  wLength;
161     WORD  wValueLength;
162     WORD  bText;
163     WCHAR szKey[1];
164 #if 0   /* variable length structure */
165     /* DWORD aligned */
166     BYTE  Value[];
167     /* DWORD aligned */
168     VS_VERSION_INFO_STRUCT32 Children[];
169 #endif
170 } VS_VERSION_INFO_STRUCT32;
171
172 #define VersionInfoIs16( ver ) \
173     ( ((VS_VERSION_INFO_STRUCT16 *)ver)->szKey[0] >= ' ' )
174
175 #define DWORD_ALIGN( base, ptr ) \
176     ( (LPBYTE)(base) + ((((LPBYTE)(ptr) - (LPBYTE)(base)) + 3) & ~3) )
177
178 #define VersionInfo16_Value( ver )  \
179     DWORD_ALIGN( (ver), (ver)->szKey + strlen((ver)->szKey) + 1 )
180 #define VersionInfo32_Value( ver )  \
181     DWORD_ALIGN( (ver), (ver)->szKey + strlenW((ver)->szKey) + 1 )
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("Converting %p to %p\n", info32, info16 );
211     TRACE("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("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("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("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("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("Finished, length is %d (%p - %p)\n", 
260                 info16->wLength, info16, child16 );
261 }
262
263
264 /***********************************************************************
265  *           GetFileVersionInfoSizeA         [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("(%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("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  *           GetFileVersionInfoSizeW         [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  *           GetFileVersionInfoA             [VERSION.1]
323  */
324 DWORD WINAPI GetFileVersionInfoA( LPCSTR filename, DWORD handle,
325                                     DWORD datasize, LPVOID data )
326 {
327     TRACE("(%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  *           GetFileVersionInfoW             [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("(%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("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 static 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 static 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 ( !strncmpiW( child->szKey, szKey, cbKey ) )
408             return child;
409
410         child = VersionInfo32_Next( child );
411     }
412
413     return NULL;
414 }
415
416 /***********************************************************************
417  *           VerQueryValueA              [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("called on PE resource!\n" );
426         return FALSE;
427     }
428
429     TRACE("(%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  *           VerQueryValueW              [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("called on NE resource!\n" );
472         return FALSE;
473     }
474
475     TRACE("(%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