5 * Copyright 1993 Robert J. Amstadt
6 * Copyright 1995 Alexandre Julliard
8 * Originally distributed under LPGL 2.1 (or later) by the Wine project.
10 * Modified for use with MPlayer, detailed CVS changelog at
11 * http://www.mplayerhq.hu/cgi-bin/cvsweb.cgi/main/
13 * File now distributed as part of VLC media player with no modifications.
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
35 #include <sys/types.h>
40 #include "wine/winbase.h"
41 #include "wine/windef.h"
42 #include "wine/winuser.h"
43 #include "wine/heap.h"
44 #include "wine/module.h"
45 #include "wine/debugtools.h"
46 #include "wine/winerror.h"
51 WORD WINE_LanguageId=0x409;//english
53 #define HRSRC_MAP_BLOCKSIZE 16
55 typedef struct _HRSRC_ELEM
61 typedef struct _HRSRC_MAP
68 static HRSRC RES_FindResource2( HMODULE hModule, LPCSTR type,
69 LPCSTR name, WORD lang, int unicode)
72 LPWSTR typeStr, nameStr;
73 WINE_MODREF *wm = MODULE32_LookupHMODULE( hModule );
77 /* 32-bit PE module */
80 if ( HIWORD( type ) && (!unicode))
81 typeStr = HEAP_strdupAtoW( GetProcessHeap(), 0, type );
83 typeStr = (LPWSTR)type;
84 if ( HIWORD( name ) && (!unicode))
85 nameStr = HEAP_strdupAtoW( GetProcessHeap(), 0, name );
87 nameStr = (LPWSTR)name;
89 hRsrc = PE_FindResourceExW( wm, nameStr, typeStr, lang );
91 if ( HIWORD( type ) && (!unicode))
92 HeapFree( GetProcessHeap(), 0, typeStr );
93 if ( HIWORD( name ) && (!unicode))
94 HeapFree( GetProcessHeap(), 0, nameStr );
99 /**********************************************************************
103 static HRSRC RES_FindResource( HMODULE hModule, LPCSTR type,
104 LPCSTR name, WORD lang, int unicode )
109 hRsrc = RES_FindResource2(hModule, type, name, lang, unicode);
111 // __EXCEPT(page_fault)
113 // WARN("page fault\n");
114 // SetLastError(ERROR_INVALID_PARAMETER);
121 /**********************************************************************
124 static DWORD RES_SizeofResource( HMODULE hModule, HRSRC hRsrc)
129 // HMODULE16 hMod16 = MapHModuleLS( hModule );
130 // NE_MODULE *pModule = NE_GetPtr( hMod16 );
131 // WINE_MODREF *wm = pModule && pModule->module32?
132 // MODULE32_LookupHMODULE( pModule->module32 ) : NULL;
133 WINE_MODREF *wm = MODULE32_LookupHMODULE( hModule );
135 if ( !hModule || !hRsrc ) return 0;
137 /* 32-bit PE module */
138 /* If we got a 16-bit hRsrc, convert it */
139 // hRsrc32 = HIWORD(hRsrc)? hRsrc : MapHRsrc16To32( pModule, hRsrc );
142 printf("16-bit hRsrcs not supported\n");
145 size = PE_SizeofResource( hModule, hRsrc );
149 /**********************************************************************
152 static HFILE RES_AccessResource( HMODULE hModule, HRSRC hRsrc )
154 HFILE hFile = HFILE_ERROR;
156 WINE_MODREF *wm = MODULE32_LookupHMODULE( hModule );
158 if ( !hModule || !hRsrc ) return HFILE_ERROR;
160 /* 32-bit PE module */
161 FIXME("32-bit modules not yet supported.\n" );
167 /**********************************************************************
170 static HGLOBAL RES_LoadResource( HMODULE hModule, HRSRC hRsrc)
174 WINE_MODREF *wm = MODULE32_LookupHMODULE( hModule );
177 if ( !hModule || !hRsrc ) return 0;
179 /* 32-bit PE module */
181 /* If we got a 16-bit hRsrc, convert it */
182 // hRsrc32 = HIWORD(hRsrc)? hRsrc : MapHRsrc16To32( pModule, hRsrc );
185 printf("16-bit hRsrcs not supported\n");
188 hMem = PE_LoadResource( wm, hRsrc );
193 /**********************************************************************
196 static LPVOID RES_LockResource( HGLOBAL handle )
200 TRACE("(%08x, %s)\n", handle, "PE" );
202 bits = (LPVOID)handle;
207 /**********************************************************************
210 static WIN_BOOL RES_FreeResource( HGLOBAL handle )
212 HGLOBAL retv = handle;
213 return (WIN_BOOL)retv;
216 /**********************************************************************
217 * FindResourceA (KERNEL32.128)
219 HANDLE WINAPI FindResourceA( HMODULE hModule, LPCSTR name, LPCSTR type )
221 return RES_FindResource( hModule, type, name,
224 HANDLE WINAPI FindResourceW( HMODULE hModule, LPCWSTR name, LPCWSTR type )
226 return RES_FindResource( hModule, (LPCSTR)type, (LPCSTR)name,
230 /**********************************************************************
231 * FindResourceExA (KERNEL32.129)
233 HANDLE WINAPI FindResourceExA( HMODULE hModule,
234 LPCSTR type, LPCSTR name, WORD lang )
236 return RES_FindResource( hModule, type, name,
240 HANDLE WINAPI FindResourceExW( HMODULE hModule,
241 LPCWSTR type, LPCWSTR name, WORD lang )
243 return RES_FindResource( hModule, (LPCSTR)type, (LPCSTR)name,
249 /**********************************************************************
250 * LockResource (KERNEL32.384)
252 LPVOID WINAPI LockResource( HGLOBAL handle )
254 return RES_LockResource( handle );
258 /**********************************************************************
259 * FreeResource (KERNEL32.145)
261 WIN_BOOL WINAPI FreeResource( HGLOBAL handle )
263 return RES_FreeResource( handle );
267 /**********************************************************************
268 * AccessResource (KERNEL32.64)
270 INT WINAPI AccessResource( HMODULE hModule, HRSRC hRsrc )
272 return RES_AccessResource( hModule, hRsrc );
274 /**********************************************************************
275 * SizeofResource (KERNEL32.522)
277 DWORD WINAPI SizeofResource( HINSTANCE hModule, HRSRC hRsrc )
279 return RES_SizeofResource( hModule, hRsrc );
283 INT WINAPI LoadStringW( HINSTANCE instance, UINT resource_id,
284 LPWSTR buffer, INT buflen );
286 /**********************************************************************
287 * LoadStringA (USER32.375)
289 INT WINAPI LoadStringA( HINSTANCE instance, UINT resource_id,
290 LPSTR buffer, INT buflen )
298 if ( buffer != NULL && buflen > 0 )
301 wbuflen = LoadStringW(instance,resource_id,NULL,0);
307 wbuf = (LPWSTR) HeapAlloc( GetProcessHeap(), 0, wbuflen * sizeof(WCHAR) );
308 wbuflen = LoadStringW(instance,resource_id,wbuf,wbuflen);
311 abuflen = WideCharToMultiByte(CP_ACP,0,wbuf,wbuflen,NULL,0,NULL,NULL);
314 if ( buffer == NULL || buflen == 0 )
318 abuf = (LPSTR) HeapAlloc( GetProcessHeap(), 0, abuflen * sizeof(CHAR) );
319 abuflen = WideCharToMultiByte(CP_ACP,0,wbuf,wbuflen,abuf,abuflen,NULL,NULL);
322 abuflen = min(abuflen,buflen - 1);
323 memcpy( buffer, abuf, abuflen );
327 HeapFree( GetProcessHeap(), 0, abuf );
331 HeapFree( GetProcessHeap(), 0, wbuf );
336 /**********************************************************************
337 * LoadStringW (USER32.376)
339 INT WINAPI LoadStringW( HINSTANCE instance, UINT resource_id,
340 LPWSTR buffer, INT buflen )
348 if (HIWORD(resource_id)==0xFFFF) /* netscape 3 passes this */
349 resource_id = (UINT)(-((INT)resource_id));
350 TRACE("instance = %04x, id = %04x, buffer = %08x, "
351 "length = %d\n", instance, (int)resource_id, (int) buffer, buflen);
353 /* Use bits 4 - 19 (incremented by 1) as resourceid, mask out
355 hrsrc = FindResourceW( instance, (LPCWSTR)(((resource_id>>4)&0xffff)+1),
357 if (!hrsrc) return 0;
358 hmem = LoadResource( instance, hrsrc );
361 p = (WCHAR*) LockResource(hmem);
362 string_num = resource_id & 0x000f;
363 for (i = 0; i < string_num; i++)
366 TRACE("strlen = %d\n", (int)*p );
368 if (buffer == NULL) return *p;
369 i = min(buflen - 1, *p);
371 memcpy(buffer, p + 1, i * sizeof (WCHAR));
372 buffer[i] = (WCHAR) 0;
375 buffer[0] = (WCHAR) 0;
379 WARN("Don't know why caller give buflen=%d *p=%d trying to obtain string '%s'\n", buflen, *p, p + 1);
383 TRACE("String loaded !\n");
387 /* Messages...used by FormatMessage32* (KERNEL32.something)
389 * They can be specified either directly or using a message ID and
390 * loading them from the resource.
392 * The resourcedata has following format:
394 * 0: DWORD nrofentries
395 * nrofentries * subentry:
396 * 0: DWORD firstentry
398 * 8: DWORD offset from start to the stringentries
400 * (lastentry-firstentry) * stringentry:
401 * 0: WORD len (0 marks end)
404 * (stringentry i of a subentry refers to the ID 'firstentry+i')
406 * Yes, ANSI strings in win32 resources. Go figure.
409 /**********************************************************************
410 * LoadMessageA (internal)
412 INT WINAPI LoadMessageA( HMODULE instance, UINT id, WORD lang,
413 LPSTR buffer, INT buflen )
417 PMESSAGE_RESOURCE_DATA mrd;
418 PMESSAGE_RESOURCE_BLOCK mrb;
419 PMESSAGE_RESOURCE_ENTRY mre;
422 TRACE("instance = %08lx, id = %08lx, buffer = %p, length = %ld\n", (DWORD)instance, (DWORD)id, buffer, (DWORD)buflen);
424 /*FIXME: I am not sure about the '1' ... But I've only seen those entries*/
425 hrsrc = FindResourceExW(instance,RT_MESSAGELISTW,(LPWSTR)1,lang);
426 if (!hrsrc) return 0;
427 hmem = LoadResource( instance, hrsrc );
430 mrd = (PMESSAGE_RESOURCE_DATA)LockResource(hmem);
432 mrb = &(mrd->Blocks[0]);
433 for (i=mrd->NumberOfBlocks;i--;) {
434 if ((id>=mrb->LowId) && (id<=mrb->HighId)) {
435 mre = (PMESSAGE_RESOURCE_ENTRY)(((char*)mrd)+mrb->OffsetToEntries);
446 mre = (PMESSAGE_RESOURCE_ENTRY)(((char*)mre)+(mre->Length));
449 TRACE(" - strlen=%d\n",slen);
450 i = min(buflen - 1, slen);
454 lstrcpynA(buffer,(char*)mre->Text,i);
463 TRACE("'%s' copied !\n", buffer);
469 /**********************************************************************
470 * EnumResourceTypesA (KERNEL32.90)
472 WIN_BOOL WINAPI EnumResourceTypesA( HMODULE hmodule,ENUMRESTYPEPROCA lpfun,
475 /* FIXME: move WINE_MODREF stuff here */
476 return PE_EnumResourceTypesA(hmodule,lpfun,lParam);
479 /**********************************************************************
480 * EnumResourceNamesA (KERNEL32.88)
482 WIN_BOOL WINAPI EnumResourceNamesA( HMODULE hmodule, LPCSTR type,
483 ENUMRESNAMEPROCA lpfun, LONG lParam )
485 /* FIXME: move WINE_MODREF stuff here */
486 return PE_EnumResourceNamesA(hmodule,type,lpfun,lParam);
488 /**********************************************************************
489 * EnumResourceLanguagesA (KERNEL32.86)
491 WIN_BOOL WINAPI EnumResourceLanguagesA( HMODULE hmodule, LPCSTR type,
492 LPCSTR name, ENUMRESLANGPROCA lpfun,
495 /* FIXME: move WINE_MODREF stuff here */
496 return PE_EnumResourceLanguagesA(hmodule,type,name,lpfun,lParam);
498 /**********************************************************************
499 * LoadResource (KERNEL32.370)
501 HGLOBAL WINAPI LoadResource( HINSTANCE hModule, HRSRC hRsrc )
503 return RES_LoadResource( hModule, hRsrc);