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"
34 #include "wine/unicode.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(ver
);
45 /**********************************************************************
48 * Find an entry by id in a resource directory
49 * Copied from loader/pe_resource.c
51 static const IMAGE_RESOURCE_DIRECTORY
*find_entry_by_id( const IMAGE_RESOURCE_DIRECTORY
*dir
,
52 WORD id
, const void *root
)
54 const IMAGE_RESOURCE_DIRECTORY_ENTRY
*entry
;
57 entry
= (const IMAGE_RESOURCE_DIRECTORY_ENTRY
*)(dir
+ 1);
58 min
= dir
->NumberOfNamedEntries
;
59 max
= min
+ dir
->NumberOfIdEntries
- 1;
62 pos
= (min
+ max
) / 2;
63 if (entry
[pos
].u
.Id
== id
)
64 return (const IMAGE_RESOURCE_DIRECTORY
*)((const char *)root
+ entry
[pos
].u2
.s2
.OffsetToDirectory
);
65 if (entry
[pos
].u
.Id
> id
) max
= pos
- 1;
72 /**********************************************************************
75 * Find a default entry in a resource directory
76 * Copied from loader/pe_resource.c
78 static const IMAGE_RESOURCE_DIRECTORY
*find_entry_default( const IMAGE_RESOURCE_DIRECTORY
*dir
,
81 const IMAGE_RESOURCE_DIRECTORY_ENTRY
*entry
;
83 entry
= (const IMAGE_RESOURCE_DIRECTORY_ENTRY
*)(dir
+ 1);
84 return (const IMAGE_RESOURCE_DIRECTORY
*)((const char *)root
+ entry
->u2
.s2
.OffsetToDirectory
);
88 /**********************************************************************
91 * Find an entry by name in a resource directory
92 * Copied from loader/pe_resource.c
94 static const IMAGE_RESOURCE_DIRECTORY
*find_entry_by_name( const IMAGE_RESOURCE_DIRECTORY
*dir
,
95 LPCSTR name
, const void *root
)
97 const IMAGE_RESOURCE_DIRECTORY
*ret
= NULL
;
101 if (!HIWORD(name
)) return find_entry_by_id( dir
, LOWORD(name
), root
);
104 return find_entry_by_id( dir
, atoi(name
+1), root
);
107 namelen
= MultiByteToWideChar( CP_ACP
, 0, name
, -1, NULL
, 0 );
108 if ((nameW
= HeapAlloc( GetProcessHeap(), 0, namelen
* sizeof(WCHAR
) )))
110 const IMAGE_RESOURCE_DIRECTORY_ENTRY
*entry
;
111 const IMAGE_RESOURCE_DIR_STRING_U
*str
;
112 int min
, max
, res
, pos
;
114 MultiByteToWideChar( CP_ACP
, 0, name
, -1, nameW
, namelen
);
115 namelen
--; /* remove terminating null */
116 entry
= (const IMAGE_RESOURCE_DIRECTORY_ENTRY
*)(dir
+ 1);
118 max
= dir
->NumberOfNamedEntries
- 1;
121 pos
= (min
+ max
) / 2;
122 str
= (const IMAGE_RESOURCE_DIR_STRING_U
*)((const char *)root
+ entry
[pos
].u
.s
.NameOffset
);
123 res
= strncmpiW( nameW
, str
->NameString
, str
->Length
);
124 if (!res
&& namelen
== str
->Length
)
126 ret
= (const IMAGE_RESOURCE_DIRECTORY
*)((const char *)root
+ entry
[pos
].u2
.s2
.OffsetToDirectory
);
129 if (res
< 0) max
= pos
- 1;
132 HeapFree( GetProcessHeap(), 0, nameW
);
138 /***********************************************************************
139 * read_xx_header [internal]
141 static int read_xx_header( HFILE lzfd
)
143 IMAGE_DOS_HEADER mzh
;
146 LZSeek( lzfd
, 0, SEEK_SET
);
147 if ( sizeof(mzh
) != LZRead( lzfd
, (LPSTR
)&mzh
, sizeof(mzh
) ) )
149 if ( mzh
.e_magic
!= IMAGE_DOS_SIGNATURE
)
152 LZSeek( lzfd
, mzh
.e_lfanew
, SEEK_SET
);
153 if ( 2 != LZRead( lzfd
, magic
, 2 ) )
156 LZSeek( lzfd
, mzh
.e_lfanew
, SEEK_SET
);
158 if ( magic
[0] == 'N' && magic
[1] == 'E' )
159 return IMAGE_OS2_SIGNATURE
;
160 if ( magic
[0] == 'P' && magic
[1] == 'E' )
161 return IMAGE_NT_SIGNATURE
;
164 WARN("Can't handle %s files.\n", magic
);
168 /***********************************************************************
169 * find_ne_resource [internal]
171 static BOOL
find_ne_resource( HFILE lzfd
, LPCSTR
typeid, LPCSTR resid
,
172 DWORD
*resLen
, DWORD
*resOff
)
174 IMAGE_OS2_HEADER nehd
;
175 NE_TYPEINFO
*typeInfo
;
176 NE_NAMEINFO
*nameInfo
;
182 /* Read in NE header */
183 nehdoffset
= LZSeek( lzfd
, 0, SEEK_CUR
);
184 if ( sizeof(nehd
) != LZRead( lzfd
, (LPSTR
)&nehd
, sizeof(nehd
) ) ) return FALSE
;
186 resTabSize
= nehd
.ne_restab
- nehd
.ne_rsrctab
;
189 TRACE("No resources in NE dll\n" );
193 /* Read in resource table */
194 resTab
= HeapAlloc( GetProcessHeap(), 0, resTabSize
);
195 if ( !resTab
) return FALSE
;
197 LZSeek( lzfd
, nehd
.ne_rsrctab
+ nehdoffset
, SEEK_SET
);
198 if ( resTabSize
!= LZRead( lzfd
, (char*)resTab
, resTabSize
) )
200 HeapFree( GetProcessHeap(), 0, resTab
);
205 typeInfo
= (NE_TYPEINFO
*)(resTab
+ 2);
207 if (HIWORD(typeid) != 0) /* named type */
209 BYTE len
= strlen( typeid );
210 while (typeInfo
->type_id
)
212 if (!(typeInfo
->type_id
& 0x8000))
214 BYTE
*p
= resTab
+ typeInfo
->type_id
;
215 if ((*p
== len
) && !strncasecmp( (char*)p
+1, typeid, len
)) goto found_type
;
217 typeInfo
= (NE_TYPEINFO
*)((char *)(typeInfo
+ 1) +
218 typeInfo
->count
* sizeof(NE_NAMEINFO
));
221 else /* numeric type id */
223 WORD id
= LOWORD(typeid) | 0x8000;
224 while (typeInfo
->type_id
)
226 if (typeInfo
->type_id
== id
) goto found_type
;
227 typeInfo
= (NE_TYPEINFO
*)((char *)(typeInfo
+ 1) +
228 typeInfo
->count
* sizeof(NE_NAMEINFO
));
231 TRACE("No typeid entry found for %p\n", typeid );
232 HeapFree( GetProcessHeap(), 0, resTab
);
236 nameInfo
= (NE_NAMEINFO
*)(typeInfo
+ 1);
238 if (HIWORD(resid
) != 0) /* named resource */
240 BYTE len
= strlen( resid
);
241 for (count
= typeInfo
->count
; count
> 0; count
--, nameInfo
++)
243 BYTE
*p
= resTab
+ nameInfo
->id
;
244 if (nameInfo
->id
& 0x8000) continue;
245 if ((*p
== len
) && !strncasecmp( (char*)p
+1, resid
, len
)) goto found_name
;
248 else /* numeric resource id */
250 WORD id
= LOWORD(resid
) | 0x8000;
251 for (count
= typeInfo
->count
; count
> 0; count
--, nameInfo
++)
252 if (nameInfo
->id
== id
) goto found_name
;
254 TRACE("No resid entry found for %p\n", typeid );
255 HeapFree( GetProcessHeap(), 0, resTab
);
259 /* Return resource data */
260 if ( resLen
) *resLen
= nameInfo
->length
<< *(WORD
*)resTab
;
261 if ( resOff
) *resOff
= nameInfo
->offset
<< *(WORD
*)resTab
;
263 HeapFree( GetProcessHeap(), 0, resTab
);
267 /***********************************************************************
268 * find_pe_resource [internal]
270 static BOOL
find_pe_resource( HFILE lzfd
, LPCSTR
typeid, LPCSTR resid
,
271 DWORD
*resLen
, DWORD
*resOff
)
273 IMAGE_NT_HEADERS pehd
;
275 PIMAGE_DATA_DIRECTORY resDataDir
;
276 PIMAGE_SECTION_HEADER sections
;
278 DWORD resSectionSize
;
280 const IMAGE_RESOURCE_DIRECTORY
*resPtr
;
281 const IMAGE_RESOURCE_DATA_ENTRY
*resData
;
285 /* Read in PE header */
286 pehdoffset
= LZSeek( lzfd
, 0, SEEK_CUR
);
287 if ( sizeof(pehd
) != LZRead( lzfd
, (LPSTR
)&pehd
, sizeof(pehd
) ) ) return FALSE
;
289 resDataDir
= pehd
.OptionalHeader
.DataDirectory
+IMAGE_DIRECTORY_ENTRY_RESOURCE
;
290 if ( !resDataDir
->Size
)
292 TRACE("No resources in PE dll\n" );
296 /* Read in section table */
297 nSections
= pehd
.FileHeader
.NumberOfSections
;
298 sections
= HeapAlloc( GetProcessHeap(), 0,
299 nSections
* sizeof(IMAGE_SECTION_HEADER
) );
300 if ( !sections
) return FALSE
;
302 LZSeek( lzfd
, pehdoffset
+
303 sizeof(DWORD
) + /* Signature */
304 sizeof(IMAGE_FILE_HEADER
) +
305 pehd
.FileHeader
.SizeOfOptionalHeader
, SEEK_SET
);
307 if ( nSections
* sizeof(IMAGE_SECTION_HEADER
) !=
308 LZRead( lzfd
, (LPSTR
)sections
, nSections
* sizeof(IMAGE_SECTION_HEADER
) ) )
310 HeapFree( GetProcessHeap(), 0, sections
);
314 /* Find resource section */
315 for ( i
= 0; i
< nSections
; i
++ )
316 if ( resDataDir
->VirtualAddress
>= sections
[i
].VirtualAddress
317 && resDataDir
->VirtualAddress
< sections
[i
].VirtualAddress
+
318 sections
[i
].SizeOfRawData
)
321 if ( i
== nSections
)
323 HeapFree( GetProcessHeap(), 0, sections
);
324 TRACE("Couldn't find resource section\n" );
328 /* Read in resource section */
329 resSectionSize
= sections
[i
].SizeOfRawData
;
330 resSection
= HeapAlloc( GetProcessHeap(), 0, resSectionSize
);
333 HeapFree( GetProcessHeap(), 0, sections
);
337 LZSeek( lzfd
, sections
[i
].PointerToRawData
, SEEK_SET
);
338 if ( resSectionSize
!= LZRead( lzfd
, (char*)resSection
, resSectionSize
) ) goto done
;
341 resDir
= resSection
+ (resDataDir
->VirtualAddress
- sections
[i
].VirtualAddress
);
344 resPtr
= find_entry_by_name( resPtr
, typeid, resDir
);
347 TRACE("No typeid entry found for %p\n", typeid );
350 resPtr
= find_entry_by_name( resPtr
, resid
, resDir
);
353 TRACE("No resid entry found for %p\n", resid
);
356 resPtr
= find_entry_default( resPtr
, resDir
);
359 TRACE("No default language entry found for %p\n", resid
);
363 /* Find resource data section */
364 resData
= (const IMAGE_RESOURCE_DATA_ENTRY
*)resPtr
;
365 for ( i
= 0; i
< nSections
; i
++ )
366 if ( resData
->OffsetToData
>= sections
[i
].VirtualAddress
367 && resData
->OffsetToData
< sections
[i
].VirtualAddress
+
368 sections
[i
].SizeOfRawData
)
371 if ( i
== nSections
)
373 TRACE("Couldn't find resource data section\n" );
377 /* Return resource data */
378 if ( resLen
) *resLen
= resData
->Size
;
379 if ( resOff
) *resOff
= resData
->OffsetToData
- sections
[i
].VirtualAddress
380 + sections
[i
].PointerToRawData
;
384 HeapFree( GetProcessHeap(), 0, resSection
);
385 HeapFree( GetProcessHeap(), 0, sections
);
390 /***********************************************************************
391 * find_resource [internal]
393 static DWORD
find_resource( HFILE lzfd
, LPCSTR type
, LPCSTR id
, DWORD
*reslen
, DWORD
*offset
)
395 DWORD magic
= read_xx_header( lzfd
);
399 case IMAGE_OS2_SIGNATURE
:
400 if (!find_ne_resource( lzfd
, type
, id
, reslen
, offset
)) magic
= 0;
402 case IMAGE_NT_SIGNATURE
:
403 if (!find_pe_resource( lzfd
, type
, id
, reslen
, offset
)) magic
= 0;
410 /*************************************************************************
411 * GetFileResourceSize [VER.2]
413 DWORD WINAPI
GetFileResourceSize16( LPCSTR lpszFileName
, LPCSTR lpszResType
,
414 LPCSTR lpszResId
, LPDWORD lpdwFileOffset
)
420 TRACE("(%s,type=%p,id=%p,off=%p)\n",
421 debugstr_a(lpszFileName
), lpszResType
, lpszResId
, lpszResId
);
423 lzfd
= LZOpenFileA( (LPSTR
)lpszFileName
, &ofs
, OF_READ
);
426 if (!find_resource( lzfd
, lpszResType
, lpszResId
, &reslen
, lpdwFileOffset
)) reslen
= 0;
433 /*************************************************************************
434 * GetFileResource [VER.3]
436 DWORD WINAPI
GetFileResource16( LPCSTR lpszFileName
, LPCSTR lpszResType
,
437 LPCSTR lpszResId
, DWORD dwFileOffset
,
438 DWORD dwResLen
, LPVOID lpvData
)
442 DWORD reslen
= dwResLen
;
444 TRACE("(%s,type=%p,id=%p,off=%d,len=%d,data=%p)\n",
445 debugstr_a(lpszFileName
), lpszResType
, lpszResId
,
446 dwFileOffset
, dwResLen
, lpvData
);
448 lzfd
= LZOpenFileA( (LPSTR
)lpszFileName
, &ofs
, OF_READ
);
449 if ( lzfd
< 0 ) return 0;
453 if (!find_resource( lzfd
, lpszResType
, lpszResId
, &reslen
, &dwFileOffset
))
460 LZSeek( lzfd
, dwFileOffset
, SEEK_SET
);
461 reslen
= LZRead( lzfd
, lpvData
, min( reslen
, dwResLen
) );
467 /*************************************************************************
468 * GetFileVersionInfoSize [VER.6]
470 DWORD WINAPI
GetFileVersionInfoSize16( LPCSTR lpszFileName
, LPDWORD lpdwHandle
)
472 TRACE("(%s, %p)\n", debugstr_a(lpszFileName
), lpdwHandle
);
473 return GetFileVersionInfoSizeA( lpszFileName
, lpdwHandle
);
476 /*************************************************************************
477 * GetFileVersionInfo [VER.7]
479 DWORD WINAPI
GetFileVersionInfo16( LPCSTR lpszFileName
, DWORD handle
,
480 DWORD cbBuf
, LPVOID lpvData
)
482 TRACE("(%s, %08x, %d, %p)\n",
483 debugstr_a(lpszFileName
), handle
, cbBuf
, lpvData
);
485 return GetFileVersionInfoA( lpszFileName
, handle
, cbBuf
, lpvData
);
488 /*************************************************************************
489 * VerFindFile [VER.8]
491 DWORD WINAPI
VerFindFile16( UINT16 flags
, LPSTR lpszFilename
,
492 LPSTR lpszWinDir
, LPSTR lpszAppDir
,
493 LPSTR lpszCurDir
, UINT16
*lpuCurDirLen
,
494 LPSTR lpszDestDir
, UINT16
*lpuDestDirLen
)
496 UINT curDirLen
, destDirLen
;
497 UINT
*pcurDirLen
= NULL
, *pdestDirLen
= NULL
;
501 curDirLen
= *lpuCurDirLen
;
502 pcurDirLen
= &curDirLen
;
505 destDirLen
= *lpuDestDirLen
;
506 pdestDirLen
= &destDirLen
;
508 retv
= VerFindFileA( flags
, lpszFilename
, lpszWinDir
, lpszAppDir
,
509 lpszCurDir
, pcurDirLen
, lpszDestDir
, pdestDirLen
);
511 *lpuCurDirLen
= (UINT16
)curDirLen
;
513 *lpuDestDirLen
= (UINT16
)destDirLen
;
517 /*************************************************************************
518 * VerInstallFile [VER.9]
520 DWORD WINAPI
VerInstallFile16( UINT16 flags
,
521 LPSTR lpszSrcFilename
, LPSTR lpszDestFilename
,
522 LPSTR lpszSrcDir
, LPSTR lpszDestDir
, LPSTR lpszCurDir
,
523 LPSTR lpszTmpFile
, UINT16
*lpwTmpFileLen
)
525 UINT filelen
= *lpwTmpFileLen
;
527 DWORD retv
= VerInstallFileA( flags
, lpszSrcFilename
, lpszDestFilename
,
528 lpszSrcDir
, lpszDestDir
, lpszCurDir
,
529 lpszTmpFile
, &filelen
);
531 *lpwTmpFileLen
= (UINT16
)filelen
;
535 /*************************************************************************
536 * VerLanguageName [VER.10]
538 DWORD WINAPI
VerLanguageName16( UINT16 uLang
, LPSTR lpszLang
, UINT16 cbLang
)
540 return VerLanguageNameA( uLang
, lpszLang
, cbLang
);
543 /*************************************************************************
544 * VerQueryValue [VER.11]
546 DWORD WINAPI
VerQueryValue16( SEGPTR spvBlock
, LPSTR lpszSubBlock
,
547 SEGPTR
*lpspBuffer
, UINT16
*lpcb
)
549 LPVOID lpvBlock
= MapSL( spvBlock
);
550 LPVOID buffer
= lpvBlock
;
554 TRACE("(%p, %s, %p, %p)\n",
555 lpvBlock
, debugstr_a(lpszSubBlock
), lpspBuffer
, lpcb
);
557 retv
= VerQueryValueA( lpvBlock
, lpszSubBlock
, &buffer
, &buflen
);
558 if ( !retv
) return FALSE
;
560 if ( OFFSETOF( spvBlock
) + ((char *) buffer
- (char *) lpvBlock
) >= 0x10000 )
562 FIXME("offset %08X too large relative to %04X:%04X\n",
563 (char *) buffer
- (char *) lpvBlock
, SELECTOROF( spvBlock
), OFFSETOF( spvBlock
) );
567 if (lpcb
) *lpcb
= buflen
;
568 *lpspBuffer
= (SEGPTR
) ((char *) spvBlock
+ ((char *) buffer
- (char *) lpvBlock
));