Release 20020904.
[wine/gsoc-2012-control.git] / loader / resource.c
blobf16d5d357f95e6ae4417fe2c89f0cb0d81680ad0
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 "msvcrt/excpt.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(resource);
49 #define HRSRC_MAP_BLOCKSIZE 16
51 typedef struct _HRSRC_ELEM
53 HANDLE hRsrc;
54 WORD type;
55 } HRSRC_ELEM;
57 typedef struct _HRSRC_MAP
59 int nAlloc;
60 int nUsed;
61 HRSRC_ELEM *elem;
62 } HRSRC_MAP;
64 /**********************************************************************
65 * MapHRsrc32To16
67 static HRSRC MapHRsrc32To16( NE_MODULE *pModule, HANDLE hRsrc32, WORD type )
69 HRSRC_MAP *map = (HRSRC_MAP *)pModule->hRsrcMap;
70 HRSRC_ELEM *newElem;
71 int i;
73 /* On first call, initialize HRSRC map */
74 if ( !map )
76 if ( !(map = (HRSRC_MAP *)HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
77 sizeof(HRSRC_MAP) ) ) )
79 ERR("Cannot allocate HRSRC map\n" );
80 return 0;
82 pModule->hRsrcMap = (LPVOID)map;
85 /* Check whether HRSRC32 already in map */
86 for ( i = 0; i < map->nUsed; i++ )
87 if ( map->elem[i].hRsrc == hRsrc32 )
88 return (HRSRC)(i + 1);
90 /* If no space left, grow table */
91 if ( map->nUsed == map->nAlloc )
93 if ( !(newElem = (HRSRC_ELEM *)HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
94 map->elem,
95 (map->nAlloc + HRSRC_MAP_BLOCKSIZE)
96 * sizeof(HRSRC_ELEM) ) ))
98 ERR("Cannot grow HRSRC map\n" );
99 return 0;
101 map->elem = newElem;
102 map->nAlloc += HRSRC_MAP_BLOCKSIZE;
105 /* Add HRSRC32 to table */
106 map->elem[map->nUsed].hRsrc = hRsrc32;
107 map->elem[map->nUsed].type = type;
108 map->nUsed++;
110 return (HRSRC)map->nUsed;
113 /**********************************************************************
114 * MapHRsrc16To32
116 static HRSRC MapHRsrc16To32( NE_MODULE *pModule, HRSRC hRsrc16 )
118 HRSRC_MAP *map = (HRSRC_MAP *)pModule->hRsrcMap;
119 if ( !map || !hRsrc16 || (int)hRsrc16 > map->nUsed ) return 0;
121 return map->elem[(int)hRsrc16-1].hRsrc;
124 /**********************************************************************
125 * MapHRsrc16ToType
127 static WORD MapHRsrc16ToType( NE_MODULE *pModule, HRSRC hRsrc16 )
129 HRSRC_MAP *map = (HRSRC_MAP *)pModule->hRsrcMap;
130 if ( !map || !hRsrc16 || (int)hRsrc16 > map->nUsed ) return 0;
132 return map->elem[(int)hRsrc16-1].type;
136 /* filter for page-fault exceptions */
137 static WINE_EXCEPTION_FILTER(page_fault)
139 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
140 return EXCEPTION_EXECUTE_HANDLER;
141 return EXCEPTION_CONTINUE_SEARCH;
144 static HRSRC RES_FindResource2( HMODULE hModule, LPCSTR type,
145 LPCSTR name, WORD lang,
146 BOOL bUnicode, BOOL bRet16 )
148 HRSRC hRsrc = 0;
150 TRACE("(%08x, %08x%s, %08x%s, %04x, %s, %s)\n",
151 hModule,
152 (UINT)type, HIWORD(type)? (bUnicode? debugstr_w((LPWSTR)type) : debugstr_a(type)) : "",
153 (UINT)name, HIWORD(name)? (bUnicode? debugstr_w((LPWSTR)name) : debugstr_a(name)) : "",
154 lang,
155 bUnicode? "W" : "A",
156 bRet16? "NE" : "PE" );
158 if (!HIWORD(hModule))
160 HMODULE16 hMod16 = MapHModuleLS( hModule );
161 NE_MODULE *pModule = NE_GetPtr( hMod16 );
162 if (!pModule) return 0;
163 if (!pModule->module32)
165 /* 16-bit NE module */
166 LPSTR typeStr, nameStr;
168 if ( HIWORD( type ) && bUnicode )
169 typeStr = HEAP_strdupWtoA( GetProcessHeap(), 0, (LPCWSTR)type );
170 else
171 typeStr = (LPSTR)type;
172 if ( HIWORD( name ) && bUnicode )
173 nameStr = HEAP_strdupWtoA( GetProcessHeap(), 0, (LPCWSTR)name );
174 else
175 nameStr = (LPSTR)name;
177 hRsrc = NE_FindResource( pModule, nameStr, typeStr );
179 if ( HIWORD( type ) && bUnicode )
180 HeapFree( GetProcessHeap(), 0, typeStr );
181 if ( HIWORD( name ) && bUnicode )
182 HeapFree( GetProcessHeap(), 0, nameStr );
184 /* If we need to return 32-bit HRSRC, no conversion is necessary,
185 we simply use the 16-bit HRSRC as 32-bit HRSRC */
187 else
189 /* 32-bit PE module */
190 hRsrc = RES_FindResource2( pModule->module32, type, name, lang, bUnicode, FALSE );
191 /* If we need to return 16-bit HRSRC, perform conversion */
192 if ( bRet16 )
193 hRsrc = MapHRsrc32To16( pModule, hRsrc,
194 HIWORD( type )? 0 : LOWORD( type ) );
197 else
199 /* 32-bit PE module */
200 LPWSTR typeStr, nameStr;
202 if ( HIWORD( type ) && !bUnicode )
203 typeStr = HEAP_strdupAtoW( GetProcessHeap(), 0, type );
204 else
205 typeStr = (LPWSTR)type;
206 if ( HIWORD( name ) && !bUnicode )
207 nameStr = HEAP_strdupAtoW( GetProcessHeap(), 0, name );
208 else
209 nameStr = (LPWSTR)name;
211 /* Here is the real difference between FindResouce and FindResourceEx */
212 if(lang == MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL) ||
213 lang == MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT) ||
214 lang == MAKELANGID(LANG_NEUTRAL, SUBLANG_SYS_DEFAULT) ||
215 lang == MAKELANGID(LANG_NEUTRAL, 3)) /* FIXME: real name? */
216 hRsrc = PE_FindResourceW( hModule, nameStr, typeStr );
217 else
218 hRsrc = PE_FindResourceExW( hModule, nameStr, typeStr, lang );
220 if ( HIWORD( type ) && !bUnicode )
221 HeapFree( GetProcessHeap(), 0, typeStr );
222 if ( HIWORD( name ) && !bUnicode )
223 HeapFree( GetProcessHeap(), 0, nameStr );
225 return hRsrc;
228 /**********************************************************************
229 * RES_FindResource
232 static HRSRC RES_FindResource( HMODULE hModule, LPCSTR type,
233 LPCSTR name, WORD lang,
234 BOOL bUnicode, BOOL bRet16 )
236 HRSRC hRsrc;
237 __TRY
239 hRsrc = RES_FindResource2(hModule, type, name, lang, bUnicode, bRet16);
241 __EXCEPT(page_fault)
243 WARN("page fault\n");
244 SetLastError(ERROR_INVALID_PARAMETER);
245 return 0;
247 __ENDTRY
248 return hRsrc;
251 /**********************************************************************
252 * RES_SizeofResource
254 static DWORD RES_SizeofResource( HMODULE hModule, HRSRC hRsrc, BOOL bRet16 )
256 if (!hRsrc) return 0;
258 TRACE("(%08x, %08x, %s)\n", hModule, hRsrc, bRet16? "NE" : "PE" );
260 if (!HIWORD(hModule))
262 HMODULE16 hMod16 = MapHModuleLS( hModule );
263 NE_MODULE *pModule = NE_GetPtr( hMod16 );
264 if (!pModule) return 0;
266 if (!pModule->module32) /* 16-bit NE module */
268 /* If we got a 32-bit hRsrc, we don't need to convert it */
269 return NE_SizeofResource( pModule, hRsrc );
272 /* If we got a 16-bit hRsrc, convert it */
273 if (!HIWORD(hRsrc)) hRsrc = MapHRsrc16To32( pModule, hRsrc );
276 /* 32-bit PE module */
277 return PE_SizeofResource( hRsrc );
280 /**********************************************************************
281 * RES_LoadResource
283 static HGLOBAL RES_LoadResource( HMODULE hModule, HRSRC hRsrc, BOOL bRet16 )
285 HGLOBAL hMem = 0;
287 TRACE("(%08x, %08x, %s)\n", hModule, hRsrc, bRet16? "NE" : "PE" );
289 if (!hRsrc) return 0;
291 if (!HIWORD(hModule))
293 HMODULE16 hMod16 = MapHModuleLS( hModule );
294 NE_MODULE *pModule = NE_GetPtr( hMod16 );
295 if (!pModule) return 0;
296 if (!pModule->module32)
298 /* 16-bit NE module */
300 /* If we got a 32-bit hRsrc, we don't need to convert it */
301 hMem = NE_LoadResource( pModule, LOWORD(hRsrc) );
303 /* If we are to return a 32-bit resource, we should probably
304 convert it but we don't for now. FIXME !!! */
305 return hMem;
307 else
309 /* If we got a 16-bit hRsrc, convert it */
310 HRSRC hRsrc32 = HIWORD(hRsrc)? hRsrc : MapHRsrc16To32( pModule, hRsrc );
312 hMem = PE_LoadResource( pModule->module32, hRsrc32 );
314 /* If we need to return a 16-bit resource, convert it */
315 if ( bRet16 )
317 WORD type = MapHRsrc16ToType( pModule, hRsrc );
318 DWORD size = SizeofResource( hModule, hRsrc );
319 LPVOID bits = LockResource( hMem );
321 hMem = NE_LoadPEResource( pModule, type, bits, size );
325 else
327 /* 32-bit PE module */
328 hMem = PE_LoadResource( hModule, hRsrc );
331 return hMem;
334 /**********************************************************************
335 * FindResource (KERNEL.60)
336 * FindResource16 (KERNEL32.@)
338 HRSRC16 WINAPI FindResource16( HMODULE16 hModule, LPCSTR name, LPCSTR type )
340 return LOWORD( RES_FindResource( hModule, type, name,
341 MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), FALSE, TRUE ) );
344 /**********************************************************************
345 * FindResourceA (KERNEL32.@)
347 HRSRC WINAPI FindResourceA( HMODULE hModule, LPCSTR name, LPCSTR type )
349 return RES_FindResource( hModule, type, name,
350 MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), FALSE, FALSE );
353 /**********************************************************************
354 * FindResourceExA (KERNEL32.@)
356 HRSRC WINAPI FindResourceExA( HMODULE hModule,
357 LPCSTR type, LPCSTR name, WORD lang )
359 return RES_FindResource( hModule, type, name,
360 lang, FALSE, FALSE );
363 /**********************************************************************
364 * FindResourceExW (KERNEL32.@)
366 HRSRC WINAPI FindResourceExW( HMODULE hModule,
367 LPCWSTR type, LPCWSTR name, WORD lang )
369 return RES_FindResource( hModule, (LPCSTR)type, (LPCSTR)name,
370 lang, TRUE, FALSE );
373 /**********************************************************************
374 * FindResourceW (KERNEL32.@)
376 HRSRC WINAPI FindResourceW(HINSTANCE hModule, LPCWSTR name, LPCWSTR type)
378 return RES_FindResource( hModule, (LPCSTR)type, (LPCSTR)name,
379 MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), TRUE, FALSE );
382 /**********************************************************************
383 * LoadResource (KERNEL.61)
384 * LoadResource16 (KERNEL32.@)
386 HGLOBAL16 WINAPI LoadResource16( HMODULE16 hModule, HRSRC16 hRsrc )
388 return RES_LoadResource( hModule, hRsrc, TRUE );
391 /**********************************************************************
392 * LoadResource (KERNEL32.@)
394 HGLOBAL WINAPI LoadResource( HINSTANCE hModule, HRSRC hRsrc )
396 return RES_LoadResource( hModule, hRsrc, FALSE );
399 /**********************************************************************
400 * LockResource (KERNEL.62)
402 SEGPTR WINAPI WIN16_LockResource16( HGLOBAL16 handle )
404 TRACE("(%04x)\n", handle );
405 /* May need to reload the resource if discarded */
406 return K32WOWGlobalLock16( handle );
409 /**********************************************************************
410 * LockResource16 (KERNEL32.@)
412 LPVOID WINAPI LockResource16( HGLOBAL16 handle )
414 return MapSL( WIN16_LockResource16(handle) );
417 /**********************************************************************
418 * LockResource (KERNEL32.@)
420 LPVOID WINAPI LockResource( HGLOBAL handle )
422 TRACE("(%08x)\n", handle );
424 if (HIWORD( handle )) /* 32-bit memory handle */
425 return (LPVOID)handle;
427 /* 16-bit memory handle */
428 return LockResource16( LOWORD(handle) );
431 typedef WORD (WINAPI *pDestroyIcon32Proc)( HGLOBAL16 handle, UINT16 flags );
434 /**********************************************************************
435 * FreeResource (KERNEL.63)
436 * FreeResource16 (KERNEL32.@)
438 BOOL16 WINAPI FreeResource16( HGLOBAL16 handle )
440 HGLOBAL16 retv = handle;
441 NE_MODULE *pModule = NE_GetPtr( FarGetOwner16( handle ) );
443 TRACE("(%04x)\n", handle );
445 /* Try NE resource first */
446 retv = NE_FreeResource( pModule, handle );
448 /* If this failed, call USER.DestroyIcon32; this will check
449 whether it is a shared cursor/icon; if not it will call
450 GlobalFree16() */
451 if ( retv )
453 pDestroyIcon32Proc proc;
454 HMODULE user = GetModuleHandleA( "user32.dll" );
456 if (user && (proc = (pDestroyIcon32Proc)GetProcAddress( user, "DestroyIcon32" )))
457 retv = proc( handle, CID_RESOURCE );
458 else
459 retv = GlobalFree16( handle );
461 return (BOOL)retv;
464 /**********************************************************************
465 * FreeResource (KERNEL32.@)
467 BOOL WINAPI FreeResource( HGLOBAL handle )
469 if (HIWORD(handle)) return 0; /* 32-bit memory handle: nothing to do */
471 return FreeResource16( LOWORD(handle) );
474 /**********************************************************************
475 * SizeofResource (KERNEL.65)
476 * SizeofResource16 (KERNEL32.@)
478 DWORD WINAPI SizeofResource16( HMODULE16 hModule, HRSRC16 hRsrc )
480 return RES_SizeofResource( hModule, hRsrc, TRUE );
483 /**********************************************************************
484 * SizeofResource (KERNEL32.@)
486 DWORD WINAPI SizeofResource( HINSTANCE hModule, HRSRC hRsrc )
488 return RES_SizeofResource( hModule, hRsrc, FALSE );