4 * Copyright 1993 Robert J. Amstadt
5 * Copyright 1995 Alexandre Julliard
7 * Modified for use with MPlayer, detailed changelog at
8 * http://svn.mplayerhq.hu/mplayer/trunk/
18 #include <sys/types.h>
23 #include "wine/winbase.h"
24 #include "wine/windef.h"
25 #include "wine/winuser.h"
26 #include "wine/heap.h"
27 #include "wine/module.h"
28 #include "wine/debugtools.h"
29 #include "wine/winerror.h"
34 WORD WINE_LanguageId
=0x409;//english
36 #define HRSRC_MAP_BLOCKSIZE 16
38 typedef struct _HRSRC_ELEM
44 typedef struct _HRSRC_MAP
51 static HRSRC
RES_FindResource2( HMODULE hModule
, LPCSTR type
,
52 LPCSTR name
, WORD lang
, int unicode
)
55 LPWSTR typeStr
, nameStr
;
56 WINE_MODREF
*wm
= MODULE32_LookupHMODULE( hModule
);
60 /* 32-bit PE module */
63 if ( HIWORD( type
) && (!unicode
))
64 typeStr
= HEAP_strdupAtoW( GetProcessHeap(), 0, type
);
66 typeStr
= (LPWSTR
)type
;
67 if ( HIWORD( name
) && (!unicode
))
68 nameStr
= HEAP_strdupAtoW( GetProcessHeap(), 0, name
);
70 nameStr
= (LPWSTR
)name
;
72 hRsrc
= PE_FindResourceExW( wm
, nameStr
, typeStr
, lang
);
74 if ( HIWORD( type
) && (!unicode
))
75 HeapFree( GetProcessHeap(), 0, typeStr
);
76 if ( HIWORD( name
) && (!unicode
))
77 HeapFree( GetProcessHeap(), 0, nameStr
);
82 /**********************************************************************
86 static HRSRC
RES_FindResource( HMODULE hModule
, LPCSTR type
,
87 LPCSTR name
, WORD lang
, int unicode
)
92 hRsrc
= RES_FindResource2(hModule
, type
, name
, lang
, unicode
);
94 // __EXCEPT(page_fault)
96 // WARN("page fault\n");
97 // SetLastError(ERROR_INVALID_PARAMETER);
104 /**********************************************************************
107 static DWORD
RES_SizeofResource( HMODULE hModule
, HRSRC hRsrc
)
112 // HMODULE16 hMod16 = MapHModuleLS( hModule );
113 // NE_MODULE *pModule = NE_GetPtr( hMod16 );
114 // WINE_MODREF *wm = pModule && pModule->module32?
115 // MODULE32_LookupHMODULE( pModule->module32 ) : NULL;
116 WINE_MODREF
*wm
= MODULE32_LookupHMODULE( hModule
);
118 if ( !hModule
|| !hRsrc
) return 0;
120 /* 32-bit PE module */
121 /* If we got a 16-bit hRsrc, convert it */
122 // hRsrc32 = HIWORD(hRsrc)? hRsrc : MapHRsrc16To32( pModule, hRsrc );
125 printf("16-bit hRsrcs not supported\n");
128 size
= PE_SizeofResource( hModule
, hRsrc
);
132 /**********************************************************************
135 static HFILE
RES_AccessResource( HMODULE hModule
, HRSRC hRsrc
)
137 HFILE hFile
= HFILE_ERROR
;
139 WINE_MODREF
*wm
= MODULE32_LookupHMODULE( hModule
);
141 if ( !hModule
|| !hRsrc
) return HFILE_ERROR
;
143 /* 32-bit PE module */
144 FIXME("32-bit modules not yet supported.\n" );
150 /**********************************************************************
153 static HGLOBAL
RES_LoadResource( HMODULE hModule
, HRSRC hRsrc
)
157 WINE_MODREF
*wm
= MODULE32_LookupHMODULE( hModule
);
160 if ( !hModule
|| !hRsrc
) return 0;
162 /* 32-bit PE module */
164 /* If we got a 16-bit hRsrc, convert it */
165 // hRsrc32 = HIWORD(hRsrc)? hRsrc : MapHRsrc16To32( pModule, hRsrc );
168 printf("16-bit hRsrcs not supported\n");
171 hMem
= PE_LoadResource( wm
, hRsrc
);
176 /**********************************************************************
179 static LPVOID
RES_LockResource( HGLOBAL handle
)
183 TRACE("(%08x, %s)\n", handle
, "PE" );
185 bits
= (LPVOID
)handle
;
190 /**********************************************************************
193 static WIN_BOOL
RES_FreeResource( HGLOBAL handle
)
195 HGLOBAL retv
= handle
;
196 return (WIN_BOOL
)retv
;
199 /**********************************************************************
200 * FindResourceA (KERNEL32.128)
202 HANDLE WINAPI
FindResourceA( HMODULE hModule
, LPCSTR name
, LPCSTR type
)
204 return RES_FindResource( hModule
, type
, name
,
207 HANDLE WINAPI
FindResourceW( HMODULE hModule
, LPCWSTR name
, LPCWSTR type
)
209 return RES_FindResource( hModule
, (LPCSTR
)type
, (LPCSTR
)name
,
213 /**********************************************************************
214 * FindResourceExA (KERNEL32.129)
216 HANDLE WINAPI
FindResourceExA( HMODULE hModule
,
217 LPCSTR type
, LPCSTR name
, WORD lang
)
219 return RES_FindResource( hModule
, type
, name
,
223 HANDLE WINAPI
FindResourceExW( HMODULE hModule
,
224 LPCWSTR type
, LPCWSTR name
, WORD lang
)
226 return RES_FindResource( hModule
, (LPCSTR
)type
, (LPCSTR
)name
,
232 /**********************************************************************
233 * LockResource (KERNEL32.384)
235 LPVOID WINAPI
LockResource( HGLOBAL handle
)
237 return RES_LockResource( handle
);
241 /**********************************************************************
242 * FreeResource (KERNEL32.145)
244 WIN_BOOL WINAPI
FreeResource( HGLOBAL handle
)
246 return RES_FreeResource( handle
);
250 /**********************************************************************
251 * AccessResource (KERNEL32.64)
253 INT WINAPI
AccessResource( HMODULE hModule
, HRSRC hRsrc
)
255 return RES_AccessResource( hModule
, hRsrc
);
257 /**********************************************************************
258 * SizeofResource (KERNEL32.522)
260 DWORD WINAPI
SizeofResource( HINSTANCE hModule
, HRSRC hRsrc
)
262 return RES_SizeofResource( hModule
, hRsrc
);
266 INT WINAPI
LoadStringW( HINSTANCE instance
, UINT resource_id
,
267 LPWSTR buffer
, INT buflen
);
269 /**********************************************************************
270 * LoadStringA (USER32.375)
272 INT WINAPI
LoadStringA( HINSTANCE instance
, UINT resource_id
,
273 LPSTR buffer
, INT buflen
)
281 if ( buffer
!= NULL
&& buflen
> 0 )
284 wbuflen
= LoadStringW(instance
,resource_id
,NULL
,0);
290 wbuf
= (LPWSTR
) HeapAlloc( GetProcessHeap(), 0, wbuflen
* sizeof(WCHAR
) );
291 wbuflen
= LoadStringW(instance
,resource_id
,wbuf
,wbuflen
);
294 abuflen
= WideCharToMultiByte(CP_ACP
,0,wbuf
,wbuflen
,NULL
,0,NULL
,NULL
);
297 if ( buffer
== NULL
|| buflen
== 0 )
301 abuf
= (LPSTR
) HeapAlloc( GetProcessHeap(), 0, abuflen
* sizeof(CHAR
) );
302 abuflen
= WideCharToMultiByte(CP_ACP
,0,wbuf
,wbuflen
,abuf
,abuflen
,NULL
,NULL
);
305 abuflen
= min(abuflen
,buflen
- 1);
306 memcpy( buffer
, abuf
, abuflen
);
310 HeapFree( GetProcessHeap(), 0, abuf
);
314 HeapFree( GetProcessHeap(), 0, wbuf
);
319 /**********************************************************************
320 * LoadStringW (USER32.376)
322 INT WINAPI
LoadStringW( HINSTANCE instance
, UINT resource_id
,
323 LPWSTR buffer
, INT buflen
)
331 if (HIWORD(resource_id
)==0xFFFF) /* netscape 3 passes this */
332 resource_id
= (UINT
)(-((INT
)resource_id
));
333 TRACE("instance = %04x, id = %04x, buffer = %08x, "
334 "length = %d\n", instance
, (int)resource_id
, (int) buffer
, buflen
);
336 /* Use bits 4 - 19 (incremented by 1) as resourceid, mask out
338 hrsrc
= FindResourceW( instance
, (LPCWSTR
)(((resource_id
>>4)&0xffff)+1),
340 if (!hrsrc
) return 0;
341 hmem
= LoadResource( instance
, hrsrc
);
344 p
= (WCHAR
*) LockResource(hmem
);
345 string_num
= resource_id
& 0x000f;
346 for (i
= 0; i
< string_num
; i
++)
349 TRACE("strlen = %d\n", (int)*p
);
351 if (buffer
== NULL
) return *p
;
352 i
= min(buflen
- 1, *p
);
354 memcpy(buffer
, p
+ 1, i
* sizeof (WCHAR
));
355 buffer
[i
] = (WCHAR
) 0;
358 buffer
[0] = (WCHAR
) 0;
362 WARN("Don't know why caller give buflen=%d *p=%d trying to obtain string '%s'\n", buflen
, *p
, p
+ 1);
366 TRACE("String loaded !\n");
370 /* Messages...used by FormatMessage32* (KERNEL32.something)
372 * They can be specified either directly or using a message ID and
373 * loading them from the resource.
375 * The resourcedata has following format:
377 * 0: DWORD nrofentries
378 * nrofentries * subentry:
379 * 0: DWORD firstentry
381 * 8: DWORD offset from start to the stringentries
383 * (lastentry-firstentry) * stringentry:
384 * 0: WORD len (0 marks end)
387 * (stringentry i of a subentry refers to the ID 'firstentry+i')
389 * Yes, ANSI strings in win32 resources. Go figure.
392 /**********************************************************************
393 * LoadMessageA (internal)
395 INT WINAPI
LoadMessageA( HMODULE instance
, UINT id
, WORD lang
,
396 LPSTR buffer
, INT buflen
)
400 PMESSAGE_RESOURCE_DATA mrd
;
401 PMESSAGE_RESOURCE_BLOCK mrb
;
402 PMESSAGE_RESOURCE_ENTRY mre
;
405 TRACE("instance = %08lx, id = %08lx, buffer = %p, length = %ld\n", (DWORD
)instance
, (DWORD
)id
, buffer
, (DWORD
)buflen
);
407 /*FIXME: I am not sure about the '1' ... But I've only seen those entries*/
408 hrsrc
= FindResourceExW(instance
,RT_MESSAGELISTW
,(LPWSTR
)1,lang
);
409 if (!hrsrc
) return 0;
410 hmem
= LoadResource( instance
, hrsrc
);
413 mrd
= (PMESSAGE_RESOURCE_DATA
)LockResource(hmem
);
415 mrb
= &(mrd
->Blocks
[0]);
416 for (i
=mrd
->NumberOfBlocks
;i
--;) {
417 if ((id
>=mrb
->LowId
) && (id
<=mrb
->HighId
)) {
418 mre
= (PMESSAGE_RESOURCE_ENTRY
)(((char*)mrd
)+mrb
->OffsetToEntries
);
429 mre
= (PMESSAGE_RESOURCE_ENTRY
)(((char*)mre
)+(mre
->Length
));
432 TRACE(" - strlen=%d\n",slen
);
433 i
= min(buflen
- 1, slen
);
437 lstrcpynA(buffer
,(char*)mre
->Text
,i
);
446 TRACE("'%s' copied !\n", buffer
);
452 /**********************************************************************
453 * EnumResourceTypesA (KERNEL32.90)
455 WIN_BOOL WINAPI
EnumResourceTypesA( HMODULE hmodule
,ENUMRESTYPEPROCA lpfun
,
458 /* FIXME: move WINE_MODREF stuff here */
459 return PE_EnumResourceTypesA(hmodule
,lpfun
,lParam
);
462 /**********************************************************************
463 * EnumResourceNamesA (KERNEL32.88)
465 WIN_BOOL WINAPI
EnumResourceNamesA( HMODULE hmodule
, LPCSTR type
,
466 ENUMRESNAMEPROCA lpfun
, LONG lParam
)
468 /* FIXME: move WINE_MODREF stuff here */
469 return PE_EnumResourceNamesA(hmodule
,type
,lpfun
,lParam
);
471 /**********************************************************************
472 * EnumResourceLanguagesA (KERNEL32.86)
474 WIN_BOOL WINAPI
EnumResourceLanguagesA( HMODULE hmodule
, LPCSTR type
,
475 LPCSTR name
, ENUMRESLANGPROCA lpfun
,
478 /* FIXME: move WINE_MODREF stuff here */
479 return PE_EnumResourceLanguagesA(hmodule
,type
,name
,lpfun
,lParam
);
481 /**********************************************************************
482 * LoadResource (KERNEL32.370)
484 HGLOBAL WINAPI
LoadResource( HINSTANCE hModule
, HRSRC hRsrc
)
486 return RES_LoadResource( hModule
, hRsrc
);