Qt: synchronization: subtitles duration parameter
[vlc/vlc-skelet.git] / libs / loader / resource.c
blob3560dd07309919256f4b6e24a74ef27822cb0b61
1 /*
2 * Resources
3 * $Id$
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.
29 #include "config.h"
31 #include <assert.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <fcntl.h>
38 #include <unistd.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"
47 #include "loader.h"
49 #define CP_ACP 0
51 WORD WINE_LanguageId=0x409;//english
53 #define HRSRC_MAP_BLOCKSIZE 16
55 typedef struct _HRSRC_ELEM
57 HANDLE hRsrc;
58 WORD type;
59 } HRSRC_ELEM;
61 typedef struct _HRSRC_MAP
63 int nAlloc;
64 int nUsed;
65 HRSRC_ELEM *elem;
66 } HRSRC_MAP;
68 static HRSRC RES_FindResource2( HMODULE hModule, LPCSTR type,
69 LPCSTR name, WORD lang, int unicode)
71 HRSRC hRsrc = 0;
72 LPWSTR typeStr, nameStr;
73 WINE_MODREF *wm = MODULE32_LookupHMODULE( hModule );
75 if(!wm)
76 return 0;
77 /* 32-bit PE module */
80 if ( HIWORD( type ) && (!unicode))
81 typeStr = HEAP_strdupAtoW( GetProcessHeap(), 0, type );
82 else
83 typeStr = (LPWSTR)type;
84 if ( HIWORD( name ) && (!unicode))
85 nameStr = HEAP_strdupAtoW( GetProcessHeap(), 0, name );
86 else
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 );
96 return hRsrc;
99 /**********************************************************************
100 * RES_FindResource
103 static HRSRC RES_FindResource( HMODULE hModule, LPCSTR type,
104 LPCSTR name, WORD lang, int unicode )
106 HRSRC hRsrc;
107 // __TRY
108 // {
109 hRsrc = RES_FindResource2(hModule, type, name, lang, unicode);
110 // }
111 // __EXCEPT(page_fault)
112 // {
113 // WARN("page fault\n");
114 // SetLastError(ERROR_INVALID_PARAMETER);
115 // return 0;
116 // }
117 // __ENDTRY
118 return hRsrc;
121 /**********************************************************************
122 * RES_SizeofResource
124 static DWORD RES_SizeofResource( HMODULE hModule, HRSRC hRsrc)
126 DWORD size = 0;
127 HRSRC hRsrc32;
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 );
140 if(!HIWORD(hRsrc))
142 printf("16-bit hRsrcs not supported\n");
143 return 0;
145 size = PE_SizeofResource( hModule, hRsrc );
146 return size;
149 /**********************************************************************
150 * RES_AccessResource
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" );
162 hFile = HFILE_ERROR;
164 return hFile;
167 /**********************************************************************
168 * RES_LoadResource
170 static HGLOBAL RES_LoadResource( HMODULE hModule, HRSRC hRsrc)
172 HGLOBAL hMem = 0;
173 HRSRC hRsrc32;
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 );
183 if(!HIWORD(hRsrc))
185 printf("16-bit hRsrcs not supported\n");
186 return 0;
188 hMem = PE_LoadResource( wm, hRsrc );
190 return hMem;
193 /**********************************************************************
194 * RES_LockResource
196 static LPVOID RES_LockResource( HGLOBAL handle )
198 LPVOID bits = NULL;
200 TRACE("(%08x, %s)\n", handle, "PE" );
202 bits = (LPVOID)handle;
204 return bits;
207 /**********************************************************************
208 * RES_FreeResource
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,
222 WINE_LanguageId, 0);
224 HANDLE WINAPI FindResourceW( HMODULE hModule, LPCWSTR name, LPCWSTR type )
226 return RES_FindResource( hModule, (LPCSTR)type, (LPCSTR)name,
227 WINE_LanguageId, 1);
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,
237 lang, 0 );
240 HANDLE WINAPI FindResourceExW( HMODULE hModule,
241 LPCWSTR type, LPCWSTR name, WORD lang )
243 return RES_FindResource( hModule, (LPCSTR)type, (LPCSTR)name,
244 lang, 1 );
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 )
292 INT retval;
293 INT wbuflen;
294 INT abuflen;
295 LPWSTR wbuf = NULL;
296 LPSTR abuf = NULL;
298 if ( buffer != NULL && buflen > 0 )
299 *buffer = 0;
301 wbuflen = LoadStringW(instance,resource_id,NULL,0);
302 if ( !wbuflen )
303 return 0;
304 wbuflen ++;
306 retval = 0;
307 wbuf = (LPWSTR) HeapAlloc( GetProcessHeap(), 0, wbuflen * sizeof(WCHAR) );
308 wbuflen = LoadStringW(instance,resource_id,wbuf,wbuflen);
309 if ( wbuflen > 0 )
311 abuflen = WideCharToMultiByte(CP_ACP,0,wbuf,wbuflen,NULL,0,NULL,NULL);
312 if ( abuflen > 0 )
314 if ( buffer == NULL || buflen == 0 )
315 retval = abuflen;
316 else
318 abuf = (LPSTR) HeapAlloc( GetProcessHeap(), 0, abuflen * sizeof(CHAR) );
319 abuflen = WideCharToMultiByte(CP_ACP,0,wbuf,wbuflen,abuf,abuflen,NULL,NULL);
320 if ( abuflen > 0 )
322 abuflen = min(abuflen,buflen - 1);
323 memcpy( buffer, abuf, abuflen );
324 buffer[abuflen] = 0;
325 retval = abuflen;
327 HeapFree( GetProcessHeap(), 0, abuf );
331 HeapFree( GetProcessHeap(), 0, wbuf );
333 return retval;
336 /**********************************************************************
337 * LoadStringW (USER32.376)
339 INT WINAPI LoadStringW( HINSTANCE instance, UINT resource_id,
340 LPWSTR buffer, INT buflen )
342 HGLOBAL hmem;
343 HRSRC hrsrc;
344 WCHAR *p;
345 int string_num;
346 int i;
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
354 * 20 - 31. */
355 hrsrc = FindResourceW( instance, (LPCWSTR)(((resource_id>>4)&0xffff)+1),
356 RT_STRINGW );
357 if (!hrsrc) return 0;
358 hmem = LoadResource( instance, hrsrc );
359 if (!hmem) return 0;
361 p = (WCHAR*) LockResource(hmem);
362 string_num = resource_id & 0x000f;
363 for (i = 0; i < string_num; i++)
364 p += *p + 1;
366 TRACE("strlen = %d\n", (int)*p );
368 if (buffer == NULL) return *p;
369 i = min(buflen - 1, *p);
370 if (i > 0) {
371 memcpy(buffer, p + 1, i * sizeof (WCHAR));
372 buffer[i] = (WCHAR) 0;
373 } else {
374 if (buflen > 1) {
375 buffer[0] = (WCHAR) 0;
376 return 0;
378 #if 0
379 WARN("Don't know why caller give buflen=%d *p=%d trying to obtain string '%s'\n", buflen, *p, p + 1);
380 #endif
383 TRACE("String loaded !\n");
384 return i;
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:
393 * start:
394 * 0: DWORD nrofentries
395 * nrofentries * subentry:
396 * 0: DWORD firstentry
397 * 4: DWORD lastentry
398 * 8: DWORD offset from start to the stringentries
400 * (lastentry-firstentry) * stringentry:
401 * 0: WORD len (0 marks end)
402 * 2: WORD flags
403 * 4: CHAR[len-4]
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 )
415 HGLOBAL hmem;
416 HRSRC hrsrc;
417 PMESSAGE_RESOURCE_DATA mrd;
418 PMESSAGE_RESOURCE_BLOCK mrb;
419 PMESSAGE_RESOURCE_ENTRY mre;
420 int i,slen;
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 );
428 if (!hmem) return 0;
430 mrd = (PMESSAGE_RESOURCE_DATA)LockResource(hmem);
431 mre = NULL;
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);
436 id -= mrb->LowId;
437 break;
439 mrb++;
441 if (!mre)
442 return 0;
443 for (i=id;i--;) {
444 if (!mre->Length)
445 return 0;
446 mre = (PMESSAGE_RESOURCE_ENTRY)(((char*)mre)+(mre->Length));
448 slen=mre->Length;
449 TRACE(" - strlen=%d\n",slen);
450 i = min(buflen - 1, slen);
451 if (buffer == NULL)
452 return slen;
453 if (i>0) {
454 lstrcpynA(buffer,(char*)mre->Text,i);
455 buffer[i]=0;
456 } else {
457 if (buflen>1) {
458 buffer[0]=0;
459 return 0;
462 if (buffer)
463 TRACE("'%s' copied !\n", buffer);
464 return i;
469 /**********************************************************************
470 * EnumResourceTypesA (KERNEL32.90)
472 WIN_BOOL WINAPI EnumResourceTypesA( HMODULE hmodule,ENUMRESTYPEPROCA lpfun,
473 LONG lParam)
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,
493 LONG lParam)
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);