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
41 #include "wine/unicode.h"
42 #include "wine/debug.h"
61 WINE_DEFAULT_DEBUG_CHANNEL(ver);
64 /**********************************************************************
67 * Find an entry by id in a resource directory
68 * Copied from loader/pe_resource.c
70 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_id( const IMAGE_RESOURCE_DIRECTORY *dir,
71 WORD id, const void *root )
73 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
76 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
77 min = dir->NumberOfNamedEntries;
78 max = min + dir->NumberOfIdEntries - 1;
81 pos = (min + max) / 2;
82 if (entry[pos].u1.s2.Id == id)
83 return (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry[pos].u2.s3.OffsetToDirectory);
84 if (entry[pos].u1.s2.Id > id) max = pos - 1;
91 /**********************************************************************
94 * Find a default entry in a resource directory
95 * Copied from loader/pe_resource.c
97 static const IMAGE_RESOURCE_DIRECTORY *find_entry_default( const IMAGE_RESOURCE_DIRECTORY *dir,
100 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
102 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
103 return (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry->u2.s3.OffsetToDirectory);
107 /***********************************************************************
108 * read_xx_header [internal]
110 static int read_xx_header( HFILE lzfd )
112 IMAGE_DOS_HEADER mzh;
115 LZSeek( lzfd, 0, SEEK_SET );
116 if ( sizeof(mzh) != LZRead( lzfd, (LPSTR)&mzh, sizeof(mzh) ) )
118 if ( mzh.e_magic != IMAGE_DOS_SIGNATURE )
120 if (!memcmp( &mzh, "\177ELF", 4 )) return 1; /* ELF */
121 if (*(UINT *)&mzh == 0xfeedface || *(UINT *)&mzh == 0xcefaedfe) return 1; /* Mach-O */
125 LZSeek( lzfd, mzh.e_lfanew, SEEK_SET );
126 if ( 2 != LZRead( lzfd, magic, 2 ) )
129 LZSeek( lzfd, mzh.e_lfanew, SEEK_SET );
131 if ( magic[0] == 'N' && magic[1] == 'E' )
132 return IMAGE_OS2_SIGNATURE;
133 if ( magic[0] == 'P' && magic[1] == 'E' )
134 return IMAGE_NT_SIGNATURE;
137 WARN("Can't handle %s files.\n", magic );
141 /***********************************************************************
142 * find_ne_resource [internal]
144 static BOOL find_ne_resource( HFILE lzfd, DWORD *resLen, DWORD *resOff )
146 const WORD typeid = VS_FILE_INFO | 0x8000;
147 const WORD resid = VS_VERSION_INFO | 0x8000;
148 IMAGE_OS2_HEADER nehd;
149 NE_TYPEINFO *typeInfo;
150 NE_NAMEINFO *nameInfo;
156 /* Read in NE header */
157 nehdoffset = LZSeek( lzfd, 0, SEEK_CUR );
158 if ( sizeof(nehd) != LZRead( lzfd, (LPSTR)&nehd, sizeof(nehd) ) ) return 0;
160 resTabSize = nehd.ne_restab - nehd.ne_rsrctab;
163 TRACE("No resources in NE dll\n" );
167 /* Read in resource table */
168 resTab = HeapAlloc( GetProcessHeap(), 0, resTabSize );
169 if ( !resTab ) return FALSE;
171 LZSeek( lzfd, nehd.ne_rsrctab + nehdoffset, SEEK_SET );
172 if ( resTabSize != LZRead( lzfd, (char*)resTab, resTabSize ) )
174 HeapFree( GetProcessHeap(), 0, resTab );
179 typeInfo = (NE_TYPEINFO *)(resTab + 2);
180 while (typeInfo->type_id)
182 if (typeInfo->type_id == typeid) goto found_type;
183 typeInfo = (NE_TYPEINFO *)((char *)(typeInfo + 1) +
184 typeInfo->count * sizeof(NE_NAMEINFO));
186 TRACE("No typeid entry found\n" );
187 HeapFree( GetProcessHeap(), 0, resTab );
191 nameInfo = (NE_NAMEINFO *)(typeInfo + 1);
193 for (count = typeInfo->count; count > 0; count--, nameInfo++)
194 if (nameInfo->id == resid) goto found_name;
196 TRACE("No resid entry found\n" );
197 HeapFree( GetProcessHeap(), 0, resTab );
201 /* Return resource data */
202 if ( resLen ) *resLen = nameInfo->length << *(WORD *)resTab;
203 if ( resOff ) *resOff = nameInfo->offset << *(WORD *)resTab;
205 HeapFree( GetProcessHeap(), 0, resTab );
209 /***********************************************************************
210 * find_pe_resource [internal]
212 static BOOL find_pe_resource( HFILE lzfd, DWORD *resLen, DWORD *resOff )
216 IMAGE_NT_HEADERS32 nt32;
217 IMAGE_NT_HEADERS64 nt64;
220 PIMAGE_DATA_DIRECTORY resDataDir;
221 PIMAGE_SECTION_HEADER sections;
223 DWORD resSectionSize;
225 const IMAGE_RESOURCE_DIRECTORY *resPtr;
226 const IMAGE_RESOURCE_DATA_ENTRY *resData;
227 int i, len, nSections;
230 /* Read in PE header */
231 pehdoffset = LZSeek( lzfd, 0, SEEK_CUR );
232 len = LZRead( lzfd, (LPSTR)&pehd, sizeof(pehd) );
233 if (len < sizeof(pehd.nt32.FileHeader)) return 0;
234 if (len < sizeof(pehd)) memset( (char *)&pehd + len, 0, sizeof(pehd) - len );
236 switch (pehd.nt32.OptionalHeader.Magic)
238 case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
239 resDataDir = pehd.nt32.OptionalHeader.DataDirectory + IMAGE_DIRECTORY_ENTRY_RESOURCE;
241 case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
242 resDataDir = pehd.nt64.OptionalHeader.DataDirectory + IMAGE_DIRECTORY_ENTRY_RESOURCE;
248 if ( !resDataDir->Size )
250 TRACE("No resources in PE dll\n" );
254 /* Read in section table */
255 nSections = pehd.nt32.FileHeader.NumberOfSections;
256 sections = HeapAlloc( GetProcessHeap(), 0,
257 nSections * sizeof(IMAGE_SECTION_HEADER) );
258 if ( !sections ) return FALSE;
260 len = FIELD_OFFSET( IMAGE_NT_HEADERS32, OptionalHeader ) + pehd.nt32.FileHeader.SizeOfOptionalHeader;
261 LZSeek( lzfd, pehdoffset + len, SEEK_SET );
263 if ( nSections * sizeof(IMAGE_SECTION_HEADER) !=
264 LZRead( lzfd, (LPSTR)sections, nSections * sizeof(IMAGE_SECTION_HEADER) ) )
266 HeapFree( GetProcessHeap(), 0, sections );
270 /* Find resource section */
271 for ( i = 0; i < nSections; i++ )
272 if ( resDataDir->VirtualAddress >= sections[i].VirtualAddress
273 && resDataDir->VirtualAddress < sections[i].VirtualAddress +
274 sections[i].SizeOfRawData )
277 if ( i == nSections )
279 HeapFree( GetProcessHeap(), 0, sections );
280 TRACE("Couldn't find resource section\n" );
284 /* Read in resource section */
285 resSectionSize = sections[i].SizeOfRawData;
286 resSection = HeapAlloc( GetProcessHeap(), 0, resSectionSize );
289 HeapFree( GetProcessHeap(), 0, sections );
293 LZSeek( lzfd, sections[i].PointerToRawData, SEEK_SET );
294 if ( resSectionSize != LZRead( lzfd, (char*)resSection, resSectionSize ) ) goto done;
297 resDir = resSection + (resDataDir->VirtualAddress - sections[i].VirtualAddress);
300 resPtr = find_entry_by_id( resPtr, VS_FILE_INFO, resDir );
303 TRACE("No typeid entry found\n" );
306 resPtr = find_entry_by_id( resPtr, VS_VERSION_INFO, resDir );
309 TRACE("No resid entry found\n" );
312 resPtr = find_entry_default( resPtr, resDir );
315 TRACE("No default language entry found\n" );
319 /* Find resource data section */
320 resData = (const IMAGE_RESOURCE_DATA_ENTRY*)resPtr;
321 for ( i = 0; i < nSections; i++ )
322 if ( resData->OffsetToData >= sections[i].VirtualAddress
323 && resData->OffsetToData < sections[i].VirtualAddress +
324 sections[i].SizeOfRawData )
327 if ( i == nSections )
329 TRACE("Couldn't find resource data section\n" );
333 /* Return resource data */
334 if ( resLen ) *resLen = resData->Size;
335 if ( resOff ) *resOff = resData->OffsetToData - sections[i].VirtualAddress
336 + sections[i].PointerToRawData;
340 HeapFree( GetProcessHeap(), 0, resSection );
341 HeapFree( GetProcessHeap(), 0, sections );
346 /***********************************************************************
347 * find_version_resource [internal]
349 DWORD find_version_resource( HFILE lzfd, DWORD *reslen, DWORD *offset )
351 DWORD magic = read_xx_header( lzfd );
355 case IMAGE_OS2_SIGNATURE:
356 if (!find_ne_resource( lzfd, reslen, offset )) magic = 0;
358 case IMAGE_NT_SIGNATURE:
359 if (!find_pe_resource( lzfd, reslen, offset )) magic = 0;