X11DRV_DrawArc: Don't overwrite the ENDCAP style.
[wine/multimedia.git] / loader / resource.c
blob27daad70d25614c82d399ce215b494e3b520fd83
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 /* handle conversions */
52 #define HRSRC_32(h16) ((HRSRC)(ULONG_PTR)(h16))
53 #define HGLOBAL_32(h16) ((HGLOBAL)(ULONG_PTR)(h16))
54 #define HGLOBAL_16(h32) (LOWORD(h32))
55 #define HMODULE_32(h16) ((HMODULE)(ULONG_PTR)(h16))
57 typedef struct _HRSRC_ELEM
59 HRSRC hRsrc;
60 WORD type;
61 } HRSRC_ELEM;
63 typedef struct _HRSRC_MAP
65 int nAlloc;
66 int nUsed;
67 HRSRC_ELEM *elem;
68 } HRSRC_MAP;
70 /**********************************************************************
71 * MapHRsrc32To16
73 static HRSRC MapHRsrc32To16( NE_MODULE *pModule, HRSRC hRsrc32, WORD type )
75 HRSRC_MAP *map = (HRSRC_MAP *)pModule->hRsrcMap;
76 HRSRC_ELEM *newElem;
77 int i;
79 /* On first call, initialize HRSRC map */
80 if ( !map )
82 if ( !(map = (HRSRC_MAP *)HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
83 sizeof(HRSRC_MAP) ) ) )
85 ERR("Cannot allocate HRSRC map\n" );
86 return 0;
88 pModule->hRsrcMap = (LPVOID)map;
91 /* Check whether HRSRC32 already in map */
92 for ( i = 0; i < map->nUsed; i++ )
93 if ( map->elem[i].hRsrc == hRsrc32 )
94 return (HRSRC)(i + 1);
96 /* If no space left, grow table */
97 if ( map->nUsed == map->nAlloc )
99 if ( !(newElem = (HRSRC_ELEM *)HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
100 map->elem,
101 (map->nAlloc + HRSRC_MAP_BLOCKSIZE)
102 * sizeof(HRSRC_ELEM) ) ))
104 ERR("Cannot grow HRSRC map\n" );
105 return 0;
107 map->elem = newElem;
108 map->nAlloc += HRSRC_MAP_BLOCKSIZE;
111 /* Add HRSRC32 to table */
112 map->elem[map->nUsed].hRsrc = hRsrc32;
113 map->elem[map->nUsed].type = type;
114 map->nUsed++;
116 return (HRSRC)map->nUsed;
119 /**********************************************************************
120 * MapHRsrc16To32
122 static HRSRC MapHRsrc16To32( NE_MODULE *pModule, HRSRC hRsrc16 )
124 HRSRC_MAP *map = (HRSRC_MAP *)pModule->hRsrcMap;
125 if ( !map || !hRsrc16 || (int)hRsrc16 > map->nUsed ) return 0;
127 return map->elem[(int)hRsrc16-1].hRsrc;
130 /**********************************************************************
131 * MapHRsrc16ToType
133 static WORD MapHRsrc16ToType( NE_MODULE *pModule, HRSRC hRsrc16 )
135 HRSRC_MAP *map = (HRSRC_MAP *)pModule->hRsrcMap;
136 if ( !map || !hRsrc16 || (int)hRsrc16 > map->nUsed ) return 0;
138 return map->elem[(int)hRsrc16-1].type;
142 /* filter for page-fault exceptions */
143 static WINE_EXCEPTION_FILTER(page_fault)
145 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
146 return EXCEPTION_EXECUTE_HANDLER;
147 return EXCEPTION_CONTINUE_SEARCH;
150 static HRSRC RES_FindResource2( HMODULE hModule, LPCSTR type,
151 LPCSTR name, WORD lang,
152 BOOL bUnicode, BOOL bRet16 )
154 HRSRC hRsrc = 0;
156 TRACE("(%p, %08x%s, %08x%s, %04x, %s, %s)\n",
157 hModule,
158 (UINT)type, HIWORD(type)? (bUnicode? debugstr_w((LPWSTR)type) : debugstr_a(type)) : "",
159 (UINT)name, HIWORD(name)? (bUnicode? debugstr_w((LPWSTR)name) : debugstr_a(name)) : "",
160 lang,
161 bUnicode? "W" : "A",
162 bRet16? "NE" : "PE" );
164 if (!HIWORD(hModule))
166 HMODULE16 hMod16 = MapHModuleLS( hModule );
167 NE_MODULE *pModule = NE_GetPtr( hMod16 );
168 if (!pModule) return 0;
169 if (!pModule->module32)
171 /* 16-bit NE module */
172 LPSTR typeStr, nameStr;
174 if ( HIWORD( type ) && bUnicode )
175 typeStr = HEAP_strdupWtoA( GetProcessHeap(), 0, (LPCWSTR)type );
176 else
177 typeStr = (LPSTR)type;
178 if ( HIWORD( name ) && bUnicode )
179 nameStr = HEAP_strdupWtoA( GetProcessHeap(), 0, (LPCWSTR)name );
180 else
181 nameStr = (LPSTR)name;
183 hRsrc = NE_FindResource( pModule, nameStr, typeStr );
185 if ( HIWORD( type ) && bUnicode )
186 HeapFree( GetProcessHeap(), 0, typeStr );
187 if ( HIWORD( name ) && bUnicode )
188 HeapFree( GetProcessHeap(), 0, nameStr );
190 /* If we need to return 32-bit HRSRC, no conversion is necessary,
191 we simply use the 16-bit HRSRC as 32-bit HRSRC */
193 else
195 /* 32-bit PE module */
196 hRsrc = RES_FindResource2( pModule->module32, type, name, lang, bUnicode, FALSE );
197 /* If we need to return 16-bit HRSRC, perform conversion */
198 if ( bRet16 )
199 hRsrc = MapHRsrc32To16( pModule, hRsrc,
200 HIWORD( type )? 0 : LOWORD( type ) );
203 else
205 /* 32-bit PE module */
206 LPWSTR typeStr, nameStr;
208 if ( HIWORD( type ) && !bUnicode )
209 typeStr = HEAP_strdupAtoW( GetProcessHeap(), 0, type );
210 else
211 typeStr = (LPWSTR)type;
212 if ( HIWORD( name ) && !bUnicode )
213 nameStr = HEAP_strdupAtoW( GetProcessHeap(), 0, name );
214 else
215 nameStr = (LPWSTR)name;
217 /* Here is the real difference between FindResouce and FindResourceEx */
218 if(lang == MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL) ||
219 lang == MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT) ||
220 lang == MAKELANGID(LANG_NEUTRAL, SUBLANG_SYS_DEFAULT) ||
221 lang == MAKELANGID(LANG_NEUTRAL, 3)) /* FIXME: real name? */
222 hRsrc = PE_FindResourceW( hModule, nameStr, typeStr );
223 else
224 hRsrc = PE_FindResourceExW( hModule, nameStr, typeStr, lang );
226 if ( HIWORD( type ) && !bUnicode )
227 HeapFree( GetProcessHeap(), 0, typeStr );
228 if ( HIWORD( name ) && !bUnicode )
229 HeapFree( GetProcessHeap(), 0, nameStr );
231 return hRsrc;
234 /**********************************************************************
235 * RES_FindResource
238 static HRSRC RES_FindResource( HMODULE hModule, LPCSTR type,
239 LPCSTR name, WORD lang,
240 BOOL bUnicode, BOOL bRet16 )
242 HRSRC hRsrc;
243 __TRY
245 hRsrc = RES_FindResource2(hModule, type, name, lang, bUnicode, bRet16);
247 __EXCEPT(page_fault)
249 WARN("page fault\n");
250 SetLastError(ERROR_INVALID_PARAMETER);
251 return 0;
253 __ENDTRY
254 return hRsrc;
257 /**********************************************************************
258 * RES_SizeofResource
260 static DWORD RES_SizeofResource( HMODULE hModule, HRSRC hRsrc, BOOL bRet16 )
262 if (!hRsrc) return 0;
264 TRACE("(%p, %p, %s)\n", hModule, hRsrc, bRet16? "NE" : "PE" );
266 if (!HIWORD(hModule))
268 HMODULE16 hMod16 = MapHModuleLS( hModule );
269 NE_MODULE *pModule = NE_GetPtr( hMod16 );
270 if (!pModule) return 0;
272 if (!pModule->module32) /* 16-bit NE module */
274 /* If we got a 32-bit hRsrc, we don't need to convert it */
275 return NE_SizeofResource( pModule, hRsrc );
278 /* If we got a 16-bit hRsrc, convert it */
279 if (!HIWORD(hRsrc)) hRsrc = MapHRsrc16To32( pModule, hRsrc );
282 /* 32-bit PE module */
283 return PE_SizeofResource( hRsrc );
286 /**********************************************************************
287 * RES_LoadResource
289 static HGLOBAL RES_LoadResource( HMODULE hModule, HRSRC hRsrc, BOOL bRet16 )
291 HGLOBAL hMem = 0;
293 TRACE("(%p, %p, %s)\n", hModule, hRsrc, bRet16? "NE" : "PE" );
295 if (!hRsrc) return 0;
297 if (!HIWORD(hModule))
299 HMODULE16 hMod16 = MapHModuleLS( hModule );
300 NE_MODULE *pModule = NE_GetPtr( hMod16 );
301 if (!pModule) return 0;
302 if (!pModule->module32)
304 /* 16-bit NE module */
306 /* If we got a 32-bit hRsrc, we don't need to convert it */
307 hMem = HGLOBAL_32(NE_LoadResource( pModule, LOWORD(hRsrc) ));
309 /* If we are to return a 32-bit resource, we should probably
310 convert it but we don't for now. FIXME !!! */
311 return hMem;
313 else
315 /* If we got a 16-bit hRsrc, convert it */
316 HRSRC hRsrc32 = HIWORD(hRsrc)? hRsrc : MapHRsrc16To32( pModule, hRsrc );
318 hMem = PE_LoadResource( pModule->module32, hRsrc32 );
320 /* If we need to return a 16-bit resource, convert it */
321 if ( bRet16 )
323 WORD type = MapHRsrc16ToType( pModule, hRsrc );
324 DWORD size = SizeofResource( hModule, hRsrc );
325 LPVOID bits = LockResource( hMem );
327 hMem = HGLOBAL_32(NE_LoadPEResource( pModule, type, bits, size ));
331 else
333 /* 32-bit PE module */
334 hMem = PE_LoadResource( hModule, hRsrc );
337 return hMem;
340 /**********************************************************************
341 * FindResource (KERNEL.60)
343 HRSRC16 WINAPI FindResource16( HMODULE16 hModule, LPCSTR name, LPCSTR type )
345 return LOWORD( RES_FindResource( HMODULE_32(hModule), type, name,
346 MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), FALSE, TRUE ) );
349 /**********************************************************************
350 * FindResourceA (KERNEL32.@)
352 HRSRC WINAPI FindResourceA( HMODULE hModule, LPCSTR name, LPCSTR type )
354 return RES_FindResource( hModule, type, name,
355 MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), FALSE, FALSE );
358 /**********************************************************************
359 * FindResourceExA (KERNEL32.@)
361 HRSRC WINAPI FindResourceExA( HMODULE hModule,
362 LPCSTR type, LPCSTR name, WORD lang )
364 return RES_FindResource( hModule, type, name,
365 lang, FALSE, FALSE );
368 /**********************************************************************
369 * FindResourceExW (KERNEL32.@)
371 HRSRC WINAPI FindResourceExW( HMODULE hModule,
372 LPCWSTR type, LPCWSTR name, WORD lang )
374 return RES_FindResource( hModule, (LPCSTR)type, (LPCSTR)name,
375 lang, TRUE, FALSE );
378 /**********************************************************************
379 * FindResourceW (KERNEL32.@)
381 HRSRC WINAPI FindResourceW(HINSTANCE hModule, LPCWSTR name, LPCWSTR type)
383 return RES_FindResource( hModule, (LPCSTR)type, (LPCSTR)name,
384 MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), TRUE, FALSE );
387 /**********************************************************************
388 * LoadResource (KERNEL.61)
390 HGLOBAL16 WINAPI LoadResource16( HMODULE16 hModule, HRSRC16 hRsrc )
392 return HGLOBAL_16(RES_LoadResource( HMODULE_32(hModule), HRSRC_32(hRsrc), TRUE ));
395 /**********************************************************************
396 * LoadResource (KERNEL32.@)
398 HGLOBAL WINAPI LoadResource( HINSTANCE hModule, HRSRC hRsrc )
400 return RES_LoadResource( hModule, hRsrc, FALSE );
403 /**********************************************************************
404 * LockResource (KERNEL.62)
406 SEGPTR WINAPI WIN16_LockResource16( HGLOBAL16 handle )
408 TRACE("(%04x)\n", handle );
409 /* May need to reload the resource if discarded */
410 return K32WOWGlobalLock16( handle );
413 /**********************************************************************
414 * LockResource16 (KERNEL32.@)
416 LPVOID WINAPI LockResource16( HGLOBAL16 handle )
418 return MapSL( WIN16_LockResource16(handle) );
421 /**********************************************************************
422 * LockResource (KERNEL32.@)
424 LPVOID WINAPI LockResource( HGLOBAL handle )
426 TRACE("(%p)\n", handle );
428 if (HIWORD( handle )) /* 32-bit memory handle */
429 return (LPVOID)handle;
431 /* 16-bit memory handle */
432 return LockResource16( LOWORD(handle) );
435 typedef WORD (WINAPI *pDestroyIcon32Proc)( HGLOBAL16 handle, UINT16 flags );
438 /**********************************************************************
439 * FreeResource (KERNEL.63)
441 BOOL16 WINAPI FreeResource16( HGLOBAL16 handle )
443 HGLOBAL16 retv = handle;
444 NE_MODULE *pModule = NE_GetPtr( FarGetOwner16( handle ) );
446 TRACE("(%04x)\n", handle );
448 /* Try NE resource first */
449 retv = NE_FreeResource( pModule, handle );
451 /* If this failed, call USER.DestroyIcon32; this will check
452 whether it is a shared cursor/icon; if not it will call
453 GlobalFree16() */
454 if ( retv )
456 pDestroyIcon32Proc proc;
457 HMODULE user = GetModuleHandleA( "user32.dll" );
459 if (user && (proc = (pDestroyIcon32Proc)GetProcAddress( user, "DestroyIcon32" )))
460 retv = proc( handle, CID_RESOURCE );
461 else
462 retv = GlobalFree16( handle );
464 return (BOOL)retv;
467 /**********************************************************************
468 * FreeResource (KERNEL32.@)
470 BOOL WINAPI FreeResource( HGLOBAL handle )
472 if (HIWORD(handle)) return 0; /* 32-bit memory handle: nothing to do */
474 return FreeResource16( LOWORD(handle) );
477 /**********************************************************************
478 * SizeofResource (KERNEL.65)
480 DWORD WINAPI SizeofResource16( HMODULE16 hModule, HRSRC16 hRsrc )
482 return RES_SizeofResource( HMODULE_32(hModule), HRSRC_32(hRsrc), TRUE );
485 /**********************************************************************
486 * SizeofResource (KERNEL32.@)
488 DWORD WINAPI SizeofResource( HINSTANCE hModule, HRSRC hRsrc )
490 return RES_SizeofResource( hModule, hRsrc, FALSE );