dvideo.dll forgets to set the DDSD_CAPS flag, just assume it set.
[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/winuser16.h"
15 #include "wine/unicode.h"
16 #include "winerror.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         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",
33                     HIWORD(vffi->dwStrucVersion),LOWORD(vffi->dwStrucVersion),
34                     HIWORD(vffi->dwFileVersionMS),LOWORD(vffi->dwFileVersionMS),
35                     HIWORD(vffi->dwFileVersionLS),LOWORD(vffi->dwFileVersionLS),
36                     HIWORD(vffi->dwProductVersionMS),LOWORD(vffi->dwProductVersionMS),
37                     HIWORD(vffi->dwProductVersionLS),LOWORD(vffi->dwProductVersionLS),
38                     vffi->dwFileFlagsMask,
39                     (vffi->dwFileFlags & VS_FF_DEBUG) ? "DEBUG," : "",
40                     (vffi->dwFileFlags & VS_FF_PRERELEASE) ? "PRERELEASE," : "",
41                     (vffi->dwFileFlags & VS_FF_PATCHED) ? "PATCHED," : "",
42                     (vffi->dwFileFlags & VS_FF_PRIVATEBUILD) ? "PRIVATEBUILD," : "",
43                     (vffi->dwFileFlags & VS_FF_INFOINFERRED) ? "INFOINFERRED," : "",
44                     (vffi->dwFileFlags & VS_FF_SPECIALBUILD) ? "SPECIALBUILD," : ""
45                     );
46
47         TRACE("(");
48         DPRINTF(" OS=0x%x.0x%x ",
49                 HIWORD(vffi->dwFileOS),
50                 LOWORD(vffi->dwFileOS)
51         );
52         switch (vffi->dwFileOS&0xFFFF0000) {
53         case VOS_DOS:DPRINTF("DOS,");break;
54         case VOS_OS216:DPRINTF("OS/2-16,");break;
55         case VOS_OS232:DPRINTF("OS/2-32,");break;
56         case VOS_NT:DPRINTF("NT,");break;
57         case VOS_UNKNOWN:
58         default:
59                 DPRINTF("UNKNOWN(0x%lx),",vffi->dwFileOS&0xFFFF0000);break;
60         }
61         switch (LOWORD(vffi->dwFileOS)) {
62         case VOS__BASE:DPRINTF("BASE");break;
63         case VOS__WINDOWS16:DPRINTF("WIN16");break;
64         case VOS__WINDOWS32:DPRINTF("WIN32");break;
65         case VOS__PM16:DPRINTF("PM16");break;
66         case VOS__PM32:DPRINTF("PM32");break;
67         default:DPRINTF("UNKNOWN(0x%x)",LOWORD(vffi->dwFileOS));break;
68         }
69         DPRINTF(")\n");
70
71         switch (vffi->dwFileType) {
72         default:
73         case VFT_UNKNOWN:
74                 TRACE("filetype=Unknown(0x%lx)",vffi->dwFileType);
75                 break;
76         case VFT_APP:TRACE("filetype=APP,");break;
77         case VFT_DLL:TRACE("filetype=DLL,");break;
78         case VFT_DRV:
79                 TRACE("filetype=DRV,");
80                 switch(vffi->dwFileSubtype) {
81                 default:
82                 case VFT2_UNKNOWN:
83                         DPRINTF("UNKNOWN(0x%lx)",vffi->dwFileSubtype);
84                         break;
85                 case VFT2_DRV_PRINTER:
86                         DPRINTF("PRINTER");
87                         break;
88                 case VFT2_DRV_KEYBOARD:
89                         DPRINTF("KEYBOARD");
90                         break;
91                 case VFT2_DRV_LANGUAGE:
92                         DPRINTF("LANGUAGE");
93                         break;
94                 case VFT2_DRV_DISPLAY:
95                         DPRINTF("DISPLAY");
96                         break;
97                 case VFT2_DRV_MOUSE:
98                         DPRINTF("MOUSE");
99                         break;
100                 case VFT2_DRV_NETWORK:
101                         DPRINTF("NETWORK");
102                         break;
103                 case VFT2_DRV_SYSTEM:
104                         DPRINTF("SYSTEM");
105                         break;
106                 case VFT2_DRV_INSTALLABLE:
107                         DPRINTF("INSTALLABLE");
108                         break;
109                 case VFT2_DRV_SOUND:
110                         DPRINTF("SOUND");
111                         break;
112                 case VFT2_DRV_COMM:
113                         DPRINTF("COMM");
114                         break;
115                 case VFT2_DRV_INPUTMETHOD:
116                         DPRINTF("INPUTMETHOD");
117                         break;
118                 }
119                 break;
120         case VFT_FONT:
121                 TRACE("filetype=FONT.");
122                 switch (vffi->dwFileSubtype) {
123                 default:
124                         DPRINTF("UNKNOWN(0x%lx)",vffi->dwFileSubtype);
125                         break;
126                 case VFT2_FONT_RASTER:DPRINTF("RASTER");break;
127                 case VFT2_FONT_VECTOR:DPRINTF("VECTOR");break;
128                 case VFT2_FONT_TRUETYPE:DPRINTF("TRUETYPE");break;
129                 }
130                 break;
131         case VFT_VXD:TRACE("filetype=VXD");break;
132         case VFT_STATIC_LIB:TRACE("filetype=STATIC_LIB");break;
133         }
134         DPRINTF("\n");
135         TRACE("  filedata=0x%lx.0x%lx\n",
136                     vffi->dwFileDateMS,vffi->dwFileDateLS);
137 }
138
139
140 /***********************************************************************
141  * Version Info Structure
142  */
143
144 typedef struct
145 {
146     WORD  wLength;
147     WORD  wValueLength;
148     CHAR  szKey[1];
149 #if 0   /* variable length structure */
150     /* DWORD aligned */
151     BYTE  Value[];
152     /* DWORD aligned */
153     VS_VERSION_INFO_STRUCT16 Children[];
154 #endif
155 } VS_VERSION_INFO_STRUCT16;
156
157 typedef struct
158 {
159     WORD  wLength;
160     WORD  wValueLength;
161     WORD  bText;
162     WCHAR szKey[1];
163 #if 0   /* variable length structure */
164     /* DWORD aligned */
165     BYTE  Value[];
166     /* DWORD aligned */
167     VS_VERSION_INFO_STRUCT32 Children[];
168 #endif
169 } VS_VERSION_INFO_STRUCT32;
170
171 #define VersionInfoIs16( ver ) \
172     ( ((VS_VERSION_INFO_STRUCT16 *)ver)->szKey[0] >= ' ' )
173
174 #define DWORD_ALIGN( base, ptr ) \
175     ( (LPBYTE)(base) + ((((LPBYTE)(ptr) - (LPBYTE)(base)) + 3) & ~3) )
176
177 #define VersionInfo16_Value( ver )  \
178     DWORD_ALIGN( (ver), (ver)->szKey + strlen((ver)->szKey) + 1 )
179 #define VersionInfo32_Value( ver )  \
180     DWORD_ALIGN( (ver), (ver)->szKey + strlenW((ver)->szKey) + 1 )
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     WideCharToMultiByte( CP_ACP, 0, info32->szKey, -1, info16->szKey, 0x7fffffff, NULL, NULL );
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 = WideCharToMultiByte( CP_ACP, 0, (LPCWSTR)lpValue, -1, NULL, 0, NULL, NULL );
228         WideCharToMultiByte( CP_ACP, 0, (LPCWSTR)lpValue, -1,
229                              VersionInfo16_Value( info16 ), info16->wValueLength, NULL, NULL );
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 = GetFileResourceSize16( filename,
276                                  MAKEINTRESOURCEA(VS_FILE_INFO),
277                                  MAKEINTRESOURCEA(VS_VERSION_INFO),
278                                  &offset );
279     if (!len) return 0;
280
281     ret = GetFileResource16( 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     DWORD ret, len = WideCharToMultiByte( CP_ACP, 0, filename, -1, NULL, 0, NULL, NULL );
316     LPSTR fn = HeapAlloc( GetProcessHeap(), 0, len );
317     WideCharToMultiByte( CP_ACP, 0, filename, -1, fn, len, NULL, NULL );
318     ret = GetFileVersionInfoSizeA( fn, handle );
319     HeapFree( GetProcessHeap(), 0, fn );
320     return ret;
321 }
322
323 /***********************************************************************
324  *           GetFileVersionInfoA             [VERSION.1]
325  */
326 DWORD WINAPI GetFileVersionInfoA( LPCSTR filename, DWORD handle,
327                                     DWORD datasize, LPVOID data )
328 {
329     TRACE("(%s,%ld,size=%ld,data=%p)\n",
330                 debugstr_a(filename), handle, datasize, data );
331
332     if ( !GetFileResource16( filename, MAKEINTRESOURCEA(VS_FILE_INFO),
333                                        MAKEINTRESOURCEA(VS_VERSION_INFO),
334                                        handle, datasize, data ) )
335         return FALSE;
336
337     if (    datasize >= sizeof(VS_VERSION_INFO_STRUCT16)
338          && datasize >= ((VS_VERSION_INFO_STRUCT16 *)data)->wLength  
339          && !VersionInfoIs16( data ) )
340     {
341         /* convert resource from PE format to NE format */
342         ConvertVersionInfo32To16( (VS_VERSION_INFO_STRUCT32 *)data, 
343                                   (VS_VERSION_INFO_STRUCT16 *)data );
344     }
345
346     return TRUE;
347 }
348
349 /***********************************************************************
350  *           GetFileVersionInfoW             [VERSION.4]
351  */
352 DWORD WINAPI GetFileVersionInfoW( LPCWSTR filename, DWORD handle,
353                                     DWORD datasize, LPVOID data )
354 {
355     DWORD len = WideCharToMultiByte( CP_ACP, 0, filename, -1, NULL, 0, NULL, NULL );
356     LPSTR fn = HeapAlloc( GetProcessHeap(), 0, len );
357     DWORD retv = TRUE;
358
359     WideCharToMultiByte( CP_ACP, 0, filename, -1, fn, len, NULL, NULL );
360
361     TRACE("(%s,%ld,size=%ld,data=%p)\n",
362                 debugstr_w(filename), handle, datasize, data );
363
364     if ( !GetFileResource16( fn, MAKEINTRESOURCEA(VS_FILE_INFO),
365                                  MAKEINTRESOURCEA(VS_VERSION_INFO),
366                                  handle, datasize, data ) )
367         retv = FALSE;
368
369     else if (    datasize >= sizeof(VS_VERSION_INFO_STRUCT16)
370               && datasize >= ((VS_VERSION_INFO_STRUCT16 *)data)->wLength 
371               && VersionInfoIs16( data ) )
372     {
373         ERR("Cannot access NE resource in %s\n", debugstr_a(fn) );
374         retv =  FALSE;
375     }
376
377     HeapFree( GetProcessHeap(), 0, fn );
378     return retv;
379 }
380
381
382 /***********************************************************************
383  *           VersionInfo16_FindChild             [internal]
384  */
385 static VS_VERSION_INFO_STRUCT16 *VersionInfo16_FindChild( VS_VERSION_INFO_STRUCT16 *info, 
386                                             LPCSTR szKey, UINT cbKey )
387 {
388     VS_VERSION_INFO_STRUCT16 *child = VersionInfo16_Children( info );
389
390     while ( (DWORD)child < (DWORD)info + info->wLength )
391     {
392         if ( !strncasecmp( child->szKey, szKey, cbKey ) )
393             return child;
394
395         if (!(child->wLength)) return NULL;
396         child = VersionInfo16_Next( child );
397     }
398
399     return NULL;
400 }
401
402 /***********************************************************************
403  *           VersionInfo32_FindChild             [internal]
404  */
405 static VS_VERSION_INFO_STRUCT32 *VersionInfo32_FindChild( VS_VERSION_INFO_STRUCT32 *info, 
406                                             LPCWSTR szKey, UINT cbKey )
407 {
408     VS_VERSION_INFO_STRUCT32 *child = VersionInfo32_Children( info );
409
410     while ( (DWORD)child < (DWORD)info + info->wLength )
411     {
412         if ( !strncmpiW( child->szKey, szKey, cbKey ) )
413             return child;
414
415         child = VersionInfo32_Next( child );
416     }
417
418     return NULL;
419 }
420
421 /***********************************************************************
422  *           VerQueryValueA              [VERSION.12]
423  */
424 DWORD WINAPI VerQueryValueA( LPVOID pBlock, LPCSTR lpSubBlock,
425                                LPVOID *lplpBuffer, UINT *puLen )
426 {
427     VS_VERSION_INFO_STRUCT16 *info = (VS_VERSION_INFO_STRUCT16 *)pBlock;
428     if ( !VersionInfoIs16( info ) )
429     {
430         ERR("called on PE resource!\n" );
431         return FALSE;
432     }
433
434     TRACE("(%p,%s,%p,%p)\n",
435                 pBlock, debugstr_a(lpSubBlock), lplpBuffer, puLen );
436
437     while ( *lpSubBlock )
438     {
439         /* Find next path component */
440         LPCSTR lpNextSlash;
441         for ( lpNextSlash = lpSubBlock; *lpNextSlash; lpNextSlash++ )
442             if ( *lpNextSlash == '\\' )
443                 break;
444
445         /* Skip empty components */
446         if ( lpNextSlash == lpSubBlock )
447         {
448             lpSubBlock++;
449             continue;
450         }
451
452         /* We have a non-empty component: search info for key */
453         info = VersionInfo16_FindChild( info, lpSubBlock, lpNextSlash-lpSubBlock );
454         if ( !info ) return FALSE;
455
456         /* Skip path component */
457         lpSubBlock = lpNextSlash;
458     }
459
460     /* Return value */
461     *lplpBuffer = VersionInfo16_Value( info );
462     *puLen = info->wValueLength;
463
464     return TRUE;
465 }
466
467 /***********************************************************************
468  *           VerQueryValueW              [VERSION.13]
469  */
470 DWORD WINAPI VerQueryValueW( LPVOID pBlock, LPCWSTR lpSubBlock,
471                                LPVOID *lplpBuffer, UINT *puLen )
472 {
473     VS_VERSION_INFO_STRUCT32 *info = (VS_VERSION_INFO_STRUCT32 *)pBlock;
474     if ( VersionInfoIs16( info ) )
475     {
476         ERR("called on NE resource!\n" );
477         return FALSE;
478     }
479
480     TRACE("(%p,%s,%p,%p)\n",
481                 pBlock, debugstr_w(lpSubBlock), lplpBuffer, puLen );
482
483     while ( *lpSubBlock )
484     {
485         /* Find next path component */
486         LPCWSTR lpNextSlash;
487         for ( lpNextSlash = lpSubBlock; *lpNextSlash; lpNextSlash++ )
488             if ( *lpNextSlash == '\\' )
489                 break;
490
491         /* Skip empty components */
492         if ( lpNextSlash == lpSubBlock )
493         {
494             lpSubBlock++;
495             continue;
496         }
497
498         /* We have a non-empty component: search info for key */
499         info = VersionInfo32_FindChild( info, lpSubBlock, lpNextSlash-lpSubBlock );
500         if ( !info ) return FALSE;
501
502         /* Skip path component */
503         lpSubBlock = lpNextSlash;
504     }
505
506     /* Return value */
507     *lplpBuffer = VersionInfo32_Value( info );
508     *puLen = info->wValueLength;
509
510     return TRUE;
511 }
512