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

Last change on this file since 1036 was 1016, checked in by phaller, 26 years ago

Fix: additional fix in findResourceW, getResourceSizeW

File size: 15.8 KB
Line 
1/* $Id: winimgres.cpp,v 1.17 1999-09-23 14:12:14 phaller 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 Win32ImageBase::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 Win32ImageBase::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 Win32ImageBase::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 Win32ImageBase::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 //pResourceSectionStart contains the virtual address of the imagebase in the PE header
266 //for the resource section (images loaded by the pe.exe)
267 //For LX images, this is 0 as OffsetToData contains a relative offset
268 char *resdata = (char *)((char *)pResDir + pData->OffsetToData - pResourceSectionStart);
269 if(stringid != -1) {//search for string in table
270 USHORT *unicodestr = (USHORT *)resdata;
271
272 for(i=0;i<stringid;i++) {
273 unicodestr += *unicodestr+1;
274 }
275 res = new Win32Resource(this, id, NTRT_STRING, (*unicodestr+1)*sizeof(WCHAR),
276 (char *)(unicodestr+1));
277 if(res == NULL) {
278 dprintf(("new Win32Resource failed!\n"));
279 return(NULL);
280 }
281 }
282 else {
283 switch(type) {
284 case NTRT_MENU:
285 res = new Win32MenuRes(this, id, type, pData->Size, resdata);
286 break;
287 default:
288 res = new Win32Resource(this, id, type, pData->Size, resdata);
289 break;
290 }
291
292 }
293
294 return (HRSRC) res;
295}
296//******************************************************************************
297//******************************************************************************
298HRSRC Win32Pe2LxImage::findResourceA(LPCSTR lpszName, LPSTR lpszType, ULONG lang)
299{
300 Win32Resource *res = NULL;
301 HRSRC hres;
302 int i;
303 LPSTR szType = (LPSTR)lpszType;
304
305 if(HIWORD(lpszType) != 0) {//type name, translate to id
306 for(i=0;i<MAX_RES;i++) {
307 if(strcmp(lpszType, ResTypes[i]) == 0)
308 break;
309 }
310 if(i == MAX_RES) {//custom resource type, stored as rcdata
311 dprintf(("FindResourceA custom type %s\n", lpszType));
312 lpszType = (LPSTR)NTRT_RCDATA;
313 }
314 else lpszType = (LPSTR)i;
315
316 szType = (LPSTR)lpszType;
317 }
318 switch((int)szType) {
319 case NTRT_GROUP_ICON:
320 szType = (LPSTR)NTRT_ICON;
321 break;
322 case NTRT_GROUP_CURSOR:
323 szType = (LPSTR)NTRT_CURSOR;
324 break;
325 case NTRT_VERSION:
326 szType = (LPSTR)NTRT_RCDATA;
327 break;
328 case NTRT_STRING:
329 case NTRT_MENU:
330 case NTRT_ICON:
331 case NTRT_BITMAP:
332 case NTRT_CURSOR:
333 case NTRT_DIALOG:
334 case NTRT_RCDATA:
335 case NTRT_ACCELERATORS:
336 szType = lpszType;
337 break;
338 default: //unknown are stored as rcdata
339 szType = (LPSTR)NTRT_RCDATA;
340 break;
341 }
342 dprintf(("FindResourceA from %X type %d (%X)\n", hinstance, szType, lpszType));
343
344 if(HIWORD(lpszName) != 0) {//convert string name identifier to numeric id
345 dprintf(("FindResource %s\n", lpszName));
346 if(lpszName[0] == '#') {// #344
347 lpszName = (LPCSTR)atoi(&lpszName[1]);
348 }
349 else lpszName = (LPCSTR)convertNameId((char *)lpszName);
350 }
351 else dprintf(("FindResource %d\n", (int)lpszName));
352
353 hres = O32_FindResource(hinstance, lpszName, szType);
354 if(hres)
355 {
356 switch((ULONG)szType) {
357 case NTRT_MENU:
358 res = new Win32MenuRes(this, hres, (ULONG)lpszName, (ULONG)szType);
359 break;
360 default:
361 res = new Win32Resource(this, hres, (ULONG)lpszName, (ULONG)szType);
362 break;
363 }
364 }
365
366 if(hres == NULL && HIWORD(lpszName) == 0 && (int)szType == NTRT_STRING) {
367 hres = O32_FindResource(hinstance, (LPCSTR)(((int)lpszName - 1)*16), (LPCSTR)NTRT_RCDATA);
368 if(hres)
369 {
370 res = new Win32Resource(this, hres, (ULONG)lpszName, (ULONG)szType);
371 }
372 else dprintf(("FindResourceA can't find string %d\n", (int)lpszName));
373 }
374 dprintf(("FindResourceA returned %X (%X)\n", hres, GetLastError()));
375
376 return (HRSRC)res;
377}
378//******************************************************************************
379//******************************************************************************
380HRSRC Win32ImageBase::findResourceW(LPWSTR lpszName, LPWSTR lpszType, ULONG lang)
381{
382 HRSRC hres;
383 char *astring1 = NULL,
384 *astring2 = NULL;
385 BOOL fAllocated1 = FALSE;
386 BOOL fAllocated2 = FALSE;
387
388 if(HIWORD(lpszName) != 0)
389 {
390 astring1 = UnicodeToAsciiString((LPWSTR)lpszName);
391 fAllocated1 = TRUE;
392 }
393 else
394 astring1 = (char *)lpszName;
395
396 if(HIWORD(lpszType) != 0)
397 {
398 astring2 = UnicodeToAsciiString(lpszType);
399 fAllocated2 = TRUE;
400 }
401 else
402 astring2 = (char *)lpszType;
403 hres = (HRSRC) findResourceA(astring1, astring2);
404
405 /* do NOT free untranslated numerical Resource IDs */
406 if(fAllocated1) FreeAsciiString(astring1);
407 if(fAllocated2) FreeAsciiString(astring2);
408
409 return(hres);
410}
411//******************************************************************************
412//TODO:
413//******************************************************************************
414ULONG Win32Pe2LxImage::getResourceSizeA(LPCSTR lpszName, LPSTR lpszType, ULONG lang)
415{
416 DebugInt3();
417 return 0;
418}
419//******************************************************************************
420//******************************************************************************
421ULONG Win32ImageBase::getResourceSizeA(LPCSTR lpszName, LPSTR lpszType, ULONG lang)
422{
423 return getPEResourceSize((ULONG)lpszName, (ULONG)lpszType, lang);
424}
425//******************************************************************************
426//******************************************************************************
427ULONG Win32ImageBase::getResourceSizeW(LPCWSTR lpszName, LPWSTR lpszType, ULONG lang)
428{
429 char *astring1 = NULL,
430 *astring2 = NULL;
431 ULONG ressize;
432 BOOL fAllocated1 = FALSE;
433 BOOL fAllocated2 = FALSE;
434
435 if(HIWORD(lpszName) != 0)
436 {
437 astring1 = UnicodeToAsciiString((LPWSTR)lpszName);
438 fAllocated1 = TRUE;
439 }
440 else
441 astring1 = (char *)lpszName;
442
443 if(HIWORD(lpszType) != 0)
444 {
445 astring2 = UnicodeToAsciiString(lpszType);
446 fAllocated2 = TRUE;
447 }
448 else
449 astring2 = (char *)lpszType;
450
451 ressize = getResourceSizeA(astring1, astring2, lang);
452
453 /* do NOT free untranslated numerical Resource IDs */
454 if(fAllocated1) FreeAsciiString(astring1);
455 if(fAllocated2) FreeAsciiString(astring2);
456
457 return(ressize);
458}
459//******************************************************************************
460//******************************************************************************
461ULONG Win32Pe2LxImage::getVersionSize()
462{
463 if(getVersionId() == -1) {
464 dprintf(("GetVersionSize: %s has no version resource!\n", szModule));
465 return(0);
466 }
467 return OSLibGetResourceSize(hinstance, getVersionId());
468}
469//******************************************************************************
470//******************************************************************************
471BOOL Win32Pe2LxImage::getVersionStruct(char *verstruct, ULONG bufLength)
472{
473 if(getVersionId() == -1) {
474 dprintf(("GetVersionStruct: %s has no version resource!\n", szModule));
475 return(FALSE);
476 }
477 return OSLibGetResource(hinstance, getVersionId(), verstruct, bufLength);
478}
479//******************************************************************************
480//******************************************************************************
481ULONG Win32ImageBase::getVersionSize()
482{
483 return getResourceSizeA((LPCSTR)1, (LPSTR)NTRT_VERSION);
484}
485//******************************************************************************
486//******************************************************************************
487BOOL Win32ImageBase::getVersionStruct(char *verstruct, ULONG bufLength)
488{
489 PIMAGE_RESOURCE_DATA_ENTRY pData = NULL;
490
491 pData = getPEResourceEntry(1, NTRT_VERSION);
492 if(pData == NULL) {
493 dprintf(("Win32PeLdrImage::getVersionStruct: couldn't find version resource!"));
494 return 0;
495 }
496 return pData->Size;
497}
498//******************************************************************************
499//******************************************************************************
Note: See TracBrowser for help on using the repository browser.