kernelbase: Fix error value for PE files without resource.
[wine.git] / dlls / kernelbase / version.c
blob4d5a8a4de934245656e97d906cbb7ef253f56a56
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"
42 #include "appmodel.h"
44 #include "kernelbase.h"
45 #include "wine/debug.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(ver);
49 typedef struct
51 WORD offset;
52 WORD length;
53 WORD flags;
54 WORD id;
55 WORD handle;
56 WORD usage;
57 } NE_NAMEINFO;
59 typedef struct
61 WORD type_id;
62 WORD count;
63 DWORD resloader;
64 } NE_TYPEINFO;
66 struct version_info
68 DWORD major;
69 DWORD minor;
70 DWORD build;
73 /***********************************************************************
74 * Version Info Structure
77 typedef struct
79 WORD wLength;
80 WORD wValueLength;
81 CHAR szKey[1];
82 #if 0 /* variable length structure */
83 /* DWORD aligned */
84 BYTE Value[];
85 /* DWORD aligned */
86 VS_VERSION_INFO_STRUCT16 Children[];
87 #endif
88 } VS_VERSION_INFO_STRUCT16;
90 typedef struct
92 WORD wLength;
93 WORD wValueLength;
94 WORD wType; /* 1:Text, 0:Binary */
95 WCHAR szKey[1];
96 #if 0 /* variable length structure */
97 /* DWORD aligned */
98 BYTE Value[];
99 /* DWORD aligned */
100 VS_VERSION_INFO_STRUCT32 Children[];
101 #endif
102 } VS_VERSION_INFO_STRUCT32;
104 #define VersionInfoIs16( ver ) \
105 ( ((const VS_VERSION_INFO_STRUCT16 *)ver)->szKey[0] >= ' ' )
107 #define DWORD_ALIGN( base, ptr ) \
108 ( (LPBYTE)(base) + ((((LPBYTE)(ptr) - (LPBYTE)(base)) + 3) & ~3) )
110 #define VersionInfo16_Value( ver ) \
111 DWORD_ALIGN( (ver), (ver)->szKey + strlen((ver)->szKey) + 1 )
112 #define VersionInfo32_Value( ver ) \
113 DWORD_ALIGN( (ver), (ver)->szKey + lstrlenW((ver)->szKey) + 1 )
115 #define VersionInfo16_Children( ver ) \
116 (const VS_VERSION_INFO_STRUCT16 *)( VersionInfo16_Value( ver ) + \
117 ( ( (ver)->wValueLength + 3 ) & ~3 ) )
118 #define VersionInfo32_Children( ver ) \
119 (const VS_VERSION_INFO_STRUCT32 *)( VersionInfo32_Value( ver ) + \
120 ( ( (ver)->wValueLength * \
121 ((ver)->wType? 2 : 1) + 3 ) & ~3 ) )
123 #define VersionInfo16_Next( ver ) \
124 (VS_VERSION_INFO_STRUCT16 *)( (LPBYTE)ver + (((ver)->wLength + 3) & ~3) )
125 #define VersionInfo32_Next( ver ) \
126 (VS_VERSION_INFO_STRUCT32 *)( (LPBYTE)ver + (((ver)->wLength + 3) & ~3) )
129 /***********************************************************************
130 * Win8 info, reported if the app doesn't provide compat GUID in the manifest and
131 * doesn't have higher OS version in PE header.
133 static const struct version_info windows8_version_info = { 6, 2, 9200 };
135 /***********************************************************************
136 * Win8.1 info, reported if the app doesn't provide compat GUID in the manifest and
137 * OS version in PE header is 8.1 or higher but below 10.
139 static const struct version_info windows8_1_version_info = { 6, 3, 9600 };
142 /***********************************************************************
143 * Windows versions that need compatibility GUID specified in manifest
144 * in order to be reported by the APIs.
146 static const struct
148 struct version_info info;
149 GUID guid;
150 } version_data[] =
152 /* Windows 8.1 */
154 { 6, 3, 9600 },
155 {0x1f676c76,0x80e1,0x4239,{0x95,0xbb,0x83,0xd0,0xf6,0xd0,0xda,0x78}}
157 /* Windows 10 */
159 { 10, 0, 18362 },
160 {0x8e0f7a12,0xbfb3,0x4fe8,{0xb9,0xa5,0x48,0xfd,0x50,0xa1,0x5a,0x9a}}
165 /******************************************************************************
166 * init_current_version
168 * Initialize the current_version variable.
170 * For compatibility, Windows 8.1 and later report Win8 version unless the app
171 * has a manifest or higher OS version in the PE optional header
172 * that confirms its compatibility with newer versions of Windows.
175 static RTL_OSVERSIONINFOEXW current_version;
177 static BOOL CALLBACK init_current_version(PINIT_ONCE init_once, PVOID parameter, PVOID *context)
179 struct acci
181 DWORD ElementCount;
182 COMPATIBILITY_CONTEXT_ELEMENT Elements[1];
183 } *acci;
184 BOOL have_os_compat_elements = FALSE;
185 const struct version_info *ver;
186 IMAGE_NT_HEADERS *nt;
187 SIZE_T req;
188 int idx;
190 current_version.dwOSVersionInfoSize = sizeof(current_version);
191 if (!set_ntstatus( RtlGetVersion(&current_version) )) return FALSE;
193 for (idx = ARRAY_SIZE(version_data); idx--;)
194 if ( current_version.dwMajorVersion > version_data[idx].info.major ||
195 (current_version.dwMajorVersion == version_data[idx].info.major &&
196 current_version.dwMinorVersion >= version_data[idx].info.minor))
197 break;
199 if (idx < 0) return TRUE;
200 ver = &windows8_version_info;
202 if (RtlQueryInformationActivationContext(0, NtCurrentTeb()->Peb->ActivationContextData, NULL,
203 CompatibilityInformationInActivationContext, NULL, 0, &req) != STATUS_BUFFER_TOO_SMALL
204 || !req)
205 goto done;
207 if (!(acci = HeapAlloc(GetProcessHeap(), 0, req)))
209 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
210 return FALSE;
213 if (RtlQueryInformationActivationContext(0, NtCurrentTeb()->Peb->ActivationContextData, NULL,
214 CompatibilityInformationInActivationContext, acci, req, &req) == STATUS_SUCCESS)
218 DWORD i;
220 for (i = 0; i < acci->ElementCount; i++)
222 if (acci->Elements[i].Type != ACTCTX_COMPATIBILITY_ELEMENT_TYPE_OS)
223 continue;
225 have_os_compat_elements = TRUE;
227 if (IsEqualGUID(&acci->Elements[i].Id, &version_data[idx].guid))
229 ver = &version_data[idx].info;
231 if (ver->major == current_version.dwMajorVersion &&
232 ver->minor == current_version.dwMinorVersion)
233 ver = NULL;
235 idx = 0; /* break from outer loop */
236 break;
239 } while (idx--);
241 HeapFree(GetProcessHeap(), 0, acci);
243 done:
244 if (!have_os_compat_elements && current_version.dwMajorVersion >= 10
245 && (nt = RtlImageNtHeader(NtCurrentTeb()->Peb->ImageBaseAddress))
246 && (nt->OptionalHeader.MajorOperatingSystemVersion > 6
247 || (nt->OptionalHeader.MajorOperatingSystemVersion == 6
248 && nt->OptionalHeader.MinorOperatingSystemVersion >= 3)))
250 if (current_version.dwMajorVersion > 10)
251 FIXME("Unsupported current_version.dwMajorVersion %lu.\n", current_version.dwMajorVersion);
253 ver = nt->OptionalHeader.MajorOperatingSystemVersion >= 10 ? NULL : &windows8_1_version_info;
256 if (ver)
258 current_version.dwMajorVersion = ver->major;
259 current_version.dwMinorVersion = ver->minor;
260 current_version.dwBuildNumber = ver->build;
262 return TRUE;
266 /**********************************************************************
267 * find_entry_by_id
269 * Find an entry by id in a resource directory
270 * Copied from loader/pe_resource.c
272 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_id( const IMAGE_RESOURCE_DIRECTORY *dir,
273 WORD id, const void *root,
274 DWORD root_size )
276 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
277 int min, max, pos;
279 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
280 min = dir->NumberOfNamedEntries;
281 max = min + dir->NumberOfIdEntries - 1;
283 if (max >= (root_size - ((INT_PTR)dir - (INT_PTR)root) - sizeof(*dir)) / sizeof(*entry))
284 return NULL;
286 while (min <= max)
288 pos = (min + max) / 2;
289 if (entry[pos].u.Id == id)
291 DWORD offset = entry[pos].u2.s2.OffsetToDirectory;
292 if (offset > root_size - sizeof(*dir)) return NULL;
293 return (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + offset);
295 if (entry[pos].u.Id > id) max = pos - 1;
296 else min = pos + 1;
298 return NULL;
302 /**********************************************************************
303 * find_entry_default
305 * Find a default entry in a resource directory
306 * Copied from loader/pe_resource.c
308 static const IMAGE_RESOURCE_DIRECTORY *find_entry_default( const IMAGE_RESOURCE_DIRECTORY *dir,
309 const void *root )
311 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
313 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
314 return (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry->u2.s2.OffsetToDirectory);
318 /**********************************************************************
319 * push_language
321 * push a language onto the list of languages to try
323 static inline int push_language( WORD *list, int pos, WORD lang )
325 int i;
326 for (i = 0; i < pos; i++) if (list[i] == lang) return pos;
327 list[pos++] = lang;
328 return pos;
332 /**********************************************************************
333 * find_entry_language
335 static const IMAGE_RESOURCE_DIRECTORY *find_entry_language( const IMAGE_RESOURCE_DIRECTORY *dir,
336 const void *root, DWORD root_size,
337 DWORD flags )
339 const IMAGE_RESOURCE_DIRECTORY *ret;
340 WORD list[9];
341 int i, pos = 0;
343 if (flags & FILE_VER_GET_LOCALISED)
345 /* cf. LdrFindResource_U */
346 pos = push_language( list, pos, MAKELANGID( LANG_NEUTRAL, SUBLANG_NEUTRAL ) );
347 pos = push_language( list, pos, LANGIDFROMLCID( NtCurrentTeb()->CurrentLocale ) );
348 pos = push_language( list, pos, GetUserDefaultLangID() );
349 pos = push_language( list, pos, MAKELANGID( PRIMARYLANGID(GetUserDefaultLangID()), SUBLANG_NEUTRAL ));
350 pos = push_language( list, pos, MAKELANGID( PRIMARYLANGID(GetUserDefaultLangID()), SUBLANG_DEFAULT ));
351 pos = push_language( list, pos, GetSystemDefaultLangID() );
352 pos = push_language( list, pos, MAKELANGID( PRIMARYLANGID(GetSystemDefaultLangID()), SUBLANG_NEUTRAL ));
353 pos = push_language( list, pos, MAKELANGID( PRIMARYLANGID(GetSystemDefaultLangID()), SUBLANG_DEFAULT ));
354 pos = push_language( list, pos, MAKELANGID( LANG_ENGLISH, SUBLANG_DEFAULT ) );
356 else
358 /* FIXME: resolve LN file here */
359 pos = push_language( list, pos, MAKELANGID( LANG_ENGLISH, SUBLANG_DEFAULT ) );
362 for (i = 0; i < pos; i++) if ((ret = find_entry_by_id( dir, list[i], root, root_size ))) return ret;
363 return find_entry_default( dir, root );
367 static DWORD read_data( HANDLE handle, DWORD offset, void *data, DWORD len )
369 DWORD res;
371 SetFilePointer( handle, offset, NULL, FILE_BEGIN );
372 if (!ReadFile( handle, data, len, &res, NULL )) res = 0;
373 return res;
376 /***********************************************************************
377 * find_ne_resource [internal]
379 static BOOL find_ne_resource( HANDLE handle, DWORD *resLen, DWORD *resOff )
381 const WORD typeid = VS_FILE_INFO | 0x8000;
382 const WORD resid = VS_VERSION_INFO | 0x8000;
383 IMAGE_OS2_HEADER nehd;
384 NE_TYPEINFO *typeInfo;
385 NE_NAMEINFO *nameInfo;
386 DWORD nehdoffset = *resOff;
387 LPBYTE resTab;
388 DWORD resTabSize;
389 int count;
391 /* Read in NE header */
392 if (read_data( handle, nehdoffset, &nehd, sizeof(nehd) ) != sizeof(nehd)) return FALSE;
394 resTabSize = nehd.ne_restab - nehd.ne_rsrctab;
395 if ( !resTabSize )
397 TRACE("No resources in NE dll\n" );
398 return FALSE;
401 /* Read in resource table */
402 resTab = HeapAlloc( GetProcessHeap(), 0, resTabSize );
403 if ( !resTab ) return FALSE;
405 if (read_data( handle, nehd.ne_rsrctab + nehdoffset, resTab, resTabSize ) != resTabSize)
407 HeapFree( GetProcessHeap(), 0, resTab );
408 return FALSE;
411 /* Find resource */
412 typeInfo = (NE_TYPEINFO *)(resTab + 2);
413 while (typeInfo->type_id)
415 if (typeInfo->type_id == typeid) goto found_type;
416 typeInfo = (NE_TYPEINFO *)((char *)(typeInfo + 1) +
417 typeInfo->count * sizeof(NE_NAMEINFO));
419 TRACE("No typeid entry found\n" );
420 HeapFree( GetProcessHeap(), 0, resTab );
421 return FALSE;
423 found_type:
424 nameInfo = (NE_NAMEINFO *)(typeInfo + 1);
426 for (count = typeInfo->count; count > 0; count--, nameInfo++)
427 if (nameInfo->id == resid) goto found_name;
429 TRACE("No resid entry found\n" );
430 HeapFree( GetProcessHeap(), 0, resTab );
431 return FALSE;
433 found_name:
434 /* Return resource data */
435 *resLen = nameInfo->length << *(WORD *)resTab;
436 *resOff = nameInfo->offset << *(WORD *)resTab;
438 HeapFree( GetProcessHeap(), 0, resTab );
439 return TRUE;
442 /***********************************************************************
443 * find_pe_resource [internal]
445 static BOOL find_pe_resource( HANDLE handle, DWORD *resLen, DWORD *resOff, DWORD flags )
447 union
449 IMAGE_NT_HEADERS32 nt32;
450 IMAGE_NT_HEADERS64 nt64;
451 } pehd;
452 DWORD pehdoffset = *resOff;
453 PIMAGE_DATA_DIRECTORY resDataDir;
454 PIMAGE_SECTION_HEADER sections;
455 LPBYTE resSection;
456 DWORD len, section_size, data_size, resDirSize;
457 const void *resDir;
458 const IMAGE_RESOURCE_DIRECTORY *resPtr;
459 const IMAGE_RESOURCE_DATA_ENTRY *resData;
460 int i, nSections;
461 BOOL ret = FALSE;
463 /* Read in PE header */
464 len = read_data( handle, pehdoffset, &pehd, sizeof(pehd) );
465 if (len < sizeof(pehd.nt32.FileHeader)) return FALSE;
466 if (len < sizeof(pehd)) memset( (char *)&pehd + len, 0, sizeof(pehd) - len );
468 switch (pehd.nt32.OptionalHeader.Magic)
470 case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
471 resDataDir = pehd.nt32.OptionalHeader.DataDirectory + IMAGE_DIRECTORY_ENTRY_RESOURCE;
472 break;
473 case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
474 resDataDir = pehd.nt64.OptionalHeader.DataDirectory + IMAGE_DIRECTORY_ENTRY_RESOURCE;
475 break;
476 default:
477 return FALSE;
480 if ( !resDataDir->Size )
482 TRACE("No resources in PE dll\n" );
483 return FALSE;
486 /* Read in section table */
487 nSections = pehd.nt32.FileHeader.NumberOfSections;
488 sections = HeapAlloc( GetProcessHeap(), 0,
489 nSections * sizeof(IMAGE_SECTION_HEADER) );
490 if ( !sections ) return FALSE;
492 len = FIELD_OFFSET( IMAGE_NT_HEADERS32, OptionalHeader ) + pehd.nt32.FileHeader.SizeOfOptionalHeader;
493 if (read_data( handle, pehdoffset + len, sections, nSections * sizeof(IMAGE_SECTION_HEADER) ) !=
494 nSections * sizeof(IMAGE_SECTION_HEADER))
496 HeapFree( GetProcessHeap(), 0, sections );
497 return FALSE;
500 /* Find resource section */
501 for ( i = 0; i < nSections; i++ )
502 if ( resDataDir->VirtualAddress >= sections[i].VirtualAddress
503 && resDataDir->VirtualAddress < sections[i].VirtualAddress +
504 sections[i].SizeOfRawData )
505 break;
507 if ( i == nSections )
509 HeapFree( GetProcessHeap(), 0, sections );
510 TRACE("Couldn't find resource section\n" );
511 return FALSE;
514 /* Read in resource section */
515 data_size = sections[i].SizeOfRawData;
516 section_size = max( data_size, sections[i].Misc.VirtualSize );
517 resSection = HeapAlloc( GetProcessHeap(), 0, section_size );
518 if ( !resSection )
520 HeapFree( GetProcessHeap(), 0, sections );
521 return FALSE;
524 if (read_data( handle, sections[i].PointerToRawData, resSection, data_size ) != data_size) goto done;
525 if (data_size < section_size) memset( (char *)resSection + data_size, 0, section_size - data_size );
527 /* Find resource */
528 resDir = resSection + (resDataDir->VirtualAddress - sections[i].VirtualAddress);
529 resDirSize = section_size - (resDataDir->VirtualAddress - sections[i].VirtualAddress);
531 resPtr = resDir;
532 resPtr = find_entry_by_id( resPtr, VS_FILE_INFO, resDir, resDirSize );
533 if ( !resPtr )
535 TRACE("No typeid entry found\n" );
536 goto done;
538 resPtr = find_entry_by_id( resPtr, VS_VERSION_INFO, resDir, resDirSize );
539 if ( !resPtr )
541 TRACE("No resid entry found\n" );
542 goto done;
544 resPtr = find_entry_language( resPtr, resDir, resDirSize, flags );
545 if ( !resPtr )
547 TRACE("No default language entry found\n" );
548 goto done;
551 /* Find resource data section */
552 resData = (const IMAGE_RESOURCE_DATA_ENTRY*)resPtr;
553 for ( i = 0; i < nSections; i++ )
554 if ( resData->OffsetToData >= sections[i].VirtualAddress
555 && resData->OffsetToData < sections[i].VirtualAddress +
556 sections[i].SizeOfRawData )
557 break;
559 if ( i == nSections )
561 TRACE("Couldn't find resource data section\n" );
562 goto done;
565 /* Return resource data */
566 *resLen = resData->Size;
567 *resOff = resData->OffsetToData - sections[i].VirtualAddress + sections[i].PointerToRawData;
568 ret = TRUE;
570 done:
571 HeapFree( GetProcessHeap(), 0, resSection );
572 HeapFree( GetProcessHeap(), 0, sections );
573 return ret;
577 /***********************************************************************
578 * find_version_resource [internal]
580 static DWORD find_version_resource( HANDLE handle, DWORD *reslen, DWORD *offset, DWORD flags )
582 IMAGE_DOS_HEADER mzh;
583 WORD magic;
585 if (read_data( handle, 0, &mzh, sizeof(mzh) ) != sizeof(mzh)) return 0;
586 if (mzh.e_magic != IMAGE_DOS_SIGNATURE) return 0;
588 if (read_data( handle, mzh.e_lfanew, &magic, sizeof(magic) ) != sizeof(magic)) return 0;
589 *offset = mzh.e_lfanew;
591 switch (magic)
593 case IMAGE_OS2_SIGNATURE:
594 if (!find_ne_resource( handle, reslen, offset )) magic = 0;
595 break;
596 case IMAGE_NT_SIGNATURE:
597 if (!find_pe_resource( handle, reslen, offset, flags )) magic = 0;
598 break;
600 WARN( "Can't handle %04x files.\n", magic );
601 return magic;
604 /******************************************************************************
605 * This function will print via standard TRACE, debug info regarding
606 * the file info structure vffi.
608 static void print_vffi_debug(const VS_FIXEDFILEINFO *vffi)
610 BOOL versioned_printer = FALSE;
612 if((vffi->dwFileType == VFT_DLL) || (vffi->dwFileType == VFT_DRV))
614 if(vffi->dwFileSubtype == VFT2_DRV_VERSIONED_PRINTER)
615 /* this is documented for newer w2k Drivers and up */
616 versioned_printer = TRUE;
617 else if( (vffi->dwFileSubtype == VFT2_DRV_PRINTER) &&
618 (vffi->dwFileVersionMS != vffi->dwProductVersionMS) &&
619 (vffi->dwFileVersionMS > 0) &&
620 (vffi->dwFileVersionMS <= 3) )
621 /* found this on NT 3.51, NT4.0 and old w2k Drivers */
622 versioned_printer = TRUE;
625 TRACE("structversion=%u.%u, ",
626 HIWORD(vffi->dwStrucVersion),LOWORD(vffi->dwStrucVersion));
627 if(versioned_printer)
629 WORD mode = LOWORD(vffi->dwFileVersionMS);
630 WORD ver_rev = HIWORD(vffi->dwFileVersionLS);
631 TRACE("fileversion=%lu.%u.%u.%u (%s.major.minor.release), ",
632 (vffi->dwFileVersionMS),
633 HIBYTE(ver_rev), LOBYTE(ver_rev), LOWORD(vffi->dwFileVersionLS),
634 (mode == 3) ? "Usermode" : ((mode <= 2) ? "Kernelmode" : "?") );
636 else
638 TRACE("fileversion=%u.%u.%u.%u, ",
639 HIWORD(vffi->dwFileVersionMS),LOWORD(vffi->dwFileVersionMS),
640 HIWORD(vffi->dwFileVersionLS),LOWORD(vffi->dwFileVersionLS));
642 TRACE("productversion=%u.%u.%u.%u\n",
643 HIWORD(vffi->dwProductVersionMS),LOWORD(vffi->dwProductVersionMS),
644 HIWORD(vffi->dwProductVersionLS),LOWORD(vffi->dwProductVersionLS));
646 TRACE("flagmask=0x%lx, flags=0x%lx %s%s%s%s%s%s\n",
647 vffi->dwFileFlagsMask, vffi->dwFileFlags,
648 (vffi->dwFileFlags & VS_FF_DEBUG) ? "DEBUG," : "",
649 (vffi->dwFileFlags & VS_FF_PRERELEASE) ? "PRERELEASE," : "",
650 (vffi->dwFileFlags & VS_FF_PATCHED) ? "PATCHED," : "",
651 (vffi->dwFileFlags & VS_FF_PRIVATEBUILD) ? "PRIVATEBUILD," : "",
652 (vffi->dwFileFlags & VS_FF_INFOINFERRED) ? "INFOINFERRED," : "",
653 (vffi->dwFileFlags & VS_FF_SPECIALBUILD) ? "SPECIALBUILD," : "");
655 TRACE("(");
657 TRACE("OS=0x%x.0x%x ", HIWORD(vffi->dwFileOS), LOWORD(vffi->dwFileOS));
659 switch (vffi->dwFileOS&0xFFFF0000)
661 case VOS_DOS:TRACE("DOS,");break;
662 case VOS_OS216:TRACE("OS/2-16,");break;
663 case VOS_OS232:TRACE("OS/2-32,");break;
664 case VOS_NT:TRACE("NT,");break;
665 case VOS_UNKNOWN:
666 default:
667 TRACE("UNKNOWN(0x%lx),",vffi->dwFileOS&0xFFFF0000);break;
670 switch (LOWORD(vffi->dwFileOS))
672 case VOS__BASE:TRACE("BASE");break;
673 case VOS__WINDOWS16:TRACE("WIN16");break;
674 case VOS__WINDOWS32:TRACE("WIN32");break;
675 case VOS__PM16:TRACE("PM16");break;
676 case VOS__PM32:TRACE("PM32");break;
677 default:
678 TRACE("UNKNOWN(0x%x)",LOWORD(vffi->dwFileOS));break;
681 TRACE(")\n");
683 switch (vffi->dwFileType)
685 case VFT_APP:TRACE("filetype=APP");break;
686 case VFT_DLL:
687 TRACE("filetype=DLL");
688 if(vffi->dwFileSubtype != 0)
690 if(versioned_printer) /* NT3.x/NT4.0 or old w2k Driver */
691 TRACE(",PRINTER");
692 TRACE(" (subtype=0x%lx)", vffi->dwFileSubtype);
694 break;
695 case VFT_DRV:
696 TRACE("filetype=DRV,");
697 switch(vffi->dwFileSubtype)
699 case VFT2_DRV_PRINTER:TRACE("PRINTER");break;
700 case VFT2_DRV_KEYBOARD:TRACE("KEYBOARD");break;
701 case VFT2_DRV_LANGUAGE:TRACE("LANGUAGE");break;
702 case VFT2_DRV_DISPLAY:TRACE("DISPLAY");break;
703 case VFT2_DRV_MOUSE:TRACE("MOUSE");break;
704 case VFT2_DRV_NETWORK:TRACE("NETWORK");break;
705 case VFT2_DRV_SYSTEM:TRACE("SYSTEM");break;
706 case VFT2_DRV_INSTALLABLE:TRACE("INSTALLABLE");break;
707 case VFT2_DRV_SOUND:TRACE("SOUND");break;
708 case VFT2_DRV_COMM:TRACE("COMM");break;
709 case VFT2_DRV_INPUTMETHOD:TRACE("INPUTMETHOD");break;
710 case VFT2_DRV_VERSIONED_PRINTER:TRACE("VERSIONED_PRINTER");break;
711 case VFT2_UNKNOWN:
712 default:
713 TRACE("UNKNOWN(0x%lx)",vffi->dwFileSubtype);break;
715 break;
716 case VFT_FONT:
717 TRACE("filetype=FONT,");
718 switch (vffi->dwFileSubtype)
720 case VFT2_FONT_RASTER:TRACE("RASTER");break;
721 case VFT2_FONT_VECTOR:TRACE("VECTOR");break;
722 case VFT2_FONT_TRUETYPE:TRACE("TRUETYPE");break;
723 default:TRACE("UNKNOWN(0x%lx)",vffi->dwFileSubtype);break;
725 break;
726 case VFT_VXD:TRACE("filetype=VXD");break;
727 case VFT_STATIC_LIB:TRACE("filetype=STATIC_LIB");break;
728 case VFT_UNKNOWN:
729 default:
730 TRACE("filetype=Unknown(0x%lx)",vffi->dwFileType);break;
733 TRACE("\n");
734 TRACE("filedate=0x%lx.0x%lx\n",vffi->dwFileDateMS,vffi->dwFileDateLS);
737 /***********************************************************************
738 * GetFileVersionInfoSizeW (kernelbase.@)
740 DWORD WINAPI GetFileVersionInfoSizeW( LPCWSTR filename, LPDWORD handle )
742 return GetFileVersionInfoSizeExW( FILE_VER_GET_LOCALISED, filename, handle );
745 /***********************************************************************
746 * GetFileVersionInfoSizeA (kernelbase.@)
748 DWORD WINAPI GetFileVersionInfoSizeA( LPCSTR filename, LPDWORD handle )
750 return GetFileVersionInfoSizeExA( FILE_VER_GET_LOCALISED, filename, handle );
753 /******************************************************************************
754 * GetFileVersionInfoSizeExW (kernelbase.@)
756 DWORD WINAPI GetFileVersionInfoSizeExW( DWORD flags, LPCWSTR filename, LPDWORD ret_handle )
758 DWORD len, offset, magic = 1;
759 HMODULE hModule;
761 TRACE("(0x%lx,%s,%p)\n", flags, debugstr_w(filename), ret_handle );
763 if (ret_handle) *ret_handle = 0;
765 if (!filename)
767 SetLastError(ERROR_INVALID_PARAMETER);
768 return 0;
770 if (!*filename)
772 SetLastError(ERROR_BAD_PATHNAME);
773 return 0;
775 if (flags & ~FILE_VER_GET_LOCALISED)
776 FIXME("flags 0x%lx ignored\n", flags & ~FILE_VER_GET_LOCALISED);
778 if ((hModule = LoadLibraryExW( filename, 0, LOAD_LIBRARY_AS_IMAGE_RESOURCE )))
780 HRSRC hRsrc = NULL;
781 if (!(flags & FILE_VER_GET_LOCALISED))
783 LANGID english = MAKELANGID( LANG_ENGLISH, SUBLANG_DEFAULT );
784 hRsrc = FindResourceExW( hModule, (LPWSTR)VS_FILE_INFO,
785 MAKEINTRESOURCEW(VS_VERSION_INFO), english );
787 if (!hRsrc)
788 hRsrc = FindResourceW( hModule, MAKEINTRESOURCEW(VS_VERSION_INFO),
789 (LPWSTR)VS_FILE_INFO );
790 if (hRsrc)
792 magic = IMAGE_NT_SIGNATURE;
793 len = SizeofResource( hModule, hRsrc );
795 FreeLibrary( hModule );
797 else
799 HANDLE handle = CreateFileW( filename, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
800 NULL, OPEN_EXISTING, 0, 0 );
801 if (handle == INVALID_HANDLE_VALUE) return 0;
802 magic = find_version_resource( handle, &len, &offset, flags );
803 CloseHandle( handle );
806 switch (magic)
808 case IMAGE_OS2_SIGNATURE:
809 /* We have a 16bit resource.
811 * XP/W2K/W2K3 uses a buffer which is more than the actual needed space:
813 * (info->wLength - sizeof(VS_FIXEDFILEINFO)) * 4
815 * This extra buffer is used for ANSI to Unicode conversions in W-Calls.
816 * info->wLength should be the same as len. Currently it isn't but that
817 * doesn't seem to be a problem (len is bigger than info->wLength).
819 SetLastError(0);
820 return (len - sizeof(VS_FIXEDFILEINFO)) * 4;
822 case IMAGE_NT_SIGNATURE:
823 /* We have a 32bit resource.
825 * XP/W2K/W2K3 uses a buffer which is 2 times the actual needed space + 4 bytes "FE2X"
826 * This extra buffer is used for Unicode to ANSI conversions in A-Calls
828 SetLastError(0);
829 return (len * 2) + 4;
831 default:
832 if (GetVersion() & 0x80000000) /* Windows 95/98 */
833 SetLastError(ERROR_FILE_NOT_FOUND);
834 else
835 SetLastError(ERROR_RESOURCE_DATA_NOT_FOUND);
836 return 0;
840 /******************************************************************************
841 * GetFileVersionInfoSizeExA (kernelbase.@)
843 DWORD WINAPI GetFileVersionInfoSizeExA( DWORD flags, LPCSTR filename, LPDWORD handle )
845 UNICODE_STRING filenameW;
846 DWORD retval;
848 TRACE("(0x%lx,%s,%p)\n", flags, debugstr_a(filename), handle );
850 if(filename)
851 RtlCreateUnicodeStringFromAsciiz(&filenameW, filename);
852 else
853 filenameW.Buffer = NULL;
855 retval = GetFileVersionInfoSizeExW(flags, filenameW.Buffer, handle);
857 RtlFreeUnicodeString(&filenameW);
859 return retval;
862 /***********************************************************************
863 * GetFileVersionInfoExW (kernelbase.@)
865 BOOL WINAPI GetFileVersionInfoExW( DWORD flags, LPCWSTR filename, DWORD ignored, DWORD datasize, LPVOID data )
867 static const char signature[4] = "FE2X";
868 DWORD len, offset, magic = 1;
869 HMODULE hModule;
870 VS_VERSION_INFO_STRUCT32* vvis = data;
872 TRACE("(0x%lx,%s,%ld,size=%ld,data=%p)\n",
873 flags, debugstr_w(filename), ignored, datasize, data );
875 if (!data)
877 SetLastError(ERROR_INVALID_DATA);
878 return FALSE;
880 if (flags & ~FILE_VER_GET_LOCALISED)
881 FIXME("flags 0x%lx ignored\n", flags & ~FILE_VER_GET_LOCALISED);
883 if ((hModule = LoadLibraryExW( filename, 0, LOAD_LIBRARY_AS_IMAGE_RESOURCE )))
885 HRSRC hRsrc = NULL;
886 if (!(flags & FILE_VER_GET_LOCALISED))
888 LANGID english = MAKELANGID( LANG_ENGLISH, SUBLANG_DEFAULT );
889 hRsrc = FindResourceExW( hModule, (LPWSTR)VS_FILE_INFO,
890 MAKEINTRESOURCEW(VS_VERSION_INFO), english );
892 if (!hRsrc)
893 hRsrc = FindResourceW( hModule, MAKEINTRESOURCEW(VS_VERSION_INFO),
894 (LPWSTR)VS_FILE_INFO );
895 if (hRsrc)
897 HGLOBAL hMem = LoadResource( hModule, hRsrc );
898 magic = IMAGE_NT_SIGNATURE;
899 len = min( SizeofResource(hModule, hRsrc), datasize );
900 memcpy( data, LockResource( hMem ), len );
901 FreeResource( hMem );
903 FreeLibrary( hModule );
905 else
907 HANDLE handle = CreateFileW( filename, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
908 NULL, OPEN_EXISTING, 0, 0 );
909 if (handle == INVALID_HANDLE_VALUE) return 0;
910 if ((magic = find_version_resource( handle, &len, &offset, flags )))
911 len = read_data( handle, offset, data, min( len, datasize ));
912 CloseHandle( handle );
915 switch (magic)
917 case IMAGE_OS2_SIGNATURE:
918 /* We have a 16bit resource. */
919 if (TRACE_ON(ver))
920 print_vffi_debug( (VS_FIXEDFILEINFO *)VersionInfo16_Value( (VS_VERSION_INFO_STRUCT16 *)data ));
921 SetLastError(0);
922 return TRUE;
924 case IMAGE_NT_SIGNATURE:
925 /* We have a 32bit resource.
927 * XP/W2K/W2K3 uses a buffer which is 2 times the actual needed space + 4 bytes "FE2X"
928 * This extra buffer is used for Unicode to ANSI conversions in A-Calls
930 len = vvis->wLength + sizeof(signature);
931 if (datasize >= len) memcpy( (char*)data + vvis->wLength, signature, sizeof(signature) );
932 if (TRACE_ON(ver))
933 print_vffi_debug( (VS_FIXEDFILEINFO *)VersionInfo32_Value( vvis ));
934 SetLastError(0);
935 return TRUE;
937 default:
938 SetLastError( ERROR_RESOURCE_DATA_NOT_FOUND );
939 return FALSE;
943 /***********************************************************************
944 * GetFileVersionInfoExA (kernelbase.@)
946 BOOL WINAPI GetFileVersionInfoExA( DWORD flags, LPCSTR filename, DWORD handle, DWORD datasize, LPVOID data )
948 UNICODE_STRING filenameW;
949 BOOL retval;
951 TRACE("(0x%lx,%s,%ld,size=%ld,data=%p)\n",
952 flags, debugstr_a(filename), handle, datasize, data );
954 if(filename)
955 RtlCreateUnicodeStringFromAsciiz(&filenameW, filename);
956 else
957 filenameW.Buffer = NULL;
959 retval = GetFileVersionInfoExW(flags, filenameW.Buffer, handle, datasize, data);
961 RtlFreeUnicodeString(&filenameW);
963 return retval;
966 /***********************************************************************
967 * GetFileVersionInfoW (kernelbase.@)
969 BOOL WINAPI GetFileVersionInfoW( LPCWSTR filename, DWORD handle, DWORD datasize, LPVOID data )
971 return GetFileVersionInfoExW(FILE_VER_GET_LOCALISED, filename, handle, datasize, data);
974 /***********************************************************************
975 * GetFileVersionInfoA (kernelbase.@)
977 BOOL WINAPI GetFileVersionInfoA( LPCSTR filename, DWORD handle, DWORD datasize, LPVOID data )
979 return GetFileVersionInfoExA(FILE_VER_GET_LOCALISED, filename, handle, datasize, data);
982 /***********************************************************************
983 * VersionInfo16_FindChild [internal]
985 static const VS_VERSION_INFO_STRUCT16 *VersionInfo16_FindChild( const VS_VERSION_INFO_STRUCT16 *info,
986 LPCSTR key, UINT len )
988 const VS_VERSION_INFO_STRUCT16 *child = VersionInfo16_Children( info );
990 while ((char *)child < (char *)info + info->wLength )
992 if (!strnicmp( child->szKey, key, len ) && !child->szKey[len])
993 return child;
995 if (!(child->wLength)) return NULL;
996 child = VersionInfo16_Next( child );
999 return NULL;
1002 /***********************************************************************
1003 * VersionInfo32_FindChild [internal]
1005 static const VS_VERSION_INFO_STRUCT32 *VersionInfo32_FindChild( const VS_VERSION_INFO_STRUCT32 *info,
1006 LPCWSTR key, UINT len )
1008 const VS_VERSION_INFO_STRUCT32 *child = VersionInfo32_Children( info );
1010 while ((char *)child < (char *)info + info->wLength )
1012 if (!wcsnicmp( child->szKey, key, len ) && !child->szKey[len])
1013 return child;
1015 if (!(child->wLength)) return NULL;
1016 child = VersionInfo32_Next( child );
1019 return NULL;
1022 /***********************************************************************
1023 * VersionInfo16_QueryValue [internal]
1025 * Gets a value from a 16-bit NE resource
1027 static BOOL VersionInfo16_QueryValue( const VS_VERSION_INFO_STRUCT16 *info, LPCSTR lpSubBlock,
1028 LPVOID *lplpBuffer, UINT *puLen )
1030 while ( *lpSubBlock )
1032 /* Find next path component */
1033 LPCSTR lpNextSlash;
1034 for ( lpNextSlash = lpSubBlock; *lpNextSlash; lpNextSlash++ )
1035 if ( *lpNextSlash == '\\' )
1036 break;
1038 /* Skip empty components */
1039 if ( lpNextSlash == lpSubBlock )
1041 lpSubBlock++;
1042 continue;
1045 /* We have a non-empty component: search info for key */
1046 info = VersionInfo16_FindChild( info, lpSubBlock, lpNextSlash-lpSubBlock );
1047 if ( !info )
1049 if (puLen) *puLen = 0 ;
1050 SetLastError( ERROR_RESOURCE_TYPE_NOT_FOUND );
1051 return FALSE;
1054 /* Skip path component */
1055 lpSubBlock = lpNextSlash;
1058 /* Return value */
1059 *lplpBuffer = VersionInfo16_Value( info );
1060 if (puLen)
1061 *puLen = info->wValueLength;
1063 return TRUE;
1066 /***********************************************************************
1067 * VersionInfo32_QueryValue [internal]
1069 * Gets a value from a 32-bit PE resource
1071 static BOOL VersionInfo32_QueryValue( const VS_VERSION_INFO_STRUCT32 *info, LPCWSTR lpSubBlock,
1072 LPVOID *lplpBuffer, UINT *puLen, BOOL *pbText )
1074 TRACE("lpSubBlock : (%s)\n", debugstr_w(lpSubBlock));
1076 while ( *lpSubBlock )
1078 /* Find next path component */
1079 LPCWSTR lpNextSlash;
1080 for ( lpNextSlash = lpSubBlock; *lpNextSlash; lpNextSlash++ )
1081 if ( *lpNextSlash == '\\' )
1082 break;
1084 /* Skip empty components */
1085 if ( lpNextSlash == lpSubBlock )
1087 lpSubBlock++;
1088 continue;
1091 /* We have a non-empty component: search info for key */
1092 info = VersionInfo32_FindChild( info, lpSubBlock, lpNextSlash-lpSubBlock );
1093 if ( !info )
1095 if (puLen) *puLen = 0 ;
1096 SetLastError( ERROR_RESOURCE_TYPE_NOT_FOUND );
1097 return FALSE;
1100 /* Skip path component */
1101 lpSubBlock = lpNextSlash;
1104 /* Return value */
1105 *lplpBuffer = VersionInfo32_Value( info );
1106 if (puLen)
1107 *puLen = info->wValueLength;
1108 if (pbText)
1109 *pbText = info->wType;
1111 return TRUE;
1114 /***********************************************************************
1115 * VerQueryValueA (kernelbase.@)
1117 BOOL WINAPI VerQueryValueA( LPCVOID pBlock, LPCSTR lpSubBlock,
1118 LPVOID *lplpBuffer, PUINT puLen )
1120 static const char rootA[] = "\\";
1121 const VS_VERSION_INFO_STRUCT16 *info = pBlock;
1123 TRACE("(%p,%s,%p,%p)\n",
1124 pBlock, debugstr_a(lpSubBlock), lplpBuffer, puLen );
1126 if (!pBlock)
1127 return FALSE;
1129 if (lpSubBlock == NULL || lpSubBlock[0] == '\0')
1130 lpSubBlock = rootA;
1132 if ( !VersionInfoIs16( info ) )
1134 BOOL ret, isText;
1135 INT len;
1136 LPWSTR lpSubBlockW;
1137 UINT value_len;
1139 len = MultiByteToWideChar(CP_ACP, 0, lpSubBlock, -1, NULL, 0);
1140 lpSubBlockW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1142 if (!lpSubBlockW)
1143 return FALSE;
1145 MultiByteToWideChar(CP_ACP, 0, lpSubBlock, -1, lpSubBlockW, len);
1147 ret = VersionInfo32_QueryValue(pBlock, lpSubBlockW, lplpBuffer, &value_len, &isText);
1148 if (puLen) *puLen = value_len;
1150 HeapFree(GetProcessHeap(), 0, lpSubBlockW);
1152 if (ret && isText)
1154 /* Set lpBuffer so it points to the 'empty' area where we store
1155 * the converted strings
1157 LPSTR lpBufferA = (LPSTR)pBlock + info->wLength + 4;
1158 DWORD pos = (LPCSTR)*lplpBuffer - (LPCSTR)pBlock;
1159 len = WideCharToMultiByte(CP_ACP, 0, *lplpBuffer, value_len,
1160 lpBufferA + pos, info->wLength - pos, NULL, NULL);
1161 *lplpBuffer = lpBufferA + pos;
1162 if (puLen) *puLen = len;
1164 return ret;
1167 return VersionInfo16_QueryValue(info, lpSubBlock, lplpBuffer, puLen);
1170 /***********************************************************************
1171 * VerQueryValueW (kernelbase.@)
1173 BOOL WINAPI VerQueryValueW( LPCVOID pBlock, LPCWSTR lpSubBlock,
1174 LPVOID *lplpBuffer, PUINT puLen )
1176 const VS_VERSION_INFO_STRUCT32 *info = pBlock;
1178 TRACE("(%p,%s,%p,%p)\n",
1179 pBlock, debugstr_w(lpSubBlock), lplpBuffer, puLen );
1181 if (!pBlock)
1182 return FALSE;
1184 if (!lpSubBlock || !lpSubBlock[0])
1185 lpSubBlock = L"\\";
1187 if ( VersionInfoIs16( info ) )
1189 BOOL ret;
1190 int len;
1191 LPSTR lpSubBlockA;
1193 len = WideCharToMultiByte(CP_ACP, 0, lpSubBlock, -1, NULL, 0, NULL, NULL);
1194 lpSubBlockA = HeapAlloc(GetProcessHeap(), 0, len * sizeof(char));
1196 if (!lpSubBlockA)
1197 return FALSE;
1199 WideCharToMultiByte(CP_ACP, 0, lpSubBlock, -1, lpSubBlockA, len, NULL, NULL);
1201 ret = VersionInfo16_QueryValue(pBlock, lpSubBlockA, lplpBuffer, puLen);
1203 HeapFree(GetProcessHeap(), 0, lpSubBlockA);
1205 if (ret && wcscmp( lpSubBlock, L"\\" ) && wcsicmp( lpSubBlock, L"\\VarFileInfo\\Translation" ))
1207 /* Set lpBuffer so it points to the 'empty' area where we store
1208 * the converted strings
1210 LPWSTR lpBufferW = (LPWSTR)((LPSTR)pBlock + info->wLength);
1211 DWORD pos = (LPCSTR)*lplpBuffer - (LPCSTR)pBlock;
1212 DWORD max = (info->wLength - sizeof(VS_FIXEDFILEINFO)) * 4 - info->wLength;
1214 len = MultiByteToWideChar(CP_ACP, 0, *lplpBuffer, -1,
1215 lpBufferW + pos, max/sizeof(WCHAR) - pos );
1216 *lplpBuffer = lpBufferW + pos;
1217 if (puLen) *puLen = len;
1219 return ret;
1222 return VersionInfo32_QueryValue(info, lpSubBlock, lplpBuffer, puLen, NULL);
1226 /******************************************************************************
1227 * file_existsA
1229 static BOOL file_existsA( char const * path, char const * file, BOOL excl )
1231 DWORD sharing = excl ? 0 : FILE_SHARE_READ | FILE_SHARE_WRITE;
1232 char filename[MAX_PATH];
1233 int len;
1234 HANDLE handle;
1236 if (path)
1238 strcpy( filename, path );
1239 len = strlen(filename);
1240 if (len && filename[len - 1] != '\\') strcat( filename, "\\" );
1241 strcat( filename, file );
1243 else if (!SearchPathA( NULL, file, NULL, MAX_PATH, filename, NULL )) return FALSE;
1245 handle = CreateFileA( filename, 0, sharing, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0 );
1246 if (handle == INVALID_HANDLE_VALUE) return FALSE;
1247 CloseHandle( handle );
1248 return TRUE;
1251 /******************************************************************************
1252 * file_existsW
1254 static BOOL file_existsW( const WCHAR *path, const WCHAR *file, BOOL excl )
1256 DWORD sharing = excl ? 0 : FILE_SHARE_READ | FILE_SHARE_WRITE;
1257 WCHAR filename[MAX_PATH];
1258 int len;
1259 HANDLE handle;
1261 if (path)
1263 lstrcpyW( filename, path );
1264 len = lstrlenW(filename);
1265 if (len && filename[len - 1] != '\\') lstrcatW( filename, L"\\" );
1266 lstrcatW( filename, file );
1268 else if (!SearchPathW( NULL, file, NULL, MAX_PATH, filename, NULL )) return FALSE;
1270 handle = CreateFileW( filename, 0, sharing, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0 );
1271 if (handle == INVALID_HANDLE_VALUE) return FALSE;
1272 CloseHandle( handle );
1273 return TRUE;
1276 /*****************************************************************************
1277 * VerFindFileA (kernelbase.@)
1279 * Determines where to install a file based on whether it locates another
1280 * version of the file in the system. The values VerFindFile returns are
1281 * used in a subsequent call to the VerInstallFile function.
1283 DWORD WINAPI VerFindFileA( DWORD flags, LPCSTR filename, LPCSTR win_dir, LPCSTR app_dir,
1284 LPSTR cur_dir, PUINT curdir_len, LPSTR dest, PUINT dest_len )
1286 DWORD retval = 0;
1287 const char *curDir;
1288 const char *destDir;
1289 char winDir[MAX_PATH], systemDir[MAX_PATH];
1291 TRACE("flags = %lx filename=%s windir=%s appdir=%s curdirlen=%p(%u) destdirlen=%p(%u)\n",
1292 flags, debugstr_a(filename), debugstr_a(win_dir), debugstr_a(app_dir),
1293 curdir_len, curdir_len ? *curdir_len : 0, dest_len, dest_len ? *dest_len : 0 );
1295 /* Figure out where the file should go; shared files default to the
1296 system directory */
1298 GetSystemDirectoryA(systemDir, sizeof(systemDir));
1299 curDir = "";
1301 if(flags & VFFF_ISSHAREDFILE)
1303 destDir = systemDir;
1304 /* Were we given a filename? If so, try to find the file. */
1305 if(filename)
1307 if(file_existsA(destDir, filename, FALSE)) curDir = destDir;
1308 else if(app_dir && file_existsA(app_dir, filename, FALSE))
1309 curDir = app_dir;
1311 if(!file_existsA(systemDir, filename, FALSE))
1312 retval |= VFF_CURNEDEST;
1315 else /* not a shared file */
1317 destDir = app_dir ? app_dir : "";
1318 if(filename)
1320 GetWindowsDirectoryA( winDir, MAX_PATH );
1321 if(file_existsA(destDir, filename, FALSE)) curDir = destDir;
1322 else if(file_existsA(winDir, filename, FALSE))
1323 curDir = winDir;
1324 else if(file_existsA(systemDir, filename, FALSE))
1325 curDir = systemDir;
1327 if (app_dir && app_dir[0])
1329 if(!file_existsA(app_dir, filename, FALSE))
1330 retval |= VFF_CURNEDEST;
1332 else if(file_existsA(NULL, filename, FALSE))
1333 retval |= VFF_CURNEDEST;
1337 /* Check to see if the file exists and is in use by another application */
1338 if (filename && file_existsA(curDir, filename, FALSE))
1340 if (filename && !file_existsA(curDir, filename, TRUE))
1341 retval |= VFF_FILEINUSE;
1344 if (dest_len && dest)
1346 UINT len = strlen(destDir) + 1;
1347 if (*dest_len < len) retval |= VFF_BUFFTOOSMALL;
1348 lstrcpynA(dest, destDir, *dest_len);
1349 *dest_len = len;
1351 if (curdir_len && cur_dir)
1353 UINT len = strlen(curDir) + 1;
1354 if (*curdir_len < len) retval |= VFF_BUFFTOOSMALL;
1355 lstrcpynA(cur_dir, curDir, *curdir_len);
1356 *curdir_len = len;
1359 TRACE("ret = %lu (%s%s%s) curdir=%s destdir=%s\n", retval,
1360 (retval & VFF_CURNEDEST) ? "VFF_CURNEDEST " : "",
1361 (retval & VFF_FILEINUSE) ? "VFF_FILEINUSE " : "",
1362 (retval & VFF_BUFFTOOSMALL) ? "VFF_BUFFTOOSMALL " : "",
1363 debugstr_a(cur_dir), debugstr_a(dest));
1365 return retval;
1368 /*****************************************************************************
1369 * VerFindFileW (kernelbase.@)
1371 DWORD WINAPI VerFindFileW( DWORD flags, LPCWSTR filename, LPCWSTR win_dir, LPCWSTR app_dir,
1372 LPWSTR cur_dir, PUINT curdir_len, LPWSTR dest, PUINT dest_len )
1374 DWORD retval = 0;
1375 const WCHAR *curDir;
1376 const WCHAR *destDir;
1378 TRACE("flags = %lx filename=%s windir=%s appdir=%s curdirlen=%p(%u) destdirlen=%p(%u)\n",
1379 flags, debugstr_w(filename), debugstr_w(win_dir), debugstr_w(app_dir),
1380 curdir_len, curdir_len ? *curdir_len : 0, dest_len, dest_len ? *dest_len : 0 );
1382 /* Figure out where the file should go; shared files default to the
1383 system directory */
1385 curDir = L"";
1387 if(flags & VFFF_ISSHAREDFILE)
1389 destDir = system_dir;
1390 /* Were we given a filename? If so, try to find the file. */
1391 if(filename)
1393 if(file_existsW(destDir, filename, FALSE)) curDir = destDir;
1394 else if(app_dir && file_existsW(app_dir, filename, FALSE))
1396 curDir = app_dir;
1397 retval |= VFF_CURNEDEST;
1401 else /* not a shared file */
1403 destDir = app_dir ? app_dir : L"";
1404 if(filename)
1406 if(file_existsW(destDir, filename, FALSE)) curDir = destDir;
1407 else if(file_existsW(windows_dir, filename, FALSE))
1409 curDir = windows_dir;
1410 retval |= VFF_CURNEDEST;
1412 else if (file_existsW(system_dir, filename, FALSE))
1414 curDir = system_dir;
1415 retval |= VFF_CURNEDEST;
1420 if (filename && !file_existsW(curDir, filename, TRUE))
1421 retval |= VFF_FILEINUSE;
1423 if (dest_len && dest)
1425 UINT len = lstrlenW(destDir) + 1;
1426 if (*dest_len < len) retval |= VFF_BUFFTOOSMALL;
1427 lstrcpynW(dest, destDir, *dest_len);
1428 *dest_len = len;
1430 if (curdir_len && cur_dir)
1432 UINT len = lstrlenW(curDir) + 1;
1433 if (*curdir_len < len) retval |= VFF_BUFFTOOSMALL;
1434 lstrcpynW(cur_dir, curDir, *curdir_len);
1435 *curdir_len = len;
1438 TRACE("ret = %lu (%s%s%s) curdir=%s destdir=%s\n", retval,
1439 (retval & VFF_CURNEDEST) ? "VFF_CURNEDEST " : "",
1440 (retval & VFF_FILEINUSE) ? "VFF_FILEINUSE " : "",
1441 (retval & VFF_BUFFTOOSMALL) ? "VFF_BUFFTOOSMALL " : "",
1442 debugstr_w(cur_dir), debugstr_w(dest));
1443 return retval;
1447 /***********************************************************************
1448 * GetProductInfo (kernelbase.@)
1450 BOOL WINAPI DECLSPEC_HOTPATCH GetProductInfo( DWORD os_major, DWORD os_minor,
1451 DWORD sp_major, DWORD sp_minor, DWORD *type )
1453 return RtlGetProductInfo( os_major, os_minor, sp_major, sp_minor, type );
1457 /***********************************************************************
1458 * GetVersion (kernelbase.@)
1460 DWORD WINAPI GetVersion(void)
1462 OSVERSIONINFOEXW info;
1463 DWORD result;
1465 info.dwOSVersionInfoSize = sizeof(info);
1466 if (!GetVersionExW( (OSVERSIONINFOW *)&info )) return 0;
1468 result = MAKELONG( MAKEWORD( info.dwMajorVersion, info.dwMinorVersion ),
1469 (info.dwPlatformId ^ 2) << 14 );
1471 if (info.dwPlatformId == VER_PLATFORM_WIN32_NT)
1472 result |= LOWORD(info.dwBuildNumber) << 16;
1473 return result;
1477 /***********************************************************************
1478 * GetVersionExA (kernelbase.@)
1480 BOOL WINAPI GetVersionExA( OSVERSIONINFOA *info )
1482 OSVERSIONINFOEXW infoW;
1484 if (info->dwOSVersionInfoSize != sizeof(OSVERSIONINFOA) &&
1485 info->dwOSVersionInfoSize != sizeof(OSVERSIONINFOEXA))
1487 WARN( "wrong OSVERSIONINFO size from app (got: %ld)\n", info->dwOSVersionInfoSize );
1488 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1489 return FALSE;
1492 infoW.dwOSVersionInfoSize = sizeof(infoW);
1493 if (!GetVersionExW( (OSVERSIONINFOW *)&infoW )) return FALSE;
1495 info->dwMajorVersion = infoW.dwMajorVersion;
1496 info->dwMinorVersion = infoW.dwMinorVersion;
1497 info->dwBuildNumber = infoW.dwBuildNumber;
1498 info->dwPlatformId = infoW.dwPlatformId;
1499 WideCharToMultiByte( CP_ACP, 0, infoW.szCSDVersion, -1,
1500 info->szCSDVersion, sizeof(info->szCSDVersion), NULL, NULL );
1502 if (info->dwOSVersionInfoSize == sizeof(OSVERSIONINFOEXA))
1504 OSVERSIONINFOEXA *vex = (OSVERSIONINFOEXA *)info;
1505 vex->wServicePackMajor = infoW.wServicePackMajor;
1506 vex->wServicePackMinor = infoW.wServicePackMinor;
1507 vex->wSuiteMask = infoW.wSuiteMask;
1508 vex->wProductType = infoW.wProductType;
1510 return TRUE;
1514 /***********************************************************************
1515 * GetVersionExW (kernelbase.@)
1517 BOOL WINAPI GetVersionExW( OSVERSIONINFOW *info )
1519 static INIT_ONCE init_once = INIT_ONCE_STATIC_INIT;
1521 if (info->dwOSVersionInfoSize != sizeof(OSVERSIONINFOW) &&
1522 info->dwOSVersionInfoSize != sizeof(OSVERSIONINFOEXW))
1524 WARN( "wrong OSVERSIONINFO size from app (got: %ld)\n", info->dwOSVersionInfoSize );
1525 return FALSE;
1528 if (!InitOnceExecuteOnce(&init_once, init_current_version, NULL, NULL)) return FALSE;
1530 info->dwMajorVersion = current_version.dwMajorVersion;
1531 info->dwMinorVersion = current_version.dwMinorVersion;
1532 info->dwBuildNumber = current_version.dwBuildNumber;
1533 info->dwPlatformId = current_version.dwPlatformId;
1534 wcscpy( info->szCSDVersion, current_version.szCSDVersion );
1536 if (info->dwOSVersionInfoSize == sizeof(OSVERSIONINFOEXW))
1538 OSVERSIONINFOEXW *vex = (OSVERSIONINFOEXW *)info;
1539 vex->wServicePackMajor = current_version.wServicePackMajor;
1540 vex->wServicePackMinor = current_version.wServicePackMinor;
1541 vex->wSuiteMask = current_version.wSuiteMask;
1542 vex->wProductType = current_version.wProductType;
1544 return TRUE;
1548 /***********************************************************************
1549 * GetCurrentPackageFamilyName (kernelbase.@)
1551 LONG WINAPI /* DECLSPEC_HOTPATCH */ GetCurrentPackageFamilyName( UINT32 *length, WCHAR *name )
1553 FIXME( "(%p %p): stub\n", length, name );
1554 return APPMODEL_ERROR_NO_PACKAGE;
1558 /***********************************************************************
1559 * GetCurrentPackageFullName (kernelbase.@)
1561 LONG WINAPI /* DECLSPEC_HOTPATCH */ GetCurrentPackageFullName( UINT32 *length, WCHAR *name )
1563 FIXME( "(%p %p): stub\n", length, name );
1564 return APPMODEL_ERROR_NO_PACKAGE;
1568 /***********************************************************************
1569 * GetCurrentPackageId (kernelbase.@)
1571 LONG WINAPI /* DECLSPEC_HOTPATCH */ GetCurrentPackageId( UINT32 *len, BYTE *buffer )
1573 FIXME( "(%p %p): stub\n", len, buffer );
1574 return APPMODEL_ERROR_NO_PACKAGE;
1578 /***********************************************************************
1579 * GetCurrentPackagePath (kernelbase.@)
1581 LONG WINAPI /* DECLSPEC_HOTPATCH */ GetCurrentPackagePath( UINT32 *length, WCHAR *path )
1583 FIXME( "(%p %p): stub\n", length, path );
1584 return APPMODEL_ERROR_NO_PACKAGE;
1588 /***********************************************************************
1589 * GetPackageFullName (kernelbase.@)
1591 LONG WINAPI /* DECLSPEC_HOTPATCH */ GetPackageFullName( HANDLE process, UINT32 *length, WCHAR *name )
1593 FIXME( "(%p %p %p): stub\n", process, length, name );
1594 return APPMODEL_ERROR_NO_PACKAGE;
1598 /***********************************************************************
1599 * GetPackageFamilyName (kernelbase.@)
1601 LONG WINAPI /* DECLSPEC_HOTPATCH */ GetPackageFamilyName( HANDLE process, UINT32 *length, WCHAR *name )
1603 FIXME( "(%p %p %p): stub\n", process, length, name );
1604 return APPMODEL_ERROR_NO_PACKAGE;
1608 static const struct
1610 UINT32 code;
1611 const WCHAR *name;
1613 arch_names[] =
1615 {PROCESSOR_ARCHITECTURE_INTEL, L"x86"},
1616 {PROCESSOR_ARCHITECTURE_ARM, L"arm"},
1617 {PROCESSOR_ARCHITECTURE_AMD64, L"x64"},
1618 {PROCESSOR_ARCHITECTURE_NEUTRAL, L"neutral"},
1619 {PROCESSOR_ARCHITECTURE_ARM64, L"arm64"},
1620 {PROCESSOR_ARCHITECTURE_UNKNOWN, L"unknown"},
1623 static UINT32 processor_arch_from_string(const WCHAR *str, unsigned int len)
1625 unsigned int i;
1627 for (i = 0; i < ARRAY_SIZE(arch_names); ++i)
1628 if (lstrlenW(arch_names[i].name) == len && !wcsnicmp(str, arch_names[i].name, len))
1629 return arch_names[i].code;
1630 return ~0u;
1633 /***********************************************************************
1634 * PackageIdFromFullName (kernelbase.@)
1636 LONG WINAPI PackageIdFromFullName(const WCHAR *full_name, UINT32 flags, UINT32 *buffer_length, BYTE *buffer)
1638 const WCHAR *name, *version_str, *arch_str, *resource_id, *publisher_id, *s;
1639 PACKAGE_ID *id = (PACKAGE_ID *)buffer;
1640 UINT32 size, buffer_size, len;
1642 TRACE("full_name %s, flags %#x, buffer_length %p, buffer %p.\n",
1643 debugstr_w(full_name), flags, buffer_length, buffer);
1645 if (flags)
1646 FIXME("Flags %#x are not supported.\n", flags);
1648 if (!full_name || !buffer_length)
1649 return ERROR_INVALID_PARAMETER;
1651 if (!buffer && *buffer_length)
1652 return ERROR_INVALID_PARAMETER;
1654 name = full_name;
1655 if (!(version_str = wcschr(name, L'_')))
1656 return ERROR_INVALID_PARAMETER;
1657 ++version_str;
1659 if (!(arch_str = wcschr(version_str, L'_')))
1660 return ERROR_INVALID_PARAMETER;
1661 ++arch_str;
1663 if (!(resource_id = wcschr(arch_str, L'_')))
1664 return ERROR_INVALID_PARAMETER;
1665 ++resource_id;
1667 if (!(publisher_id = wcschr(resource_id, L'_')))
1668 return ERROR_INVALID_PARAMETER;
1669 ++publisher_id;
1671 /* Publisher id length should be 13. */
1672 size = sizeof(*id) + sizeof(WCHAR) * ((version_str - name) + (publisher_id - resource_id) + 13 + 1);
1673 buffer_size = *buffer_length;
1674 *buffer_length = size;
1675 if (buffer_size < size)
1676 return ERROR_INSUFFICIENT_BUFFER;
1678 memset(id, 0, sizeof(*id));
1679 if ((id->processorArchitecture = processor_arch_from_string(arch_str, resource_id - arch_str - 1)) == ~0u)
1681 FIXME("Unrecognized arch %s.\n", debugstr_w(arch_str));
1682 return ERROR_INVALID_PARAMETER;
1684 buffer += sizeof(*id);
1686 id->version.u.s.Major = wcstol(version_str, NULL, 10);
1687 if (!(s = wcschr(version_str, L'.')))
1688 return ERROR_INVALID_PARAMETER;
1689 ++s;
1690 id->version.u.s.Minor = wcstol(s, NULL, 10);
1691 if (!(s = wcschr(s, L'.')))
1692 return ERROR_INVALID_PARAMETER;
1693 ++s;
1694 id->version.u.s.Build = wcstol(s, NULL, 10);
1695 if (!(s = wcschr(s, L'.')))
1696 return ERROR_INVALID_PARAMETER;
1697 ++s;
1698 id->version.u.s.Revision = wcstol(s, NULL, 10);
1700 id->name = (WCHAR *)buffer;
1701 len = version_str - name - 1;
1702 memcpy(id->name, name, sizeof(*id->name) * len);
1703 id->name[len] = 0;
1704 buffer += sizeof(*id->name) * (len + 1);
1706 id->resourceId = (WCHAR *)buffer;
1707 len = publisher_id - resource_id - 1;
1708 memcpy(id->resourceId, resource_id, sizeof(*id->resourceId) * len);
1709 id->resourceId[len] = 0;
1710 buffer += sizeof(*id->resourceId) * (len + 1);
1712 id->publisherId = (WCHAR *)buffer;
1713 len = lstrlenW(publisher_id);
1714 if (len != 13)
1715 return ERROR_INVALID_PARAMETER;
1716 memcpy(id->publisherId, publisher_id, sizeof(*id->publisherId) * len);
1717 id->publisherId[len] = 0;
1719 return ERROR_SUCCESS;