source: trunk/src/kernel32/winimgres.cpp@ 1330

Last change on this file since 1330 was 1274, checked in by bird, 26 years ago

New Pe2Lx implementation.

File size: 16.3 KB
Line 
1/* $Id: winimgres.cpp,v 1.23 1999-10-14 01:37:56 bird Exp $ */
2
3/*
4 * Win32 PE Image class (resource methods)
5 *
6 * Copyright 1998 Sander van Leeuwen (sandervl@xs4all.nl)
7 *
8 *
9 * Project Odin Software License can be found in LICENSE.TXT
10 *
11 * TODO: Check created resource objects before loading the resource!
12 * TODO: Once the resource handling in PE2LX/win32k is changed,
13 * getVersionStruct/Size can be moved into the Win32ImageBase class
14 *
15 */
16#include <os2win.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20
21#include <misc.h>
22#include <winimagebase.h>
23#include <winimagepe2lx.h>
24#include <winimagepeldr.h>
25#include <winimagelx.h>
26#include <winres.h>
27#include <winresmenu.h>
28#include <unicode.h>
29#include <heapstring.h>
30#include "pefile.h"
31#include "oslibmisc.h"
32
33//******************************************************************************
34//Assuming names are case insensitive
35//PE spec says names & ids are sorted; keep on searching just to be sure
36//******************************************************************************
37PIMAGE_RESOURCE_DATA_ENTRY
38 Win32ImageBase::getPEResourceEntry(ULONG id, ULONG type, ULONG lang)
39{
40 PIMAGE_RESOURCE_DIRECTORY prdType;
41 PIMAGE_RESOURCE_DIRECTORY_ENTRY prde;
42 PIMAGE_RESOURCE_DIR_STRING_U pstring;
43 PIMAGE_RESOURCE_DATA_ENTRY pData = NULL;
44 ULONG nodeData[3], i, j, nameOffset;
45 BOOL fFound = FALSE, fNumType;
46
47 //PH: our system LX DLLs might not have a resource segment
48 if (pResDir == NULL)
49 return NULL;
50
51 /* set pointer to first resource type entry */
52 prde = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)((ULONG)pResDir + sizeof(IMAGE_RESOURCE_DIRECTORY));
53
54 /* loop through all resource directory entry types */
55 //1st level -> types
56 //2nd level -> names
57 //3rd level -> language
58 nodeData[0] = id;
59 nodeData[1] = lang;
60 nodeData[2] = 0xFFFFFFFF;
61
62 fNumType = TRUE; //assume numeric
63 if(HIWORD(type) != 0) {//string id?
64 for(i=0;i<MAX_RES;i++) {
65 if(stricmp((char *)type, ResTypes[i]) == 0)
66 break;
67 }
68 if(i == MAX_RES) {//custom resource type
69 fNumType = FALSE;
70 }
71 else type = i;
72 }
73
74 for (i=0; i<pResDir->NumberOfNamedEntries+pResDir->NumberOfIdEntries; i++) {
75 /* locate directory or each resource type */
76 prdType = (PIMAGE_RESOURCE_DIRECTORY)((int)pResDir + (int)prde->u2.OffsetToData);
77
78 if(i < pResDir->NumberOfNamedEntries)
79 {//name or id entry?
80 //SvL: 30-10-'97, high bit is set, so clear to get real offset
81 nameOffset = prde->u1.Name & ~0x80000000;
82
83 pstring = (PIMAGE_RESOURCE_DIR_STRING_U)((ULONG)pResDir + nameOffset);
84 char *typename = (char *)malloc(pstring->Length+1);
85 lstrcpynWtoA(typename, pstring->NameString, pstring->Length+1);
86 typename[pstring->Length] = 0;
87
88 if(!fNumType) {
89 if(stricmp(typename, (char *)type) == 0) {
90 fFound = TRUE;
91 }
92 }
93 else {
94 for(j=0;j<MAX_RES;j++) {
95 if(stricmp(typename, ResTypes[j]) == 0)
96 break;
97 }
98 if(j == type) {
99 fFound = TRUE;
100 }
101 }
102 free(typename);
103 }
104 else {
105 if(prde->u1.Id == type) {
106 fFound = TRUE;
107 }
108 }
109 if(fFound) {
110 if((ULONG)prdType & 0x80000000) {//subdirectory?
111 pData = ProcessResSubDir(prdType, &nodeData[0], 2);
112 }
113 else {
114 pData = (PIMAGE_RESOURCE_DATA_ENTRY)prdType;
115 dprintf(("getResource: not a subdir!!\n"));
116 }
117 break;
118 }
119 /* increment to next entry */
120 prde++;
121 }
122 return pData;
123}
124//******************************************************************************
125//level: 2 ids
126// 3 languages
127//******************************************************************************
128PIMAGE_RESOURCE_DATA_ENTRY
129 Win32ImageBase::ProcessResSubDir(PIMAGE_RESOURCE_DIRECTORY prdType,
130 ULONG *nodeData, int level)
131{
132 PIMAGE_RESOURCE_DIRECTORY prdType2;
133 PIMAGE_RESOURCE_DIRECTORY_ENTRY prde;
134 PIMAGE_RESOURCE_DIR_STRING_U pstring;
135 PIMAGE_RESOURCE_DATA_ENTRY pData;
136 BOOL fFound = FALSE, fNumId;
137 ULONG nrres, nameOffset;
138 char *resname;
139 int i;
140
141 if(*nodeData == 0xFFFFFFFF) {//shouldn't happen!
142 dprintf(("ProcessResSubDir: *nodeData == 0xFFFFFFFF!\n"));
143 return(NULL);
144 }
145 prdType = (PIMAGE_RESOURCE_DIRECTORY)((ULONG)prdType & ~0x80000000);
146 prde = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)((DWORD)prdType + sizeof(IMAGE_RESOURCE_DIRECTORY));
147
148 //level 2 (id) -> get first id?
149 //level 3 (lang) -> get first language?
150 if(*nodeData == IDLANG_GETFIRST) {
151 nrres = prdType->NumberOfNamedEntries + prdType->NumberOfIdEntries;
152 fNumId = (prdType->NumberOfNamedEntries == 0);
153 }
154 else {
155 fNumId = HIWORD(*nodeData) == 0;
156
157 if(fNumId) {//numeric or string id?
158 nrres = prdType->NumberOfIdEntries;
159 prde += prdType->NumberOfNamedEntries; //skip name entries
160 }
161 else nrres = prdType->NumberOfNamedEntries;
162 }
163
164 for(i=0;i<nrres;i++) {
165 /* locate directory or each resource type */
166 prdType2 = (PIMAGE_RESOURCE_DIRECTORY)((ULONG)pResDir + (ULONG)prde->u2.OffsetToData);
167
168 if(*nodeData == IDLANG_GETFIRST) {
169 fFound = TRUE; //always take the first one
170 }
171 else
172 if(!fNumId) {//name or id entry?
173 nameOffset = prde->u1.Name;
174 if(prde->u1.s.NameIsString) //unicode directory string /*PLF Sat 97-06-21 22:30:35*/
175 nameOffset &= ~0x80000000;
176
177 pstring = (PIMAGE_RESOURCE_DIR_STRING_U)((ULONG)pResDir + nameOffset);
178
179 resname = (char *)malloc(pstring->Length+1);
180 lstrcpynWtoA(resname, pstring->NameString, pstring->Length+1);
181 resname[pstring->Length] = 0;
182 if(stricmp(resname, (char *)*nodeData) == 0) {
183 fFound = TRUE;
184 }
185 free(resname);
186 }
187 else {
188 if(*nodeData == prde->u1.Id)
189 fFound = TRUE;
190 }
191
192 if(fFound) {
193 if((ULONG)prdType2 & 0x80000000) {//subdirectory?
194 return ProcessResSubDir(prdType2, nodeData+1, 3);
195 }
196 else {
197 pData = (PIMAGE_RESOURCE_DATA_ENTRY)prdType2;
198 if(pData->Size) {//winamp17 winzip archive has resource with size 0
199 return(pData);
200 }
201 else return(NULL);
202 }
203 }
204 prde++;
205 }
206 return(NULL);
207}
208//******************************************************************************
209//******************************************************************************
210ULONG Win32ImageBase::getPEResourceSize(ULONG id, ULONG type, ULONG lang)
211{
212 PIMAGE_RESOURCE_DATA_ENTRY pData = NULL;
213
214 pData = getPEResourceEntry(id, type, lang);
215 if(pData == NULL) {
216 dprintf(("Win32ImageBase::getPEResourceSize: couldn't find resource %d (type %d, lang %d)", id, type, lang));
217 return 0;
218 }
219 return pData->Size;
220}
221//******************************************************************************
222//******************************************************************************
223HRSRC Win32ImageBase::findResourceA(LPCSTR lpszName, LPSTR lpszType, ULONG lang)
224{
225 PIMAGE_RESOURCE_DATA_ENTRY pData = NULL;
226 Win32Resource *res;
227 BOOL fNumType;
228 char *winres = NULL;
229 ULONG id, type;
230 int i, stringid = -1, j;
231
232 fNumType = TRUE; //assume numeric
233 if(HIWORD(lpszType) != 0) {//string id?
234 for(i=0;i<MAX_RES;i++) {
235 if(stricmp(lpszType, ResTypes[i]) == 0)
236 break;
237 }
238 if(i == MAX_RES) {//custom resource type
239 fNumType = FALSE;
240 type = (ULONG)lpszType;
241 }
242 else type = i;
243 }
244 else type = (ULONG)lpszType;
245
246 //String format: tables of 16 strings stored as one resource
247 //upper 12 bits of resource id passed by user determines block (res id)
248 //lower 4 bits are an index into the string table
249 if(fNumType) {
250 if(type == NTRT_STRING) {
251 stringid = (ULONG)lpszName & 0xF;
252 id = (((ULONG)lpszName) >> 4)+1;
253 }
254 else id = (ULONG)lpszName;
255 }
256 else {
257 if(stricmp((char *)type, ResTypes[NTRT_STRING]) == 0) {
258 stringid = (ULONG)lpszName & 0xF;
259 id = (((ULONG)lpszName) >> 4)+1;
260 }
261 else id = (ULONG)lpszName;
262 }
263
264 pData = getPEResourceEntry(id, type, lang);
265 if(pData == NULL) {
266 if(HIWORD(id)) {
267 dprintf(("Win32ImageBase::getPEResource %s: couldn't find resource %s (type %d, lang %d)", szModule, id, type, lang));
268 }
269 else dprintf(("Win32ImageBase::getPEResource %s: couldn't find resource %d (type %d, lang %d)", szModule, id, type, lang));
270 return 0;
271 }
272 //pResourceSectionStart contains the virtual address of the imagebase in the PE header
273 //for the resource section (images loaded by the pe.exe)
274 //For LX images, this is 0 as OffsetToData contains a relative offset
275 char *resdata = (char *)((char *)pResDir + pData->OffsetToData - pResourceSectionStart);
276 if(stringid != -1) {//search for string in table
277 USHORT *unicodestr = (USHORT *)resdata;
278
279 for(i=0;i<stringid;i++) {
280 unicodestr += *unicodestr+1;
281 }
282 res = new Win32Resource(this, id, NTRT_STRING, (*unicodestr+1)*sizeof(WCHAR),
283 (char *)(unicodestr+1));
284 if(res == NULL) {
285 dprintf(("new Win32Resource failed!\n"));
286 return(NULL);
287 }
288 }
289 else {
290 switch(type) {
291 case NTRT_MENU:
292 res = new Win32MenuRes(this, id, type, pData->Size, resdata);
293 break;
294 default:
295 res = new Win32Resource(this, id, type, pData->Size, resdata);
296 break;
297 }
298
299 }
300
301 return (HRSRC) res;
302}
303//******************************************************************************
304//******************************************************************************
305#if 0
306HRSRC Win32Pe2LxImage::findResourceA(LPCSTR lpszName, LPSTR lpszType, ULONG lang)
307{
308 Win32Resource *res = NULL;
309 HRSRC hres;
310 int i;
311 LPSTR szType = (LPSTR)lpszType;
312
313 if(HIWORD(lpszType) != 0) {//type name, translate to id
314 for(i=0;i<MAX_RES;i++) {
315 if(strcmp(lpszType, ResTypes[i]) == 0)
316 break;
317 }
318 if(i == MAX_RES) {//custom resource type, stored as rcdata
319 dprintf(("FindResourceA custom type %s\n", lpszType));
320 lpszType = (LPSTR)NTRT_RCDATA;
321 }
322 else lpszType = (LPSTR)i;
323
324 szType = (LPSTR)lpszType;
325 }
326 switch((int)szType) {
327 case NTRT_GROUP_ICON:
328 szType = (LPSTR)NTRT_ICON;
329 break;
330 case NTRT_GROUP_CURSOR:
331 szType = (LPSTR)NTRT_CURSOR;
332 break;
333 case NTRT_VERSION:
334 szType = (LPSTR)NTRT_RCDATA;
335 break;
336 case NTRT_STRING:
337 case NTRT_MENU:
338 case NTRT_ICON:
339 case NTRT_BITMAP:
340 case NTRT_CURSOR:
341 case NTRT_DIALOG:
342 case NTRT_RCDATA:
343 case NTRT_ACCELERATORS:
344 szType = lpszType;
345 break;
346 default: //unknown are stored as rcdata
347 szType = (LPSTR)NTRT_RCDATA;
348 break;
349 }
350 dprintf(("FindResourceA from %X type %d (%X)\n", hinstance, szType, lpszType));
351
352 if(HIWORD(lpszName) != 0) {//convert string name identifier to numeric id
353 dprintf(("FindResource %s\n", lpszName));
354 if(lpszName[0] == '#') {// #344
355 lpszName = (LPCSTR)atoi(&lpszName[1]);
356 }
357 else lpszName = (LPCSTR)convertNameId((char *)lpszName);
358 }
359 else dprintf(("FindResource %d\n", (int)lpszName));
360
361 hres = O32_FindResource(hinstance, lpszName, szType);
362 if(hres)
363 {
364 switch((ULONG)szType) {
365 case NTRT_MENU:
366 res = new Win32MenuRes(this, hres, (ULONG)lpszName, (ULONG)szType);
367 break;
368 default:
369 res = new Win32Resource(this, hres, (ULONG)lpszName, (ULONG)szType);
370 break;
371 }
372 }
373
374 if(hres == NULL && HIWORD(lpszName) == 0 && (int)szType == NTRT_STRING) {
375 hres = O32_FindResource(hinstance, (LPCSTR)(((int)lpszName - 1)*16), (LPCSTR)NTRT_RCDATA);
376 if(hres)
377 {
378 res = new Win32Resource(this, hres, (ULONG)lpszName, (ULONG)szType);
379 }
380 else dprintf(("FindResourceA can't find string %d\n", (int)lpszName));
381 }
382 dprintf(("FindResourceA returned %X (%X)\n", hres, GetLastError()));
383
384 return (HRSRC)res;
385}
386#endif
387//******************************************************************************
388//******************************************************************************
389HRSRC Win32ImageBase::findResourceW(LPWSTR lpszName, LPWSTR lpszType, ULONG lang)
390{
391 HRSRC hres;
392 char *astring1 = NULL, *astring2 = NULL;
393
394 if(HIWORD(lpszName) != 0) {
395 astring1 = UnicodeToAsciiString(lpszName);
396 }
397 else astring1 = (char *)lpszName;
398
399 if(HIWORD(lpszType) != 0) {
400 astring2 = UnicodeToAsciiString(lpszType);
401 }
402 else astring2 = (char *)lpszType;
403
404 hres = (HRSRC) findResourceA(astring1, astring2);
405
406 if(HIWORD(astring1)) FreeAsciiString(astring1);
407 if(HIWORD(astring2)) FreeAsciiString(astring2);
408
409 return(hres);
410}
411//******************************************************************************
412//TODO:
413//******************************************************************************
414#if 0
415ULONG Win32Pe2LxImage::getResourceSizeA(LPCSTR lpszName, LPSTR lpszType, ULONG lang)
416{
417 DebugInt3();
418 return 0;
419}
420#endif
421//******************************************************************************
422//******************************************************************************
423ULONG Win32ImageBase::getResourceSizeA(LPCSTR lpszName, LPSTR lpszType, ULONG lang)
424{
425 return getPEResourceSize((ULONG)lpszName, (ULONG)lpszType, lang);
426}
427//******************************************************************************
428//******************************************************************************
429ULONG Win32ImageBase::getResourceSizeW(LPCWSTR lpszName, LPWSTR lpszType, ULONG lang)
430{
431 char *astring1 = NULL, *astring2 = NULL;
432 ULONG ressize;
433
434 if(HIWORD(lpszName) != 0) {
435 astring1 = UnicodeToAsciiString((LPWSTR)lpszName);
436 }
437 else astring1 = (char *)lpszName;
438
439 if(HIWORD(lpszType) != 0) {
440 astring2 = UnicodeToAsciiString(lpszType);
441 }
442 else astring2 = (char *)lpszType;
443
444 ressize = getResourceSizeA(astring2, astring1, lang);
445
446 if(HIWORD(astring1)) FreeAsciiString(astring1);
447 if(HIWORD(astring2)) FreeAsciiString(astring2);
448
449 return(ressize);
450}
451//******************************************************************************
452//******************************************************************************
453#if 0
454ULONG Win32Pe2LxImage::getVersionSize()
455{
456 if(getVersionId() == -1) {
457 dprintf(("GetVersionSize: %s has no version resource!\n", szModule));
458 return(0);
459 }
460 return OSLibGetResourceSize(hinstance, getVersionId());
461}
462//******************************************************************************
463//******************************************************************************
464BOOL Win32Pe2LxImage::getVersionStruct(char *verstruct, ULONG bufLength)
465{
466 if(getVersionId() == -1) {
467 dprintf(("GetVersionStruct: %s has no version resource!\n", szModule));
468 return(FALSE);
469 }
470 return OSLibGetResource(hinstance, getVersionId(), verstruct, bufLength);
471}
472#endif
473//******************************************************************************
474//******************************************************************************
475ULONG Win32ImageBase::getVersionSize()
476{
477 return getResourceSizeA((LPCSTR)1, (LPSTR)NTRT_VERSION);
478}
479//******************************************************************************
480//******************************************************************************
481BOOL Win32ImageBase::getVersionStruct(char *verstruct, ULONG bufLength)
482{
483 PIMAGE_RESOURCE_DATA_ENTRY pData = NULL;
484
485 if(verstruct == NULL || bufLength == 0) {
486 SetLastError(ERROR_INVALID_PARAMETER);
487 return FALSE;
488 }
489 pData = getPEResourceEntry(ID_GETFIRST, NTRT_VERSION);
490 if(pData == NULL) {
491 dprintf(("Win32PeLdrImage::getVersionStruct: couldn't find version resource!"));
492 return 0;
493 }
494 char *resdata = (char *)((char *)pResDir + pData->OffsetToData - pResourceSectionStart);
495 memcpy(verstruct, resdata, min(bufLength, pData->Size));
496 return TRUE;
497}
498//******************************************************************************
499//******************************************************************************
Note: See TracBrowser for help on using the repository browser.