Implemented _CheckNotSysLevel.
[wine/multimedia.git] / loader / resource.c
blobb3d0c1ebf790176301f724e76d23a0e764b87829
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 "callback.h"
21 #include "cursoricon.h"
22 #include "module.h"
23 #include "file.h"
24 #include "debugtools.h"
25 #include "winerror.h"
26 #include "winnls.h"
28 DEFAULT_DEBUG_CHANNEL(resource);
30 #define HRSRC_MAP_BLOCKSIZE 16
32 typedef struct _HRSRC_ELEM
34 HANDLE hRsrc;
35 WORD type;
36 } HRSRC_ELEM;
38 typedef struct _HRSRC_MAP
40 int nAlloc;
41 int nUsed;
42 HRSRC_ELEM *elem;
43 } HRSRC_MAP;
45 /**********************************************************************
46 * MapHRsrc32To16
48 static HRSRC16 MapHRsrc32To16( NE_MODULE *pModule, HANDLE hRsrc32, WORD type )
50 HRSRC_MAP *map = (HRSRC_MAP *)pModule->hRsrcMap;
51 HRSRC_ELEM *newElem;
52 int i;
54 /* On first call, initialize HRSRC map */
55 if ( !map )
57 if ( !(map = (HRSRC_MAP *)HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
58 sizeof(HRSRC_MAP) ) ) )
60 ERR("Cannot allocate HRSRC map\n" );
61 return 0;
63 pModule->hRsrcMap = (LPVOID)map;
66 /* Check whether HRSRC32 already in map */
67 for ( i = 0; i < map->nUsed; i++ )
68 if ( map->elem[i].hRsrc == hRsrc32 )
69 return (HRSRC16)(i + 1);
71 /* If no space left, grow table */
72 if ( map->nUsed == map->nAlloc )
74 if ( !(newElem = (HRSRC_ELEM *)HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
75 map->elem,
76 (map->nAlloc + HRSRC_MAP_BLOCKSIZE)
77 * sizeof(HRSRC_ELEM) ) ))
79 ERR("Cannot grow HRSRC map\n" );
80 return 0;
82 map->elem = newElem;
83 map->nAlloc += HRSRC_MAP_BLOCKSIZE;
86 /* Add HRSRC32 to table */
87 map->elem[map->nUsed].hRsrc = hRsrc32;
88 map->elem[map->nUsed].type = type;
89 map->nUsed++;
91 return (HRSRC16)map->nUsed;
94 /**********************************************************************
95 * MapHRsrc16To32
97 static HANDLE MapHRsrc16To32( NE_MODULE *pModule, HRSRC16 hRsrc16 )
99 HRSRC_MAP *map = (HRSRC_MAP *)pModule->hRsrcMap;
100 if ( !map || !hRsrc16 || (int)hRsrc16 > map->nUsed ) return 0;
102 return map->elem[(int)hRsrc16-1].hRsrc;
105 /**********************************************************************
106 * MapHRsrc16ToType
108 static WORD MapHRsrc16ToType( NE_MODULE *pModule, HRSRC16 hRsrc16 )
110 HRSRC_MAP *map = (HRSRC_MAP *)pModule->hRsrcMap;
111 if ( !map || !hRsrc16 || (int)hRsrc16 > map->nUsed ) return 0;
113 return map->elem[(int)hRsrc16-1].type;
117 /* filter for page-fault exceptions */
118 static WINE_EXCEPTION_FILTER(page_fault)
120 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
121 return EXCEPTION_EXECUTE_HANDLER;
122 return EXCEPTION_CONTINUE_SEARCH;
125 static HRSRC RES_FindResource2( HMODULE hModule, LPCSTR type,
126 LPCSTR name, WORD lang,
127 BOOL bUnicode, BOOL bRet16 )
129 HRSRC hRsrc = 0;
131 TRACE("(%08x, %08x%s, %08x%s, %04x, %s, %s)\n",
132 hModule,
133 (UINT)type, HIWORD(type)? (bUnicode? debugstr_w((LPWSTR)type) : debugstr_a(type)) : "",
134 (UINT)name, HIWORD(name)? (bUnicode? debugstr_w((LPWSTR)name) : debugstr_a(name)) : "",
135 lang,
136 bUnicode? "W" : "A",
137 bRet16? "NE" : "PE" );
139 if (!HIWORD(hModule))
141 HMODULE16 hMod16 = MapHModuleLS( hModule );
142 NE_MODULE *pModule = NE_GetPtr( hMod16 );
143 if (!pModule) return 0;
144 if (!pModule->module32)
146 /* 16-bit NE module */
147 LPSTR typeStr, nameStr;
149 if ( HIWORD( type ) && bUnicode )
150 typeStr = HEAP_strdupWtoA( GetProcessHeap(), 0, (LPCWSTR)type );
151 else
152 typeStr = (LPSTR)type;
153 if ( HIWORD( name ) && bUnicode )
154 nameStr = HEAP_strdupWtoA( GetProcessHeap(), 0, (LPCWSTR)name );
155 else
156 nameStr = (LPSTR)name;
158 hRsrc = NE_FindResource( pModule, nameStr, typeStr );
160 if ( HIWORD( type ) && bUnicode )
161 HeapFree( GetProcessHeap(), 0, typeStr );
162 if ( HIWORD( name ) && bUnicode )
163 HeapFree( GetProcessHeap(), 0, nameStr );
165 /* If we need to return 32-bit HRSRC, no conversion is necessary,
166 we simply use the 16-bit HRSRC as 32-bit HRSRC */
168 else
170 /* 32-bit PE module */
171 hRsrc = RES_FindResource2( pModule->module32, type, name, lang, bUnicode, FALSE );
172 /* If we need to return 16-bit HRSRC, perform conversion */
173 if ( bRet16 )
174 hRsrc = MapHRsrc32To16( pModule, hRsrc,
175 HIWORD( type )? 0 : LOWORD( type ) );
178 else
180 /* 32-bit PE module */
181 LPWSTR typeStr, nameStr;
183 if ( HIWORD( type ) && !bUnicode )
184 typeStr = HEAP_strdupAtoW( GetProcessHeap(), 0, type );
185 else
186 typeStr = (LPWSTR)type;
187 if ( HIWORD( name ) && !bUnicode )
188 nameStr = HEAP_strdupAtoW( GetProcessHeap(), 0, name );
189 else
190 nameStr = (LPWSTR)name;
192 /* Here is the real difference between FindResouce and FindResourceEx */
193 if(lang == MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL) ||
194 lang == MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT) ||
195 lang == MAKELANGID(LANG_NEUTRAL, SUBLANG_SYS_DEFAULT) ||
196 lang == MAKELANGID(LANG_NEUTRAL, 3)) /* FIXME: real name? */
197 hRsrc = PE_FindResourceW( hModule, nameStr, typeStr );
198 else
199 hRsrc = PE_FindResourceExW( hModule, nameStr, typeStr, lang );
201 if ( HIWORD( type ) && !bUnicode )
202 HeapFree( GetProcessHeap(), 0, typeStr );
203 if ( HIWORD( name ) && !bUnicode )
204 HeapFree( GetProcessHeap(), 0, nameStr );
206 return hRsrc;
209 /**********************************************************************
210 * RES_FindResource
213 static HRSRC RES_FindResource( HMODULE hModule, LPCSTR type,
214 LPCSTR name, WORD lang,
215 BOOL bUnicode, BOOL bRet16 )
217 HRSRC hRsrc;
218 __TRY
220 hRsrc = RES_FindResource2(hModule, type, name, lang, bUnicode, bRet16);
222 __EXCEPT(page_fault)
224 WARN("page fault\n");
225 SetLastError(ERROR_INVALID_PARAMETER);
226 return 0;
228 __ENDTRY
229 return hRsrc;
232 /**********************************************************************
233 * RES_SizeofResource
235 static DWORD RES_SizeofResource( HMODULE hModule, HRSRC hRsrc, BOOL bRet16 )
237 if (!hRsrc) return 0;
239 TRACE("(%08x, %08x, %s)\n", hModule, hRsrc, bRet16? "NE" : "PE" );
241 if (!HIWORD(hModule))
243 HMODULE16 hMod16 = MapHModuleLS( hModule );
244 NE_MODULE *pModule = NE_GetPtr( hMod16 );
245 if (!pModule) return 0;
247 if (!pModule->module32) /* 16-bit NE module */
249 /* If we got a 32-bit hRsrc, we don't need to convert it */
250 return NE_SizeofResource( pModule, hRsrc );
253 /* If we got a 16-bit hRsrc, convert it */
254 if (!HIWORD(hRsrc)) hRsrc = MapHRsrc16To32( pModule, hRsrc );
257 /* 32-bit PE module */
258 return PE_SizeofResource( hRsrc );
261 /**********************************************************************
262 * RES_LoadResource
264 static HGLOBAL RES_LoadResource( HMODULE hModule, HRSRC hRsrc, BOOL bRet16 )
266 HGLOBAL hMem = 0;
268 TRACE("(%08x, %08x, %s)\n", hModule, hRsrc, bRet16? "NE" : "PE" );
270 if (!hRsrc) return 0;
272 if (!HIWORD(hModule))
274 HMODULE16 hMod16 = MapHModuleLS( hModule );
275 NE_MODULE *pModule = NE_GetPtr( hMod16 );
276 if (!pModule) return 0;
277 if (!pModule->module32)
279 /* 16-bit NE module */
281 /* If we got a 32-bit hRsrc, we don't need to convert it */
282 hMem = NE_LoadResource( pModule, hRsrc );
284 /* If we are to return a 32-bit resource, we should probably
285 convert it but we don't for now. FIXME !!! */
286 return hMem;
288 else
290 /* If we got a 16-bit hRsrc, convert it */
291 HRSRC hRsrc32 = HIWORD(hRsrc)? hRsrc : MapHRsrc16To32( pModule, hRsrc );
293 hMem = PE_LoadResource( pModule->module32, hRsrc32 );
295 /* If we need to return a 16-bit resource, convert it */
296 if ( bRet16 )
298 WORD type = MapHRsrc16ToType( pModule, hRsrc );
299 DWORD size = SizeofResource( hModule, hRsrc );
300 LPVOID bits = LockResource( hMem );
302 hMem = NE_LoadPEResource( pModule, type, bits, size );
306 else
308 /* 32-bit PE module */
309 hMem = PE_LoadResource( hModule, hRsrc );
312 return hMem;
315 /**********************************************************************
316 * FindResource (KERNEL.60)
317 * FindResource16 (KERNEL32.@)
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.@)
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.@)
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.@)
347 HRSRC WINAPI FindResourceExW( HMODULE hModule,
348 LPCWSTR type, LPCWSTR name, WORD lang )
350 return RES_FindResource( hModule, (LPCSTR)type, (LPCSTR)name,
351 lang, TRUE, FALSE );
354 /**********************************************************************
355 * FindResourceW (KERNEL32.@)
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 * LoadResource (KERNEL.61)
365 * LoadResource16 (KERNEL32.@)
367 HGLOBAL16 WINAPI LoadResource16( HMODULE16 hModule, HRSRC16 hRsrc )
369 return RES_LoadResource( hModule, hRsrc, TRUE );
372 /**********************************************************************
373 * LoadResource (KERNEL32.@)
375 HGLOBAL WINAPI LoadResource( HINSTANCE hModule, HRSRC hRsrc )
377 return RES_LoadResource( hModule, hRsrc, FALSE );
380 /**********************************************************************
381 * LockResource (KERNEL.62)
383 SEGPTR WINAPI WIN16_LockResource16( HGLOBAL16 handle )
385 TRACE("(%04x)\n", handle );
386 /* May need to reload the resource if discarded */
387 return K32WOWGlobalLock16( handle );
390 /**********************************************************************
391 * LockResource16 (KERNEL32.@)
393 LPVOID WINAPI LockResource16( HGLOBAL16 handle )
395 return MapSL( WIN16_LockResource16(handle) );
398 /**********************************************************************
399 * LockResource (KERNEL32.@)
401 LPVOID WINAPI LockResource( HGLOBAL handle )
403 TRACE("(%08x)\n", handle );
405 if (HIWORD( handle )) /* 32-bit memory handle */
406 return (LPVOID)handle;
408 /* 16-bit memory handle */
409 return LockResource16( handle );
412 /**********************************************************************
413 * FreeResource (KERNEL.63)
414 * FreeResource16 (KERNEL32.@)
416 BOOL16 WINAPI FreeResource16( HGLOBAL16 handle )
418 HGLOBAL retv = handle;
419 NE_MODULE *pModule = NE_GetPtr( FarGetOwner16( handle ) );
421 TRACE("(%04x)\n", handle );
423 /* Try NE resource first */
424 retv = NE_FreeResource( pModule, handle );
426 /* If this failed, call USER.DestroyIcon32; this will check
427 whether it is a shared cursor/icon; if not it will call
428 GlobalFree16() */
429 if ( retv )
431 if ( Callout.DestroyIcon32 )
432 retv = Callout.DestroyIcon32( handle, CID_RESOURCE );
433 else
434 retv = GlobalFree16( handle );
436 return (BOOL)retv;
439 /**********************************************************************
440 * FreeResource (KERNEL32.@)
442 BOOL WINAPI FreeResource( HGLOBAL handle )
444 if (HIWORD(handle)) return 0; /* 32-bit memory handle: nothing to do */
446 return FreeResource16( handle );
449 /**********************************************************************
450 * SizeofResource (KERNEL.65)
451 * SizeofResource16 (KERNEL32.@)
453 DWORD WINAPI SizeofResource16( HMODULE16 hModule, HRSRC16 hRsrc )
455 return RES_SizeofResource( hModule, hRsrc, TRUE );
458 /**********************************************************************
459 * SizeofResource (KERNEL32.@)
461 DWORD WINAPI SizeofResource( HINSTANCE hModule, HRSRC hRsrc )
463 return RES_SizeofResource( hModule, hRsrc, FALSE );