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/winuser16.h"
15 #include "wine/unicode.h"
17 #include "debugtools.h"
19 DEFAULT_DEBUG_CHANNEL(ver);
22 /******************************************************************************
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.
29 *****************************************************************************/
30 static void print_vffi_debug(VS_FIXEDFILEINFO *vffi)
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," : ""
48 DPRINTF(" OS=0x%x.0x%x ",
49 HIWORD(vffi->dwFileOS),
50 LOWORD(vffi->dwFileOS)
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;
59 DPRINTF("UNKNOWN(0x%lx),",vffi->dwFileOS&0xFFFF0000);break;
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;
71 switch (vffi->dwFileType) {
74 TRACE("filetype=Unknown(0x%lx)",vffi->dwFileType);
76 case VFT_APP:TRACE("filetype=APP,");break;
77 case VFT_DLL:TRACE("filetype=DLL,");break;
79 TRACE("filetype=DRV,");
80 switch(vffi->dwFileSubtype) {
83 DPRINTF("UNKNOWN(0x%lx)",vffi->dwFileSubtype);
85 case VFT2_DRV_PRINTER:
88 case VFT2_DRV_KEYBOARD:
91 case VFT2_DRV_LANGUAGE:
94 case VFT2_DRV_DISPLAY:
100 case VFT2_DRV_NETWORK:
103 case VFT2_DRV_SYSTEM:
106 case VFT2_DRV_INSTALLABLE:
107 DPRINTF("INSTALLABLE");
115 case VFT2_DRV_INPUTMETHOD:
116 DPRINTF("INPUTMETHOD");
121 TRACE("filetype=FONT.");
122 switch (vffi->dwFileSubtype) {
124 DPRINTF("UNKNOWN(0x%lx)",vffi->dwFileSubtype);
126 case VFT2_FONT_RASTER:DPRINTF("RASTER");break;
127 case VFT2_FONT_VECTOR:DPRINTF("VECTOR");break;
128 case VFT2_FONT_TRUETYPE:DPRINTF("TRUETYPE");break;
131 case VFT_VXD:TRACE("filetype=VXD");break;
132 case VFT_STATIC_LIB:TRACE("filetype=STATIC_LIB");break;
135 TRACE(" filedata=0x%lx.0x%lx\n",
136 vffi->dwFileDateMS,vffi->dwFileDateLS);
140 /***********************************************************************
141 * Version Info Structure
149 #if 0 /* variable length structure */
153 VS_VERSION_INFO_STRUCT16 Children[];
155 } VS_VERSION_INFO_STRUCT16;
163 #if 0 /* variable length structure */
167 VS_VERSION_INFO_STRUCT32 Children[];
169 } VS_VERSION_INFO_STRUCT32;
171 #define VersionInfoIs16( ver ) \
172 ( ((VS_VERSION_INFO_STRUCT16 *)ver)->szKey[0] >= ' ' )
174 #define DWORD_ALIGN( base, ptr ) \
175 ( (LPBYTE)(base) + ((((LPBYTE)(ptr) - (LPBYTE)(base)) + 3) & ~3) )
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 )
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 ) )
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) )
195 /***********************************************************************
196 * ConvertVersionInfo32To16 [internal]
198 void ConvertVersionInfo32To16( VS_VERSION_INFO_STRUCT32 *info32,
199 VS_VERSION_INFO_STRUCT16 *info16 )
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;
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 );
214 WideCharToMultiByte( CP_ACP, 0, info32->szKey, -1, info16->szKey, 0x7fffffff, NULL, NULL );
216 TRACE("Copied key from %p to %p: %s\n", info32->szKey, info16->szKey,
217 debugstr_a(info16->szKey) );
220 if ( wValueLength == 0 )
222 info16->wValueLength = 0;
223 TRACE("No value present\n" );
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 );
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 = GetFileResourceSize16( filename,
276 MAKEINTRESOURCEA(VS_FILE_INFO),
277 MAKEINTRESOURCEA(VS_VERSION_INFO),
281 ret = GetFileResource16( 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 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 );
323 /***********************************************************************
324 * GetFileVersionInfoA [VERSION.1]
326 DWORD WINAPI GetFileVersionInfoA( LPCSTR filename, DWORD handle,
327 DWORD datasize, LPVOID data )
329 TRACE("(%s,%ld,size=%ld,data=%p)\n",
330 debugstr_a(filename), handle, datasize, data );
332 if ( !GetFileResource16( filename, MAKEINTRESOURCEA(VS_FILE_INFO),
333 MAKEINTRESOURCEA(VS_VERSION_INFO),
334 handle, datasize, data ) )
337 if ( datasize >= sizeof(VS_VERSION_INFO_STRUCT16)
338 && datasize >= ((VS_VERSION_INFO_STRUCT16 *)data)->wLength
339 && !VersionInfoIs16( data ) )
341 /* convert resource from PE format to NE format */
342 ConvertVersionInfo32To16( (VS_VERSION_INFO_STRUCT32 *)data,
343 (VS_VERSION_INFO_STRUCT16 *)data );
349 /***********************************************************************
350 * GetFileVersionInfoW [VERSION.4]
352 DWORD WINAPI GetFileVersionInfoW( LPCWSTR filename, DWORD handle,
353 DWORD datasize, LPVOID data )
355 DWORD len = WideCharToMultiByte( CP_ACP, 0, filename, -1, NULL, 0, NULL, NULL );
356 LPSTR fn = HeapAlloc( GetProcessHeap(), 0, len );
359 WideCharToMultiByte( CP_ACP, 0, filename, -1, fn, len, NULL, NULL );
361 TRACE("(%s,%ld,size=%ld,data=%p)\n",
362 debugstr_w(filename), handle, datasize, data );
364 if ( !GetFileResource16( fn, MAKEINTRESOURCEA(VS_FILE_INFO),
365 MAKEINTRESOURCEA(VS_VERSION_INFO),
366 handle, datasize, data ) )
369 else if ( datasize >= sizeof(VS_VERSION_INFO_STRUCT16)
370 && datasize >= ((VS_VERSION_INFO_STRUCT16 *)data)->wLength
371 && VersionInfoIs16( data ) )
373 ERR("Cannot access NE resource in %s\n", debugstr_a(fn) );
377 HeapFree( GetProcessHeap(), 0, fn );
382 /***********************************************************************
383 * VersionInfo16_FindChild [internal]
385 static VS_VERSION_INFO_STRUCT16 *VersionInfo16_FindChild( VS_VERSION_INFO_STRUCT16 *info,
386 LPCSTR szKey, UINT cbKey )
388 VS_VERSION_INFO_STRUCT16 *child = VersionInfo16_Children( info );
390 while ( (DWORD)child < (DWORD)info + info->wLength )
392 if ( !strncasecmp( child->szKey, szKey, cbKey ) )
395 if (!(child->wLength)) return NULL;
396 child = VersionInfo16_Next( child );
402 /***********************************************************************
403 * VersionInfo32_FindChild [internal]
405 static VS_VERSION_INFO_STRUCT32 *VersionInfo32_FindChild( VS_VERSION_INFO_STRUCT32 *info,
406 LPCWSTR szKey, UINT cbKey )
408 VS_VERSION_INFO_STRUCT32 *child = VersionInfo32_Children( info );
410 while ( (DWORD)child < (DWORD)info + info->wLength )
412 if ( !strncmpiW( child->szKey, szKey, cbKey ) )
415 child = VersionInfo32_Next( child );
421 /***********************************************************************
422 * VerQueryValueA [VERSION.12]
424 DWORD WINAPI VerQueryValueA( LPVOID pBlock, LPCSTR lpSubBlock,
425 LPVOID *lplpBuffer, UINT *puLen )
427 VS_VERSION_INFO_STRUCT16 *info = (VS_VERSION_INFO_STRUCT16 *)pBlock;
428 if ( !VersionInfoIs16( info ) )
430 ERR("called on PE resource!\n" );
434 TRACE("(%p,%s,%p,%p)\n",
435 pBlock, debugstr_a(lpSubBlock), lplpBuffer, puLen );
437 while ( *lpSubBlock )
439 /* Find next path component */
441 for ( lpNextSlash = lpSubBlock; *lpNextSlash; lpNextSlash++ )
442 if ( *lpNextSlash == '\\' )
445 /* Skip empty components */
446 if ( lpNextSlash == lpSubBlock )
452 /* We have a non-empty component: search info for key */
453 info = VersionInfo16_FindChild( info, lpSubBlock, lpNextSlash-lpSubBlock );
454 if ( !info ) return FALSE;
456 /* Skip path component */
457 lpSubBlock = lpNextSlash;
461 *lplpBuffer = VersionInfo16_Value( info );
462 *puLen = info->wValueLength;
467 /***********************************************************************
468 * VerQueryValueW [VERSION.13]
470 DWORD WINAPI VerQueryValueW( LPVOID pBlock, LPCWSTR lpSubBlock,
471 LPVOID *lplpBuffer, UINT *puLen )
473 VS_VERSION_INFO_STRUCT32 *info = (VS_VERSION_INFO_STRUCT32 *)pBlock;
474 if ( VersionInfoIs16( info ) )
476 ERR("called on NE resource!\n" );
480 TRACE("(%p,%s,%p,%p)\n",
481 pBlock, debugstr_w(lpSubBlock), lplpBuffer, puLen );
483 while ( *lpSubBlock )
485 /* Find next path component */
487 for ( lpNextSlash = lpSubBlock; *lpNextSlash; lpNextSlash++ )
488 if ( *lpNextSlash == '\\' )
491 /* Skip empty components */
492 if ( lpNextSlash == lpSubBlock )
498 /* We have a non-empty component: search info for key */
499 info = VersionInfo32_FindChild( info, lpSubBlock, lpNextSlash-lpSubBlock );
500 if ( !info ) return FALSE;
502 /* Skip path component */
503 lpSubBlock = lpNextSlash;
507 *lplpBuffer = VersionInfo32_Value( info );
508 *puLen = info->wValueLength;