Fixed duplicate initialization of some of the wm->ldr fields.
[wine/wine-kai.git] / loader / resource.c
blob5b8f9a1d3a0704f125741794c4d355fcf5dc1c99
1 /*
2 * Resources
4 * Copyright 1993 Robert J. Amstadt
5 * Copyright 1995 Alexandre Julliard
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <assert.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31 #ifdef HAVE_UNISTD_H
32 # include <unistd.h>
33 #endif
34 #include "windef.h"
35 #include "winbase.h"
36 #include "wine/winbase16.h"
37 #include "wine/exception.h"
38 #include "heap.h"
39 #include "cursoricon.h"
40 #include "module.h"
41 #include "file.h"
42 #include "wine/debug.h"
43 #include "winerror.h"
44 #include "winnls.h"
45 #include "excpt.h"
46 #include "winternl.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(resource);
50 #define HRSRC_MAP_BLOCKSIZE 16
52 /* handle conversions */
53 #define HRSRC_32(h16) ((HRSRC)(ULONG_PTR)(h16))
54 #define HGLOBAL_32(h16) ((HGLOBAL)(ULONG_PTR)(h16))
55 #define HGLOBAL_16(h32) (LOWORD(h32))
56 #define HMODULE_32(h16) ((HMODULE)(ULONG_PTR)(h16))
58 typedef struct _HRSRC_ELEM
60 HRSRC hRsrc;
61 WORD type;
62 } HRSRC_ELEM;
64 typedef struct _HRSRC_MAP
66 int nAlloc;
67 int nUsed;
68 HRSRC_ELEM *elem;
69 } HRSRC_MAP;
71 /**********************************************************************
72 * MapHRsrc32To16
74 static HRSRC MapHRsrc32To16( NE_MODULE *pModule, HRSRC hRsrc32, WORD type )
76 HRSRC_MAP *map = (HRSRC_MAP *)pModule->hRsrcMap;
77 HRSRC_ELEM *newElem;
78 int i;
80 /* On first call, initialize HRSRC map */
81 if ( !map )
83 if ( !(map = (HRSRC_MAP *)HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
84 sizeof(HRSRC_MAP) ) ) )
86 ERR("Cannot allocate HRSRC map\n" );
87 return 0;
89 pModule->hRsrcMap = (LPVOID)map;
92 /* Check whether HRSRC32 already in map */
93 for ( i = 0; i < map->nUsed; i++ )
94 if ( map->elem[i].hRsrc == hRsrc32 )
95 return (HRSRC)(i + 1);
97 /* If no space left, grow table */
98 if ( map->nUsed == map->nAlloc )
100 if ( !(newElem = (HRSRC_ELEM *)HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
101 map->elem,
102 (map->nAlloc + HRSRC_MAP_BLOCKSIZE)
103 * sizeof(HRSRC_ELEM) ) ))
105 ERR("Cannot grow HRSRC map\n" );
106 return 0;
108 map->elem = newElem;
109 map->nAlloc += HRSRC_MAP_BLOCKSIZE;
112 /* Add HRSRC32 to table */
113 map->elem[map->nUsed].hRsrc = hRsrc32;
114 map->elem[map->nUsed].type = type;
115 map->nUsed++;
117 return (HRSRC)map->nUsed;
120 /**********************************************************************
121 * MapHRsrc16To32
123 static HRSRC MapHRsrc16To32( NE_MODULE *pModule, HRSRC hRsrc16 )
125 HRSRC_MAP *map = (HRSRC_MAP *)pModule->hRsrcMap;
126 if ( !map || !hRsrc16 || (int)hRsrc16 > map->nUsed ) return 0;
128 return map->elem[(int)hRsrc16-1].hRsrc;
131 /**********************************************************************
132 * MapHRsrc16ToType
134 static WORD MapHRsrc16ToType( NE_MODULE *pModule, HRSRC hRsrc16 )
136 HRSRC_MAP *map = (HRSRC_MAP *)pModule->hRsrcMap;
137 if ( !map || !hRsrc16 || (int)hRsrc16 > map->nUsed ) return 0;
139 return map->elem[(int)hRsrc16-1].type;
143 /* filter for page-fault exceptions */
144 static WINE_EXCEPTION_FILTER(page_fault)
146 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
147 return EXCEPTION_EXECUTE_HANDLER;
148 return EXCEPTION_CONTINUE_SEARCH;
151 static HRSRC RES_FindResource2( HMODULE hModule, LPCSTR type,
152 LPCSTR name, WORD lang,
153 BOOL bUnicode, BOOL bRet16 )
155 HRSRC hRsrc = 0;
157 TRACE("(%p, %08x%s, %08x%s, %04x, %s, %s)\n",
158 hModule,
159 (UINT)type, HIWORD(type)? (bUnicode? debugstr_w((LPWSTR)type) : debugstr_a(type)) : "",
160 (UINT)name, HIWORD(name)? (bUnicode? debugstr_w((LPWSTR)name) : debugstr_a(name)) : "",
161 lang,
162 bUnicode? "W" : "A",
163 bRet16? "NE" : "PE" );
165 if (!HIWORD(hModule))
167 HMODULE16 hMod16 = MapHModuleLS( hModule );
168 NE_MODULE *pModule = NE_GetPtr( hMod16 );
169 if (!pModule) return 0;
170 if (!pModule->module32)
172 /* 16-bit NE module */
173 LPSTR typeStr, nameStr;
175 if ( HIWORD( type ) && bUnicode )
176 typeStr = HEAP_strdupWtoA( GetProcessHeap(), 0, (LPCWSTR)type );
177 else
178 typeStr = (LPSTR)type;
179 if ( HIWORD( name ) && bUnicode )
180 nameStr = HEAP_strdupWtoA( GetProcessHeap(), 0, (LPCWSTR)name );
181 else
182 nameStr = (LPSTR)name;
184 hRsrc = NE_FindResource( pModule, nameStr, typeStr );
186 if ( HIWORD( type ) && bUnicode )
187 HeapFree( GetProcessHeap(), 0, typeStr );
188 if ( HIWORD( name ) && bUnicode )
189 HeapFree( GetProcessHeap(), 0, nameStr );
191 /* If we need to return 32-bit HRSRC, no conversion is necessary,
192 we simply use the 16-bit HRSRC as 32-bit HRSRC */
194 else
196 /* 32-bit PE module */
197 hRsrc = RES_FindResource2( pModule->module32, type, name, lang, bUnicode, FALSE );
198 /* If we need to return 16-bit HRSRC, perform conversion */
199 if ( bRet16 )
200 hRsrc = MapHRsrc32To16( pModule, hRsrc,
201 HIWORD( type )? 0 : LOWORD( type ) );
204 else
206 /* 32-bit PE module */
207 LPWSTR typeStr, nameStr;
209 if ( HIWORD( type ) && !bUnicode )
211 UNICODE_STRING usBuffer;
212 RtlCreateUnicodeStringFromAsciiz(&usBuffer,type);
213 typeStr = usBuffer.Buffer;
215 else
216 typeStr = (LPWSTR)type;
217 if ( HIWORD( name ) && !bUnicode )
219 UNICODE_STRING usBuffer;
220 RtlCreateUnicodeStringFromAsciiz(&usBuffer,name);
221 nameStr = usBuffer.Buffer;
223 else
224 nameStr = (LPWSTR)name;
226 /* Here is the real difference between FindResouce and FindResourceEx */
227 if(lang == MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL) ||
228 lang == MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT) ||
229 lang == MAKELANGID(LANG_NEUTRAL, SUBLANG_SYS_DEFAULT) ||
230 lang == MAKELANGID(LANG_NEUTRAL, 3)) /* FIXME: real name? */
231 hRsrc = PE_FindResourceW( hModule, nameStr, typeStr );
232 else
233 hRsrc = PE_FindResourceExW( hModule, nameStr, typeStr, lang );
235 if ( HIWORD( type ) && !bUnicode )
236 HeapFree( GetProcessHeap(), 0, typeStr );
237 if ( HIWORD( name ) && !bUnicode )
238 HeapFree( GetProcessHeap(), 0, nameStr );
240 return hRsrc;
243 /**********************************************************************
244 * RES_FindResource
247 static HRSRC RES_FindResource( HMODULE hModule, LPCSTR type,
248 LPCSTR name, WORD lang,
249 BOOL bUnicode, BOOL bRet16 )
251 HRSRC hRsrc;
252 __TRY
254 hRsrc = RES_FindResource2(hModule, type, name, lang, bUnicode, bRet16);
256 __EXCEPT(page_fault)
258 WARN("page fault\n");
259 SetLastError(ERROR_INVALID_PARAMETER);
260 return 0;
262 __ENDTRY
263 return hRsrc;
266 /**********************************************************************
267 * RES_SizeofResource
269 static DWORD RES_SizeofResource( HMODULE hModule, HRSRC hRsrc, BOOL bRet16 )
271 if (!hRsrc) return 0;
273 TRACE("(%p, %p, %s)\n", hModule, hRsrc, bRet16? "NE" : "PE" );
275 if (!HIWORD(hModule))
277 HMODULE16 hMod16 = MapHModuleLS( hModule );
278 NE_MODULE *pModule = NE_GetPtr( hMod16 );
279 if (!pModule) return 0;
281 if (!pModule->module32) /* 16-bit NE module */
283 /* If we got a 32-bit hRsrc, we don't need to convert it */
284 return NE_SizeofResource( pModule, hRsrc );
287 /* If we got a 16-bit hRsrc, convert it */
288 if (!HIWORD(hRsrc)) hRsrc = MapHRsrc16To32( pModule, hRsrc );
291 /* 32-bit PE module */
292 return PE_SizeofResource( hRsrc );
295 /**********************************************************************
296 * RES_LoadResource
298 static HGLOBAL RES_LoadResource( HMODULE hModule, HRSRC hRsrc, BOOL bRet16 )
300 HGLOBAL hMem = 0;
302 TRACE("(%p, %p, %s)\n", hModule, hRsrc, bRet16? "NE" : "PE" );
304 if (!hRsrc) return 0;
306 if (!HIWORD(hModule))
308 HMODULE16 hMod16 = MapHModuleLS( hModule );
309 NE_MODULE *pModule = NE_GetPtr( hMod16 );
310 if (!pModule) return 0;
311 if (!pModule->module32)
313 /* 16-bit NE module */
315 /* If we got a 32-bit hRsrc, we don't need to convert it */
316 hMem = HGLOBAL_32(NE_LoadResource( pModule, LOWORD(hRsrc) ));
318 /* If we are to return a 32-bit resource, we should probably
319 convert it but we don't for now. FIXME !!! */
320 return hMem;
322 else
324 /* If we got a 16-bit hRsrc, convert it */
325 HRSRC hRsrc32 = HIWORD(hRsrc)? hRsrc : MapHRsrc16To32( pModule, hRsrc );
327 hMem = PE_LoadResource( pModule->module32, hRsrc32 );
329 /* If we need to return a 16-bit resource, convert it */
330 if ( bRet16 )
332 WORD type = MapHRsrc16ToType( pModule, hRsrc );
333 DWORD size = SizeofResource( hModule, hRsrc );
334 LPVOID bits = LockResource( hMem );
336 hMem = HGLOBAL_32(NE_LoadPEResource( pModule, type, bits, size ));
340 else
342 /* 32-bit PE module */
343 hMem = PE_LoadResource( hModule, hRsrc );
346 return hMem;
349 /**********************************************************************
350 * FindResource (KERNEL.60)
352 HRSRC16 WINAPI FindResource16( HMODULE16 hModule, LPCSTR name, LPCSTR type )
354 return LOWORD( RES_FindResource( HMODULE_32(hModule), type, name,
355 MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), FALSE, TRUE ) );
358 /**********************************************************************
359 * FindResourceA (KERNEL32.@)
361 HRSRC WINAPI FindResourceA( HMODULE hModule, LPCSTR name, LPCSTR type )
363 return RES_FindResource( hModule, type, name,
364 MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), FALSE, FALSE );
367 /**********************************************************************
368 * FindResourceExA (KERNEL32.@)
370 HRSRC WINAPI FindResourceExA( HMODULE hModule,
371 LPCSTR type, LPCSTR name, WORD lang )
373 return RES_FindResource( hModule, type, name,
374 lang, FALSE, FALSE );
377 /**********************************************************************
378 * FindResourceExW (KERNEL32.@)
380 HRSRC WINAPI FindResourceExW( HMODULE hModule,
381 LPCWSTR type, LPCWSTR name, WORD lang )
383 return RES_FindResource( hModule, (LPCSTR)type, (LPCSTR)name,
384 lang, TRUE, FALSE );
387 /**********************************************************************
388 * FindResourceW (KERNEL32.@)
390 HRSRC WINAPI FindResourceW(HINSTANCE hModule, LPCWSTR name, LPCWSTR type)
392 return RES_FindResource( hModule, (LPCSTR)type, (LPCSTR)name,
393 MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), TRUE, FALSE );
396 /**********************************************************************
397 * LoadResource (KERNEL.61)
399 HGLOBAL16 WINAPI LoadResource16( HMODULE16 hModule, HRSRC16 hRsrc )
401 return HGLOBAL_16(RES_LoadResource( HMODULE_32(hModule), HRSRC_32(hRsrc), TRUE ));
404 /**********************************************************************
405 * LoadResource (KERNEL32.@)
407 HGLOBAL WINAPI LoadResource( HINSTANCE hModule, HRSRC hRsrc )
409 return RES_LoadResource( hModule, hRsrc, FALSE );
412 /**********************************************************************
413 * LockResource (KERNEL.62)
415 SEGPTR WINAPI WIN16_LockResource16( HGLOBAL16 handle )
417 TRACE("(%04x)\n", handle );
418 /* May need to reload the resource if discarded */
419 return K32WOWGlobalLock16( handle );
422 /**********************************************************************
423 * LockResource16 (KERNEL32.@)
425 LPVOID WINAPI LockResource16( HGLOBAL16 handle )
427 return MapSL( WIN16_LockResource16(handle) );
430 /**********************************************************************
431 * LockResource (KERNEL32.@)
433 LPVOID WINAPI LockResource( HGLOBAL handle )
435 TRACE("(%p)\n", handle );
437 if (HIWORD( handle )) /* 32-bit memory handle */
438 return (LPVOID)handle;
440 /* 16-bit memory handle */
441 return LockResource16( LOWORD(handle) );
444 typedef WORD (WINAPI *pDestroyIcon32Proc)( HGLOBAL16 handle, UINT16 flags );
447 /**********************************************************************
448 * FreeResource (KERNEL.63)
450 BOOL16 WINAPI FreeResource16( HGLOBAL16 handle )
452 HGLOBAL16 retv = handle;
453 NE_MODULE *pModule = NE_GetPtr( FarGetOwner16( handle ) );
455 TRACE("(%04x)\n", handle );
457 /* Try NE resource first */
458 retv = NE_FreeResource( pModule, handle );
460 /* If this failed, call USER.DestroyIcon32; this will check
461 whether it is a shared cursor/icon; if not it will call
462 GlobalFree16() */
463 if ( retv )
465 pDestroyIcon32Proc proc;
466 HMODULE user = GetModuleHandleA( "user32.dll" );
468 if (user && (proc = (pDestroyIcon32Proc)GetProcAddress( user, "DestroyIcon32" )))
469 retv = proc( handle, CID_RESOURCE );
470 else
471 retv = GlobalFree16( handle );
473 return (BOOL)retv;
476 /**********************************************************************
477 * FreeResource (KERNEL32.@)
479 BOOL WINAPI FreeResource( HGLOBAL handle )
481 if (HIWORD(handle)) return 0; /* 32-bit memory handle: nothing to do */
483 return FreeResource16( LOWORD(handle) );
486 /**********************************************************************
487 * SizeofResource (KERNEL.65)
489 DWORD WINAPI SizeofResource16( HMODULE16 hModule, HRSRC16 hRsrc )
491 return RES_SizeofResource( HMODULE_32(hModule), HRSRC_32(hRsrc), TRUE );
494 /**********************************************************************
495 * SizeofResource (KERNEL32.@)
497 DWORD WINAPI SizeofResource( HINSTANCE hModule, HRSRC hRsrc )
499 return RES_SizeofResource( hModule, hRsrc, FALSE );