source: trunk/src/version/resource.c

Last change on this file was 10300, checked in by sandervl, 22 years ago

flexible debug logging

File size: 14.6 KB
Line 
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23#include "config.h"
24
25#include <stdarg.h>
26#include <stdlib.h>
27#include <string.h>
28#include <sys/types.h>
29#ifdef HAVE_UNISTD_H
30# include <unistd.h>
31#endif
32
33#define NONAMELESSUNION
34#define NONAMELESSSTRUCT
35#include "windef.h"
36#include "winbase.h"
37#include "lzexpand.h"
38
39#include "wine/unicode.h"
40#include "wine/winbase16.h"
41#include "wine/winuser16.h"
42#include "winver.h"
43
44#include "wine/debug.h"
45#include <module.h>
46
47#define DBG_LOCALLOG DBG_resource
48#include "dbglocal.h"
49
50WINE_DEFAULT_DEBUG_CHANNEL(ver);
51
52
53/**********************************************************************
54 * find_entry_by_id
55 *
56 * Find an entry by id in a resource directory
57 * Copied from loader/pe_resource.c
58 */
59static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_id( const IMAGE_RESOURCE_DIRECTORY *dir,
60 WORD id, const void *root )
61{
62 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
63 int min, max, pos;
64
65 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
66 min = dir->NumberOfNamedEntries;
67 max = min + dir->NumberOfIdEntries - 1;
68 while (min <= max)
69 {
70 pos = (min + max) / 2;
71 if (entry[pos].u1.Id == id)
72 return (IMAGE_RESOURCE_DIRECTORY *)((char *)root + entry[pos].u2.s.OffsetToDirectory);
73 if (entry[pos].u1.Id > id) max = pos - 1;
74 else min = pos + 1;
75 }
76 return NULL;
77}
78
79
80/**********************************************************************
81 * find_entry_default
82 *
83 * Find a default entry in a resource directory
84 * Copied from loader/pe_resource.c
85 */
86static const IMAGE_RESOURCE_DIRECTORY *find_entry_default( const IMAGE_RESOURCE_DIRECTORY *dir,
87 const void *root )
88{
89 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
90
91 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
92 return (IMAGE_RESOURCE_DIRECTORY *)((char *)root + entry->u2.s.OffsetToDirectory);
93}
94
95
96/**********************************************************************
97 * find_entry_by_name
98 *
99 * Find an entry by name in a resource directory
100 * Copied from loader/pe_resource.c
101 */
102static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_name( const IMAGE_RESOURCE_DIRECTORY *dir,
103 LPCSTR name, const void *root )
104{
105 const IMAGE_RESOURCE_DIRECTORY *ret = NULL;
106 LPWSTR nameW;
107 DWORD namelen;
108
109 if (!HIWORD(name)) return find_entry_by_id( dir, LOWORD(name), root );
110 if (name[0] == '#')
111 {
112 return find_entry_by_id( dir, atoi(name+1), root );
113 }
114
115 namelen = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
116 if ((nameW = HeapAlloc( GetProcessHeap(), 0, namelen * sizeof(WCHAR) )))
117 {
118 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
119 const IMAGE_RESOURCE_DIR_STRING_U *str;
120 int min, max, res, pos;
121
122 MultiByteToWideChar( CP_ACP, 0, name, -1, nameW, namelen );
123 namelen--; /* remove terminating null */
124 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
125 min = 0;
126 max = dir->NumberOfNamedEntries - 1;
127 while (min <= max)
128 {
129 pos = (min + max) / 2;
130 str = (IMAGE_RESOURCE_DIR_STRING_U *)((char *)root + entry[pos].u1.s.NameOffset);
131 res = strncmpiW( nameW, str->NameString, str->Length );
132 if (!res && namelen == str->Length)
133 {
134 ret = (IMAGE_RESOURCE_DIRECTORY *)((char *)root + entry[pos].u2.s.OffsetToDirectory);
135 break;
136 }
137 if (res < 0) max = pos - 1;
138 else min = pos + 1;
139 }
140 HeapFree( GetProcessHeap(), 0, nameW );
141 }
142 return ret;
143}
144
145
146/***********************************************************************
147 * read_xx_header [internal]
148 */
149static int read_xx_header( HFILE lzfd )
150{
151 IMAGE_DOS_HEADER mzh;
152 char magic[3];
153
154 LZSeek( lzfd, 0, SEEK_SET );
155 if ( sizeof(mzh) != LZRead( lzfd, (LPSTR)&mzh, sizeof(mzh) ) )
156 return 0;
157 if ( mzh.e_magic != IMAGE_DOS_SIGNATURE )
158 return 0;
159
160 LZSeek( lzfd, mzh.e_lfanew, SEEK_SET );
161 if ( 2 != LZRead( lzfd, magic, 2 ) )
162 return 0;
163
164 LZSeek( lzfd, mzh.e_lfanew, SEEK_SET );
165
166 if ( magic[0] == 'N' && magic[1] == 'E' )
167 return IMAGE_OS2_SIGNATURE;
168 if ( magic[0] == 'P' && magic[1] == 'E' )
169 return IMAGE_NT_SIGNATURE;
170
171 magic[2] = '\0';
172 WARN("Can't handle %s files.\n", magic );
173 return 0;
174}
175
176/***********************************************************************
177 * load_ne_resource [internal]
178 */
179static BOOL find_ne_resource( HFILE lzfd, LPCSTR typeid, LPCSTR resid,
180 DWORD *resLen, DWORD *resOff )
181{
182 IMAGE_OS2_HEADER nehd;
183 NE_TYPEINFO *typeInfo;
184 NE_NAMEINFO *nameInfo;
185 DWORD nehdoffset;
186 LPBYTE resTab;
187 DWORD resTabSize;
188 int count;
189
190 /* Read in NE header */
191 nehdoffset = LZSeek( lzfd, 0, SEEK_CUR );
192 if ( sizeof(nehd) != LZRead( lzfd, (LPSTR)&nehd, sizeof(nehd) ) ) return 0;
193
194 resTabSize = nehd.ne_restab - nehd.ne_rsrctab;
195 if ( !resTabSize )
196 {
197 dprintf(("No resources in NE dll\n" ));
198 return FALSE;
199 }
200
201 /* Read in resource table */
202 resTab = HeapAlloc( GetProcessHeap(), 0, resTabSize );
203 if ( !resTab ) return FALSE;
204
205 LZSeek( lzfd, nehd.ne_rsrctab + nehdoffset, SEEK_SET );
206 if ( resTabSize != LZRead( lzfd, resTab, resTabSize ) )
207 {
208 HeapFree( GetProcessHeap(), 0, resTab );
209 return FALSE;
210 }
211
212 /* Find resource */
213 typeInfo = (NE_TYPEINFO *)(resTab + 2);
214
215 if (HIWORD(typeid) != 0) /* named type */
216 {
217 BYTE len = strlen( typeid );
218 while (typeInfo->type_id)
219 {
220 if (!(typeInfo->type_id & 0x8000))
221 {
222 BYTE *p = resTab + typeInfo->type_id;
223 if ((*p == len) && !strncasecmp( p+1, typeid, len )) goto found_type;
224 }
225 typeInfo = (NE_TYPEINFO *)((char *)(typeInfo + 1) +
226 typeInfo->count * sizeof(NE_NAMEINFO));
227 }
228 }
229 else /* numeric type id */
230 {
231 WORD id = LOWORD(typeid) | 0x8000;
232 while (typeInfo->type_id)
233 {
234 if (typeInfo->type_id == id) goto found_type;
235 typeInfo = (NE_TYPEINFO *)((char *)(typeInfo + 1) +
236 typeInfo->count * sizeof(NE_NAMEINFO));
237 }
238 }
239 dprintf(("No typeid entry found for %p\n", typeid ));
240 HeapFree( GetProcessHeap(), 0, resTab );
241 return FALSE;
242
243 found_type:
244 nameInfo = (NE_NAMEINFO *)(typeInfo + 1);
245
246 if (HIWORD(resid) != 0) /* named resource */
247 {
248 BYTE len = strlen( resid );
249 for (count = typeInfo->count; count > 0; count--, nameInfo++)
250 {
251 BYTE *p = resTab + nameInfo->id;
252 if (nameInfo->id & 0x8000) continue;
253 if ((*p == len) && !strncasecmp( p+1, resid, len )) goto found_name;
254 }
255 }
256 else /* numeric resource id */
257 {
258 WORD id = LOWORD(resid) | 0x8000;
259 for (count = typeInfo->count; count > 0; count--, nameInfo++)
260 if (nameInfo->id == id) goto found_name;
261 }
262 dprintf(("No resid entry found for %p\n", typeid ));
263 HeapFree( GetProcessHeap(), 0, resTab );
264 return FALSE;
265
266 found_name:
267 /* Return resource data */
268 if ( resLen ) *resLen = nameInfo->length << *(WORD *)resTab;
269 if ( resOff ) *resOff = nameInfo->offset << *(WORD *)resTab;
270
271 HeapFree( GetProcessHeap(), 0, resTab );
272 return TRUE;
273}
274
275/***********************************************************************
276 * load_pe_resource [internal]
277 */
278static 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_FILE_RESOURCE_DIRECTORY;
298 if ( !resDataDir->Size )
299 {
300 dprintf(("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 dprintf(("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, resSection, resSectionSize ) ) goto done;
347
348 /* Find resource */
349 resDir = resSection + (resDataDir->VirtualAddress - sections[i].VirtualAddress);
350
351 resPtr = (PIMAGE_RESOURCE_DIRECTORY)resDir;
352 resPtr = find_entry_by_name( resPtr, typeid, resDir );
353 if ( !resPtr )
354 {
355 dprintf(("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 dprintf(("No resid entry found for %p\n", resid ));
362 goto done;
363 }
364 resPtr = find_entry_default( resPtr, resDir );
365 if ( !resPtr )
366 {
367 dprintf(("No default language entry found for %p\n", resid ));
368 goto done;
369 }
370
371 /* Find resource data section */
372 resData = (PIMAGE_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 dprintf(("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 */
401DWORD 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 dprintf(("(%s,type=0x%lx,id=0x%lx,off=%p)\n",
410 debugstr_a(lpszFileName), (LONG)lpszResType, (LONG)lpszResId,
411 lpszResId ));
412
413 lzfd = LZOpenFileA( (LPSTR)lpszFileName, &ofs, OF_READ );
414 if ( lzfd < 0 ) return 0;
415
416 switch ( read_xx_header( lzfd ) )
417 {
418 case IMAGE_OS2_SIGNATURE:
419 retv = find_ne_resource( lzfd, lpszResType, lpszResId,
420 &reslen, lpdwFileOffset );
421 break;
422
423 case IMAGE_NT_SIGNATURE:
424 retv = find_pe_resource( lzfd, lpszResType, lpszResId,
425 &reslen, lpdwFileOffset );
426 break;
427 }
428
429 LZClose( lzfd );
430 return retv? reslen : 0;
431}
432
433
434/*************************************************************************
435 * GetFileResource [VER.3]
436 */
437DWORD WINAPI GetFileResource16( LPCSTR lpszFileName, LPCSTR lpszResType,
438 LPCSTR lpszResId, DWORD dwFileOffset,
439 DWORD dwResLen, LPVOID lpvData )
440{
441 BOOL retv = FALSE;
442 HFILE lzfd;
443 OFSTRUCT ofs;
444 DWORD reslen = dwResLen;
445
446 dprintf(("(%s,type=0x%lx,id=0x%lx,off=%ld,len=%ld,data=%p)\n",
447 debugstr_a(lpszFileName), (LONG)lpszResType, (LONG)lpszResId,
448 dwFileOffset, dwResLen, lpvData ));
449
450 lzfd = LZOpenFileA( (LPSTR)lpszFileName, &ofs, OF_READ );
451 if ( lzfd < 0 ) return 0;
452
453 if ( !dwFileOffset )
454 {
455 switch ( read_xx_header( lzfd ) )
456 {
457 case IMAGE_OS2_SIGNATURE:
458 retv = find_ne_resource( lzfd, lpszResType, lpszResId,
459 &reslen, &dwFileOffset );
460 break;
461
462 case IMAGE_NT_SIGNATURE:
463 retv = find_pe_resource( lzfd, lpszResType, lpszResId,
464 &reslen, &dwFileOffset );
465 break;
466 }
467
468 if ( !retv )
469 {
470 LZClose( lzfd );
471 return 0;
472 }
473 }
474
475 LZSeek( lzfd, dwFileOffset, SEEK_SET );
476 reslen = LZRead( lzfd, lpvData, min( reslen, dwResLen ) );
477 LZClose( lzfd );
478
479 return reslen;
480}
481
Note: See TracBrowser for help on using the repository browser.