Superflous WINAPI removed.
[wine/multimedia.git] / loader / pe_resource.c
blobe87f665af2bf24fc2e7877fe2b323e36c5c0cbd2
1 /*
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
13 #include <stdlib.h>
14 #include <sys/types.h>
15 #include "wintypes.h"
16 #include "windows.h"
17 #include "pe_image.h"
18 #include "module.h"
19 #include "heap.h"
20 #include "task.h"
21 #include "process.h"
22 #include "libres.h"
23 #include "stackframe.h"
24 #include "neexe.h"
25 #include "debug.h"
27 /**********************************************************************
28 * HMODULE32toPE_MODREF
30 * small helper function to get a PE_MODREF from a passed HMODULE32
32 static PE_MODREF*
33 HMODULE32toPE_MODREF(HMODULE32 hmod) {
34 WINE_MODREF *wm;
35 PDB32 *pdb = PROCESS_Current();
37 wm = MODULE32_LookupHMODULE( pdb, hmod );
38 if (!wm || wm->type!=MODULE32_PE)
39 return NULL;
40 return &(wm->binfmt.pe);
43 /**********************************************************************
44 * GetResDirEntryW
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,
51 BOOL32 allowdefault)
53 int entrynum;
54 PIMAGE_RESOURCE_DIRECTORY_ENTRY entryTable;
55 int namelen;
57 if (HIWORD(name)) {
58 if (name[0]=='#') {
59 char buf[10];
61 lstrcpynWtoA(buf,name+1,10);
62 return GetResDirEntryW(resdirptr,(LPCWSTR)atoi(buf),root,allowdefault);
64 entryTable = (PIMAGE_RESOURCE_DIRECTORY_ENTRY) (
65 (BYTE *) resdirptr +
66 sizeof(IMAGE_RESOURCE_DIRECTORY));
67 namelen = lstrlen32W(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)
74 continue;
75 if(lstrncmpi32W(name,str->NameString,str->Length)==0)
76 return (PIMAGE_RESOURCE_DIRECTORY) (
77 root +
78 entryTable[entrynum].u2.s.OffsetToDirectory);
80 return NULL;
81 } else {
82 entryTable = (PIMAGE_RESOURCE_DIRECTORY_ENTRY) (
83 (BYTE *) resdirptr +
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) (
89 root +
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) (
94 root +
95 entryTable[0].u2.s.OffsetToDirectory);
96 return NULL;
100 /**********************************************************************
101 * PE_FindResourceEx32W
103 HANDLE32 PE_FindResourceEx32W(
104 WINE_MODREF *wm,LPCWSTR name,LPCWSTR type,WORD lang
106 PIMAGE_RESOURCE_DIRECTORY resdirptr;
107 DWORD root;
108 HANDLE32 result;
109 PE_MODREF *pem = &(wm->binfmt.pe);
111 if (!pem || !pem->pe_resource)
112 return 0;
114 resdirptr = pem->pe_resource;
115 root = (DWORD) resdirptr;
116 if ((resdirptr = GetResDirEntryW(resdirptr, type, root, FALSE)) == NULL)
117 return 0;
118 if ((resdirptr = GetResDirEntryW(resdirptr, name, root, FALSE)) == NULL)
119 return 0;
120 result = (HANDLE32)GetResDirEntryW(resdirptr, (LPCWSTR)(UINT32)lang, root, FALSE);
121 /* Try LANG_NEUTRAL, too */
122 if(!result)
123 return (HANDLE32)GetResDirEntryW(resdirptr, (LPCWSTR)0, root, TRUE);
124 return result;
128 /**********************************************************************
129 * PE_LoadResource32
131 HANDLE32 PE_LoadResource32( WINE_MODREF *wm, HANDLE32 hRsrc )
133 if (!hRsrc || !wm || wm->type!=MODULE32_PE)
134 return 0;
135 return (HANDLE32) (wm->module + ((PIMAGE_RESOURCE_DATA_ENTRY)hRsrc)->OffsetToData);
139 /**********************************************************************
140 * PE_SizeofResource32
142 DWORD PE_SizeofResource32( HINSTANCE32 hModule, HANDLE32 hRsrc )
144 /* we don't need hModule */
145 if (!hRsrc)
146 return 0;
147 return ((PIMAGE_RESOURCE_DATA_ENTRY)hRsrc)->Size;
150 /**********************************************************************
151 * PE_EnumResourceTypes32A
153 BOOL32
154 PE_EnumResourceTypes32A(HMODULE32 hmod,ENUMRESTYPEPROC32A lpfun,LONG lparam) {
155 PE_MODREF *pem = HMODULE32toPE_MODREF(hmod);
156 int i;
157 PIMAGE_RESOURCE_DIRECTORY resdir;
158 PIMAGE_RESOURCE_DIRECTORY_ENTRY et;
159 BOOL32 ret;
160 HANDLE32 heap = GetProcessHeap();
162 if (!pem || !pem->pe_resource)
163 return FALSE;
165 resdir = (PIMAGE_RESOURCE_DIRECTORY)pem->pe_resource;
166 et =(PIMAGE_RESOURCE_DIRECTORY_ENTRY)((LPBYTE)resdir+sizeof(IMAGE_RESOURCE_DIRECTORY));
167 ret = FALSE;
168 for (i=0;i<resdir->NumberOfNamedEntries+resdir->NumberOfIdEntries;i++) {
169 LPSTR name;
171 if (HIWORD(et[i].u1.Name))
172 name = HEAP_strdupWtoA(heap,0,(LPWSTR)((LPBYTE)pem->pe_resource+et[i].u1.Name));
173 else
174 name = (LPSTR)et[i].u1.Name;
175 ret = lpfun(hmod,name,lparam);
176 if (HIWORD(name))
177 HeapFree(heap,0,name);
178 if (!ret)
179 break;
181 return ret;
184 /**********************************************************************
185 * PE_EnumResourceTypes32W
187 BOOL32
188 PE_EnumResourceTypes32W(HMODULE32 hmod,ENUMRESTYPEPROC32W lpfun,LONG lparam) {
189 PE_MODREF *pem = HMODULE32toPE_MODREF(hmod);
190 int i;
191 PIMAGE_RESOURCE_DIRECTORY resdir;
192 PIMAGE_RESOURCE_DIRECTORY_ENTRY et;
193 BOOL32 ret;
195 if (!pem || !pem->pe_resource)
196 return FALSE;
198 resdir = (PIMAGE_RESOURCE_DIRECTORY)pem->pe_resource;
199 et =(PIMAGE_RESOURCE_DIRECTORY_ENTRY)((LPBYTE)resdir+sizeof(IMAGE_RESOURCE_DIRECTORY));
200 ret = FALSE;
201 for (i=0;i<resdir->NumberOfNamedEntries+resdir->NumberOfIdEntries;i++) {
202 LPWSTR type;
203 if (HIWORD(et[i].u1.Name))
204 type = (LPWSTR)((LPBYTE)pem->pe_resource+et[i].u1.Name);
205 else
206 type = (LPWSTR)et[i].u1.Name;
208 ret = lpfun(hmod,type,lparam);
209 if (!ret)
210 break;
212 return ret;
215 /**********************************************************************
216 * PE_EnumResourceNames32A
218 BOOL32
219 PE_EnumResourceNames32A(
220 HMODULE32 hmod,LPCSTR type,ENUMRESNAMEPROC32A lpfun,LONG lparam
222 PE_MODREF *pem = HMODULE32toPE_MODREF(hmod);
223 int i;
224 PIMAGE_RESOURCE_DIRECTORY resdir;
225 PIMAGE_RESOURCE_DIRECTORY_ENTRY et;
226 BOOL32 ret;
227 HANDLE32 heap = GetProcessHeap();
228 LPWSTR typeW;
230 if (!pem || !pem->pe_resource)
231 return FALSE;
232 resdir = (PIMAGE_RESOURCE_DIRECTORY)pem->pe_resource;
233 if (HIWORD(type))
234 typeW = HEAP_strdupAtoW(heap,0,type);
235 else
236 typeW = (LPWSTR)type;
237 resdir = GetResDirEntryW(resdir,typeW,(DWORD)pem->pe_resource,FALSE);
238 if (HIWORD(typeW))
239 HeapFree(heap,0,typeW);
240 if (!resdir)
241 return FALSE;
242 et =(PIMAGE_RESOURCE_DIRECTORY_ENTRY)((LPBYTE)resdir+sizeof(IMAGE_RESOURCE_DIRECTORY));
243 ret = FALSE;
244 for (i=0;i<resdir->NumberOfNamedEntries+resdir->NumberOfIdEntries;i++) {
245 LPSTR name;
247 if (HIWORD(et[i].u1.Name))
248 name = HEAP_strdupWtoA(heap,0,(LPWSTR)((LPBYTE)pem->pe_resource+et[i].u1.Name));
249 else
250 name = (LPSTR)et[i].u1.Name;
251 ret = lpfun(hmod,type,name,lparam);
252 if (HIWORD(name)) HeapFree(heap,0,name);
253 if (!ret)
254 break;
256 return ret;
259 /**********************************************************************
260 * PE_EnumResourceNames32W
262 BOOL32
263 PE_EnumResourceNames32W(
264 HMODULE32 hmod,LPCWSTR type,ENUMRESNAMEPROC32W lpfun,LONG lparam
266 PE_MODREF *pem = HMODULE32toPE_MODREF(hmod);
267 int i;
268 PIMAGE_RESOURCE_DIRECTORY resdir;
269 PIMAGE_RESOURCE_DIRECTORY_ENTRY et;
270 BOOL32 ret;
272 if (!pem || !pem->pe_resource)
273 return FALSE;
275 resdir = (PIMAGE_RESOURCE_DIRECTORY)pem->pe_resource;
276 resdir = GetResDirEntryW(resdir,type,(DWORD)pem->pe_resource,FALSE);
277 if (!resdir)
278 return FALSE;
279 et =(PIMAGE_RESOURCE_DIRECTORY_ENTRY)((LPBYTE)resdir+sizeof(IMAGE_RESOURCE_DIRECTORY));
280 ret = FALSE;
281 for (i=0;i<resdir->NumberOfNamedEntries+resdir->NumberOfIdEntries;i++) {
282 LPWSTR name;
283 if (HIWORD(et[i].u1.Name))
284 name = (LPWSTR)((LPBYTE)pem->pe_resource+et[i].u1.Name);
285 else
286 name = (LPWSTR)et[i].u1.Name;
287 ret = lpfun(hmod,type,name,lparam);
288 if (!ret)
289 break;
291 return ret;
294 /**********************************************************************
295 * PE_EnumResourceNames32A
297 BOOL32
298 PE_EnumResourceLanguages32A(
299 HMODULE32 hmod,LPCSTR name,LPCSTR type,ENUMRESLANGPROC32A lpfun,
300 LONG lparam
302 PE_MODREF *pem = HMODULE32toPE_MODREF(hmod);
303 int i;
304 PIMAGE_RESOURCE_DIRECTORY resdir;
305 PIMAGE_RESOURCE_DIRECTORY_ENTRY et;
306 BOOL32 ret;
307 HANDLE32 heap = GetProcessHeap();
308 LPWSTR nameW,typeW;
310 if (!pem || !pem->pe_resource)
311 return FALSE;
313 resdir = (PIMAGE_RESOURCE_DIRECTORY)pem->pe_resource;
314 if (HIWORD(name))
315 nameW = HEAP_strdupAtoW(heap,0,name);
316 else
317 nameW = (LPWSTR)name;
318 resdir = GetResDirEntryW(resdir,nameW,(DWORD)pem->pe_resource,FALSE);
319 if (HIWORD(nameW))
320 HeapFree(heap,0,nameW);
321 if (!resdir)
322 return FALSE;
323 if (HIWORD(type))
324 typeW = HEAP_strdupAtoW(heap,0,type);
325 else
326 typeW = (LPWSTR)type;
327 resdir = GetResDirEntryW(resdir,typeW,(DWORD)pem->pe_resource,FALSE);
328 if (HIWORD(typeW))
329 HeapFree(heap,0,typeW);
330 if (!resdir)
331 return FALSE;
332 et =(PIMAGE_RESOURCE_DIRECTORY_ENTRY)((LPBYTE)resdir+sizeof(IMAGE_RESOURCE_DIRECTORY));
333 ret = FALSE;
334 for (i=0;i<resdir->NumberOfNamedEntries+resdir->NumberOfIdEntries;i++) {
335 /* languages are just ids... I hopem */
336 ret = lpfun(hmod,name,type,et[i].u1.Id,lparam);
337 if (!ret)
338 break;
340 return ret;
343 /**********************************************************************
344 * PE_EnumResourceLanguages32W
346 BOOL32
347 PE_EnumResourceLanguages32W(
348 HMODULE32 hmod,LPCWSTR name,LPCWSTR type,ENUMRESLANGPROC32W lpfun,
349 LONG lparam
351 PE_MODREF *pem = HMODULE32toPE_MODREF(hmod);
352 int i;
353 PIMAGE_RESOURCE_DIRECTORY resdir;
354 PIMAGE_RESOURCE_DIRECTORY_ENTRY et;
355 BOOL32 ret;
357 if (!pem || !pem->pe_resource)
358 return FALSE;
360 resdir = (PIMAGE_RESOURCE_DIRECTORY)pem->pe_resource;
361 resdir = GetResDirEntryW(resdir,name,(DWORD)pem->pe_resource,FALSE);
362 if (!resdir)
363 return FALSE;
364 resdir = GetResDirEntryW(resdir,type,(DWORD)pem->pe_resource,FALSE);
365 if (!resdir)
366 return FALSE;
367 et =(PIMAGE_RESOURCE_DIRECTORY_ENTRY)((LPBYTE)resdir+sizeof(IMAGE_RESOURCE_DIRECTORY));
368 ret = FALSE;
369 for (i=0;i<resdir->NumberOfNamedEntries+resdir->NumberOfIdEntries;i++) {
370 ret = lpfun(hmod,name,type,et[i].u1.Id,lparam);
371 if (!ret)
372 break;
374 return ret;