2 * Implementation of VERSION.DLL - Resource Access routines
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
29 #include <sys/types.h>
34 #define NONAMELESSUNION
35 #define NONAMELESSSTRUCT
42 #define VS_FILE_INFO 16
44 #include "wine/unicode.h"
46 #include "wine/debug.h"
65 WINE_DEFAULT_DEBUG_CHANNEL(ver
);
68 /**********************************************************************
71 * Find an entry by id in a resource directory
72 * Copied from loader/pe_resource.c
74 static const IMAGE_RESOURCE_DIRECTORY
*find_entry_by_id( const IMAGE_RESOURCE_DIRECTORY
*dir
,
75 WORD id
, const void *root
)
77 const IMAGE_RESOURCE_DIRECTORY_ENTRY
*entry
;
80 entry
= (const IMAGE_RESOURCE_DIRECTORY_ENTRY
*)(dir
+ 1);
81 min
= dir
->NumberOfNamedEntries
;
82 max
= min
+ dir
->NumberOfIdEntries
- 1;
85 pos
= (min
+ max
) / 2;
86 if (entry
[pos
].u1
.Id
== id
)
87 return (const IMAGE_RESOURCE_DIRECTORY
*)((const char *)root
+ entry
[pos
].u2
.s3
.OffsetToDirectory
);
88 if (entry
[pos
].u1
.Id
> id
) max
= pos
- 1;
95 /**********************************************************************
98 * Find a default entry in a resource directory
99 * Copied from loader/pe_resource.c
101 static const IMAGE_RESOURCE_DIRECTORY
*find_entry_default( const IMAGE_RESOURCE_DIRECTORY
*dir
,
104 const IMAGE_RESOURCE_DIRECTORY_ENTRY
*entry
;
106 entry
= (const IMAGE_RESOURCE_DIRECTORY_ENTRY
*)(dir
+ 1);
107 return (const IMAGE_RESOURCE_DIRECTORY
*)((const char *)root
+ entry
->u2
.s3
.OffsetToDirectory
);
111 /***********************************************************************
112 * read_xx_header [internal]
114 static int read_xx_header( HFILE lzfd
)
116 IMAGE_DOS_HEADER mzh
;
119 LZSeek( lzfd
, 0, SEEK_SET
);
120 if ( sizeof(mzh
) != LZRead( lzfd
, (LPSTR
)&mzh
, sizeof(mzh
) ) )
122 if ( mzh
.e_magic
!= IMAGE_DOS_SIGNATURE
)
124 if (!memcmp( &mzh
, "\177ELF", 4 )) return 1; /* ELF */
125 if (*(UINT
*)&mzh
== 0xfeedface || *(UINT
*)&mzh
== 0xcefaedfe) return 1; /* Mach-O */
129 LZSeek( lzfd
, mzh
.e_lfanew
, SEEK_SET
);
130 if ( 2 != LZRead( lzfd
, magic
, 2 ) )
133 LZSeek( lzfd
, mzh
.e_lfanew
, SEEK_SET
);
135 if ( magic
[0] == 'N' && magic
[1] == 'E' )
136 return IMAGE_OS2_SIGNATURE
;
137 if ( magic
[0] == 'P' && magic
[1] == 'E' )
138 return IMAGE_NT_SIGNATURE
;
141 WARN("Can't handle %s files.\n", magic
);
145 /***********************************************************************
146 * find_ne_resource [internal]
148 static BOOL
find_ne_resource( HFILE lzfd
, DWORD
*resLen
, DWORD
*resOff
)
150 const WORD
typeid = VS_FILE_INFO
| 0x8000;
151 const WORD resid
= VS_VERSION_INFO
| 0x8000;
152 IMAGE_OS2_HEADER nehd
;
153 NE_TYPEINFO
*typeInfo
;
154 NE_NAMEINFO
*nameInfo
;
160 /* Read in NE header */
161 nehdoffset
= LZSeek( lzfd
, 0, SEEK_CUR
);
162 if ( sizeof(nehd
) != LZRead( lzfd
, (LPSTR
)&nehd
, sizeof(nehd
) ) ) return 0;
164 resTabSize
= nehd
.ne_restab
- nehd
.ne_rsrctab
;
167 TRACE("No resources in NE dll\n" );
171 /* Read in resource table */
172 resTab
= HeapAlloc( GetProcessHeap(), 0, resTabSize
);
173 if ( !resTab
) return FALSE
;
175 LZSeek( lzfd
, nehd
.ne_rsrctab
+ nehdoffset
, SEEK_SET
);
176 if ( resTabSize
!= LZRead( lzfd
, (char*)resTab
, resTabSize
) )
178 HeapFree( GetProcessHeap(), 0, resTab
);
183 typeInfo
= (NE_TYPEINFO
*)(resTab
+ 2);
184 while (typeInfo
->type_id
)
186 if (typeInfo
->type_id
== typeid) goto found_type
;
187 typeInfo
= (NE_TYPEINFO
*)((char *)(typeInfo
+ 1) +
188 typeInfo
->count
* sizeof(NE_NAMEINFO
));
190 TRACE("No typeid entry found\n" );
191 HeapFree( GetProcessHeap(), 0, resTab
);
195 nameInfo
= (NE_NAMEINFO
*)(typeInfo
+ 1);
197 for (count
= typeInfo
->count
; count
> 0; count
--, nameInfo
++)
198 if (nameInfo
->id
== resid
) goto found_name
;
200 TRACE("No resid entry found\n" );
201 HeapFree( GetProcessHeap(), 0, resTab
);
205 /* Return resource data */
206 if ( resLen
) *resLen
= nameInfo
->length
<< *(WORD
*)resTab
;
207 if ( resOff
) *resOff
= nameInfo
->offset
<< *(WORD
*)resTab
;
209 HeapFree( GetProcessHeap(), 0, resTab
);
213 /***********************************************************************
214 * find_pe_resource [internal]
216 static BOOL
find_pe_resource( HFILE lzfd
, DWORD
*resLen
, DWORD
*resOff
)
220 IMAGE_NT_HEADERS32 nt32
;
221 IMAGE_NT_HEADERS64 nt64
;
224 PIMAGE_DATA_DIRECTORY resDataDir
;
225 PIMAGE_SECTION_HEADER sections
;
227 DWORD resSectionSize
;
229 const IMAGE_RESOURCE_DIRECTORY
*resPtr
;
230 const IMAGE_RESOURCE_DATA_ENTRY
*resData
;
231 int i
, len
, nSections
;
234 /* Read in PE header */
235 pehdoffset
= LZSeek( lzfd
, 0, SEEK_CUR
);
236 len
= LZRead( lzfd
, (LPSTR
)&pehd
, sizeof(pehd
) );
237 if (len
< sizeof(pehd
.nt32
.FileHeader
)) return 0;
238 if (len
< sizeof(pehd
)) memset( (char *)&pehd
+ len
, 0, sizeof(pehd
) - len
);
240 switch (pehd
.nt32
.OptionalHeader
.Magic
)
242 case IMAGE_NT_OPTIONAL_HDR32_MAGIC
:
243 resDataDir
= pehd
.nt32
.OptionalHeader
.DataDirectory
+ IMAGE_DIRECTORY_ENTRY_RESOURCE
;
245 case IMAGE_NT_OPTIONAL_HDR64_MAGIC
:
246 resDataDir
= pehd
.nt64
.OptionalHeader
.DataDirectory
+ IMAGE_DIRECTORY_ENTRY_RESOURCE
;
252 if ( !resDataDir
->Size
)
254 TRACE("No resources in PE dll\n" );
258 /* Read in section table */
259 nSections
= pehd
.nt32
.FileHeader
.NumberOfSections
;
260 sections
= HeapAlloc( GetProcessHeap(), 0,
261 nSections
* sizeof(IMAGE_SECTION_HEADER
) );
262 if ( !sections
) return FALSE
;
264 len
= FIELD_OFFSET( IMAGE_NT_HEADERS32
, OptionalHeader
) + pehd
.nt32
.FileHeader
.SizeOfOptionalHeader
;
265 LZSeek( lzfd
, pehdoffset
+ len
, SEEK_SET
);
267 if ( nSections
* sizeof(IMAGE_SECTION_HEADER
) !=
268 LZRead( lzfd
, (LPSTR
)sections
, nSections
* sizeof(IMAGE_SECTION_HEADER
) ) )
270 HeapFree( GetProcessHeap(), 0, sections
);
274 /* Find resource section */
275 for ( i
= 0; i
< nSections
; i
++ )
276 if ( resDataDir
->VirtualAddress
>= sections
[i
].VirtualAddress
277 && resDataDir
->VirtualAddress
< sections
[i
].VirtualAddress
+
278 sections
[i
].SizeOfRawData
)
281 if ( i
== nSections
)
283 HeapFree( GetProcessHeap(), 0, sections
);
284 TRACE("Couldn't find resource section\n" );
288 /* Read in resource section */
289 resSectionSize
= sections
[i
].SizeOfRawData
;
290 resSection
= HeapAlloc( GetProcessHeap(), 0, resSectionSize
);
293 HeapFree( GetProcessHeap(), 0, sections
);
297 LZSeek( lzfd
, sections
[i
].PointerToRawData
, SEEK_SET
);
298 if ( resSectionSize
!= LZRead( lzfd
, (char*)resSection
, resSectionSize
) ) goto done
;
301 resDir
= resSection
+ (resDataDir
->VirtualAddress
- sections
[i
].VirtualAddress
);
304 resPtr
= find_entry_by_id( resPtr
, VS_FILE_INFO
, resDir
);
307 TRACE("No typeid entry found\n" );
310 resPtr
= find_entry_by_id( resPtr
, VS_VERSION_INFO
, resDir
);
313 TRACE("No resid entry found\n" );
316 resPtr
= find_entry_default( resPtr
, resDir
);
319 TRACE("No default language entry found\n" );
323 /* Find resource data section */
324 resData
= (const IMAGE_RESOURCE_DATA_ENTRY
*)resPtr
;
325 for ( i
= 0; i
< nSections
; i
++ )
326 if ( resData
->OffsetToData
>= sections
[i
].VirtualAddress
327 && resData
->OffsetToData
< sections
[i
].VirtualAddress
+
328 sections
[i
].SizeOfRawData
)
331 if ( i
== nSections
)
333 TRACE("Couldn't find resource data section\n" );
337 /* Return resource data */
338 if ( resLen
) *resLen
= resData
->Size
;
339 if ( resOff
) *resOff
= resData
->OffsetToData
- sections
[i
].VirtualAddress
340 + sections
[i
].PointerToRawData
;
344 HeapFree( GetProcessHeap(), 0, resSection
);
345 HeapFree( GetProcessHeap(), 0, sections
);
350 /***********************************************************************
351 * find_version_resource [internal]
353 DWORD
find_version_resource( HFILE lzfd
, DWORD
*reslen
, DWORD
*offset
)
355 DWORD magic
= read_xx_header( lzfd
);
359 case IMAGE_OS2_SIGNATURE
:
360 if (!find_ne_resource( lzfd
, reslen
, offset
)) magic
= 0;
362 case IMAGE_NT_SIGNATURE
:
363 if (!find_pe_resource( lzfd
, reslen
, offset
)) magic
= 0;