MSVCRT___RTDynamicCast: Reject a NULL cppobj.
[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  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  * TODO
23  *   o Verify VerQueryValue()
24  */
25
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include "windef.h"
31 #include "winbase.h"
32 #include "winreg.h"
33 #include "winver.h"
34 #include "wine/winuser16.h"
35 #include "wine/unicode.h"
36 #include "winerror.h"
37 #include "wine/debug.h"
38
39 WINE_DEFAULT_DEBUG_CHANNEL(ver);
40
41
42 /******************************************************************************
43  *
44  *   This function will print via dprintf[_]ver to stddeb debug info regarding
45  *   the file info structure vffi.
46  *      15-Feb-1998 Dimitrie Paun (dimi@cs.toronto.edu)
47  *      Added this function to clean up the code.
48  *
49  *****************************************************************************/
50 static void print_vffi_debug(VS_FIXEDFILEINFO *vffi)
51 {
52         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",
53                     HIWORD(vffi->dwStrucVersion),LOWORD(vffi->dwStrucVersion),
54                     HIWORD(vffi->dwFileVersionMS),LOWORD(vffi->dwFileVersionMS),
55                     HIWORD(vffi->dwFileVersionLS),LOWORD(vffi->dwFileVersionLS),
56                     HIWORD(vffi->dwProductVersionMS),LOWORD(vffi->dwProductVersionMS),
57                     HIWORD(vffi->dwProductVersionLS),LOWORD(vffi->dwProductVersionLS),
58                     vffi->dwFileFlagsMask,
59                     (vffi->dwFileFlags & VS_FF_DEBUG) ? "DEBUG," : "",
60                     (vffi->dwFileFlags & VS_FF_PRERELEASE) ? "PRERELEASE," : "",
61                     (vffi->dwFileFlags & VS_FF_PATCHED) ? "PATCHED," : "",
62                     (vffi->dwFileFlags & VS_FF_PRIVATEBUILD) ? "PRIVATEBUILD," : "",
63                     (vffi->dwFileFlags & VS_FF_INFOINFERRED) ? "INFOINFERRED," : "",
64                     (vffi->dwFileFlags & VS_FF_SPECIALBUILD) ? "SPECIALBUILD," : ""
65                     );
66
67         TRACE("(");
68         TRACE(" OS=0x%x.0x%x ",
69                 HIWORD(vffi->dwFileOS),
70                 LOWORD(vffi->dwFileOS)
71         );
72         switch (vffi->dwFileOS&0xFFFF0000) {
73         case VOS_DOS:TRACE("DOS,");break;
74         case VOS_OS216:TRACE("OS/2-16,");break;
75         case VOS_OS232:TRACE("OS/2-32,");break;
76         case VOS_NT:TRACE("NT,");break;
77         case VOS_UNKNOWN:
78         default:
79                 TRACE("UNKNOWN(0x%lx),",vffi->dwFileOS&0xFFFF0000);break;
80         }
81         switch (LOWORD(vffi->dwFileOS)) {
82         case VOS__BASE:TRACE("BASE");break;
83         case VOS__WINDOWS16:TRACE("WIN16");break;
84         case VOS__WINDOWS32:TRACE("WIN32");break;
85         case VOS__PM16:TRACE("PM16");break;
86         case VOS__PM32:TRACE("PM32");break;
87         default:TRACE("UNKNOWN(0x%x)",LOWORD(vffi->dwFileOS));break;
88         }
89         TRACE(")\n");
90
91         switch (vffi->dwFileType) {
92         default:
93         case VFT_UNKNOWN:
94                 TRACE("filetype=Unknown(0x%lx)",vffi->dwFileType);
95                 break;
96         case VFT_APP:TRACE("filetype=APP,");break;
97         case VFT_DLL:TRACE("filetype=DLL,");break;
98         case VFT_DRV:
99                 TRACE("filetype=DRV,");
100                 switch(vffi->dwFileSubtype) {
101                 default:
102                 case VFT2_UNKNOWN:
103                         TRACE("UNKNOWN(0x%lx)",vffi->dwFileSubtype);
104                         break;
105                 case VFT2_DRV_PRINTER:
106                         TRACE("PRINTER");
107                         break;
108                 case VFT2_DRV_KEYBOARD:
109                         TRACE("KEYBOARD");
110                         break;
111                 case VFT2_DRV_LANGUAGE:
112                         TRACE("LANGUAGE");
113                         break;
114                 case VFT2_DRV_DISPLAY:
115                         TRACE("DISPLAY");
116                         break;
117                 case VFT2_DRV_MOUSE:
118                         TRACE("MOUSE");
119                         break;
120                 case VFT2_DRV_NETWORK:
121                         TRACE("NETWORK");
122                         break;
123                 case VFT2_DRV_SYSTEM:
124                         TRACE("SYSTEM");
125                         break;
126                 case VFT2_DRV_INSTALLABLE:
127                         TRACE("INSTALLABLE");
128                         break;
129                 case VFT2_DRV_SOUND:
130                         TRACE("SOUND");
131                         break;
132                 case VFT2_DRV_COMM:
133                         TRACE("COMM");
134                         break;
135                 case VFT2_DRV_INPUTMETHOD:
136                         TRACE("INPUTMETHOD");
137                         break;
138                 }
139                 break;
140         case VFT_FONT:
141                 TRACE("filetype=FONT.");
142                 switch (vffi->dwFileSubtype) {
143                 default:
144                         TRACE("UNKNOWN(0x%lx)",vffi->dwFileSubtype);
145                         break;
146                 case VFT2_FONT_RASTER:TRACE("RASTER");break;
147                 case VFT2_FONT_VECTOR:TRACE("VECTOR");break;
148                 case VFT2_FONT_TRUETYPE:TRACE("TRUETYPE");break;
149                 }
150                 break;
151         case VFT_VXD:TRACE("filetype=VXD");break;
152         case VFT_STATIC_LIB:TRACE("filetype=STATIC_LIB");break;
153         }
154         TRACE("\n");
155         TRACE("  filedata=0x%lx.0x%lx\n",
156                     vffi->dwFileDateMS,vffi->dwFileDateLS);
157 }
158
159
160 /***********************************************************************
161  * Version Info Structure
162  */
163
164 typedef struct
165 {
166     WORD  wLength;
167     WORD  wValueLength;
168     CHAR  szKey[1];
169 #if 0   /* variable length structure */
170     /* DWORD aligned */
171     BYTE  Value[];
172     /* DWORD aligned */
173     VS_VERSION_INFO_STRUCT16 Children[];
174 #endif
175 } VS_VERSION_INFO_STRUCT16;
176
177 typedef struct
178 {
179     WORD  wLength;
180     WORD  wValueLength;
181     WORD  bText;
182     WCHAR szKey[1];
183 #if 0   /* variable length structure */
184     /* DWORD aligned */
185     BYTE  Value[];
186     /* DWORD aligned */
187     VS_VERSION_INFO_STRUCT32 Children[];
188 #endif
189 } VS_VERSION_INFO_STRUCT32;
190
191 #define VersionInfoIs16( ver ) \
192     ( ((VS_VERSION_INFO_STRUCT16 *)ver)->szKey[0] >= ' ' )
193
194 #define DWORD_ALIGN( base, ptr ) \
195     ( (LPBYTE)(base) + ((((LPBYTE)(ptr) - (LPBYTE)(base)) + 3) & ~3) )
196
197 #define VersionInfo16_Value( ver )  \
198     DWORD_ALIGN( (ver), (ver)->szKey + strlen((ver)->szKey) + 1 )
199 #define VersionInfo32_Value( ver )  \
200     DWORD_ALIGN( (ver), (ver)->szKey + strlenW((ver)->szKey) + 1 )
201
202 #define VersionInfo16_Children( ver )  \
203     (VS_VERSION_INFO_STRUCT16 *)( VersionInfo16_Value( ver ) + \
204                            ( ( (ver)->wValueLength + 3 ) & ~3 ) )
205 #define VersionInfo32_Children( ver )  \
206     (VS_VERSION_INFO_STRUCT32 *)( VersionInfo32_Value( ver ) + \
207                            ( ( (ver)->wValueLength * \
208                                ((ver)->bText? 2 : 1) + 3 ) & ~3 ) )
209
210 #define VersionInfo16_Next( ver ) \
211     (VS_VERSION_INFO_STRUCT16 *)( (LPBYTE)ver + (((ver)->wLength + 3) & ~3) )
212 #define VersionInfo32_Next( ver ) \
213     (VS_VERSION_INFO_STRUCT32 *)( (LPBYTE)ver + (((ver)->wLength + 3) & ~3) )
214
215 /***********************************************************************
216  *           ConvertVersionInfo32To16        [internal]
217  */
218 void ConvertVersionInfo32To16( VS_VERSION_INFO_STRUCT32 *info32,
219                                VS_VERSION_INFO_STRUCT16 *info16 )
220 {
221     /* Copy data onto local stack to prevent overwrites */
222     WORD wLength = info32->wLength;
223     WORD wValueLength = info32->wValueLength;
224     WORD bText = info32->bText;
225     LPBYTE lpValue = VersionInfo32_Value( info32 );
226     VS_VERSION_INFO_STRUCT32 *child32 = VersionInfo32_Children( info32 );
227     VS_VERSION_INFO_STRUCT16 *child16;
228
229     TRACE("Converting %p to %p\n", info32, info16 );
230     TRACE("wLength %d, wValueLength %d, bText %d, value %p, child %p\n",
231                 wLength, wValueLength, bText, lpValue, child32 );
232
233     /* Convert key */
234     WideCharToMultiByte( CP_ACP, 0, info32->szKey, -1, info16->szKey, 0x7fffffff, NULL, NULL );
235
236     TRACE("Copied key from %p to %p: %s\n", info32->szKey, info16->szKey,
237                 debugstr_a(info16->szKey) );
238
239     /* Convert value */
240     if ( wValueLength == 0 )
241     {
242         info16->wValueLength = 0;
243         TRACE("No value present\n" );
244     }
245     else if ( bText )
246     {
247         info16->wValueLength = WideCharToMultiByte( CP_ACP, 0, (LPCWSTR)lpValue, -1, NULL, 0, NULL, NULL );
248         WideCharToMultiByte( CP_ACP, 0, (LPCWSTR)lpValue, -1,
249                              VersionInfo16_Value( info16 ), info16->wValueLength, NULL, NULL );
250
251         TRACE("Copied value from %p to %p: %s\n", lpValue,
252                     VersionInfo16_Value( info16 ),
253                     debugstr_a(VersionInfo16_Value( info16 )) );
254     }
255     else
256     {
257         info16->wValueLength = wValueLength;
258         memmove( VersionInfo16_Value( info16 ), lpValue, wValueLength );
259
260         TRACE("Copied value from %p to %p: %d bytes\n", lpValue,
261                      VersionInfo16_Value( info16 ), wValueLength );
262     }
263
264     /* Convert children */
265     child16 = VersionInfo16_Children( info16 );
266     while ( (DWORD)child32 < (DWORD)info32 + wLength && child32->wLength != 0 )
267     {
268         VS_VERSION_INFO_STRUCT32 *nextChild = VersionInfo32_Next( child32 );
269
270         ConvertVersionInfo32To16( child32, child16 );
271
272         child16 = VersionInfo16_Next( child16 );
273         child32 = nextChild;
274     }
275
276     /* Fixup length */
277     info16->wLength = (DWORD)child16 - (DWORD)info16;
278
279     TRACE("Finished, length is %d (%p - %p)\n",
280                 info16->wLength, info16, child16 );
281 }
282
283 /***********************************************************************
284  *           VERSION_GetFileVersionInfo_PE             [internal]
285  *
286  *    NOTE: returns size of the PE VERSION resource or 0xFFFFFFFF
287  *    in the case if file exists, but VERSION_INFO not found.
288  *    FIXME: handle is not used.
289  */
290 static DWORD VERSION_GetFileVersionInfo_PE( LPCSTR filename, LPDWORD handle,
291                                     DWORD datasize, LPVOID data )
292 {
293     VS_FIXEDFILEINFO *vffi;
294     DWORD len;
295     BYTE *buf;
296     HMODULE hModule;
297     HRSRC hRsrc;
298     HGLOBAL hMem;
299
300     TRACE("(%s,%p)\n", debugstr_a(filename), handle );
301
302     hModule = GetModuleHandleA(filename);
303     if(!hModule)
304         hModule = LoadLibraryExA(filename, 0, LOAD_LIBRARY_AS_DATAFILE);
305     else
306         hModule = LoadLibraryExA(filename, 0, 0);
307     if(!hModule)
308     {
309         WARN("Could not load %s\n", debugstr_a(filename));
310         return 0;
311     }
312     hRsrc = FindResourceW(hModule,
313                           MAKEINTRESOURCEW(VS_VERSION_INFO),
314                           MAKEINTRESOURCEW(VS_FILE_INFO));
315     if(!hRsrc)
316     {
317         WARN("Could not find VS_VERSION_INFO in %s\n", debugstr_a(filename));
318         FreeLibrary(hModule);
319         return 0xFFFFFFFF;
320     }
321     len = SizeofResource(hModule, hRsrc);
322     hMem = LoadResource(hModule, hRsrc);
323     if(!hMem)
324     {
325         WARN("Could not load VS_VERSION_INFO from %s\n", debugstr_a(filename));
326         FreeLibrary(hModule);
327         return 0xFFFFFFFF;
328     }
329     buf = LockResource(hMem);
330
331     vffi = (VS_FIXEDFILEINFO *)VersionInfo32_Value( (VS_VERSION_INFO_STRUCT32 *)buf );
332
333     if ( vffi->dwSignature != VS_FFI_SIGNATURE )
334     {
335         WARN("vffi->dwSignature is 0x%08lx, but not 0x%08lx!\n",
336                    vffi->dwSignature, VS_FFI_SIGNATURE );
337         len = 0xFFFFFFFF;
338         goto END;
339     }
340
341     if ( TRACE_ON(ver) )
342         print_vffi_debug( vffi );
343
344     if(data)
345     {
346         if(datasize < len)
347             len = datasize; /* truncate data */
348         if(len)
349             memcpy(data, buf, len);
350         else
351             len = 0xFFFFFFFF;
352     }
353 END:
354     FreeResource(hMem);
355     FreeLibrary(hModule);
356
357     return len;
358 }
359
360 /***********************************************************************
361  *           VERSION_GetFileVersionInfo_16             [internal]
362  *
363  *    NOTE: returns size of the 16-bit VERSION resource or 0xFFFFFFFF
364  *    in the case if file exists, but VERSION_INFO not found.
365  *    FIXME: handle is not used.
366  */
367 static DWORD VERSION_GetFileVersionInfo_16( LPCSTR filename, LPDWORD handle,
368                                     DWORD datasize, LPVOID data )
369 {
370     VS_FIXEDFILEINFO *vffi;
371     DWORD len;
372     BYTE *buf;
373     HMODULE16 hModule;
374     HRSRC16 hRsrc;
375     HGLOBAL16 hMem;
376
377     TRACE("(%s,%p)\n", debugstr_a(filename), handle );
378
379     hModule = LoadLibrary16(filename);
380     if(hModule < 32)
381     {
382         WARN("Could not load %s\n", debugstr_a(filename));
383         return 0;
384     }
385     hRsrc = FindResource16(hModule,
386                           MAKEINTRESOURCEA(VS_VERSION_INFO),
387                           MAKEINTRESOURCEA(VS_FILE_INFO));
388     if(!hRsrc)
389     {
390         WARN("Could not find VS_VERSION_INFO in %s\n", debugstr_a(filename));
391         FreeLibrary16(hModule);
392         return 0xFFFFFFFF;
393     }
394     len = SizeofResource16(hModule, hRsrc);
395     hMem = LoadResource16(hModule, hRsrc);
396     if(!hMem)
397     {
398         WARN("Could not load VS_VERSION_INFO from %s\n", debugstr_a(filename));
399         FreeLibrary16(hModule);
400         return 0xFFFFFFFF;
401     }
402     buf = LockResource16(hMem);
403
404     if(!VersionInfoIs16(buf))
405         goto END;
406
407     vffi = (VS_FIXEDFILEINFO *)VersionInfo16_Value( (VS_VERSION_INFO_STRUCT16 *)buf );
408
409     if ( vffi->dwSignature != VS_FFI_SIGNATURE )
410     {
411         WARN("vffi->dwSignature is 0x%08lx, but not 0x%08lx!\n",
412                    vffi->dwSignature, VS_FFI_SIGNATURE );
413         len = 0xFFFFFFFF;
414         goto END;
415     }
416
417     if ( TRACE_ON(ver) )
418         print_vffi_debug( vffi );
419
420     if(data)
421     {
422         if(datasize < len)
423             len = datasize; /* truncate data */
424         if(len)
425             memcpy(data, buf, len);
426         else
427             len = 0xFFFFFFFF;
428     }
429 END:
430     FreeResource16(hMem);
431     FreeLibrary16(hModule);
432
433     return len;
434 }
435
436 /***********************************************************************
437  *           GetFileVersionInfoSizeA         [VERSION.@]
438  */
439 DWORD WINAPI GetFileVersionInfoSizeA( LPCSTR filename, LPDWORD handle )
440 {
441     VS_FIXEDFILEINFO *vffi;
442     DWORD len, ret, offset;
443     BYTE buf[144];
444
445     TRACE("(%s,%p)\n", debugstr_a(filename), handle );
446
447     len = VERSION_GetFileVersionInfo_PE(filename, handle, 0, NULL);
448     /* 0xFFFFFFFF means: file exists, but VERSION_INFO not found */
449     if(len == 0xFFFFFFFF)
450     {
451         SetLastError(ERROR_RESOURCE_DATA_NOT_FOUND);
452         return 0;
453     }
454     if(len) return len;
455     len = VERSION_GetFileVersionInfo_16(filename, handle, 0, NULL);
456     /* 0xFFFFFFFF means: file exists, but VERSION_INFO not found */
457     if(len == 0xFFFFFFFF)
458     {
459         SetLastError(ERROR_RESOURCE_DATA_NOT_FOUND);
460         return 0;
461     }
462     if(len) return len;
463
464     len = GetFileResourceSize16( filename,
465                                  MAKEINTRESOURCEA(VS_FILE_INFO),
466                                  MAKEINTRESOURCEA(VS_VERSION_INFO),
467                                  &offset );
468     if (!len) return 0;
469
470     ret = GetFileResource16( filename,
471                              MAKEINTRESOURCEA(VS_FILE_INFO),
472                              MAKEINTRESOURCEA(VS_VERSION_INFO),
473                              offset, sizeof( buf ), buf );
474     if (!ret) return 0;
475
476     if ( handle ) *handle = offset;
477
478     if ( VersionInfoIs16( buf ) )
479         vffi = (VS_FIXEDFILEINFO *)VersionInfo16_Value( (VS_VERSION_INFO_STRUCT16 *)buf );
480     else
481         vffi = (VS_FIXEDFILEINFO *)VersionInfo32_Value( (VS_VERSION_INFO_STRUCT32 *)buf );
482
483     if ( vffi->dwSignature != VS_FFI_SIGNATURE )
484     {
485         WARN("vffi->dwSignature is 0x%08lx, but not 0x%08lx!\n",
486                    vffi->dwSignature, VS_FFI_SIGNATURE );
487         return 0;
488     }
489
490     if ( ((VS_VERSION_INFO_STRUCT16 *)buf)->wLength < len )
491         len = ((VS_VERSION_INFO_STRUCT16 *)buf)->wLength;
492
493     if ( TRACE_ON(ver) )
494         print_vffi_debug( vffi );
495
496     return len;
497 }
498
499 /***********************************************************************
500  *           GetFileVersionInfoSizeW         [VERSION.@]
501  */
502 DWORD WINAPI GetFileVersionInfoSizeW( LPCWSTR filename, LPDWORD handle )
503 {
504     DWORD ret, len = WideCharToMultiByte( CP_ACP, 0, filename, -1, NULL, 0, NULL, NULL );
505     LPSTR fn = HeapAlloc( GetProcessHeap(), 0, len );
506     WideCharToMultiByte( CP_ACP, 0, filename, -1, fn, len, NULL, NULL );
507     ret = GetFileVersionInfoSizeA( fn, handle );
508     HeapFree( GetProcessHeap(), 0, fn );
509     return ret;
510 }
511
512 /***********************************************************************
513  *           GetFileVersionInfoA             [VERSION.@]
514  */
515 BOOL WINAPI GetFileVersionInfoA( LPCSTR filename, DWORD handle,
516                                     DWORD datasize, LPVOID data )
517 {
518     DWORD len;
519
520     TRACE("(%s,%ld,size=%ld,data=%p)\n",
521                 debugstr_a(filename), handle, datasize, data );
522
523     len = VERSION_GetFileVersionInfo_PE(filename, &handle, datasize, data);
524     /* 0xFFFFFFFF means: file exists, but VERSION_INFO not found */
525     if(len == 0xFFFFFFFF) return FALSE;
526     if(len)
527         goto DO_CONVERT;
528     len = VERSION_GetFileVersionInfo_16(filename, &handle, datasize, data);
529     /* 0xFFFFFFFF means: file exists, but VERSION_INFO not found */
530     if(len == 0xFFFFFFFF) return FALSE;
531     if(len)
532         goto DO_CONVERT;
533
534     if ( !GetFileResource16( filename, MAKEINTRESOURCEA(VS_FILE_INFO),
535                                        MAKEINTRESOURCEA(VS_VERSION_INFO),
536                                        handle, datasize, data ) )
537         return FALSE;
538 DO_CONVERT:
539     if (    datasize >= sizeof(VS_VERSION_INFO_STRUCT16)
540          && datasize >= ((VS_VERSION_INFO_STRUCT16 *)data)->wLength
541          && !VersionInfoIs16( data ) )
542     {
543         /* convert resource from PE format to NE format */
544         ConvertVersionInfo32To16( (VS_VERSION_INFO_STRUCT32 *)data,
545                                   (VS_VERSION_INFO_STRUCT16 *)data );
546     }
547
548     return TRUE;
549 }
550
551 /***********************************************************************
552  *           GetFileVersionInfoW             [VERSION.@]
553  */
554 BOOL WINAPI GetFileVersionInfoW( LPCWSTR filename, DWORD handle,
555                                     DWORD datasize, LPVOID data )
556 {
557     DWORD len = WideCharToMultiByte( CP_ACP, 0, filename, -1, NULL, 0, NULL, NULL );
558     LPSTR fn = HeapAlloc( GetProcessHeap(), 0, len );
559     DWORD retv = TRUE;
560
561     WideCharToMultiByte( CP_ACP, 0, filename, -1, fn, len, NULL, NULL );
562
563     TRACE("(%s,%ld,size=%ld,data=%p)\n",
564                 debugstr_w(filename), handle, datasize, data );
565
566     if(VERSION_GetFileVersionInfo_PE(fn, &handle, datasize, data))
567         goto END;
568     if(VERSION_GetFileVersionInfo_16(fn, &handle, datasize, data))
569         goto END;
570
571     if ( !GetFileResource16( fn, MAKEINTRESOURCEA(VS_FILE_INFO),
572                                  MAKEINTRESOURCEA(VS_VERSION_INFO),
573                                  handle, datasize, data ) )
574         retv = FALSE;
575
576     else if (    datasize >= sizeof(VS_VERSION_INFO_STRUCT16)
577               && datasize >= ((VS_VERSION_INFO_STRUCT16 *)data)->wLength
578               && VersionInfoIs16( data ) )
579     {
580         ERR("Cannot access NE resource in %s\n", debugstr_a(fn) );
581         retv =  FALSE;
582     }
583 END:
584     HeapFree( GetProcessHeap(), 0, fn );
585     return retv;
586 }
587
588
589 /***********************************************************************
590  *           VersionInfo16_FindChild             [internal]
591  */
592 static VS_VERSION_INFO_STRUCT16 *VersionInfo16_FindChild( VS_VERSION_INFO_STRUCT16 *info,
593                                             LPCSTR szKey, UINT cbKey )
594 {
595     VS_VERSION_INFO_STRUCT16 *child = VersionInfo16_Children( info );
596
597     while ( (DWORD)child < (DWORD)info + info->wLength )
598     {
599         if ( !strncasecmp( child->szKey, szKey, cbKey ) )
600             return child;
601
602         if (!(child->wLength)) return NULL;
603         child = VersionInfo16_Next( child );
604     }
605
606     return NULL;
607 }
608
609 /***********************************************************************
610  *           VersionInfo32_FindChild             [internal]
611  */
612 static VS_VERSION_INFO_STRUCT32 *VersionInfo32_FindChild( VS_VERSION_INFO_STRUCT32 *info,
613                                             LPCWSTR szKey, UINT cbKey )
614 {
615     VS_VERSION_INFO_STRUCT32 *child = VersionInfo32_Children( info );
616
617     while ( (DWORD)child < (DWORD)info + info->wLength )
618     {
619         if ( !strncmpiW( child->szKey, szKey, cbKey ) )
620             return child;
621
622         child = VersionInfo32_Next( child );
623     }
624
625     return NULL;
626 }
627
628 /***********************************************************************
629  *           VerQueryValueA              [VERSION.@]
630  */
631 DWORD WINAPI VerQueryValueA( LPVOID pBlock, LPCSTR lpSubBlock,
632                                LPVOID *lplpBuffer, UINT *puLen )
633 {
634     VS_VERSION_INFO_STRUCT16 *info = (VS_VERSION_INFO_STRUCT16 *)pBlock;
635     if ( !VersionInfoIs16( info ) )
636     {
637         INT len;
638         LPWSTR wide_str;
639         DWORD give;
640
641         /* <lawson_whitney@juno.com> Feb 2001 */
642         /* AOL 5.0 does this, expecting to get this: */
643         len = MultiByteToWideChar(CP_ACP, 0, lpSubBlock, -1, NULL, 0);
644         wide_str = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
645         MultiByteToWideChar(CP_ACP, 0, lpSubBlock, -1, wide_str, len);
646
647         give = VerQueryValueW(pBlock, wide_str, lplpBuffer, puLen);
648         HeapFree(GetProcessHeap(), 0, wide_str);
649         return give;
650     }
651
652     TRACE("(%p,%s,%p,%p)\n",
653                 pBlock, debugstr_a(lpSubBlock), lplpBuffer, puLen );
654
655     while ( *lpSubBlock )
656     {
657         /* Find next path component */
658         LPCSTR lpNextSlash;
659         for ( lpNextSlash = lpSubBlock; *lpNextSlash; lpNextSlash++ )
660             if ( *lpNextSlash == '\\' )
661                 break;
662
663         /* Skip empty components */
664         if ( lpNextSlash == lpSubBlock )
665         {
666             lpSubBlock++;
667             continue;
668         }
669
670         /* We have a non-empty component: search info for key */
671         info = VersionInfo16_FindChild( info, lpSubBlock, lpNextSlash-lpSubBlock );
672         if ( !info ) return FALSE;
673
674         /* Skip path component */
675         lpSubBlock = lpNextSlash;
676     }
677
678     /* Return value */
679     *lplpBuffer = VersionInfo16_Value( info );
680     *puLen = info->wValueLength;
681
682     return TRUE;
683 }
684
685 /***********************************************************************
686  *           VerQueryValueW              [VERSION.@]
687  */
688 DWORD WINAPI VerQueryValueW( LPVOID pBlock, LPCWSTR lpSubBlock,
689                                LPVOID *lplpBuffer, UINT *puLen )
690 {
691     VS_VERSION_INFO_STRUCT32 *info = (VS_VERSION_INFO_STRUCT32 *)pBlock;
692     if ( VersionInfoIs16( info ) )
693     {
694         ERR("called on NE resource!\n" );
695         return FALSE;
696     }
697
698     TRACE("(%p,%s,%p,%p)\n",
699                 pBlock, debugstr_w(lpSubBlock), lplpBuffer, puLen );
700
701     while ( *lpSubBlock )
702     {
703         /* Find next path component */
704         LPCWSTR lpNextSlash;
705         for ( lpNextSlash = lpSubBlock; *lpNextSlash; lpNextSlash++ )
706             if ( *lpNextSlash == '\\' )
707                 break;
708
709         /* Skip empty components */
710         if ( lpNextSlash == lpSubBlock )
711         {
712             lpSubBlock++;
713             continue;
714         }
715
716         /* We have a non-empty component: search info for key */
717         info = VersionInfo32_FindChild( info, lpSubBlock, lpNextSlash-lpSubBlock );
718         if ( !info ) return FALSE;
719
720         /* Skip path component */
721         lpSubBlock = lpNextSlash;
722     }
723
724     /* Return value */
725     *lplpBuffer = VersionInfo32_Value( info );
726     *puLen = info->wValueLength;
727
728     return TRUE;
729 }