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

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

Compilation fixes

File size: 13.1 KB
Line 
1/* $Id: winimgres.cpp,v 1.7 1999-08-17 17:04:52 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 */
12#include <os2win.h>
13#include <stdio.h>
14#include <stdlib.h>
15#include <string.h>
16
17#include "misc.h"
18#include "nameid.h"
19#include "winimage.h"
20#include "windll.h"
21#include "winexe.h"
22#include "winres.h"
23#include "pefile.h"
24#include "unicode.h"
25
26char *ResTypes[MAX_RES] =
27 {"niks", "CURSOR", "BITMAP", "ICON", "MENU", "DIALOG", "STRING",
28 "FONTDIR", "FONT", "ACCELERATOR", "RCDATA", "MESSAGETABLE",
29 "GROUP_CURSOR", "niks", "GROUP_ICON", "niks", "VERSION"};
30
31
32//******************************************************************************
33//TODO: use OS/2 unicode stuff
34//******************************************************************************
35char *extremelyuglyUnicodeToAscii(int length, WCHAR *NameString)
36{
37static char asciistring[256];
38int i;
39
40 if(length >= 255) length = 255;
41 for(i=0;i<length;i++) {
42 asciistring[i] = NameString[i] & 0xFF;
43 }
44 asciistring[length] = 0;
45 return(asciistring);
46}
47//******************************************************************************
48//******************************************************************************
49
50//******************************************************************************
51//Assuming names are case insensitive
52//PE spec says names & ids are sorted; keep on searching just to be sure
53//******************************************************************************
54Win32Resource *Win32Image::getPEResource(ULONG id, ULONG type, ULONG lang)
55{
56 PIMAGE_RESOURCE_DIRECTORY prdType;
57 PIMAGE_RESOURCE_DIRECTORY_ENTRY prde;
58 PIMAGE_RESOURCE_DATA_ENTRY pData = NULL;
59 Win32Resource *res;
60 ULONG nodeData[3];
61 ULONG nrres;
62 BOOL fFound = FALSE, fNumId;
63 char *winres = NULL;
64 int i, stringid = -1, j;
65
66 /* set pointer to first resource type entry */
67 prde = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)((ULONG)pResDir + sizeof(IMAGE_RESOURCE_DIRECTORY));
68
69 /* loop through all resource directory entry types */
70 //1st level -> types
71 //2nd level -> names
72 //3rd level -> language
73 nodeData[0] = id;
74 nodeData[1] = lang;
75 nodeData[2] = 0xFFFFFFFF;
76
77 nrres = pResDir->NumberOfIdEntries + pResDir->NumberOfNamedEntries;
78 fNumId = TRUE; //assume numeric
79 if(((ULONG)type >> 16) != 0) {//string id?
80 for(i=0;i<MAX_RES;i++) {
81 if(stricmp((char *)type, ResTypes[i]) == 0)
82 break;
83 }
84 if(i == MAX_RES) {//custom resource type
85 nrres = pResDir->NumberOfNamedEntries;
86 fNumId = FALSE;
87 }
88 else type = i;
89 }
90
91 //String format: tables of 16 strings stored as one resource
92 //upper 12 bits of resource id passed by user determines block (res id)
93 //lower 4 bits are an index into the string table
94 if(fNumId) {
95 if(type == NTRT_STRING) {
96 stringid = id & 0xF;
97 id = (id >> 4);
98 }
99 }
100 else {
101 if(stricmp((char *)type, ResTypes[NTRT_STRING]) == 0) {
102 stringid = id & 0xF;
103 id = (id >> 4);
104 }
105 }
106
107 for(i=0;i<nrres;i++) {
108 /* locate directory or each resource type */
109 prdType = (PIMAGE_RESOURCE_DIRECTORY)((ULONG)pResDir + (ULONG)prde->u2.OffsetToData);
110
111 if(i < prdType->NumberOfNamedEntries) {//name or id entry?
112 //SvL: 30-10-'97, high bit is set, so clear to get real offset
113 prde->u1.Name &= ~0x80000000;
114 char *resname = extremelyuglyUnicodeToAscii(*(WCHAR *)((ULONG)pResDir + (ULONG)prde->u1.Name), (WCHAR *)((ULONG)pResDir + (ULONG)prde->u1.Name + sizeof(WCHAR))); // first word = string length
115 if(!fNumId) {
116 if(stricmp(resname, (char *)type) == 0) {
117 fFound = TRUE;
118 }
119 }
120 else {
121 for(j=0;j<MAX_RES;j++) {
122 if(stricmp((char *)type, ResTypes[j]) == 0)
123 break;
124 }
125 if(j == type) {
126 fFound = TRUE;
127 }
128 }
129 }
130 else {
131 if(prde->u1.Id == type) {
132 fFound = TRUE;
133 }
134 }
135 if(fFound) {
136 if((ULONG)prdType & 0x80000000) {//subdirectory?
137 pData = ProcessResSubDir(prdType, &nodeData[0]);
138 }
139 else {
140 pData = (PIMAGE_RESOURCE_DATA_ENTRY)prdType;
141 dprintf(("getResource: not a subdir!!\n"));
142 }
143 break;
144 }
145 /* increment to next entry */
146 prde++;
147 }
148 if(pData == NULL) {
149 dprintf(("getResource: res not found!\n"));
150 return(NULL);
151 }
152
153 char *resdata = (char *)((char *)pResDir + pData->OffsetToData - pResSection->virtaddr);
154 if(stringid != -1) {//search for string in table
155 USHORT *unicodestr = (USHORT *)resdata;
156
157 for(i=0;i<stringid;i++) {
158 unicodestr += *unicodestr;
159 }
160 res = new Win32Resource(this, id, NTRT_STRING, (ULONG)*unicodestr, (char *)unicodestr);
161 if(res == NULL) {
162 dprintf(("new Win32Resource failed!\n"));
163 return(NULL);
164 }
165 }
166 else res = new Win32Resource(this, id, type, pData->Size, resdata);
167
168 return res;
169}
170//******************************************************************************
171//******************************************************************************
172PIMAGE_RESOURCE_DATA_ENTRY
173 Win32Image::ProcessResSubDir(PIMAGE_RESOURCE_DIRECTORY prdType,
174 ULONG *nodeData)
175{
176 PIMAGE_RESOURCE_DIRECTORY prdType2;
177 PIMAGE_RESOURCE_DIRECTORY_ENTRY prde;
178 PIMAGE_RESOURCE_DIR_STRING_U pstring;
179 PIMAGE_RESOURCE_DATA_ENTRY pData;
180 BOOL fFound = FALSE, fNumId;
181 ULONG nrres;
182 char *resname;
183 int i;
184
185 if(*nodeData == 0xFFFFFFFF) {//shouldn't happen!
186 dprintf(("ProcessResSubDir: *nodeData == 0xFFFFFFFF!\n"));
187 return(NULL);
188 }
189 prdType = (PIMAGE_RESOURCE_DIRECTORY)((ULONG)prdType & ~0x80000000);
190 prde = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)((DWORD)prdType + sizeof(IMAGE_RESOURCE_DIRECTORY));
191
192 fNumId = (*nodeData >> 16) == 0;
193
194 if(fNumId) {//numeric or string id?
195 nrres = pResDir->NumberOfIdEntries;
196 prde += pResDir->NumberOfNamedEntries; //skip name entries
197 }
198 else nrres = pResDir->NumberOfNamedEntries;
199
200 for(i=0;i<nrres;i++) {
201 /* locate directory or each resource type */
202 prdType2 = (PIMAGE_RESOURCE_DIRECTORY)((ULONG)pResDir + (ULONG)prde->u2.OffsetToData);
203
204 if(!fNumId) {//name or id entry?
205 if(prde->u1.s.NameIsString) //unicode directory string /*PLF Sat 97-06-21 22:30:35*/
206 prde->u1.Name &= ~0x80000000;
207 pstring = (PIMAGE_RESOURCE_DIR_STRING_U)((ULONG)pResDir + (ULONG)prde->u1.Name);
208 resname = extremelyuglyUnicodeToAscii(pstring->Length, pstring->NameString);
209 if(stricmp(resname, (char *)*nodeData) == 0) {
210 fFound = TRUE;
211 }
212 }
213 else {
214 if(*nodeData == prde->u1.Id)
215 fFound = TRUE;
216 }
217 if(*nodeData == LANG_GETFIRST)
218 fFound = TRUE;
219
220 if(fFound) {
221 if((ULONG)prdType2 & 0x80000000) {//subdirectory?
222 ProcessResSubDir(prdType2, nodeData+1);
223 }
224 else {
225 pData = (PIMAGE_RESOURCE_DATA_ENTRY)prdType2;
226 if(pData->Size) {//winamp17 winzip archive has resource with size 0
227 return(pData);
228 }
229 else return(NULL);
230 }
231 }
232 prde++;
233 }
234 return(NULL);
235}
236//******************************************************************************
237//******************************************************************************
238HRSRC Win32Image::findResourceA(LPCSTR lpszName, LPSTR lpszType)
239{
240 Win32Resource *res = NULL;
241 HRSRC hres;
242 int i;
243 LPSTR szType = (LPSTR)lpszType;
244
245 if(fNativePEImage == TRUE) {
246 return (HRSRC) getPEResource((ULONG)lpszName, (ULONG)lpszType);
247 }
248 //else converted win32 exe/dll
249
250 if((int)lpszType >> 16 != 0) {//type name, translate to id
251 for(i=0;i<MAX_RES;i++) {
252 if(strcmp(lpszType, ResTypes[i]) == 0)
253 break;
254 }
255 if(i == MAX_RES) {//custom resource type, stored as rcdata
256 dprintf(("FindResourceA custom type %s\n", lpszType));
257 lpszType = (LPSTR)NTRT_RCDATA;
258 }
259 else lpszType = (LPSTR)i;
260
261 szType = (LPSTR)lpszType;
262 }
263 switch((int)szType) {
264 case NTRT_GROUP_ICON:
265 szType = (LPSTR)NTRT_ICON;
266 break;
267 case NTRT_GROUP_CURSOR:
268 szType = (LPSTR)NTRT_CURSOR;
269 break;
270 case NTRT_VERSION:
271 szType = (LPSTR)NTRT_RCDATA;
272 break;
273 case NTRT_STRING:
274 case NTRT_MENU:
275 case NTRT_ICON:
276 case NTRT_BITMAP:
277 case NTRT_CURSOR:
278 case NTRT_DIALOG:
279 case NTRT_RCDATA:
280 case NTRT_ACCELERATORS:
281 szType = lpszType;
282 break;
283 default: //unknown are stored as rcdata
284 szType = (LPSTR)NTRT_RCDATA;
285 break;
286 }
287 dprintf(("FindResourceA from %X type %d (%X)\n", hinstance, szType, lpszType));
288
289 if((int)lpszName >> 16 != 0) {//convert string name identifier to numeric id
290 dprintf(("FindResource %s\n", lpszName));
291 if(lpszName[0] == '#') {// #344
292 lpszName = (LPCSTR)atoi(&lpszName[1]);
293 }
294 else lpszName = (LPCSTR)ConvertNameId(hinstance, (char *)lpszName);
295 }
296 else dprintf(("FindResource %d\n", (int)lpszName));
297
298 hres = O32_FindResource(hinstance, lpszName, szType);
299 if(hres)
300 {
301 res = new Win32Resource(this, hres, (ULONG)lpszName, (ULONG)szType);
302 }
303
304 if(hres == NULL && (int)lpszName >> 16 == 0 && (int)szType == NTRT_STRING) {
305 hres = O32_FindResource(hinstance, (LPCSTR)(((int)lpszName - 1)*16), (LPCSTR)NTRT_RCDATA);
306 if(hres)
307 {
308 res = new Win32Resource(this, hres, (ULONG)lpszName, (ULONG)szType);
309 }
310 else dprintf(("FindResourceA can't find string %d\n", (int)lpszName));
311 }
312 dprintf(("FindResourceA returned %X (%X)\n", hres, GetLastError()));
313
314 return (HRSRC)res;
315}
316//******************************************************************************
317//******************************************************************************
318HRSRC Win32Image::findResourceW(LPWSTR lpszName, LPWSTR lpszType)
319{
320 Win32Resource *res = NULL;
321 HRSRC hres;
322 LPSTR szType = (LPSTR)lpszType;
323 int i;
324 char *astring1 = NULL, *astring2 = NULL;
325
326 if(fNativePEImage == TRUE) {//load resources directly from res section
327 if((int)lpszType >> 16 != 0) {
328 char *resname = UnicodeToAsciiString(lpszType);
329 }
330 else astring1 = (char *)lpszType;
331
332 if((int)lpszName >> 16 != 0) {
333 astring2 = UnicodeToAsciiString(lpszName);
334 }
335 else astring2 = (char *)lpszName;
336
337 hres = (HRSRC) getPEResource((ULONG)astring1, (ULONG)astring1);
338 if(astring1) FreeAsciiString(astring1);
339 if(astring2) FreeAsciiString(astring2);
340
341 return(hres);
342 }
343 //else converted win32 exe/dll
344 if((int)lpszType >> 16 != 0) {//type name, translate to id
345 char *resname = UnicodeToAsciiString(lpszType);
346 for(i=0;i<MAX_RES;i++) {
347 if(strcmp(resname, ResTypes[i]) == 0)
348 break;
349 }
350 if(i == MAX_RES) {//custom resource type, stored as rcdata
351 dprintf(("FindResourceW custom type %s\n", resname));
352 i = NTRT_RCDATA;
353 }
354 FreeAsciiString(resname);
355 lpszType = (LPWSTR)i;
356
357 szType = (LPSTR)lpszType;
358 }
359 switch((int)szType) {
360 case NTRT_GROUP_ICON:
361 szType = (LPSTR)NTRT_ICON;
362 break;
363 case NTRT_GROUP_CURSOR:
364 szType = (LPSTR)NTRT_CURSOR;
365 break;
366 case NTRT_VERSION:
367 szType = (LPSTR)NTRT_RCDATA;
368 break;
369 case NTRT_STRING:
370 case NTRT_MENU:
371 case NTRT_ICON:
372 case NTRT_BITMAP:
373 case NTRT_CURSOR:
374 case NTRT_DIALOG:
375 case NTRT_RCDATA:
376 case NTRT_ACCELERATORS:
377 szType = (LPSTR)lpszType;
378 break;
379 default: //unknown are stored as rcdata
380 szType = (LPSTR)NTRT_RCDATA;
381 break;
382 }
383 dprintf(("FindResourceW type %d\n", szType));
384
385 if((int)lpszName >> 16 != 0) {//convert string name identifier to numeric id
386 astring1 = UnicodeToAsciiString(lpszName);
387 dprintf(("FindResourceW %X %s\n", hinstance, astring1));
388 if(astring1[0] == '#') {// #344
389 lpszName = (LPWSTR)atoi(&astring1[1]);
390 }
391 else lpszName = (LPWSTR)ConvertNameId(hinstance, (char *)astring1);
392 }
393 else dprintf(("FindResourceW %X %d\n", hinstance, (int)lpszName));
394
395 hres = O32_FindResource(hinstance, (LPCSTR)lpszName, (LPCSTR)szType);
396 if(hres)
397 {
398 res = new Win32Resource(this, hres, (ULONG)lpszName, (ULONG)szType);
399 }
400
401 if(hres == NULL && (int)lpszName >> 16 == 0 && (int)szType == NTRT_STRING) {
402 hres = O32_FindResource(hinstance, (LPCSTR)(((ULONG)lpszName - 1)*16), (LPCSTR)NTRT_RCDATA);
403 if(hres)
404 {
405 res = new Win32Resource(this, hres, (ULONG)lpszName, (ULONG)szType);
406 }
407 else dprintf(("FindResourceW can't find string %d\n", (int)lpszName));
408 }
409 if(astring1) FreeAsciiString(astring1);
410
411 dprintf(("FindResourceW returned %X (%X)\n", hres, GetLastError()));
412
413 return (HRSRC)res;
414}
415//******************************************************************************
416//******************************************************************************
Note: See TracBrowser for help on using the repository browser.