Removed unnecessary includes.
[wine] / loader / resource.c
1 /*
2  * Resources
3  *
4  * Copyright 1993 Robert J. Amstadt
5  * Copyright 1995 Alexandre Julliard
6  */
7
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"
26
27 DEFAULT_DEBUG_CHANNEL(resource);
28
29 #define HRSRC_MAP_BLOCKSIZE 16
30
31 typedef struct _HRSRC_ELEM
32 {
33     HANDLE hRsrc;
34     WORD     type;
35 } HRSRC_ELEM;
36
37 typedef struct _HRSRC_MAP
38 {
39     int nAlloc;
40     int nUsed;
41     HRSRC_ELEM *elem;
42 } HRSRC_MAP;
43
44 /**********************************************************************
45  *          MapHRsrc32To16
46  */
47 static HRSRC16 MapHRsrc32To16( NE_MODULE *pModule, HANDLE hRsrc32, WORD type )
48 {
49     HRSRC_MAP *map = (HRSRC_MAP *)pModule->hRsrcMap;
50     HRSRC_ELEM *newElem;
51     int i;
52
53     /* On first call, initialize HRSRC map */
54     if ( !map )
55     {
56         if ( !(map = (HRSRC_MAP *)HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 
57                                              sizeof(HRSRC_MAP) ) ) )
58         {
59             ERR("Cannot allocate HRSRC map\n" );
60             return 0;
61         }
62         pModule->hRsrcMap = (LPVOID)map;
63     }
64
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);
69
70     /* If no space left, grow table */
71     if ( map->nUsed == map->nAlloc )
72     {
73         if ( !(newElem = (HRSRC_ELEM *)HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 
74                                                     map->elem, 
75                                                     (map->nAlloc + HRSRC_MAP_BLOCKSIZE) 
76                                                     * sizeof(HRSRC_ELEM) ) ))
77         {
78             ERR("Cannot grow HRSRC map\n" );
79             return 0;
80         }
81         map->elem = newElem;
82         map->nAlloc += HRSRC_MAP_BLOCKSIZE;
83     }
84
85     /* Add HRSRC32 to table */
86     map->elem[map->nUsed].hRsrc = hRsrc32;
87     map->elem[map->nUsed].type  = type;
88     map->nUsed++;
89
90     return (HRSRC16)map->nUsed;
91 }
92
93 /**********************************************************************
94  *          MapHRsrc16To32
95  */
96 static HANDLE MapHRsrc16To32( NE_MODULE *pModule, HRSRC16 hRsrc16 )
97 {
98     HRSRC_MAP *map = (HRSRC_MAP *)pModule->hRsrcMap;
99     if ( !map || !hRsrc16 || (int)hRsrc16 > map->nUsed ) return 0;
100
101     return map->elem[(int)hRsrc16-1].hRsrc;
102 }
103
104 /**********************************************************************
105  *          MapHRsrc16ToType
106  */
107 static WORD MapHRsrc16ToType( NE_MODULE *pModule, HRSRC16 hRsrc16 )
108 {
109     HRSRC_MAP *map = (HRSRC_MAP *)pModule->hRsrcMap;
110     if ( !map || !hRsrc16 || (int)hRsrc16 > map->nUsed ) return 0;
111
112     return map->elem[(int)hRsrc16-1].type;
113 }
114
115
116 /* filter for page-fault exceptions */
117 static WINE_EXCEPTION_FILTER(page_fault)
118 {
119     if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
120         return EXCEPTION_EXECUTE_HANDLER;
121     return EXCEPTION_CONTINUE_SEARCH;
122 }
123
124 static HRSRC RES_FindResource2( HMODULE hModule, LPCSTR type,
125                                 LPCSTR name, WORD lang, 
126                                 BOOL bUnicode, BOOL bRet16 )
127 {
128     HRSRC hRsrc = 0;
129     
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" );
137
138     if (!HIWORD(hModule))
139     {
140         HMODULE16 hMod16   = MapHModuleLS( hModule );
141         NE_MODULE *pModule = NE_GetPtr( hMod16 );
142         if (!pModule) return 0;
143         if (!pModule->module32)
144         {
145             /* 16-bit NE module */
146             LPSTR typeStr, nameStr;
147             
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;
156             
157             hRsrc = NE_FindResource( pModule, nameStr, typeStr );
158             
159             if ( HIWORD( type ) && bUnicode ) 
160                 HeapFree( GetProcessHeap(), 0, typeStr );
161             if ( HIWORD( name ) && bUnicode ) 
162                 HeapFree( GetProcessHeap(), 0, nameStr );
163             
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 */
166         }
167         else
168         {
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 ) );
175         }
176     }
177     else
178     {
179         /* 32-bit PE module */
180         LPWSTR typeStr, nameStr;
181             
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;
190
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 );
199             
200         if ( HIWORD( type ) && !bUnicode ) 
201             HeapFree( GetProcessHeap(), 0, typeStr );
202         if ( HIWORD( name ) && !bUnicode ) 
203             HeapFree( GetProcessHeap(), 0, nameStr );
204     }
205     return hRsrc;
206 }
207
208 /**********************************************************************
209  *          RES_FindResource
210  */
211
212 static HRSRC RES_FindResource( HMODULE hModule, LPCSTR type,
213                                LPCSTR name, WORD lang, 
214                                BOOL bUnicode, BOOL bRet16 )
215 {
216     HRSRC hRsrc;
217     __TRY
218     {
219         hRsrc = RES_FindResource2(hModule, type, name, lang, bUnicode, bRet16);
220     }
221     __EXCEPT(page_fault)
222     {
223         WARN("page fault\n");
224         SetLastError(ERROR_INVALID_PARAMETER);
225         return 0;
226     }
227     __ENDTRY
228     return hRsrc;
229 }
230
231 /**********************************************************************
232  *          RES_SizeofResource
233  */
234 static DWORD RES_SizeofResource( HMODULE hModule, HRSRC hRsrc, BOOL bRet16 )
235 {
236     if (!hRsrc) return 0;
237
238     TRACE("(%08x, %08x, %s)\n", hModule, hRsrc, bRet16? "NE" : "PE" );
239
240     if (!HIWORD(hModule))
241     {
242         HMODULE16 hMod16   = MapHModuleLS( hModule );
243         NE_MODULE *pModule = NE_GetPtr( hMod16 );
244         if (!pModule) return 0;
245
246         if (!pModule->module32)  /* 16-bit NE module */
247         {
248             /* If we got a 32-bit hRsrc, we don't need to convert it */
249             return NE_SizeofResource( pModule, hRsrc );
250         }
251
252         /* If we got a 16-bit hRsrc, convert it */
253         if (!HIWORD(hRsrc)) hRsrc = MapHRsrc16To32( pModule, hRsrc );
254     }
255
256     /* 32-bit PE module */
257     return PE_SizeofResource( hRsrc );
258 }
259
260 /**********************************************************************
261  *          RES_LoadResource
262  */
263 static HGLOBAL RES_LoadResource( HMODULE hModule, HRSRC hRsrc, BOOL bRet16 )
264 {
265     HGLOBAL hMem = 0;
266
267     TRACE("(%08x, %08x, %s)\n", hModule, hRsrc, bRet16? "NE" : "PE" );
268
269     if (!hRsrc) return 0;
270
271     if (!HIWORD(hModule))
272     {
273         HMODULE16 hMod16   = MapHModuleLS( hModule );
274         NE_MODULE *pModule = NE_GetPtr( hMod16 );
275         if (!pModule) return 0;
276         if (!pModule->module32)
277         {
278             /* 16-bit NE module */
279
280             /* If we got a 32-bit hRsrc, we don't need to convert it */
281             hMem = NE_LoadResource( pModule, hRsrc );
282
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;
286         }
287         else
288         {
289             /* If we got a 16-bit hRsrc, convert it */
290             HRSRC hRsrc32 = HIWORD(hRsrc)? hRsrc : MapHRsrc16To32( pModule, hRsrc );
291
292             hMem = PE_LoadResource( pModule->module32, hRsrc32 );
293
294             /* If we need to return a 16-bit resource, convert it */
295             if ( bRet16 )
296             {
297                 WORD type   = MapHRsrc16ToType( pModule, hRsrc );
298                 DWORD size  = SizeofResource( hModule, hRsrc );
299                 LPVOID bits = LockResource( hMem );
300
301                 hMem = NE_LoadPEResource( pModule, type, bits, size );
302             }
303         }
304     }
305     else
306     {
307         /* 32-bit PE module */
308         hMem = PE_LoadResource( hModule, hRsrc );
309     }
310
311     return hMem;
312 }
313
314 /**********************************************************************
315  *          FindResource     (KERNEL.60)
316  *          FindResource16   (KERNEL32.@)
317  */
318 HRSRC16 WINAPI FindResource16( HMODULE16 hModule, LPCSTR name, LPCSTR type )
319 {
320     return RES_FindResource( hModule, type, name,
321                     MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), FALSE, TRUE );
322 }
323
324 /**********************************************************************
325  *          FindResourceA    (KERNEL32.@)
326  */
327 HRSRC WINAPI FindResourceA( HMODULE hModule, LPCSTR name, LPCSTR type )
328 {
329     return RES_FindResource( hModule, type, name, 
330                     MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), FALSE, FALSE );
331 }
332
333 /**********************************************************************
334  *          FindResourceExA  (KERNEL32.@)
335  */
336 HRSRC WINAPI FindResourceExA( HMODULE hModule, 
337                                LPCSTR type, LPCSTR name, WORD lang )
338 {
339     return RES_FindResource( hModule, type, name, 
340                              lang, FALSE, FALSE );
341 }
342
343 /**********************************************************************
344  *          FindResourceExW  (KERNEL32.@)
345  */
346 HRSRC WINAPI FindResourceExW( HMODULE hModule, 
347                               LPCWSTR type, LPCWSTR name, WORD lang )
348 {
349     return RES_FindResource( hModule, (LPCSTR)type, (LPCSTR)name, 
350                              lang, TRUE, FALSE );
351 }
352
353 /**********************************************************************
354  *          FindResourceW    (KERNEL32.@)
355  */
356 HRSRC WINAPI FindResourceW(HINSTANCE hModule, LPCWSTR name, LPCWSTR type)
357 {
358     return RES_FindResource( hModule, (LPCSTR)type, (LPCSTR)name, 
359                     MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), TRUE, FALSE );
360 }
361
362 /**********************************************************************
363  *          LoadResource     (KERNEL.61)
364  *          LoadResource16   (KERNEL32.@)
365  */
366 HGLOBAL16 WINAPI LoadResource16( HMODULE16 hModule, HRSRC16 hRsrc )
367 {
368     return RES_LoadResource( hModule, hRsrc, TRUE );
369 }
370
371 /**********************************************************************
372  *          LoadResource     (KERNEL32.@)
373  */
374 HGLOBAL WINAPI LoadResource( HINSTANCE hModule, HRSRC hRsrc )
375 {
376     return RES_LoadResource( hModule, hRsrc, FALSE );
377 }
378
379 /**********************************************************************
380  *          LockResource   (KERNEL.62)
381  */
382 SEGPTR WINAPI WIN16_LockResource16( HGLOBAL16 handle )
383 {
384     TRACE("(%04x)\n", handle );
385     /* May need to reload the resource if discarded */
386     return K32WOWGlobalLock16( handle );
387 }
388
389 /**********************************************************************
390  *          LockResource16 (KERNEL32.@)
391  */
392 LPVOID WINAPI LockResource16( HGLOBAL16 handle )
393 {
394     return MapSL( WIN16_LockResource16(handle) );
395 }
396
397 /**********************************************************************
398  *          LockResource     (KERNEL32.@)
399  */
400 LPVOID WINAPI LockResource( HGLOBAL handle )
401 {
402     TRACE("(%08x)\n", handle );
403
404     if (HIWORD( handle ))  /* 32-bit memory handle */
405         return (LPVOID)handle;
406
407     /* 16-bit memory handle */
408     return LockResource16( handle );
409 }
410
411 typedef WORD WINAPI (*pDestroyIcon32Proc)( HGLOBAL16 handle, UINT16 flags );
412
413
414 /**********************************************************************
415  *          FreeResource     (KERNEL.63)
416  *          FreeResource16   (KERNEL32.@)
417  */
418 BOOL16 WINAPI FreeResource16( HGLOBAL16 handle )
419 {
420     HGLOBAL retv = handle;
421     NE_MODULE *pModule = NE_GetPtr( FarGetOwner16( handle ) );
422
423     TRACE("(%04x)\n", handle );
424
425     /* Try NE resource first */
426     retv = NE_FreeResource( pModule, handle );
427
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 )
432     {
433         pDestroyIcon32Proc proc;
434         HMODULE user = GetModuleHandleA( "user32.dll" );
435
436         if (user && (proc = (pDestroyIcon32Proc)GetProcAddress( user, "DestroyIcon32" )))
437             retv = proc( handle, CID_RESOURCE );
438         else
439             retv = GlobalFree16( handle );
440     }
441     return (BOOL)retv;
442 }
443
444 /**********************************************************************
445  *          FreeResource     (KERNEL32.@)
446  */
447 BOOL WINAPI FreeResource( HGLOBAL handle )
448 {
449     if (HIWORD(handle)) return 0; /* 32-bit memory handle: nothing to do */
450
451     return FreeResource16( handle );
452 }
453
454 /**********************************************************************
455  *          SizeofResource   (KERNEL.65)
456  *          SizeofResource16 (KERNEL32.@)
457  */
458 DWORD WINAPI SizeofResource16( HMODULE16 hModule, HRSRC16 hRsrc )
459 {
460     return RES_SizeofResource( hModule, hRsrc, TRUE );
461 }
462
463 /**********************************************************************
464  *          SizeofResource   (KERNEL32.@)
465  */
466 DWORD WINAPI SizeofResource( HINSTANCE hModule, HRSRC hRsrc )
467 {
468     return RES_SizeofResource( hModule, hRsrc, FALSE );
469 }