e7d34e08a8841d576f18f990d0b18d8e18e0ff0e
[reactos.git] / dll / win32 / version / resource.c
1 /*
2 * Implementation of VERSION.DLL - Resource Access routines
3 *
4 * Copyright 1996,1997 Marcus Meissner
5 * Copyright 1997 David Cuthbert
6 * Copyright 1999 Ulrich Weigand
7 *
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.
12 *
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.
17 *
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
21 */
22
23 #include "config.h"
24
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
33
34 #define NONAMELESSUNION
35 #define NONAMELESSSTRUCT
36 #include "windef.h"
37 #include "winbase.h"
38 #include "lzexpand.h"
39 #include "winuser.h"
40 #include "winver.h"
41 #undef VS_FILE_INFO
42 #define VS_FILE_INFO 16
43
44 #include "wine/unicode.h"
45 #include "wine/winbase16.h"
46
47 #include "wine/debug.h"
48
49 WINE_DEFAULT_DEBUG_CHANNEL(ver);
50
51
52 /**********************************************************************
53 * find_entry_by_id
54 *
55 * Find an entry by id in a resource directory
56 * Copied from loader/pe_resource.c
57 */
58 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_id( const IMAGE_RESOURCE_DIRECTORY *dir,
59 WORD id, const void *root )
60 {
61 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
62 int min, max, pos;
63
64 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
65 min = dir->NumberOfNamedEntries;
66 max = min + dir->NumberOfIdEntries - 1;
67 while (min <= max)
68 {
69 pos = (min + max) / 2;
70 if (entry[pos].u1.s2.Id == id)
71 return (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry[pos].u2.s3.OffsetToDirectory);
72 if (entry[pos].u1.s2.Id > id) max = pos - 1;
73 else min = pos + 1;
74 }
75 return NULL;
76 }
77
78
79 /**********************************************************************
80 * find_entry_default
81 *
82 * Find a default entry in a resource directory
83 * Copied from loader/pe_resource.c
84 */
85 static const IMAGE_RESOURCE_DIRECTORY *find_entry_default( const IMAGE_RESOURCE_DIRECTORY *dir,
86 const void *root )
87 {
88 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
89
90 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
91 return (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry->u2.s3.OffsetToDirectory);
92 }
93
94
95 /***********************************************************************
96 * read_xx_header [internal]
97 */
98 static int read_xx_header( HFILE lzfd )
99 {
100 IMAGE_DOS_HEADER mzh;
101 char magic[3];
102
103 LZSeek( lzfd, 0, SEEK_SET );
104 if ( sizeof(mzh) != LZRead( lzfd, (LPSTR)&mzh, sizeof(mzh) ) )
105 return 0;
106 if ( mzh.e_magic != IMAGE_DOS_SIGNATURE )
107 {
108 if (!memcmp( &mzh, "\177ELF", 4 )) return 1; /* ELF */
109 if (*(UINT *)&mzh == 0xfeedface || *(UINT *)&mzh == 0xcefaedfe) return 1; /* Mach-O */
110 return 0;
111 }
112
113 LZSeek( lzfd, mzh.e_lfanew, SEEK_SET );
114 if ( 2 != LZRead( lzfd, magic, 2 ) )
115 return 0;
116
117 LZSeek( lzfd, mzh.e_lfanew, SEEK_SET );
118
119 if ( magic[0] == 'N' && magic[1] == 'E' )
120 return IMAGE_OS2_SIGNATURE;
121 if ( magic[0] == 'P' && magic[1] == 'E' )
122 return IMAGE_NT_SIGNATURE;
123
124 magic[2] = '\0';
125 WARN("Can't handle %s files.\n", magic );
126 return 0;
127 }
128
129 /***********************************************************************
130 * find_ne_resource [internal]
131 */
132 static BOOL find_ne_resource( HFILE lzfd, DWORD *resLen, DWORD *resOff )
133 {
134 const WORD typeid = VS_FILE_INFO | 0x8000;
135 const WORD resid = VS_VERSION_INFO | 0x8000;
136 IMAGE_OS2_HEADER nehd;
137 NE_TYPEINFO *typeInfo;
138 NE_NAMEINFO *nameInfo;
139 DWORD nehdoffset;
140 LPBYTE resTab;
141 DWORD resTabSize;
142 int count;
143
144 /* Read in NE header */
145 nehdoffset = LZSeek( lzfd, 0, SEEK_CUR );
146 if ( sizeof(nehd) != LZRead( lzfd, (LPSTR)&nehd, sizeof(nehd) ) ) return 0;
147
148 resTabSize = nehd.ne_restab - nehd.ne_rsrctab;
149 if ( !resTabSize )
150 {
151 TRACE("No resources in NE dll\n" );
152 return FALSE;
153 }
154
155 /* Read in resource table */
156 resTab = HeapAlloc( GetProcessHeap(), 0, resTabSize );
157 if ( !resTab ) return FALSE;
158
159 LZSeek( lzfd, nehd.ne_rsrctab + nehdoffset, SEEK_SET );
160 if ( resTabSize != LZRead( lzfd, (char*)resTab, resTabSize ) )
161 {
162 HeapFree( GetProcessHeap(), 0, resTab );
163 return FALSE;
164 }
165
166 /* Find resource */
167 typeInfo = (NE_TYPEINFO *)(resTab + 2);
168 while (typeInfo->type_id)
169 {
170 if (typeInfo->type_id == typeid) goto found_type;
171 typeInfo = (NE_TYPEINFO *)((char *)(typeInfo + 1) +
172 typeInfo->count * sizeof(NE_NAMEINFO));
173 }
174 TRACE("No typeid entry found\n" );
175 HeapFree( GetProcessHeap(), 0, resTab );
176 return FALSE;
177
178 found_type:
179 nameInfo = (NE_NAMEINFO *)(typeInfo + 1);
180
181 for (count = typeInfo->count; count > 0; count--, nameInfo++)
182 if (nameInfo->id == resid) goto found_name;
183
184 TRACE("No resid entry found\n" );
185 HeapFree( GetProcessHeap(), 0, resTab );
186 return FALSE;
187
188 found_name:
189 /* Return resource data */
190 if ( resLen ) *resLen = nameInfo->length << *(WORD *)resTab;
191 if ( resOff ) *resOff = nameInfo->offset << *(WORD *)resTab;
192
193 HeapFree( GetProcessHeap(), 0, resTab );
194 return TRUE;
195 }
196
197 /***********************************************************************
198 * find_pe_resource [internal]
199 */
200 static BOOL find_pe_resource( HFILE lzfd, DWORD *resLen, DWORD *resOff )
201 {
202 IMAGE_NT_HEADERS pehd;
203 DWORD pehdoffset;
204 PIMAGE_DATA_DIRECTORY resDataDir;
205 PIMAGE_SECTION_HEADER sections;
206 LPBYTE resSection;
207 DWORD resSectionSize;
208 const void *resDir;
209 const IMAGE_RESOURCE_DIRECTORY *resPtr;
210 const IMAGE_RESOURCE_DATA_ENTRY *resData;
211 int i, nSections;
212 BOOL ret = FALSE;
213
214 /* Read in PE header */
215 pehdoffset = LZSeek( lzfd, 0, SEEK_CUR );
216 if ( sizeof(pehd) != LZRead( lzfd, (LPSTR)&pehd, sizeof(pehd) ) ) return 0;
217
218 resDataDir = pehd.OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_RESOURCE;
219 if ( !resDataDir->Size )
220 {
221 TRACE("No resources in PE dll\n" );
222 return FALSE;
223 }
224
225 /* Read in section table */
226 nSections = pehd.FileHeader.NumberOfSections;
227 sections = HeapAlloc( GetProcessHeap(), 0,
228 nSections * sizeof(IMAGE_SECTION_HEADER) );
229 if ( !sections ) return FALSE;
230
231 LZSeek( lzfd, pehdoffset +
232 sizeof(DWORD) + /* Signature */
233 sizeof(IMAGE_FILE_HEADER) +
234 pehd.FileHeader.SizeOfOptionalHeader, SEEK_SET );
235
236 if ( nSections * sizeof(IMAGE_SECTION_HEADER) !=
237 LZRead( lzfd, (LPSTR)sections, nSections * sizeof(IMAGE_SECTION_HEADER) ) )
238 {
239 HeapFree( GetProcessHeap(), 0, sections );
240 return FALSE;
241 }
242
243 /* Find resource section */
244 for ( i = 0; i < nSections; i++ )
245 if ( resDataDir->VirtualAddress >= sections[i].VirtualAddress
246 && resDataDir->VirtualAddress < sections[i].VirtualAddress +
247 sections[i].SizeOfRawData )
248 break;
249
250 if ( i == nSections )
251 {
252 HeapFree( GetProcessHeap(), 0, sections );
253 TRACE("Couldn't find resource section\n" );
254 return FALSE;
255 }
256
257 /* Read in resource section */
258 resSectionSize = sections[i].SizeOfRawData;
259 resSection = HeapAlloc( GetProcessHeap(), 0, resSectionSize );
260 if ( !resSection )
261 {
262 HeapFree( GetProcessHeap(), 0, sections );
263 return FALSE;
264 }
265
266 LZSeek( lzfd, sections[i].PointerToRawData, SEEK_SET );
267 if ( resSectionSize != LZRead( lzfd, (char*)resSection, resSectionSize ) ) goto done;
268
269 /* Find resource */
270 resDir = resSection + (resDataDir->VirtualAddress - sections[i].VirtualAddress);
271
272 resPtr = resDir;
273 resPtr = find_entry_by_id( resPtr, VS_FILE_INFO, resDir );
274 if ( !resPtr )
275 {
276 TRACE("No typeid entry found\n" );
277 goto done;
278 }
279 resPtr = find_entry_by_id( resPtr, VS_VERSION_INFO, resDir );
280 if ( !resPtr )
281 {
282 TRACE("No resid entry found\n" );
283 goto done;
284 }
285 resPtr = find_entry_default( resPtr, resDir );
286 if ( !resPtr )
287 {
288 TRACE("No default language entry found\n" );
289 goto done;
290 }
291
292 /* Find resource data section */
293 resData = (const IMAGE_RESOURCE_DATA_ENTRY*)resPtr;
294 for ( i = 0; i < nSections; i++ )
295 if ( resData->OffsetToData >= sections[i].VirtualAddress
296 && resData->OffsetToData < sections[i].VirtualAddress +
297 sections[i].SizeOfRawData )
298 break;
299
300 if ( i == nSections )
301 {
302 TRACE("Couldn't find resource data section\n" );
303 goto done;
304 }
305
306 /* Return resource data */
307 if ( resLen ) *resLen = resData->Size;
308 if ( resOff ) *resOff = resData->OffsetToData - sections[i].VirtualAddress
309 + sections[i].PointerToRawData;
310 ret = TRUE;
311
312 done:
313 HeapFree( GetProcessHeap(), 0, resSection );
314 HeapFree( GetProcessHeap(), 0, sections );
315 return ret;
316 }
317
318
319 /***********************************************************************
320 * find_version_resource [internal]
321 */
322 DWORD find_version_resource( HFILE lzfd, DWORD *reslen, DWORD *offset )
323 {
324 DWORD magic = read_xx_header( lzfd );
325
326 switch (magic)
327 {
328 case IMAGE_OS2_SIGNATURE:
329 if (!find_ne_resource( lzfd, reslen, offset )) magic = 0;
330 break;
331 case IMAGE_NT_SIGNATURE:
332 if (!find_pe_resource( lzfd, reslen, offset )) magic = 0;
333 break;
334 }
335 return magic;
336 }