2 * Implementation of VERSION.DLL - Version Info access
4 * Copyright 1996,1997 Marcus Meissner
5 * Copyright 1997 David Cuthbert
6 * Copyright 1999 Ulrich Weigand
14 #include "wine/winestring.h"
15 #include "wine/unicode.h"
18 #include "debugtools.h"
20 DEFAULT_DEBUG_CHANNEL(ver);
23 /******************************************************************************
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.
30 *****************************************************************************/
31 static void print_vffi_debug(VS_FIXEDFILEINFO *vffi)
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," : ""
49 DPRINTF(" OS=0x%x.0x%x ",
50 HIWORD(vffi->dwFileOS),
51 LOWORD(vffi->dwFileOS)
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;
60 DPRINTF("UNKNOWN(0x%lx),",vffi->dwFileOS&0xFFFF0000);break;
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;
72 switch (vffi->dwFileType) {
75 TRACE("filetype=Unknown(0x%lx)",vffi->dwFileType);
77 case VFT_APP:TRACE("filetype=APP,");break;
78 case VFT_DLL:TRACE("filetype=DLL,");break;
80 TRACE("filetype=DRV,");
81 switch(vffi->dwFileSubtype) {
84 DPRINTF("UNKNOWN(0x%lx)",vffi->dwFileSubtype);
86 case VFT2_DRV_PRINTER:
89 case VFT2_DRV_KEYBOARD:
92 case VFT2_DRV_LANGUAGE:
95 case VFT2_DRV_DISPLAY:
101 case VFT2_DRV_NETWORK:
104 case VFT2_DRV_SYSTEM:
107 case VFT2_DRV_INSTALLABLE:
108 DPRINTF("INSTALLABLE");
116 case VFT2_DRV_INPUTMETHOD:
117 DPRINTF("INPUTMETHOD");
122 TRACE("filetype=FONT.");
123 switch (vffi->dwFileSubtype) {
125 DPRINTF("UNKNOWN(0x%lx)",vffi->dwFileSubtype);
127 case VFT2_FONT_RASTER:DPRINTF("RASTER");break;
128 case VFT2_FONT_VECTOR:DPRINTF("VECTOR");break;
129 case VFT2_FONT_TRUETYPE:DPRINTF("TRUETYPE");break;
132 case VFT_VXD:TRACE("filetype=VXD");break;
133 case VFT_STATIC_LIB:TRACE("filetype=STATIC_LIB");break;
136 TRACE(" filedata=0x%lx.0x%lx\n",
137 vffi->dwFileDateMS,vffi->dwFileDateLS);
141 /***********************************************************************
142 * Version Info Structure
150 #if 0 /* variable length structure */
154 VS_VERSION_INFO_STRUCT16 Children[];
156 } VS_VERSION_INFO_STRUCT16;
164 #if 0 /* variable length structure */
168 VS_VERSION_INFO_STRUCT32 Children[];
170 } VS_VERSION_INFO_STRUCT32;
172 #define VersionInfoIs16( ver ) \
173 ( ((VS_VERSION_INFO_STRUCT16 *)ver)->szKey[0] >= ' ' )
175 #define DWORD_ALIGN( base, ptr ) \
176 ( (LPBYTE)(base) + ((((LPBYTE)(ptr) - (LPBYTE)(base)) + 3) & ~3) )
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 )
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 ) )
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) )
196 /***********************************************************************
197 * ConvertVersionInfo32To16 [internal]
199 void ConvertVersionInfo32To16( VS_VERSION_INFO_STRUCT32 *info32,
200 VS_VERSION_INFO_STRUCT16 *info16 )
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;
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 );
215 lstrcpyWtoA( info16->szKey, info32->szKey );
217 TRACE("Copied key from %p to %p: %s\n", info32->szKey, info16->szKey,
218 debugstr_a(info16->szKey) );
221 if ( wValueLength == 0 )
223 info16->wValueLength = 0;
224 TRACE("No value present\n" );
228 info16->wValueLength = lstrlenW( (LPCWSTR)lpValue ) + 1;
229 lstrcpyWtoA( VersionInfo16_Value( info16 ), (LPCWSTR)lpValue );
231 TRACE("Copied value from %p to %p: %s\n", lpValue,
232 VersionInfo16_Value( info16 ),
233 debugstr_a(VersionInfo16_Value( info16 )) );
237 info16->wValueLength = wValueLength;
238 memmove( VersionInfo16_Value( info16 ), lpValue, wValueLength );
240 TRACE("Copied value from %p to %p: %d bytes\n", lpValue,
241 VersionInfo16_Value( info16 ), wValueLength );
244 /* Convert children */
245 child16 = VersionInfo16_Children( info16 );
246 while ( (DWORD)child32 < (DWORD)info32 + wLength )
248 VS_VERSION_INFO_STRUCT32 *nextChild = VersionInfo32_Next( child32 );
250 ConvertVersionInfo32To16( child32, child16 );
252 child16 = VersionInfo16_Next( child16 );
257 info16->wLength = (DWORD)child16 - (DWORD)info16;
259 TRACE("Finished, length is %d (%p - %p)\n",
260 info16->wLength, info16, child16 );
264 /***********************************************************************
265 * GetFileVersionInfoSizeA [VERSION.2]
267 DWORD WINAPI GetFileVersionInfoSizeA( LPCSTR filename, LPDWORD handle )
269 VS_FIXEDFILEINFO *vffi;
270 DWORD len, ret, offset;
273 TRACE("(%s,%p)\n", debugstr_a(filename), handle );
275 len = GetFileResourceSize( filename,
276 MAKEINTRESOURCEA(VS_FILE_INFO),
277 MAKEINTRESOURCEA(VS_VERSION_INFO),
281 ret = GetFileResource( filename,
282 MAKEINTRESOURCEA(VS_FILE_INFO),
283 MAKEINTRESOURCEA(VS_VERSION_INFO),
284 offset, sizeof( buf ), buf );
287 if ( handle ) *handle = offset;
289 if ( VersionInfoIs16( buf ) )
290 vffi = (VS_FIXEDFILEINFO *)VersionInfo16_Value( (VS_VERSION_INFO_STRUCT16 *)buf );
292 vffi = (VS_FIXEDFILEINFO *)VersionInfo32_Value( (VS_VERSION_INFO_STRUCT32 *)buf );
294 if ( vffi->dwSignature != VS_FFI_SIGNATURE )
296 WARN("vffi->dwSignature is 0x%08lx, but not 0x%08lx!\n",
297 vffi->dwSignature, VS_FFI_SIGNATURE );
301 if ( ((VS_VERSION_INFO_STRUCT16 *)buf)->wLength < len )
302 len = ((VS_VERSION_INFO_STRUCT16 *)buf)->wLength;
305 print_vffi_debug( vffi );
310 /***********************************************************************
311 * GetFileVersionInfoSizeW [VERSION.3]
313 DWORD WINAPI GetFileVersionInfoSizeW( LPCWSTR filename, LPDWORD handle )
315 LPSTR fn = HEAP_strdupWtoA( GetProcessHeap(), 0, filename );
316 DWORD ret = GetFileVersionInfoSizeA( fn, handle );
317 HeapFree( GetProcessHeap(), 0, fn );
321 /***********************************************************************
322 * GetFileVersionInfoA [VERSION.1]
324 DWORD WINAPI GetFileVersionInfoA( LPCSTR filename, DWORD handle,
325 DWORD datasize, LPVOID data )
327 TRACE("(%s,%ld,size=%ld,data=%p)\n",
328 debugstr_a(filename), handle, datasize, data );
330 if ( !GetFileResource( filename, MAKEINTRESOURCEA(VS_FILE_INFO),
331 MAKEINTRESOURCEA(VS_VERSION_INFO),
332 handle, datasize, data ) )
335 if ( datasize >= sizeof(VS_VERSION_INFO_STRUCT16)
336 && datasize >= ((VS_VERSION_INFO_STRUCT16 *)data)->wLength
337 && !VersionInfoIs16( data ) )
339 /* convert resource from PE format to NE format */
340 ConvertVersionInfo32To16( (VS_VERSION_INFO_STRUCT32 *)data,
341 (VS_VERSION_INFO_STRUCT16 *)data );
347 /***********************************************************************
348 * GetFileVersionInfoW [VERSION.4]
350 DWORD WINAPI GetFileVersionInfoW( LPCWSTR filename, DWORD handle,
351 DWORD datasize, LPVOID data )
353 LPSTR fn = HEAP_strdupWtoA( GetProcessHeap(), 0, filename );
356 TRACE("(%s,%ld,size=%ld,data=%p)\n",
357 debugstr_a(fn), handle, datasize, data );
359 if ( !GetFileResource( fn, MAKEINTRESOURCEA(VS_FILE_INFO),
360 MAKEINTRESOURCEA(VS_VERSION_INFO),
361 handle, datasize, data ) )
364 else if ( datasize >= sizeof(VS_VERSION_INFO_STRUCT16)
365 && datasize >= ((VS_VERSION_INFO_STRUCT16 *)data)->wLength
366 && VersionInfoIs16( data ) )
368 ERR("Cannot access NE resource in %s\n", debugstr_a(fn) );
372 HeapFree( GetProcessHeap(), 0, fn );
377 /***********************************************************************
378 * VersionInfo16_FindChild [internal]
380 static VS_VERSION_INFO_STRUCT16 *VersionInfo16_FindChild( VS_VERSION_INFO_STRUCT16 *info,
381 LPCSTR szKey, UINT cbKey )
383 VS_VERSION_INFO_STRUCT16 *child = VersionInfo16_Children( info );
385 while ( (DWORD)child < (DWORD)info + info->wLength )
387 if ( !lstrncmpiA( child->szKey, szKey, cbKey ) )
390 if (!(child->wLength)) return NULL;
391 child = VersionInfo16_Next( child );
397 /***********************************************************************
398 * VersionInfo32_FindChild [internal]
400 static VS_VERSION_INFO_STRUCT32 *VersionInfo32_FindChild( VS_VERSION_INFO_STRUCT32 *info,
401 LPCWSTR szKey, UINT cbKey )
403 VS_VERSION_INFO_STRUCT32 *child = VersionInfo32_Children( info );
405 while ( (DWORD)child < (DWORD)info + info->wLength )
407 if ( !strncmpiW( child->szKey, szKey, cbKey ) )
410 child = VersionInfo32_Next( child );
416 /***********************************************************************
417 * VerQueryValueA [VERSION.12]
419 DWORD WINAPI VerQueryValueA( LPVOID pBlock, LPCSTR lpSubBlock,
420 LPVOID *lplpBuffer, UINT *puLen )
422 VS_VERSION_INFO_STRUCT16 *info = (VS_VERSION_INFO_STRUCT16 *)pBlock;
423 if ( !VersionInfoIs16( info ) )
425 ERR("called on PE resource!\n" );
429 TRACE("(%p,%s,%p,%p)\n",
430 pBlock, debugstr_a(lpSubBlock), lplpBuffer, puLen );
432 while ( *lpSubBlock )
434 /* Find next path component */
436 for ( lpNextSlash = lpSubBlock; *lpNextSlash; lpNextSlash++ )
437 if ( *lpNextSlash == '\\' )
440 /* Skip empty components */
441 if ( lpNextSlash == lpSubBlock )
447 /* We have a non-empty component: search info for key */
448 info = VersionInfo16_FindChild( info, lpSubBlock, lpNextSlash-lpSubBlock );
449 if ( !info ) return FALSE;
451 /* Skip path component */
452 lpSubBlock = lpNextSlash;
456 *lplpBuffer = VersionInfo16_Value( info );
457 *puLen = info->wValueLength;
462 /***********************************************************************
463 * VerQueryValueW [VERSION.13]
465 DWORD WINAPI VerQueryValueW( LPVOID pBlock, LPCWSTR lpSubBlock,
466 LPVOID *lplpBuffer, UINT *puLen )
468 VS_VERSION_INFO_STRUCT32 *info = (VS_VERSION_INFO_STRUCT32 *)pBlock;
469 if ( VersionInfoIs16( info ) )
471 ERR("called on NE resource!\n" );
475 TRACE("(%p,%s,%p,%p)\n",
476 pBlock, debugstr_w(lpSubBlock), lplpBuffer, puLen );
478 while ( *lpSubBlock )
480 /* Find next path component */
482 for ( lpNextSlash = lpSubBlock; *lpNextSlash; lpNextSlash++ )
483 if ( *lpNextSlash == '\\' )
486 /* Skip empty components */
487 if ( lpNextSlash == lpSubBlock )
493 /* We have a non-empty component: search info for key */
494 info = VersionInfo32_FindChild( info, lpSubBlock, lpNextSlash-lpSubBlock );
495 if ( !info ) return FALSE;
497 /* Skip path component */
498 lpSubBlock = lpNextSlash;
502 *lplpBuffer = VersionInfo32_Value( info );
503 *puLen = info->wValueLength;