Removed obsolete dosmod.
[wine/multimedia.git] / loader / resource.c
blob2f8b4fd5703a668e8a0b4679bd869e17a10ffe44
1 /*
2 * Resources
4 * Copyright 1993 Robert J. Amstadt
5 * Copyright 1995 Alexandre Julliard
6 */
8 #include <assert.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <fcntl.h>
14 #include <unistd.h>
15 #include "windef.h"
16 #include "winbase.h"
17 #include "wine/winbase16.h"
18 #include "wine/exception.h"
19 #include "heap.h"
20 #include "cursoricon.h"
21 #include "module.h"
22 #include "file.h"
23 #include "debugtools.h"
24 #include "winerror.h"
25 #include "winnls.h"
27 DEFAULT_DEBUG_CHANNEL(resource);
29 #define HRSRC_MAP_BLOCKSIZE 16
31 typedef struct _HRSRC_ELEM
33 HANDLE hRsrc;
34 WORD type;
35 } HRSRC_ELEM;
37 typedef struct _HRSRC_MAP
39 int nAlloc;
40 int nUsed;
41 HRSRC_ELEM *elem;
42 } HRSRC_MAP;
44 /**********************************************************************
45 * MapHRsrc32To16
47 static HRSRC16 MapHRsrc32To16( NE_MODULE *pModule, HANDLE hRsrc32, WORD type )
49 HRSRC_MAP *map = (HRSRC_MAP *)pModule->hRsrcMap;
50 HRSRC_ELEM *newElem;
51 int i;
53 /* On first call, initialize HRSRC map */
54 if ( !map )
56 if ( !(map = (HRSRC_MAP *)HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
57 sizeof(HRSRC_MAP) ) ) )
59 ERR("Cannot allocate HRSRC map\n" );
60 return 0;
62 pModule->hRsrcMap = (LPVOID)map;
65 /* Check whether HRSRC32 already in map */
66 for ( i = 0; i < map->nUsed; i++ )
67 if ( map->elem[i].hRsrc == hRsrc32 )
68 return (HRSRC16)(i + 1);
70 /* If no space left, grow table */
71 if ( map->nUsed == map->nAlloc )
73 if ( !(newElem = (HRSRC_ELEM *)HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
74 map->elem,
75 (map->nAlloc + HRSRC_MAP_BLOCKSIZE)
76 * sizeof(HRSRC_ELEM) ) ))
78 ERR("Cannot grow HRSRC map\n" );
79 return 0;
81 map->elem = newElem;
82 map->nAlloc += HRSRC_MAP_BLOCKSIZE;
85 /* Add HRSRC32 to table */
86 map->elem[map->nUsed].hRsrc = hRsrc32;
87 map->elem[map->nUsed].type = type;
88 map->nUsed++;
90 return (HRSRC16)map->nUsed;
93 /**********************************************************************
94 * MapHRsrc16To32
96 static HANDLE MapHRsrc16To32( NE_MODULE *pModule, HRSRC16 hRsrc16 )
98 HRSRC_MAP *map = (HRSRC_MAP *)pModule->hRsrcMap;
99 if ( !map || !hRsrc16 || (int)hRsrc16 > map->nUsed ) return 0;
101 return map->elem[(int)hRsrc16-1].hRsrc;
104 /**********************************************************************
105 * MapHRsrc16ToType
107 static WORD MapHRsrc16ToType( NE_MODULE *pModule, HRSRC16 hRsrc16 )
109 HRSRC_MAP *map = (HRSRC_MAP *)pModule->hRsrcMap;
110 if ( !map || !hRsrc16 || (int)hRsrc16 > map->nUsed ) return 0;
112 return map->elem[(int)hRsrc16-1].type;
116 /* filter for page-fault exceptions */
117 static WINE_EXCEPTION_FILTER(page_fault)
119 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
120 return EXCEPTION_EXECUTE_HANDLER;
121 return EXCEPTION_CONTINUE_SEARCH;
124 static HRSRC RES_FindResource2( HMODULE hModule, LPCSTR type,
125 LPCSTR name, WORD lang,
126 BOOL bUnicode, BOOL bRet16 )
128 HRSRC hRsrc = 0;
130 TRACE("(%08x, %08x%s, %08x%s, %04x, %s, %s)\n",
131 hModule,
132 (UINT)type, HIWORD(type)? (bUnicode? debugstr_w((LPWSTR)type) : debugstr_a(type)) : "",
133 (UINT)name, HIWORD(name)? (bUnicode? debugstr_w((LPWSTR)name) : debugstr_a(name)) : "",
134 lang,
135 bUnicode? "W" : "A",
136 bRet16? "NE" : "PE" );
138 if (!HIWORD(hModule))
140 HMODULE16 hMod16 = MapHModuleLS( hModule );
141 NE_MODULE *pModule = NE_GetPtr( hMod16 );
142 if (!pModule) return 0;
143 if (!pModule->module32)
145 /* 16-bit NE module */
146 LPSTR typeStr, nameStr;
148 if ( HIWORD( type ) && bUnicode )
149 typeStr = HEAP_strdupWtoA( GetProcessHeap(), 0, (LPCWSTR)type );
150 else
151 typeStr = (LPSTR)type;
152 if ( HIWORD( name ) && bUnicode )
153 nameStr = HEAP_strdupWtoA( GetProcessHeap(), 0, (LPCWSTR)name );
154 else
155 nameStr = (LPSTR)name;
157 hRsrc = NE_FindResource( pModule, nameStr, typeStr );
159 if ( HIWORD( type ) && bUnicode )
160 HeapFree( GetProcessHeap(), 0, typeStr );
161 if ( HIWORD( name ) && bUnicode )
162 HeapFree( GetProcessHeap(), 0, nameStr );
164 /* If we need to return 32-bit HRSRC, no conversion is necessary,
165 we simply use the 16-bit HRSRC as 32-bit HRSRC */
167 else
169 /* 32-bit PE module */
170 hRsrc = RES_FindResource2( pModule->module32, type, name, lang, bUnicode, FALSE );
171 /* If we need to return 16-bit HRSRC, perform conversion */
172 if ( bRet16 )
173 hRsrc = MapHRsrc32To16( pModule, hRsrc,
174 HIWORD( type )? 0 : LOWORD( type ) );
177 else
179 /* 32-bit PE module */
180 LPWSTR typeStr, nameStr;
182 if ( HIWORD( type ) && !bUnicode )
183 typeStr = HEAP_strdupAtoW( GetProcessHeap(), 0, type );
184 else
185 typeStr = (LPWSTR)type;
186 if ( HIWORD( name ) && !bUnicode )
187 nameStr = HEAP_strdupAtoW( GetProcessHeap(), 0, name );
188 else
189 nameStr = (LPWSTR)name;
191 /* Here is the real difference between FindResouce and FindResourceEx */
192 if(lang == MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL) ||
193 lang == MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT) ||
194 lang == MAKELANGID(LANG_NEUTRAL, SUBLANG_SYS_DEFAULT) ||
195 lang == MAKELANGID(LANG_NEUTRAL, 3)) /* FIXME: real name? */
196 hRsrc = PE_FindResourceW( hModule, nameStr, typeStr );
197 else
198 hRsrc = PE_FindResourceExW( hModule, nameStr, typeStr, lang );
200 if ( HIWORD( type ) && !bUnicode )
201 HeapFree( GetProcessHeap(), 0, typeStr );
202 if ( HIWORD( name ) && !bUnicode )
203 HeapFree( GetProcessHeap(), 0, nameStr );
205 return hRsrc;
208 /**********************************************************************
209 * RES_FindResource
212 static HRSRC RES_FindResource( HMODULE hModule, LPCSTR type,
213 LPCSTR name, WORD lang,
214 BOOL bUnicode, BOOL bRet16 )
216 HRSRC hRsrc;
217 __TRY
219 hRsrc = RES_FindResource2(hModule, type, name, lang, bUnicode, bRet16);
221 __EXCEPT(page_fault)
223 WARN("page fault\n");
224 SetLastError(ERROR_INVALID_PARAMETER);
225 return 0;
227 __ENDTRY
228 return hRsrc;
231 /**********************************************************************
232 * RES_SizeofResource
234 static DWORD RES_SizeofResource( HMODULE hModule, HRSRC hRsrc, BOOL bRet16 )
236 if (!hRsrc) return 0;
238 TRACE("(%08x, %08x, %s)\n", hModule, hRsrc, bRet16? "NE" : "PE" );
240 if (!HIWORD(hModule))
242 HMODULE16 hMod16 = MapHModuleLS( hModule );
243 NE_MODULE *pModule = NE_GetPtr( hMod16 );
244 if (!pModule) return 0;
246 if (!pModule->module32) /* 16-bit NE module */
248 /* If we got a 32-bit hRsrc, we don't need to convert it */
249 return NE_SizeofResource( pModule, hRsrc );
252 /* If we got a 16-bit hRsrc, convert it */
253 if (!HIWORD(hRsrc)) hRsrc = MapHRsrc16To32( pModule, hRsrc );
256 /* 32-bit PE module */
257 return PE_SizeofResource( hRsrc );
260 /**********************************************************************
261 * RES_LoadResource
263 static HGLOBAL RES_LoadResource( HMODULE hModule, HRSRC hRsrc, BOOL bRet16 )
265 HGLOBAL hMem = 0;
267 TRACE("(%08x, %08x, %s)\n", hModule, hRsrc, bRet16? "NE" : "PE" );
269 if (!hRsrc) return 0;
271 if (!HIWORD(hModule))
273 HMODULE16 hMod16 = MapHModuleLS( hModule );
274 NE_MODULE *pModule = NE_GetPtr( hMod16 );
275 if (!pModule) return 0;
276 if (!pModule->module32)
278 /* 16-bit NE module */
280 /* If we got a 32-bit hRsrc, we don't need to convert it */
281 hMem = NE_LoadResource( pModule, hRsrc );
283 /* If we are to return a 32-bit resource, we should probably
284 convert it but we don't for now. FIXME !!! */
285 return hMem;
287 else
289 /* If we got a 16-bit hRsrc, convert it */
290 HRSRC hRsrc32 = HIWORD(hRsrc)? hRsrc : MapHRsrc16To32( pModule, hRsrc );
292 hMem = PE_LoadResource( pModule->module32, hRsrc32 );
294 /* If we need to return a 16-bit resource, convert it */
295 if ( bRet16 )
297 WORD type = MapHRsrc16ToType( pModule, hRsrc );
298 DWORD size = SizeofResource( hModule, hRsrc );
299 LPVOID bits = LockResource( hMem );
301 hMem = NE_LoadPEResource( pModule, type, bits, size );
305 else
307 /* 32-bit PE module */
308 hMem = PE_LoadResource( hModule, hRsrc );
311 return hMem;
314 /**********************************************************************
315 * FindResource (KERNEL.60)
316 * FindResource16 (KERNEL32.@)
318 HRSRC16 WINAPI FindResource16( HMODULE16 hModule, LPCSTR name, LPCSTR type )
320 return RES_FindResource( hModule, type, name,
321 MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), FALSE, TRUE );
324 /**********************************************************************
325 * FindResourceA (KERNEL32.@)
327 HRSRC WINAPI FindResourceA( HMODULE hModule, LPCSTR name, LPCSTR type )
329 return RES_FindResource( hModule, type, name,
330 MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), FALSE, FALSE );
333 /**********************************************************************
334 * FindResourceExA (KERNEL32.@)
336 HRSRC WINAPI FindResourceExA( HMODULE hModule,
337 LPCSTR type, LPCSTR name, WORD lang )
339 return RES_FindResource( hModule, type, name,
340 lang, FALSE, FALSE );
343 /**********************************************************************
344 * FindResourceExW (KERNEL32.@)
346 HRSRC WINAPI FindResourceExW( HMODULE hModule,
347 LPCWSTR type, LPCWSTR name, WORD lang )
349 return RES_FindResource( hModule, (LPCSTR)type, (LPCSTR)name,
350 lang, TRUE, FALSE );
353 /**********************************************************************
354 * FindResourceW (KERNEL32.@)
356 HRSRC WINAPI FindResourceW(HINSTANCE hModule, LPCWSTR name, LPCWSTR type)
358 return RES_FindResource( hModule, (LPCSTR)type, (LPCSTR)name,
359 MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), TRUE, FALSE );
362 /**********************************************************************
363 * LoadResource (KERNEL.61)
364 * LoadResource16 (KERNEL32.@)
366 HGLOBAL16 WINAPI LoadResource16( HMODULE16 hModule, HRSRC16 hRsrc )
368 return RES_LoadResource( hModule, hRsrc, TRUE );
371 /**********************************************************************
372 * LoadResource (KERNEL32.@)
374 HGLOBAL WINAPI LoadResource( HINSTANCE hModule, HRSRC hRsrc )
376 return RES_LoadResource( hModule, hRsrc, FALSE );
379 /**********************************************************************
380 * LockResource (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 (KERNEL32.@)
392 LPVOID WINAPI LockResource16( HGLOBAL16 handle )
394 return MapSL( WIN16_LockResource16(handle) );
397 /**********************************************************************
398 * LockResource (KERNEL32.@)
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 typedef WORD WINAPI (*pDestroyIcon32Proc)( HGLOBAL16 handle, UINT16 flags );
414 /**********************************************************************
415 * FreeResource (KERNEL.63)
416 * FreeResource16 (KERNEL32.@)
418 BOOL16 WINAPI FreeResource16( HGLOBAL16 handle )
420 HGLOBAL retv = handle;
421 NE_MODULE *pModule = NE_GetPtr( FarGetOwner16( handle ) );
423 TRACE("(%04x)\n", handle );
425 /* Try NE resource first */
426 retv = NE_FreeResource( pModule, handle );
428 /* If this failed, call USER.DestroyIcon32; this will check
429 whether it is a shared cursor/icon; if not it will call
430 GlobalFree16() */
431 if ( retv )
433 pDestroyIcon32Proc proc;
434 HMODULE user = GetModuleHandleA( "user32.dll" );
436 if (user && (proc = (pDestroyIcon32Proc)GetProcAddress( user, "DestroyIcon32" )))
437 retv = proc( handle, CID_RESOURCE );
438 else
439 retv = GlobalFree16( handle );
441 return (BOOL)retv;
444 /**********************************************************************
445 * FreeResource (KERNEL32.@)
447 BOOL WINAPI FreeResource( HGLOBAL handle )
449 if (HIWORD(handle)) return 0; /* 32-bit memory handle: nothing to do */
451 return FreeResource16( handle );
454 /**********************************************************************
455 * SizeofResource (KERNEL.65)
456 * SizeofResource16 (KERNEL32.@)
458 DWORD WINAPI SizeofResource16( HMODULE16 hModule, HRSRC16 hRsrc )
460 return RES_SizeofResource( hModule, hRsrc, TRUE );
463 /**********************************************************************
464 * SizeofResource (KERNEL32.@)
466 DWORD WINAPI SizeofResource( HINSTANCE hModule, HRSRC hRsrc )
468 return RES_SizeofResource( hModule, hRsrc, FALSE );