4 * Copyright 1993 Robert J. Amstadt
5 * Copyright 1995 Alexandre Julliard
11 #include <sys/types.h>
17 #include "wine/winbase16.h"
18 #include "wine/exception.h"
21 #include "cursoricon.h"
25 #include "debugtools.h"
29 DEFAULT_DEBUG_CHANNEL(resource);
31 #define HRSRC_MAP_BLOCKSIZE 16
33 typedef struct _HRSRC_ELEM
39 typedef struct _HRSRC_MAP
46 /**********************************************************************
49 static HRSRC16 MapHRsrc32To16( NE_MODULE *pModule, HANDLE hRsrc32, WORD type )
51 HRSRC_MAP *map = (HRSRC_MAP *)pModule->hRsrcMap;
55 /* On first call, initialize HRSRC map */
58 if ( !(map = (HRSRC_MAP *)HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
59 sizeof(HRSRC_MAP) ) ) )
61 ERR("Cannot allocate HRSRC map\n" );
64 pModule->hRsrcMap = (LPVOID)map;
67 /* Check whether HRSRC32 already in map */
68 for ( i = 0; i < map->nUsed; i++ )
69 if ( map->elem[i].hRsrc == hRsrc32 )
70 return (HRSRC16)(i + 1);
72 /* If no space left, grow table */
73 if ( map->nUsed == map->nAlloc )
75 if ( !(newElem = (HRSRC_ELEM *)HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
77 (map->nAlloc + HRSRC_MAP_BLOCKSIZE)
78 * sizeof(HRSRC_ELEM) ) ))
80 ERR("Cannot grow HRSRC map\n" );
84 map->nAlloc += HRSRC_MAP_BLOCKSIZE;
87 /* Add HRSRC32 to table */
88 map->elem[map->nUsed].hRsrc = hRsrc32;
89 map->elem[map->nUsed].type = type;
92 return (HRSRC16)map->nUsed;
95 /**********************************************************************
98 static HANDLE MapHRsrc16To32( NE_MODULE *pModule, HRSRC16 hRsrc16 )
100 HRSRC_MAP *map = (HRSRC_MAP *)pModule->hRsrcMap;
101 if ( !map || !hRsrc16 || (int)hRsrc16 > map->nUsed ) return 0;
103 return map->elem[(int)hRsrc16-1].hRsrc;
106 /**********************************************************************
109 static WORD MapHRsrc16ToType( NE_MODULE *pModule, HRSRC16 hRsrc16 )
111 HRSRC_MAP *map = (HRSRC_MAP *)pModule->hRsrcMap;
112 if ( !map || !hRsrc16 || (int)hRsrc16 > map->nUsed ) return 0;
114 return map->elem[(int)hRsrc16-1].type;
118 /* filter for page-fault exceptions */
119 static WINE_EXCEPTION_FILTER(page_fault)
121 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
122 return EXCEPTION_EXECUTE_HANDLER;
123 return EXCEPTION_CONTINUE_SEARCH;
126 static HRSRC RES_FindResource2( HMODULE hModule, LPCSTR type,
127 LPCSTR name, WORD lang,
128 BOOL bUnicode, BOOL bRet16 )
132 TRACE("(%08x, %08x%s, %08x%s, %04x, %s, %s)\n",
134 (UINT)type, HIWORD(type)? (bUnicode? debugstr_w((LPWSTR)type) : debugstr_a(type)) : "",
135 (UINT)name, HIWORD(name)? (bUnicode? debugstr_w((LPWSTR)name) : debugstr_a(name)) : "",
138 bRet16? "NE" : "PE" );
140 if (!HIWORD(hModule))
142 HMODULE16 hMod16 = MapHModuleLS( hModule );
143 NE_MODULE *pModule = NE_GetPtr( hMod16 );
144 if (!pModule) return 0;
145 if (!pModule->module32)
147 /* 16-bit NE module */
148 LPSTR typeStr, nameStr;
150 if ( HIWORD( type ) && bUnicode )
151 typeStr = HEAP_strdupWtoA( GetProcessHeap(), 0, (LPCWSTR)type );
153 typeStr = (LPSTR)type;
154 if ( HIWORD( name ) && bUnicode )
155 nameStr = HEAP_strdupWtoA( GetProcessHeap(), 0, (LPCWSTR)name );
157 nameStr = (LPSTR)name;
159 hRsrc = NE_FindResource( pModule, nameStr, typeStr );
161 if ( HIWORD( type ) && bUnicode )
162 HeapFree( GetProcessHeap(), 0, typeStr );
163 if ( HIWORD( name ) && bUnicode )
164 HeapFree( GetProcessHeap(), 0, nameStr );
166 /* If we need to return 32-bit HRSRC, no conversion is necessary,
167 we simply use the 16-bit HRSRC as 32-bit HRSRC */
171 /* 32-bit PE module */
172 hRsrc = RES_FindResource2( pModule->module32, type, name, lang, bUnicode, FALSE );
173 /* If we need to return 16-bit HRSRC, perform conversion */
175 hRsrc = MapHRsrc32To16( pModule, hRsrc,
176 HIWORD( type )? 0 : LOWORD( type ) );
181 /* 32-bit PE module */
182 LPWSTR typeStr, nameStr;
184 if ( HIWORD( type ) && !bUnicode )
185 typeStr = HEAP_strdupAtoW( GetProcessHeap(), 0, type );
187 typeStr = (LPWSTR)type;
188 if ( HIWORD( name ) && !bUnicode )
189 nameStr = HEAP_strdupAtoW( GetProcessHeap(), 0, name );
191 nameStr = (LPWSTR)name;
193 /* Here is the real difference between FindResouce and FindResourceEx */
194 if(lang == MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL) ||
195 lang == MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT) ||
196 lang == MAKELANGID(LANG_NEUTRAL, SUBLANG_SYS_DEFAULT) ||
197 lang == MAKELANGID(LANG_NEUTRAL, 3)) /* FIXME: real name? */
198 hRsrc = PE_FindResourceW( hModule, nameStr, typeStr );
200 hRsrc = PE_FindResourceExW( hModule, nameStr, typeStr, lang );
202 if ( HIWORD( type ) && !bUnicode )
203 HeapFree( GetProcessHeap(), 0, typeStr );
204 if ( HIWORD( name ) && !bUnicode )
205 HeapFree( GetProcessHeap(), 0, nameStr );
210 /**********************************************************************
214 static HRSRC RES_FindResource( HMODULE hModule, LPCSTR type,
215 LPCSTR name, WORD lang,
216 BOOL bUnicode, BOOL bRet16 )
221 hRsrc = RES_FindResource2(hModule, type, name, lang, bUnicode, bRet16);
225 WARN("page fault\n");
226 SetLastError(ERROR_INVALID_PARAMETER);
233 /**********************************************************************
236 static DWORD RES_SizeofResource( HMODULE hModule, HRSRC hRsrc, BOOL bRet16 )
238 if (!hRsrc) return 0;
240 TRACE("(%08x, %08x, %s)\n", hModule, hRsrc, bRet16? "NE" : "PE" );
242 if (!HIWORD(hModule))
244 HMODULE16 hMod16 = MapHModuleLS( hModule );
245 NE_MODULE *pModule = NE_GetPtr( hMod16 );
246 if (!pModule) return 0;
248 if (!pModule->module32) /* 16-bit NE module */
250 /* If we got a 32-bit hRsrc, we don't need to convert it */
251 return NE_SizeofResource( pModule, hRsrc );
254 /* If we got a 16-bit hRsrc, convert it */
255 if (!HIWORD(hRsrc)) hRsrc = MapHRsrc16To32( pModule, hRsrc );
258 /* 32-bit PE module */
259 return PE_SizeofResource( hRsrc );
262 /**********************************************************************
265 static HGLOBAL RES_LoadResource( HMODULE hModule, HRSRC hRsrc, BOOL bRet16 )
269 TRACE("(%08x, %08x, %s)\n", hModule, hRsrc, bRet16? "NE" : "PE" );
271 if (!hRsrc) return 0;
273 if (!HIWORD(hModule))
275 HMODULE16 hMod16 = MapHModuleLS( hModule );
276 NE_MODULE *pModule = NE_GetPtr( hMod16 );
277 if (!pModule) return 0;
278 if (!pModule->module32)
280 /* 16-bit NE module */
282 /* If we got a 32-bit hRsrc, we don't need to convert it */
283 hMem = NE_LoadResource( pModule, hRsrc );
285 /* If we are to return a 32-bit resource, we should probably
286 convert it but we don't for now. FIXME !!! */
291 /* If we got a 16-bit hRsrc, convert it */
292 HRSRC hRsrc32 = HIWORD(hRsrc)? hRsrc : MapHRsrc16To32( pModule, hRsrc );
294 hMem = PE_LoadResource( pModule->module32, hRsrc32 );
296 /* If we need to return a 16-bit resource, convert it */
299 WORD type = MapHRsrc16ToType( pModule, hRsrc );
300 DWORD size = SizeofResource( hModule, hRsrc );
301 LPVOID bits = LockResource( hMem );
303 hMem = NE_LoadPEResource( pModule, type, bits, size );
309 /* 32-bit PE module */
310 hMem = PE_LoadResource( hModule, hRsrc );
316 /**********************************************************************
317 * FindResource16 (KERNEL.60)
319 HRSRC16 WINAPI FindResource16( HMODULE16 hModule, LPCSTR name, LPCSTR type )
321 return RES_FindResource( hModule, type, name,
322 MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), FALSE, TRUE );
325 /**********************************************************************
326 * FindResourceA (KERNEL32.128)
328 HRSRC WINAPI FindResourceA( HMODULE hModule, LPCSTR name, LPCSTR type )
330 return RES_FindResource( hModule, type, name,
331 MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), FALSE, FALSE );
334 /**********************************************************************
335 * FindResourceExA (KERNEL32.129)
337 HRSRC WINAPI FindResourceExA( HMODULE hModule,
338 LPCSTR type, LPCSTR name, WORD lang )
340 return RES_FindResource( hModule, type, name,
341 lang, FALSE, FALSE );
344 /**********************************************************************
345 * FindResourceExW (KERNEL32.130)
347 HRSRC WINAPI FindResourceExW( HMODULE hModule,
348 LPCWSTR type, LPCWSTR name, WORD lang )
350 return RES_FindResource( hModule, (LPCSTR)type, (LPCSTR)name,
354 /**********************************************************************
355 * FindResourceW (KERNEL32.131)
357 HRSRC WINAPI FindResourceW(HINSTANCE hModule, LPCWSTR name, LPCWSTR type)
359 return RES_FindResource( hModule, (LPCSTR)type, (LPCSTR)name,
360 MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), TRUE, FALSE );
363 /**********************************************************************
364 * LoadResource16 (KERNEL.61)
366 HGLOBAL16 WINAPI LoadResource16( HMODULE16 hModule, HRSRC16 hRsrc )
368 return RES_LoadResource( hModule, hRsrc, TRUE );
371 /**********************************************************************
372 * LoadResource (KERNEL32.370)
374 HGLOBAL WINAPI LoadResource( HINSTANCE hModule, HRSRC hRsrc )
376 return RES_LoadResource( hModule, hRsrc, FALSE );
379 /**********************************************************************
380 * LockResource16 (KERNEL.62)
382 SEGPTR WINAPI WIN16_LockResource16( HGLOBAL16 handle )
384 TRACE("(%04x)\n", handle );
385 /* May need to reload the resource if discarded */
386 return K32WOWGlobalLock16( handle );
389 /**********************************************************************
390 * LockResource16 (KERNEL but also exported from KERNEL32 in Wine)
392 LPVOID WINAPI LockResource16( HGLOBAL16 handle )
394 return MapSL( WIN16_LockResource16(handle) );
397 /**********************************************************************
398 * LockResource (KERNEL32.384)
400 LPVOID WINAPI LockResource( HGLOBAL handle )
402 TRACE("(%08x)\n", handle );
404 if (HIWORD( handle )) /* 32-bit memory handle */
405 return (LPVOID)handle;
407 /* 16-bit memory handle */
408 return LockResource16( handle );
411 /**********************************************************************
412 * FreeResource16 (KERNEL.63)
414 BOOL16 WINAPI FreeResource16( HGLOBAL16 handle )
416 HGLOBAL retv = handle;
417 NE_MODULE *pModule = NE_GetPtr( FarGetOwner16( handle ) );
419 TRACE("(%04x)\n", handle );
421 /* Try NE resource first */
422 retv = NE_FreeResource( pModule, handle );
424 /* If this failed, call USER.DestroyIcon32; this will check
425 whether it is a shared cursor/icon; if not it will call
429 if ( Callout.DestroyIcon32 )
430 retv = Callout.DestroyIcon32( handle, CID_RESOURCE );
432 retv = GlobalFree16( handle );
437 /**********************************************************************
438 * FreeResource (KERNEL32.145)
440 BOOL WINAPI FreeResource( HGLOBAL handle )
442 if (HIWORD(handle)) return 0; /* 32-bit memory handle: nothing to do */
444 return FreeResource16( handle );
447 /**********************************************************************
448 * SizeofResource16 (KERNEL.65)
450 DWORD WINAPI SizeofResource16( HMODULE16 hModule, HRSRC16 hRsrc )
452 return RES_SizeofResource( hModule, hRsrc, TRUE );
455 /**********************************************************************
456 * SizeofResource (KERNEL32.522)
458 DWORD WINAPI SizeofResource( HINSTANCE hModule, HRSRC hRsrc )
460 return RES_SizeofResource( hModule, hRsrc, FALSE );