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

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

Rewrite for new win32 image classes

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