Added include protection for unistd.h and sys/time.h.
[wine/hacks.git] / loader / resource.c
bloba4a5e610a78dfb9638b588d23aaf77db030ab69b
1 /*
2 * Resources
4 * Copyright 1993 Robert J. Amstadt
5 * Copyright 1995 Alexandre Julliard
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "config.h"
24 #include <assert.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <fcntl.h>
30 #ifdef HAVE_UNISTD_H
31 # include <unistd.h>
32 #endif
33 #include "windef.h"
34 #include "winbase.h"
35 #include "wine/winbase16.h"
36 #include "wine/exception.h"
37 #include "heap.h"
38 #include "cursoricon.h"
39 #include "module.h"
40 #include "file.h"
41 #include "wine/debug.h"
42 #include "winerror.h"
43 #include "winnls.h"
44 #include "msvcrt/excpt.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(resource);
48 #define HRSRC_MAP_BLOCKSIZE 16
50 typedef struct _HRSRC_ELEM
52 HANDLE hRsrc;
53 WORD type;
54 } HRSRC_ELEM;
56 typedef struct _HRSRC_MAP
58 int nAlloc;
59 int nUsed;
60 HRSRC_ELEM *elem;
61 } HRSRC_MAP;
63 /**********************************************************************
64 * MapHRsrc32To16
66 static HRSRC MapHRsrc32To16( NE_MODULE *pModule, HANDLE hRsrc32, WORD type )
68 HRSRC_MAP *map = (HRSRC_MAP *)pModule->hRsrcMap;
69 HRSRC_ELEM *newElem;
70 int i;
72 /* On first call, initialize HRSRC map */
73 if ( !map )
75 if ( !(map = (HRSRC_MAP *)HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
76 sizeof(HRSRC_MAP) ) ) )
78 ERR("Cannot allocate HRSRC map\n" );
79 return 0;
81 pModule->hRsrcMap = (LPVOID)map;
84 /* Check whether HRSRC32 already in map */
85 for ( i = 0; i < map->nUsed; i++ )
86 if ( map->elem[i].hRsrc == hRsrc32 )
87 return (HRSRC)(i + 1);
89 /* If no space left, grow table */
90 if ( map->nUsed == map->nAlloc )
92 if ( !(newElem = (HRSRC_ELEM *)HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
93 map->elem,
94 (map->nAlloc + HRSRC_MAP_BLOCKSIZE)
95 * sizeof(HRSRC_ELEM) ) ))
97 ERR("Cannot grow HRSRC map\n" );
98 return 0;
100 map->elem = newElem;
101 map->nAlloc += HRSRC_MAP_BLOCKSIZE;
104 /* Add HRSRC32 to table */
105 map->elem[map->nUsed].hRsrc = hRsrc32;
106 map->elem[map->nUsed].type = type;
107 map->nUsed++;
109 return (HRSRC)map->nUsed;
112 /**********************************************************************
113 * MapHRsrc16To32
115 static HRSRC MapHRsrc16To32( NE_MODULE *pModule, HRSRC hRsrc16 )
117 HRSRC_MAP *map = (HRSRC_MAP *)pModule->hRsrcMap;
118 if ( !map || !hRsrc16 || (int)hRsrc16 > map->nUsed ) return 0;
120 return map->elem[(int)hRsrc16-1].hRsrc;
123 /**********************************************************************
124 * MapHRsrc16ToType
126 static WORD MapHRsrc16ToType( NE_MODULE *pModule, HRSRC hRsrc16 )
128 HRSRC_MAP *map = (HRSRC_MAP *)pModule->hRsrcMap;
129 if ( !map || !hRsrc16 || (int)hRsrc16 > map->nUsed ) return 0;
131 return map->elem[(int)hRsrc16-1].type;
135 /* filter for page-fault exceptions */
136 static WINE_EXCEPTION_FILTER(page_fault)
138 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
139 return EXCEPTION_EXECUTE_HANDLER;
140 return EXCEPTION_CONTINUE_SEARCH;
143 static HRSRC RES_FindResource2( HMODULE hModule, LPCSTR type,
144 LPCSTR name, WORD lang,
145 BOOL bUnicode, BOOL bRet16 )
147 HRSRC hRsrc = 0;
149 TRACE("(%08x, %08x%s, %08x%s, %04x, %s, %s)\n",
150 hModule,
151 (UINT)type, HIWORD(type)? (bUnicode? debugstr_w((LPWSTR)type) : debugstr_a(type)) : "",
152 (UINT)name, HIWORD(name)? (bUnicode? debugstr_w((LPWSTR)name) : debugstr_a(name)) : "",
153 lang,
154 bUnicode? "W" : "A",
155 bRet16? "NE" : "PE" );
157 if (!HIWORD(hModule))
159 HMODULE16 hMod16 = MapHModuleLS( hModule );
160 NE_MODULE *pModule = NE_GetPtr( hMod16 );
161 if (!pModule) return 0;
162 if (!pModule->module32)
164 /* 16-bit NE module */
165 LPSTR typeStr, nameStr;
167 if ( HIWORD( type ) && bUnicode )
168 typeStr = HEAP_strdupWtoA( GetProcessHeap(), 0, (LPCWSTR)type );
169 else
170 typeStr = (LPSTR)type;
171 if ( HIWORD( name ) && bUnicode )
172 nameStr = HEAP_strdupWtoA( GetProcessHeap(), 0, (LPCWSTR)name );
173 else
174 nameStr = (LPSTR)name;
176 hRsrc = NE_FindResource( pModule, nameStr, typeStr );
178 if ( HIWORD( type ) && bUnicode )
179 HeapFree( GetProcessHeap(), 0, typeStr );
180 if ( HIWORD( name ) && bUnicode )
181 HeapFree( GetProcessHeap(), 0, nameStr );
183 /* If we need to return 32-bit HRSRC, no conversion is necessary,
184 we simply use the 16-bit HRSRC as 32-bit HRSRC */
186 else
188 /* 32-bit PE module */
189 hRsrc = RES_FindResource2( pModule->module32, type, name, lang, bUnicode, FALSE );
190 /* If we need to return 16-bit HRSRC, perform conversion */
191 if ( bRet16 )
192 hRsrc = MapHRsrc32To16( pModule, hRsrc,
193 HIWORD( type )? 0 : LOWORD( type ) );
196 else
198 /* 32-bit PE module */
199 LPWSTR typeStr, nameStr;
201 if ( HIWORD( type ) && !bUnicode )
202 typeStr = HEAP_strdupAtoW( GetProcessHeap(), 0, type );
203 else
204 typeStr = (LPWSTR)type;
205 if ( HIWORD( name ) && !bUnicode )
206 nameStr = HEAP_strdupAtoW( GetProcessHeap(), 0, name );
207 else
208 nameStr = (LPWSTR)name;
210 /* Here is the real difference between FindResouce and FindResourceEx */
211 if(lang == MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL) ||
212 lang == MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT) ||
213 lang == MAKELANGID(LANG_NEUTRAL, SUBLANG_SYS_DEFAULT) ||
214 lang == MAKELANGID(LANG_NEUTRAL, 3)) /* FIXME: real name? */
215 hRsrc = PE_FindResourceW( hModule, nameStr, typeStr );
216 else
217 hRsrc = PE_FindResourceExW( hModule, nameStr, typeStr, lang );
219 if ( HIWORD( type ) && !bUnicode )
220 HeapFree( GetProcessHeap(), 0, typeStr );
221 if ( HIWORD( name ) && !bUnicode )
222 HeapFree( GetProcessHeap(), 0, nameStr );
224 return hRsrc;
227 /**********************************************************************
228 * RES_FindResource
231 static HRSRC RES_FindResource( HMODULE hModule, LPCSTR type,
232 LPCSTR name, WORD lang,
233 BOOL bUnicode, BOOL bRet16 )
235 HRSRC hRsrc;
236 __TRY
238 hRsrc = RES_FindResource2(hModule, type, name, lang, bUnicode, bRet16);
240 __EXCEPT(page_fault)
242 WARN("page fault\n");
243 SetLastError(ERROR_INVALID_PARAMETER);
244 return 0;
246 __ENDTRY
247 return hRsrc;
250 /**********************************************************************
251 * RES_SizeofResource
253 static DWORD RES_SizeofResource( HMODULE hModule, HRSRC hRsrc, BOOL bRet16 )
255 if (!hRsrc) return 0;
257 TRACE("(%08x, %08x, %s)\n", hModule, hRsrc, bRet16? "NE" : "PE" );
259 if (!HIWORD(hModule))
261 HMODULE16 hMod16 = MapHModuleLS( hModule );
262 NE_MODULE *pModule = NE_GetPtr( hMod16 );
263 if (!pModule) return 0;
265 if (!pModule->module32) /* 16-bit NE module */
267 /* If we got a 32-bit hRsrc, we don't need to convert it */
268 return NE_SizeofResource( pModule, hRsrc );
271 /* If we got a 16-bit hRsrc, convert it */
272 if (!HIWORD(hRsrc)) hRsrc = MapHRsrc16To32( pModule, hRsrc );
275 /* 32-bit PE module */
276 return PE_SizeofResource( hRsrc );
279 /**********************************************************************
280 * RES_LoadResource
282 static HGLOBAL RES_LoadResource( HMODULE hModule, HRSRC hRsrc, BOOL bRet16 )
284 HGLOBAL hMem = 0;
286 TRACE("(%08x, %08x, %s)\n", hModule, hRsrc, bRet16? "NE" : "PE" );
288 if (!hRsrc) return 0;
290 if (!HIWORD(hModule))
292 HMODULE16 hMod16 = MapHModuleLS( hModule );
293 NE_MODULE *pModule = NE_GetPtr( hMod16 );
294 if (!pModule) return 0;
295 if (!pModule->module32)
297 /* 16-bit NE module */
299 /* If we got a 32-bit hRsrc, we don't need to convert it */
300 hMem = NE_LoadResource( pModule, LOWORD(hRsrc) );
302 /* If we are to return a 32-bit resource, we should probably
303 convert it but we don't for now. FIXME !!! */
304 return hMem;
306 else
308 /* If we got a 16-bit hRsrc, convert it */
309 HRSRC hRsrc32 = HIWORD(hRsrc)? hRsrc : MapHRsrc16To32( pModule, hRsrc );
311 hMem = PE_LoadResource( pModule->module32, hRsrc32 );
313 /* If we need to return a 16-bit resource, convert it */
314 if ( bRet16 )
316 WORD type = MapHRsrc16ToType( pModule, hRsrc );
317 DWORD size = SizeofResource( hModule, hRsrc );
318 LPVOID bits = LockResource( hMem );
320 hMem = NE_LoadPEResource( pModule, type, bits, size );
324 else
326 /* 32-bit PE module */
327 hMem = PE_LoadResource( hModule, hRsrc );
330 return hMem;
333 /**********************************************************************
334 * FindResource (KERNEL.60)
335 * FindResource16 (KERNEL32.@)
337 HRSRC16 WINAPI FindResource16( HMODULE16 hModule, LPCSTR name, LPCSTR type )
339 return LOWORD( RES_FindResource( hModule, type, name,
340 MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), FALSE, TRUE ) );
343 /**********************************************************************
344 * FindResourceA (KERNEL32.@)
346 HRSRC WINAPI FindResourceA( HMODULE hModule, LPCSTR name, LPCSTR type )
348 return RES_FindResource( hModule, type, name,
349 MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), FALSE, FALSE );
352 /**********************************************************************
353 * FindResourceExA (KERNEL32.@)
355 HRSRC WINAPI FindResourceExA( HMODULE hModule,
356 LPCSTR type, LPCSTR name, WORD lang )
358 return RES_FindResource( hModule, type, name,
359 lang, FALSE, FALSE );
362 /**********************************************************************
363 * FindResourceExW (KERNEL32.@)
365 HRSRC WINAPI FindResourceExW( HMODULE hModule,
366 LPCWSTR type, LPCWSTR name, WORD lang )
368 return RES_FindResource( hModule, (LPCSTR)type, (LPCSTR)name,
369 lang, TRUE, FALSE );
372 /**********************************************************************
373 * FindResourceW (KERNEL32.@)
375 HRSRC WINAPI FindResourceW(HINSTANCE hModule, LPCWSTR name, LPCWSTR type)
377 return RES_FindResource( hModule, (LPCSTR)type, (LPCSTR)name,
378 MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), TRUE, FALSE );
381 /**********************************************************************
382 * LoadResource (KERNEL.61)
383 * LoadResource16 (KERNEL32.@)
385 HGLOBAL16 WINAPI LoadResource16( HMODULE16 hModule, HRSRC16 hRsrc )
387 return RES_LoadResource( hModule, hRsrc, TRUE );
390 /**********************************************************************
391 * LoadResource (KERNEL32.@)
393 HGLOBAL WINAPI LoadResource( HINSTANCE hModule, HRSRC hRsrc )
395 return RES_LoadResource( hModule, hRsrc, FALSE );
398 /**********************************************************************
399 * LockResource (KERNEL.62)
401 SEGPTR WINAPI WIN16_LockResource16( HGLOBAL16 handle )
403 TRACE("(%04x)\n", handle );
404 /* May need to reload the resource if discarded */
405 return K32WOWGlobalLock16( handle );
408 /**********************************************************************
409 * LockResource16 (KERNEL32.@)
411 LPVOID WINAPI LockResource16( HGLOBAL16 handle )
413 return MapSL( WIN16_LockResource16(handle) );
416 /**********************************************************************
417 * LockResource (KERNEL32.@)
419 LPVOID WINAPI LockResource( HGLOBAL handle )
421 TRACE("(%08x)\n", handle );
423 if (HIWORD( handle )) /* 32-bit memory handle */
424 return (LPVOID)handle;
426 /* 16-bit memory handle */
427 return LockResource16( LOWORD(handle) );
430 typedef WORD (WINAPI *pDestroyIcon32Proc)( HGLOBAL16 handle, UINT16 flags );
433 /**********************************************************************
434 * FreeResource (KERNEL.63)
435 * FreeResource16 (KERNEL32.@)
437 BOOL16 WINAPI FreeResource16( HGLOBAL16 handle )
439 HGLOBAL16 retv = handle;
440 NE_MODULE *pModule = NE_GetPtr( FarGetOwner16( handle ) );
442 TRACE("(%04x)\n", handle );
444 /* Try NE resource first */
445 retv = NE_FreeResource( pModule, handle );
447 /* If this failed, call USER.DestroyIcon32; this will check
448 whether it is a shared cursor/icon; if not it will call
449 GlobalFree16() */
450 if ( retv )
452 pDestroyIcon32Proc proc;
453 HMODULE user = GetModuleHandleA( "user32.dll" );
455 if (user && (proc = (pDestroyIcon32Proc)GetProcAddress( user, "DestroyIcon32" )))
456 retv = proc( handle, CID_RESOURCE );
457 else
458 retv = GlobalFree16( handle );
460 return (BOOL)retv;
463 /**********************************************************************
464 * FreeResource (KERNEL32.@)
466 BOOL WINAPI FreeResource( HGLOBAL handle )
468 if (HIWORD(handle)) return 0; /* 32-bit memory handle: nothing to do */
470 return FreeResource16( LOWORD(handle) );
473 /**********************************************************************
474 * SizeofResource (KERNEL.65)
475 * SizeofResource16 (KERNEL32.@)
477 DWORD WINAPI SizeofResource16( HMODULE16 hModule, HRSRC16 hRsrc )
479 return RES_SizeofResource( hModule, hRsrc, TRUE );
482 /**********************************************************************
483 * SizeofResource (KERNEL32.@)
485 DWORD WINAPI SizeofResource( HINSTANCE hModule, HRSRC hRsrc )
487 return RES_SizeofResource( hModule, hRsrc, FALSE );