2 * Implementation of VER.DLL
4 * Copyright 1996, 1997 Marcus Meissner
5 * Copyright 1997 David Cuthbert
6 * Copyright 1999 Ulrich Weigand
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include <sys/types.h>
28 #define NONAMELESSUNION
29 #define NONAMELESSSTRUCT
31 #include "wine/winbase16.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(ver
);
40 /**********************************************************************
43 * Find an entry by id in a resource directory
44 * Copied from loader/pe_resource.c
46 static const IMAGE_RESOURCE_DIRECTORY
*find_entry_by_id( const IMAGE_RESOURCE_DIRECTORY
*dir
,
47 WORD id
, const void *root
)
49 const IMAGE_RESOURCE_DIRECTORY_ENTRY
*entry
;
52 entry
= (const IMAGE_RESOURCE_DIRECTORY_ENTRY
*)(dir
+ 1);
53 min
= dir
->NumberOfNamedEntries
;
54 max
= min
+ dir
->NumberOfIdEntries
- 1;
57 pos
= (min
+ max
) / 2;
58 if (entry
[pos
].u
.Id
== id
)
59 return (const IMAGE_RESOURCE_DIRECTORY
*)((const char *)root
+ entry
[pos
].u2
.s2
.OffsetToDirectory
);
60 if (entry
[pos
].u
.Id
> id
) max
= pos
- 1;
67 /**********************************************************************
70 * Find a default entry in a resource directory
71 * Copied from loader/pe_resource.c
73 static const IMAGE_RESOURCE_DIRECTORY
*find_entry_default( const IMAGE_RESOURCE_DIRECTORY
*dir
,
76 const IMAGE_RESOURCE_DIRECTORY_ENTRY
*entry
;
78 entry
= (const IMAGE_RESOURCE_DIRECTORY_ENTRY
*)(dir
+ 1);
79 return (const IMAGE_RESOURCE_DIRECTORY
*)((const char *)root
+ entry
->u2
.s2
.OffsetToDirectory
);
83 /**********************************************************************
86 * Find an entry by name in a resource directory
87 * Copied from loader/pe_resource.c
89 static const IMAGE_RESOURCE_DIRECTORY
*find_entry_by_name( const IMAGE_RESOURCE_DIRECTORY
*dir
,
90 LPCSTR name
, const void *root
)
92 const IMAGE_RESOURCE_DIRECTORY
*ret
= NULL
;
96 if (!HIWORD(name
)) return find_entry_by_id( dir
, LOWORD(name
), root
);
99 return find_entry_by_id( dir
, atoi(name
+1), root
);
102 namelen
= MultiByteToWideChar( CP_ACP
, 0, name
, -1, NULL
, 0 );
103 if ((nameW
= HeapAlloc( GetProcessHeap(), 0, namelen
* sizeof(WCHAR
) )))
105 const IMAGE_RESOURCE_DIRECTORY_ENTRY
*entry
;
106 const IMAGE_RESOURCE_DIR_STRING_U
*str
;
107 int min
, max
, res
, pos
;
109 MultiByteToWideChar( CP_ACP
, 0, name
, -1, nameW
, namelen
);
110 namelen
--; /* remove terminating null */
111 entry
= (const IMAGE_RESOURCE_DIRECTORY_ENTRY
*)(dir
+ 1);
113 max
= dir
->NumberOfNamedEntries
- 1;
116 pos
= (min
+ max
) / 2;
117 str
= (const IMAGE_RESOURCE_DIR_STRING_U
*)((const char *)root
+ entry
[pos
].u
.s
.NameOffset
);
118 res
= wcsnicmp( nameW
, str
->NameString
, str
->Length
);
119 if (!res
&& namelen
== str
->Length
)
121 ret
= (const IMAGE_RESOURCE_DIRECTORY
*)((const char *)root
+ entry
[pos
].u2
.s2
.OffsetToDirectory
);
124 if (res
< 0) max
= pos
- 1;
127 HeapFree( GetProcessHeap(), 0, nameW
);
133 /***********************************************************************
134 * read_xx_header [internal]
136 static int read_xx_header( HFILE lzfd
)
138 IMAGE_DOS_HEADER mzh
;
141 LZSeek( lzfd
, 0, FILE_BEGIN
);
142 if ( sizeof(mzh
) != LZRead( lzfd
, (LPSTR
)&mzh
, sizeof(mzh
) ) )
144 if ( mzh
.e_magic
!= IMAGE_DOS_SIGNATURE
)
147 LZSeek( lzfd
, mzh
.e_lfanew
, FILE_BEGIN
);
148 if ( 2 != LZRead( lzfd
, magic
, 2 ) )
151 LZSeek( lzfd
, mzh
.e_lfanew
, FILE_BEGIN
);
153 if ( magic
[0] == 'N' && magic
[1] == 'E' )
154 return IMAGE_OS2_SIGNATURE
;
155 if ( magic
[0] == 'P' && magic
[1] == 'E' )
156 return IMAGE_NT_SIGNATURE
;
159 WARN("Can't handle %s files.\n", magic
);
163 /***********************************************************************
164 * find_ne_resource [internal]
166 static BOOL
find_ne_resource( HFILE lzfd
, LPCSTR
typeid, LPCSTR resid
,
167 DWORD
*resLen
, DWORD
*resOff
)
169 IMAGE_OS2_HEADER nehd
;
170 NE_TYPEINFO
*typeInfo
;
171 NE_NAMEINFO
*nameInfo
;
177 /* Read in NE header */
178 nehdoffset
= LZSeek( lzfd
, 0, FILE_CURRENT
);
179 if ( sizeof(nehd
) != LZRead( lzfd
, (LPSTR
)&nehd
, sizeof(nehd
) ) ) return FALSE
;
181 resTabSize
= nehd
.ne_restab
- nehd
.ne_rsrctab
;
184 TRACE("No resources in NE dll\n" );
188 /* Read in resource table */
189 resTab
= HeapAlloc( GetProcessHeap(), 0, resTabSize
);
190 if ( !resTab
) return FALSE
;
192 LZSeek( lzfd
, nehd
.ne_rsrctab
+ nehdoffset
, FILE_BEGIN
);
193 if ( resTabSize
!= LZRead( lzfd
, (char*)resTab
, resTabSize
) )
195 HeapFree( GetProcessHeap(), 0, resTab
);
200 typeInfo
= (NE_TYPEINFO
*)(resTab
+ 2);
202 if (HIWORD(typeid) != 0) /* named type */
204 BYTE len
= strlen( typeid );
205 while (typeInfo
->type_id
)
207 if (!(typeInfo
->type_id
& 0x8000))
209 BYTE
*p
= resTab
+ typeInfo
->type_id
;
210 if ((*p
== len
) && !_strnicmp( (char*)p
+1, typeid, len
)) goto found_type
;
212 typeInfo
= (NE_TYPEINFO
*)((char *)(typeInfo
+ 1) +
213 typeInfo
->count
* sizeof(NE_NAMEINFO
));
216 else /* numeric type id */
218 WORD id
= LOWORD(typeid) | 0x8000;
219 while (typeInfo
->type_id
)
221 if (typeInfo
->type_id
== id
) goto found_type
;
222 typeInfo
= (NE_TYPEINFO
*)((char *)(typeInfo
+ 1) +
223 typeInfo
->count
* sizeof(NE_NAMEINFO
));
226 TRACE("No typeid entry found for %p\n", typeid );
227 HeapFree( GetProcessHeap(), 0, resTab
);
231 nameInfo
= (NE_NAMEINFO
*)(typeInfo
+ 1);
233 if (HIWORD(resid
) != 0) /* named resource */
235 BYTE len
= strlen( resid
);
236 for (count
= typeInfo
->count
; count
> 0; count
--, nameInfo
++)
238 BYTE
*p
= resTab
+ nameInfo
->id
;
239 if (nameInfo
->id
& 0x8000) continue;
240 if ((*p
== len
) && !_strnicmp( (char*)p
+1, resid
, len
)) goto found_name
;
243 else /* numeric resource id */
245 WORD id
= LOWORD(resid
) | 0x8000;
246 for (count
= typeInfo
->count
; count
> 0; count
--, nameInfo
++)
247 if (nameInfo
->id
== id
) goto found_name
;
249 TRACE("No resid entry found for %p\n", typeid );
250 HeapFree( GetProcessHeap(), 0, resTab
);
254 /* Return resource data */
255 if ( resLen
) *resLen
= nameInfo
->length
<< *(WORD
*)resTab
;
256 if ( resOff
) *resOff
= nameInfo
->offset
<< *(WORD
*)resTab
;
258 HeapFree( GetProcessHeap(), 0, resTab
);
262 /***********************************************************************
263 * find_pe_resource [internal]
265 static BOOL
find_pe_resource( HFILE lzfd
, LPCSTR
typeid, LPCSTR resid
,
266 DWORD
*resLen
, DWORD
*resOff
)
268 IMAGE_NT_HEADERS pehd
;
270 PIMAGE_DATA_DIRECTORY resDataDir
;
271 PIMAGE_SECTION_HEADER sections
;
273 DWORD resSectionSize
;
275 const IMAGE_RESOURCE_DIRECTORY
*resPtr
;
276 const IMAGE_RESOURCE_DATA_ENTRY
*resData
;
280 /* Read in PE header */
281 pehdoffset
= LZSeek( lzfd
, 0, FILE_CURRENT
);
282 if ( sizeof(pehd
) != LZRead( lzfd
, (LPSTR
)&pehd
, sizeof(pehd
) ) ) return FALSE
;
284 resDataDir
= pehd
.OptionalHeader
.DataDirectory
+IMAGE_DIRECTORY_ENTRY_RESOURCE
;
285 if ( !resDataDir
->Size
)
287 TRACE("No resources in PE dll\n" );
291 /* Read in section table */
292 nSections
= pehd
.FileHeader
.NumberOfSections
;
293 sections
= HeapAlloc( GetProcessHeap(), 0,
294 nSections
* sizeof(IMAGE_SECTION_HEADER
) );
295 if ( !sections
) return FALSE
;
297 LZSeek( lzfd
, pehdoffset
+
298 sizeof(DWORD
) + /* Signature */
299 sizeof(IMAGE_FILE_HEADER
) +
300 pehd
.FileHeader
.SizeOfOptionalHeader
, FILE_BEGIN
);
302 if ( nSections
* sizeof(IMAGE_SECTION_HEADER
) !=
303 LZRead( lzfd
, (LPSTR
)sections
, nSections
* sizeof(IMAGE_SECTION_HEADER
) ) )
305 HeapFree( GetProcessHeap(), 0, sections
);
309 /* Find resource section */
310 for ( i
= 0; i
< nSections
; i
++ )
311 if ( resDataDir
->VirtualAddress
>= sections
[i
].VirtualAddress
312 && resDataDir
->VirtualAddress
< sections
[i
].VirtualAddress
+
313 sections
[i
].SizeOfRawData
)
316 if ( i
== nSections
)
318 HeapFree( GetProcessHeap(), 0, sections
);
319 TRACE("Couldn't find resource section\n" );
323 /* Read in resource section */
324 resSectionSize
= sections
[i
].SizeOfRawData
;
325 resSection
= HeapAlloc( GetProcessHeap(), 0, resSectionSize
);
328 HeapFree( GetProcessHeap(), 0, sections
);
332 LZSeek( lzfd
, sections
[i
].PointerToRawData
, FILE_BEGIN
);
333 if ( resSectionSize
!= LZRead( lzfd
, (char*)resSection
, resSectionSize
) ) goto done
;
336 resDir
= resSection
+ (resDataDir
->VirtualAddress
- sections
[i
].VirtualAddress
);
339 resPtr
= find_entry_by_name( resPtr
, typeid, resDir
);
342 TRACE("No typeid entry found for %p\n", typeid );
345 resPtr
= find_entry_by_name( resPtr
, resid
, resDir
);
348 TRACE("No resid entry found for %p\n", resid
);
351 resPtr
= find_entry_default( resPtr
, resDir
);
354 TRACE("No default language entry found for %p\n", resid
);
358 /* Find resource data section */
359 resData
= (const IMAGE_RESOURCE_DATA_ENTRY
*)resPtr
;
360 for ( i
= 0; i
< nSections
; i
++ )
361 if ( resData
->OffsetToData
>= sections
[i
].VirtualAddress
362 && resData
->OffsetToData
< sections
[i
].VirtualAddress
+
363 sections
[i
].SizeOfRawData
)
366 if ( i
== nSections
)
368 TRACE("Couldn't find resource data section\n" );
372 /* Return resource data */
373 if ( resLen
) *resLen
= resData
->Size
;
374 if ( resOff
) *resOff
= resData
->OffsetToData
- sections
[i
].VirtualAddress
375 + sections
[i
].PointerToRawData
;
379 HeapFree( GetProcessHeap(), 0, resSection
);
380 HeapFree( GetProcessHeap(), 0, sections
);
385 /***********************************************************************
386 * find_resource [internal]
388 static DWORD
find_resource( HFILE lzfd
, LPCSTR type
, LPCSTR id
, DWORD
*reslen
, DWORD
*offset
)
390 DWORD magic
= read_xx_header( lzfd
);
394 case IMAGE_OS2_SIGNATURE
:
395 if (!find_ne_resource( lzfd
, type
, id
, reslen
, offset
)) magic
= 0;
397 case IMAGE_NT_SIGNATURE
:
398 if (!find_pe_resource( lzfd
, type
, id
, reslen
, offset
)) magic
= 0;
405 /*************************************************************************
406 * GetFileResourceSize [VER.2]
408 DWORD WINAPI
GetFileResourceSize16( LPCSTR lpszFileName
, LPCSTR lpszResType
,
409 LPCSTR lpszResId
, LPDWORD lpdwFileOffset
)
415 TRACE("(%s,type=%p,id=%p,off=%p)\n",
416 debugstr_a(lpszFileName
), lpszResType
, lpszResId
, lpszResId
);
418 lzfd
= LZOpenFileA( (LPSTR
)lpszFileName
, &ofs
, OF_READ
);
421 if (!find_resource( lzfd
, lpszResType
, lpszResId
, &reslen
, lpdwFileOffset
)) reslen
= 0;
428 /*************************************************************************
429 * GetFileResource [VER.3]
431 DWORD WINAPI
GetFileResource16( LPCSTR lpszFileName
, LPCSTR lpszResType
,
432 LPCSTR lpszResId
, DWORD dwFileOffset
,
433 DWORD dwResLen
, LPVOID lpvData
)
437 DWORD reslen
= dwResLen
;
439 TRACE("(%s,type=%p,id=%p,off=%d,len=%d,data=%p)\n",
440 debugstr_a(lpszFileName
), lpszResType
, lpszResId
,
441 dwFileOffset
, dwResLen
, lpvData
);
443 lzfd
= LZOpenFileA( (LPSTR
)lpszFileName
, &ofs
, OF_READ
);
444 if ( lzfd
< 0 ) return 0;
448 if (!find_resource( lzfd
, lpszResType
, lpszResId
, &reslen
, &dwFileOffset
))
455 LZSeek( lzfd
, dwFileOffset
, FILE_BEGIN
);
456 reslen
= LZRead( lzfd
, lpvData
, min( reslen
, dwResLen
) );
462 /*************************************************************************
463 * GetFileVersionInfoSize [VER.6]
465 DWORD WINAPI
GetFileVersionInfoSize16( LPCSTR filename
, LPDWORD handle
)
469 TRACE("(%s, %p)\n", debugstr_a(filename
), handle
);
471 return GetFileResourceSize16( filename
, (LPCSTR
)RT_VERSION
, (LPCSTR
)VS_VERSION_INFO
, &offset
);
474 /*************************************************************************
475 * GetFileVersionInfo [VER.7]
477 DWORD WINAPI
GetFileVersionInfo16( LPCSTR filename
, DWORD handle
, DWORD datasize
, LPVOID data
)
479 TRACE("(%s, %08x, %d, %p)\n", debugstr_a(filename
), handle
, datasize
, data
);
481 return GetFileResource16( filename
, (LPCSTR
)RT_VERSION
, (LPCSTR
)VS_VERSION_INFO
, 0, datasize
, data
);
484 /*************************************************************************
485 * VerFindFile [VER.8]
487 DWORD WINAPI
VerFindFile16( UINT16 flags
, LPSTR lpszFilename
,
488 LPSTR lpszWinDir
, LPSTR lpszAppDir
,
489 LPSTR lpszCurDir
, UINT16
*lpuCurDirLen
,
490 LPSTR lpszDestDir
, UINT16
*lpuDestDirLen
)
492 UINT curDirLen
, destDirLen
;
493 UINT
*pcurDirLen
= NULL
, *pdestDirLen
= NULL
;
497 curDirLen
= *lpuCurDirLen
;
498 pcurDirLen
= &curDirLen
;
501 destDirLen
= *lpuDestDirLen
;
502 pdestDirLen
= &destDirLen
;
504 retv
= VerFindFileA( flags
, lpszFilename
, lpszWinDir
, lpszAppDir
,
505 lpszCurDir
, pcurDirLen
, lpszDestDir
, pdestDirLen
);
507 *lpuCurDirLen
= (UINT16
)curDirLen
;
509 *lpuDestDirLen
= (UINT16
)destDirLen
;
513 /*************************************************************************
514 * VerInstallFile [VER.9]
516 DWORD WINAPI
VerInstallFile16( UINT16 flags
,
517 LPSTR lpszSrcFilename
, LPSTR lpszDestFilename
,
518 LPSTR lpszSrcDir
, LPSTR lpszDestDir
, LPSTR lpszCurDir
,
519 LPSTR lpszTmpFile
, UINT16
*lpwTmpFileLen
)
521 UINT filelen
= *lpwTmpFileLen
;
523 DWORD retv
= VerInstallFileA( flags
, lpszSrcFilename
, lpszDestFilename
,
524 lpszSrcDir
, lpszDestDir
, lpszCurDir
,
525 lpszTmpFile
, &filelen
);
527 *lpwTmpFileLen
= (UINT16
)filelen
;
531 /*************************************************************************
532 * VerLanguageName [VER.10]
534 DWORD WINAPI
VerLanguageName16( UINT16 uLang
, LPSTR lpszLang
, UINT16 cbLang
)
536 return VerLanguageNameA( uLang
, lpszLang
, cbLang
);
539 /*************************************************************************
540 * VerQueryValue [VER.11]
542 DWORD WINAPI
VerQueryValue16( SEGPTR spvBlock
, LPSTR lpszSubBlock
,
543 SEGPTR
*lpspBuffer
, UINT16
*lpcb
)
545 LPVOID lpvBlock
= MapSL( spvBlock
);
546 LPVOID buffer
= lpvBlock
;
550 TRACE("(%p, %s, %p, %p)\n",
551 lpvBlock
, debugstr_a(lpszSubBlock
), lpspBuffer
, lpcb
);
553 retv
= VerQueryValueA( lpvBlock
, lpszSubBlock
, &buffer
, &buflen
);
554 if ( !retv
) return FALSE
;
556 if ( OFFSETOF( spvBlock
) + ((char *) buffer
- (char *) lpvBlock
) >= 0x10000 )
558 FIXME("offset %08X too large relative to %04X:%04X\n",
559 (char *) buffer
- (char *) lpvBlock
, SELECTOROF( spvBlock
), OFFSETOF( spvBlock
) );
563 if (lpcb
) *lpcb
= buflen
;
564 *lpspBuffer
= (SEGPTR
) ((char *) spvBlock
+ ((char *) buffer
- (char *) lpvBlock
));