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/winuser16.h"
16 #include "wine/unicode.h"
19 #include "debugtools.h"
21 DEFAULT_DEBUG_CHANNEL(ver);
24 /******************************************************************************
26 * This function will print via dprintf[_]ver to stddeb debug info regarding
27 * the file info structure vffi.
28 * 15-Feb-1998 Dimitrie Paun (dimi@cs.toronto.edu)
29 * Added this function to clean up the code.
31 *****************************************************************************/
32 static void print_vffi_debug(VS_FIXEDFILEINFO *vffi)
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," : ""
50 DPRINTF(" OS=0x%x.0x%x ",
51 HIWORD(vffi->dwFileOS),
52 LOWORD(vffi->dwFileOS)
54 switch (vffi->dwFileOS&0xFFFF0000) {
55 case VOS_DOS:DPRINTF("DOS,");break;
56 case VOS_OS216:DPRINTF("OS/2-16,");break;
57 case VOS_OS232:DPRINTF("OS/2-32,");break;
58 case VOS_NT:DPRINTF("NT,");break;
61 DPRINTF("UNKNOWN(0x%lx),",vffi->dwFileOS&0xFFFF0000);break;
63 switch (LOWORD(vffi->dwFileOS)) {
64 case VOS__BASE:DPRINTF("BASE");break;
65 case VOS__WINDOWS16:DPRINTF("WIN16");break;
66 case VOS__WINDOWS32:DPRINTF("WIN32");break;
67 case VOS__PM16:DPRINTF("PM16");break;
68 case VOS__PM32:DPRINTF("PM32");break;
69 default:DPRINTF("UNKNOWN(0x%x)",LOWORD(vffi->dwFileOS));break;
73 switch (vffi->dwFileType) {
76 TRACE("filetype=Unknown(0x%lx)",vffi->dwFileType);
78 case VFT_APP:TRACE("filetype=APP,");break;
79 case VFT_DLL:TRACE("filetype=DLL,");break;
81 TRACE("filetype=DRV,");
82 switch(vffi->dwFileSubtype) {
85 DPRINTF("UNKNOWN(0x%lx)",vffi->dwFileSubtype);
87 case VFT2_DRV_PRINTER:
90 case VFT2_DRV_KEYBOARD:
93 case VFT2_DRV_LANGUAGE:
96 case VFT2_DRV_DISPLAY:
102 case VFT2_DRV_NETWORK:
105 case VFT2_DRV_SYSTEM:
108 case VFT2_DRV_INSTALLABLE:
109 DPRINTF("INSTALLABLE");
117 case VFT2_DRV_INPUTMETHOD:
118 DPRINTF("INPUTMETHOD");
123 TRACE("filetype=FONT.");
124 switch (vffi->dwFileSubtype) {
126 DPRINTF("UNKNOWN(0x%lx)",vffi->dwFileSubtype);
128 case VFT2_FONT_RASTER:DPRINTF("RASTER");break;
129 case VFT2_FONT_VECTOR:DPRINTF("VECTOR");break;
130 case VFT2_FONT_TRUETYPE:DPRINTF("TRUETYPE");break;
133 case VFT_VXD:TRACE("filetype=VXD");break;
134 case VFT_STATIC_LIB:TRACE("filetype=STATIC_LIB");break;
137 TRACE(" filedata=0x%lx.0x%lx\n",
138 vffi->dwFileDateMS,vffi->dwFileDateLS);
142 /***********************************************************************
143 * Version Info Structure
151 #if 0 /* variable length structure */
155 VS_VERSION_INFO_STRUCT16 Children[];
157 } VS_VERSION_INFO_STRUCT16;
165 #if 0 /* variable length structure */
169 VS_VERSION_INFO_STRUCT32 Children[];
171 } VS_VERSION_INFO_STRUCT32;
173 #define VersionInfoIs16( ver ) \
174 ( ((VS_VERSION_INFO_STRUCT16 *)ver)->szKey[0] >= ' ' )
176 #define DWORD_ALIGN( base, ptr ) \
177 ( (LPBYTE)(base) + ((((LPBYTE)(ptr) - (LPBYTE)(base)) + 3) & ~3) )
179 #define VersionInfo16_Value( ver ) \
180 DWORD_ALIGN( (ver), (ver)->szKey + strlen((ver)->szKey) + 1 )
181 #define VersionInfo32_Value( ver ) \
182 DWORD_ALIGN( (ver), (ver)->szKey + strlenW((ver)->szKey) + 1 )
184 #define VersionInfo16_Children( ver ) \
185 (VS_VERSION_INFO_STRUCT16 *)( VersionInfo16_Value( ver ) + \
186 ( ( (ver)->wValueLength + 3 ) & ~3 ) )
187 #define VersionInfo32_Children( ver ) \
188 (VS_VERSION_INFO_STRUCT32 *)( VersionInfo32_Value( ver ) + \
189 ( ( (ver)->wValueLength * \
190 ((ver)->bText? 2 : 1) + 3 ) & ~3 ) )
192 #define VersionInfo16_Next( ver ) \
193 (VS_VERSION_INFO_STRUCT16 *)( (LPBYTE)ver + (((ver)->wLength + 3) & ~3) )
194 #define VersionInfo32_Next( ver ) \
195 (VS_VERSION_INFO_STRUCT32 *)( (LPBYTE)ver + (((ver)->wLength + 3) & ~3) )
197 /***********************************************************************
198 * ConvertVersionInfo32To16 [internal]
200 void ConvertVersionInfo32To16( VS_VERSION_INFO_STRUCT32 *info32,
201 VS_VERSION_INFO_STRUCT16 *info16 )
203 /* Copy data onto local stack to prevent overwrites */
204 WORD wLength = info32->wLength;
205 WORD wValueLength = info32->wValueLength;
206 WORD bText = info32->bText;
207 LPBYTE lpValue = VersionInfo32_Value( info32 );
208 VS_VERSION_INFO_STRUCT32 *child32 = VersionInfo32_Children( info32 );
209 VS_VERSION_INFO_STRUCT16 *child16;
211 TRACE("Converting %p to %p\n", info32, info16 );
212 TRACE("wLength %d, wValueLength %d, bText %d, value %p, child %p\n",
213 wLength, wValueLength, bText, lpValue, child32 );
216 lstrcpyWtoA( info16->szKey, info32->szKey );
218 TRACE("Copied key from %p to %p: %s\n", info32->szKey, info16->szKey,
219 debugstr_a(info16->szKey) );
222 if ( wValueLength == 0 )
224 info16->wValueLength = 0;
225 TRACE("No value present\n" );
229 info16->wValueLength = lstrlenW( (LPCWSTR)lpValue ) + 1;
230 lstrcpyWtoA( VersionInfo16_Value( info16 ), (LPCWSTR)lpValue );
232 TRACE("Copied value from %p to %p: %s\n", lpValue,
233 VersionInfo16_Value( info16 ),
234 debugstr_a(VersionInfo16_Value( info16 )) );
238 info16->wValueLength = wValueLength;
239 memmove( VersionInfo16_Value( info16 ), lpValue, wValueLength );
241 TRACE("Copied value from %p to %p: %d bytes\n", lpValue,
242 VersionInfo16_Value( info16 ), wValueLength );
245 /* Convert children */
246 child16 = VersionInfo16_Children( info16 );
247 while ( (DWORD)child32 < (DWORD)info32 + wLength )
249 VS_VERSION_INFO_STRUCT32 *nextChild = VersionInfo32_Next( child32 );
251 ConvertVersionInfo32To16( child32, child16 );
253 child16 = VersionInfo16_Next( child16 );
258 info16->wLength = (DWORD)child16 - (DWORD)info16;
260 TRACE("Finished, length is %d (%p - %p)\n",
261 info16->wLength, info16, child16 );
265 /***********************************************************************
266 * GetFileVersionInfoSizeA [VERSION.2]
268 DWORD WINAPI GetFileVersionInfoSizeA( LPCSTR filename, LPDWORD handle )
270 VS_FIXEDFILEINFO *vffi;
271 DWORD len, ret, offset;
274 TRACE("(%s,%p)\n", debugstr_a(filename), handle );
276 len = GetFileResourceSize16( filename,
277 MAKEINTRESOURCEA(VS_FILE_INFO),
278 MAKEINTRESOURCEA(VS_VERSION_INFO),
282 ret = GetFileResource16( filename,
283 MAKEINTRESOURCEA(VS_FILE_INFO),
284 MAKEINTRESOURCEA(VS_VERSION_INFO),
285 offset, sizeof( buf ), buf );
288 if ( handle ) *handle = offset;
290 if ( VersionInfoIs16( buf ) )
291 vffi = (VS_FIXEDFILEINFO *)VersionInfo16_Value( (VS_VERSION_INFO_STRUCT16 *)buf );
293 vffi = (VS_FIXEDFILEINFO *)VersionInfo32_Value( (VS_VERSION_INFO_STRUCT32 *)buf );
295 if ( vffi->dwSignature != VS_FFI_SIGNATURE )
297 WARN("vffi->dwSignature is 0x%08lx, but not 0x%08lx!\n",
298 vffi->dwSignature, VS_FFI_SIGNATURE );
302 if ( ((VS_VERSION_INFO_STRUCT16 *)buf)->wLength < len )
303 len = ((VS_VERSION_INFO_STRUCT16 *)buf)->wLength;
306 print_vffi_debug( vffi );
311 /***********************************************************************
312 * GetFileVersionInfoSizeW [VERSION.3]
314 DWORD WINAPI GetFileVersionInfoSizeW( LPCWSTR filename, LPDWORD handle )
316 LPSTR fn = HEAP_strdupWtoA( GetProcessHeap(), 0, filename );
317 DWORD ret = GetFileVersionInfoSizeA( fn, handle );
318 HeapFree( GetProcessHeap(), 0, fn );
322 /***********************************************************************
323 * GetFileVersionInfoA [VERSION.1]
325 DWORD WINAPI GetFileVersionInfoA( LPCSTR filename, DWORD handle,
326 DWORD datasize, LPVOID data )
328 TRACE("(%s,%ld,size=%ld,data=%p)\n",
329 debugstr_a(filename), handle, datasize, data );
331 if ( !GetFileResource16( filename, MAKEINTRESOURCEA(VS_FILE_INFO),
332 MAKEINTRESOURCEA(VS_VERSION_INFO),
333 handle, datasize, data ) )
336 if ( datasize >= sizeof(VS_VERSION_INFO_STRUCT16)
337 && datasize >= ((VS_VERSION_INFO_STRUCT16 *)data)->wLength
338 && !VersionInfoIs16( data ) )
340 /* convert resource from PE format to NE format */
341 ConvertVersionInfo32To16( (VS_VERSION_INFO_STRUCT32 *)data,
342 (VS_VERSION_INFO_STRUCT16 *)data );
348 /***********************************************************************
349 * GetFileVersionInfoW [VERSION.4]
351 DWORD WINAPI GetFileVersionInfoW( LPCWSTR filename, DWORD handle,
352 DWORD datasize, LPVOID data )
354 LPSTR fn = HEAP_strdupWtoA( GetProcessHeap(), 0, filename );
357 TRACE("(%s,%ld,size=%ld,data=%p)\n",
358 debugstr_a(fn), handle, datasize, data );
360 if ( !GetFileResource16( fn, MAKEINTRESOURCEA(VS_FILE_INFO),
361 MAKEINTRESOURCEA(VS_VERSION_INFO),
362 handle, datasize, data ) )
365 else if ( datasize >= sizeof(VS_VERSION_INFO_STRUCT16)
366 && datasize >= ((VS_VERSION_INFO_STRUCT16 *)data)->wLength
367 && VersionInfoIs16( data ) )
369 ERR("Cannot access NE resource in %s\n", debugstr_a(fn) );
373 HeapFree( GetProcessHeap(), 0, fn );
378 /***********************************************************************
379 * VersionInfo16_FindChild [internal]
381 static VS_VERSION_INFO_STRUCT16 *VersionInfo16_FindChild( VS_VERSION_INFO_STRUCT16 *info,
382 LPCSTR szKey, UINT cbKey )
384 VS_VERSION_INFO_STRUCT16 *child = VersionInfo16_Children( info );
386 while ( (DWORD)child < (DWORD)info + info->wLength )
388 if ( !strncasecmp( child->szKey, szKey, cbKey ) )
391 if (!(child->wLength)) return NULL;
392 child = VersionInfo16_Next( child );
398 /***********************************************************************
399 * VersionInfo32_FindChild [internal]
401 static VS_VERSION_INFO_STRUCT32 *VersionInfo32_FindChild( VS_VERSION_INFO_STRUCT32 *info,
402 LPCWSTR szKey, UINT cbKey )
404 VS_VERSION_INFO_STRUCT32 *child = VersionInfo32_Children( info );
406 while ( (DWORD)child < (DWORD)info + info->wLength )
408 if ( !strncmpiW( child->szKey, szKey, cbKey ) )
411 child = VersionInfo32_Next( child );
417 /***********************************************************************
418 * VerQueryValueA [VERSION.12]
420 DWORD WINAPI VerQueryValueA( LPVOID pBlock, LPCSTR lpSubBlock,
421 LPVOID *lplpBuffer, UINT *puLen )
423 VS_VERSION_INFO_STRUCT16 *info = (VS_VERSION_INFO_STRUCT16 *)pBlock;
424 if ( !VersionInfoIs16( info ) )
426 ERR("called on PE resource!\n" );
430 TRACE("(%p,%s,%p,%p)\n",
431 pBlock, debugstr_a(lpSubBlock), lplpBuffer, puLen );
433 while ( *lpSubBlock )
435 /* Find next path component */
437 for ( lpNextSlash = lpSubBlock; *lpNextSlash; lpNextSlash++ )
438 if ( *lpNextSlash == '\\' )
441 /* Skip empty components */
442 if ( lpNextSlash == lpSubBlock )
448 /* We have a non-empty component: search info for key */
449 info = VersionInfo16_FindChild( info, lpSubBlock, lpNextSlash-lpSubBlock );
450 if ( !info ) return FALSE;
452 /* Skip path component */
453 lpSubBlock = lpNextSlash;
457 *lplpBuffer = VersionInfo16_Value( info );
458 *puLen = info->wValueLength;
463 /***********************************************************************
464 * VerQueryValueW [VERSION.13]
466 DWORD WINAPI VerQueryValueW( LPVOID pBlock, LPCWSTR lpSubBlock,
467 LPVOID *lplpBuffer, UINT *puLen )
469 VS_VERSION_INFO_STRUCT32 *info = (VS_VERSION_INFO_STRUCT32 *)pBlock;
470 if ( VersionInfoIs16( info ) )
472 ERR("called on NE resource!\n" );
476 TRACE("(%p,%s,%p,%p)\n",
477 pBlock, debugstr_w(lpSubBlock), lplpBuffer, puLen );
479 while ( *lpSubBlock )
481 /* Find next path component */
483 for ( lpNextSlash = lpSubBlock; *lpNextSlash; lpNextSlash++ )
484 if ( *lpNextSlash == '\\' )
487 /* Skip empty components */
488 if ( lpNextSlash == lpSubBlock )
494 /* We have a non-empty component: search info for key */
495 info = VersionInfo32_FindChild( info, lpSubBlock, lpNextSlash-lpSubBlock );
496 if ( !info ) return FALSE;
498 /* Skip path component */
499 lpSubBlock = lpNextSlash;
503 *lplpBuffer = VersionInfo32_Value( info );
504 *puLen = info->wValueLength;