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

Last change on this file since 1192 was 1192, checked in by sandervl, 26 years ago

Winspool load bugfix

File size: 15.9 KB
Line 
1/* $Id: winimgres.cpp,v 1.22 1999-10-08 16:27:21 sandervl 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//******************************************************************************
305HRSRC Win32Pe2LxImage::findResourceA(LPCSTR lpszName, LPSTR lpszType, ULONG lang)
306{
307 Win32Resource *res = NULL;
308 HRSRC hres;
309 int i;
310 LPSTR szType = (LPSTR)lpszType;
311
312 if(HIWORD(lpszType) != 0) {//type name, translate to id
313 for(i=0;i<MAX_RES;i++) {
314 if(strcmp(lpszType, ResTypes[i]) == 0)
315 break;
316 }
317 if(i == MAX_RES) {//custom resource type, stored as rcdata
318 dprintf(("FindResourceA custom type %s\n", lpszType));
319 lpszType = (LPSTR)NTRT_RCDATA;
320 }
321 else lpszType = (LPSTR)i;
322
323 szType = (LPSTR)lpszType;
324 }
325 switch((int)szType) {
326 case NTRT_GROUP_ICON:
327 szType = (LPSTR)NTRT_ICON;
328 break;
329 case NTRT_GROUP_CURSOR:
330 szType = (LPSTR)NTRT_CURSOR;
331 break;
332 case NTRT_VERSION:
333 szType = (LPSTR)NTRT_RCDATA;
334 break;
335 case NTRT_STRING:
336 case NTRT_MENU:
337 case NTRT_ICON:
338 case NTRT_BITMAP:
339 case NTRT_CURSOR:
340 case NTRT_DIALOG:
341 case NTRT_RCDATA:
342 case NTRT_ACCELERATORS:
343 szType = lpszType;
344 break;
345 default: //unknown are stored as rcdata
346 szType = (LPSTR)NTRT_RCDATA;
347 break;
348 }
349 dprintf(("FindResourceA from %X type %d (%X)\n", hinstance, szType, lpszType));
350
351 if(HIWORD(lpszName) != 0) {//convert string name identifier to numeric id
352 dprintf(("FindResource %s\n", lpszName));
353 if(lpszName[0] == '#') {// #344
354 lpszName = (LPCSTR)atoi(&lpszName[1]);
355 }
356 else lpszName = (LPCSTR)convertNameId((char *)lpszName);
357 }
358 else dprintf(("FindResource %d\n", (int)lpszName));
359
360 hres = O32_FindResource(hinstance, lpszName, szType);
361 if(hres)
362 {
363 switch((ULONG)szType) {
364 case NTRT_MENU:
365 res = new Win32MenuRes(this, hres, (ULONG)lpszName, (ULONG)szType);
366 break;
367 default:
368 res = new Win32Resource(this, hres, (ULONG)lpszName, (ULONG)szType);
369 break;
370 }
371 }
372
373 if(hres == NULL && HIWORD(lpszName) == 0 && (int)szType == NTRT_STRING) {
374 hres = O32_FindResource(hinstance, (LPCSTR)(((int)lpszName - 1)*16), (LPCSTR)NTRT_RCDATA);
375 if(hres)
376 {
377 res = new Win32Resource(this, hres, (ULONG)lpszName, (ULONG)szType);
378 }
379 else dprintf(("FindResourceA can't find string %d\n", (int)lpszName));
380 }
381 dprintf(("FindResourceA returned %X (%X)\n", hres, GetLastError()));
382
383 return (HRSRC)res;
384}
385//******************************************************************************
386//******************************************************************************
387HRSRC Win32ImageBase::findResourceW(LPWSTR lpszName, LPWSTR lpszType, ULONG lang)
388{
389 HRSRC hres;
390 char *astring1 = NULL, *astring2 = NULL;
391
392 if(HIWORD(lpszName) != 0) {
393 astring1 = UnicodeToAsciiString(lpszName);
394 }
395 else astring1 = (char *)lpszName;
396
397 if(HIWORD(lpszType) != 0) {
398 astring2 = UnicodeToAsciiString(lpszType);
399 }
400 else astring2 = (char *)lpszType;
401
402 hres = (HRSRC) findResourceA(astring1, astring2);
403
404 if(HIWORD(astring1)) FreeAsciiString(astring1);
405 if(HIWORD(astring2)) FreeAsciiString(astring2);
406
407 return(hres);
408}
409//******************************************************************************
410//TODO:
411//******************************************************************************
412ULONG Win32Pe2LxImage::getResourceSizeA(LPCSTR lpszName, LPSTR lpszType, ULONG lang)
413{
414 DebugInt3();
415 return 0;
416}
417//******************************************************************************
418//******************************************************************************
419ULONG Win32ImageBase::getResourceSizeA(LPCSTR lpszName, LPSTR lpszType, ULONG lang)
420{
421 return getPEResourceSize((ULONG)lpszName, (ULONG)lpszType, lang);
422}
423//******************************************************************************
424//******************************************************************************
425ULONG Win32ImageBase::getResourceSizeW(LPCWSTR lpszName, LPWSTR lpszType, ULONG lang)
426{
427 char *astring1 = NULL, *astring2 = NULL;
428 ULONG ressize;
429
430 if(HIWORD(lpszName) != 0) {
431 astring1 = UnicodeToAsciiString((LPWSTR)lpszName);
432 }
433 else astring1 = (char *)lpszName;
434
435 if(HIWORD(lpszType) != 0) {
436 astring2 = UnicodeToAsciiString(lpszType);
437 }
438 else astring2 = (char *)lpszType;
439
440 ressize = getResourceSizeA(astring2, astring1, lang);
441
442 if(HIWORD(astring1)) FreeAsciiString(astring1);
443 if(HIWORD(astring2)) FreeAsciiString(astring2);
444
445 return(ressize);
446}
447//******************************************************************************
448//******************************************************************************
449ULONG Win32Pe2LxImage::getVersionSize()
450{
451 if(getVersionId() == -1) {
452 dprintf(("GetVersionSize: %s has no version resource!\n", szModule));
453 return(0);
454 }
455 return OSLibGetResourceSize(hinstance, getVersionId());
456}
457//******************************************************************************
458//******************************************************************************
459BOOL Win32Pe2LxImage::getVersionStruct(char *verstruct, ULONG bufLength)
460{
461 if(getVersionId() == -1) {
462 dprintf(("GetVersionStruct: %s has no version resource!\n", szModule));
463 return(FALSE);
464 }
465 return OSLibGetResource(hinstance, getVersionId(), verstruct, bufLength);
466}
467//******************************************************************************
468//******************************************************************************
469ULONG Win32ImageBase::getVersionSize()
470{
471 return getResourceSizeA((LPCSTR)1, (LPSTR)NTRT_VERSION);
472}
473//******************************************************************************
474//******************************************************************************
475BOOL Win32ImageBase::getVersionStruct(char *verstruct, ULONG bufLength)
476{
477 PIMAGE_RESOURCE_DATA_ENTRY pData = NULL;
478
479 if(verstruct == NULL || bufLength == 0) {
480 SetLastError(ERROR_INVALID_PARAMETER);
481 return FALSE;
482 }
483 pData = getPEResourceEntry(ID_GETFIRST, NTRT_VERSION);
484 if(pData == NULL) {
485 dprintf(("Win32PeLdrImage::getVersionStruct: couldn't find version resource!"));
486 return 0;
487 }
488 char *resdata = (char *)((char *)pResDir + pData->OffsetToData - pResourceSectionStart);
489 memcpy(verstruct, resdata, min(bufLength, pData->Size));
490 return TRUE;
491}
492//******************************************************************************
493//******************************************************************************
Note: See TracBrowser for help on using the repository browser.