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
31 #include <sys/types.h>
36 #define NONAMELESSUNION
37 #define NONAMELESSSTRUCT
45 #include "wine/unicode.h"
47 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(ver
);
69 /**********************************************************************
72 * Find an entry by id in a resource directory
73 * Copied from loader/pe_resource.c
75 static const IMAGE_RESOURCE_DIRECTORY
*find_entry_by_id( const IMAGE_RESOURCE_DIRECTORY
*dir
,
76 WORD id
, const void *root
)
78 const IMAGE_RESOURCE_DIRECTORY_ENTRY
*entry
;
81 entry
= (const IMAGE_RESOURCE_DIRECTORY_ENTRY
*)(dir
+ 1);
82 min
= dir
->NumberOfNamedEntries
;
83 max
= min
+ dir
->NumberOfIdEntries
- 1;
86 pos
= (min
+ max
) / 2;
87 if (entry
[pos
].u1
.s2
.Id
== id
)
88 return (const IMAGE_RESOURCE_DIRECTORY
*)((const char *)root
+ entry
[pos
].u2
.s3
.OffsetToDirectory
);
89 if (entry
[pos
].u1
.s2
.Id
> id
) max
= pos
- 1;
96 /**********************************************************************
99 * Find a default entry in a resource directory
100 * Copied from loader/pe_resource.c
102 static const IMAGE_RESOURCE_DIRECTORY
*find_entry_default( const IMAGE_RESOURCE_DIRECTORY
*dir
,
105 const IMAGE_RESOURCE_DIRECTORY_ENTRY
*entry
;
107 entry
= (const IMAGE_RESOURCE_DIRECTORY_ENTRY
*)(dir
+ 1);
108 return (const IMAGE_RESOURCE_DIRECTORY
*)((const char *)root
+ entry
->u2
.s3
.OffsetToDirectory
);
112 /***********************************************************************
113 * read_xx_header [internal]
115 static int read_xx_header( HFILE lzfd
)
117 IMAGE_DOS_HEADER mzh
;
120 LZSeek( lzfd
, 0, SEEK_SET
);
121 if ( sizeof(mzh
) != LZRead( lzfd
, (LPSTR
)&mzh
, sizeof(mzh
) ) )
123 if ( mzh
.e_magic
!= IMAGE_DOS_SIGNATURE
)
125 if (!memcmp( &mzh
, "\177ELF", 4 )) return 1; /* ELF */
126 if (*(UINT
*)&mzh
== 0xfeedface || *(UINT
*)&mzh
== 0xcefaedfe) return 1; /* Mach-O */
130 LZSeek( lzfd
, mzh
.e_lfanew
, SEEK_SET
);
131 if ( 2 != LZRead( lzfd
, magic
, 2 ) )
134 LZSeek( lzfd
, mzh
.e_lfanew
, SEEK_SET
);
136 if ( magic
[0] == 'N' && magic
[1] == 'E' )
137 return IMAGE_OS2_SIGNATURE
;
138 if ( magic
[0] == 'P' && magic
[1] == 'E' )
139 return IMAGE_NT_SIGNATURE
;
142 WARN("Can't handle %s files.\n", magic
);
146 /***********************************************************************
147 * find_ne_resource [internal]
149 static BOOL
find_ne_resource( HFILE lzfd
, DWORD
*resLen
, DWORD
*resOff
)
151 const WORD
typeid = VS_FILE_INFO
| 0x8000;
152 const WORD resid
= VS_VERSION_INFO
| 0x8000;
153 IMAGE_OS2_HEADER nehd
;
154 NE_TYPEINFO
*typeInfo
;
155 NE_NAMEINFO
*nameInfo
;
161 /* Read in NE header */
162 nehdoffset
= LZSeek( lzfd
, 0, SEEK_CUR
);
163 if ( sizeof(nehd
) != LZRead( lzfd
, (LPSTR
)&nehd
, sizeof(nehd
) ) ) return 0;
165 resTabSize
= nehd
.ne_restab
- nehd
.ne_rsrctab
;
168 TRACE("No resources in NE dll\n" );
172 /* Read in resource table */
173 resTab
= HeapAlloc( GetProcessHeap(), 0, resTabSize
);
174 if ( !resTab
) return FALSE
;
176 LZSeek( lzfd
, nehd
.ne_rsrctab
+ nehdoffset
, SEEK_SET
);
177 if ( resTabSize
!= LZRead( lzfd
, (char*)resTab
, resTabSize
) )
179 HeapFree( GetProcessHeap(), 0, resTab
);
184 typeInfo
= (NE_TYPEINFO
*)(resTab
+ 2);
185 while (typeInfo
->type_id
)
187 if (typeInfo
->type_id
== typeid) goto found_type
;
188 typeInfo
= (NE_TYPEINFO
*)((char *)(typeInfo
+ 1) +
189 typeInfo
->count
* sizeof(NE_NAMEINFO
));
191 TRACE("No typeid entry found\n" );
192 HeapFree( GetProcessHeap(), 0, resTab
);
196 nameInfo
= (NE_NAMEINFO
*)(typeInfo
+ 1);
198 for (count
= typeInfo
->count
; count
> 0; count
--, nameInfo
++)
199 if (nameInfo
->id
== resid
) goto found_name
;
201 TRACE("No resid entry found\n" );
202 HeapFree( GetProcessHeap(), 0, resTab
);
206 /* Return resource data */
207 if ( resLen
) *resLen
= nameInfo
->length
<< *(WORD
*)resTab
;
208 if ( resOff
) *resOff
= nameInfo
->offset
<< *(WORD
*)resTab
;
210 HeapFree( GetProcessHeap(), 0, resTab
);
214 /***********************************************************************
215 * find_pe_resource [internal]
217 static BOOL
find_pe_resource( HFILE lzfd
, DWORD
*resLen
, DWORD
*resOff
)
221 IMAGE_NT_HEADERS32 nt32
;
222 IMAGE_NT_HEADERS64 nt64
;
225 PIMAGE_DATA_DIRECTORY resDataDir
;
226 PIMAGE_SECTION_HEADER sections
;
228 DWORD section_size
, data_size
;
230 const IMAGE_RESOURCE_DIRECTORY
*resPtr
;
231 const IMAGE_RESOURCE_DATA_ENTRY
*resData
;
232 int i
, len
, nSections
;
235 /* Read in PE header */
236 pehdoffset
= LZSeek( lzfd
, 0, SEEK_CUR
);
237 len
= LZRead( lzfd
, (LPSTR
)&pehd
, sizeof(pehd
) );
238 if (len
< sizeof(pehd
.nt32
.FileHeader
)) return 0;
239 if (len
< sizeof(pehd
)) memset( (char *)&pehd
+ len
, 0, sizeof(pehd
) - len
);
241 switch (pehd
.nt32
.OptionalHeader
.Magic
)
243 case IMAGE_NT_OPTIONAL_HDR32_MAGIC
:
244 resDataDir
= pehd
.nt32
.OptionalHeader
.DataDirectory
+ IMAGE_DIRECTORY_ENTRY_RESOURCE
;
246 case IMAGE_NT_OPTIONAL_HDR64_MAGIC
:
247 resDataDir
= pehd
.nt64
.OptionalHeader
.DataDirectory
+ IMAGE_DIRECTORY_ENTRY_RESOURCE
;
253 if ( !resDataDir
->Size
)
255 TRACE("No resources in PE dll\n" );
259 /* Read in section table */
260 nSections
= pehd
.nt32
.FileHeader
.NumberOfSections
;
261 sections
= HeapAlloc( GetProcessHeap(), 0,
262 nSections
* sizeof(IMAGE_SECTION_HEADER
) );
263 if ( !sections
) return FALSE
;
265 len
= FIELD_OFFSET( IMAGE_NT_HEADERS32
, OptionalHeader
) + pehd
.nt32
.FileHeader
.SizeOfOptionalHeader
;
266 LZSeek( lzfd
, pehdoffset
+ len
, SEEK_SET
);
268 if ( nSections
* sizeof(IMAGE_SECTION_HEADER
) !=
269 LZRead( lzfd
, (LPSTR
)sections
, nSections
* sizeof(IMAGE_SECTION_HEADER
) ) )
271 HeapFree( GetProcessHeap(), 0, sections
);
275 /* Find resource section */
276 for ( i
= 0; i
< nSections
; i
++ )
277 if ( resDataDir
->VirtualAddress
>= sections
[i
].VirtualAddress
278 && resDataDir
->VirtualAddress
< sections
[i
].VirtualAddress
+
279 sections
[i
].SizeOfRawData
)
282 if ( i
== nSections
)
284 HeapFree( GetProcessHeap(), 0, sections
);
285 TRACE("Couldn't find resource section\n" );
289 /* Read in resource section */
290 data_size
= sections
[i
].SizeOfRawData
;
291 section_size
= max( data_size
, sections
[i
].Misc
.VirtualSize
);
292 resSection
= HeapAlloc( GetProcessHeap(), 0, section_size
);
295 HeapFree( GetProcessHeap(), 0, sections
);
299 LZSeek( lzfd
, sections
[i
].PointerToRawData
, SEEK_SET
);
300 if (data_size
!= LZRead( lzfd
, (char*)resSection
, data_size
)) goto done
;
301 if (data_size
< section_size
) memset( (char *)resSection
+ data_size
, 0, section_size
- data_size
);
304 resDir
= resSection
+ (resDataDir
->VirtualAddress
- sections
[i
].VirtualAddress
);
307 resPtr
= find_entry_by_id( resPtr
, VS_FILE_INFO
, resDir
);
310 TRACE("No typeid entry found\n" );
313 resPtr
= find_entry_by_id( resPtr
, VS_VERSION_INFO
, resDir
);
316 TRACE("No resid entry found\n" );
319 resPtr
= find_entry_default( resPtr
, resDir
);
322 TRACE("No default language entry found\n" );
326 /* Find resource data section */
327 resData
= (const IMAGE_RESOURCE_DATA_ENTRY
*)resPtr
;
328 for ( i
= 0; i
< nSections
; i
++ )
329 if ( resData
->OffsetToData
>= sections
[i
].VirtualAddress
330 && resData
->OffsetToData
< sections
[i
].VirtualAddress
+
331 sections
[i
].SizeOfRawData
)
334 if ( i
== nSections
)
336 TRACE("Couldn't find resource data section\n" );
340 /* Return resource data */
341 if ( resLen
) *resLen
= resData
->Size
;
342 if ( resOff
) *resOff
= resData
->OffsetToData
- sections
[i
].VirtualAddress
343 + sections
[i
].PointerToRawData
;
347 HeapFree( GetProcessHeap(), 0, resSection
);
348 HeapFree( GetProcessHeap(), 0, sections
);
353 /***********************************************************************
354 * find_version_resource [internal]
356 static DWORD
find_version_resource( HFILE lzfd
, DWORD
*reslen
, DWORD
*offset
)
358 DWORD magic
= read_xx_header( lzfd
);
362 case IMAGE_OS2_SIGNATURE
:
363 if (!find_ne_resource( lzfd
, reslen
, offset
)) magic
= 0;
365 case IMAGE_NT_SIGNATURE
:
366 if (!find_pe_resource( lzfd
, reslen
, offset
)) magic
= 0;
372 /******************************************************************************
374 * This function will print via standard TRACE, debug info regarding
375 * the file info structure vffi.
376 * 15-Feb-1998 Dimitrie Paun (dimi@cs.toronto.edu)
377 * Added this function to clean up the code.
379 *****************************************************************************/
380 static void print_vffi_debug(const VS_FIXEDFILEINFO
*vffi
)
382 BOOL versioned_printer
= FALSE
;
384 if((vffi
->dwFileType
== VFT_DLL
) || (vffi
->dwFileType
== VFT_DRV
))
386 if(vffi
->dwFileSubtype
== VFT2_DRV_VERSIONED_PRINTER
)
387 /* this is documented for newer w2k Drivers and up */
388 versioned_printer
= TRUE
;
389 else if( (vffi
->dwFileSubtype
== VFT2_DRV_PRINTER
) &&
390 (vffi
->dwFileVersionMS
!= vffi
->dwProductVersionMS
) &&
391 (vffi
->dwFileVersionMS
> 0) &&
392 (vffi
->dwFileVersionMS
<= 3) )
393 /* found this on NT 3.51, NT4.0 and old w2k Drivers */
394 versioned_printer
= TRUE
;
397 TRACE("structversion=%u.%u, ",
398 HIWORD(vffi
->dwStrucVersion
),LOWORD(vffi
->dwStrucVersion
));
399 if(versioned_printer
)
401 WORD mode
= LOWORD(vffi
->dwFileVersionMS
);
402 WORD ver_rev
= HIWORD(vffi
->dwFileVersionLS
);
403 TRACE("fileversion=%u.%u.%u.%u (%s.major.minor.release), ",
404 (vffi
->dwFileVersionMS
),
405 HIBYTE(ver_rev
), LOBYTE(ver_rev
), LOWORD(vffi
->dwFileVersionLS
),
406 (mode
== 3) ? "Usermode" : ((mode
<= 2) ? "Kernelmode" : "?") );
410 TRACE("fileversion=%u.%u.%u.%u, ",
411 HIWORD(vffi
->dwFileVersionMS
),LOWORD(vffi
->dwFileVersionMS
),
412 HIWORD(vffi
->dwFileVersionLS
),LOWORD(vffi
->dwFileVersionLS
));
414 TRACE("productversion=%u.%u.%u.%u\n",
415 HIWORD(vffi
->dwProductVersionMS
),LOWORD(vffi
->dwProductVersionMS
),
416 HIWORD(vffi
->dwProductVersionLS
),LOWORD(vffi
->dwProductVersionLS
));
418 TRACE("flagmask=0x%x, flags=0x%x %s%s%s%s%s%s\n",
419 vffi
->dwFileFlagsMask
, vffi
->dwFileFlags
,
420 (vffi
->dwFileFlags
& VS_FF_DEBUG
) ? "DEBUG," : "",
421 (vffi
->dwFileFlags
& VS_FF_PRERELEASE
) ? "PRERELEASE," : "",
422 (vffi
->dwFileFlags
& VS_FF_PATCHED
) ? "PATCHED," : "",
423 (vffi
->dwFileFlags
& VS_FF_PRIVATEBUILD
) ? "PRIVATEBUILD," : "",
424 (vffi
->dwFileFlags
& VS_FF_INFOINFERRED
) ? "INFOINFERRED," : "",
425 (vffi
->dwFileFlags
& VS_FF_SPECIALBUILD
) ? "SPECIALBUILD," : "");
429 TRACE("OS=0x%x.0x%x ", HIWORD(vffi
->dwFileOS
), LOWORD(vffi
->dwFileOS
));
431 switch (vffi
->dwFileOS
&0xFFFF0000)
433 case VOS_DOS
:TRACE("DOS,");break;
434 case VOS_OS216
:TRACE("OS/2-16,");break;
435 case VOS_OS232
:TRACE("OS/2-32,");break;
436 case VOS_NT
:TRACE("NT,");break;
439 TRACE("UNKNOWN(0x%x),",vffi
->dwFileOS
&0xFFFF0000);break;
442 switch (LOWORD(vffi
->dwFileOS
))
444 case VOS__BASE
:TRACE("BASE");break;
445 case VOS__WINDOWS16
:TRACE("WIN16");break;
446 case VOS__WINDOWS32
:TRACE("WIN32");break;
447 case VOS__PM16
:TRACE("PM16");break;
448 case VOS__PM32
:TRACE("PM32");break;
450 TRACE("UNKNOWN(0x%x)",LOWORD(vffi
->dwFileOS
));break;
455 switch (vffi
->dwFileType
)
457 case VFT_APP
:TRACE("filetype=APP");break;
459 TRACE("filetype=DLL");
460 if(vffi
->dwFileSubtype
!= 0)
462 if(versioned_printer
) /* NT3.x/NT4.0 or old w2k Driver */
464 TRACE(" (subtype=0x%x)", vffi
->dwFileSubtype
);
468 TRACE("filetype=DRV,");
469 switch(vffi
->dwFileSubtype
)
471 case VFT2_DRV_PRINTER
:TRACE("PRINTER");break;
472 case VFT2_DRV_KEYBOARD
:TRACE("KEYBOARD");break;
473 case VFT2_DRV_LANGUAGE
:TRACE("LANGUAGE");break;
474 case VFT2_DRV_DISPLAY
:TRACE("DISPLAY");break;
475 case VFT2_DRV_MOUSE
:TRACE("MOUSE");break;
476 case VFT2_DRV_NETWORK
:TRACE("NETWORK");break;
477 case VFT2_DRV_SYSTEM
:TRACE("SYSTEM");break;
478 case VFT2_DRV_INSTALLABLE
:TRACE("INSTALLABLE");break;
479 case VFT2_DRV_SOUND
:TRACE("SOUND");break;
480 case VFT2_DRV_COMM
:TRACE("COMM");break;
481 case VFT2_DRV_INPUTMETHOD
:TRACE("INPUTMETHOD");break;
482 case VFT2_DRV_VERSIONED_PRINTER
:TRACE("VERSIONED_PRINTER");break;
485 TRACE("UNKNOWN(0x%x)",vffi
->dwFileSubtype
);break;
489 TRACE("filetype=FONT,");
490 switch (vffi
->dwFileSubtype
)
492 case VFT2_FONT_RASTER
:TRACE("RASTER");break;
493 case VFT2_FONT_VECTOR
:TRACE("VECTOR");break;
494 case VFT2_FONT_TRUETYPE
:TRACE("TRUETYPE");break;
495 default:TRACE("UNKNOWN(0x%x)",vffi
->dwFileSubtype
);break;
498 case VFT_VXD
:TRACE("filetype=VXD");break;
499 case VFT_STATIC_LIB
:TRACE("filetype=STATIC_LIB");break;
502 TRACE("filetype=Unknown(0x%x)",vffi
->dwFileType
);break;
506 TRACE("filedate=0x%x.0x%x\n",vffi
->dwFileDateMS
,vffi
->dwFileDateLS
);
509 /***********************************************************************
510 * Version Info Structure
518 #if 0 /* variable length structure */
522 VS_VERSION_INFO_STRUCT16 Children
[];
524 } VS_VERSION_INFO_STRUCT16
;
532 #if 0 /* variable length structure */
536 VS_VERSION_INFO_STRUCT32 Children
[];
538 } VS_VERSION_INFO_STRUCT32
;
540 #define VersionInfoIs16( ver ) \
541 ( ((const VS_VERSION_INFO_STRUCT16 *)ver)->szKey[0] >= ' ' )
543 #define DWORD_ALIGN( base, ptr ) \
544 ( (LPBYTE)(base) + ((((LPBYTE)(ptr) - (LPBYTE)(base)) + 3) & ~3) )
546 #define VersionInfo16_Value( ver ) \
547 DWORD_ALIGN( (ver), (ver)->szKey + strlen((ver)->szKey) + 1 )
548 #define VersionInfo32_Value( ver ) \
549 DWORD_ALIGN( (ver), (ver)->szKey + strlenW((ver)->szKey) + 1 )
551 #define VersionInfo16_Children( ver ) \
552 (const VS_VERSION_INFO_STRUCT16 *)( VersionInfo16_Value( ver ) + \
553 ( ( (ver)->wValueLength + 3 ) & ~3 ) )
554 #define VersionInfo32_Children( ver ) \
555 (const VS_VERSION_INFO_STRUCT32 *)( VersionInfo32_Value( ver ) + \
556 ( ( (ver)->wValueLength * \
557 ((ver)->wType? 2 : 1) + 3 ) & ~3 ) )
559 #define VersionInfo16_Next( ver ) \
560 (VS_VERSION_INFO_STRUCT16 *)( (LPBYTE)ver + (((ver)->wLength + 3) & ~3) )
561 #define VersionInfo32_Next( ver ) \
562 (VS_VERSION_INFO_STRUCT32 *)( (LPBYTE)ver + (((ver)->wLength + 3) & ~3) )
565 /***********************************************************************
566 * GetFileVersionInfoSizeW [VERSION.@]
568 DWORD WINAPI
GetFileVersionInfoSizeW( LPCWSTR filename
, LPDWORD handle
)
570 DWORD len
, offset
, magic
= 1;
575 TRACE("(%s,%p)\n", debugstr_w(filename
), handle
);
577 if (handle
) *handle
= 0;
581 SetLastError(ERROR_INVALID_PARAMETER
);
586 SetLastError(ERROR_BAD_PATHNAME
);
590 if ((lzfd
= LZOpenFileW( (LPWSTR
)filename
, &ofs
, OF_READ
)) != HFILE_ERROR
)
592 magic
= find_version_resource( lzfd
, &len
, &offset
);
596 if ((magic
== 1) && (hModule
= LoadLibraryExW( filename
, 0, LOAD_LIBRARY_AS_DATAFILE
)))
598 HRSRC hRsrc
= FindResourceW( hModule
, MAKEINTRESOURCEW(VS_VERSION_INFO
),
599 MAKEINTRESOURCEW(VS_FILE_INFO
) );
602 magic
= IMAGE_NT_SIGNATURE
;
603 len
= SizeofResource( hModule
, hRsrc
);
605 FreeLibrary( hModule
);
610 case IMAGE_OS2_SIGNATURE
:
611 /* We have a 16bit resource.
613 * XP/W2K/W2K3 uses a buffer which is more than the actual needed space:
615 * (info->wLength - sizeof(VS_FIXEDFILEINFO)) * 4
617 * This extra buffer is used for ANSI to Unicode conversions in W-Calls.
618 * info->wLength should be the same as len. Currently it isn't but that
619 * doesn't seem to be a problem (len is bigger than info->wLength).
622 return (len
- sizeof(VS_FIXEDFILEINFO
)) * 4;
624 case IMAGE_NT_SIGNATURE
:
625 /* We have a 32bit resource.
627 * XP/W2K/W2K3 uses a buffer which is 2 times the actual needed space + 4 bytes "FE2X"
628 * This extra buffer is used for Unicode to ANSI conversions in A-Calls
631 return (len
* 2) + 4;
634 SetLastError( lzfd
== HFILE_ERROR
? ofs
.nErrCode
: ERROR_RESOURCE_DATA_NOT_FOUND
);
639 /***********************************************************************
640 * GetFileVersionInfoSizeA [VERSION.@]
642 DWORD WINAPI
GetFileVersionInfoSizeA( LPCSTR filename
, LPDWORD handle
)
644 UNICODE_STRING filenameW
;
647 TRACE("(%s,%p)\n", debugstr_a(filename
), handle
);
650 RtlCreateUnicodeStringFromAsciiz(&filenameW
, filename
);
652 filenameW
.Buffer
= NULL
;
654 retval
= GetFileVersionInfoSizeW(filenameW
.Buffer
, handle
);
656 RtlFreeUnicodeString(&filenameW
);
661 /***********************************************************************
662 * GetFileVersionInfoW [VERSION.@]
664 BOOL WINAPI
GetFileVersionInfoW( LPCWSTR filename
, DWORD handle
,
665 DWORD datasize
, LPVOID data
)
667 static const char signature
[4] = "FE2X";
668 DWORD len
, offset
, magic
= 1;
672 VS_VERSION_INFO_STRUCT32
* vvis
= data
;
674 TRACE("(%s,%d,size=%d,data=%p)\n",
675 debugstr_w(filename
), handle
, datasize
, data
);
679 SetLastError(ERROR_INVALID_DATA
);
683 if ((lzfd
= LZOpenFileW( (LPWSTR
)filename
, &ofs
, OF_READ
)) != HFILE_ERROR
)
685 if ((magic
= find_version_resource( lzfd
, &len
, &offset
)) > 1)
687 LZSeek( lzfd
, offset
, 0 /* SEEK_SET */ );
688 len
= LZRead( lzfd
, data
, min( len
, datasize
) );
693 if ((magic
== 1) && (hModule
= LoadLibraryExW( filename
, 0, LOAD_LIBRARY_AS_DATAFILE
)))
695 HRSRC hRsrc
= FindResourceW( hModule
, MAKEINTRESOURCEW(VS_VERSION_INFO
),
696 MAKEINTRESOURCEW(VS_FILE_INFO
) );
699 HGLOBAL hMem
= LoadResource( hModule
, hRsrc
);
700 magic
= IMAGE_NT_SIGNATURE
;
701 len
= min( SizeofResource(hModule
, hRsrc
), datasize
);
702 memcpy( data
, LockResource( hMem
), len
);
703 FreeResource( hMem
);
705 FreeLibrary( hModule
);
710 case IMAGE_OS2_SIGNATURE
:
711 /* We have a 16bit resource. */
713 print_vffi_debug( (VS_FIXEDFILEINFO
*)VersionInfo16_Value( (VS_VERSION_INFO_STRUCT16
*)data
));
717 case IMAGE_NT_SIGNATURE
:
718 /* We have a 32bit resource.
720 * XP/W2K/W2K3 uses a buffer which is 2 times the actual needed space + 4 bytes "FE2X"
721 * This extra buffer is used for Unicode to ANSI conversions in A-Calls
723 len
= vvis
->wLength
+ sizeof(signature
);
724 if (datasize
>= len
) memcpy( (char*)data
+ vvis
->wLength
, signature
, sizeof(signature
) );
726 print_vffi_debug( (VS_FIXEDFILEINFO
*)VersionInfo32_Value( vvis
));
731 SetLastError( lzfd
== HFILE_ERROR
? ofs
.nErrCode
: ERROR_RESOURCE_DATA_NOT_FOUND
);
736 /***********************************************************************
737 * GetFileVersionInfoA [VERSION.@]
739 BOOL WINAPI
GetFileVersionInfoA( LPCSTR filename
, DWORD handle
,
740 DWORD datasize
, LPVOID data
)
742 UNICODE_STRING filenameW
;
745 TRACE("(%s,%d,size=%d,data=%p)\n",
746 debugstr_a(filename
), handle
, datasize
, data
);
749 RtlCreateUnicodeStringFromAsciiz(&filenameW
, filename
);
751 filenameW
.Buffer
= NULL
;
753 retval
= GetFileVersionInfoW(filenameW
.Buffer
, handle
, datasize
, data
);
755 RtlFreeUnicodeString(&filenameW
);
760 /***********************************************************************
761 * VersionInfo16_FindChild [internal]
763 static const VS_VERSION_INFO_STRUCT16
*VersionInfo16_FindChild( const VS_VERSION_INFO_STRUCT16
*info
,
764 LPCSTR szKey
, UINT cbKey
)
766 const VS_VERSION_INFO_STRUCT16
*child
= VersionInfo16_Children( info
);
768 while ((char *)child
< (char *)info
+ info
->wLength
)
770 if (!strncasecmp( child
->szKey
, szKey
, cbKey
) && !child
->szKey
[cbKey
])
773 if (!(child
->wLength
)) return NULL
;
774 child
= VersionInfo16_Next( child
);
780 /***********************************************************************
781 * VersionInfo32_FindChild [internal]
783 static const VS_VERSION_INFO_STRUCT32
*VersionInfo32_FindChild( const VS_VERSION_INFO_STRUCT32
*info
,
784 LPCWSTR szKey
, UINT cbKey
)
786 const VS_VERSION_INFO_STRUCT32
*child
= VersionInfo32_Children( info
);
788 while ((char *)child
< (char *)info
+ info
->wLength
)
790 if (!strncmpiW( child
->szKey
, szKey
, cbKey
) && !child
->szKey
[cbKey
])
793 if (!(child
->wLength
)) return NULL
;
794 child
= VersionInfo32_Next( child
);
800 /***********************************************************************
801 * VersionInfo16_QueryValue [internal]
803 * Gets a value from a 16-bit NE resource
805 static BOOL
VersionInfo16_QueryValue( const VS_VERSION_INFO_STRUCT16
*info
, LPCSTR lpSubBlock
,
806 LPVOID
*lplpBuffer
, UINT
*puLen
)
808 while ( *lpSubBlock
)
810 /* Find next path component */
812 for ( lpNextSlash
= lpSubBlock
; *lpNextSlash
; lpNextSlash
++ )
813 if ( *lpNextSlash
== '\\' )
816 /* Skip empty components */
817 if ( lpNextSlash
== lpSubBlock
)
823 /* We have a non-empty component: search info for key */
824 info
= VersionInfo16_FindChild( info
, lpSubBlock
, lpNextSlash
-lpSubBlock
);
827 if (puLen
) *puLen
= 0 ;
828 SetLastError( ERROR_RESOURCE_TYPE_NOT_FOUND
);
832 /* Skip path component */
833 lpSubBlock
= lpNextSlash
;
837 *lplpBuffer
= VersionInfo16_Value( info
);
839 *puLen
= info
->wValueLength
;
844 /***********************************************************************
845 * VersionInfo32_QueryValue [internal]
847 * Gets a value from a 32-bit PE resource
849 static BOOL
VersionInfo32_QueryValue( const VS_VERSION_INFO_STRUCT32
*info
, LPCWSTR lpSubBlock
,
850 LPVOID
*lplpBuffer
, UINT
*puLen
)
852 TRACE("lpSubBlock : (%s)\n", debugstr_w(lpSubBlock
));
854 while ( *lpSubBlock
)
856 /* Find next path component */
858 for ( lpNextSlash
= lpSubBlock
; *lpNextSlash
; lpNextSlash
++ )
859 if ( *lpNextSlash
== '\\' )
862 /* Skip empty components */
863 if ( lpNextSlash
== lpSubBlock
)
869 /* We have a non-empty component: search info for key */
870 info
= VersionInfo32_FindChild( info
, lpSubBlock
, lpNextSlash
-lpSubBlock
);
873 if (puLen
) *puLen
= 0 ;
874 SetLastError( ERROR_RESOURCE_TYPE_NOT_FOUND
);
878 /* Skip path component */
879 lpSubBlock
= lpNextSlash
;
883 *lplpBuffer
= VersionInfo32_Value( info
);
885 *puLen
= info
->wValueLength
;
890 /***********************************************************************
891 * VerQueryValueA [VERSION.@]
893 BOOL WINAPI
VerQueryValueA( LPCVOID pBlock
, LPCSTR lpSubBlock
,
894 LPVOID
*lplpBuffer
, PUINT puLen
)
896 static const char rootA
[] = "\\";
897 static const char varfileinfoA
[] = "\\VarFileInfo\\Translation";
898 const VS_VERSION_INFO_STRUCT16
*info
= pBlock
;
900 TRACE("(%p,%s,%p,%p)\n",
901 pBlock
, debugstr_a(lpSubBlock
), lplpBuffer
, puLen
);
906 if (lpSubBlock
== NULL
|| lpSubBlock
[0] == '\0')
909 if ( !VersionInfoIs16( info
) )
915 len
= MultiByteToWideChar(CP_ACP
, 0, lpSubBlock
, -1, NULL
, 0);
916 lpSubBlockW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
921 MultiByteToWideChar(CP_ACP
, 0, lpSubBlock
, -1, lpSubBlockW
, len
);
923 ret
= VersionInfo32_QueryValue(pBlock
, lpSubBlockW
, lplpBuffer
, puLen
);
925 HeapFree(GetProcessHeap(), 0, lpSubBlockW
);
927 if (ret
&& strcasecmp( lpSubBlock
, rootA
) && strcasecmp( lpSubBlock
, varfileinfoA
))
929 /* Set lpBuffer so it points to the 'empty' area where we store
930 * the converted strings
932 LPSTR lpBufferA
= (LPSTR
)pBlock
+ info
->wLength
+ 4;
933 DWORD pos
= (LPCSTR
)*lplpBuffer
- (LPCSTR
)pBlock
;
935 len
= WideCharToMultiByte(CP_ACP
, 0, *lplpBuffer
, -1,
936 lpBufferA
+ pos
, info
->wLength
- pos
, NULL
, NULL
);
937 *lplpBuffer
= lpBufferA
+ pos
;
943 return VersionInfo16_QueryValue(info
, lpSubBlock
, lplpBuffer
, puLen
);
946 /***********************************************************************
947 * VerQueryValueW [VERSION.@]
949 BOOL WINAPI
VerQueryValueW( LPCVOID pBlock
, LPCWSTR lpSubBlock
,
950 LPVOID
*lplpBuffer
, PUINT puLen
)
952 static const WCHAR nullW
[] = { 0 };
953 static const WCHAR rootW
[] = { '\\', 0 };
954 static const WCHAR varfileinfoW
[] = { '\\','V','a','r','F','i','l','e','I','n','f','o',
955 '\\','T','r','a','n','s','l','a','t','i','o','n', 0 };
957 const VS_VERSION_INFO_STRUCT32
*info
= pBlock
;
959 TRACE("(%p,%s,%p,%p)\n",
960 pBlock
, debugstr_w(lpSubBlock
), lplpBuffer
, puLen
);
965 if (lpSubBlock
== NULL
|| lpSubBlock
[0] == nullW
[0])
968 if ( VersionInfoIs16( info
) )
974 len
= WideCharToMultiByte(CP_ACP
, 0, lpSubBlock
, -1, NULL
, 0, NULL
, NULL
);
975 lpSubBlockA
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(char));
980 WideCharToMultiByte(CP_ACP
, 0, lpSubBlock
, -1, lpSubBlockA
, len
, NULL
, NULL
);
982 ret
= VersionInfo16_QueryValue(pBlock
, lpSubBlockA
, lplpBuffer
, puLen
);
984 HeapFree(GetProcessHeap(), 0, lpSubBlockA
);
986 if (ret
&& strcmpiW( lpSubBlock
, rootW
) && strcmpiW( lpSubBlock
, varfileinfoW
))
988 /* Set lpBuffer so it points to the 'empty' area where we store
989 * the converted strings
991 LPWSTR lpBufferW
= (LPWSTR
)((LPSTR
)pBlock
+ info
->wLength
);
992 DWORD pos
= (LPCSTR
)*lplpBuffer
- (LPCSTR
)pBlock
;
993 DWORD max
= (info
->wLength
- sizeof(VS_FIXEDFILEINFO
)) * 4 - info
->wLength
;
995 len
= MultiByteToWideChar(CP_ACP
, 0, *lplpBuffer
, -1,
996 lpBufferW
+ pos
, max
/sizeof(WCHAR
) - pos
);
997 *lplpBuffer
= lpBufferW
+ pos
;
1003 return VersionInfo32_QueryValue(info
, lpSubBlock
, lplpBuffer
, puLen
);
1007 /******************************************************************************
1008 * testFileExistenceA
1010 * Tests whether a given path/file combination exists. If the file does
1011 * not exist, the return value is zero. If it does exist, the return
1012 * value is non-zero.
1015 * 30-May-1997 Dave Cuthbert (dacut@ece.cmu.edu)
1016 * Original implementation
1019 static int testFileExistenceA( char const * path
, char const * file
, BOOL excl
)
1021 char filename
[1024];
1025 fileinfo
.cBytes
= sizeof(OFSTRUCT
);
1027 strcpy(filename
, path
);
1028 filenamelen
= strlen(filename
);
1030 /* Add a trailing \ if necessary */
1032 if(filename
[filenamelen
- 1] != '\\')
1033 strcat(filename
, "\\");
1035 else /* specify the current directory */
1036 strcpy(filename
, ".\\");
1038 /* Create the full pathname */
1039 strcat(filename
, file
);
1041 return (OpenFile(filename
, &fileinfo
,
1042 OF_EXIST
| (excl
? OF_SHARE_EXCLUSIVE
: 0)) != HFILE_ERROR
);
1045 /******************************************************************************
1046 * testFileExistenceW
1048 static int testFileExistenceW( const WCHAR
*path
, const WCHAR
*file
, BOOL excl
)
1051 DWORD pathlen
, filelen
;
1055 fileinfo
.cBytes
= sizeof(OFSTRUCT
);
1057 pathlen
= WideCharToMultiByte( CP_ACP
, 0, path
, -1, NULL
, 0, NULL
, NULL
);
1058 filelen
= WideCharToMultiByte( CP_ACP
, 0, file
, -1, NULL
, 0, NULL
, NULL
);
1059 filename
= HeapAlloc( GetProcessHeap(), 0, pathlen
+filelen
+2 );
1061 WideCharToMultiByte( CP_ACP
, 0, path
, -1, filename
, pathlen
, NULL
, NULL
);
1062 /* Add a trailing \ if necessary */
1065 if (filename
[pathlen
-2] != '\\') strcpy( &filename
[pathlen
-1], "\\" );
1067 else /* specify the current directory */
1068 strcpy(filename
, ".\\");
1070 WideCharToMultiByte( CP_ACP
, 0, file
, -1, filename
+strlen(filename
), filelen
, NULL
, NULL
);
1072 ret
= (OpenFile(filename
, &fileinfo
,
1073 OF_EXIST
| (excl
? OF_SHARE_EXCLUSIVE
: 0)) != HFILE_ERROR
);
1074 HeapFree( GetProcessHeap(), 0, filename
);
1078 /*****************************************************************************
1079 * VerFindFileA [VERSION.@]
1081 * Determines where to install a file based on whether it locates another
1082 * version of the file in the system. The values VerFindFile returns are
1083 * used in a subsequent call to the VerInstallFile function.
1086 * 30-May-1997 Dave Cuthbert (dacut@ece.cmu.edu)
1087 * Reimplementation of VerFindFile from original stub.
1089 DWORD WINAPI
VerFindFileA(
1091 LPCSTR lpszFilename
,
1097 PUINT lpuDestDirLen
)
1101 const char *destDir
;
1102 unsigned int curDirSizeReq
;
1103 unsigned int destDirSizeReq
;
1104 char systemDir
[MAX_PATH
];
1106 /* Print out debugging information */
1107 TRACE("flags = %x filename=%s windir=%s appdir=%s curdirlen=%p(%u) destdirlen=%p(%u)\n",
1108 flags
, debugstr_a(lpszFilename
), debugstr_a(lpszWinDir
), debugstr_a(lpszAppDir
),
1109 lpuCurDirLen
, lpuCurDirLen
? *lpuCurDirLen
: 0,
1110 lpuDestDirLen
, lpuDestDirLen
? *lpuDestDirLen
: 0 );
1112 /* Figure out where the file should go; shared files default to the
1115 GetSystemDirectoryA(systemDir
, sizeof(systemDir
));
1119 if(flags
& VFFF_ISSHAREDFILE
)
1121 destDir
= systemDir
;
1122 /* Were we given a filename? If so, try to find the file. */
1125 if(testFileExistenceA(destDir
, lpszFilename
, FALSE
)) curDir
= destDir
;
1126 else if(lpszAppDir
&& testFileExistenceA(lpszAppDir
, lpszFilename
, FALSE
))
1128 curDir
= lpszAppDir
;
1129 retval
|= VFF_CURNEDEST
;
1133 else /* not a shared file */
1137 destDir
= lpszAppDir
;
1140 if(testFileExistenceA(destDir
, lpszFilename
, FALSE
)) curDir
= destDir
;
1141 else if(testFileExistenceA(systemDir
, lpszFilename
, FALSE
))
1144 retval
|= VFF_CURNEDEST
;
1150 /* Check to see if the file exists and is in use by another application */
1151 if (lpszFilename
&& testFileExistenceA(curDir
, lpszFilename
, FALSE
)) {
1152 if (lpszFilename
&& !testFileExistenceA(curDir
, lpszFilename
, TRUE
))
1153 retval
|= VFF_FILEINUSE
;
1156 curDirSizeReq
= strlen(curDir
) + 1;
1157 destDirSizeReq
= strlen(destDir
) + 1;
1159 /* Make sure that the pointers to the size of the buffers are
1160 valid; if not, do NOTHING with that buffer. If that pointer
1161 is valid, then make sure that the buffer pointer is valid, too! */
1163 if(lpuDestDirLen
&& lpszDestDir
)
1165 if (*lpuDestDirLen
< destDirSizeReq
) retval
|= VFF_BUFFTOOSMALL
;
1166 lstrcpynA(lpszDestDir
, destDir
, *lpuDestDirLen
);
1167 *lpuDestDirLen
= destDirSizeReq
;
1169 if(lpuCurDirLen
&& lpszCurDir
)
1171 if(*lpuCurDirLen
< curDirSizeReq
) retval
|= VFF_BUFFTOOSMALL
;
1172 lstrcpynA(lpszCurDir
, curDir
, *lpuCurDirLen
);
1173 *lpuCurDirLen
= curDirSizeReq
;
1176 TRACE("ret = %u (%s%s%s) curdir=%s destdir=%s\n", retval
,
1177 (retval
& VFF_CURNEDEST
) ? "VFF_CURNEDEST " : "",
1178 (retval
& VFF_FILEINUSE
) ? "VFF_FILEINUSE " : "",
1179 (retval
& VFF_BUFFTOOSMALL
) ? "VFF_BUFFTOOSMALL " : "",
1180 debugstr_a(lpszCurDir
), debugstr_a(lpszDestDir
));
1185 /*****************************************************************************
1186 * VerFindFileW [VERSION.@]
1188 DWORD WINAPI
VerFindFileW( DWORD flags
,LPCWSTR lpszFilename
,LPCWSTR lpszWinDir
,
1189 LPCWSTR lpszAppDir
, LPWSTR lpszCurDir
,PUINT lpuCurDirLen
,
1190 LPWSTR lpszDestDir
,PUINT lpuDestDirLen
)
1192 static const WCHAR emptyW
;
1194 const WCHAR
*curDir
;
1195 const WCHAR
*destDir
;
1196 unsigned int curDirSizeReq
;
1197 unsigned int destDirSizeReq
;
1198 WCHAR systemDir
[MAX_PATH
];
1200 /* Print out debugging information */
1201 TRACE("flags = %x filename=%s windir=%s appdir=%s curdirlen=%p(%u) destdirlen=%p(%u)\n",
1202 flags
, debugstr_w(lpszFilename
), debugstr_w(lpszWinDir
), debugstr_w(lpszAppDir
),
1203 lpuCurDirLen
, lpuCurDirLen
? *lpuCurDirLen
: 0,
1204 lpuDestDirLen
, lpuDestDirLen
? *lpuDestDirLen
: 0 );
1206 /* Figure out where the file should go; shared files default to the
1209 GetSystemDirectoryW(systemDir
, sizeof(systemDir
)/sizeof(WCHAR
));
1213 if(flags
& VFFF_ISSHAREDFILE
)
1215 destDir
= systemDir
;
1216 /* Were we given a filename? If so, try to find the file. */
1219 if(testFileExistenceW(destDir
, lpszFilename
, FALSE
)) curDir
= destDir
;
1220 else if(lpszAppDir
&& testFileExistenceW(lpszAppDir
, lpszFilename
, FALSE
))
1222 curDir
= lpszAppDir
;
1223 retval
|= VFF_CURNEDEST
;
1227 else /* not a shared file */
1231 destDir
= lpszAppDir
;
1234 if(testFileExistenceW(destDir
, lpszFilename
, FALSE
)) curDir
= destDir
;
1235 else if(testFileExistenceW(systemDir
, lpszFilename
, FALSE
))
1238 retval
|= VFF_CURNEDEST
;
1244 if (lpszFilename
&& !testFileExistenceW(curDir
, lpszFilename
, TRUE
))
1245 retval
|= VFF_FILEINUSE
;
1247 curDirSizeReq
= strlenW(curDir
) + 1;
1248 destDirSizeReq
= strlenW(destDir
) + 1;
1250 /* Make sure that the pointers to the size of the buffers are
1251 valid; if not, do NOTHING with that buffer. If that pointer
1252 is valid, then make sure that the buffer pointer is valid, too! */
1254 if(lpuDestDirLen
&& lpszDestDir
)
1256 if (*lpuDestDirLen
< destDirSizeReq
) retval
|= VFF_BUFFTOOSMALL
;
1257 lstrcpynW(lpszDestDir
, destDir
, *lpuDestDirLen
);
1258 *lpuDestDirLen
= destDirSizeReq
;
1260 if(lpuCurDirLen
&& lpszCurDir
)
1262 if(*lpuCurDirLen
< curDirSizeReq
) retval
|= VFF_BUFFTOOSMALL
;
1263 lstrcpynW(lpszCurDir
, curDir
, *lpuCurDirLen
);
1264 *lpuCurDirLen
= curDirSizeReq
;
1267 TRACE("ret = %u (%s%s%s) curdir=%s destdir=%s\n", retval
,
1268 (retval
& VFF_CURNEDEST
) ? "VFF_CURNEDEST " : "",
1269 (retval
& VFF_FILEINUSE
) ? "VFF_FILEINUSE " : "",
1270 (retval
& VFF_BUFFTOOSMALL
) ? "VFF_BUFFTOOSMALL " : "",
1271 debugstr_w(lpszCurDir
), debugstr_w(lpszDestDir
));
1276 _fetch_versioninfo(LPSTR fn
,VS_FIXEDFILEINFO
**vffi
) {
1282 buf
=HeapAlloc(GetProcessHeap(), 0, alloclen
);
1284 WARN("Memory exausted while fetching version info!\n");
1288 ret
= GetFileVersionInfoA(fn
,0,alloclen
,buf
);
1290 HeapFree(GetProcessHeap(), 0, buf
);
1293 if (alloclen
<*(WORD
*)buf
) {
1294 alloclen
= *(WORD
*)buf
;
1295 HeapFree(GetProcessHeap(), 0, buf
);
1296 buf
= HeapAlloc(GetProcessHeap(), 0, alloclen
);
1298 WARN("Memory exausted while fetching version info!\n");
1302 *vffi
= (VS_FIXEDFILEINFO
*)(buf
+0x14);
1303 if ((*vffi
)->dwSignature
== 0x004f0049) /* hack to detect unicode */
1304 *vffi
= (VS_FIXEDFILEINFO
*)(buf
+0x28);
1305 if ((*vffi
)->dwSignature
!= VS_FFI_SIGNATURE
)
1306 WARN("Bad VS_FIXEDFILEINFO signature 0x%08x\n",(*vffi
)->dwSignature
);
1313 _error2vif(DWORD error
) {
1315 case ERROR_ACCESS_DENIED
:
1316 return VIF_ACCESSVIOLATION
;
1317 case ERROR_SHARING_VIOLATION
:
1318 return VIF_SHARINGVIOLATION
;
1325 /******************************************************************************
1326 * VerInstallFileA [VERSION.@]
1328 DWORD WINAPI
VerInstallFileA(
1329 DWORD flags
,LPCSTR srcfilename
,LPCSTR destfilename
,LPCSTR srcdir
,
1330 LPCSTR destdir
,LPCSTR curdir
,LPSTR tmpfile
,PUINT tmpfilelen
)
1333 char destfn
[260],tmpfn
[260],srcfn
[260];
1335 DWORD attr
,xret
,tmplast
;
1340 TRACE("(%x,%s,%s,%s,%s,%s,%p,%d)\n",
1341 flags
,srcfilename
,destfilename
,srcdir
,destdir
,curdir
,tmpfile
,*tmpfilelen
1344 sprintf(srcfn
,"%s\\%s",srcdir
,srcfilename
);
1345 if (!destdir
|| !*destdir
) pdest
= srcdir
;
1346 else pdest
= destdir
;
1347 sprintf(destfn
,"%s\\%s",pdest
,destfilename
);
1348 hfsrc
=LZOpenFileA(srcfn
,&ofs
,OF_READ
);
1350 return VIF_CANNOTREADSRC
;
1351 sprintf(tmpfn
,"%s\\%s",pdest
,destfilename
);
1352 tmplast
=strlen(pdest
)+1;
1353 attr
= GetFileAttributesA(tmpfn
);
1354 if (attr
!= INVALID_FILE_ATTRIBUTES
) {
1355 if (attr
& FILE_ATTRIBUTE_READONLY
) {
1357 return VIF_WRITEPROT
;
1359 /* FIXME: check if file currently in use and return VIF_FILEINUSE */
1361 attr
= INVALID_FILE_ATTRIBUTES
;
1362 if (flags
& VIFF_FORCEINSTALL
) {
1364 sprintf(tmpfn
,"%s\\%s",pdest
,tmpfile
);
1365 tmplast
= strlen(pdest
)+1;
1366 attr
= GetFileAttributesA(tmpfn
);
1367 /* if it exists, it has been copied by the call before.
1368 * we jump over the copy part...
1372 if (attr
== INVALID_FILE_ATTRIBUTES
) {
1375 GetTempFileNameA(pdest
,"ver",0,tmpfn
); /* should not fail ... */
1376 s
=strrchr(tmpfn
,'\\');
1381 hfdst
= OpenFile(tmpfn
,&ofs
,OF_CREATE
);
1382 if (hfdst
== HFILE_ERROR
) {
1384 return VIF_CANNOTCREATE
; /* | translated dos error */
1386 ret
= LZCopy(hfsrc
,hfdst
);
1389 /* translate LZ errors into VIF_xxx */
1391 case LZERROR_BADINHANDLE
:
1393 case LZERROR_BADVALUE
:
1394 case LZERROR_UNKNOWNALG
:
1395 xret
= VIF_CANNOTREADSRC
;
1397 case LZERROR_BADOUTHANDLE
:
1399 xret
= VIF_OUTOFSPACE
;
1401 case LZERROR_GLOBALLOC
:
1402 case LZERROR_GLOBLOCK
:
1403 xret
= VIF_OUTOFMEMORY
;
1405 default: /* unknown error, should not happen */
1406 FIXME("Unknown LZCopy error %d, ignoring.\n", ret
);
1416 if (!(flags
& VIFF_FORCEINSTALL
)) {
1417 VS_FIXEDFILEINFO
*destvffi
,*tmpvffi
;
1418 buf1
= _fetch_versioninfo(destfn
,&destvffi
);
1420 buf2
= _fetch_versioninfo(tmpfn
,&tmpvffi
);
1423 static const CHAR trans_array
[] = "\\VarFileInfo\\Translation";
1428 /* compare file versions */
1429 if ((destvffi
->dwFileVersionMS
> tmpvffi
->dwFileVersionMS
)||
1430 ((destvffi
->dwFileVersionMS
==tmpvffi
->dwFileVersionMS
)&&
1431 (destvffi
->dwFileVersionLS
> tmpvffi
->dwFileVersionLS
)
1434 xret
|= VIF_MISMATCH
|VIF_SRCOLD
;
1435 /* compare filetypes and filesubtypes */
1436 if ((destvffi
->dwFileType
!=tmpvffi
->dwFileType
) ||
1437 (destvffi
->dwFileSubtype
!=tmpvffi
->dwFileSubtype
)
1439 xret
|= VIF_MISMATCH
|VIF_DIFFTYPE
;
1440 if (VerQueryValueA(buf1
,trans_array
,(LPVOID
*)&tbuf1
,&len1
) &&
1441 VerQueryValueA(buf2
,trans_array
,(LPVOID
*)&tbuf2
,&len2
)
1443 /* Do something with tbuf1 and tbuf2
1444 * generates DIFFLANG|MISMATCH
1447 HeapFree(GetProcessHeap(), 0, buf2
);
1449 xret
=VIF_MISMATCH
|VIF_SRCOLD
;
1450 HeapFree(GetProcessHeap(), 0, buf1
);
1454 if (*tmpfilelen
<strlen(tmpfn
+tmplast
)) {
1455 xret
|=VIF_BUFFTOOSMALL
;
1458 strcpy(tmpfile
,tmpfn
+tmplast
);
1459 *tmpfilelen
= strlen(tmpfn
+tmplast
)+1;
1463 if (INVALID_FILE_ATTRIBUTES
!=GetFileAttributesA(destfn
))
1464 if (!DeleteFileA(destfn
)) {
1465 xret
|=_error2vif(GetLastError())|VIF_CANNOTDELETE
;
1470 if ((!(flags
& VIFF_DONTDELETEOLD
)) &&
1473 lstrcmpiA(curdir
,pdest
)
1477 sprintf(curfn
,"%s\\%s",curdir
,destfilename
);
1478 if (INVALID_FILE_ATTRIBUTES
!= GetFileAttributesA(curfn
)) {
1479 /* FIXME: check if in use ... if it is, VIF_CANNOTDELETECUR */
1480 if (!DeleteFileA(curfn
))
1481 xret
|=_error2vif(GetLastError())|VIF_CANNOTDELETECUR
;
1484 if (!MoveFileA(tmpfn
,destfn
)) {
1485 xret
|=_error2vif(GetLastError())|VIF_CANNOTRENAME
;
1494 /******************************************************************************
1495 * VerInstallFileW [VERSION.@]
1497 DWORD WINAPI
VerInstallFileW(
1498 DWORD flags
,LPCWSTR srcfilename
,LPCWSTR destfilename
,LPCWSTR srcdir
,
1499 LPCWSTR destdir
,LPCWSTR curdir
,LPWSTR tmpfile
,PUINT tmpfilelen
)
1501 LPSTR wsrcf
= NULL
, wsrcd
= NULL
, wdestf
= NULL
, wdestd
= NULL
, wtmpf
= NULL
, wcurd
= NULL
;
1507 len
= WideCharToMultiByte( CP_ACP
, 0, srcfilename
, -1, NULL
, 0, NULL
, NULL
);
1508 if ((wsrcf
= HeapAlloc( GetProcessHeap(), 0, len
)))
1509 WideCharToMultiByte( CP_ACP
, 0, srcfilename
, -1, wsrcf
, len
, NULL
, NULL
);
1511 ret
= VIF_OUTOFMEMORY
;
1515 len
= WideCharToMultiByte( CP_ACP
, 0, srcdir
, -1, NULL
, 0, NULL
, NULL
);
1516 if ((wsrcd
= HeapAlloc( GetProcessHeap(), 0, len
)))
1517 WideCharToMultiByte( CP_ACP
, 0, srcdir
, -1, wsrcd
, len
, NULL
, NULL
);
1519 ret
= VIF_OUTOFMEMORY
;
1521 if (destfilename
&& !ret
)
1523 len
= WideCharToMultiByte( CP_ACP
, 0, destfilename
, -1, NULL
, 0, NULL
, NULL
);
1524 if ((wdestf
= HeapAlloc( GetProcessHeap(), 0, len
)))
1525 WideCharToMultiByte( CP_ACP
, 0, destfilename
, -1, wdestf
, len
, NULL
, NULL
);
1527 ret
= VIF_OUTOFMEMORY
;
1529 if (destdir
&& !ret
)
1531 len
= WideCharToMultiByte( CP_ACP
, 0, destdir
, -1, NULL
, 0, NULL
, NULL
);
1532 if ((wdestd
= HeapAlloc( GetProcessHeap(), 0, len
)))
1533 WideCharToMultiByte( CP_ACP
, 0, destdir
, -1, wdestd
, len
, NULL
, NULL
);
1535 ret
= VIF_OUTOFMEMORY
;
1539 len
= WideCharToMultiByte( CP_ACP
, 0, curdir
, -1, NULL
, 0, NULL
, NULL
);
1540 if ((wcurd
= HeapAlloc( GetProcessHeap(), 0, len
)))
1541 WideCharToMultiByte( CP_ACP
, 0, curdir
, -1, wcurd
, len
, NULL
, NULL
);
1543 ret
= VIF_OUTOFMEMORY
;
1547 len
= *tmpfilelen
* sizeof(WCHAR
);
1548 wtmpf
= HeapAlloc( GetProcessHeap(), 0, len
);
1550 ret
= VIF_OUTOFMEMORY
;
1553 ret
= VerInstallFileA(flags
,wsrcf
,wdestf
,wsrcd
,wdestd
,wcurd
,wtmpf
,&len
);
1555 *tmpfilelen
= MultiByteToWideChar( CP_ACP
, 0, wtmpf
, -1, tmpfile
, *tmpfilelen
);
1556 else if (ret
& VIF_BUFFTOOSMALL
)
1557 *tmpfilelen
= len
; /* FIXME: not correct */
1559 HeapFree( GetProcessHeap(), 0, wsrcf
);
1560 HeapFree( GetProcessHeap(), 0, wsrcd
);
1561 HeapFree( GetProcessHeap(), 0, wdestf
);
1562 HeapFree( GetProcessHeap(), 0, wdestd
);
1563 HeapFree( GetProcessHeap(), 0, wtmpf
);
1564 HeapFree( GetProcessHeap(), 0, wcurd
);