]> git.sesse.net Git - vlc/blob - loader/resource.c
3560dd07309919256f4b6e24a74ef27822cb0b61
[vlc] / loader / resource.c
1 /*
2  * Resources
3  * $Id$
4  *
5  * Copyright 1993 Robert J. Amstadt
6  * Copyright 1995 Alexandre Julliard
7  *
8  * Originally distributed under LPGL 2.1 (or later) by the Wine project.
9  *
10  * Modified for use with MPlayer, detailed CVS changelog at
11  * http://www.mplayerhq.hu/cgi-bin/cvsweb.cgi/main/
12  *
13  * File now distributed as part of VLC media player with no modifications.
14  *
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.
19  *
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.
24  *
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.
28  */
29 #include "config.h"
30
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>
39
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"
48
49 #define CP_ACP                                  0
50
51 WORD WINE_LanguageId=0x409;//english
52
53 #define HRSRC_MAP_BLOCKSIZE 16
54
55 typedef struct _HRSRC_ELEM
56 {
57     HANDLE hRsrc;
58     WORD     type;
59 } HRSRC_ELEM;
60
61 typedef struct _HRSRC_MAP
62 {
63     int nAlloc;
64     int nUsed;
65     HRSRC_ELEM *elem;
66 } HRSRC_MAP;
67
68 static HRSRC RES_FindResource2( HMODULE hModule, LPCSTR type,
69                                 LPCSTR name, WORD lang, int unicode)
70 {
71     HRSRC hRsrc = 0;
72     LPWSTR typeStr, nameStr;    
73     WINE_MODREF *wm = MODULE32_LookupHMODULE( hModule );
74
75     if(!wm)
76         return 0;    
77     /* 32-bit PE module */
78
79     
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;
88     
89     hRsrc = PE_FindResourceExW( wm, nameStr, typeStr, lang );
90     
91     if ( HIWORD( type ) && (!unicode)) 
92         HeapFree( GetProcessHeap(), 0, typeStr );
93     if ( HIWORD( name ) && (!unicode)) 
94         HeapFree( GetProcessHeap(), 0, nameStr );
95
96     return hRsrc;
97 }
98
99 /**********************************************************************
100  *          RES_FindResource
101  */
102
103 static HRSRC RES_FindResource( HMODULE hModule, LPCSTR type,
104                                LPCSTR name, WORD lang, int unicode )
105 {
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;
119 }
120
121 /**********************************************************************
122  *          RES_SizeofResource
123  */
124 static DWORD RES_SizeofResource( HMODULE hModule, HRSRC hRsrc)
125 {
126     DWORD size = 0;
127     HRSRC hRsrc32;
128
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 );
134
135     if ( !hModule || !hRsrc ) return 0;
136
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))
141     {
142         printf("16-bit hRsrcs not supported\n");
143         return 0;
144     }   
145     size = PE_SizeofResource( hModule, hRsrc );
146     return size;
147 }
148
149 /**********************************************************************
150  *          RES_AccessResource
151  */
152 static HFILE RES_AccessResource( HMODULE hModule, HRSRC hRsrc )
153 {
154     HFILE hFile = HFILE_ERROR;
155
156     WINE_MODREF *wm = MODULE32_LookupHMODULE( hModule );
157
158     if ( !hModule || !hRsrc ) return HFILE_ERROR;
159
160     /* 32-bit PE module */
161     FIXME("32-bit modules not yet supported.\n" );
162     hFile = HFILE_ERROR;
163
164     return hFile;
165 }
166
167 /**********************************************************************
168  *          RES_LoadResource
169  */
170 static HGLOBAL RES_LoadResource( HMODULE hModule, HRSRC hRsrc)
171 {
172     HGLOBAL hMem = 0;
173     HRSRC hRsrc32;
174     WINE_MODREF *wm = MODULE32_LookupHMODULE( hModule );
175
176
177     if ( !hModule || !hRsrc ) return 0;
178
179     /* 32-bit PE module */
180
181     /* If we got a 16-bit hRsrc, convert it */
182 //    hRsrc32 = HIWORD(hRsrc)? hRsrc : MapHRsrc16To32( pModule, hRsrc );
183     if(!HIWORD(hRsrc))
184     {
185         printf("16-bit hRsrcs not supported\n");
186         return 0;
187     }
188     hMem = PE_LoadResource( wm, hRsrc );
189
190     return hMem;
191 }
192
193 /**********************************************************************
194  *          RES_LockResource
195  */
196 static LPVOID RES_LockResource( HGLOBAL handle )
197 {
198     LPVOID bits = NULL;
199
200     TRACE("(%08x, %s)\n", handle, "PE" );
201
202     bits = (LPVOID)handle;
203
204     return bits;
205 }
206
207 /**********************************************************************
208  *          RES_FreeResource
209  */
210 static WIN_BOOL RES_FreeResource( HGLOBAL handle )
211 {
212     HGLOBAL retv = handle;
213     return (WIN_BOOL)retv;
214 }
215
216 /**********************************************************************
217  *          FindResourceA    (KERNEL32.128)
218  */
219 HANDLE WINAPI FindResourceA( HMODULE hModule, LPCSTR name, LPCSTR type )
220 {
221     return RES_FindResource( hModule, type, name, 
222                              WINE_LanguageId, 0);
223 }
224 HANDLE WINAPI FindResourceW( HMODULE hModule, LPCWSTR name, LPCWSTR type )
225 {
226     return RES_FindResource( hModule, (LPCSTR)type, (LPCSTR)name, 
227                              WINE_LanguageId, 1);
228 }
229
230 /**********************************************************************
231  *          FindResourceExA  (KERNEL32.129)
232  */
233 HANDLE WINAPI FindResourceExA( HMODULE hModule, 
234                                LPCSTR type, LPCSTR name, WORD lang )
235 {
236     return RES_FindResource( hModule, type, name, 
237                              lang, 0 );
238 }
239
240 HANDLE WINAPI FindResourceExW( HMODULE hModule, 
241                                LPCWSTR type, LPCWSTR name, WORD lang )
242 {
243     return RES_FindResource( hModule, (LPCSTR)type, (LPCSTR)name, 
244                              lang, 1 );
245 }
246
247
248
249 /**********************************************************************
250  *          LockResource     (KERNEL32.384)
251  */
252 LPVOID WINAPI LockResource( HGLOBAL handle )
253 {
254     return RES_LockResource( handle );
255 }
256
257
258 /**********************************************************************
259  *          FreeResource     (KERNEL32.145)
260  */
261 WIN_BOOL WINAPI FreeResource( HGLOBAL handle )
262 {
263     return RES_FreeResource( handle );
264 }
265
266
267 /**********************************************************************
268  *          AccessResource   (KERNEL32.64)
269  */
270 INT WINAPI AccessResource( HMODULE hModule, HRSRC hRsrc )
271 {
272     return RES_AccessResource( hModule, hRsrc );
273 }
274 /**********************************************************************
275  *          SizeofResource   (KERNEL32.522)
276  */
277 DWORD WINAPI SizeofResource( HINSTANCE hModule, HRSRC hRsrc )
278 {
279     return RES_SizeofResource( hModule, hRsrc );
280 }
281
282
283 INT WINAPI LoadStringW( HINSTANCE instance, UINT resource_id,
284                             LPWSTR buffer, INT buflen );
285
286 /**********************************************************************
287  *      LoadStringA     (USER32.375)
288  */
289 INT WINAPI LoadStringA( HINSTANCE instance, UINT resource_id,
290                             LPSTR buffer, INT buflen )
291 {
292     INT    retval;
293     INT    wbuflen;
294     INT    abuflen;
295     LPWSTR wbuf = NULL;
296     LPSTR  abuf = NULL;
297
298     if ( buffer != NULL && buflen > 0 )
299         *buffer = 0;
300
301     wbuflen = LoadStringW(instance,resource_id,NULL,0);
302     if ( !wbuflen )
303         return 0;
304     wbuflen ++;
305
306     retval = 0;
307     wbuf = (LPWSTR) HeapAlloc( GetProcessHeap(), 0, wbuflen * sizeof(WCHAR) );
308     wbuflen = LoadStringW(instance,resource_id,wbuf,wbuflen);
309     if ( wbuflen > 0 )
310     {
311         abuflen = WideCharToMultiByte(CP_ACP,0,wbuf,wbuflen,NULL,0,NULL,NULL);
312         if ( abuflen > 0 )
313         {
314             if ( buffer == NULL || buflen == 0 )
315                 retval = abuflen;
316             else
317             {
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 )
321                 {
322                     abuflen = min(abuflen,buflen - 1);
323                     memcpy( buffer, abuf, abuflen );
324                     buffer[abuflen] = 0;
325                     retval = abuflen;
326                 }
327                 HeapFree( GetProcessHeap(), 0, abuf );
328             }
329         }
330     }
331     HeapFree( GetProcessHeap(), 0, wbuf );
332
333     return retval;
334 }
335
336 /**********************************************************************
337  *      LoadStringW             (USER32.376)
338  */
339 INT WINAPI LoadStringW( HINSTANCE instance, UINT resource_id,
340                             LPWSTR buffer, INT buflen )
341 {
342     HGLOBAL hmem;
343     HRSRC hrsrc;
344     WCHAR *p;
345     int string_num;
346     int i;
347
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);
352
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;
360     
361     p = (WCHAR*) LockResource(hmem);
362     string_num = resource_id & 0x000f;
363     for (i = 0; i < string_num; i++)
364         p += *p + 1;
365     
366     TRACE("strlen = %d\n", (int)*p );
367     
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;
377         }
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
381     }
382
383     TRACE("String loaded !\n");
384     return i;
385 }
386
387 /* Messages...used by FormatMessage32* (KERNEL32.something)
388  * 
389  * They can be specified either directly or using a message ID and
390  * loading them from the resource.
391  * 
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
399  *
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')
405  *
406  * Yes, ANSI strings in win32 resources. Go figure.
407  */
408
409 /**********************************************************************
410  *      LoadMessageA            (internal)
411  */
412 INT WINAPI LoadMessageA( HMODULE instance, UINT id, WORD lang,
413                       LPSTR buffer, INT buflen )
414 {
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;
421
422     TRACE("instance = %08lx, id = %08lx, buffer = %p, length = %ld\n", (DWORD)instance, (DWORD)id, buffer, (DWORD)buflen);
423
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;
429     
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;
438         }
439         mrb++;
440     }
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));
447     }
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;
460         }
461     }
462     if (buffer)
463             TRACE("'%s' copied !\n", buffer);
464     return i;
465 }
466
467
468
469 /**********************************************************************
470  *      EnumResourceTypesA      (KERNEL32.90)
471  */
472 WIN_BOOL WINAPI EnumResourceTypesA( HMODULE hmodule,ENUMRESTYPEPROCA lpfun,
473                                     LONG lParam)
474 {
475         /* FIXME: move WINE_MODREF stuff here */
476     return PE_EnumResourceTypesA(hmodule,lpfun,lParam);
477 }
478
479 /**********************************************************************
480  *      EnumResourceNamesA      (KERNEL32.88)
481  */
482 WIN_BOOL WINAPI EnumResourceNamesA( HMODULE hmodule, LPCSTR type,
483                                     ENUMRESNAMEPROCA lpfun, LONG lParam )
484 {
485         /* FIXME: move WINE_MODREF stuff here */
486     return PE_EnumResourceNamesA(hmodule,type,lpfun,lParam);
487 }
488 /**********************************************************************
489  *      EnumResourceLanguagesA  (KERNEL32.86)
490  */
491 WIN_BOOL WINAPI EnumResourceLanguagesA( HMODULE hmodule, LPCSTR type,
492                                         LPCSTR name, ENUMRESLANGPROCA lpfun,
493                                         LONG lParam)
494 {
495         /* FIXME: move WINE_MODREF stuff here */
496     return PE_EnumResourceLanguagesA(hmodule,type,name,lpfun,lParam);
497 }
498 /**********************************************************************
499  *          LoadResource     (KERNEL32.370)
500  */
501 HGLOBAL WINAPI LoadResource( HINSTANCE hModule, HRSRC hRsrc )
502 {
503     return RES_LoadResource( hModule, hRsrc);
504 }