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

Last change on this file since 150 was 99, checked in by phaller, 26 years ago

Add: added cvs variable $Id$ to source files.

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