Set fragments for wave input to 7 x 1024 byte.
[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 "callback.h"
21 #include "cursoricon.h"
22 #include "task.h"
23 #include "module.h"
24 #include "file.h"
25 #include "debugtools.h"
26 #include "winerror.h"
27 #include "winnls.h"
28
29 DEFAULT_DEBUG_CHANNEL(resource);
30
31 #define HRSRC_MAP_BLOCKSIZE 16
32
33 typedef struct _HRSRC_ELEM
34 {
35     HANDLE hRsrc;
36     WORD     type;
37 } HRSRC_ELEM;
38
39 typedef struct _HRSRC_MAP
40 {
41     int nAlloc;
42     int nUsed;
43     HRSRC_ELEM *elem;
44 } HRSRC_MAP;
45
46 /**********************************************************************
47  *          MapHRsrc32To16
48  */
49 static HRSRC16 MapHRsrc32To16( NE_MODULE *pModule, HANDLE hRsrc32, WORD type )
50 {
51     HRSRC_MAP *map = (HRSRC_MAP *)pModule->hRsrcMap;
52     HRSRC_ELEM *newElem;
53     int i;
54
55     /* On first call, initialize HRSRC map */
56     if ( !map )
57     {
58         if ( !(map = (HRSRC_MAP *)HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 
59                                              sizeof(HRSRC_MAP) ) ) )
60         {
61             ERR("Cannot allocate HRSRC map\n" );
62             return 0;
63         }
64         pModule->hRsrcMap = (LPVOID)map;
65     }
66
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);
71
72     /* If no space left, grow table */
73     if ( map->nUsed == map->nAlloc )
74     {
75         if ( !(newElem = (HRSRC_ELEM *)HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 
76                                                     map->elem, 
77                                                     (map->nAlloc + HRSRC_MAP_BLOCKSIZE) 
78                                                     * sizeof(HRSRC_ELEM) ) ))
79         {
80             ERR("Cannot grow HRSRC map\n" );
81             return 0;
82         }
83         map->elem = newElem;
84         map->nAlloc += HRSRC_MAP_BLOCKSIZE;
85     }
86
87     /* Add HRSRC32 to table */
88     map->elem[map->nUsed].hRsrc = hRsrc32;
89     map->elem[map->nUsed].type  = type;
90     map->nUsed++;
91
92     return (HRSRC16)map->nUsed;
93 }
94
95 /**********************************************************************
96  *          MapHRsrc16To32
97  */
98 static HANDLE MapHRsrc16To32( NE_MODULE *pModule, HRSRC16 hRsrc16 )
99 {
100     HRSRC_MAP *map = (HRSRC_MAP *)pModule->hRsrcMap;
101     if ( !map || !hRsrc16 || (int)hRsrc16 > map->nUsed ) return 0;
102
103     return map->elem[(int)hRsrc16-1].hRsrc;
104 }
105
106 /**********************************************************************
107  *          MapHRsrc16ToType
108  */
109 static WORD MapHRsrc16ToType( NE_MODULE *pModule, HRSRC16 hRsrc16 )
110 {
111     HRSRC_MAP *map = (HRSRC_MAP *)pModule->hRsrcMap;
112     if ( !map || !hRsrc16 || (int)hRsrc16 > map->nUsed ) return 0;
113
114     return map->elem[(int)hRsrc16-1].type;
115 }
116
117
118 /* filter for page-fault exceptions */
119 static WINE_EXCEPTION_FILTER(page_fault)
120 {
121     if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
122         return EXCEPTION_EXECUTE_HANDLER;
123     return EXCEPTION_CONTINUE_SEARCH;
124 }
125
126 static HRSRC RES_FindResource2( HMODULE hModule, LPCSTR type,
127                                 LPCSTR name, WORD lang, 
128                                 BOOL bUnicode, BOOL bRet16 )
129 {
130     HRSRC hRsrc = 0;
131     
132     TRACE("(%08x, %08x%s, %08x%s, %04x, %s, %s)\n",
133           hModule,
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)) : "",
136           lang,
137           bUnicode? "W"  : "A",
138           bRet16?   "NE" : "PE" );
139
140     if (!HIWORD(hModule))
141     {
142         HMODULE16 hMod16   = MapHModuleLS( hModule );
143         NE_MODULE *pModule = NE_GetPtr( hMod16 );
144         if (!pModule) return 0;
145         if (!pModule->module32)
146         {
147             /* 16-bit NE module */
148             LPSTR typeStr, nameStr;
149             
150             if ( HIWORD( type ) && bUnicode )
151                 typeStr = HEAP_strdupWtoA( GetProcessHeap(), 0, (LPCWSTR)type );
152             else
153                 typeStr = (LPSTR)type;
154             if ( HIWORD( name ) && bUnicode )
155                 nameStr = HEAP_strdupWtoA( GetProcessHeap(), 0, (LPCWSTR)name );
156             else
157                 nameStr = (LPSTR)name;
158             
159             hRsrc = NE_FindResource( pModule, nameStr, typeStr );
160             
161             if ( HIWORD( type ) && bUnicode ) 
162                 HeapFree( GetProcessHeap(), 0, typeStr );
163             if ( HIWORD( name ) && bUnicode ) 
164                 HeapFree( GetProcessHeap(), 0, nameStr );
165             
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 */
168         }
169         else
170         {
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 */
174             if ( bRet16 )
175                 hRsrc = MapHRsrc32To16( pModule, hRsrc, 
176                                         HIWORD( type )? 0 : LOWORD( type ) );
177         }
178     }
179     else
180     {
181         /* 32-bit PE module */
182         LPWSTR typeStr, nameStr;
183             
184         if ( HIWORD( type ) && !bUnicode )
185             typeStr = HEAP_strdupAtoW( GetProcessHeap(), 0, type );
186         else
187             typeStr = (LPWSTR)type;
188         if ( HIWORD( name ) && !bUnicode )
189             nameStr = HEAP_strdupAtoW( GetProcessHeap(), 0, name );
190         else
191             nameStr = (LPWSTR)name;
192
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 );
199         else
200             hRsrc = PE_FindResourceExW( hModule, nameStr, typeStr, lang );
201             
202         if ( HIWORD( type ) && !bUnicode ) 
203             HeapFree( GetProcessHeap(), 0, typeStr );
204         if ( HIWORD( name ) && !bUnicode ) 
205             HeapFree( GetProcessHeap(), 0, nameStr );
206     }
207     return hRsrc;
208 }
209
210 /**********************************************************************
211  *          RES_FindResource
212  */
213
214 static HRSRC RES_FindResource( HMODULE hModule, LPCSTR type,
215                                LPCSTR name, WORD lang, 
216                                BOOL bUnicode, BOOL bRet16 )
217 {
218     HRSRC hRsrc;
219     __TRY
220     {
221         hRsrc = RES_FindResource2(hModule, type, name, lang, bUnicode, bRet16);
222     }
223     __EXCEPT(page_fault)
224     {
225         WARN("page fault\n");
226         SetLastError(ERROR_INVALID_PARAMETER);
227         return 0;
228     }
229     __ENDTRY
230     return hRsrc;
231 }
232
233 /**********************************************************************
234  *          RES_SizeofResource
235  */
236 static DWORD RES_SizeofResource( HMODULE hModule, HRSRC hRsrc, BOOL bRet16 )
237 {
238     if (!hRsrc) return 0;
239
240     TRACE("(%08x, %08x, %s)\n", hModule, hRsrc, bRet16? "NE" : "PE" );
241
242     if (!HIWORD(hModule))
243     {
244         HMODULE16 hMod16   = MapHModuleLS( hModule );
245         NE_MODULE *pModule = NE_GetPtr( hMod16 );
246         if (!pModule) return 0;
247
248         if (!pModule->module32)  /* 16-bit NE module */
249         {
250             /* If we got a 32-bit hRsrc, we don't need to convert it */
251             return NE_SizeofResource( pModule, hRsrc );
252         }
253
254         /* If we got a 16-bit hRsrc, convert it */
255         if (!HIWORD(hRsrc)) hRsrc = MapHRsrc16To32( pModule, hRsrc );
256     }
257
258     /* 32-bit PE module */
259     return PE_SizeofResource( hRsrc );
260 }
261
262 /**********************************************************************
263  *          RES_LoadResource
264  */
265 static HGLOBAL RES_LoadResource( HMODULE hModule, HRSRC hRsrc, BOOL bRet16 )
266 {
267     HGLOBAL hMem = 0;
268
269     TRACE("(%08x, %08x, %s)\n", hModule, hRsrc, bRet16? "NE" : "PE" );
270
271     if (!hRsrc) return 0;
272
273     if (!HIWORD(hModule))
274     {
275         HMODULE16 hMod16   = MapHModuleLS( hModule );
276         NE_MODULE *pModule = NE_GetPtr( hMod16 );
277         if (!pModule) return 0;
278         if (!pModule->module32)
279         {
280             /* 16-bit NE module */
281
282             /* If we got a 32-bit hRsrc, we don't need to convert it */
283             hMem = NE_LoadResource( pModule, hRsrc );
284
285             /* If we are to return a 32-bit resource, we should probably
286                convert it but we don't for now.  FIXME !!! */
287             return hMem;
288         }
289         else
290         {
291             /* If we got a 16-bit hRsrc, convert it */
292             HRSRC hRsrc32 = HIWORD(hRsrc)? hRsrc : MapHRsrc16To32( pModule, hRsrc );
293
294             hMem = PE_LoadResource( pModule->module32, hRsrc32 );
295
296             /* If we need to return a 16-bit resource, convert it */
297             if ( bRet16 )
298             {
299                 WORD type   = MapHRsrc16ToType( pModule, hRsrc );
300                 DWORD size  = SizeofResource( hModule, hRsrc );
301                 LPVOID bits = LockResource( hMem );
302
303                 hMem = NE_LoadPEResource( pModule, type, bits, size );
304             }
305         }
306     }
307     else
308     {
309         /* 32-bit PE module */
310         hMem = PE_LoadResource( hModule, hRsrc );
311     }
312
313     return hMem;
314 }
315
316 /**********************************************************************
317  *          FindResource16   (KERNEL.60)
318  */
319 HRSRC16 WINAPI FindResource16( HMODULE16 hModule, LPCSTR name, LPCSTR type )
320 {
321     return RES_FindResource( hModule, type, name,
322                     MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), FALSE, TRUE );
323 }
324
325 /**********************************************************************
326  *          FindResourceA    (KERNEL32.128)
327  */
328 HRSRC WINAPI FindResourceA( HMODULE hModule, LPCSTR name, LPCSTR type )
329 {
330     return RES_FindResource( hModule, type, name, 
331                     MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), FALSE, FALSE );
332 }
333
334 /**********************************************************************
335  *          FindResourceExA  (KERNEL32.129)
336  */
337 HRSRC WINAPI FindResourceExA( HMODULE hModule, 
338                                LPCSTR type, LPCSTR name, WORD lang )
339 {
340     return RES_FindResource( hModule, type, name, 
341                              lang, FALSE, FALSE );
342 }
343
344 /**********************************************************************
345  *          FindResourceExW  (KERNEL32.130)
346  */
347 HRSRC WINAPI FindResourceExW( HMODULE hModule, 
348                               LPCWSTR type, LPCWSTR name, WORD lang )
349 {
350     return RES_FindResource( hModule, (LPCSTR)type, (LPCSTR)name, 
351                              lang, TRUE, FALSE );
352 }
353
354 /**********************************************************************
355  *          FindResourceW    (KERNEL32.131)
356  */
357 HRSRC WINAPI FindResourceW(HINSTANCE hModule, LPCWSTR name, LPCWSTR type)
358 {
359     return RES_FindResource( hModule, (LPCSTR)type, (LPCSTR)name, 
360                     MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), TRUE, FALSE );
361 }
362
363 /**********************************************************************
364  *          LoadResource16   (KERNEL.61)
365  */
366 HGLOBAL16 WINAPI LoadResource16( HMODULE16 hModule, HRSRC16 hRsrc )
367 {
368     return RES_LoadResource( hModule, hRsrc, TRUE );
369 }
370
371 /**********************************************************************
372  *          LoadResource     (KERNEL32.370)
373  */
374 HGLOBAL WINAPI LoadResource( HINSTANCE hModule, HRSRC hRsrc )
375 {
376     return RES_LoadResource( hModule, hRsrc, FALSE );
377 }
378
379 /**********************************************************************
380  *          LockResource16   (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   (KERNEL but also exported from KERNEL32 in Wine)
391  */
392 LPVOID WINAPI LockResource16( HGLOBAL16 handle )
393 {
394     return MapSL( WIN16_LockResource16(handle) );
395 }
396
397 /**********************************************************************
398  *          LockResource     (KERNEL32.384)
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 /**********************************************************************
412  *          FreeResource16   (KERNEL.63)
413  */
414 BOOL16 WINAPI FreeResource16( HGLOBAL16 handle )
415 {
416     HGLOBAL retv = handle;
417     NE_MODULE *pModule = NE_GetPtr( FarGetOwner16( handle ) );
418
419     TRACE("(%04x)\n", handle );
420
421     /* Try NE resource first */
422     retv = NE_FreeResource( pModule, handle );
423
424     /* If this failed, call USER.DestroyIcon32; this will check
425        whether it is a shared cursor/icon; if not it will call
426        GlobalFree16() */
427     if ( retv )
428     {
429         if ( Callout.DestroyIcon32 )
430             retv = Callout.DestroyIcon32( handle, CID_RESOURCE );
431         else
432             retv = GlobalFree16( handle );
433     }
434     return (BOOL)retv;
435 }
436
437 /**********************************************************************
438  *          FreeResource     (KERNEL32.145)
439  */
440 BOOL WINAPI FreeResource( HGLOBAL handle )
441 {
442     if (HIWORD(handle)) return 0; /* 32-bit memory handle: nothing to do */
443
444     return FreeResource16( handle );
445 }
446
447 /**********************************************************************
448  *          SizeofResource16 (KERNEL.65)
449  */
450 DWORD WINAPI SizeofResource16( HMODULE16 hModule, HRSRC16 hRsrc )
451 {
452     return RES_SizeofResource( hModule, hRsrc, TRUE );
453 }
454
455 /**********************************************************************
456  *          SizeofResource   (KERNEL32.522)
457  */
458 DWORD WINAPI SizeofResource( HINSTANCE hModule, HRSRC hRsrc )
459 {
460     return RES_SizeofResource( hModule, hRsrc, FALSE );
461 }