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/winbase16.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(ver);
49 /**********************************************************************
52 * Find an entry by id in a resource directory
53 * Copied from loader/pe_resource.c
55 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_id( const IMAGE_RESOURCE_DIRECTORY *dir,
56 WORD id, const void *root )
58 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
61 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
62 min = dir->NumberOfNamedEntries;
63 max = min + dir->NumberOfIdEntries - 1;
66 pos = (min + max) / 2;
67 if (entry[pos].u1.s2.Id == id)
68 return (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry[pos].u2.s3.OffsetToDirectory);
69 if (entry[pos].u1.s2.Id > id) max = pos - 1;
76 /**********************************************************************
79 * Find a default entry in a resource directory
80 * Copied from loader/pe_resource.c
82 static const IMAGE_RESOURCE_DIRECTORY *find_entry_default( const IMAGE_RESOURCE_DIRECTORY *dir,
85 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
87 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
88 return (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry->u2.s3.OffsetToDirectory);
92 /***********************************************************************
93 * read_xx_header [internal]
95 static int read_xx_header( HFILE lzfd )
100 LZSeek( lzfd, 0, SEEK_SET );
101 if ( sizeof(mzh) != LZRead( lzfd, (LPSTR)&mzh, sizeof(mzh) ) )
103 if ( mzh.e_magic != IMAGE_DOS_SIGNATURE )
105 if (!memcmp( &mzh, "\177ELF", 4 )) return 1; /* ELF */
106 if (*(UINT *)&mzh == 0xfeedface || *(UINT *)&mzh == 0xcefaedfe) return 1; /* Mach-O */
110 LZSeek( lzfd, mzh.e_lfanew, SEEK_SET );
111 if ( 2 != LZRead( lzfd, magic, 2 ) )
114 LZSeek( lzfd, mzh.e_lfanew, SEEK_SET );
116 if ( magic[0] == 'N' && magic[1] == 'E' )
117 return IMAGE_OS2_SIGNATURE;
118 if ( magic[0] == 'P' && magic[1] == 'E' )
119 return IMAGE_NT_SIGNATURE;
122 WARN("Can't handle %s files.\n", magic );
126 /***********************************************************************
127 * find_ne_resource [internal]
129 static BOOL find_ne_resource( HFILE lzfd, DWORD *resLen, DWORD *resOff )
131 const WORD typeid = VS_FILE_INFO | 0x8000;
132 const WORD resid = VS_VERSION_INFO | 0x8000;
133 IMAGE_OS2_HEADER nehd;
134 NE_TYPEINFO *typeInfo;
135 NE_NAMEINFO *nameInfo;
141 /* Read in NE header */
142 nehdoffset = LZSeek( lzfd, 0, SEEK_CUR );
143 if ( sizeof(nehd) != LZRead( lzfd, (LPSTR)&nehd, sizeof(nehd) ) ) return 0;
145 resTabSize = nehd.ne_restab - nehd.ne_rsrctab;
148 TRACE("No resources in NE dll\n" );
152 /* Read in resource table */
153 resTab = HeapAlloc( GetProcessHeap(), 0, resTabSize );
154 if ( !resTab ) return FALSE;
156 LZSeek( lzfd, nehd.ne_rsrctab + nehdoffset, SEEK_SET );
157 if ( resTabSize != LZRead( lzfd, (char*)resTab, resTabSize ) )
159 HeapFree( GetProcessHeap(), 0, resTab );
164 typeInfo = (NE_TYPEINFO *)(resTab + 2);
165 while (typeInfo->type_id)
167 if (typeInfo->type_id == typeid) goto found_type;
168 typeInfo = (NE_TYPEINFO *)((char *)(typeInfo + 1) +
169 typeInfo->count * sizeof(NE_NAMEINFO));
171 TRACE("No typeid entry found\n" );
172 HeapFree( GetProcessHeap(), 0, resTab );
176 nameInfo = (NE_NAMEINFO *)(typeInfo + 1);
178 for (count = typeInfo->count; count > 0; count--, nameInfo++)
179 if (nameInfo->id == resid) goto found_name;
181 TRACE("No resid entry found\n" );
182 HeapFree( GetProcessHeap(), 0, resTab );
186 /* Return resource data */
187 if ( resLen ) *resLen = nameInfo->length << *(WORD *)resTab;
188 if ( resOff ) *resOff = nameInfo->offset << *(WORD *)resTab;
190 HeapFree( GetProcessHeap(), 0, resTab );
194 /***********************************************************************
195 * find_pe_resource [internal]
197 static BOOL find_pe_resource( HFILE lzfd, DWORD *resLen, DWORD *resOff )
201 IMAGE_NT_HEADERS32 nt32;
202 IMAGE_NT_HEADERS64 nt64;
205 PIMAGE_DATA_DIRECTORY resDataDir;
206 PIMAGE_SECTION_HEADER sections;
208 DWORD resSectionSize;
210 const IMAGE_RESOURCE_DIRECTORY *resPtr;
211 const IMAGE_RESOURCE_DATA_ENTRY *resData;
212 int i, len, nSections;
215 /* Read in PE header */
216 pehdoffset = LZSeek( lzfd, 0, SEEK_CUR );
217 len = LZRead( lzfd, (LPSTR)&pehd, sizeof(pehd) );
218 if (len < sizeof(pehd.nt32.FileHeader)) return 0;
219 if (len < sizeof(pehd)) memset( (char *)&pehd + len, 0, sizeof(pehd) - len );
221 switch (pehd.nt32.OptionalHeader.Magic)
223 case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
224 resDataDir = pehd.nt32.OptionalHeader.DataDirectory + IMAGE_DIRECTORY_ENTRY_RESOURCE;
226 case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
227 resDataDir = pehd.nt64.OptionalHeader.DataDirectory + IMAGE_DIRECTORY_ENTRY_RESOURCE;
233 if ( !resDataDir->Size )
235 TRACE("No resources in PE dll\n" );
239 /* Read in section table */
240 nSections = pehd.nt32.FileHeader.NumberOfSections;
241 sections = HeapAlloc( GetProcessHeap(), 0,
242 nSections * sizeof(IMAGE_SECTION_HEADER) );
243 if ( !sections ) return FALSE;
245 len = FIELD_OFFSET( IMAGE_NT_HEADERS32, OptionalHeader ) + pehd.nt32.FileHeader.SizeOfOptionalHeader;
246 LZSeek( lzfd, pehdoffset + len, SEEK_SET );
248 if ( nSections * sizeof(IMAGE_SECTION_HEADER) !=
249 LZRead( lzfd, (LPSTR)sections, nSections * sizeof(IMAGE_SECTION_HEADER) ) )
251 HeapFree( GetProcessHeap(), 0, sections );
255 /* Find resource section */
256 for ( i = 0; i < nSections; i++ )
257 if ( resDataDir->VirtualAddress >= sections[i].VirtualAddress
258 && resDataDir->VirtualAddress < sections[i].VirtualAddress +
259 sections[i].SizeOfRawData )
262 if ( i == nSections )
264 HeapFree( GetProcessHeap(), 0, sections );
265 TRACE("Couldn't find resource section\n" );
269 /* Read in resource section */
270 resSectionSize = sections[i].SizeOfRawData;
271 resSection = HeapAlloc( GetProcessHeap(), 0, resSectionSize );
274 HeapFree( GetProcessHeap(), 0, sections );
278 LZSeek( lzfd, sections[i].PointerToRawData, SEEK_SET );
279 if ( resSectionSize != LZRead( lzfd, (char*)resSection, resSectionSize ) ) goto done;
282 resDir = resSection + (resDataDir->VirtualAddress - sections[i].VirtualAddress);
285 resPtr = find_entry_by_id( resPtr, VS_FILE_INFO, resDir );
288 TRACE("No typeid entry found\n" );
291 resPtr = find_entry_by_id( resPtr, VS_VERSION_INFO, resDir );
294 TRACE("No resid entry found\n" );
297 resPtr = find_entry_default( resPtr, resDir );
300 TRACE("No default language entry found\n" );
304 /* Find resource data section */
305 resData = (const IMAGE_RESOURCE_DATA_ENTRY*)resPtr;
306 for ( i = 0; i < nSections; i++ )
307 if ( resData->OffsetToData >= sections[i].VirtualAddress
308 && resData->OffsetToData < sections[i].VirtualAddress +
309 sections[i].SizeOfRawData )
312 if ( i == nSections )
314 TRACE("Couldn't find resource data section\n" );
318 /* Return resource data */
319 if ( resLen ) *resLen = resData->Size;
320 if ( resOff ) *resOff = resData->OffsetToData - sections[i].VirtualAddress
321 + sections[i].PointerToRawData;
325 HeapFree( GetProcessHeap(), 0, resSection );
326 HeapFree( GetProcessHeap(), 0, sections );
331 /***********************************************************************
332 * find_version_resource [internal]
334 DWORD find_version_resource( HFILE lzfd, DWORD *reslen, DWORD *offset )
336 DWORD magic = read_xx_header( lzfd );
340 case IMAGE_OS2_SIGNATURE:
341 if (!find_ne_resource( lzfd, reslen, offset )) magic = 0;
343 case IMAGE_NT_SIGNATURE:
344 if (!find_pe_resource( lzfd, reslen, offset )) magic = 0;