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>
16 #include <wine/windef.h>
17 #include <wine/pe_image.h>
18 #include <wine/module.h>
19 #include <wine/heap.h>
21 //#include "process.h"
22 //#include "stackframe.h"
23 #include <wine/debugtools.h>
25 /**********************************************************************
26 * HMODULE32toPE_MODREF
28 * small helper function to get a PE_MODREF from a passed HMODULE32
31 HMODULE32toPE_MODREF(HMODULE hmod) {
34 wm = MODULE32_LookupHMODULE( hmod );
35 if (!wm || wm->type!=MODULE32_PE)
37 return &(wm->binfmt.pe);
40 /**********************************************************************
43 * Helper function - goes down one level of PE resource tree
46 PIMAGE_RESOURCE_DIRECTORY GetResDirEntryW(PIMAGE_RESOURCE_DIRECTORY resdirptr,
47 LPCWSTR name,DWORD root,
48 WIN_BOOL allowdefault)
51 PIMAGE_RESOURCE_DIRECTORY_ENTRY entryTable;
58 lstrcpynWtoA(buf,name+1,10);
59 return GetResDirEntryW(resdirptr,(LPCWSTR)atoi(buf),root,allowdefault);
61 entryTable = (PIMAGE_RESOURCE_DIRECTORY_ENTRY) (
63 sizeof(IMAGE_RESOURCE_DIRECTORY));
64 namelen = lstrlenW(name);
65 for (entrynum = 0; entrynum < resdirptr->NumberOfNamedEntries; entrynum++)
67 PIMAGE_RESOURCE_DIR_STRING_U str =
68 (PIMAGE_RESOURCE_DIR_STRING_U) (root +
69 entryTable[entrynum].u1.s.NameOffset);
70 if(namelen != str->Length)
72 if(wcsnicmp(name,str->NameString,str->Length)==0)
73 return (PIMAGE_RESOURCE_DIRECTORY) (
75 entryTable[entrynum].u2.s.OffsetToDirectory);
79 entryTable = (PIMAGE_RESOURCE_DIRECTORY_ENTRY) (
81 sizeof(IMAGE_RESOURCE_DIRECTORY) +
82 resdirptr->NumberOfNamedEntries * sizeof(IMAGE_RESOURCE_DIRECTORY_ENTRY));
83 for (entrynum = 0; entrynum < resdirptr->NumberOfIdEntries; entrynum++)
84 if ((DWORD)entryTable[entrynum].u1.Name == (DWORD)name)
85 return (PIMAGE_RESOURCE_DIRECTORY) (
87 entryTable[entrynum].u2.s.OffsetToDirectory);
88 /* just use first entry if no default can be found */
89 if (allowdefault && !name && resdirptr->NumberOfIdEntries)
90 return (PIMAGE_RESOURCE_DIRECTORY) (
92 entryTable[0].u2.s.OffsetToDirectory);
97 /**********************************************************************
100 PIMAGE_RESOURCE_DIRECTORY GetResDirEntryA( PIMAGE_RESOURCE_DIRECTORY resdirptr,
101 LPCSTR name, DWORD root,
102 WIN_BOOL allowdefault )
104 PIMAGE_RESOURCE_DIRECTORY retv;
105 LPWSTR nameW = HIWORD(name)? HEAP_strdupAtoW( GetProcessHeap(), 0, name )
108 retv = GetResDirEntryW( resdirptr, nameW, root, allowdefault );
110 if ( HIWORD(name) ) HeapFree( GetProcessHeap(), 0, nameW );
115 /**********************************************************************
116 * PE_FindResourceEx32W
118 HANDLE PE_FindResourceExW(
119 WINE_MODREF *wm,LPCWSTR name,LPCWSTR type,WORD lang
121 PIMAGE_RESOURCE_DIRECTORY resdirptr;
124 PE_MODREF *pem = &(wm->binfmt.pe);
126 if (!pem || !pem->pe_resource)
129 resdirptr = pem->pe_resource;
130 root = (DWORD) resdirptr;
131 if ((resdirptr = GetResDirEntryW(resdirptr, type, root, FALSE)) == NULL)
133 if ((resdirptr = GetResDirEntryW(resdirptr, name, root, FALSE)) == NULL)
135 result = (HANDLE)GetResDirEntryW(resdirptr, (LPCWSTR)(UINT)lang, root, FALSE);
136 /* Try LANG_NEUTRAL, too */
138 return (HANDLE)GetResDirEntryW(resdirptr, (LPCWSTR)0, root, TRUE);
143 /**********************************************************************
146 HANDLE PE_LoadResource( WINE_MODREF *wm, HANDLE hRsrc )
148 if (!hRsrc || !wm || wm->type!=MODULE32_PE)
150 return (HANDLE) (wm->module + ((PIMAGE_RESOURCE_DATA_ENTRY)hRsrc)->OffsetToData);
154 /**********************************************************************
155 * PE_SizeofResource32
157 DWORD PE_SizeofResource( HINSTANCE hModule, HANDLE hRsrc )
159 /* we don't need hModule */
162 return ((PIMAGE_RESOURCE_DATA_ENTRY)hRsrc)->Size;
165 /**********************************************************************
166 * PE_EnumResourceTypes32A
169 PE_EnumResourceTypesA(HMODULE hmod,ENUMRESTYPEPROCA lpfun,LONG lparam) {
170 PE_MODREF *pem = HMODULE32toPE_MODREF(hmod);
172 PIMAGE_RESOURCE_DIRECTORY resdir;
173 PIMAGE_RESOURCE_DIRECTORY_ENTRY et;
175 HANDLE heap = GetProcessHeap();
177 if (!pem || !pem->pe_resource)
180 resdir = (PIMAGE_RESOURCE_DIRECTORY)pem->pe_resource;
181 et =(PIMAGE_RESOURCE_DIRECTORY_ENTRY)((LPBYTE)resdir+sizeof(IMAGE_RESOURCE_DIRECTORY));
183 for (i=0;i<resdir->NumberOfNamedEntries+resdir->NumberOfIdEntries;i++) {
186 if (et[i].u1.s.NameIsString)
187 name = HEAP_strdupWtoA(heap,0,(LPWSTR)((LPBYTE)pem->pe_resource+et[i].u1.s.NameOffset));
189 name = (LPSTR)(int)et[i].u1.Id;
190 ret = lpfun(hmod,name,lparam);
192 HeapFree(heap,0,name);
199 /**********************************************************************
200 * PE_EnumResourceTypes32W
203 PE_EnumResourceTypesW(HMODULE hmod,ENUMRESTYPEPROCW lpfun,LONG lparam) {
204 PE_MODREF *pem = HMODULE32toPE_MODREF(hmod);
206 PIMAGE_RESOURCE_DIRECTORY resdir;
207 PIMAGE_RESOURCE_DIRECTORY_ENTRY et;
210 if (!pem || !pem->pe_resource)
213 resdir = (PIMAGE_RESOURCE_DIRECTORY)pem->pe_resource;
214 et =(PIMAGE_RESOURCE_DIRECTORY_ENTRY)((LPBYTE)resdir+sizeof(IMAGE_RESOURCE_DIRECTORY));
216 for (i=0;i<resdir->NumberOfNamedEntries+resdir->NumberOfIdEntries;i++) {
218 if (et[i].u1.s.NameIsString)
219 type = (LPWSTR)((LPBYTE)pem->pe_resource+et[i].u1.s.NameOffset);
221 type = (LPWSTR)(int)et[i].u1.Id;
223 ret = lpfun(hmod,type,lparam);
230 /**********************************************************************
231 * PE_EnumResourceNames32A
234 PE_EnumResourceNamesA(
235 HMODULE hmod,LPCSTR type,ENUMRESNAMEPROCA lpfun,LONG lparam
237 PE_MODREF *pem = HMODULE32toPE_MODREF(hmod);
239 PIMAGE_RESOURCE_DIRECTORY resdir;
240 PIMAGE_RESOURCE_DIRECTORY_ENTRY et;
242 HANDLE heap = GetProcessHeap();
245 if (!pem || !pem->pe_resource)
247 resdir = (PIMAGE_RESOURCE_DIRECTORY)pem->pe_resource;
249 typeW = HEAP_strdupAtoW(heap,0,type);
251 typeW = (LPWSTR)type;
252 resdir = GetResDirEntryW(resdir,typeW,(DWORD)pem->pe_resource,FALSE);
254 HeapFree(heap,0,typeW);
257 et =(PIMAGE_RESOURCE_DIRECTORY_ENTRY)((LPBYTE)resdir+sizeof(IMAGE_RESOURCE_DIRECTORY));
259 for (i=0;i<resdir->NumberOfNamedEntries+resdir->NumberOfIdEntries;i++) {
262 if (et[i].u1.s.NameIsString)
263 name = HEAP_strdupWtoA(heap,0,(LPWSTR)((LPBYTE)pem->pe_resource+et[i].u1.s.NameOffset));
265 name = (LPSTR)(int)et[i].u1.Id;
266 ret = lpfun(hmod,type,name,lparam);
267 if (HIWORD(name)) HeapFree(heap,0,name);
274 /**********************************************************************
275 * PE_EnumResourceNames32W
278 PE_EnumResourceNamesW(
279 HMODULE hmod,LPCWSTR type,ENUMRESNAMEPROCW lpfun,LONG lparam
281 PE_MODREF *pem = HMODULE32toPE_MODREF(hmod);
283 PIMAGE_RESOURCE_DIRECTORY resdir;
284 PIMAGE_RESOURCE_DIRECTORY_ENTRY et;
287 if (!pem || !pem->pe_resource)
290 resdir = (PIMAGE_RESOURCE_DIRECTORY)pem->pe_resource;
291 resdir = GetResDirEntryW(resdir,type,(DWORD)pem->pe_resource,FALSE);
294 et =(PIMAGE_RESOURCE_DIRECTORY_ENTRY)((LPBYTE)resdir+sizeof(IMAGE_RESOURCE_DIRECTORY));
296 for (i=0;i<resdir->NumberOfNamedEntries+resdir->NumberOfIdEntries;i++) {
298 if (et[i].u1.s.NameIsString)
299 name = (LPWSTR)((LPBYTE)pem->pe_resource+et[i].u1.s.NameOffset);
301 name = (LPWSTR)(int)et[i].u1.Id;
302 ret = lpfun(hmod,type,name,lparam);
309 /**********************************************************************
310 * PE_EnumResourceNames32A
313 PE_EnumResourceLanguagesA(
314 HMODULE hmod,LPCSTR name,LPCSTR type,ENUMRESLANGPROCA lpfun,
317 PE_MODREF *pem = HMODULE32toPE_MODREF(hmod);
319 PIMAGE_RESOURCE_DIRECTORY resdir;
320 PIMAGE_RESOURCE_DIRECTORY_ENTRY et;
322 HANDLE heap = GetProcessHeap();
325 if (!pem || !pem->pe_resource)
328 resdir = (PIMAGE_RESOURCE_DIRECTORY)pem->pe_resource;
330 nameW = HEAP_strdupAtoW(heap,0,name);
332 nameW = (LPWSTR)name;
333 resdir = GetResDirEntryW(resdir,nameW,(DWORD)pem->pe_resource,FALSE);
335 HeapFree(heap,0,nameW);
339 typeW = HEAP_strdupAtoW(heap,0,type);
341 typeW = (LPWSTR)type;
342 resdir = GetResDirEntryW(resdir,typeW,(DWORD)pem->pe_resource,FALSE);
344 HeapFree(heap,0,typeW);
347 et =(PIMAGE_RESOURCE_DIRECTORY_ENTRY)((LPBYTE)resdir+sizeof(IMAGE_RESOURCE_DIRECTORY));
349 for (i=0;i<resdir->NumberOfNamedEntries+resdir->NumberOfIdEntries;i++) {
350 /* languages are just ids... I hopem */
351 ret = lpfun(hmod,name,type,et[i].u1.Id,lparam);
358 /**********************************************************************
359 * PE_EnumResourceLanguages32W
362 PE_EnumResourceLanguagesW(
363 HMODULE hmod,LPCWSTR name,LPCWSTR type,ENUMRESLANGPROCW lpfun,
366 PE_MODREF *pem = HMODULE32toPE_MODREF(hmod);
368 PIMAGE_RESOURCE_DIRECTORY resdir;
369 PIMAGE_RESOURCE_DIRECTORY_ENTRY et;
372 if (!pem || !pem->pe_resource)
375 resdir = (PIMAGE_RESOURCE_DIRECTORY)pem->pe_resource;
376 resdir = GetResDirEntryW(resdir,name,(DWORD)pem->pe_resource,FALSE);
379 resdir = GetResDirEntryW(resdir,type,(DWORD)pem->pe_resource,FALSE);
382 et =(PIMAGE_RESOURCE_DIRECTORY_ENTRY)((LPBYTE)resdir+sizeof(IMAGE_RESOURCE_DIRECTORY));
384 for (i=0;i<resdir->NumberOfNamedEntries+resdir->NumberOfIdEntries;i++) {
385 ret = lpfun(hmod,name,type,et[i].u1.Id,lparam);