include/mscvpdb.h: Use flexible array members for the rest of structures.
[wine.git] / dlls / kernelbase / version.c
blob0636634badb27363c33fb7ea5416b845b27f84bb
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 #include "windef.h"
34 #include "winbase.h"
35 #include "winver.h"
36 #include "winuser.h"
37 #include "winnls.h"
38 #include "winternl.h"
39 #include "winerror.h"
40 #include "appmodel.h"
42 #include "kernelbase.h"
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(ver);
47 typedef struct
49 WORD offset;
50 WORD length;
51 WORD flags;
52 WORD id;
53 WORD handle;
54 WORD usage;
55 } NE_NAMEINFO;
57 typedef struct
59 WORD type_id;
60 WORD count;
61 DWORD resloader;
62 } NE_TYPEINFO;
64 struct version_info
66 DWORD major;
67 DWORD minor;
68 DWORD build;
71 /***********************************************************************
72 * Version Info Structure
75 typedef struct
77 WORD wLength;
78 WORD wValueLength;
79 CHAR szKey[1];
80 #if 0 /* variable length structure */
81 /* DWORD aligned */
82 BYTE Value[];
83 /* DWORD aligned */
84 VS_VERSION_INFO_STRUCT16 Children[];
85 #endif
86 } VS_VERSION_INFO_STRUCT16;
88 typedef struct
90 WORD wLength;
91 WORD wValueLength;
92 WORD wType; /* 1:Text, 0:Binary */
93 WCHAR szKey[1];
94 #if 0 /* variable length structure */
95 /* DWORD aligned */
96 BYTE Value[];
97 /* DWORD aligned */
98 VS_VERSION_INFO_STRUCT32 Children[];
99 #endif
100 } VS_VERSION_INFO_STRUCT32;
102 #define VersionInfoIs16( ver ) \
103 ( ((const VS_VERSION_INFO_STRUCT16 *)ver)->szKey[0] >= ' ' )
105 #define DWORD_ALIGN( base, ptr ) \
106 ( (LPBYTE)(base) + ((((LPBYTE)(ptr) - (LPBYTE)(base)) + 3) & ~3) )
108 #define VersionInfo16_Value( ver ) \
109 DWORD_ALIGN( (ver), (ver)->szKey + strlen((ver)->szKey) + 1 )
110 #define VersionInfo32_Value( ver ) \
111 DWORD_ALIGN( (ver), (ver)->szKey + lstrlenW((ver)->szKey) + 1 )
113 #define VersionInfo16_Children( ver ) \
114 (const VS_VERSION_INFO_STRUCT16 *)( VersionInfo16_Value( ver ) + \
115 ( ( (ver)->wValueLength + 3 ) & ~3 ) )
116 #define VersionInfo32_Children( ver ) \
117 (const VS_VERSION_INFO_STRUCT32 *)( VersionInfo32_Value( ver ) + \
118 ( ( (ver)->wValueLength * \
119 ((ver)->wType? 2 : 1) + 3 ) & ~3 ) )
121 #define VersionInfo16_Next( ver ) \
122 (VS_VERSION_INFO_STRUCT16 *)( (LPBYTE)ver + (((ver)->wLength + 3) & ~3) )
123 #define VersionInfo32_Next( ver ) \
124 (VS_VERSION_INFO_STRUCT32 *)( (LPBYTE)ver + (((ver)->wLength + 3) & ~3) )
127 /***********************************************************************
128 * Win8 info, reported if the app doesn't provide compat GUID in the manifest and
129 * doesn't have higher OS version in PE header.
131 static const struct version_info windows8_version_info = { 6, 2, 9200 };
133 /***********************************************************************
134 * Win8.1 info, reported if the app doesn't provide compat GUID in the manifest and
135 * OS version in PE header is 8.1 or higher but below 10.
137 static const struct version_info windows8_1_version_info = { 6, 3, 9600 };
140 /***********************************************************************
141 * Windows versions that need compatibility GUID specified in manifest
142 * in order to be reported by the APIs.
144 static const struct
146 struct version_info info;
147 GUID guid;
148 } version_data[] =
150 /* Windows 8.1 */
152 { 6, 3, 9600 },
153 {0x1f676c76,0x80e1,0x4239,{0x95,0xbb,0x83,0xd0,0xf6,0xd0,0xda,0x78}}
155 /* Windows 10 */
157 { 10, 0, 19043 },
158 {0x8e0f7a12,0xbfb3,0x4fe8,{0xb9,0xa5,0x48,0xfd,0x50,0xa1,0x5a,0x9a}}
163 /******************************************************************************
164 * init_current_version
166 * Initialize the current_version variable.
168 * For compatibility, Windows 8.1 and later report Win8 version unless the app
169 * has a manifest or higher OS version in the PE optional header
170 * that confirms its compatibility with newer versions of Windows.
173 static RTL_OSVERSIONINFOEXW current_version;
175 static BOOL CALLBACK init_current_version(PINIT_ONCE init_once, PVOID parameter, PVOID *context)
177 struct acci
179 DWORD ElementCount;
180 COMPATIBILITY_CONTEXT_ELEMENT Elements[1];
181 } *acci;
182 BOOL have_os_compat_elements = FALSE;
183 const struct version_info *ver;
184 IMAGE_NT_HEADERS *nt;
185 SIZE_T req;
186 int idx;
188 current_version.dwOSVersionInfoSize = sizeof(current_version);
189 if (!set_ntstatus( RtlGetVersion(&current_version) )) return FALSE;
191 for (idx = ARRAY_SIZE(version_data); idx--;)
192 if ( current_version.dwMajorVersion > version_data[idx].info.major ||
193 (current_version.dwMajorVersion == version_data[idx].info.major &&
194 current_version.dwMinorVersion >= version_data[idx].info.minor))
195 break;
197 if (idx < 0) return TRUE;
198 ver = &windows8_version_info;
200 if (RtlQueryInformationActivationContext(0, NtCurrentTeb()->Peb->ActivationContextData, NULL,
201 CompatibilityInformationInActivationContext, NULL, 0, &req) != STATUS_BUFFER_TOO_SMALL
202 || !req)
203 goto done;
205 if (!(acci = HeapAlloc(GetProcessHeap(), 0, req)))
207 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
208 return FALSE;
211 if (RtlQueryInformationActivationContext(0, NtCurrentTeb()->Peb->ActivationContextData, NULL,
212 CompatibilityInformationInActivationContext, acci, req, &req) == STATUS_SUCCESS)
216 DWORD i;
218 for (i = 0; i < acci->ElementCount; i++)
220 if (acci->Elements[i].Type != ACTCTX_COMPATIBILITY_ELEMENT_TYPE_OS)
221 continue;
223 have_os_compat_elements = TRUE;
225 if (IsEqualGUID(&acci->Elements[i].Id, &version_data[idx].guid))
227 ver = &version_data[idx].info;
229 if (ver->major == current_version.dwMajorVersion &&
230 ver->minor == current_version.dwMinorVersion)
231 ver = NULL;
233 idx = 0; /* break from outer loop */
234 break;
237 } while (idx--);
239 HeapFree(GetProcessHeap(), 0, acci);
241 done:
242 if (!have_os_compat_elements && current_version.dwMajorVersion >= 10
243 && (nt = RtlImageNtHeader(NtCurrentTeb()->Peb->ImageBaseAddress))
244 && (nt->OptionalHeader.MajorOperatingSystemVersion > 6
245 || (nt->OptionalHeader.MajorOperatingSystemVersion == 6
246 && nt->OptionalHeader.MinorOperatingSystemVersion >= 3)))
248 if (current_version.dwMajorVersion > 10)
249 FIXME("Unsupported current_version.dwMajorVersion %lu.\n", current_version.dwMajorVersion);
251 ver = nt->OptionalHeader.MajorOperatingSystemVersion >= 10 ? NULL : &windows8_1_version_info;
254 if (ver)
256 current_version.dwMajorVersion = ver->major;
257 current_version.dwMinorVersion = ver->minor;
258 current_version.dwBuildNumber = ver->build;
260 return TRUE;
264 /**********************************************************************
265 * find_entry_by_id
267 * Find an entry by id in a resource directory
268 * Copied from loader/pe_resource.c
270 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_id( const IMAGE_RESOURCE_DIRECTORY *dir,
271 WORD id, const void *root,
272 DWORD root_size )
274 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
275 int min, max, pos;
277 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
278 min = dir->NumberOfNamedEntries;
279 max = min + dir->NumberOfIdEntries - 1;
281 if (max >= (root_size - ((INT_PTR)dir - (INT_PTR)root) - sizeof(*dir)) / sizeof(*entry))
282 return NULL;
284 while (min <= max)
286 pos = (min + max) / 2;
287 if (entry[pos].Id == id)
289 DWORD offset = entry[pos].OffsetToDirectory;
290 if (offset > root_size - sizeof(*dir)) return NULL;
291 return (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + offset);
293 if (entry[pos].Id > id) max = pos - 1;
294 else min = pos + 1;
296 return NULL;
300 /**********************************************************************
301 * find_entry_default
303 * Find a default entry in a resource directory
304 * Copied from loader/pe_resource.c
306 static const IMAGE_RESOURCE_DIRECTORY *find_entry_default( const IMAGE_RESOURCE_DIRECTORY *dir,
307 const void *root )
309 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
311 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
312 return (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry->OffsetToDirectory);
316 /**********************************************************************
317 * push_language
319 * push a language onto the list of languages to try
321 static inline int push_language( WORD *list, int pos, WORD lang )
323 int i;
324 for (i = 0; i < pos; i++) if (list[i] == lang) return pos;
325 list[pos++] = lang;
326 return pos;
330 /**********************************************************************
331 * find_entry_language
333 static const IMAGE_RESOURCE_DIRECTORY *find_entry_language( const IMAGE_RESOURCE_DIRECTORY *dir,
334 const void *root, DWORD root_size,
335 DWORD flags )
337 const IMAGE_RESOURCE_DIRECTORY *ret;
338 WORD list[9];
339 int i, pos = 0;
341 if (flags & FILE_VER_GET_LOCALISED)
343 /* cf. LdrFindResource_U */
344 pos = push_language( list, pos, MAKELANGID( LANG_NEUTRAL, SUBLANG_NEUTRAL ) );
345 pos = push_language( list, pos, LANGIDFROMLCID( NtCurrentTeb()->CurrentLocale ) );
346 pos = push_language( list, pos, GetUserDefaultLangID() );
347 pos = push_language( list, pos, MAKELANGID( PRIMARYLANGID(GetUserDefaultLangID()), SUBLANG_NEUTRAL ));
348 pos = push_language( list, pos, MAKELANGID( PRIMARYLANGID(GetUserDefaultLangID()), SUBLANG_DEFAULT ));
349 pos = push_language( list, pos, GetSystemDefaultLangID() );
350 pos = push_language( list, pos, MAKELANGID( PRIMARYLANGID(GetSystemDefaultLangID()), SUBLANG_NEUTRAL ));
351 pos = push_language( list, pos, MAKELANGID( PRIMARYLANGID(GetSystemDefaultLangID()), SUBLANG_DEFAULT ));
352 pos = push_language( list, pos, MAKELANGID( LANG_ENGLISH, SUBLANG_DEFAULT ) );
354 else
356 /* FIXME: resolve LN file here */
357 pos = push_language( list, pos, MAKELANGID( LANG_ENGLISH, SUBLANG_DEFAULT ) );
360 for (i = 0; i < pos; i++) if ((ret = find_entry_by_id( dir, list[i], root, root_size ))) return ret;
361 return find_entry_default( dir, root );
365 static DWORD read_data( HANDLE handle, DWORD offset, void *data, DWORD len )
367 DWORD res;
369 SetFilePointer( handle, offset, NULL, FILE_BEGIN );
370 if (!ReadFile( handle, data, len, &res, NULL )) res = 0;
371 return res;
374 /***********************************************************************
375 * find_ne_resource [internal]
377 static BOOL find_ne_resource( HANDLE handle, DWORD *resLen, DWORD *resOff )
379 const WORD typeid = VS_FILE_INFO | 0x8000;
380 const WORD resid = VS_VERSION_INFO | 0x8000;
381 IMAGE_OS2_HEADER nehd;
382 NE_TYPEINFO *typeInfo;
383 NE_NAMEINFO *nameInfo;
384 DWORD nehdoffset = *resOff;
385 LPBYTE resTab;
386 DWORD resTabSize;
387 int count;
389 /* Read in NE header */
390 if (read_data( handle, nehdoffset, &nehd, sizeof(nehd) ) != sizeof(nehd)) return FALSE;
392 resTabSize = nehd.ne_restab - nehd.ne_rsrctab;
393 if ( !resTabSize )
395 TRACE("No resources in NE dll\n" );
396 return FALSE;
399 /* Read in resource table */
400 resTab = HeapAlloc( GetProcessHeap(), 0, resTabSize );
401 if ( !resTab ) return FALSE;
403 if (read_data( handle, nehd.ne_rsrctab + nehdoffset, resTab, resTabSize ) != resTabSize)
405 HeapFree( GetProcessHeap(), 0, resTab );
406 return FALSE;
409 /* Find resource */
410 typeInfo = (NE_TYPEINFO *)(resTab + 2);
411 while (typeInfo->type_id)
413 if (typeInfo->type_id == typeid) goto found_type;
414 typeInfo = (NE_TYPEINFO *)((char *)(typeInfo + 1) +
415 typeInfo->count * sizeof(NE_NAMEINFO));
417 TRACE("No typeid entry found\n" );
418 HeapFree( GetProcessHeap(), 0, resTab );
419 return FALSE;
421 found_type:
422 nameInfo = (NE_NAMEINFO *)(typeInfo + 1);
424 for (count = typeInfo->count; count > 0; count--, nameInfo++)
425 if (nameInfo->id == resid) goto found_name;
427 TRACE("No resid entry found\n" );
428 HeapFree( GetProcessHeap(), 0, resTab );
429 return FALSE;
431 found_name:
432 /* Return resource data */
433 *resLen = nameInfo->length << *(WORD *)resTab;
434 *resOff = nameInfo->offset << *(WORD *)resTab;
436 HeapFree( GetProcessHeap(), 0, resTab );
437 return TRUE;
440 /***********************************************************************
441 * find_pe_resource [internal]
443 static BOOL find_pe_resource( HANDLE handle, DWORD *resLen, DWORD *resOff, DWORD flags )
445 union
447 IMAGE_NT_HEADERS32 nt32;
448 IMAGE_NT_HEADERS64 nt64;
449 } pehd;
450 DWORD pehdoffset = *resOff;
451 PIMAGE_DATA_DIRECTORY resDataDir;
452 PIMAGE_SECTION_HEADER sections;
453 LPBYTE resSection;
454 DWORD len, section_size, data_size, resDirSize;
455 const void *resDir;
456 const IMAGE_RESOURCE_DIRECTORY *resPtr;
457 const IMAGE_RESOURCE_DATA_ENTRY *resData;
458 int i, nSections;
459 BOOL ret = FALSE;
461 /* Read in PE header */
462 len = read_data( handle, pehdoffset, &pehd, sizeof(pehd) );
463 if (len < sizeof(pehd.nt32.FileHeader)) return FALSE;
464 if (len < sizeof(pehd)) memset( (char *)&pehd + len, 0, sizeof(pehd) - len );
466 switch (pehd.nt32.OptionalHeader.Magic)
468 case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
469 resDataDir = pehd.nt32.OptionalHeader.DataDirectory + IMAGE_DIRECTORY_ENTRY_RESOURCE;
470 break;
471 case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
472 resDataDir = pehd.nt64.OptionalHeader.DataDirectory + IMAGE_DIRECTORY_ENTRY_RESOURCE;
473 break;
474 default:
475 return FALSE;
478 if ( !resDataDir->Size )
480 TRACE("No resources in PE dll\n" );
481 return FALSE;
484 /* Read in section table */
485 nSections = pehd.nt32.FileHeader.NumberOfSections;
486 sections = HeapAlloc( GetProcessHeap(), 0,
487 nSections * sizeof(IMAGE_SECTION_HEADER) );
488 if ( !sections ) return FALSE;
490 len = FIELD_OFFSET( IMAGE_NT_HEADERS32, OptionalHeader ) + pehd.nt32.FileHeader.SizeOfOptionalHeader;
491 if (read_data( handle, pehdoffset + len, sections, nSections * sizeof(IMAGE_SECTION_HEADER) ) !=
492 nSections * sizeof(IMAGE_SECTION_HEADER))
494 HeapFree( GetProcessHeap(), 0, sections );
495 return FALSE;
498 /* Find resource section */
499 for ( i = 0; i < nSections; i++ )
500 if ( resDataDir->VirtualAddress >= sections[i].VirtualAddress
501 && resDataDir->VirtualAddress < sections[i].VirtualAddress +
502 sections[i].SizeOfRawData )
503 break;
505 if ( i == nSections )
507 HeapFree( GetProcessHeap(), 0, sections );
508 TRACE("Couldn't find resource section\n" );
509 return FALSE;
512 /* Read in resource section */
513 data_size = sections[i].SizeOfRawData;
514 section_size = max( data_size, sections[i].Misc.VirtualSize );
515 resSection = HeapAlloc( GetProcessHeap(), 0, section_size );
516 if ( !resSection )
518 HeapFree( GetProcessHeap(), 0, sections );
519 return FALSE;
522 if (read_data( handle, sections[i].PointerToRawData, resSection, data_size ) != data_size) goto done;
523 if (data_size < section_size) memset( (char *)resSection + data_size, 0, section_size - data_size );
525 /* Find resource */
526 resDir = resSection + (resDataDir->VirtualAddress - sections[i].VirtualAddress);
527 resDirSize = section_size - (resDataDir->VirtualAddress - sections[i].VirtualAddress);
529 resPtr = resDir;
530 resPtr = find_entry_by_id( resPtr, VS_FILE_INFO, resDir, resDirSize );
531 if ( !resPtr )
533 TRACE("No typeid entry found\n" );
534 goto done;
536 resPtr = find_entry_by_id( resPtr, VS_VERSION_INFO, resDir, resDirSize );
537 if ( !resPtr )
539 TRACE("No resid entry found\n" );
540 goto done;
542 resPtr = find_entry_language( resPtr, resDir, resDirSize, flags );
543 if ( !resPtr )
545 TRACE("No default language entry found\n" );
546 goto done;
549 /* Find resource data section */
550 resData = (const IMAGE_RESOURCE_DATA_ENTRY*)resPtr;
551 for ( i = 0; i < nSections; i++ )
552 if ( resData->OffsetToData >= sections[i].VirtualAddress
553 && resData->OffsetToData < sections[i].VirtualAddress +
554 sections[i].SizeOfRawData )
555 break;
557 if ( i == nSections )
559 TRACE("Couldn't find resource data section\n" );
560 goto done;
563 /* Return resource data */
564 *resLen = resData->Size;
565 *resOff = resData->OffsetToData - sections[i].VirtualAddress + sections[i].PointerToRawData;
566 ret = TRUE;
568 done:
569 HeapFree( GetProcessHeap(), 0, resSection );
570 HeapFree( GetProcessHeap(), 0, sections );
571 return ret;
575 /***********************************************************************
576 * find_version_resource [internal]
578 static DWORD find_version_resource( HANDLE handle, DWORD *reslen, DWORD *offset, DWORD flags )
580 IMAGE_DOS_HEADER mzh;
581 WORD magic;
583 if (read_data( handle, 0, &mzh, sizeof(mzh) ) != sizeof(mzh)) return 0;
584 if (mzh.e_magic != IMAGE_DOS_SIGNATURE) return 0;
586 if (read_data( handle, mzh.e_lfanew, &magic, sizeof(magic) ) != sizeof(magic)) return 0;
587 *offset = mzh.e_lfanew;
589 switch (magic)
591 case IMAGE_OS2_SIGNATURE:
592 if (!find_ne_resource( handle, reslen, offset )) magic = 0;
593 break;
594 case IMAGE_NT_SIGNATURE:
595 if (!find_pe_resource( handle, reslen, offset, flags )) magic = 0;
596 break;
598 WARN( "Can't handle %04x files.\n", magic );
599 return magic;
602 /******************************************************************************
603 * This function will print via standard TRACE, debug info regarding
604 * the file info structure vffi.
606 static void print_vffi_debug(const VS_FIXEDFILEINFO *vffi)
608 BOOL versioned_printer = FALSE;
610 if((vffi->dwFileType == VFT_DLL) || (vffi->dwFileType == VFT_DRV))
612 if(vffi->dwFileSubtype == VFT2_DRV_VERSIONED_PRINTER)
613 /* this is documented for newer w2k Drivers and up */
614 versioned_printer = TRUE;
615 else if( (vffi->dwFileSubtype == VFT2_DRV_PRINTER) &&
616 (vffi->dwFileVersionMS != vffi->dwProductVersionMS) &&
617 (vffi->dwFileVersionMS > 0) &&
618 (vffi->dwFileVersionMS <= 3) )
619 /* found this on NT 3.51, NT4.0 and old w2k Drivers */
620 versioned_printer = TRUE;
623 TRACE("structversion=%u.%u, ",
624 HIWORD(vffi->dwStrucVersion),LOWORD(vffi->dwStrucVersion));
625 if(versioned_printer)
627 WORD mode = LOWORD(vffi->dwFileVersionMS);
628 WORD ver_rev = HIWORD(vffi->dwFileVersionLS);
629 TRACE("fileversion=%lu.%u.%u.%u (%s.major.minor.release), ",
630 (vffi->dwFileVersionMS),
631 HIBYTE(ver_rev), LOBYTE(ver_rev), LOWORD(vffi->dwFileVersionLS),
632 (mode == 3) ? "Usermode" : ((mode <= 2) ? "Kernelmode" : "?") );
634 else
636 TRACE("fileversion=%u.%u.%u.%u, ",
637 HIWORD(vffi->dwFileVersionMS),LOWORD(vffi->dwFileVersionMS),
638 HIWORD(vffi->dwFileVersionLS),LOWORD(vffi->dwFileVersionLS));
640 TRACE("productversion=%u.%u.%u.%u\n",
641 HIWORD(vffi->dwProductVersionMS),LOWORD(vffi->dwProductVersionMS),
642 HIWORD(vffi->dwProductVersionLS),LOWORD(vffi->dwProductVersionLS));
644 TRACE("flagmask=0x%lx, flags=0x%lx %s%s%s%s%s%s\n",
645 vffi->dwFileFlagsMask, vffi->dwFileFlags,
646 (vffi->dwFileFlags & VS_FF_DEBUG) ? "DEBUG," : "",
647 (vffi->dwFileFlags & VS_FF_PRERELEASE) ? "PRERELEASE," : "",
648 (vffi->dwFileFlags & VS_FF_PATCHED) ? "PATCHED," : "",
649 (vffi->dwFileFlags & VS_FF_PRIVATEBUILD) ? "PRIVATEBUILD," : "",
650 (vffi->dwFileFlags & VS_FF_INFOINFERRED) ? "INFOINFERRED," : "",
651 (vffi->dwFileFlags & VS_FF_SPECIALBUILD) ? "SPECIALBUILD," : "");
653 TRACE("(");
655 TRACE("OS=0x%x.0x%x ", HIWORD(vffi->dwFileOS), LOWORD(vffi->dwFileOS));
657 switch (vffi->dwFileOS&0xFFFF0000)
659 case VOS_DOS:TRACE("DOS,");break;
660 case VOS_OS216:TRACE("OS/2-16,");break;
661 case VOS_OS232:TRACE("OS/2-32,");break;
662 case VOS_NT:TRACE("NT,");break;
663 case VOS_UNKNOWN:
664 default:
665 TRACE("UNKNOWN(0x%lx),",vffi->dwFileOS&0xFFFF0000);break;
668 switch (LOWORD(vffi->dwFileOS))
670 case VOS__BASE:TRACE("BASE");break;
671 case VOS__WINDOWS16:TRACE("WIN16");break;
672 case VOS__WINDOWS32:TRACE("WIN32");break;
673 case VOS__PM16:TRACE("PM16");break;
674 case VOS__PM32:TRACE("PM32");break;
675 default:
676 TRACE("UNKNOWN(0x%x)",LOWORD(vffi->dwFileOS));break;
679 TRACE(")\n");
681 switch (vffi->dwFileType)
683 case VFT_APP:TRACE("filetype=APP");break;
684 case VFT_DLL:
685 TRACE("filetype=DLL");
686 if(vffi->dwFileSubtype != 0)
688 if(versioned_printer) /* NT3.x/NT4.0 or old w2k Driver */
689 TRACE(",PRINTER");
690 TRACE(" (subtype=0x%lx)", vffi->dwFileSubtype);
692 break;
693 case VFT_DRV:
694 TRACE("filetype=DRV,");
695 switch(vffi->dwFileSubtype)
697 case VFT2_DRV_PRINTER:TRACE("PRINTER");break;
698 case VFT2_DRV_KEYBOARD:TRACE("KEYBOARD");break;
699 case VFT2_DRV_LANGUAGE:TRACE("LANGUAGE");break;
700 case VFT2_DRV_DISPLAY:TRACE("DISPLAY");break;
701 case VFT2_DRV_MOUSE:TRACE("MOUSE");break;
702 case VFT2_DRV_NETWORK:TRACE("NETWORK");break;
703 case VFT2_DRV_SYSTEM:TRACE("SYSTEM");break;
704 case VFT2_DRV_INSTALLABLE:TRACE("INSTALLABLE");break;
705 case VFT2_DRV_SOUND:TRACE("SOUND");break;
706 case VFT2_DRV_COMM:TRACE("COMM");break;
707 case VFT2_DRV_INPUTMETHOD:TRACE("INPUTMETHOD");break;
708 case VFT2_DRV_VERSIONED_PRINTER:TRACE("VERSIONED_PRINTER");break;
709 case VFT2_UNKNOWN:
710 default:
711 TRACE("UNKNOWN(0x%lx)",vffi->dwFileSubtype);break;
713 break;
714 case VFT_FONT:
715 TRACE("filetype=FONT,");
716 switch (vffi->dwFileSubtype)
718 case VFT2_FONT_RASTER:TRACE("RASTER");break;
719 case VFT2_FONT_VECTOR:TRACE("VECTOR");break;
720 case VFT2_FONT_TRUETYPE:TRACE("TRUETYPE");break;
721 default:TRACE("UNKNOWN(0x%lx)",vffi->dwFileSubtype);break;
723 break;
724 case VFT_VXD:TRACE("filetype=VXD");break;
725 case VFT_STATIC_LIB:TRACE("filetype=STATIC_LIB");break;
726 case VFT_UNKNOWN:
727 default:
728 TRACE("filetype=Unknown(0x%lx)",vffi->dwFileType);break;
731 TRACE("\n");
732 TRACE("filedate=0x%lx.0x%lx\n",vffi->dwFileDateMS,vffi->dwFileDateLS);
735 /***********************************************************************
736 * GetFileVersionInfoSizeW (kernelbase.@)
738 DWORD WINAPI GetFileVersionInfoSizeW( LPCWSTR filename, LPDWORD handle )
740 return GetFileVersionInfoSizeExW( FILE_VER_GET_LOCALISED, filename, handle );
743 /***********************************************************************
744 * GetFileVersionInfoSizeA (kernelbase.@)
746 DWORD WINAPI GetFileVersionInfoSizeA( LPCSTR filename, LPDWORD handle )
748 return GetFileVersionInfoSizeExA( FILE_VER_GET_LOCALISED, filename, handle );
751 /******************************************************************************
752 * GetFileVersionInfoSizeExW (kernelbase.@)
754 DWORD WINAPI GetFileVersionInfoSizeExW( DWORD flags, LPCWSTR filename, LPDWORD ret_handle )
756 DWORD len, offset, magic = 1;
757 HMODULE hModule;
759 TRACE("(0x%lx,%s,%p)\n", flags, debugstr_w(filename), ret_handle );
761 if (ret_handle) *ret_handle = 0;
763 if (!filename)
765 SetLastError(ERROR_INVALID_PARAMETER);
766 return 0;
768 if (!*filename)
770 SetLastError(ERROR_BAD_PATHNAME);
771 return 0;
773 if (flags & ~FILE_VER_GET_LOCALISED)
774 FIXME("flags 0x%lx ignored\n", flags & ~FILE_VER_GET_LOCALISED);
776 if ((hModule = LoadLibraryExW( filename, 0, LOAD_LIBRARY_AS_IMAGE_RESOURCE )))
778 HRSRC hRsrc = NULL;
779 if (!(flags & FILE_VER_GET_LOCALISED))
781 LANGID english = MAKELANGID( LANG_ENGLISH, SUBLANG_DEFAULT );
782 hRsrc = FindResourceExW( hModule, (LPWSTR)VS_FILE_INFO,
783 MAKEINTRESOURCEW(VS_VERSION_INFO), english );
785 if (!hRsrc)
786 hRsrc = FindResourceW( hModule, MAKEINTRESOURCEW(VS_VERSION_INFO),
787 (LPWSTR)VS_FILE_INFO );
788 if (hRsrc)
790 magic = IMAGE_NT_SIGNATURE;
791 len = SizeofResource( hModule, hRsrc );
793 FreeLibrary( hModule );
795 else
797 HANDLE handle = CreateFileW( filename, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
798 NULL, OPEN_EXISTING, 0, 0 );
799 if (handle == INVALID_HANDLE_VALUE) return 0;
800 magic = find_version_resource( handle, &len, &offset, flags );
801 CloseHandle( handle );
804 switch (magic)
806 case IMAGE_OS2_SIGNATURE:
807 /* We have a 16bit resource.
809 * XP/W2K/W2K3 uses a buffer which is more than the actual needed space:
811 * (info->wLength - sizeof(VS_FIXEDFILEINFO)) * 4
813 * This extra buffer is used for ANSI to Unicode conversions in W-Calls.
814 * info->wLength should be the same as len. Currently it isn't but that
815 * doesn't seem to be a problem (len is bigger than info->wLength).
817 SetLastError(0);
818 return (len - sizeof(VS_FIXEDFILEINFO)) * 4;
820 case IMAGE_NT_SIGNATURE:
821 /* We have a 32bit resource.
823 * XP/W2K/W2K3 uses a buffer which is 2 times the actual needed space + 4 bytes "FE2X"
824 * This extra buffer is used for Unicode to ANSI conversions in A-Calls
826 SetLastError(0);
827 return (len * 2) + 4;
829 default:
830 if (GetVersion() & 0x80000000) /* Windows 95/98 */
831 SetLastError(ERROR_FILE_NOT_FOUND);
832 else
833 SetLastError(ERROR_RESOURCE_DATA_NOT_FOUND);
834 return 0;
838 /******************************************************************************
839 * GetFileVersionInfoSizeExA (kernelbase.@)
841 DWORD WINAPI GetFileVersionInfoSizeExA( DWORD flags, LPCSTR filename, LPDWORD handle )
843 UNICODE_STRING filenameW;
844 DWORD retval;
846 TRACE("(0x%lx,%s,%p)\n", flags, debugstr_a(filename), handle );
848 if(filename)
849 RtlCreateUnicodeStringFromAsciiz(&filenameW, filename);
850 else
851 filenameW.Buffer = NULL;
853 retval = GetFileVersionInfoSizeExW(flags, filenameW.Buffer, handle);
855 RtlFreeUnicodeString(&filenameW);
857 return retval;
860 /***********************************************************************
861 * GetFileVersionInfoExW (kernelbase.@)
863 BOOL WINAPI GetFileVersionInfoExW( DWORD flags, LPCWSTR filename, DWORD ignored, DWORD datasize, LPVOID data )
865 static const char signature[4] = "FE2X";
866 DWORD len, offset, magic = 1;
867 HMODULE hModule;
868 VS_VERSION_INFO_STRUCT32* vvis = data;
870 TRACE("(0x%lx,%s,%ld,size=%ld,data=%p)\n",
871 flags, debugstr_w(filename), ignored, datasize, data );
873 if (!data)
875 SetLastError(ERROR_INVALID_DATA);
876 return FALSE;
878 if (flags & ~FILE_VER_GET_LOCALISED)
879 FIXME("flags 0x%lx ignored\n", flags & ~FILE_VER_GET_LOCALISED);
881 if ((hModule = LoadLibraryExW( filename, 0, LOAD_LIBRARY_AS_IMAGE_RESOURCE )))
883 HRSRC hRsrc = NULL;
884 if (!(flags & FILE_VER_GET_LOCALISED))
886 LANGID english = MAKELANGID( LANG_ENGLISH, SUBLANG_DEFAULT );
887 hRsrc = FindResourceExW( hModule, (LPWSTR)VS_FILE_INFO,
888 MAKEINTRESOURCEW(VS_VERSION_INFO), english );
890 if (!hRsrc)
891 hRsrc = FindResourceW( hModule, MAKEINTRESOURCEW(VS_VERSION_INFO),
892 (LPWSTR)VS_FILE_INFO );
893 if (hRsrc)
895 HGLOBAL hMem = LoadResource( hModule, hRsrc );
896 magic = IMAGE_NT_SIGNATURE;
897 len = min( SizeofResource(hModule, hRsrc), datasize );
898 memcpy( data, LockResource( hMem ), len );
899 FreeResource( hMem );
901 FreeLibrary( hModule );
903 else
905 HANDLE handle = CreateFileW( filename, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
906 NULL, OPEN_EXISTING, 0, 0 );
907 if (handle == INVALID_HANDLE_VALUE) return 0;
908 if ((magic = find_version_resource( handle, &len, &offset, flags )))
909 len = read_data( handle, offset, data, min( len, datasize ));
910 CloseHandle( handle );
913 switch (magic)
915 case IMAGE_OS2_SIGNATURE:
916 /* We have a 16bit resource. */
917 if (TRACE_ON(ver))
918 print_vffi_debug( (VS_FIXEDFILEINFO *)VersionInfo16_Value( (VS_VERSION_INFO_STRUCT16 *)data ));
919 SetLastError(0);
920 return TRUE;
922 case IMAGE_NT_SIGNATURE:
923 /* We have a 32bit resource.
925 * XP/W2K/W2K3 uses a buffer which is 2 times the actual needed space + 4 bytes "FE2X"
926 * This extra buffer is used for Unicode to ANSI conversions in A-Calls
928 len = vvis->wLength + sizeof(signature);
929 if (datasize >= len) memcpy( (char*)data + vvis->wLength, signature, sizeof(signature) );
930 if (TRACE_ON(ver))
931 print_vffi_debug( (VS_FIXEDFILEINFO *)VersionInfo32_Value( vvis ));
932 SetLastError(0);
933 return TRUE;
935 default:
936 SetLastError( ERROR_RESOURCE_DATA_NOT_FOUND );
937 return FALSE;
941 /***********************************************************************
942 * GetFileVersionInfoExA (kernelbase.@)
944 BOOL WINAPI GetFileVersionInfoExA( DWORD flags, LPCSTR filename, DWORD handle, DWORD datasize, LPVOID data )
946 UNICODE_STRING filenameW;
947 BOOL retval;
949 TRACE("(0x%lx,%s,%ld,size=%ld,data=%p)\n",
950 flags, debugstr_a(filename), handle, datasize, data );
952 if(filename)
953 RtlCreateUnicodeStringFromAsciiz(&filenameW, filename);
954 else
955 filenameW.Buffer = NULL;
957 retval = GetFileVersionInfoExW(flags, filenameW.Buffer, handle, datasize, data);
959 RtlFreeUnicodeString(&filenameW);
961 return retval;
964 /***********************************************************************
965 * GetFileVersionInfoW (kernelbase.@)
967 BOOL WINAPI GetFileVersionInfoW( LPCWSTR filename, DWORD handle, DWORD datasize, LPVOID data )
969 return GetFileVersionInfoExW(FILE_VER_GET_LOCALISED, filename, handle, datasize, data);
972 /***********************************************************************
973 * GetFileVersionInfoA (kernelbase.@)
975 BOOL WINAPI GetFileVersionInfoA( LPCSTR filename, DWORD handle, DWORD datasize, LPVOID data )
977 return GetFileVersionInfoExA(FILE_VER_GET_LOCALISED, filename, handle, datasize, data);
980 /***********************************************************************
981 * VersionInfo16_FindChild [internal]
983 static const VS_VERSION_INFO_STRUCT16 *VersionInfo16_FindChild( const VS_VERSION_INFO_STRUCT16 *info,
984 LPCSTR key, UINT len )
986 const VS_VERSION_INFO_STRUCT16 *child = VersionInfo16_Children( info );
988 while ((char *)child < (char *)info + info->wLength )
990 if (!strnicmp( child->szKey, key, len ) && !child->szKey[len])
991 return child;
993 if (!(child->wLength)) return NULL;
994 child = VersionInfo16_Next( child );
997 return NULL;
1000 /***********************************************************************
1001 * VersionInfo32_FindChild [internal]
1003 static const VS_VERSION_INFO_STRUCT32 *VersionInfo32_FindChild( const VS_VERSION_INFO_STRUCT32 *info,
1004 LPCWSTR key, UINT len )
1006 const VS_VERSION_INFO_STRUCT32 *child = VersionInfo32_Children( info );
1008 while ((char *)child < (char *)info + info->wLength )
1010 if (!wcsnicmp( child->szKey, key, len ) && !child->szKey[len])
1011 return child;
1013 if (!(child->wLength)) return NULL;
1014 child = VersionInfo32_Next( child );
1017 return NULL;
1020 /***********************************************************************
1021 * VersionInfo16_QueryValue [internal]
1023 * Gets a value from a 16-bit NE resource
1025 static BOOL VersionInfo16_QueryValue( const VS_VERSION_INFO_STRUCT16 *info, LPCSTR lpSubBlock,
1026 LPVOID *lplpBuffer, UINT *puLen )
1028 while ( *lpSubBlock )
1030 /* Find next path component */
1031 LPCSTR lpNextSlash;
1032 for ( lpNextSlash = lpSubBlock; *lpNextSlash; lpNextSlash++ )
1033 if ( *lpNextSlash == '\\' )
1034 break;
1036 /* Skip empty components */
1037 if ( lpNextSlash == lpSubBlock )
1039 lpSubBlock++;
1040 continue;
1043 /* We have a non-empty component: search info for key */
1044 info = VersionInfo16_FindChild( info, lpSubBlock, lpNextSlash-lpSubBlock );
1045 if ( !info )
1047 if (puLen) *puLen = 0 ;
1048 SetLastError( ERROR_RESOURCE_TYPE_NOT_FOUND );
1049 return FALSE;
1052 /* Skip path component */
1053 lpSubBlock = lpNextSlash;
1056 /* Return value */
1057 *lplpBuffer = VersionInfo16_Value( info );
1058 if (puLen)
1059 *puLen = info->wValueLength;
1061 return TRUE;
1064 /***********************************************************************
1065 * VersionInfo32_QueryValue [internal]
1067 * Gets a value from a 32-bit PE resource
1069 static BOOL VersionInfo32_QueryValue( const VS_VERSION_INFO_STRUCT32 *info, LPCWSTR lpSubBlock,
1070 LPVOID *lplpBuffer, UINT *puLen, BOOL *pbText )
1072 TRACE("lpSubBlock : (%s)\n", debugstr_w(lpSubBlock));
1074 while ( *lpSubBlock )
1076 /* Find next path component */
1077 LPCWSTR lpNextSlash;
1078 for ( lpNextSlash = lpSubBlock; *lpNextSlash; lpNextSlash++ )
1079 if ( *lpNextSlash == '\\' )
1080 break;
1082 /* Skip empty components */
1083 if ( lpNextSlash == lpSubBlock )
1085 lpSubBlock++;
1086 continue;
1089 /* We have a non-empty component: search info for key */
1090 info = VersionInfo32_FindChild( info, lpSubBlock, lpNextSlash-lpSubBlock );
1091 if ( !info )
1093 if (puLen) *puLen = 0 ;
1094 SetLastError( ERROR_RESOURCE_TYPE_NOT_FOUND );
1095 return FALSE;
1098 /* Skip path component */
1099 lpSubBlock = lpNextSlash;
1102 /* Return value */
1103 *lplpBuffer = VersionInfo32_Value( info );
1104 if (puLen)
1105 *puLen = info->wValueLength;
1106 if (pbText)
1107 *pbText = info->wType;
1109 return TRUE;
1112 /***********************************************************************
1113 * VerQueryValueA (kernelbase.@)
1115 BOOL WINAPI VerQueryValueA( LPCVOID pBlock, LPCSTR lpSubBlock,
1116 LPVOID *lplpBuffer, PUINT puLen )
1118 static const char rootA[] = "\\";
1119 const VS_VERSION_INFO_STRUCT16 *info = pBlock;
1121 TRACE("(%p,%s,%p,%p)\n",
1122 pBlock, debugstr_a(lpSubBlock), lplpBuffer, puLen );
1124 if (!pBlock)
1125 return FALSE;
1127 if (lpSubBlock == NULL || lpSubBlock[0] == '\0')
1128 lpSubBlock = rootA;
1130 if ( !VersionInfoIs16( info ) )
1132 BOOL ret, isText;
1133 INT len;
1134 LPWSTR lpSubBlockW;
1135 UINT value_len;
1137 len = MultiByteToWideChar(CP_ACP, 0, lpSubBlock, -1, NULL, 0);
1138 lpSubBlockW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1140 if (!lpSubBlockW)
1141 return FALSE;
1143 MultiByteToWideChar(CP_ACP, 0, lpSubBlock, -1, lpSubBlockW, len);
1145 ret = VersionInfo32_QueryValue(pBlock, lpSubBlockW, lplpBuffer, &value_len, &isText);
1146 if (puLen) *puLen = value_len;
1148 HeapFree(GetProcessHeap(), 0, lpSubBlockW);
1150 if (ret && isText)
1152 /* Set lpBuffer so it points to the 'empty' area where we store
1153 * the converted strings
1155 LPSTR lpBufferA = (LPSTR)pBlock + info->wLength + 4;
1156 DWORD pos = (LPCSTR)*lplpBuffer - (LPCSTR)pBlock;
1157 len = WideCharToMultiByte(CP_ACP, 0, *lplpBuffer, value_len,
1158 lpBufferA + pos, info->wLength - pos, NULL, NULL);
1159 *lplpBuffer = lpBufferA + pos;
1160 if (puLen) *puLen = len;
1162 return ret;
1165 return VersionInfo16_QueryValue(info, lpSubBlock, lplpBuffer, puLen);
1168 /***********************************************************************
1169 * VerQueryValueW (kernelbase.@)
1171 BOOL WINAPI VerQueryValueW( LPCVOID pBlock, LPCWSTR lpSubBlock,
1172 LPVOID *lplpBuffer, PUINT puLen )
1174 const VS_VERSION_INFO_STRUCT32 *info = pBlock;
1176 TRACE("(%p,%s,%p,%p)\n",
1177 pBlock, debugstr_w(lpSubBlock), lplpBuffer, puLen );
1179 if (!pBlock)
1180 return FALSE;
1182 if (!lpSubBlock || !lpSubBlock[0])
1183 lpSubBlock = L"\\";
1185 if ( VersionInfoIs16( info ) )
1187 BOOL ret;
1188 int len;
1189 LPSTR lpSubBlockA;
1191 len = WideCharToMultiByte(CP_ACP, 0, lpSubBlock, -1, NULL, 0, NULL, NULL);
1192 lpSubBlockA = HeapAlloc(GetProcessHeap(), 0, len * sizeof(char));
1194 if (!lpSubBlockA)
1195 return FALSE;
1197 WideCharToMultiByte(CP_ACP, 0, lpSubBlock, -1, lpSubBlockA, len, NULL, NULL);
1199 ret = VersionInfo16_QueryValue(pBlock, lpSubBlockA, lplpBuffer, puLen);
1201 HeapFree(GetProcessHeap(), 0, lpSubBlockA);
1203 if (ret && wcscmp( lpSubBlock, L"\\" ) && wcsicmp( lpSubBlock, L"\\VarFileInfo\\Translation" ))
1205 /* Set lpBuffer so it points to the 'empty' area where we store
1206 * the converted strings
1208 LPWSTR lpBufferW = (LPWSTR)((LPSTR)pBlock + info->wLength);
1209 DWORD pos = (LPCSTR)*lplpBuffer - (LPCSTR)pBlock;
1210 DWORD max = (info->wLength - sizeof(VS_FIXEDFILEINFO)) * 4 - info->wLength;
1212 len = MultiByteToWideChar(CP_ACP, 0, *lplpBuffer, -1,
1213 lpBufferW + pos, max/sizeof(WCHAR) - pos );
1214 *lplpBuffer = lpBufferW + pos;
1215 if (puLen) *puLen = len;
1217 return ret;
1220 return VersionInfo32_QueryValue(info, lpSubBlock, lplpBuffer, puLen, NULL);
1224 /******************************************************************************
1225 * file_existsA
1227 static BOOL file_existsA( char const * path, char const * file, BOOL excl )
1229 DWORD sharing = excl ? 0 : FILE_SHARE_READ | FILE_SHARE_WRITE;
1230 char filename[MAX_PATH];
1231 int len;
1232 HANDLE handle;
1234 if (path)
1236 strcpy( filename, path );
1237 len = strlen(filename);
1238 if (len && filename[len - 1] != '\\') strcat( filename, "\\" );
1239 strcat( filename, file );
1241 else if (!SearchPathA( NULL, file, NULL, MAX_PATH, filename, NULL )) return FALSE;
1243 handle = CreateFileA( filename, 0, sharing, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0 );
1244 if (handle == INVALID_HANDLE_VALUE) return FALSE;
1245 CloseHandle( handle );
1246 return TRUE;
1249 /******************************************************************************
1250 * file_existsW
1252 static BOOL file_existsW( const WCHAR *path, const WCHAR *file, BOOL excl )
1254 DWORD sharing = excl ? 0 : FILE_SHARE_READ | FILE_SHARE_WRITE;
1255 WCHAR filename[MAX_PATH];
1256 int len;
1257 HANDLE handle;
1259 if (path)
1261 lstrcpyW( filename, path );
1262 len = lstrlenW(filename);
1263 if (len && filename[len - 1] != '\\') lstrcatW( filename, L"\\" );
1264 lstrcatW( filename, file );
1266 else if (!SearchPathW( NULL, file, NULL, MAX_PATH, filename, NULL )) return FALSE;
1268 handle = CreateFileW( filename, 0, sharing, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0 );
1269 if (handle == INVALID_HANDLE_VALUE) return FALSE;
1270 CloseHandle( handle );
1271 return TRUE;
1274 /*****************************************************************************
1275 * VerFindFileA (kernelbase.@)
1277 * Determines where to install a file based on whether it locates another
1278 * version of the file in the system. The values VerFindFile returns are
1279 * used in a subsequent call to the VerInstallFile function.
1281 DWORD WINAPI VerFindFileA( DWORD flags, LPCSTR filename, LPCSTR win_dir, LPCSTR app_dir,
1282 LPSTR cur_dir, PUINT curdir_len, LPSTR dest, PUINT dest_len )
1284 DWORD retval = 0;
1285 const char *curDir;
1286 const char *destDir;
1287 char winDir[MAX_PATH], systemDir[MAX_PATH];
1289 TRACE("flags = %lx filename=%s windir=%s appdir=%s curdirlen=%p(%u) destdirlen=%p(%u)\n",
1290 flags, debugstr_a(filename), debugstr_a(win_dir), debugstr_a(app_dir),
1291 curdir_len, curdir_len ? *curdir_len : 0, dest_len, dest_len ? *dest_len : 0 );
1293 /* Figure out where the file should go; shared files default to the
1294 system directory */
1296 GetSystemDirectoryA(systemDir, sizeof(systemDir));
1297 curDir = "";
1299 if(flags & VFFF_ISSHAREDFILE)
1301 destDir = systemDir;
1302 /* Were we given a filename? If so, try to find the file. */
1303 if(filename)
1305 if(file_existsA(destDir, filename, FALSE)) curDir = destDir;
1306 else if(app_dir && file_existsA(app_dir, filename, FALSE))
1307 curDir = app_dir;
1309 if(!file_existsA(systemDir, filename, FALSE))
1310 retval |= VFF_CURNEDEST;
1313 else /* not a shared file */
1315 destDir = app_dir ? app_dir : "";
1316 if(filename)
1318 GetWindowsDirectoryA( winDir, MAX_PATH );
1319 if(file_existsA(destDir, filename, FALSE)) curDir = destDir;
1320 else if(file_existsA(winDir, filename, FALSE))
1321 curDir = winDir;
1322 else if(file_existsA(systemDir, filename, FALSE))
1323 curDir = systemDir;
1325 if (app_dir && app_dir[0])
1327 if(!file_existsA(app_dir, filename, FALSE))
1328 retval |= VFF_CURNEDEST;
1330 else if(file_existsA(NULL, filename, FALSE))
1331 retval |= VFF_CURNEDEST;
1335 /* Check to see if the file exists and is in use by another application */
1336 if (filename && file_existsA(curDir, filename, FALSE))
1338 if (filename && !file_existsA(curDir, filename, TRUE))
1339 retval |= VFF_FILEINUSE;
1342 if (dest_len && dest)
1344 UINT len = strlen(destDir) + 1;
1345 if (*dest_len < len) retval |= VFF_BUFFTOOSMALL;
1346 lstrcpynA(dest, destDir, *dest_len);
1347 *dest_len = len;
1349 if (curdir_len && cur_dir)
1351 UINT len = strlen(curDir) + 1;
1352 if (*curdir_len < len) retval |= VFF_BUFFTOOSMALL;
1353 lstrcpynA(cur_dir, curDir, *curdir_len);
1354 *curdir_len = len;
1357 TRACE("ret = %lu (%s%s%s) curdir=%s destdir=%s\n", retval,
1358 (retval & VFF_CURNEDEST) ? "VFF_CURNEDEST " : "",
1359 (retval & VFF_FILEINUSE) ? "VFF_FILEINUSE " : "",
1360 (retval & VFF_BUFFTOOSMALL) ? "VFF_BUFFTOOSMALL " : "",
1361 debugstr_a(cur_dir), debugstr_a(dest));
1363 return retval;
1366 /*****************************************************************************
1367 * VerFindFileW (kernelbase.@)
1369 DWORD WINAPI VerFindFileW( DWORD flags, LPCWSTR filename, LPCWSTR win_dir, LPCWSTR app_dir,
1370 LPWSTR cur_dir, PUINT curdir_len, LPWSTR dest, PUINT dest_len )
1372 DWORD retval = 0;
1373 const WCHAR *curDir;
1374 const WCHAR *destDir;
1376 TRACE("flags = %lx filename=%s windir=%s appdir=%s curdirlen=%p(%u) destdirlen=%p(%u)\n",
1377 flags, debugstr_w(filename), debugstr_w(win_dir), debugstr_w(app_dir),
1378 curdir_len, curdir_len ? *curdir_len : 0, dest_len, dest_len ? *dest_len : 0 );
1380 /* Figure out where the file should go; shared files default to the
1381 system directory */
1383 curDir = L"";
1385 if(flags & VFFF_ISSHAREDFILE)
1387 destDir = system_dir;
1388 /* Were we given a filename? If so, try to find the file. */
1389 if(filename)
1391 if(file_existsW(destDir, filename, FALSE)) curDir = destDir;
1392 else if(app_dir && file_existsW(app_dir, filename, FALSE))
1394 curDir = app_dir;
1395 retval |= VFF_CURNEDEST;
1399 else /* not a shared file */
1401 destDir = app_dir ? app_dir : L"";
1402 if(filename)
1404 if(file_existsW(destDir, filename, FALSE)) curDir = destDir;
1405 else if(file_existsW(windows_dir, filename, FALSE))
1407 curDir = windows_dir;
1408 retval |= VFF_CURNEDEST;
1410 else if (file_existsW(system_dir, filename, FALSE))
1412 curDir = system_dir;
1413 retval |= VFF_CURNEDEST;
1418 if (filename && !file_existsW(curDir, filename, TRUE))
1419 retval |= VFF_FILEINUSE;
1421 if (dest_len && dest)
1423 UINT len = lstrlenW(destDir) + 1;
1424 if (*dest_len < len) retval |= VFF_BUFFTOOSMALL;
1425 lstrcpynW(dest, destDir, *dest_len);
1426 *dest_len = len;
1428 if (curdir_len && cur_dir)
1430 UINT len = lstrlenW(curDir) + 1;
1431 if (*curdir_len < len) retval |= VFF_BUFFTOOSMALL;
1432 lstrcpynW(cur_dir, curDir, *curdir_len);
1433 *curdir_len = len;
1436 TRACE("ret = %lu (%s%s%s) curdir=%s destdir=%s\n", retval,
1437 (retval & VFF_CURNEDEST) ? "VFF_CURNEDEST " : "",
1438 (retval & VFF_FILEINUSE) ? "VFF_FILEINUSE " : "",
1439 (retval & VFF_BUFFTOOSMALL) ? "VFF_BUFFTOOSMALL " : "",
1440 debugstr_w(cur_dir), debugstr_w(dest));
1441 return retval;
1445 /***********************************************************************
1446 * GetProductInfo (kernelbase.@)
1448 BOOL WINAPI DECLSPEC_HOTPATCH GetProductInfo( DWORD os_major, DWORD os_minor,
1449 DWORD sp_major, DWORD sp_minor, DWORD *type )
1451 return RtlGetProductInfo( os_major, os_minor, sp_major, sp_minor, type );
1455 /***********************************************************************
1456 * GetVersion (kernelbase.@)
1458 DWORD WINAPI GetVersion(void)
1460 OSVERSIONINFOEXW info;
1461 DWORD result;
1463 info.dwOSVersionInfoSize = sizeof(info);
1464 if (!GetVersionExW( (OSVERSIONINFOW *)&info )) return 0;
1466 result = MAKELONG( MAKEWORD( info.dwMajorVersion, info.dwMinorVersion ),
1467 (info.dwPlatformId ^ 2) << 14 );
1469 if (info.dwPlatformId == VER_PLATFORM_WIN32_NT)
1470 result |= LOWORD(info.dwBuildNumber) << 16;
1471 return result;
1475 /***********************************************************************
1476 * GetVersionExA (kernelbase.@)
1478 BOOL WINAPI GetVersionExA( OSVERSIONINFOA *info )
1480 OSVERSIONINFOEXW infoW;
1482 if (info->dwOSVersionInfoSize != sizeof(OSVERSIONINFOA) &&
1483 info->dwOSVersionInfoSize != sizeof(OSVERSIONINFOEXA))
1485 WARN( "wrong OSVERSIONINFO size from app (got: %ld)\n", info->dwOSVersionInfoSize );
1486 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1487 return FALSE;
1490 infoW.dwOSVersionInfoSize = sizeof(infoW);
1491 if (!GetVersionExW( (OSVERSIONINFOW *)&infoW )) return FALSE;
1493 info->dwMajorVersion = infoW.dwMajorVersion;
1494 info->dwMinorVersion = infoW.dwMinorVersion;
1495 info->dwBuildNumber = infoW.dwBuildNumber;
1496 info->dwPlatformId = infoW.dwPlatformId;
1497 WideCharToMultiByte( CP_ACP, 0, infoW.szCSDVersion, -1,
1498 info->szCSDVersion, sizeof(info->szCSDVersion), NULL, NULL );
1500 if (info->dwOSVersionInfoSize == sizeof(OSVERSIONINFOEXA))
1502 OSVERSIONINFOEXA *vex = (OSVERSIONINFOEXA *)info;
1503 vex->wServicePackMajor = infoW.wServicePackMajor;
1504 vex->wServicePackMinor = infoW.wServicePackMinor;
1505 vex->wSuiteMask = infoW.wSuiteMask;
1506 vex->wProductType = infoW.wProductType;
1508 return TRUE;
1512 /***********************************************************************
1513 * GetVersionExW (kernelbase.@)
1515 BOOL WINAPI GetVersionExW( OSVERSIONINFOW *info )
1517 static INIT_ONCE init_once = INIT_ONCE_STATIC_INIT;
1519 if (info->dwOSVersionInfoSize != sizeof(OSVERSIONINFOW) &&
1520 info->dwOSVersionInfoSize != sizeof(OSVERSIONINFOEXW))
1522 WARN( "wrong OSVERSIONINFO size from app (got: %ld)\n", info->dwOSVersionInfoSize );
1523 return FALSE;
1526 if (!InitOnceExecuteOnce(&init_once, init_current_version, NULL, NULL)) return FALSE;
1528 info->dwMajorVersion = current_version.dwMajorVersion;
1529 info->dwMinorVersion = current_version.dwMinorVersion;
1530 info->dwBuildNumber = current_version.dwBuildNumber;
1531 info->dwPlatformId = current_version.dwPlatformId;
1532 wcscpy( info->szCSDVersion, current_version.szCSDVersion );
1534 if (info->dwOSVersionInfoSize == sizeof(OSVERSIONINFOEXW))
1536 OSVERSIONINFOEXW *vex = (OSVERSIONINFOEXW *)info;
1537 vex->wServicePackMajor = current_version.wServicePackMajor;
1538 vex->wServicePackMinor = current_version.wServicePackMinor;
1539 vex->wSuiteMask = current_version.wSuiteMask;
1540 vex->wProductType = current_version.wProductType;
1542 return TRUE;
1546 /***********************************************************************
1547 * GetCurrentPackageFamilyName (kernelbase.@)
1549 LONG WINAPI /* DECLSPEC_HOTPATCH */ GetCurrentPackageFamilyName( UINT32 *length, WCHAR *name )
1551 FIXME( "(%p %p): stub\n", length, name );
1552 return APPMODEL_ERROR_NO_PACKAGE;
1556 /***********************************************************************
1557 * GetCurrentPackageFullName (kernelbase.@)
1559 LONG WINAPI /* DECLSPEC_HOTPATCH */ GetCurrentPackageFullName( UINT32 *length, WCHAR *name )
1561 FIXME( "(%p %p): stub\n", length, name );
1562 return APPMODEL_ERROR_NO_PACKAGE;
1566 /***********************************************************************
1567 * GetCurrentPackageId (kernelbase.@)
1569 LONG WINAPI /* DECLSPEC_HOTPATCH */ GetCurrentPackageId( UINT32 *len, BYTE *buffer )
1571 FIXME( "(%p %p): stub\n", len, buffer );
1572 return APPMODEL_ERROR_NO_PACKAGE;
1576 /***********************************************************************
1577 * GetCurrentPackagePath (kernelbase.@)
1579 LONG WINAPI /* DECLSPEC_HOTPATCH */ GetCurrentPackagePath( UINT32 *length, WCHAR *path )
1581 FIXME( "(%p %p): stub\n", length, path );
1582 return APPMODEL_ERROR_NO_PACKAGE;
1586 /***********************************************************************
1587 * GetPackageFullName (kernelbase.@)
1589 LONG WINAPI /* DECLSPEC_HOTPATCH */ GetPackageFullName( HANDLE process, UINT32 *length, WCHAR *name )
1591 FIXME( "(%p %p %p): stub\n", process, length, name );
1592 return APPMODEL_ERROR_NO_PACKAGE;
1596 /***********************************************************************
1597 * GetPackageFamilyName (kernelbase.@)
1599 LONG WINAPI /* DECLSPEC_HOTPATCH */ GetPackageFamilyName( HANDLE process, UINT32 *length, WCHAR *name )
1601 FIXME( "(%p %p %p): stub\n", process, length, name );
1602 return APPMODEL_ERROR_NO_PACKAGE;
1605 /***********************************************************************
1606 * GetPackagesByPackageFamily (kernelbase.@)
1608 LONG WINAPI DECLSPEC_HOTPATCH GetPackagesByPackageFamily(const WCHAR *family_name, UINT32 *count,
1609 WCHAR *full_names, UINT32 *buffer_len, WCHAR *buffer)
1611 FIXME( "(%s %p %p %p %p): stub\n", debugstr_w(family_name), count, full_names, buffer_len, buffer );
1613 if (!count || !buffer_len)
1614 return ERROR_INVALID_PARAMETER;
1616 *count = 0;
1617 *buffer_len = 0;
1618 return ERROR_SUCCESS;
1621 /***********************************************************************
1622 * GetPackagePathByFullName (kernelbase.@)
1624 LONG WINAPI GetPackagePathByFullName(const WCHAR *name, UINT32 *len, WCHAR *path)
1626 if (!len || !name)
1627 return ERROR_INVALID_PARAMETER;
1629 FIXME( "(%s %p %p): stub\n", debugstr_w(name), len, path );
1631 return APPMODEL_ERROR_NO_PACKAGE;
1634 static const struct
1636 UINT32 code;
1637 const WCHAR *name;
1639 arch_names[] =
1641 {PROCESSOR_ARCHITECTURE_INTEL, L"x86"},
1642 {PROCESSOR_ARCHITECTURE_ARM, L"arm"},
1643 {PROCESSOR_ARCHITECTURE_AMD64, L"x64"},
1644 {PROCESSOR_ARCHITECTURE_NEUTRAL, L"neutral"},
1645 {PROCESSOR_ARCHITECTURE_ARM64, L"arm64"},
1646 {PROCESSOR_ARCHITECTURE_UNKNOWN, L"unknown"},
1649 static UINT32 processor_arch_from_string(const WCHAR *str, unsigned int len)
1651 unsigned int i;
1653 for (i = 0; i < ARRAY_SIZE(arch_names); ++i)
1654 if (lstrlenW(arch_names[i].name) == len && !wcsnicmp(str, arch_names[i].name, len))
1655 return arch_names[i].code;
1656 return ~0u;
1659 /***********************************************************************
1660 * PackageIdFromFullName (kernelbase.@)
1662 LONG WINAPI PackageIdFromFullName(const WCHAR *full_name, UINT32 flags, UINT32 *buffer_length, BYTE *buffer)
1664 const WCHAR *name, *version_str, *arch_str, *resource_id, *publisher_id, *s;
1665 PACKAGE_ID *id = (PACKAGE_ID *)buffer;
1666 UINT32 size, buffer_size, len;
1668 TRACE("full_name %s, flags %#x, buffer_length %p, buffer %p.\n",
1669 debugstr_w(full_name), flags, buffer_length, buffer);
1671 if (flags)
1672 FIXME("Flags %#x are not supported.\n", flags);
1674 if (!full_name || !buffer_length)
1675 return ERROR_INVALID_PARAMETER;
1677 if (!buffer && *buffer_length)
1678 return ERROR_INVALID_PARAMETER;
1680 name = full_name;
1681 if (!(version_str = wcschr(name, L'_')))
1682 return ERROR_INVALID_PARAMETER;
1683 ++version_str;
1685 if (!(arch_str = wcschr(version_str, L'_')))
1686 return ERROR_INVALID_PARAMETER;
1687 ++arch_str;
1689 if (!(resource_id = wcschr(arch_str, L'_')))
1690 return ERROR_INVALID_PARAMETER;
1691 ++resource_id;
1693 if (!(publisher_id = wcschr(resource_id, L'_')))
1694 return ERROR_INVALID_PARAMETER;
1695 ++publisher_id;
1697 /* Publisher id length should be 13. */
1698 size = sizeof(*id) + sizeof(WCHAR) * ((version_str - name) + (publisher_id - resource_id) + 13 + 1);
1699 buffer_size = *buffer_length;
1700 *buffer_length = size;
1701 if (buffer_size < size)
1702 return ERROR_INSUFFICIENT_BUFFER;
1704 memset(id, 0, sizeof(*id));
1705 if ((id->processorArchitecture = processor_arch_from_string(arch_str, resource_id - arch_str - 1)) == ~0u)
1707 FIXME("Unrecognized arch %s.\n", debugstr_w(arch_str));
1708 return ERROR_INVALID_PARAMETER;
1710 buffer += sizeof(*id);
1712 id->version.Major = wcstol(version_str, NULL, 10);
1713 if (!(s = wcschr(version_str, L'.')))
1714 return ERROR_INVALID_PARAMETER;
1715 ++s;
1716 id->version.Minor = wcstol(s, NULL, 10);
1717 if (!(s = wcschr(s, L'.')))
1718 return ERROR_INVALID_PARAMETER;
1719 ++s;
1720 id->version.Build = wcstol(s, NULL, 10);
1721 if (!(s = wcschr(s, L'.')))
1722 return ERROR_INVALID_PARAMETER;
1723 ++s;
1724 id->version.Revision = wcstol(s, NULL, 10);
1726 id->name = (WCHAR *)buffer;
1727 len = version_str - name - 1;
1728 memcpy(id->name, name, sizeof(*id->name) * len);
1729 id->name[len] = 0;
1730 buffer += sizeof(*id->name) * (len + 1);
1732 id->resourceId = (WCHAR *)buffer;
1733 len = publisher_id - resource_id - 1;
1734 memcpy(id->resourceId, resource_id, sizeof(*id->resourceId) * len);
1735 id->resourceId[len] = 0;
1736 buffer += sizeof(*id->resourceId) * (len + 1);
1738 id->publisherId = (WCHAR *)buffer;
1739 len = lstrlenW(publisher_id);
1740 if (len != 13)
1741 return ERROR_INVALID_PARAMETER;
1742 memcpy(id->publisherId, publisher_id, sizeof(*id->publisherId) * len);
1743 id->publisherId[len] = 0;
1745 return ERROR_SUCCESS;