a1123c55bf7064ad44ba87ff7866cf31c33190c0
[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
40 #include "wine/unicode.h"
41 #include "wine/winbase16.h"
42
43 #include "wine/debug.h"
44
45 /* winnt.h */
46 #define IMAGE_FILE_RESOURCE_DIRECTORY 2
47
48 WINE_DEFAULT_DEBUG_CHANNEL(ver);
49
50
51 /**********************************************************************
52 * find_entry_by_id
53 *
54 * Find an entry by id in a resource directory
55 * Copied from loader/pe_resource.c
56 */
57 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_id( const IMAGE_RESOURCE_DIRECTORY *dir,
58 WORD id, const void *root )
59 {
60 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
61 int min, max, pos;
62
63 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
64 min = dir->NumberOfNamedEntries;
65 max = min + dir->NumberOfIdEntries - 1;
66 while (min <= max)
67 {
68 pos = (min + max) / 2;
69 if (entry[pos].u1.s2.Id == id)
70 return (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry[pos].u2.s3.OffsetToDirectory);
71 if (entry[pos].u1.s2.Id > id) max = pos - 1;
72 else min = pos + 1;
73 }
74 return NULL;
75 }
76
77
78 /**********************************************************************
79 * find_entry_default
80 *
81 * Find a default entry in a resource directory
82 * Copied from loader/pe_resource.c
83 */
84 static const IMAGE_RESOURCE_DIRECTORY *find_entry_default( const IMAGE_RESOURCE_DIRECTORY *dir,
85 const void *root )
86 {
87 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
88
89 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
90 return (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry->u2.s3.OffsetToDirectory);
91 }
92
93
94 /**********************************************************************
95 * find_entry_by_name
96 *
97 * Find an entry by name in a resource directory
98 * Copied from loader/pe_resource.c
99 */
100 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_name( const IMAGE_RESOURCE_DIRECTORY *dir,
101 LPCSTR name, const void *root )
102 {
103 const IMAGE_RESOURCE_DIRECTORY *ret = NULL;
104 LPWSTR nameW;
105 DWORD namelen;
106
107 if (!HIWORD(name)) return find_entry_by_id( dir, LOWORD(name), root );
108 if (name[0] == '#')
109 {
110 return find_entry_by_id( dir, atoi(name+1), root );
111 }
112
113 namelen = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
114 if ((nameW = HeapAlloc( GetProcessHeap(), 0, namelen * sizeof(WCHAR) )))
115 {
116 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
117 const IMAGE_RESOURCE_DIR_STRING_U *str;
118 int min, max, res, pos;
119
120 MultiByteToWideChar( CP_ACP, 0, name, -1, nameW, namelen );
121 namelen--; /* remove terminating null */
122 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
123 min = 0;
124 max = dir->NumberOfNamedEntries - 1;
125 while (min <= max)
126 {
127 pos = (min + max) / 2;
128 str = (const IMAGE_RESOURCE_DIR_STRING_U *)((const char *)root + entry[pos].u1.s1.NameOffset);
129 res = strncmpiW( nameW, str->NameString, str->Length );
130 if (!res && namelen == str->Length)
131 {
132 ret = (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry[pos].u2.s3.OffsetToDirectory);
133 break;
134 }
135 if (res < 0) max = pos - 1;
136 else min = pos + 1;
137 }
138 HeapFree( GetProcessHeap(), 0, nameW );
139 }
140 return ret;
141 }
142
143
144 /***********************************************************************
145 * read_xx_header [internal]
146 */
147 static int read_xx_header( HFILE lzfd )
148 {
149 IMAGE_DOS_HEADER mzh;
150 char magic[3];
151
152 LZSeek( lzfd, 0, SEEK_SET );
153 if ( sizeof(mzh) != LZRead( lzfd, (LPSTR)&mzh, sizeof(mzh) ) )
154 return 0;
155 if ( mzh.e_magic != IMAGE_DOS_SIGNATURE )
156 return 0;
157
158 LZSeek( lzfd, mzh.e_lfanew, SEEK_SET );
159 if ( 2 != LZRead( lzfd, magic, 2 ) )
160 return 0;
161
162 LZSeek( lzfd, mzh.e_lfanew, SEEK_SET );
163
164 if ( magic[0] == 'N' && magic[1] == 'E' )
165 return IMAGE_OS2_SIGNATURE;
166 if ( magic[0] == 'P' && magic[1] == 'E' )
167 return IMAGE_NT_SIGNATURE;
168
169 magic[2] = '\0';
170 WARN("Can't handle %s files.\n", magic );
171 return 0;
172 }
173
174 #ifndef __REACTOS__
175 /***********************************************************************
176 * find_ne_resource [internal]
177 */
178 static BOOL find_ne_resource( HFILE lzfd, LPCSTR typeid, LPCSTR resid,
179 DWORD *resLen, DWORD *resOff )
180 {
181 IMAGE_OS2_HEADER nehd;
182 NE_TYPEINFO *typeInfo;
183 NE_NAMEINFO *nameInfo;
184 DWORD nehdoffset;
185 LPBYTE resTab;
186 DWORD resTabSize;
187 int count;
188
189 /* Read in NE header */
190 nehdoffset = LZSeek( lzfd, 0, SEEK_CUR );
191 if ( sizeof(nehd) != LZRead( lzfd, (LPSTR)&nehd, sizeof(nehd) ) ) return 0;
192
193 resTabSize = nehd.ne_restab - nehd.ne_rsrctab;
194 if ( !resTabSize )
195 {
196 TRACE("No resources in NE dll\n" );
197 return FALSE;
198 }
199
200 /* Read in resource table */
201 resTab = HeapAlloc( GetProcessHeap(), 0, resTabSize );
202 if ( !resTab ) return FALSE;
203
204 LZSeek( lzfd, nehd.ne_rsrctab + nehdoffset, SEEK_SET );
205 if ( resTabSize != LZRead( lzfd, (char*)resTab, resTabSize ) )
206 {
207 HeapFree( GetProcessHeap(), 0, resTab );
208 return FALSE;
209 }
210
211 /* Find resource */
212 typeInfo = (NE_TYPEINFO *)(resTab + 2);
213
214 if (HIWORD(typeid) != 0) /* named type */
215 {
216 BYTE len = strlen( typeid );
217 while (typeInfo->type_id)
218 {
219 if (!(typeInfo->type_id & 0x8000))
220 {
221 BYTE *p = resTab + typeInfo->type_id;
222 if ((*p == len) && !strncasecmp( (char*)p+1, typeid, len )) goto found_type;
223 }
224 typeInfo = (NE_TYPEINFO *)((char *)(typeInfo + 1) +
225 typeInfo->count * sizeof(NE_NAMEINFO));
226 }
227 }
228 else /* numeric type id */
229 {
230 WORD id = LOWORD(typeid) | 0x8000;
231 while (typeInfo->type_id)
232 {
233 if (typeInfo->type_id == id) goto found_type;
234 typeInfo = (NE_TYPEINFO *)((char *)(typeInfo + 1) +
235 typeInfo->count * sizeof(NE_NAMEINFO));
236 }
237 }
238 TRACE("No typeid entry found for %p\n", typeid );
239 HeapFree( GetProcessHeap(), 0, resTab );
240 return FALSE;
241
242 found_type:
243 nameInfo = (NE_NAMEINFO *)(typeInfo + 1);
244
245 if (HIWORD(resid) != 0) /* named resource */
246 {
247 BYTE len = strlen( resid );
248 for (count = typeInfo->count; count > 0; count--, nameInfo++)
249 {
250 BYTE *p = resTab + nameInfo->id;
251 if (nameInfo->id & 0x8000) continue;
252 if ((*p == len) && !strncasecmp( (char*)p+1, resid, len )) goto found_name;
253 }
254 }
255 else /* numeric resource id */
256 {
257 WORD id = LOWORD(resid) | 0x8000;
258 for (count = typeInfo->count; count > 0; count--, nameInfo++)
259 if (nameInfo->id == id) goto found_name;
260 }
261 TRACE("No resid entry found for %p\n", typeid );
262 HeapFree( GetProcessHeap(), 0, resTab );
263 return FALSE;
264
265 found_name:
266 /* Return resource data */
267 if ( resLen ) *resLen = nameInfo->length << *(WORD *)resTab;
268 if ( resOff ) *resOff = nameInfo->offset << *(WORD *)resTab;
269
270 HeapFree( GetProcessHeap(), 0, resTab );
271 return TRUE;
272 }
273 #endif /* ! __REACTOS__ */
274
275 /***********************************************************************
276 * find_pe_resource [internal]
277 */
278 static BOOL find_pe_resource( HFILE lzfd, LPCSTR typeid, LPCSTR resid,
279 DWORD *resLen, DWORD *resOff )
280 {
281 IMAGE_NT_HEADERS pehd;
282 DWORD pehdoffset;
283 PIMAGE_DATA_DIRECTORY resDataDir;
284 PIMAGE_SECTION_HEADER sections;
285 LPBYTE resSection;
286 DWORD resSectionSize;
287 const void *resDir;
288 const IMAGE_RESOURCE_DIRECTORY *resPtr;
289 const IMAGE_RESOURCE_DATA_ENTRY *resData;
290 int i, nSections;
291 BOOL ret = FALSE;
292
293 /* Read in PE header */
294 pehdoffset = LZSeek( lzfd, 0, SEEK_CUR );
295 if ( sizeof(pehd) != LZRead( lzfd, (LPSTR)&pehd, sizeof(pehd) ) ) return 0;
296
297 resDataDir = pehd.OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_RESOURCE;
298 if ( !resDataDir->Size )
299 {
300 TRACE("No resources in PE dll\n" );
301 return FALSE;
302 }
303
304 /* Read in section table */
305 nSections = pehd.FileHeader.NumberOfSections;
306 sections = HeapAlloc( GetProcessHeap(), 0,
307 nSections * sizeof(IMAGE_SECTION_HEADER) );
308 if ( !sections ) return FALSE;
309
310 LZSeek( lzfd, pehdoffset +
311 sizeof(DWORD) + /* Signature */
312 sizeof(IMAGE_FILE_HEADER) +
313 pehd.FileHeader.SizeOfOptionalHeader, SEEK_SET );
314
315 if ( nSections * sizeof(IMAGE_SECTION_HEADER) !=
316 LZRead( lzfd, (LPSTR)sections, nSections * sizeof(IMAGE_SECTION_HEADER) ) )
317 {
318 HeapFree( GetProcessHeap(), 0, sections );
319 return FALSE;
320 }
321
322 /* Find resource section */
323 for ( i = 0; i < nSections; i++ )
324 if ( resDataDir->VirtualAddress >= sections[i].VirtualAddress
325 && resDataDir->VirtualAddress < sections[i].VirtualAddress +
326 sections[i].SizeOfRawData )
327 break;
328
329 if ( i == nSections )
330 {
331 HeapFree( GetProcessHeap(), 0, sections );
332 TRACE("Couldn't find resource section\n" );
333 return FALSE;
334 }
335
336 /* Read in resource section */
337 resSectionSize = sections[i].SizeOfRawData;
338 resSection = HeapAlloc( GetProcessHeap(), 0, resSectionSize );
339 if ( !resSection )
340 {
341 HeapFree( GetProcessHeap(), 0, sections );
342 return FALSE;
343 }
344
345 LZSeek( lzfd, sections[i].PointerToRawData, SEEK_SET );
346 if ( resSectionSize != LZRead( lzfd, (char*)resSection, resSectionSize ) ) goto done;
347
348 /* Find resource */
349 resDir = resSection + (resDataDir->VirtualAddress - sections[i].VirtualAddress);
350
351 resPtr = resDir;
352 resPtr = find_entry_by_name( resPtr, typeid, resDir );
353 if ( !resPtr )
354 {
355 TRACE("No typeid entry found for %p\n", typeid );
356 goto done;
357 }
358 resPtr = find_entry_by_name( resPtr, resid, resDir );
359 if ( !resPtr )
360 {
361 TRACE("No resid entry found for %p\n", resid );
362 goto done;
363 }
364 resPtr = find_entry_default( resPtr, resDir );
365 if ( !resPtr )
366 {
367 TRACE("No default language entry found for %p\n", resid );
368 goto done;
369 }
370
371 /* Find resource data section */
372 resData = (const IMAGE_RESOURCE_DATA_ENTRY*)resPtr;
373 for ( i = 0; i < nSections; i++ )
374 if ( resData->OffsetToData >= sections[i].VirtualAddress
375 && resData->OffsetToData < sections[i].VirtualAddress +
376 sections[i].SizeOfRawData )
377 break;
378
379 if ( i == nSections )
380 {
381 TRACE("Couldn't find resource data section\n" );
382 goto done;
383 }
384
385 /* Return resource data */
386 if ( resLen ) *resLen = resData->Size;
387 if ( resOff ) *resOff = resData->OffsetToData - sections[i].VirtualAddress
388 + sections[i].PointerToRawData;
389 ret = TRUE;
390
391 done:
392 HeapFree( GetProcessHeap(), 0, resSection );
393 HeapFree( GetProcessHeap(), 0, sections );
394 return ret;
395 }
396
397
398 /*************************************************************************
399 * GetFileResourceSize [VER.2]
400 */
401 DWORD WINAPI GetFileResourceSize16( LPCSTR lpszFileName, LPCSTR lpszResType,
402 LPCSTR lpszResId, LPDWORD lpdwFileOffset )
403 {
404 BOOL retv = FALSE;
405 HFILE lzfd;
406 OFSTRUCT ofs;
407 DWORD reslen;
408
409 TRACE("(%s,type=%p,id=%p,off=%p)\n",
410 debugstr_a(lpszFileName), lpszResType, lpszResId, lpszResId );
411
412 lzfd = LZOpenFileA( (LPSTR)lpszFileName, &ofs, OF_READ );
413 if ( lzfd < 0 ) return 0;
414
415 switch ( read_xx_header( lzfd ) )
416 {
417 case IMAGE_OS2_SIGNATURE:
418 #ifdef __REACTOS__
419 ERR("OS2 Images not supported under ReactOS at this time.");
420 retv = 0;
421 #else
422 retv = find_ne_resource( lzfd, lpszResType, lpszResId,
423 &reslen, lpdwFileOffset );
424 #endif
425 break;
426
427 case IMAGE_NT_SIGNATURE:
428 retv = find_pe_resource( lzfd, lpszResType, lpszResId,
429 &reslen, lpdwFileOffset );
430 break;
431 }
432
433 LZClose( lzfd );
434 return retv? reslen : 0;
435 }
436
437
438 /*************************************************************************
439 * GetFileResource [VER.3]
440 */
441 DWORD WINAPI GetFileResource16( LPCSTR lpszFileName, LPCSTR lpszResType,
442 LPCSTR lpszResId, DWORD dwFileOffset,
443 DWORD dwResLen, LPVOID lpvData )
444 {
445 BOOL retv = FALSE;
446 HFILE lzfd;
447 OFSTRUCT ofs;
448 DWORD reslen = dwResLen;
449
450 TRACE("(%s,type=%p,id=%p,off=%d,len=%d,data=%p)\n",
451 debugstr_a(lpszFileName), lpszResType, lpszResId,
452 dwFileOffset, dwResLen, lpvData );
453
454 lzfd = LZOpenFileA( (LPSTR)lpszFileName, &ofs, OF_READ );
455 if ( lzfd < 0 ) return 0;
456
457 if ( !dwFileOffset )
458 {
459 switch ( read_xx_header( lzfd ) )
460 {
461 case IMAGE_OS2_SIGNATURE:
462 #ifdef __REACTOS__
463 ERR("OS2 Images not supported under ReactOS at this time.");
464 retv = 0;
465 #else
466 retv = find_ne_resource( lzfd, lpszResType, lpszResId,
467 &reslen, &dwFileOffset );
468 #endif
469 break;
470
471 case IMAGE_NT_SIGNATURE:
472 retv = find_pe_resource( lzfd, lpszResType, lpszResId,
473 &reslen, &dwFileOffset );
474 break;
475 }
476
477 if ( !retv )
478 {
479 LZClose( lzfd );
480 return 0;
481 }
482 }
483
484 LZSeek( lzfd, dwFileOffset, SEEK_SET );
485 reslen = LZRead( lzfd, lpvData, min( reslen, dwResLen ) );
486 LZClose( lzfd );
487
488 return reslen;
489 }