2 * PE (Portable Execute) File Resources
4 * Copyright 1995 Thomas Sandford
5 * Copyright 1996 Martin von Loewis
7 * Based on the Win16 resource handling code in loader/resource.c
8 * Copyright 1993 Robert J. Amstadt
9 * Copyright 1995 Alexandre Julliard
10 * Copyright 1997 Marcus Meissner
14 #include <sys/types.h>
15 #include "wine/winestring.h"
23 #include "stackframe.h"
26 #include "debugtools.h"
28 /**********************************************************************
29 * HMODULE32toPE_MODREF
31 * small helper function to get a PE_MODREF from a passed HMODULE32
34 HMODULE32toPE_MODREF(HMODULE hmod
) {
37 wm
= MODULE32_LookupHMODULE( hmod
);
38 if (!wm
|| wm
->type
!=MODULE32_PE
)
40 return &(wm
->binfmt
.pe
);
43 /**********************************************************************
46 * Helper function - goes down one level of PE resource tree
49 PIMAGE_RESOURCE_DIRECTORY
GetResDirEntryW(PIMAGE_RESOURCE_DIRECTORY resdirptr
,
50 LPCWSTR name
,DWORD root
,
54 PIMAGE_RESOURCE_DIRECTORY_ENTRY entryTable
;
61 lstrcpynWtoA(buf
,name
+1,10);
62 return GetResDirEntryW(resdirptr
,(LPCWSTR
)atoi(buf
),root
,allowdefault
);
64 entryTable
= (PIMAGE_RESOURCE_DIRECTORY_ENTRY
) (
66 sizeof(IMAGE_RESOURCE_DIRECTORY
));
67 namelen
= lstrlenW(name
);
68 for (entrynum
= 0; entrynum
< resdirptr
->NumberOfNamedEntries
; entrynum
++)
70 PIMAGE_RESOURCE_DIR_STRING_U str
=
71 (PIMAGE_RESOURCE_DIR_STRING_U
) (root
+
72 entryTable
[entrynum
].u1
.s
.NameOffset
);
73 if(namelen
!= str
->Length
)
75 if(CRTDLL__wcsnicmp(name
,str
->NameString
,str
->Length
)==0)
76 return (PIMAGE_RESOURCE_DIRECTORY
) (
78 entryTable
[entrynum
].u2
.s
.OffsetToDirectory
);
82 entryTable
= (PIMAGE_RESOURCE_DIRECTORY_ENTRY
) (
84 sizeof(IMAGE_RESOURCE_DIRECTORY
) +
85 resdirptr
->NumberOfNamedEntries
* sizeof(IMAGE_RESOURCE_DIRECTORY_ENTRY
));
86 for (entrynum
= 0; entrynum
< resdirptr
->NumberOfIdEntries
; entrynum
++)
87 if ((DWORD
)entryTable
[entrynum
].u1
.Name
== (DWORD
)name
)
88 return (PIMAGE_RESOURCE_DIRECTORY
) (
90 entryTable
[entrynum
].u2
.s
.OffsetToDirectory
);
91 /* just use first entry if no default can be found */
92 if (allowdefault
&& !name
&& resdirptr
->NumberOfIdEntries
)
93 return (PIMAGE_RESOURCE_DIRECTORY
) (
95 entryTable
[0].u2
.s
.OffsetToDirectory
);
100 /**********************************************************************
103 PIMAGE_RESOURCE_DIRECTORY
GetResDirEntryA( PIMAGE_RESOURCE_DIRECTORY resdirptr
,
104 LPCSTR name
, DWORD root
,
107 PIMAGE_RESOURCE_DIRECTORY retv
;
108 LPWSTR nameW
= HIWORD(name
)? HEAP_strdupAtoW( GetProcessHeap(), 0, name
)
111 retv
= GetResDirEntryW( resdirptr
, nameW
, root
, allowdefault
);
113 if ( HIWORD(name
) ) HeapFree( GetProcessHeap(), 0, nameW
);
118 /**********************************************************************
119 * PE_FindResourceEx32W
121 HANDLE
PE_FindResourceExW(
122 WINE_MODREF
*wm
,LPCWSTR name
,LPCWSTR type
,WORD lang
124 PIMAGE_RESOURCE_DIRECTORY resdirptr
;
127 PE_MODREF
*pem
= &(wm
->binfmt
.pe
);
129 if (!pem
|| !pem
->pe_resource
)
132 resdirptr
= pem
->pe_resource
;
133 root
= (DWORD
) resdirptr
;
134 if ((resdirptr
= GetResDirEntryW(resdirptr
, type
, root
, FALSE
)) == NULL
)
136 if ((resdirptr
= GetResDirEntryW(resdirptr
, name
, root
, FALSE
)) == NULL
)
138 result
= (HANDLE
)GetResDirEntryW(resdirptr
, (LPCWSTR
)(UINT
)lang
, root
, FALSE
);
139 /* Try LANG_NEUTRAL, too */
141 return (HANDLE
)GetResDirEntryW(resdirptr
, (LPCWSTR
)0, root
, TRUE
);
146 /**********************************************************************
149 HANDLE
PE_LoadResource( WINE_MODREF
*wm
, HANDLE hRsrc
)
151 if (!hRsrc
|| !wm
|| wm
->type
!=MODULE32_PE
)
153 return (HANDLE
) (wm
->module
+ ((PIMAGE_RESOURCE_DATA_ENTRY
)hRsrc
)->OffsetToData
);
157 /**********************************************************************
158 * PE_SizeofResource32
160 DWORD
PE_SizeofResource( HINSTANCE hModule
, HANDLE hRsrc
)
162 /* we don't need hModule */
165 return ((PIMAGE_RESOURCE_DATA_ENTRY
)hRsrc
)->Size
;
168 /**********************************************************************
169 * PE_EnumResourceTypes32A
172 PE_EnumResourceTypesA(HMODULE hmod
,ENUMRESTYPEPROCA lpfun
,LONG lparam
) {
173 PE_MODREF
*pem
= HMODULE32toPE_MODREF(hmod
);
175 PIMAGE_RESOURCE_DIRECTORY resdir
;
176 PIMAGE_RESOURCE_DIRECTORY_ENTRY et
;
178 HANDLE heap
= GetProcessHeap();
180 if (!pem
|| !pem
->pe_resource
)
183 resdir
= (PIMAGE_RESOURCE_DIRECTORY
)pem
->pe_resource
;
184 et
=(PIMAGE_RESOURCE_DIRECTORY_ENTRY
)((LPBYTE
)resdir
+sizeof(IMAGE_RESOURCE_DIRECTORY
));
186 for (i
=0;i
<resdir
->NumberOfNamedEntries
+resdir
->NumberOfIdEntries
;i
++) {
189 if (et
[i
].u1
.s
.NameIsString
)
190 name
= HEAP_strdupWtoA(heap
,0,(LPWSTR
)((LPBYTE
)pem
->pe_resource
+et
[i
].u1
.s
.NameOffset
));
192 name
= (LPSTR
)(int)et
[i
].u1
.Id
;
193 ret
= lpfun(hmod
,name
,lparam
);
195 HeapFree(heap
,0,name
);
202 /**********************************************************************
203 * PE_EnumResourceTypes32W
206 PE_EnumResourceTypesW(HMODULE hmod
,ENUMRESTYPEPROCW lpfun
,LONG lparam
) {
207 PE_MODREF
*pem
= HMODULE32toPE_MODREF(hmod
);
209 PIMAGE_RESOURCE_DIRECTORY resdir
;
210 PIMAGE_RESOURCE_DIRECTORY_ENTRY et
;
213 if (!pem
|| !pem
->pe_resource
)
216 resdir
= (PIMAGE_RESOURCE_DIRECTORY
)pem
->pe_resource
;
217 et
=(PIMAGE_RESOURCE_DIRECTORY_ENTRY
)((LPBYTE
)resdir
+sizeof(IMAGE_RESOURCE_DIRECTORY
));
219 for (i
=0;i
<resdir
->NumberOfNamedEntries
+resdir
->NumberOfIdEntries
;i
++) {
221 if (et
[i
].u1
.s
.NameIsString
)
222 type
= (LPWSTR
)((LPBYTE
)pem
->pe_resource
+et
[i
].u1
.s
.NameOffset
);
224 type
= (LPWSTR
)(int)et
[i
].u1
.Id
;
226 ret
= lpfun(hmod
,type
,lparam
);
233 /**********************************************************************
234 * PE_EnumResourceNames32A
237 PE_EnumResourceNamesA(
238 HMODULE hmod
,LPCSTR type
,ENUMRESNAMEPROCA lpfun
,LONG lparam
240 PE_MODREF
*pem
= HMODULE32toPE_MODREF(hmod
);
242 PIMAGE_RESOURCE_DIRECTORY resdir
;
243 PIMAGE_RESOURCE_DIRECTORY_ENTRY et
;
245 HANDLE heap
= GetProcessHeap();
248 if (!pem
|| !pem
->pe_resource
)
250 resdir
= (PIMAGE_RESOURCE_DIRECTORY
)pem
->pe_resource
;
252 typeW
= HEAP_strdupAtoW(heap
,0,type
);
254 typeW
= (LPWSTR
)type
;
255 resdir
= GetResDirEntryW(resdir
,typeW
,(DWORD
)pem
->pe_resource
,FALSE
);
257 HeapFree(heap
,0,typeW
);
260 et
=(PIMAGE_RESOURCE_DIRECTORY_ENTRY
)((LPBYTE
)resdir
+sizeof(IMAGE_RESOURCE_DIRECTORY
));
262 for (i
=0;i
<resdir
->NumberOfNamedEntries
+resdir
->NumberOfIdEntries
;i
++) {
265 if (et
[i
].u1
.s
.NameIsString
)
266 name
= HEAP_strdupWtoA(heap
,0,(LPWSTR
)((LPBYTE
)pem
->pe_resource
+et
[i
].u1
.s
.NameOffset
));
268 name
= (LPSTR
)(int)et
[i
].u1
.Id
;
269 ret
= lpfun(hmod
,type
,name
,lparam
);
270 if (HIWORD(name
)) HeapFree(heap
,0,name
);
277 /**********************************************************************
278 * PE_EnumResourceNames32W
281 PE_EnumResourceNamesW(
282 HMODULE hmod
,LPCWSTR type
,ENUMRESNAMEPROCW lpfun
,LONG lparam
284 PE_MODREF
*pem
= HMODULE32toPE_MODREF(hmod
);
286 PIMAGE_RESOURCE_DIRECTORY resdir
;
287 PIMAGE_RESOURCE_DIRECTORY_ENTRY et
;
290 if (!pem
|| !pem
->pe_resource
)
293 resdir
= (PIMAGE_RESOURCE_DIRECTORY
)pem
->pe_resource
;
294 resdir
= GetResDirEntryW(resdir
,type
,(DWORD
)pem
->pe_resource
,FALSE
);
297 et
=(PIMAGE_RESOURCE_DIRECTORY_ENTRY
)((LPBYTE
)resdir
+sizeof(IMAGE_RESOURCE_DIRECTORY
));
299 for (i
=0;i
<resdir
->NumberOfNamedEntries
+resdir
->NumberOfIdEntries
;i
++) {
301 if (et
[i
].u1
.s
.NameIsString
)
302 name
= (LPWSTR
)((LPBYTE
)pem
->pe_resource
+et
[i
].u1
.s
.NameOffset
);
304 name
= (LPWSTR
)(int)et
[i
].u1
.Id
;
305 ret
= lpfun(hmod
,type
,name
,lparam
);
312 /**********************************************************************
313 * PE_EnumResourceNames32A
316 PE_EnumResourceLanguagesA(
317 HMODULE hmod
,LPCSTR name
,LPCSTR type
,ENUMRESLANGPROCA lpfun
,
320 PE_MODREF
*pem
= HMODULE32toPE_MODREF(hmod
);
322 PIMAGE_RESOURCE_DIRECTORY resdir
;
323 PIMAGE_RESOURCE_DIRECTORY_ENTRY et
;
325 HANDLE heap
= GetProcessHeap();
328 if (!pem
|| !pem
->pe_resource
)
331 resdir
= (PIMAGE_RESOURCE_DIRECTORY
)pem
->pe_resource
;
333 nameW
= HEAP_strdupAtoW(heap
,0,name
);
335 nameW
= (LPWSTR
)name
;
336 resdir
= GetResDirEntryW(resdir
,nameW
,(DWORD
)pem
->pe_resource
,FALSE
);
338 HeapFree(heap
,0,nameW
);
342 typeW
= HEAP_strdupAtoW(heap
,0,type
);
344 typeW
= (LPWSTR
)type
;
345 resdir
= GetResDirEntryW(resdir
,typeW
,(DWORD
)pem
->pe_resource
,FALSE
);
347 HeapFree(heap
,0,typeW
);
350 et
=(PIMAGE_RESOURCE_DIRECTORY_ENTRY
)((LPBYTE
)resdir
+sizeof(IMAGE_RESOURCE_DIRECTORY
));
352 for (i
=0;i
<resdir
->NumberOfNamedEntries
+resdir
->NumberOfIdEntries
;i
++) {
353 /* languages are just ids... I hopem */
354 ret
= lpfun(hmod
,name
,type
,et
[i
].u1
.Id
,lparam
);
361 /**********************************************************************
362 * PE_EnumResourceLanguages32W
365 PE_EnumResourceLanguagesW(
366 HMODULE hmod
,LPCWSTR name
,LPCWSTR type
,ENUMRESLANGPROCW lpfun
,
369 PE_MODREF
*pem
= HMODULE32toPE_MODREF(hmod
);
371 PIMAGE_RESOURCE_DIRECTORY resdir
;
372 PIMAGE_RESOURCE_DIRECTORY_ENTRY et
;
375 if (!pem
|| !pem
->pe_resource
)
378 resdir
= (PIMAGE_RESOURCE_DIRECTORY
)pem
->pe_resource
;
379 resdir
= GetResDirEntryW(resdir
,name
,(DWORD
)pem
->pe_resource
,FALSE
);
382 resdir
= GetResDirEntryW(resdir
,type
,(DWORD
)pem
->pe_resource
,FALSE
);
385 et
=(PIMAGE_RESOURCE_DIRECTORY_ENTRY
)((LPBYTE
)resdir
+sizeof(IMAGE_RESOURCE_DIRECTORY
));
387 for (i
=0;i
<resdir
->NumberOfNamedEntries
+resdir
->NumberOfIdEntries
;i
++) {
388 ret
= lpfun(hmod
,name
,type
,et
[i
].u1
.Id
,lparam
);