Commit files by Steinar Gunderson, forgotten in r30866.
[mplayer/glamo.git] / loader / resource.c
blobc15b308c697cc74c5c41da28d25d7958bc4c1e6c
1 /*
2 * Resources
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/
11 #include "config.h"
12 #include "debug.h"
14 #include <assert.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <fcntl.h>
21 #include <unistd.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"
30 #include "loader.h"
32 #define CP_ACP 0
34 WORD WINE_LanguageId=0x409;//english
36 #define HRSRC_MAP_BLOCKSIZE 16
38 typedef struct HRSRC_ELEM
40 HANDLE hRsrc;
41 WORD type;
42 } HRSRC_ELEM;
44 typedef struct HRSRC_MAP
46 int nAlloc;
47 int nUsed;
48 HRSRC_ELEM *elem;
49 } HRSRC_MAP;
51 static HRSRC RES_FindResource2( HMODULE hModule, LPCSTR type,
52 LPCSTR name, WORD lang, int unicode)
54 HRSRC hRsrc = 0;
55 LPWSTR typeStr, nameStr;
56 WINE_MODREF *wm = MODULE32_LookupHMODULE( hModule );
58 if(!wm)
59 return 0;
60 /* 32-bit PE module */
63 if ( HIWORD( type ) && (!unicode))
64 typeStr = HEAP_strdupAtoW( GetProcessHeap(), 0, type );
65 else
66 typeStr = (LPWSTR)type;
67 if ( HIWORD( name ) && (!unicode))
68 nameStr = HEAP_strdupAtoW( GetProcessHeap(), 0, name );
69 else
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 );
79 return hRsrc;
82 /**********************************************************************
83 * RES_FindResource
86 static HRSRC RES_FindResource( HMODULE hModule, LPCSTR type,
87 LPCSTR name, WORD lang, int unicode )
89 HRSRC hRsrc;
90 // __TRY
91 // {
92 hRsrc = RES_FindResource2(hModule, type, name, lang, unicode);
93 // }
94 // __EXCEPT(page_fault)
95 // {
96 // WARN("page fault\n");
97 // SetLastError(ERROR_INVALID_PARAMETER);
98 // return 0;
99 // }
100 // __ENDTRY
101 return hRsrc;
104 /**********************************************************************
105 * RES_SizeofResource
107 static DWORD RES_SizeofResource( HMODULE hModule, HRSRC hRsrc)
109 DWORD size = 0;
110 // HRSRC hRsrc32;
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 );
123 if(!HIWORD(hRsrc))
125 printf("16-bit hRsrcs not supported\n");
126 return 0;
128 size = PE_SizeofResource( hModule, hRsrc );
129 return size;
132 /**********************************************************************
133 * RES_AccessResource
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" );
145 hFile = HFILE_ERROR;
147 return hFile;
150 /**********************************************************************
151 * RES_LoadResource
153 static HGLOBAL RES_LoadResource( HMODULE hModule, HRSRC hRsrc)
155 HGLOBAL hMem = 0;
156 // HRSRC hRsrc32;
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 );
166 if(!HIWORD(hRsrc))
168 printf("16-bit hRsrcs not supported\n");
169 return 0;
171 hMem = PE_LoadResource( wm, hRsrc );
173 return hMem;
176 /**********************************************************************
177 * RES_LockResource
179 static LPVOID RES_LockResource( HGLOBAL handle )
181 LPVOID bits = NULL;
183 TRACE("(%08x, %s)\n", handle, "PE" );
185 bits = (LPVOID)handle;
187 return bits;
190 /**********************************************************************
191 * RES_FreeResource
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,
205 WINE_LanguageId, 0);
207 HANDLE WINAPI FindResourceW( HMODULE hModule, LPCWSTR name, LPCWSTR type )
209 return RES_FindResource( hModule, (LPCSTR)type, (LPCSTR)name,
210 WINE_LanguageId, 1);
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,
220 lang, 0 );
223 HANDLE WINAPI FindResourceExW( HMODULE hModule,
224 LPCWSTR type, LPCWSTR name, WORD lang )
226 return RES_FindResource( hModule, (LPCSTR)type, (LPCSTR)name,
227 lang, 1 );
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 )
275 INT retval;
276 INT wbuflen;
277 INT abuflen;
278 LPWSTR wbuf = NULL;
279 LPSTR abuf = NULL;
281 if ( buffer != NULL && buflen > 0 )
282 *buffer = 0;
284 wbuflen = LoadStringW(instance,resource_id,NULL,0);
285 if ( !wbuflen )
286 return 0;
287 wbuflen ++;
289 retval = 0;
290 wbuf = (LPWSTR) HeapAlloc( GetProcessHeap(), 0, wbuflen * sizeof(WCHAR) );
291 wbuflen = LoadStringW(instance,resource_id,wbuf,wbuflen);
292 if ( wbuflen > 0 )
294 abuflen = WideCharToMultiByte(CP_ACP,0,wbuf,wbuflen,NULL,0,NULL,NULL);
295 if ( abuflen > 0 )
297 if ( buffer == NULL || buflen == 0 )
298 retval = abuflen;
299 else
301 abuf = (LPSTR) HeapAlloc( GetProcessHeap(), 0, abuflen * sizeof(CHAR) );
302 abuflen = WideCharToMultiByte(CP_ACP,0,wbuf,wbuflen,abuf,abuflen,NULL,NULL);
303 if ( abuflen > 0 )
305 abuflen = min(abuflen,buflen - 1);
306 memcpy( buffer, abuf, abuflen );
307 buffer[abuflen] = 0;
308 retval = abuflen;
310 HeapFree( GetProcessHeap(), 0, abuf );
314 HeapFree( GetProcessHeap(), 0, wbuf );
316 return retval;
319 /**********************************************************************
320 * LoadStringW (USER32.376)
322 INT WINAPI LoadStringW( HINSTANCE instance, UINT resource_id,
323 LPWSTR buffer, INT buflen )
325 HGLOBAL hmem;
326 HRSRC hrsrc;
327 WCHAR *p;
328 int string_num;
329 int i;
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
337 * 20 - 31. */
338 hrsrc = FindResourceW( instance, (LPCWSTR)(((resource_id>>4)&0xffff)+1),
339 RT_STRINGW );
340 if (!hrsrc) return 0;
341 hmem = LoadResource( instance, hrsrc );
342 if (!hmem) return 0;
344 p = (WCHAR*) LockResource(hmem);
345 string_num = resource_id & 0x000f;
346 for (i = 0; i < string_num; i++)
347 p += *p + 1;
349 TRACE("strlen = %d\n", (int)*p );
351 if (buffer == NULL) return *p;
352 i = min(buflen - 1, *p);
353 if (i > 0) {
354 memcpy(buffer, p + 1, i * sizeof (WCHAR));
355 buffer[i] = (WCHAR) 0;
356 } else {
357 if (buflen > 1) {
358 buffer[0] = (WCHAR) 0;
359 return 0;
361 #if 0
362 WARN("Don't know why caller give buflen=%d *p=%d trying to obtain string '%s'\n", buflen, *p, p + 1);
363 #endif
366 TRACE("String loaded !\n");
367 return i;
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:
376 * start:
377 * 0: DWORD nrofentries
378 * nrofentries * subentry:
379 * 0: DWORD firstentry
380 * 4: DWORD lastentry
381 * 8: DWORD offset from start to the stringentries
383 * (lastentry-firstentry) * stringentry:
384 * 0: WORD len (0 marks end)
385 * 2: WORD flags
386 * 4: CHAR[len-4]
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 )
398 HGLOBAL hmem;
399 HRSRC hrsrc;
400 PMESSAGE_RESOURCE_DATA mrd;
401 PMESSAGE_RESOURCE_BLOCK mrb;
402 PMESSAGE_RESOURCE_ENTRY mre;
403 int i,slen;
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 );
411 if (!hmem) return 0;
413 mrd = (PMESSAGE_RESOURCE_DATA)LockResource(hmem);
414 mre = NULL;
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);
419 id -= mrb->LowId;
420 break;
422 mrb++;
424 if (!mre)
425 return 0;
426 for (i=id;i--;) {
427 if (!mre->Length)
428 return 0;
429 mre = (PMESSAGE_RESOURCE_ENTRY)(((char*)mre)+(mre->Length));
431 slen=mre->Length;
432 TRACE(" - strlen=%d\n",slen);
433 i = min(buflen - 1, slen);
434 if (buffer == NULL)
435 return slen;
436 if (i>0) {
437 lstrcpynA(buffer,(char*)mre->Text,i);
438 buffer[i]=0;
439 } else {
440 if (buflen>1) {
441 buffer[0]=0;
442 return 0;
445 if (buffer)
446 TRACE("'%s' copied !\n", buffer);
447 return i;
452 /**********************************************************************
453 * EnumResourceTypesA (KERNEL32.90)
455 WIN_BOOL WINAPI EnumResourceTypesA( HMODULE hmodule,ENUMRESTYPEPROCA lpfun,
456 LONG lParam)
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,
476 LONG lParam)
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);