wined3d: Fix missing break inside switch in SetAutoGenFilterType.
[wine.git] / dlls / version / info.c
blob395895b58ca3f37b142cba57ecf960fc58b14739
1 /*
2 * Implementation of VERSION.DLL - Version Info access
4 * Copyright 1996,1997 Marcus Meissner
5 * Copyright 1997 David Cuthbert
6 * Copyright 1999 Ulrich Weigand
7 * Copyright 2005 Paul Vriens
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include <stdarg.h>
26 #include <stdlib.h>
27 #include <string.h>
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winver.h"
32 #include "winternl.h"
33 #include "wine/winuser16.h"
34 #include "wine/unicode.h"
35 #include "winerror.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(ver);
40 /******************************************************************************
42 * This function will print via standard TRACE, debug info regarding
43 * the file info structure vffi.
44 * 15-Feb-1998 Dimitrie Paun (dimi@cs.toronto.edu)
45 * Added this function to clean up the code.
47 *****************************************************************************/
48 static void print_vffi_debug(const VS_FIXEDFILEINFO *vffi)
50 BOOL versioned_printer = FALSE;
52 if((vffi->dwFileType == VFT_DLL) || (vffi->dwFileType == VFT_DRV))
54 if(vffi->dwFileSubtype == VFT2_DRV_VERSIONED_PRINTER)
55 /* this is documented for newer w2k Drivers and up */
56 versioned_printer = TRUE;
57 else if( (vffi->dwFileSubtype == VFT2_DRV_PRINTER) &&
58 (vffi->dwFileVersionMS != vffi->dwProductVersionMS) &&
59 (vffi->dwFileVersionMS > 0) &&
60 (vffi->dwFileVersionMS <= 3) )
61 /* found this on NT 3.51, NT4.0 and old w2k Drivers */
62 versioned_printer = TRUE;
65 TRACE("structversion=%u.%u, ",
66 HIWORD(vffi->dwStrucVersion),LOWORD(vffi->dwStrucVersion));
67 if(versioned_printer)
69 WORD mode = LOWORD(vffi->dwFileVersionMS);
70 WORD ver_rev = HIWORD(vffi->dwFileVersionLS);
71 TRACE("fileversion=%u.%u.%u.%u (%s.major.minor.release), ",
72 (vffi->dwFileVersionMS),
73 HIBYTE(ver_rev), LOBYTE(ver_rev), LOWORD(vffi->dwFileVersionLS),
74 (mode == 3) ? "Usermode" : ((mode <= 2) ? "Kernelmode" : "?") );
76 else
78 TRACE("fileversion=%u.%u.%u.%u, ",
79 HIWORD(vffi->dwFileVersionMS),LOWORD(vffi->dwFileVersionMS),
80 HIWORD(vffi->dwFileVersionLS),LOWORD(vffi->dwFileVersionLS));
82 TRACE("productversion=%u.%u.%u.%u\n",
83 HIWORD(vffi->dwProductVersionMS),LOWORD(vffi->dwProductVersionMS),
84 HIWORD(vffi->dwProductVersionLS),LOWORD(vffi->dwProductVersionLS));
86 TRACE("flagmask=0x%x, flags=0x%x %s%s%s%s%s%s\n",
87 vffi->dwFileFlagsMask, vffi->dwFileFlags,
88 (vffi->dwFileFlags & VS_FF_DEBUG) ? "DEBUG," : "",
89 (vffi->dwFileFlags & VS_FF_PRERELEASE) ? "PRERELEASE," : "",
90 (vffi->dwFileFlags & VS_FF_PATCHED) ? "PATCHED," : "",
91 (vffi->dwFileFlags & VS_FF_PRIVATEBUILD) ? "PRIVATEBUILD," : "",
92 (vffi->dwFileFlags & VS_FF_INFOINFERRED) ? "INFOINFERRED," : "",
93 (vffi->dwFileFlags & VS_FF_SPECIALBUILD) ? "SPECIALBUILD," : "");
95 TRACE("(");
97 TRACE("OS=0x%x.0x%x ", HIWORD(vffi->dwFileOS), LOWORD(vffi->dwFileOS));
99 switch (vffi->dwFileOS&0xFFFF0000)
101 case VOS_DOS:TRACE("DOS,");break;
102 case VOS_OS216:TRACE("OS/2-16,");break;
103 case VOS_OS232:TRACE("OS/2-32,");break;
104 case VOS_NT:TRACE("NT,");break;
105 case VOS_UNKNOWN:
106 default:
107 TRACE("UNKNOWN(0x%x),",vffi->dwFileOS&0xFFFF0000);break;
110 switch (LOWORD(vffi->dwFileOS))
112 case VOS__BASE:TRACE("BASE");break;
113 case VOS__WINDOWS16:TRACE("WIN16");break;
114 case VOS__WINDOWS32:TRACE("WIN32");break;
115 case VOS__PM16:TRACE("PM16");break;
116 case VOS__PM32:TRACE("PM32");break;
117 default:
118 TRACE("UNKNOWN(0x%x)",LOWORD(vffi->dwFileOS));break;
121 TRACE(")\n");
123 switch (vffi->dwFileType)
125 case VFT_APP:TRACE("filetype=APP");break;
126 case VFT_DLL:
127 TRACE("filetype=DLL");
128 if(vffi->dwFileSubtype != 0)
130 if(versioned_printer) /* NT3.x/NT4.0 or old w2k Driver */
131 TRACE(",PRINTER");
132 TRACE(" (subtype=0x%x)", vffi->dwFileSubtype);
134 break;
135 case VFT_DRV:
136 TRACE("filetype=DRV,");
137 switch(vffi->dwFileSubtype)
139 case VFT2_DRV_PRINTER:TRACE("PRINTER");break;
140 case VFT2_DRV_KEYBOARD:TRACE("KEYBOARD");break;
141 case VFT2_DRV_LANGUAGE:TRACE("LANGUAGE");break;
142 case VFT2_DRV_DISPLAY:TRACE("DISPLAY");break;
143 case VFT2_DRV_MOUSE:TRACE("MOUSE");break;
144 case VFT2_DRV_NETWORK:TRACE("NETWORK");break;
145 case VFT2_DRV_SYSTEM:TRACE("SYSTEM");break;
146 case VFT2_DRV_INSTALLABLE:TRACE("INSTALLABLE");break;
147 case VFT2_DRV_SOUND:TRACE("SOUND");break;
148 case VFT2_DRV_COMM:TRACE("COMM");break;
149 case VFT2_DRV_INPUTMETHOD:TRACE("INPUTMETHOD");break;
150 case VFT2_DRV_VERSIONED_PRINTER:TRACE("VERSIONED_PRINTER");break;
151 case VFT2_UNKNOWN:
152 default:
153 TRACE("UNKNOWN(0x%x)",vffi->dwFileSubtype);break;
155 break;
156 case VFT_FONT:
157 TRACE("filetype=FONT,");
158 switch (vffi->dwFileSubtype)
160 case VFT2_FONT_RASTER:TRACE("RASTER");break;
161 case VFT2_FONT_VECTOR:TRACE("VECTOR");break;
162 case VFT2_FONT_TRUETYPE:TRACE("TRUETYPE");break;
163 default:TRACE("UNKNOWN(0x%x)",vffi->dwFileSubtype);break;
165 break;
166 case VFT_VXD:TRACE("filetype=VXD");break;
167 case VFT_STATIC_LIB:TRACE("filetype=STATIC_LIB");break;
168 case VFT_UNKNOWN:
169 default:
170 TRACE("filetype=Unknown(0x%x)",vffi->dwFileType);break;
173 TRACE("\n");
174 TRACE("filedate=0x%x.0x%x\n",vffi->dwFileDateMS,vffi->dwFileDateLS);
177 /***********************************************************************
178 * Version Info Structure
181 typedef struct
183 WORD wLength;
184 WORD wValueLength;
185 CHAR szKey[1];
186 #if 0 /* variable length structure */
187 /* DWORD aligned */
188 BYTE Value[];
189 /* DWORD aligned */
190 VS_VERSION_INFO_STRUCT16 Children[];
191 #endif
192 } VS_VERSION_INFO_STRUCT16;
194 typedef struct
196 WORD wLength;
197 WORD wValueLength;
198 WORD wType;
199 WCHAR szKey[1];
200 #if 0 /* variable length structure */
201 /* DWORD aligned */
202 BYTE Value[];
203 /* DWORD aligned */
204 VS_VERSION_INFO_STRUCT32 Children[];
205 #endif
206 } VS_VERSION_INFO_STRUCT32;
208 #define VersionInfoIs16( ver ) \
209 ( ((const VS_VERSION_INFO_STRUCT16 *)ver)->szKey[0] >= ' ' )
211 #define DWORD_ALIGN( base, ptr ) \
212 ( (LPBYTE)(base) + ((((LPBYTE)(ptr) - (LPBYTE)(base)) + 3) & ~3) )
214 #define VersionInfo16_Value( ver ) \
215 DWORD_ALIGN( (ver), (ver)->szKey + strlen((ver)->szKey) + 1 )
216 #define VersionInfo32_Value( ver ) \
217 DWORD_ALIGN( (ver), (ver)->szKey + strlenW((ver)->szKey) + 1 )
219 #define VersionInfo16_Children( ver ) \
220 (const VS_VERSION_INFO_STRUCT16 *)( VersionInfo16_Value( ver ) + \
221 ( ( (ver)->wValueLength + 3 ) & ~3 ) )
222 #define VersionInfo32_Children( ver ) \
223 (const VS_VERSION_INFO_STRUCT32 *)( VersionInfo32_Value( ver ) + \
224 ( ( (ver)->wValueLength * \
225 ((ver)->wType? 2 : 1) + 3 ) & ~3 ) )
227 #define VersionInfo16_Next( ver ) \
228 (VS_VERSION_INFO_STRUCT16 *)( (LPBYTE)ver + (((ver)->wLength + 3) & ~3) )
229 #define VersionInfo32_Next( ver ) \
230 (VS_VERSION_INFO_STRUCT32 *)( (LPBYTE)ver + (((ver)->wLength + 3) & ~3) )
232 /***********************************************************************
233 * VERSION_GetFileVersionInfo_PE [internal]
235 * NOTE: returns size of the PE VERSION resource or 0xFFFFFFFF
236 * in the case the file is a PE module, but VERSION_INFO not found.
238 static DWORD VERSION_GetFileVersionInfo_PE( LPCWSTR filename, DWORD datasize, LPVOID data )
240 const VS_FIXEDFILEINFO *vffi;
241 DWORD len;
242 BYTE *buf;
243 HMODULE hModule;
244 HRSRC hRsrc;
245 HGLOBAL hMem;
247 TRACE("%s\n", debugstr_w(filename));
249 if (!GetModuleHandleExW(0, filename, &hModule))
250 hModule = LoadLibraryExW(filename, 0, LOAD_LIBRARY_AS_DATAFILE);
252 if(!hModule)
254 WARN("Could not load %s\n", debugstr_w(filename));
256 return 0;
258 hRsrc = FindResourceW(hModule,
259 MAKEINTRESOURCEW(VS_VERSION_INFO),
260 MAKEINTRESOURCEW(VS_FILE_INFO));
261 if(!hRsrc)
263 WARN("Could not find VS_VERSION_INFO in %s\n", debugstr_w(filename));
264 FreeLibrary(hModule);
265 return 0xFFFFFFFF;
267 len = SizeofResource(hModule, hRsrc);
268 hMem = LoadResource(hModule, hRsrc);
269 if(!hMem)
271 WARN("Could not load VS_VERSION_INFO from %s\n", debugstr_w(filename));
272 FreeLibrary(hModule);
273 return 0xFFFFFFFF;
275 buf = LockResource(hMem);
277 vffi = (VS_FIXEDFILEINFO *)VersionInfo32_Value( (VS_VERSION_INFO_STRUCT32 *)buf );
279 if ( vffi->dwSignature != VS_FFI_SIGNATURE )
281 WARN("vffi->dwSignature is 0x%08x, but not 0x%08lx!\n",
282 vffi->dwSignature, VS_FFI_SIGNATURE );
283 len = 0xFFFFFFFF;
284 goto END;
287 if ( TRACE_ON(ver) )
288 print_vffi_debug( vffi );
290 if(data)
292 if(datasize < len)
293 len = datasize; /* truncate data */
294 if(len)
295 memcpy(data, buf, len);
296 else
297 len = 0xFFFFFFFF;
299 END:
300 FreeResource(hMem);
301 FreeLibrary(hModule);
303 return len;
306 /***********************************************************************
307 * VERSION_GetFileVersionInfo_16 [internal]
309 * NOTE: returns size of the 16-bit VERSION resource or 0xFFFFFFFF
310 * in the case the file exists, but VERSION_INFO not found.
312 static DWORD VERSION_GetFileVersionInfo_16( LPCSTR filename, DWORD datasize, LPVOID data )
314 const VS_FIXEDFILEINFO *vffi;
315 DWORD len, offset;
316 BYTE *buf;
317 HMODULE16 hModule;
318 HRSRC16 hRsrc;
319 HGLOBAL16 hMem;
320 char dllname[20], owner[20], *p;
321 const char *basename;
322 BOOL is_builtin = FALSE;
324 TRACE("%s\n", debugstr_a(filename));
326 /* strip path information */
328 basename = filename;
329 if (basename[0] && basename[1] == ':') basename += 2; /* strip drive specification */
330 if ((p = strrchr( basename, '\\' ))) basename = p + 1;
331 if ((p = strrchr( basename, '/' ))) basename = p + 1;
333 if (strlen(basename) < sizeof(dllname)-4)
335 int file_exists;
337 strcpy( dllname, basename );
338 p = strrchr( dllname, '.' );
339 if (!p) strcat( dllname, ".dll" );
340 for (p = dllname; *p; p++) if (*p >= 'A' && *p <= 'Z') *p += 32;
342 if (wine_dll_get_owner( dllname, owner, sizeof(owner), &file_exists ) == 0)
343 is_builtin = TRUE;
346 /* first try without loading a 16-bit module */
347 if (is_builtin)
348 len = 0;
349 else
350 len = GetFileResourceSize16( filename,
351 MAKEINTRESOURCEA(VS_FILE_INFO),
352 MAKEINTRESOURCEA(VS_VERSION_INFO),
353 &offset );
354 if (len)
356 if (!data) return len;
358 len = GetFileResource16( filename,
359 MAKEINTRESOURCEA(VS_FILE_INFO),
360 MAKEINTRESOURCEA(VS_VERSION_INFO),
361 offset, datasize, data );
362 if (len)
364 vffi = (VS_FIXEDFILEINFO *)VersionInfo16_Value( (VS_VERSION_INFO_STRUCT16 *)data );
366 if ( vffi->dwSignature == VS_FFI_SIGNATURE )
368 if ( ((VS_VERSION_INFO_STRUCT16 *)data)->wLength < len )
369 len = ((VS_VERSION_INFO_STRUCT16 *)data)->wLength;
371 if ( TRACE_ON(ver) )
372 print_vffi_debug( vffi );
374 return len;
379 /* this might be a builtin 16-bit module */
380 hModule = LoadLibrary16(filename);
381 if(hModule < 32)
383 WARN("Could not load %s\n", debugstr_a(filename));
384 if (hModule == ERROR_BAD_FORMAT)
385 return 0xFFFFFFFF;
386 else
387 return 0x0;
389 hRsrc = FindResource16(hModule,
390 MAKEINTRESOURCEA(VS_VERSION_INFO),
391 MAKEINTRESOURCEA(VS_FILE_INFO));
392 if(!hRsrc)
394 WARN("Could not find VS_VERSION_INFO in %s\n", debugstr_a(filename));
395 FreeLibrary16(hModule);
396 return 0xFFFFFFFF;
398 len = SizeofResource16(hModule, hRsrc);
399 hMem = LoadResource16(hModule, hRsrc);
400 if(!hMem)
402 WARN("Could not load VS_VERSION_INFO from %s\n", debugstr_a(filename));
403 FreeLibrary16(hModule);
404 return 0xFFFFFFFF;
406 buf = LockResource16(hMem);
408 if(!VersionInfoIs16(buf))
410 len = 0xFFFFFFFF;
411 goto END;
414 vffi = (VS_FIXEDFILEINFO *)VersionInfo16_Value( (VS_VERSION_INFO_STRUCT16 *)buf );
416 if ( vffi->dwSignature != VS_FFI_SIGNATURE )
418 WARN("vffi->dwSignature is 0x%08x, but not 0x%08lx!\n",
419 vffi->dwSignature, VS_FFI_SIGNATURE );
420 len = 0xFFFFFFFF;
421 goto END;
424 if ( TRACE_ON(ver) )
425 print_vffi_debug( vffi );
427 if(data)
429 if(datasize < len)
430 len = datasize; /* truncate data */
431 if(len)
432 memcpy(data, buf, len);
433 else
434 len = 0xFFFFFFFF;
436 END:
437 FreeResource16(hMem);
438 FreeLibrary16(hModule);
440 return len;
443 /***********************************************************************
444 * GetFileVersionInfoSizeW [VERSION.@]
446 DWORD WINAPI GetFileVersionInfoSizeW( LPCWSTR filename, LPDWORD handle )
448 DWORD len;
450 TRACE("(%s,%p)\n", debugstr_w(filename), handle );
452 if (handle) *handle = 0;
454 if (!filename)
456 SetLastError(ERROR_INVALID_PARAMETER);
457 return 0;
459 if (!*filename)
461 SetLastError(ERROR_BAD_PATHNAME);
462 return 0;
465 len = VERSION_GetFileVersionInfo_PE(filename, 0, NULL);
466 /* 0xFFFFFFFF means: file is a PE module, but VERSION_INFO not found */
467 if(len == 0xFFFFFFFF)
469 SetLastError(ERROR_RESOURCE_DATA_NOT_FOUND);
470 return 0;
473 if (!len)
475 LPSTR filenameA;
477 len = WideCharToMultiByte( CP_ACP, 0, filename, -1, NULL, 0, NULL, NULL );
478 filenameA = HeapAlloc( GetProcessHeap(), 0, len );
479 WideCharToMultiByte( CP_ACP, 0, filename, -1, filenameA, len, NULL, NULL );
481 len = VERSION_GetFileVersionInfo_16(filenameA, 0, NULL);
482 HeapFree( GetProcessHeap(), 0, filenameA );
483 /* 0xFFFFFFFF means: file exists, but VERSION_INFO not found */
484 if (!len)
486 SetLastError(ERROR_FILE_NOT_FOUND);
487 return 0;
489 if (len == 0xFFFFFFFF)
491 SetLastError(ERROR_RESOURCE_DATA_NOT_FOUND);
492 return 0;
495 /* We have a 16bit resource.
497 * XP/W2K/W2K3 uses a buffer which is more than the actual needed space:
499 * (info->wLength - sizeof(VS_FIXEDFILEINFO)) * 4
501 * This extra buffer is used for ANSI to Unicode conversions in W-Calls.
502 * info->wLength should be the same as len. Currently it isn't but that
503 * doesn't seem to be a problem (len is bigger than info->wLength).
505 len = (len - sizeof(VS_FIXEDFILEINFO)) * 4;
507 else
509 /* We have a 32bit resource.
511 * XP/W2K/W2K3 uses a buffer which is 2 times the actual needed space + 4 bytes "FE2X"
512 * This extra buffer is used for Unicode to ANSI conversions in A-Calls
514 len = (len * 2) + 4;
517 SetLastError(0);
518 return len;
521 /***********************************************************************
522 * GetFileVersionInfoSizeA [VERSION.@]
524 DWORD WINAPI GetFileVersionInfoSizeA( LPCSTR filename, LPDWORD handle )
526 UNICODE_STRING filenameW;
527 DWORD retval;
529 TRACE("(%s,%p)\n", debugstr_a(filename), handle );
531 if(filename)
532 RtlCreateUnicodeStringFromAsciiz(&filenameW, filename);
533 else
534 filenameW.Buffer = NULL;
536 retval = GetFileVersionInfoSizeW(filenameW.Buffer, handle);
538 RtlFreeUnicodeString(&filenameW);
540 return retval;
543 /***********************************************************************
544 * GetFileVersionInfoW [VERSION.@]
546 BOOL WINAPI GetFileVersionInfoW( LPCWSTR filename, DWORD handle,
547 DWORD datasize, LPVOID data )
549 DWORD len;
550 VS_VERSION_INFO_STRUCT32* vvis = (VS_VERSION_INFO_STRUCT32*)data;
552 TRACE("(%s,%d,size=%d,data=%p)\n",
553 debugstr_w(filename), handle, datasize, data );
555 if (!data)
557 SetLastError(ERROR_INVALID_DATA);
558 return FALSE;
560 len = VERSION_GetFileVersionInfo_PE(filename, datasize, data);
561 /* 0xFFFFFFFF means: file is a PE module, but VERSION_INFO not found */
562 if (len == 0xFFFFFFFF)
564 SetLastError(ERROR_RESOURCE_DATA_NOT_FOUND);
565 return FALSE;
568 if (!len)
570 LPSTR filenameA;
572 len = WideCharToMultiByte( CP_ACP, 0, filename, -1, NULL, 0, NULL, NULL );
573 filenameA = HeapAlloc( GetProcessHeap(), 0, len );
574 WideCharToMultiByte( CP_ACP, 0, filename, -1, filenameA, len, NULL, NULL );
576 len = VERSION_GetFileVersionInfo_16(filenameA, datasize, data);
577 HeapFree( GetProcessHeap(), 0, filenameA );
578 /* 0xFFFFFFFF means: file exists, but VERSION_INFO not found */
579 if (!len || len == 0xFFFFFFFF)
581 SetLastError(ERROR_RESOURCE_DATA_NOT_FOUND);
582 return FALSE;
584 /* We have a 16bit resource. */
586 else
588 static const char signature[] = "FE2X";
589 DWORD bufsize = vvis->wLength + strlen(signature);
590 DWORD convbuf;
592 /* We have a 32bit resource.
594 * XP/W2K/W2K3 uses a buffer which is 2 times the actual needed space + 4 bytes "FE2X"
595 * This extra buffer is used for Unicode to ANSI conversions in A-Calls
598 /* information is truncated to datasize bytes */
599 if (datasize >= bufsize)
601 convbuf = datasize - vvis->wLength;
602 memcpy( ((char*)(data))+vvis->wLength, signature, convbuf > 4 ? 4 : convbuf );
606 SetLastError(0);
607 return TRUE;
610 /***********************************************************************
611 * GetFileVersionInfoA [VERSION.@]
613 BOOL WINAPI GetFileVersionInfoA( LPCSTR filename, DWORD handle,
614 DWORD datasize, LPVOID data )
616 UNICODE_STRING filenameW;
617 BOOL retval;
619 TRACE("(%s,%d,size=%d,data=%p)\n",
620 debugstr_a(filename), handle, datasize, data );
622 if(filename)
623 RtlCreateUnicodeStringFromAsciiz(&filenameW, filename);
624 else
625 filenameW.Buffer = NULL;
627 retval = GetFileVersionInfoW(filenameW.Buffer, handle, datasize, data);
629 RtlFreeUnicodeString(&filenameW);
631 return retval;
634 /***********************************************************************
635 * VersionInfo16_FindChild [internal]
637 static const VS_VERSION_INFO_STRUCT16 *VersionInfo16_FindChild( const VS_VERSION_INFO_STRUCT16 *info,
638 LPCSTR szKey, UINT cbKey )
640 const VS_VERSION_INFO_STRUCT16 *child = VersionInfo16_Children( info );
642 while ((char *)child < (char *)info + info->wLength )
644 if (!strncasecmp( child->szKey, szKey, cbKey ) && !child->szKey[cbKey])
645 return child;
647 if (!(child->wLength)) return NULL;
648 child = VersionInfo16_Next( child );
651 return NULL;
654 /***********************************************************************
655 * VersionInfo32_FindChild [internal]
657 static const VS_VERSION_INFO_STRUCT32 *VersionInfo32_FindChild( const VS_VERSION_INFO_STRUCT32 *info,
658 LPCWSTR szKey, UINT cbKey )
660 const VS_VERSION_INFO_STRUCT32 *child = VersionInfo32_Children( info );
662 while ((char *)child < (char *)info + info->wLength )
664 if (!strncmpiW( child->szKey, szKey, cbKey ) && !child->szKey[cbKey])
665 return child;
667 child = VersionInfo32_Next( child );
670 return NULL;
673 /***********************************************************************
674 * VersionInfo16_QueryValue [internal]
676 * Gets a value from a 16-bit NE resource
678 static BOOL WINAPI VersionInfo16_QueryValue( const VS_VERSION_INFO_STRUCT16 *info, LPCSTR lpSubBlock,
679 LPVOID *lplpBuffer, UINT *puLen )
681 while ( *lpSubBlock )
683 /* Find next path component */
684 LPCSTR lpNextSlash;
685 for ( lpNextSlash = lpSubBlock; *lpNextSlash; lpNextSlash++ )
686 if ( *lpNextSlash == '\\' )
687 break;
689 /* Skip empty components */
690 if ( lpNextSlash == lpSubBlock )
692 lpSubBlock++;
693 continue;
696 /* We have a non-empty component: search info for key */
697 info = VersionInfo16_FindChild( info, lpSubBlock, lpNextSlash-lpSubBlock );
698 if ( !info )
700 if (puLen) *puLen = 0 ;
701 SetLastError( ERROR_RESOURCE_TYPE_NOT_FOUND );
702 return FALSE;
705 /* Skip path component */
706 lpSubBlock = lpNextSlash;
709 /* Return value */
710 *lplpBuffer = VersionInfo16_Value( info );
711 if (puLen)
712 *puLen = info->wValueLength;
714 return TRUE;
717 /***********************************************************************
718 * VersionInfo32_QueryValue [internal]
720 * Gets a value from a 32-bit PE resource
722 static BOOL WINAPI VersionInfo32_QueryValue( const VS_VERSION_INFO_STRUCT32 *info, LPCWSTR lpSubBlock,
723 LPVOID *lplpBuffer, UINT *puLen )
725 TRACE("lpSubBlock : (%s)\n", debugstr_w(lpSubBlock));
727 while ( *lpSubBlock )
729 /* Find next path component */
730 LPCWSTR lpNextSlash;
731 for ( lpNextSlash = lpSubBlock; *lpNextSlash; lpNextSlash++ )
732 if ( *lpNextSlash == '\\' )
733 break;
735 /* Skip empty components */
736 if ( lpNextSlash == lpSubBlock )
738 lpSubBlock++;
739 continue;
742 /* We have a non-empty component: search info for key */
743 info = VersionInfo32_FindChild( info, lpSubBlock, lpNextSlash-lpSubBlock );
744 if ( !info )
746 if (puLen) *puLen = 0 ;
747 SetLastError( ERROR_RESOURCE_TYPE_NOT_FOUND );
748 return FALSE;
751 /* Skip path component */
752 lpSubBlock = lpNextSlash;
755 /* Return value */
756 *lplpBuffer = VersionInfo32_Value( info );
757 if (puLen)
758 *puLen = info->wValueLength;
760 return TRUE;
763 /***********************************************************************
764 * VerQueryValueA [VERSION.@]
766 BOOL WINAPI VerQueryValueA( LPCVOID pBlock, LPCSTR lpSubBlock,
767 LPVOID *lplpBuffer, PUINT puLen )
769 static const char rootA[] = "\\";
770 static const char varfileinfoA[] = "\\VarFileInfo\\Translation";
771 const VS_VERSION_INFO_STRUCT16 *info = (const VS_VERSION_INFO_STRUCT16 *)pBlock;
773 TRACE("(%p,%s,%p,%p)\n",
774 pBlock, debugstr_a(lpSubBlock), lplpBuffer, puLen );
776 if (!pBlock)
777 return FALSE;
779 if ( !VersionInfoIs16( info ) )
781 BOOL ret;
782 INT len;
783 LPWSTR lpSubBlockW;
785 len = MultiByteToWideChar(CP_ACP, 0, lpSubBlock, -1, NULL, 0);
786 lpSubBlockW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
788 if (!lpSubBlockW)
789 return FALSE;
791 MultiByteToWideChar(CP_ACP, 0, lpSubBlock, -1, lpSubBlockW, len);
793 ret = VersionInfo32_QueryValue(pBlock, lpSubBlockW, lplpBuffer, puLen);
795 HeapFree(GetProcessHeap(), 0, lpSubBlockW);
797 if (ret && strcasecmp( lpSubBlock, rootA ) && strcasecmp( lpSubBlock, varfileinfoA ))
799 /* Set lpBuffer so it points to the 'empty' area where we store
800 * the converted strings
802 LPSTR lpBufferA = (LPSTR)pBlock + info->wLength + 4;
803 DWORD pos = (LPCSTR)*lplpBuffer - (LPCSTR)pBlock;
805 len = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)*lplpBuffer, -1,
806 lpBufferA + pos, info->wLength - pos, NULL, NULL);
807 *lplpBuffer = lpBufferA + pos;
808 *puLen = len;
810 return ret;
813 return VersionInfo16_QueryValue(info, lpSubBlock, lplpBuffer, puLen);
816 /***********************************************************************
817 * VerQueryValueW [VERSION.@]
819 BOOL WINAPI VerQueryValueW( LPCVOID pBlock, LPCWSTR lpSubBlock,
820 LPVOID *lplpBuffer, PUINT puLen )
822 static const WCHAR rootW[] = { '\\', 0 };
823 static const WCHAR varfileinfoW[] = { '\\','V','a','r','F','i','l','e','I','n','f','o',
824 '\\','T','r','a','n','s','l','a','t','i','o','n', 0 };
826 const VS_VERSION_INFO_STRUCT32 *info = (const VS_VERSION_INFO_STRUCT32 *)pBlock;
828 TRACE("(%p,%s,%p,%p)\n",
829 pBlock, debugstr_w(lpSubBlock), lplpBuffer, puLen );
831 if (!pBlock)
832 return FALSE;
834 if ( VersionInfoIs16( info ) )
836 BOOL ret;
837 int len;
838 LPSTR lpSubBlockA;
840 len = WideCharToMultiByte(CP_ACP, 0, lpSubBlock, -1, NULL, 0, NULL, NULL);
841 lpSubBlockA = HeapAlloc(GetProcessHeap(), 0, len * sizeof(char));
843 if (!lpSubBlockA)
844 return FALSE;
846 WideCharToMultiByte(CP_ACP, 0, lpSubBlock, -1, lpSubBlockA, len, NULL, NULL);
848 ret = VersionInfo16_QueryValue(pBlock, lpSubBlockA, lplpBuffer, puLen);
850 HeapFree(GetProcessHeap(), 0, lpSubBlockA);
852 if (ret && strcmpiW( lpSubBlock, rootW ) && strcmpiW( lpSubBlock, varfileinfoW ))
854 /* Set lpBuffer so it points to the 'empty' area where we store
855 * the converted strings
857 LPWSTR lpBufferW = (LPWSTR)((LPSTR)pBlock + info->wLength);
858 DWORD pos = (LPCSTR)*lplpBuffer - (LPCSTR)pBlock;
859 DWORD max = (info->wLength - sizeof(VS_FIXEDFILEINFO)) * 4 - info->wLength;
861 len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)*lplpBuffer, -1,
862 lpBufferW + pos, max/sizeof(WCHAR) - pos );
863 *lplpBuffer = lpBufferW + pos;
864 *puLen = len;
866 return ret;
869 return VersionInfo32_QueryValue(info, lpSubBlock, lplpBuffer, puLen);