kernelbase: Fix GetFileVersionInfo*() crashes with malformed resources.
[wine.git] / dlls / kernelbase / version.c
blobd5bfa08193993e096849732bb16a266fbf327abe
1 /*
2 * Implementation of VERSION.DLL
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>
28 #include <stdio.h>
29 #include <sys/types.h>
31 #include "ntstatus.h"
32 #define WIN32_NO_STATUS
33 #define NONAMELESSUNION
34 #define NONAMELESSSTRUCT
35 #include "windef.h"
36 #include "winbase.h"
37 #include "winver.h"
38 #include "winuser.h"
39 #include "winnls.h"
40 #include "winternl.h"
41 #include "winerror.h"
43 #include "kernelbase.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(ver);
48 typedef struct
50 WORD offset;
51 WORD length;
52 WORD flags;
53 WORD id;
54 WORD handle;
55 WORD usage;
56 } NE_NAMEINFO;
58 typedef struct
60 WORD type_id;
61 WORD count;
62 DWORD resloader;
63 } NE_TYPEINFO;
65 struct version_info
67 DWORD major;
68 DWORD minor;
69 DWORD build;
72 /***********************************************************************
73 * Version Info Structure
76 typedef struct
78 WORD wLength;
79 WORD wValueLength;
80 CHAR szKey[1];
81 #if 0 /* variable length structure */
82 /* DWORD aligned */
83 BYTE Value[];
84 /* DWORD aligned */
85 VS_VERSION_INFO_STRUCT16 Children[];
86 #endif
87 } VS_VERSION_INFO_STRUCT16;
89 typedef struct
91 WORD wLength;
92 WORD wValueLength;
93 WORD wType; /* 1:Text, 0:Binary */
94 WCHAR szKey[1];
95 #if 0 /* variable length structure */
96 /* DWORD aligned */
97 BYTE Value[];
98 /* DWORD aligned */
99 VS_VERSION_INFO_STRUCT32 Children[];
100 #endif
101 } VS_VERSION_INFO_STRUCT32;
103 #define VersionInfoIs16( ver ) \
104 ( ((const VS_VERSION_INFO_STRUCT16 *)ver)->szKey[0] >= ' ' )
106 #define DWORD_ALIGN( base, ptr ) \
107 ( (LPBYTE)(base) + ((((LPBYTE)(ptr) - (LPBYTE)(base)) + 3) & ~3) )
109 #define VersionInfo16_Value( ver ) \
110 DWORD_ALIGN( (ver), (ver)->szKey + strlen((ver)->szKey) + 1 )
111 #define VersionInfo32_Value( ver ) \
112 DWORD_ALIGN( (ver), (ver)->szKey + lstrlenW((ver)->szKey) + 1 )
114 #define VersionInfo16_Children( ver ) \
115 (const VS_VERSION_INFO_STRUCT16 *)( VersionInfo16_Value( ver ) + \
116 ( ( (ver)->wValueLength + 3 ) & ~3 ) )
117 #define VersionInfo32_Children( ver ) \
118 (const VS_VERSION_INFO_STRUCT32 *)( VersionInfo32_Value( ver ) + \
119 ( ( (ver)->wValueLength * \
120 ((ver)->wType? 2 : 1) + 3 ) & ~3 ) )
122 #define VersionInfo16_Next( ver ) \
123 (VS_VERSION_INFO_STRUCT16 *)( (LPBYTE)ver + (((ver)->wLength + 3) & ~3) )
124 #define VersionInfo32_Next( ver ) \
125 (VS_VERSION_INFO_STRUCT32 *)( (LPBYTE)ver + (((ver)->wLength + 3) & ~3) )
128 /***********************************************************************
129 * Win8 info, reported if app doesn't provide compat GUID in manifest.
131 static const struct version_info windows8_version_info = { 6, 2, 0x23f0 };
134 /***********************************************************************
135 * Windows versions that need compatibility GUID specified in manifest
136 * in order to be reported by the APIs.
138 static const struct
140 struct version_info info;
141 GUID guid;
142 } version_data[] =
144 /* Windows 8.1 */
146 { 6, 3, 0x2580 },
147 {0x1f676c76,0x80e1,0x4239,{0x95,0xbb,0x83,0xd0,0xf6,0xd0,0xda,0x78}}
149 /* Windows 10 */
151 { 10, 0, 0x42ee },
152 {0x8e0f7a12,0xbfb3,0x4fe8,{0xb9,0xa5,0x48,0xfd,0x50,0xa1,0x5a,0x9a}}
157 /******************************************************************************
158 * init_current_version
160 * Initialize the current_version variable.
162 * For compatibility, Windows 8.1 and later report Win8 version unless the app
163 * has a manifest that confirms its compatibility with newer versions of Windows.
166 static RTL_OSVERSIONINFOEXW current_version;
168 static BOOL CALLBACK init_current_version(PINIT_ONCE init_once, PVOID parameter, PVOID *context)
170 /*ACTIVATION_CONTEXT_COMPATIBILITY_INFORMATION*/DWORD *acci;
171 const struct version_info *ver;
172 SIZE_T req;
173 int idx;
175 current_version.dwOSVersionInfoSize = sizeof(current_version);
176 if (!set_ntstatus( RtlGetVersion(&current_version) )) return FALSE;
178 for (idx = ARRAY_SIZE(version_data); idx--;)
179 if ( current_version.dwMajorVersion > version_data[idx].info.major ||
180 (current_version.dwMajorVersion == version_data[idx].info.major &&
181 current_version.dwMinorVersion >= version_data[idx].info.minor))
182 break;
184 if (idx < 0) return TRUE;
185 ver = &windows8_version_info;
187 if (RtlQueryInformationActivationContext(0, NtCurrentTeb()->Peb->ActivationContextData, NULL,
188 CompatibilityInformationInActivationContext, NULL, 0, &req) != STATUS_BUFFER_TOO_SMALL
189 || !req)
190 goto done;
192 if (!(acci = HeapAlloc(GetProcessHeap(), 0, req)))
194 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
195 return FALSE;
198 if (RtlQueryInformationActivationContext(0, NtCurrentTeb()->Peb->ActivationContextData, NULL,
199 CompatibilityInformationInActivationContext, acci, req, &req) == STATUS_SUCCESS)
203 COMPATIBILITY_CONTEXT_ELEMENT *elements = (COMPATIBILITY_CONTEXT_ELEMENT*)(acci + 1);
204 DWORD i, count = *acci;
206 for (i = 0; i < count; i++)
208 if (elements[i].Type == ACTCTX_COMPATIBILITY_ELEMENT_TYPE_OS &&
209 IsEqualGUID(&elements[i].Id, &version_data[idx].guid))
211 ver = &version_data[idx].info;
213 if (ver->major == current_version.dwMajorVersion &&
214 ver->minor == current_version.dwMinorVersion)
215 ver = NULL;
217 idx = 0; /* break from outer loop */
218 break;
221 } while (idx--);
223 HeapFree(GetProcessHeap(), 0, acci);
225 done:
226 if (ver)
228 current_version.dwMajorVersion = ver->major;
229 current_version.dwMinorVersion = ver->minor;
230 current_version.dwBuildNumber = ver->build;
232 return TRUE;
236 /**********************************************************************
237 * find_entry_by_id
239 * Find an entry by id in a resource directory
240 * Copied from loader/pe_resource.c
242 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_id( const IMAGE_RESOURCE_DIRECTORY *dir,
243 WORD id, const void *root,
244 DWORD root_size )
246 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
247 int min, max, pos;
249 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
250 min = dir->NumberOfNamedEntries;
251 max = min + dir->NumberOfIdEntries - 1;
253 if (max >= (root_size - ((INT_PTR)dir - (INT_PTR)root) - sizeof(*dir)) / sizeof(*entry))
254 return NULL;
256 while (min <= max)
258 pos = (min + max) / 2;
259 if (entry[pos].u.Id == id)
261 DWORD offset = entry[pos].u2.s2.OffsetToDirectory;
262 if (offset > root_size - sizeof(*dir)) return NULL;
263 return (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + offset);
265 if (entry[pos].u.Id > id) max = pos - 1;
266 else min = pos + 1;
268 return NULL;
272 /**********************************************************************
273 * find_entry_default
275 * Find a default entry in a resource directory
276 * Copied from loader/pe_resource.c
278 static const IMAGE_RESOURCE_DIRECTORY *find_entry_default( const IMAGE_RESOURCE_DIRECTORY *dir,
279 const void *root )
281 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
283 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
284 return (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry->u2.s2.OffsetToDirectory);
288 /**********************************************************************
289 * push_language
291 * push a language onto the list of languages to try
293 static inline int push_language( WORD *list, int pos, WORD lang )
295 int i;
296 for (i = 0; i < pos; i++) if (list[i] == lang) return pos;
297 list[pos++] = lang;
298 return pos;
302 /**********************************************************************
303 * find_entry_language
305 static const IMAGE_RESOURCE_DIRECTORY *find_entry_language( const IMAGE_RESOURCE_DIRECTORY *dir,
306 const void *root, DWORD root_size,
307 DWORD flags )
309 const IMAGE_RESOURCE_DIRECTORY *ret;
310 WORD list[9];
311 int i, pos = 0;
313 if (flags & FILE_VER_GET_LOCALISED)
315 /* cf. LdrFindResource_U */
316 pos = push_language( list, pos, MAKELANGID( LANG_NEUTRAL, SUBLANG_NEUTRAL ) );
317 pos = push_language( list, pos, LANGIDFROMLCID( NtCurrentTeb()->CurrentLocale ) );
318 pos = push_language( list, pos, GetUserDefaultLangID() );
319 pos = push_language( list, pos, MAKELANGID( PRIMARYLANGID(GetUserDefaultLangID()), SUBLANG_NEUTRAL ));
320 pos = push_language( list, pos, MAKELANGID( PRIMARYLANGID(GetUserDefaultLangID()), SUBLANG_DEFAULT ));
321 pos = push_language( list, pos, GetSystemDefaultLangID() );
322 pos = push_language( list, pos, MAKELANGID( PRIMARYLANGID(GetSystemDefaultLangID()), SUBLANG_NEUTRAL ));
323 pos = push_language( list, pos, MAKELANGID( PRIMARYLANGID(GetSystemDefaultLangID()), SUBLANG_DEFAULT ));
324 pos = push_language( list, pos, MAKELANGID( LANG_ENGLISH, SUBLANG_DEFAULT ) );
326 else
328 /* FIXME: resolve LN file here */
329 pos = push_language( list, pos, MAKELANGID( LANG_ENGLISH, SUBLANG_DEFAULT ) );
332 for (i = 0; i < pos; i++) if ((ret = find_entry_by_id( dir, list[i], root, root_size ))) return ret;
333 return find_entry_default( dir, root );
337 static DWORD read_data( HANDLE handle, DWORD offset, void *data, DWORD len )
339 DWORD res;
341 SetFilePointer( handle, offset, NULL, FILE_BEGIN );
342 if (!ReadFile( handle, data, len, &res, NULL )) res = 0;
343 return res;
346 /***********************************************************************
347 * find_ne_resource [internal]
349 static BOOL find_ne_resource( HANDLE handle, DWORD *resLen, DWORD *resOff )
351 const WORD typeid = VS_FILE_INFO | 0x8000;
352 const WORD resid = VS_VERSION_INFO | 0x8000;
353 IMAGE_OS2_HEADER nehd;
354 NE_TYPEINFO *typeInfo;
355 NE_NAMEINFO *nameInfo;
356 DWORD nehdoffset = *resOff;
357 LPBYTE resTab;
358 DWORD resTabSize;
359 int count;
361 /* Read in NE header */
362 if (read_data( handle, nehdoffset, &nehd, sizeof(nehd) ) != sizeof(nehd)) return FALSE;
364 resTabSize = nehd.ne_restab - nehd.ne_rsrctab;
365 if ( !resTabSize )
367 TRACE("No resources in NE dll\n" );
368 return FALSE;
371 /* Read in resource table */
372 resTab = HeapAlloc( GetProcessHeap(), 0, resTabSize );
373 if ( !resTab ) return FALSE;
375 if (read_data( handle, nehd.ne_rsrctab + nehdoffset, resTab, resTabSize ) != resTabSize)
377 HeapFree( GetProcessHeap(), 0, resTab );
378 return FALSE;
381 /* Find resource */
382 typeInfo = (NE_TYPEINFO *)(resTab + 2);
383 while (typeInfo->type_id)
385 if (typeInfo->type_id == typeid) goto found_type;
386 typeInfo = (NE_TYPEINFO *)((char *)(typeInfo + 1) +
387 typeInfo->count * sizeof(NE_NAMEINFO));
389 TRACE("No typeid entry found\n" );
390 HeapFree( GetProcessHeap(), 0, resTab );
391 return FALSE;
393 found_type:
394 nameInfo = (NE_NAMEINFO *)(typeInfo + 1);
396 for (count = typeInfo->count; count > 0; count--, nameInfo++)
397 if (nameInfo->id == resid) goto found_name;
399 TRACE("No resid entry found\n" );
400 HeapFree( GetProcessHeap(), 0, resTab );
401 return FALSE;
403 found_name:
404 /* Return resource data */
405 *resLen = nameInfo->length << *(WORD *)resTab;
406 *resOff = nameInfo->offset << *(WORD *)resTab;
408 HeapFree( GetProcessHeap(), 0, resTab );
409 return TRUE;
412 /***********************************************************************
413 * find_pe_resource [internal]
415 static BOOL find_pe_resource( HANDLE handle, DWORD *resLen, DWORD *resOff, DWORD flags )
417 union
419 IMAGE_NT_HEADERS32 nt32;
420 IMAGE_NT_HEADERS64 nt64;
421 } pehd;
422 DWORD pehdoffset = *resOff;
423 PIMAGE_DATA_DIRECTORY resDataDir;
424 PIMAGE_SECTION_HEADER sections;
425 LPBYTE resSection;
426 DWORD len, section_size, data_size, resDirSize;
427 const void *resDir;
428 const IMAGE_RESOURCE_DIRECTORY *resPtr;
429 const IMAGE_RESOURCE_DATA_ENTRY *resData;
430 int i, nSections;
431 BOOL ret = FALSE;
433 /* Read in PE header */
434 len = read_data( handle, pehdoffset, &pehd, sizeof(pehd) );
435 if (len < sizeof(pehd.nt32.FileHeader)) return FALSE;
436 if (len < sizeof(pehd)) memset( (char *)&pehd + len, 0, sizeof(pehd) - len );
438 switch (pehd.nt32.OptionalHeader.Magic)
440 case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
441 resDataDir = pehd.nt32.OptionalHeader.DataDirectory + IMAGE_DIRECTORY_ENTRY_RESOURCE;
442 break;
443 case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
444 resDataDir = pehd.nt64.OptionalHeader.DataDirectory + IMAGE_DIRECTORY_ENTRY_RESOURCE;
445 break;
446 default:
447 return FALSE;
450 if ( !resDataDir->Size )
452 TRACE("No resources in PE dll\n" );
453 return FALSE;
456 /* Read in section table */
457 nSections = pehd.nt32.FileHeader.NumberOfSections;
458 sections = HeapAlloc( GetProcessHeap(), 0,
459 nSections * sizeof(IMAGE_SECTION_HEADER) );
460 if ( !sections ) return FALSE;
462 len = FIELD_OFFSET( IMAGE_NT_HEADERS32, OptionalHeader ) + pehd.nt32.FileHeader.SizeOfOptionalHeader;
463 if (read_data( handle, pehdoffset + len, sections, nSections * sizeof(IMAGE_SECTION_HEADER) ) !=
464 nSections * sizeof(IMAGE_SECTION_HEADER))
466 HeapFree( GetProcessHeap(), 0, sections );
467 return FALSE;
470 /* Find resource section */
471 for ( i = 0; i < nSections; i++ )
472 if ( resDataDir->VirtualAddress >= sections[i].VirtualAddress
473 && resDataDir->VirtualAddress < sections[i].VirtualAddress +
474 sections[i].SizeOfRawData )
475 break;
477 if ( i == nSections )
479 HeapFree( GetProcessHeap(), 0, sections );
480 TRACE("Couldn't find resource section\n" );
481 return FALSE;
484 /* Read in resource section */
485 data_size = sections[i].SizeOfRawData;
486 section_size = max( data_size, sections[i].Misc.VirtualSize );
487 resSection = HeapAlloc( GetProcessHeap(), 0, section_size );
488 if ( !resSection )
490 HeapFree( GetProcessHeap(), 0, sections );
491 return FALSE;
494 if (read_data( handle, sections[i].PointerToRawData, resSection, data_size ) != data_size) goto done;
495 if (data_size < section_size) memset( (char *)resSection + data_size, 0, section_size - data_size );
497 /* Find resource */
498 resDir = resSection + (resDataDir->VirtualAddress - sections[i].VirtualAddress);
499 resDirSize = section_size - (resDataDir->VirtualAddress - sections[i].VirtualAddress);
501 resPtr = resDir;
502 resPtr = find_entry_by_id( resPtr, VS_FILE_INFO, resDir, resDirSize );
503 if ( !resPtr )
505 TRACE("No typeid entry found\n" );
506 goto done;
508 resPtr = find_entry_by_id( resPtr, VS_VERSION_INFO, resDir, resDirSize );
509 if ( !resPtr )
511 TRACE("No resid entry found\n" );
512 goto done;
514 resPtr = find_entry_language( resPtr, resDir, resDirSize, flags );
515 if ( !resPtr )
517 TRACE("No default language entry found\n" );
518 goto done;
521 /* Find resource data section */
522 resData = (const IMAGE_RESOURCE_DATA_ENTRY*)resPtr;
523 for ( i = 0; i < nSections; i++ )
524 if ( resData->OffsetToData >= sections[i].VirtualAddress
525 && resData->OffsetToData < sections[i].VirtualAddress +
526 sections[i].SizeOfRawData )
527 break;
529 if ( i == nSections )
531 TRACE("Couldn't find resource data section\n" );
532 goto done;
535 /* Return resource data */
536 *resLen = resData->Size;
537 *resOff = resData->OffsetToData - sections[i].VirtualAddress + sections[i].PointerToRawData;
538 ret = TRUE;
540 done:
541 HeapFree( GetProcessHeap(), 0, resSection );
542 HeapFree( GetProcessHeap(), 0, sections );
543 return ret;
547 /***********************************************************************
548 * find_version_resource [internal]
550 static DWORD find_version_resource( HANDLE handle, DWORD *reslen, DWORD *offset, DWORD flags )
552 IMAGE_DOS_HEADER mzh;
553 WORD magic;
555 if (read_data( handle, 0, &mzh, sizeof(mzh) ) != sizeof(mzh)) return 0;
556 if (mzh.e_magic != IMAGE_DOS_SIGNATURE) return 0;
558 if (read_data( handle, mzh.e_lfanew, &magic, sizeof(magic) ) != sizeof(magic)) return 0;
559 *offset = mzh.e_lfanew;
561 switch (magic)
563 case IMAGE_OS2_SIGNATURE:
564 if (!find_ne_resource( handle, reslen, offset )) magic = 0;
565 break;
566 case IMAGE_NT_SIGNATURE:
567 if (!find_pe_resource( handle, reslen, offset, flags )) magic = 0;
568 break;
570 WARN( "Can't handle %04x files.\n", magic );
571 return magic;
574 /******************************************************************************
575 * This function will print via standard TRACE, debug info regarding
576 * the file info structure vffi.
578 static void print_vffi_debug(const VS_FIXEDFILEINFO *vffi)
580 BOOL versioned_printer = FALSE;
582 if((vffi->dwFileType == VFT_DLL) || (vffi->dwFileType == VFT_DRV))
584 if(vffi->dwFileSubtype == VFT2_DRV_VERSIONED_PRINTER)
585 /* this is documented for newer w2k Drivers and up */
586 versioned_printer = TRUE;
587 else if( (vffi->dwFileSubtype == VFT2_DRV_PRINTER) &&
588 (vffi->dwFileVersionMS != vffi->dwProductVersionMS) &&
589 (vffi->dwFileVersionMS > 0) &&
590 (vffi->dwFileVersionMS <= 3) )
591 /* found this on NT 3.51, NT4.0 and old w2k Drivers */
592 versioned_printer = TRUE;
595 TRACE("structversion=%u.%u, ",
596 HIWORD(vffi->dwStrucVersion),LOWORD(vffi->dwStrucVersion));
597 if(versioned_printer)
599 WORD mode = LOWORD(vffi->dwFileVersionMS);
600 WORD ver_rev = HIWORD(vffi->dwFileVersionLS);
601 TRACE("fileversion=%u.%u.%u.%u (%s.major.minor.release), ",
602 (vffi->dwFileVersionMS),
603 HIBYTE(ver_rev), LOBYTE(ver_rev), LOWORD(vffi->dwFileVersionLS),
604 (mode == 3) ? "Usermode" : ((mode <= 2) ? "Kernelmode" : "?") );
606 else
608 TRACE("fileversion=%u.%u.%u.%u, ",
609 HIWORD(vffi->dwFileVersionMS),LOWORD(vffi->dwFileVersionMS),
610 HIWORD(vffi->dwFileVersionLS),LOWORD(vffi->dwFileVersionLS));
612 TRACE("productversion=%u.%u.%u.%u\n",
613 HIWORD(vffi->dwProductVersionMS),LOWORD(vffi->dwProductVersionMS),
614 HIWORD(vffi->dwProductVersionLS),LOWORD(vffi->dwProductVersionLS));
616 TRACE("flagmask=0x%x, flags=0x%x %s%s%s%s%s%s\n",
617 vffi->dwFileFlagsMask, vffi->dwFileFlags,
618 (vffi->dwFileFlags & VS_FF_DEBUG) ? "DEBUG," : "",
619 (vffi->dwFileFlags & VS_FF_PRERELEASE) ? "PRERELEASE," : "",
620 (vffi->dwFileFlags & VS_FF_PATCHED) ? "PATCHED," : "",
621 (vffi->dwFileFlags & VS_FF_PRIVATEBUILD) ? "PRIVATEBUILD," : "",
622 (vffi->dwFileFlags & VS_FF_INFOINFERRED) ? "INFOINFERRED," : "",
623 (vffi->dwFileFlags & VS_FF_SPECIALBUILD) ? "SPECIALBUILD," : "");
625 TRACE("(");
627 TRACE("OS=0x%x.0x%x ", HIWORD(vffi->dwFileOS), LOWORD(vffi->dwFileOS));
629 switch (vffi->dwFileOS&0xFFFF0000)
631 case VOS_DOS:TRACE("DOS,");break;
632 case VOS_OS216:TRACE("OS/2-16,");break;
633 case VOS_OS232:TRACE("OS/2-32,");break;
634 case VOS_NT:TRACE("NT,");break;
635 case VOS_UNKNOWN:
636 default:
637 TRACE("UNKNOWN(0x%x),",vffi->dwFileOS&0xFFFF0000);break;
640 switch (LOWORD(vffi->dwFileOS))
642 case VOS__BASE:TRACE("BASE");break;
643 case VOS__WINDOWS16:TRACE("WIN16");break;
644 case VOS__WINDOWS32:TRACE("WIN32");break;
645 case VOS__PM16:TRACE("PM16");break;
646 case VOS__PM32:TRACE("PM32");break;
647 default:
648 TRACE("UNKNOWN(0x%x)",LOWORD(vffi->dwFileOS));break;
651 TRACE(")\n");
653 switch (vffi->dwFileType)
655 case VFT_APP:TRACE("filetype=APP");break;
656 case VFT_DLL:
657 TRACE("filetype=DLL");
658 if(vffi->dwFileSubtype != 0)
660 if(versioned_printer) /* NT3.x/NT4.0 or old w2k Driver */
661 TRACE(",PRINTER");
662 TRACE(" (subtype=0x%x)", vffi->dwFileSubtype);
664 break;
665 case VFT_DRV:
666 TRACE("filetype=DRV,");
667 switch(vffi->dwFileSubtype)
669 case VFT2_DRV_PRINTER:TRACE("PRINTER");break;
670 case VFT2_DRV_KEYBOARD:TRACE("KEYBOARD");break;
671 case VFT2_DRV_LANGUAGE:TRACE("LANGUAGE");break;
672 case VFT2_DRV_DISPLAY:TRACE("DISPLAY");break;
673 case VFT2_DRV_MOUSE:TRACE("MOUSE");break;
674 case VFT2_DRV_NETWORK:TRACE("NETWORK");break;
675 case VFT2_DRV_SYSTEM:TRACE("SYSTEM");break;
676 case VFT2_DRV_INSTALLABLE:TRACE("INSTALLABLE");break;
677 case VFT2_DRV_SOUND:TRACE("SOUND");break;
678 case VFT2_DRV_COMM:TRACE("COMM");break;
679 case VFT2_DRV_INPUTMETHOD:TRACE("INPUTMETHOD");break;
680 case VFT2_DRV_VERSIONED_PRINTER:TRACE("VERSIONED_PRINTER");break;
681 case VFT2_UNKNOWN:
682 default:
683 TRACE("UNKNOWN(0x%x)",vffi->dwFileSubtype);break;
685 break;
686 case VFT_FONT:
687 TRACE("filetype=FONT,");
688 switch (vffi->dwFileSubtype)
690 case VFT2_FONT_RASTER:TRACE("RASTER");break;
691 case VFT2_FONT_VECTOR:TRACE("VECTOR");break;
692 case VFT2_FONT_TRUETYPE:TRACE("TRUETYPE");break;
693 default:TRACE("UNKNOWN(0x%x)",vffi->dwFileSubtype);break;
695 break;
696 case VFT_VXD:TRACE("filetype=VXD");break;
697 case VFT_STATIC_LIB:TRACE("filetype=STATIC_LIB");break;
698 case VFT_UNKNOWN:
699 default:
700 TRACE("filetype=Unknown(0x%x)",vffi->dwFileType);break;
703 TRACE("\n");
704 TRACE("filedate=0x%x.0x%x\n",vffi->dwFileDateMS,vffi->dwFileDateLS);
707 /***********************************************************************
708 * GetFileVersionInfoSizeW (kernelbase.@)
710 DWORD WINAPI GetFileVersionInfoSizeW( LPCWSTR filename, LPDWORD handle )
712 return GetFileVersionInfoSizeExW( FILE_VER_GET_LOCALISED, filename, handle );
715 /***********************************************************************
716 * GetFileVersionInfoSizeA (kernelbase.@)
718 DWORD WINAPI GetFileVersionInfoSizeA( LPCSTR filename, LPDWORD handle )
720 return GetFileVersionInfoSizeExA( FILE_VER_GET_LOCALISED, filename, handle );
723 /******************************************************************************
724 * GetFileVersionInfoSizeExW (kernelbase.@)
726 DWORD WINAPI GetFileVersionInfoSizeExW( DWORD flags, LPCWSTR filename, LPDWORD ret_handle )
728 DWORD len, offset, magic = 1;
729 HMODULE hModule;
731 TRACE("(0x%x,%s,%p)\n", flags, debugstr_w(filename), ret_handle );
733 if (ret_handle) *ret_handle = 0;
735 if (!filename)
737 SetLastError(ERROR_INVALID_PARAMETER);
738 return 0;
740 if (!*filename)
742 SetLastError(ERROR_BAD_PATHNAME);
743 return 0;
745 if (flags & ~FILE_VER_GET_LOCALISED)
746 FIXME("flags 0x%x ignored\n", flags & ~FILE_VER_GET_LOCALISED);
748 if ((hModule = LoadLibraryExW( filename, 0, LOAD_LIBRARY_AS_DATAFILE )))
750 HRSRC hRsrc = NULL;
751 if (!(flags & FILE_VER_GET_LOCALISED))
753 LANGID english = MAKELANGID( LANG_ENGLISH, SUBLANG_DEFAULT );
754 hRsrc = FindResourceExW( hModule, MAKEINTRESOURCEW(VS_VERSION_INFO),
755 (LPWSTR)VS_FILE_INFO, english );
757 if (!hRsrc)
758 hRsrc = FindResourceW( hModule, MAKEINTRESOURCEW(VS_VERSION_INFO),
759 (LPWSTR)VS_FILE_INFO );
760 if (hRsrc)
762 magic = IMAGE_NT_SIGNATURE;
763 len = SizeofResource( hModule, hRsrc );
765 FreeLibrary( hModule );
768 if (magic == 1)
770 HANDLE handle = CreateFileW( filename, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
771 NULL, OPEN_EXISTING, 0, 0 );
772 if (handle == INVALID_HANDLE_VALUE) return 0;
773 magic = find_version_resource( handle, &len, &offset, flags );
774 CloseHandle( handle );
777 switch (magic)
779 case IMAGE_OS2_SIGNATURE:
780 /* We have a 16bit resource.
782 * XP/W2K/W2K3 uses a buffer which is more than the actual needed space:
784 * (info->wLength - sizeof(VS_FIXEDFILEINFO)) * 4
786 * This extra buffer is used for ANSI to Unicode conversions in W-Calls.
787 * info->wLength should be the same as len. Currently it isn't but that
788 * doesn't seem to be a problem (len is bigger than info->wLength).
790 SetLastError(0);
791 return (len - sizeof(VS_FIXEDFILEINFO)) * 4;
793 case IMAGE_NT_SIGNATURE:
794 /* We have a 32bit resource.
796 * XP/W2K/W2K3 uses a buffer which is 2 times the actual needed space + 4 bytes "FE2X"
797 * This extra buffer is used for Unicode to ANSI conversions in A-Calls
799 SetLastError(0);
800 return (len * 2) + 4;
802 default:
803 if (GetVersion() & 0x80000000) /* Windows 95/98 */
804 SetLastError(ERROR_FILE_NOT_FOUND);
805 else
806 SetLastError(ERROR_RESOURCE_DATA_NOT_FOUND);
807 return 0;
811 /******************************************************************************
812 * GetFileVersionInfoSizeExA (kernelbase.@)
814 DWORD WINAPI GetFileVersionInfoSizeExA( DWORD flags, LPCSTR filename, LPDWORD handle )
816 UNICODE_STRING filenameW;
817 DWORD retval;
819 TRACE("(0x%x,%s,%p)\n", flags, debugstr_a(filename), handle );
821 if(filename)
822 RtlCreateUnicodeStringFromAsciiz(&filenameW, filename);
823 else
824 filenameW.Buffer = NULL;
826 retval = GetFileVersionInfoSizeExW(flags, filenameW.Buffer, handle);
828 RtlFreeUnicodeString(&filenameW);
830 return retval;
833 /***********************************************************************
834 * GetFileVersionInfoExW (kernelbase.@)
836 BOOL WINAPI GetFileVersionInfoExW( DWORD flags, LPCWSTR filename, DWORD ignored, DWORD datasize, LPVOID data )
838 static const char signature[4] = "FE2X";
839 DWORD len, offset, magic = 1;
840 HMODULE hModule;
841 VS_VERSION_INFO_STRUCT32* vvis = data;
843 TRACE("(0x%x,%s,%d,size=%d,data=%p)\n",
844 flags, debugstr_w(filename), ignored, datasize, data );
846 if (!data)
848 SetLastError(ERROR_INVALID_DATA);
849 return FALSE;
851 if (flags & ~FILE_VER_GET_LOCALISED)
852 FIXME("flags 0x%x ignored\n", flags & ~FILE_VER_GET_LOCALISED);
854 if ((hModule = LoadLibraryExW( filename, 0, LOAD_LIBRARY_AS_DATAFILE )))
856 HRSRC hRsrc = NULL;
857 if (!(flags & FILE_VER_GET_LOCALISED))
859 LANGID english = MAKELANGID( LANG_ENGLISH, SUBLANG_DEFAULT );
860 hRsrc = FindResourceExW( hModule, MAKEINTRESOURCEW(VS_VERSION_INFO),
861 (LPWSTR)VS_FILE_INFO, english );
863 if (!hRsrc)
864 hRsrc = FindResourceW( hModule, MAKEINTRESOURCEW(VS_VERSION_INFO),
865 (LPWSTR)VS_FILE_INFO );
866 if (hRsrc)
868 HGLOBAL hMem = LoadResource( hModule, hRsrc );
869 magic = IMAGE_NT_SIGNATURE;
870 len = min( SizeofResource(hModule, hRsrc), datasize );
871 memcpy( data, LockResource( hMem ), len );
872 FreeResource( hMem );
874 FreeLibrary( hModule );
877 if (magic == 1)
879 HANDLE handle = CreateFileW( filename, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
880 NULL, OPEN_EXISTING, 0, 0 );
881 if (handle == INVALID_HANDLE_VALUE) return 0;
882 if ((magic = find_version_resource( handle, &len, &offset, flags )))
883 len = read_data( handle, offset, data, min( len, datasize ));
884 CloseHandle( handle );
887 switch (magic)
889 case IMAGE_OS2_SIGNATURE:
890 /* We have a 16bit resource. */
891 if (TRACE_ON(ver))
892 print_vffi_debug( (VS_FIXEDFILEINFO *)VersionInfo16_Value( (VS_VERSION_INFO_STRUCT16 *)data ));
893 SetLastError(0);
894 return TRUE;
896 case IMAGE_NT_SIGNATURE:
897 /* We have a 32bit resource.
899 * XP/W2K/W2K3 uses a buffer which is 2 times the actual needed space + 4 bytes "FE2X"
900 * This extra buffer is used for Unicode to ANSI conversions in A-Calls
902 len = vvis->wLength + sizeof(signature);
903 if (datasize >= len) memcpy( (char*)data + vvis->wLength, signature, sizeof(signature) );
904 if (TRACE_ON(ver))
905 print_vffi_debug( (VS_FIXEDFILEINFO *)VersionInfo32_Value( vvis ));
906 SetLastError(0);
907 return TRUE;
909 default:
910 SetLastError( ERROR_RESOURCE_DATA_NOT_FOUND );
911 return FALSE;
915 /***********************************************************************
916 * GetFileVersionInfoExA (kernelbase.@)
918 BOOL WINAPI GetFileVersionInfoExA( DWORD flags, LPCSTR filename, DWORD handle, DWORD datasize, LPVOID data )
920 UNICODE_STRING filenameW;
921 BOOL retval;
923 TRACE("(0x%x,%s,%d,size=%d,data=%p)\n",
924 flags, debugstr_a(filename), handle, datasize, data );
926 if(filename)
927 RtlCreateUnicodeStringFromAsciiz(&filenameW, filename);
928 else
929 filenameW.Buffer = NULL;
931 retval = GetFileVersionInfoExW(flags, filenameW.Buffer, handle, datasize, data);
933 RtlFreeUnicodeString(&filenameW);
935 return retval;
938 /***********************************************************************
939 * GetFileVersionInfoW (kernelbase.@)
941 BOOL WINAPI GetFileVersionInfoW( LPCWSTR filename, DWORD handle, DWORD datasize, LPVOID data )
943 return GetFileVersionInfoExW(FILE_VER_GET_LOCALISED, filename, handle, datasize, data);
946 /***********************************************************************
947 * GetFileVersionInfoA (kernelbase.@)
949 BOOL WINAPI GetFileVersionInfoA( LPCSTR filename, DWORD handle, DWORD datasize, LPVOID data )
951 return GetFileVersionInfoExA(FILE_VER_GET_LOCALISED, filename, handle, datasize, data);
954 /***********************************************************************
955 * VersionInfo16_FindChild [internal]
957 static const VS_VERSION_INFO_STRUCT16 *VersionInfo16_FindChild( const VS_VERSION_INFO_STRUCT16 *info,
958 LPCSTR key, UINT len )
960 const VS_VERSION_INFO_STRUCT16 *child = VersionInfo16_Children( info );
962 while ((char *)child < (char *)info + info->wLength )
964 if (!strnicmp( child->szKey, key, len ) && !child->szKey[len])
965 return child;
967 if (!(child->wLength)) return NULL;
968 child = VersionInfo16_Next( child );
971 return NULL;
974 /***********************************************************************
975 * VersionInfo32_FindChild [internal]
977 static const VS_VERSION_INFO_STRUCT32 *VersionInfo32_FindChild( const VS_VERSION_INFO_STRUCT32 *info,
978 LPCWSTR key, UINT len )
980 const VS_VERSION_INFO_STRUCT32 *child = VersionInfo32_Children( info );
982 while ((char *)child < (char *)info + info->wLength )
984 if (!wcsnicmp( child->szKey, key, len ) && !child->szKey[len])
985 return child;
987 if (!(child->wLength)) return NULL;
988 child = VersionInfo32_Next( child );
991 return NULL;
994 /***********************************************************************
995 * VersionInfo16_QueryValue [internal]
997 * Gets a value from a 16-bit NE resource
999 static BOOL VersionInfo16_QueryValue( const VS_VERSION_INFO_STRUCT16 *info, LPCSTR lpSubBlock,
1000 LPVOID *lplpBuffer, UINT *puLen )
1002 while ( *lpSubBlock )
1004 /* Find next path component */
1005 LPCSTR lpNextSlash;
1006 for ( lpNextSlash = lpSubBlock; *lpNextSlash; lpNextSlash++ )
1007 if ( *lpNextSlash == '\\' )
1008 break;
1010 /* Skip empty components */
1011 if ( lpNextSlash == lpSubBlock )
1013 lpSubBlock++;
1014 continue;
1017 /* We have a non-empty component: search info for key */
1018 info = VersionInfo16_FindChild( info, lpSubBlock, lpNextSlash-lpSubBlock );
1019 if ( !info )
1021 if (puLen) *puLen = 0 ;
1022 SetLastError( ERROR_RESOURCE_TYPE_NOT_FOUND );
1023 return FALSE;
1026 /* Skip path component */
1027 lpSubBlock = lpNextSlash;
1030 /* Return value */
1031 *lplpBuffer = VersionInfo16_Value( info );
1032 if (puLen)
1033 *puLen = info->wValueLength;
1035 return TRUE;
1038 /***********************************************************************
1039 * VersionInfo32_QueryValue [internal]
1041 * Gets a value from a 32-bit PE resource
1043 static BOOL VersionInfo32_QueryValue( const VS_VERSION_INFO_STRUCT32 *info, LPCWSTR lpSubBlock,
1044 LPVOID *lplpBuffer, UINT *puLen, BOOL *pbText )
1046 TRACE("lpSubBlock : (%s)\n", debugstr_w(lpSubBlock));
1048 while ( *lpSubBlock )
1050 /* Find next path component */
1051 LPCWSTR lpNextSlash;
1052 for ( lpNextSlash = lpSubBlock; *lpNextSlash; lpNextSlash++ )
1053 if ( *lpNextSlash == '\\' )
1054 break;
1056 /* Skip empty components */
1057 if ( lpNextSlash == lpSubBlock )
1059 lpSubBlock++;
1060 continue;
1063 /* We have a non-empty component: search info for key */
1064 info = VersionInfo32_FindChild( info, lpSubBlock, lpNextSlash-lpSubBlock );
1065 if ( !info )
1067 if (puLen) *puLen = 0 ;
1068 SetLastError( ERROR_RESOURCE_TYPE_NOT_FOUND );
1069 return FALSE;
1072 /* Skip path component */
1073 lpSubBlock = lpNextSlash;
1076 /* Return value */
1077 *lplpBuffer = VersionInfo32_Value( info );
1078 if (puLen)
1079 *puLen = info->wValueLength;
1080 if (pbText)
1081 *pbText = info->wType;
1083 return TRUE;
1086 /***********************************************************************
1087 * VerQueryValueA (kernelbase.@)
1089 BOOL WINAPI VerQueryValueA( LPCVOID pBlock, LPCSTR lpSubBlock,
1090 LPVOID *lplpBuffer, PUINT puLen )
1092 static const char rootA[] = "\\";
1093 const VS_VERSION_INFO_STRUCT16 *info = pBlock;
1095 TRACE("(%p,%s,%p,%p)\n",
1096 pBlock, debugstr_a(lpSubBlock), lplpBuffer, puLen );
1098 if (!pBlock)
1099 return FALSE;
1101 if (lpSubBlock == NULL || lpSubBlock[0] == '\0')
1102 lpSubBlock = rootA;
1104 if ( !VersionInfoIs16( info ) )
1106 BOOL ret, isText;
1107 INT len;
1108 LPWSTR lpSubBlockW;
1109 UINT value_len;
1111 len = MultiByteToWideChar(CP_ACP, 0, lpSubBlock, -1, NULL, 0);
1112 lpSubBlockW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1114 if (!lpSubBlockW)
1115 return FALSE;
1117 MultiByteToWideChar(CP_ACP, 0, lpSubBlock, -1, lpSubBlockW, len);
1119 ret = VersionInfo32_QueryValue(pBlock, lpSubBlockW, lplpBuffer, &value_len, &isText);
1120 if (puLen) *puLen = value_len;
1122 HeapFree(GetProcessHeap(), 0, lpSubBlockW);
1124 if (ret && isText)
1126 /* Set lpBuffer so it points to the 'empty' area where we store
1127 * the converted strings
1129 LPSTR lpBufferA = (LPSTR)pBlock + info->wLength + 4;
1130 DWORD pos = (LPCSTR)*lplpBuffer - (LPCSTR)pBlock;
1131 len = WideCharToMultiByte(CP_ACP, 0, *lplpBuffer, value_len,
1132 lpBufferA + pos, info->wLength - pos, NULL, NULL);
1133 *lplpBuffer = lpBufferA + pos;
1134 if (puLen) *puLen = len;
1136 return ret;
1139 return VersionInfo16_QueryValue(info, lpSubBlock, lplpBuffer, puLen);
1142 /***********************************************************************
1143 * VerQueryValueW (kernelbase.@)
1145 BOOL WINAPI VerQueryValueW( LPCVOID pBlock, LPCWSTR lpSubBlock,
1146 LPVOID *lplpBuffer, PUINT puLen )
1148 const VS_VERSION_INFO_STRUCT32 *info = pBlock;
1150 TRACE("(%p,%s,%p,%p)\n",
1151 pBlock, debugstr_w(lpSubBlock), lplpBuffer, puLen );
1153 if (!pBlock)
1154 return FALSE;
1156 if (!lpSubBlock || !lpSubBlock[0])
1157 lpSubBlock = L"\\";
1159 if ( VersionInfoIs16( info ) )
1161 BOOL ret;
1162 int len;
1163 LPSTR lpSubBlockA;
1165 len = WideCharToMultiByte(CP_ACP, 0, lpSubBlock, -1, NULL, 0, NULL, NULL);
1166 lpSubBlockA = HeapAlloc(GetProcessHeap(), 0, len * sizeof(char));
1168 if (!lpSubBlockA)
1169 return FALSE;
1171 WideCharToMultiByte(CP_ACP, 0, lpSubBlock, -1, lpSubBlockA, len, NULL, NULL);
1173 ret = VersionInfo16_QueryValue(pBlock, lpSubBlockA, lplpBuffer, puLen);
1175 HeapFree(GetProcessHeap(), 0, lpSubBlockA);
1177 if (ret && wcscmp( lpSubBlock, L"\\" ) && wcsicmp( lpSubBlock, L"\\VarFileInfo\\Translation" ))
1179 /* Set lpBuffer so it points to the 'empty' area where we store
1180 * the converted strings
1182 LPWSTR lpBufferW = (LPWSTR)((LPSTR)pBlock + info->wLength);
1183 DWORD pos = (LPCSTR)*lplpBuffer - (LPCSTR)pBlock;
1184 DWORD max = (info->wLength - sizeof(VS_FIXEDFILEINFO)) * 4 - info->wLength;
1186 len = MultiByteToWideChar(CP_ACP, 0, *lplpBuffer, -1,
1187 lpBufferW + pos, max/sizeof(WCHAR) - pos );
1188 *lplpBuffer = lpBufferW + pos;
1189 if (puLen) *puLen = len;
1191 return ret;
1194 return VersionInfo32_QueryValue(info, lpSubBlock, lplpBuffer, puLen, NULL);
1198 /******************************************************************************
1199 * file_existsA
1201 static BOOL file_existsA( char const * path, char const * file, BOOL excl )
1203 DWORD sharing = excl ? 0 : FILE_SHARE_READ | FILE_SHARE_WRITE;
1204 char filename[MAX_PATH];
1205 int len;
1206 HANDLE handle;
1208 if (path)
1210 strcpy( filename, path );
1211 len = strlen(filename);
1212 if (len && filename[len - 1] != '\\') strcat( filename, "\\" );
1213 strcat( filename, file );
1215 else if (!SearchPathA( NULL, file, NULL, MAX_PATH, filename, NULL )) return FALSE;
1217 handle = CreateFileA( filename, 0, sharing, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0 );
1218 if (handle == INVALID_HANDLE_VALUE) return FALSE;
1219 CloseHandle( handle );
1220 return TRUE;
1223 /******************************************************************************
1224 * file_existsW
1226 static BOOL file_existsW( const WCHAR *path, const WCHAR *file, BOOL excl )
1228 DWORD sharing = excl ? 0 : FILE_SHARE_READ | FILE_SHARE_WRITE;
1229 WCHAR filename[MAX_PATH];
1230 int len;
1231 HANDLE handle;
1233 if (path)
1235 lstrcpyW( filename, path );
1236 len = lstrlenW(filename);
1237 if (len && filename[len - 1] != '\\') lstrcatW( filename, L"\\" );
1238 lstrcatW( filename, file );
1240 else if (!SearchPathW( NULL, file, NULL, MAX_PATH, filename, NULL )) return FALSE;
1242 handle = CreateFileW( filename, 0, sharing, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0 );
1243 if (handle == INVALID_HANDLE_VALUE) return FALSE;
1244 CloseHandle( handle );
1245 return TRUE;
1248 /*****************************************************************************
1249 * VerFindFileA (kernelbase.@)
1251 * Determines where to install a file based on whether it locates another
1252 * version of the file in the system. The values VerFindFile returns are
1253 * used in a subsequent call to the VerInstallFile function.
1255 DWORD WINAPI VerFindFileA( DWORD flags, LPCSTR filename, LPCSTR win_dir, LPCSTR app_dir,
1256 LPSTR cur_dir, PUINT curdir_len, LPSTR dest, PUINT dest_len )
1258 DWORD retval = 0;
1259 const char *curDir;
1260 const char *destDir;
1261 char winDir[MAX_PATH], systemDir[MAX_PATH];
1263 TRACE("flags = %x filename=%s windir=%s appdir=%s curdirlen=%p(%u) destdirlen=%p(%u)\n",
1264 flags, debugstr_a(filename), debugstr_a(win_dir), debugstr_a(app_dir),
1265 curdir_len, curdir_len ? *curdir_len : 0, dest_len, dest_len ? *dest_len : 0 );
1267 /* Figure out where the file should go; shared files default to the
1268 system directory */
1270 GetSystemDirectoryA(systemDir, sizeof(systemDir));
1271 curDir = "";
1273 if(flags & VFFF_ISSHAREDFILE)
1275 destDir = systemDir;
1276 /* Were we given a filename? If so, try to find the file. */
1277 if(filename)
1279 if(file_existsA(destDir, filename, FALSE)) curDir = destDir;
1280 else if(app_dir && file_existsA(app_dir, filename, FALSE))
1281 curDir = app_dir;
1283 if(!file_existsA(systemDir, filename, FALSE))
1284 retval |= VFF_CURNEDEST;
1287 else /* not a shared file */
1289 destDir = app_dir ? app_dir : "";
1290 if(filename)
1292 GetWindowsDirectoryA( winDir, MAX_PATH );
1293 if(file_existsA(destDir, filename, FALSE)) curDir = destDir;
1294 else if(file_existsA(winDir, filename, FALSE))
1295 curDir = winDir;
1296 else if(file_existsA(systemDir, filename, FALSE))
1297 curDir = systemDir;
1299 if (app_dir && app_dir[0])
1301 if(!file_existsA(app_dir, filename, FALSE))
1302 retval |= VFF_CURNEDEST;
1304 else if(file_existsA(NULL, filename, FALSE))
1305 retval |= VFF_CURNEDEST;
1309 /* Check to see if the file exists and is in use by another application */
1310 if (filename && file_existsA(curDir, filename, FALSE))
1312 if (filename && !file_existsA(curDir, filename, TRUE))
1313 retval |= VFF_FILEINUSE;
1316 if (dest_len && dest)
1318 UINT len = strlen(destDir) + 1;
1319 if (*dest_len < len) retval |= VFF_BUFFTOOSMALL;
1320 lstrcpynA(dest, destDir, *dest_len);
1321 *dest_len = len;
1323 if (curdir_len && cur_dir)
1325 UINT len = strlen(curDir) + 1;
1326 if (*curdir_len < len) retval |= VFF_BUFFTOOSMALL;
1327 lstrcpynA(cur_dir, curDir, *curdir_len);
1328 *curdir_len = len;
1331 TRACE("ret = %u (%s%s%s) curdir=%s destdir=%s\n", retval,
1332 (retval & VFF_CURNEDEST) ? "VFF_CURNEDEST " : "",
1333 (retval & VFF_FILEINUSE) ? "VFF_FILEINUSE " : "",
1334 (retval & VFF_BUFFTOOSMALL) ? "VFF_BUFFTOOSMALL " : "",
1335 debugstr_a(cur_dir), debugstr_a(dest));
1337 return retval;
1340 /*****************************************************************************
1341 * VerFindFileW (kernelbase.@)
1343 DWORD WINAPI VerFindFileW( DWORD flags, LPCWSTR filename, LPCWSTR win_dir, LPCWSTR app_dir,
1344 LPWSTR cur_dir, PUINT curdir_len, LPWSTR dest, PUINT dest_len )
1346 DWORD retval = 0;
1347 const WCHAR *curDir;
1348 const WCHAR *destDir;
1350 TRACE("flags = %x filename=%s windir=%s appdir=%s curdirlen=%p(%u) destdirlen=%p(%u)\n",
1351 flags, debugstr_w(filename), debugstr_w(win_dir), debugstr_w(app_dir),
1352 curdir_len, curdir_len ? *curdir_len : 0, dest_len, dest_len ? *dest_len : 0 );
1354 /* Figure out where the file should go; shared files default to the
1355 system directory */
1357 curDir = L"";
1359 if(flags & VFFF_ISSHAREDFILE)
1361 destDir = system_dir;
1362 /* Were we given a filename? If so, try to find the file. */
1363 if(filename)
1365 if(file_existsW(destDir, filename, FALSE)) curDir = destDir;
1366 else if(app_dir && file_existsW(app_dir, filename, FALSE))
1368 curDir = app_dir;
1369 retval |= VFF_CURNEDEST;
1373 else /* not a shared file */
1375 destDir = app_dir ? app_dir : L"";
1376 if(filename)
1378 if(file_existsW(destDir, filename, FALSE)) curDir = destDir;
1379 else if(file_existsW(windows_dir, filename, FALSE))
1381 curDir = windows_dir;
1382 retval |= VFF_CURNEDEST;
1384 else if (file_existsW(system_dir, filename, FALSE))
1386 curDir = system_dir;
1387 retval |= VFF_CURNEDEST;
1392 if (filename && !file_existsW(curDir, filename, TRUE))
1393 retval |= VFF_FILEINUSE;
1395 if (dest_len && dest)
1397 UINT len = lstrlenW(destDir) + 1;
1398 if (*dest_len < len) retval |= VFF_BUFFTOOSMALL;
1399 lstrcpynW(dest, destDir, *dest_len);
1400 *dest_len = len;
1402 if (curdir_len && cur_dir)
1404 UINT len = lstrlenW(curDir) + 1;
1405 if (*curdir_len < len) retval |= VFF_BUFFTOOSMALL;
1406 lstrcpynW(cur_dir, curDir, *curdir_len);
1407 *curdir_len = len;
1410 TRACE("ret = %u (%s%s%s) curdir=%s destdir=%s\n", retval,
1411 (retval & VFF_CURNEDEST) ? "VFF_CURNEDEST " : "",
1412 (retval & VFF_FILEINUSE) ? "VFF_FILEINUSE " : "",
1413 (retval & VFF_BUFFTOOSMALL) ? "VFF_BUFFTOOSMALL " : "",
1414 debugstr_w(cur_dir), debugstr_w(dest));
1415 return retval;
1419 /***********************************************************************
1420 * GetProductInfo (kernelbase.@)
1422 BOOL WINAPI DECLSPEC_HOTPATCH GetProductInfo( DWORD os_major, DWORD os_minor,
1423 DWORD sp_major, DWORD sp_minor, DWORD *type )
1425 return RtlGetProductInfo( os_major, os_minor, sp_major, sp_minor, type );
1429 /***********************************************************************
1430 * GetVersion (kernelbase.@)
1432 DWORD WINAPI GetVersion(void)
1434 OSVERSIONINFOEXW info;
1435 DWORD result;
1437 info.dwOSVersionInfoSize = sizeof(info);
1438 if (!GetVersionExW( (OSVERSIONINFOW *)&info )) return 0;
1440 result = MAKELONG( MAKEWORD( info.dwMajorVersion, info.dwMinorVersion ),
1441 (info.dwPlatformId ^ 2) << 14 );
1443 if (info.dwPlatformId == VER_PLATFORM_WIN32_NT)
1444 result |= LOWORD(info.dwBuildNumber) << 16;
1445 return result;
1449 /***********************************************************************
1450 * GetVersionExA (kernelbase.@)
1452 BOOL WINAPI GetVersionExA( OSVERSIONINFOA *info )
1454 OSVERSIONINFOEXW infoW;
1456 if (info->dwOSVersionInfoSize != sizeof(OSVERSIONINFOA) &&
1457 info->dwOSVersionInfoSize != sizeof(OSVERSIONINFOEXA))
1459 WARN( "wrong OSVERSIONINFO size from app (got: %d)\n", info->dwOSVersionInfoSize );
1460 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1461 return FALSE;
1464 infoW.dwOSVersionInfoSize = sizeof(infoW);
1465 if (!GetVersionExW( (OSVERSIONINFOW *)&infoW )) return FALSE;
1467 info->dwMajorVersion = infoW.dwMajorVersion;
1468 info->dwMinorVersion = infoW.dwMinorVersion;
1469 info->dwBuildNumber = infoW.dwBuildNumber;
1470 info->dwPlatformId = infoW.dwPlatformId;
1471 WideCharToMultiByte( CP_ACP, 0, infoW.szCSDVersion, -1,
1472 info->szCSDVersion, sizeof(info->szCSDVersion), NULL, NULL );
1474 if (info->dwOSVersionInfoSize == sizeof(OSVERSIONINFOEXA))
1476 OSVERSIONINFOEXA *vex = (OSVERSIONINFOEXA *)info;
1477 vex->wServicePackMajor = infoW.wServicePackMajor;
1478 vex->wServicePackMinor = infoW.wServicePackMinor;
1479 vex->wSuiteMask = infoW.wSuiteMask;
1480 vex->wProductType = infoW.wProductType;
1482 return TRUE;
1486 /***********************************************************************
1487 * GetVersionExW (kernelbase.@)
1489 BOOL WINAPI GetVersionExW( OSVERSIONINFOW *info )
1491 static INIT_ONCE init_once = INIT_ONCE_STATIC_INIT;
1493 if (info->dwOSVersionInfoSize != sizeof(OSVERSIONINFOW) &&
1494 info->dwOSVersionInfoSize != sizeof(OSVERSIONINFOEXW))
1496 WARN( "wrong OSVERSIONINFO size from app (got: %d)\n", info->dwOSVersionInfoSize );
1497 return FALSE;
1500 if (!InitOnceExecuteOnce(&init_once, init_current_version, NULL, NULL)) return FALSE;
1502 info->dwMajorVersion = current_version.dwMajorVersion;
1503 info->dwMinorVersion = current_version.dwMinorVersion;
1504 info->dwBuildNumber = current_version.dwBuildNumber;
1505 info->dwPlatformId = current_version.dwPlatformId;
1506 wcscpy( info->szCSDVersion, current_version.szCSDVersion );
1508 if (info->dwOSVersionInfoSize == sizeof(OSVERSIONINFOEXW))
1510 OSVERSIONINFOEXW *vex = (OSVERSIONINFOEXW *)info;
1511 vex->wServicePackMajor = current_version.wServicePackMajor;
1512 vex->wServicePackMinor = current_version.wServicePackMinor;
1513 vex->wSuiteMask = current_version.wSuiteMask;
1514 vex->wProductType = current_version.wProductType;
1516 return TRUE;
1520 /***********************************************************************
1521 * GetCurrentPackageFamilyName (kernelbase.@)
1523 LONG WINAPI /* DECLSPEC_HOTPATCH */ GetCurrentPackageFamilyName( UINT32 *length, WCHAR *name )
1525 FIXME( "(%p %p): stub\n", length, name );
1526 return APPMODEL_ERROR_NO_PACKAGE;
1530 /***********************************************************************
1531 * GetCurrentPackageFullName (kernelbase.@)
1533 LONG WINAPI /* DECLSPEC_HOTPATCH */ GetCurrentPackageFullName( UINT32 *length, WCHAR *name )
1535 FIXME( "(%p %p): stub\n", length, name );
1536 return APPMODEL_ERROR_NO_PACKAGE;
1540 /***********************************************************************
1541 * GetCurrentPackageId (kernelbase.@)
1543 LONG WINAPI /* DECLSPEC_HOTPATCH */ GetCurrentPackageId( UINT32 *len, BYTE *buffer )
1545 FIXME( "(%p %p): stub\n", len, buffer );
1546 return APPMODEL_ERROR_NO_PACKAGE;
1550 /***********************************************************************
1551 * GetPackageFullName (kernelbase.@)
1553 LONG WINAPI /* DECLSPEC_HOTPATCH */ GetPackageFullName( HANDLE process, UINT32 *length, WCHAR *name )
1555 FIXME( "(%p %p %p): stub\n", process, length, name );
1556 return APPMODEL_ERROR_NO_PACKAGE;
1560 /***********************************************************************
1561 * GetPackageFamilyName (kernelbase.@)
1563 LONG WINAPI /* DECLSPEC_HOTPATCH */ GetPackageFamilyName( HANDLE process, UINT32 *length, WCHAR *name )
1565 FIXME( "(%p %p %p): stub\n", process, length, name );
1566 return APPMODEL_ERROR_NO_PACKAGE;