ole32/tests: Mark the draw_continue callbacks as optional. Win9x can skip calls under...
[wine/wine-gecko.git] / dlls / version / resource.c
blob64d39dd195879a113d18d1c1c0ed3c13742405e1
1 /*
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
23 #include "config.h"
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sys/types.h>
30 #ifdef HAVE_UNISTD_H
31 # include <unistd.h>
32 #endif
34 #define NONAMELESSUNION
35 #define NONAMELESSSTRUCT
36 #include "windef.h"
37 #include "winbase.h"
38 #include "lzexpand.h"
39 #include "winver.h"
41 #include "wine/unicode.h"
42 #include "wine/debug.h"
44 typedef struct
46 WORD offset;
47 WORD length;
48 WORD flags;
49 WORD id;
50 WORD handle;
51 WORD usage;
52 } NE_NAMEINFO;
54 typedef struct
56 WORD type_id;
57 WORD count;
58 DWORD resloader;
59 } NE_TYPEINFO;
61 WINE_DEFAULT_DEBUG_CHANNEL(ver);
64 /**********************************************************************
65 * find_entry_by_id
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;
74 int min, max, pos;
76 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
77 min = dir->NumberOfNamedEntries;
78 max = min + dir->NumberOfIdEntries - 1;
79 while (min <= max)
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;
85 else min = pos + 1;
87 return NULL;
91 /**********************************************************************
92 * find_entry_default
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,
98 const void *root )
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;
113 char magic[3];
115 LZSeek( lzfd, 0, SEEK_SET );
116 if ( sizeof(mzh) != LZRead( lzfd, (LPSTR)&mzh, sizeof(mzh) ) )
117 return 0;
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 */
122 return 0;
125 LZSeek( lzfd, mzh.e_lfanew, SEEK_SET );
126 if ( 2 != LZRead( lzfd, magic, 2 ) )
127 return 0;
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;
136 magic[2] = '\0';
137 WARN("Can't handle %s files.\n", magic );
138 return 0;
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;
151 DWORD nehdoffset;
152 LPBYTE resTab;
153 DWORD resTabSize;
154 int count;
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;
161 if ( !resTabSize )
163 TRACE("No resources in NE dll\n" );
164 return FALSE;
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 );
175 return FALSE;
178 /* Find resource */
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 );
188 return FALSE;
190 found_type:
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 );
198 return FALSE;
200 found_name:
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 );
206 return TRUE;
209 /***********************************************************************
210 * find_pe_resource [internal]
212 static BOOL find_pe_resource( HFILE lzfd, DWORD *resLen, DWORD *resOff )
214 union
216 IMAGE_NT_HEADERS32 nt32;
217 IMAGE_NT_HEADERS64 nt64;
218 } pehd;
219 DWORD pehdoffset;
220 PIMAGE_DATA_DIRECTORY resDataDir;
221 PIMAGE_SECTION_HEADER sections;
222 LPBYTE resSection;
223 DWORD resSectionSize;
224 const void *resDir;
225 const IMAGE_RESOURCE_DIRECTORY *resPtr;
226 const IMAGE_RESOURCE_DATA_ENTRY *resData;
227 int i, len, nSections;
228 BOOL ret = FALSE;
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;
240 break;
241 case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
242 resDataDir = pehd.nt64.OptionalHeader.DataDirectory + IMAGE_DIRECTORY_ENTRY_RESOURCE;
243 break;
244 default:
245 return 0;
248 if ( !resDataDir->Size )
250 TRACE("No resources in PE dll\n" );
251 return FALSE;
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 );
267 return FALSE;
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 )
275 break;
277 if ( i == nSections )
279 HeapFree( GetProcessHeap(), 0, sections );
280 TRACE("Couldn't find resource section\n" );
281 return FALSE;
284 /* Read in resource section */
285 resSectionSize = sections[i].SizeOfRawData;
286 resSection = HeapAlloc( GetProcessHeap(), 0, resSectionSize );
287 if ( !resSection )
289 HeapFree( GetProcessHeap(), 0, sections );
290 return FALSE;
293 LZSeek( lzfd, sections[i].PointerToRawData, SEEK_SET );
294 if ( resSectionSize != LZRead( lzfd, (char*)resSection, resSectionSize ) ) goto done;
296 /* Find resource */
297 resDir = resSection + (resDataDir->VirtualAddress - sections[i].VirtualAddress);
299 resPtr = resDir;
300 resPtr = find_entry_by_id( resPtr, VS_FILE_INFO, resDir );
301 if ( !resPtr )
303 TRACE("No typeid entry found\n" );
304 goto done;
306 resPtr = find_entry_by_id( resPtr, VS_VERSION_INFO, resDir );
307 if ( !resPtr )
309 TRACE("No resid entry found\n" );
310 goto done;
312 resPtr = find_entry_default( resPtr, resDir );
313 if ( !resPtr )
315 TRACE("No default language entry found\n" );
316 goto done;
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 )
325 break;
327 if ( i == nSections )
329 TRACE("Couldn't find resource data section\n" );
330 goto done;
333 /* Return resource data */
334 if ( resLen ) *resLen = resData->Size;
335 if ( resOff ) *resOff = resData->OffsetToData - sections[i].VirtualAddress
336 + sections[i].PointerToRawData;
337 ret = TRUE;
339 done:
340 HeapFree( GetProcessHeap(), 0, resSection );
341 HeapFree( GetProcessHeap(), 0, sections );
342 return ret;
346 /***********************************************************************
347 * find_version_resource [internal]
349 DWORD find_version_resource( HFILE lzfd, DWORD *reslen, DWORD *offset )
351 DWORD magic = read_xx_header( lzfd );
353 switch (magic)
355 case IMAGE_OS2_SIGNATURE:
356 if (!find_ne_resource( lzfd, reslen, offset )) magic = 0;
357 break;
358 case IMAGE_NT_SIGNATURE:
359 if (!find_pe_resource( lzfd, reslen, offset )) magic = 0;
360 break;
362 return magic;