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"
23 #include "cursoricon.h"
28 #include "debugtools.h"
32 DEFAULT_DEBUG_CHANNEL(resource);
34 #define HRSRC_MAP_BLOCKSIZE 16
36 typedef struct _HRSRC_ELEM
42 typedef struct _HRSRC_MAP
49 /**********************************************************************
52 static HRSRC16 MapHRsrc32To16( NE_MODULE *pModule, HANDLE hRsrc32, WORD type )
54 HRSRC_MAP *map = (HRSRC_MAP *)pModule->hRsrcMap;
58 /* On first call, initialize HRSRC map */
61 if ( !(map = (HRSRC_MAP *)HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
62 sizeof(HRSRC_MAP) ) ) )
64 ERR("Cannot allocate HRSRC map\n" );
67 pModule->hRsrcMap = (LPVOID)map;
70 /* Check whether HRSRC32 already in map */
71 for ( i = 0; i < map->nUsed; i++ )
72 if ( map->elem[i].hRsrc == hRsrc32 )
73 return (HRSRC16)(i + 1);
75 /* If no space left, grow table */
76 if ( map->nUsed == map->nAlloc )
78 if ( !(newElem = (HRSRC_ELEM *)HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
80 (map->nAlloc + HRSRC_MAP_BLOCKSIZE)
81 * sizeof(HRSRC_ELEM) ) ))
83 ERR("Cannot grow HRSRC map\n" );
87 map->nAlloc += HRSRC_MAP_BLOCKSIZE;
90 /* Add HRSRC32 to table */
91 map->elem[map->nUsed].hRsrc = hRsrc32;
92 map->elem[map->nUsed].type = type;
95 return (HRSRC16)map->nUsed;
98 /**********************************************************************
101 static HANDLE MapHRsrc16To32( NE_MODULE *pModule, HRSRC16 hRsrc16 )
103 HRSRC_MAP *map = (HRSRC_MAP *)pModule->hRsrcMap;
104 if ( !map || !hRsrc16 || (int)hRsrc16 > map->nUsed ) return 0;
106 return map->elem[(int)hRsrc16-1].hRsrc;
109 /**********************************************************************
112 static WORD MapHRsrc16ToType( NE_MODULE *pModule, HRSRC16 hRsrc16 )
114 HRSRC_MAP *map = (HRSRC_MAP *)pModule->hRsrcMap;
115 if ( !map || !hRsrc16 || (int)hRsrc16 > map->nUsed ) return 0;
117 return map->elem[(int)hRsrc16-1].type;
121 /* filter for page-fault exceptions */
122 static WINE_EXCEPTION_FILTER(page_fault)
124 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
125 return EXCEPTION_EXECUTE_HANDLER;
126 return EXCEPTION_CONTINUE_SEARCH;
129 static HRSRC RES_FindResource2( HMODULE hModule, LPCSTR type,
130 LPCSTR name, WORD lang,
131 BOOL bUnicode, BOOL bRet16 )
135 TRACE("(%08x, %08x%s, %08x%s, %04x, %s, %s)\n",
137 (UINT)type, HIWORD(type)? (bUnicode? debugstr_w((LPWSTR)type) : debugstr_a(type)) : "",
138 (UINT)name, HIWORD(name)? (bUnicode? debugstr_w((LPWSTR)name) : debugstr_a(name)) : "",
141 bRet16? "NE" : "PE" );
143 if (!HIWORD(hModule))
145 HMODULE16 hMod16 = MapHModuleLS( hModule );
146 NE_MODULE *pModule = NE_GetPtr( hMod16 );
147 if (!pModule) return 0;
148 if (!pModule->module32)
150 /* 16-bit NE module */
151 LPSTR typeStr, nameStr;
153 if ( HIWORD( type ) && bUnicode )
154 typeStr = HEAP_strdupWtoA( GetProcessHeap(), 0, (LPCWSTR)type );
156 typeStr = (LPSTR)type;
157 if ( HIWORD( name ) && bUnicode )
158 nameStr = HEAP_strdupWtoA( GetProcessHeap(), 0, (LPCWSTR)name );
160 nameStr = (LPSTR)name;
162 hRsrc = NE_FindResource( pModule, nameStr, typeStr );
164 if ( HIWORD( type ) && bUnicode )
165 HeapFree( GetProcessHeap(), 0, typeStr );
166 if ( HIWORD( name ) && bUnicode )
167 HeapFree( GetProcessHeap(), 0, nameStr );
169 /* If we need to return 32-bit HRSRC, no conversion is necessary,
170 we simply use the 16-bit HRSRC as 32-bit HRSRC */
174 /* 32-bit PE module */
175 hRsrc = RES_FindResource2( pModule->module32, type, name, lang, bUnicode, FALSE );
176 /* If we need to return 16-bit HRSRC, perform conversion */
178 hRsrc = MapHRsrc32To16( pModule, hRsrc,
179 HIWORD( type )? 0 : LOWORD( type ) );
184 /* 32-bit PE module */
185 LPWSTR typeStr, nameStr;
187 if ( HIWORD( type ) && !bUnicode )
188 typeStr = HEAP_strdupAtoW( GetProcessHeap(), 0, type );
190 typeStr = (LPWSTR)type;
191 if ( HIWORD( name ) && !bUnicode )
192 nameStr = HEAP_strdupAtoW( GetProcessHeap(), 0, name );
194 nameStr = (LPWSTR)name;
196 /* Here is the real difference between FindResouce and FindResourceEx */
197 if(lang == MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL) ||
198 lang == MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT) ||
199 lang == MAKELANGID(LANG_NEUTRAL, SUBLANG_SYS_DEFAULT) ||
200 lang == MAKELANGID(LANG_NEUTRAL, 3)) /* FIXME: real name? */
201 hRsrc = PE_FindResourceW( hModule, nameStr, typeStr );
203 hRsrc = PE_FindResourceExW( hModule, nameStr, typeStr, lang );
205 if ( HIWORD( type ) && !bUnicode )
206 HeapFree( GetProcessHeap(), 0, typeStr );
207 if ( HIWORD( name ) && !bUnicode )
208 HeapFree( GetProcessHeap(), 0, nameStr );
213 /**********************************************************************
217 static HRSRC RES_FindResource( HMODULE hModule, LPCSTR type,
218 LPCSTR name, WORD lang,
219 BOOL bUnicode, BOOL bRet16 )
224 hRsrc = RES_FindResource2(hModule, type, name, lang, bUnicode, bRet16);
228 WARN("page fault\n");
229 SetLastError(ERROR_INVALID_PARAMETER);
236 /**********************************************************************
239 static DWORD RES_SizeofResource( HMODULE hModule, HRSRC hRsrc, BOOL bRet16 )
241 if (!hRsrc) return 0;
243 TRACE("(%08x, %08x, %s)\n", hModule, hRsrc, bRet16? "NE" : "PE" );
245 if (!HIWORD(hModule))
247 HMODULE16 hMod16 = MapHModuleLS( hModule );
248 NE_MODULE *pModule = NE_GetPtr( hMod16 );
249 if (!pModule) return 0;
251 if (!pModule->module32) /* 16-bit NE module */
253 /* If we got a 32-bit hRsrc, we don't need to convert it */
254 return NE_SizeofResource( pModule, hRsrc );
257 /* If we got a 16-bit hRsrc, convert it */
258 if (!HIWORD(hRsrc)) hRsrc = MapHRsrc16To32( pModule, hRsrc );
261 /* 32-bit PE module */
262 return PE_SizeofResource( hRsrc );
265 /**********************************************************************
268 static HGLOBAL RES_LoadResource( HMODULE hModule, HRSRC hRsrc, BOOL bRet16 )
272 TRACE("(%08x, %08x, %s)\n", hModule, hRsrc, bRet16? "NE" : "PE" );
274 if (!hRsrc) return 0;
276 if (!HIWORD(hModule))
278 HMODULE16 hMod16 = MapHModuleLS( hModule );
279 NE_MODULE *pModule = NE_GetPtr( hMod16 );
280 if (!pModule) return 0;
281 if (!pModule->module32)
283 /* 16-bit NE module */
285 /* If we got a 32-bit hRsrc, we don't need to convert it */
286 hMem = NE_LoadResource( pModule, hRsrc );
288 /* If we are to return a 32-bit resource, we should probably
289 convert it but we don't for now. FIXME !!! */
294 /* If we got a 16-bit hRsrc, convert it */
295 HRSRC hRsrc32 = HIWORD(hRsrc)? hRsrc : MapHRsrc16To32( pModule, hRsrc );
297 hMem = PE_LoadResource( pModule->module32, hRsrc32 );
299 /* If we need to return a 16-bit resource, convert it */
302 WORD type = MapHRsrc16ToType( pModule, hRsrc );
303 DWORD size = SizeofResource( hModule, hRsrc );
304 LPVOID bits = LockResource( hMem );
306 hMem = NE_LoadPEResource( pModule, type, bits, size );
312 /* 32-bit PE module */
313 hMem = PE_LoadResource( hModule, hRsrc );
319 /**********************************************************************
320 * FindResource16 (KERNEL.60)
322 HRSRC16 WINAPI FindResource16( HMODULE16 hModule, LPCSTR name, LPCSTR type )
324 return RES_FindResource( hModule, type, name,
325 MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), FALSE, TRUE );
328 /**********************************************************************
329 * FindResourceA (KERNEL32.128)
331 HANDLE WINAPI FindResourceA( HMODULE hModule, LPCSTR name, LPCSTR type )
333 return RES_FindResource( hModule, type, name,
334 MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), FALSE, FALSE );
337 /**********************************************************************
338 * FindResourceExA (KERNEL32.129)
340 HANDLE WINAPI FindResourceExA( HMODULE hModule,
341 LPCSTR type, LPCSTR name, WORD lang )
343 return RES_FindResource( hModule, type, name,
344 lang, FALSE, FALSE );
347 /**********************************************************************
348 * FindResourceExW (KERNEL32.130)
350 HRSRC WINAPI FindResourceExW( HMODULE hModule,
351 LPCWSTR type, LPCWSTR name, WORD lang )
353 return RES_FindResource( hModule, (LPCSTR)type, (LPCSTR)name,
357 /**********************************************************************
358 * FindResourceW (KERNEL32.131)
360 HRSRC WINAPI FindResourceW(HINSTANCE hModule, LPCWSTR name, LPCWSTR type)
362 return RES_FindResource( hModule, (LPCSTR)type, (LPCSTR)name,
363 MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), TRUE, FALSE );
366 /**********************************************************************
367 * LoadResource16 (KERNEL.61)
369 HGLOBAL16 WINAPI LoadResource16( HMODULE16 hModule, HRSRC16 hRsrc )
371 return RES_LoadResource( hModule, hRsrc, TRUE );
374 /**********************************************************************
375 * LoadResource (KERNEL32.370)
377 HGLOBAL WINAPI LoadResource( HINSTANCE hModule, HRSRC hRsrc )
379 return RES_LoadResource( hModule, hRsrc, FALSE );
382 /**********************************************************************
383 * LockResource16 (KERNEL.62)
385 SEGPTR WINAPI WIN16_LockResource16( HGLOBAL16 handle )
387 TRACE("(%04x)\n", handle );
388 /* May need to reload the resource if discarded */
389 return WIN16_GlobalLock16( handle );
391 LPVOID WINAPI LockResource16( HGLOBAL16 handle )
393 return PTR_SEG_TO_LIN( WIN16_LockResource16(handle) );
396 /**********************************************************************
397 * LockResource (KERNEL32.384)
399 LPVOID WINAPI LockResource( HGLOBAL handle )
401 TRACE("(%08x)\n", handle );
403 if (HIWORD( handle )) /* 32-bit memory handle */
404 return (LPVOID)handle;
406 /* 16-bit memory handle */
407 return LockResource16( handle );
410 /**********************************************************************
411 * FreeResource16 (KERNEL.63)
413 BOOL16 WINAPI FreeResource16( HGLOBAL16 handle )
415 HGLOBAL retv = handle;
416 NE_MODULE *pModule = NE_GetPtr( FarGetOwner16( handle ) );
418 TRACE("(%04x)\n", handle );
420 /* Try NE resource first */
421 retv = NE_FreeResource( pModule, handle );
423 /* If this failed, call USER.DestroyIcon32; this will check
424 whether it is a shared cursor/icon; if not it will call
428 if ( Callout.DestroyIcon32 )
429 retv = Callout.DestroyIcon32( handle, CID_RESOURCE );
431 retv = GlobalFree16( handle );
436 /**********************************************************************
437 * FreeResource (KERNEL32.145)
439 BOOL WINAPI FreeResource( HGLOBAL handle )
441 if (HIWORD(handle)) return 0; /* 32-bit memory handle: nothing to do */
443 return FreeResource16( handle );
446 /**********************************************************************
447 * SizeofResource16 (KERNEL.65)
449 DWORD WINAPI SizeofResource16( HMODULE16 hModule, HRSRC16 hRsrc )
451 return RES_SizeofResource( hModule, hRsrc, TRUE );
454 /**********************************************************************
455 * SizeofResource (KERNEL32.522)
457 DWORD WINAPI SizeofResource( HINSTANCE hModule, HRSRC hRsrc )
459 return RES_SizeofResource( hModule, hRsrc, FALSE );