1 /*
2 * Resources
3 *
4 * Copyright 1993 Robert J. Amstadt
5 * Copyright 1995 Alexandre Julliard
6 */
7 #include "config.h"
8
9 #include <assert.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <fcntl.h>
16 #include <unistd.h>
17 #include "winbase.h"
18 #include "windef.h"
19 #include "winuser.h"
20 #include "heap.h"
21 #include "module.h"
22 #include "debugtools.h"
23 #include "winerror.h"
24 #include "loader.h"
25 #include "ext.h"
26
27 #define CP_ACP 0
28
29 WORD WINE_LanguageId=0x409;//english
30
31 #define HRSRC_MAP_BLOCKSIZE 16
32
33 typedef struct _HRSRC_ELEM
34 {
35 HANDLE hRsrc;
36 WORD type;
37 } HRSRC_ELEM;
38
39 typedef struct _HRSRC_MAP
40 {
41 int nAlloc;
42 int nUsed;
43 HRSRC_ELEM *elem;
44 } HRSRC_MAP;
45
RES_FindResource2(HMODULE hModule,LPCSTR type,LPCSTR name,WORD lang,int unicode)46 static HRSRC RES_FindResource2( HMODULE hModule, LPCSTR type,
47 LPCSTR name, WORD lang, int unicode)
48 {
49 HRSRC hRsrc = 0;
50 LPWSTR typeStr, nameStr;
51 WINE_MODREF *wm = MODULE32_LookupHMODULE( hModule );
52
53 if(!wm)
54 return 0;
55 /* 32-bit PE module */
56
57
58 if ( HIWORD( type ) && (!unicode))
59 typeStr = HEAP_strdupAtoW( GetProcessHeap(), 0, type );
60 else
61 typeStr = (LPWSTR)type;
62 if ( HIWORD( name ) && (!unicode))
63 nameStr = HEAP_strdupAtoW( GetProcessHeap(), 0, name );
64 else
65 nameStr = (LPWSTR)name;
66
67 hRsrc = PE_FindResourceExW( wm, nameStr, typeStr, lang );
68
69 if ( HIWORD( type ) && (!unicode))
70 HeapFree( GetProcessHeap(), 0, typeStr );
71 if ( HIWORD( name ) && (!unicode))
72 HeapFree( GetProcessHeap(), 0, nameStr );
73
74 return hRsrc;
75 }
76
77 /**********************************************************************
78 * RES_FindResource
79 */
80
RES_FindResource(HMODULE hModule,LPCSTR type,LPCSTR name,WORD lang,int unicode)81 static HRSRC RES_FindResource( HMODULE hModule, LPCSTR type,
82 LPCSTR name, WORD lang, int unicode )
83 {
84 HRSRC hRsrc;
85 // __TRY
86 // {
87 hRsrc = RES_FindResource2(hModule, type, name, lang, unicode);
88 // }
89 // __EXCEPT(page_fault)
90 // {
91 // WARN("page fault\n");
92 // SetLastError(ERROR_INVALID_PARAMETER);
93 // return 0;
94 // }
95 // __ENDTRY
96 return hRsrc;
97 }
98
99 /**********************************************************************
100 * RES_SizeofResource
101 */
RES_SizeofResource(HMODULE hModule,HRSRC hRsrc)102 static DWORD RES_SizeofResource( HMODULE hModule, HRSRC hRsrc)
103 {
104 DWORD size = 0;
105 // HRSRC hRsrc32;
106 //
107 // HMODULE16 hMod16 = MapHModuleLS( hModule );
108 // NE_MODULE *pModule = NE_GetPtr( hMod16 );
109 // WINE_MODREF *wm = pModule && pModule->module32?
110 // MODULE32_LookupHMODULE( pModule->module32 ) : NULL;
111 // WINE_MODREF *wm = MODULE32_LookupHMODULE( hModule );
112
113 if ( !hModule || !hRsrc ) return 0;
114
115 /* 32-bit PE module */
116 /* If we got a 16-bit hRsrc, convert it */
117 // hRsrc32 = HIWORD(hRsrc)? hRsrc : MapHRsrc16To32( pModule, hRsrc );
118 if(!HIWORD(hRsrc))
119 {
120 printf("16-bit hRsrcs not supported\n");
121 return 0;
122 }
123 size = PE_SizeofResource( hModule, hRsrc );
124 return size;
125 }
126
127 /**********************************************************************
128 * RES_AccessResource
129 */
RES_AccessResource(HMODULE hModule,HRSRC hRsrc)130 static HFILE RES_AccessResource( HMODULE hModule, HRSRC hRsrc )
131 {
132 HFILE hFile = HFILE_ERROR;
133
134 // WINE_MODREF *wm = MODULE32_LookupHMODULE( hModule );
135
136 if ( !hModule || !hRsrc ) return HFILE_ERROR;
137
138 /* 32-bit PE module */
139 FIXME("32-bit modules not yet supported.\n" );
140 hFile = HFILE_ERROR;
141
142 return hFile;
143 }
144
145 /**********************************************************************
146 * RES_LoadResource
147 */
RES_LoadResource(HMODULE hModule,HRSRC hRsrc)148 static HGLOBAL RES_LoadResource( HMODULE hModule, HRSRC hRsrc)
149 {
150 HGLOBAL hMem = 0;
151 // HRSRC hRsrc32;
152 WINE_MODREF *wm = MODULE32_LookupHMODULE( hModule );
153
154
155 if ( !hModule || !hRsrc ) return 0;
156
157 /* 32-bit PE module */
158
159 /* If we got a 16-bit hRsrc, convert it */
160 // hRsrc32 = HIWORD(hRsrc)? hRsrc : MapHRsrc16To32( pModule, hRsrc );
161 if(!HIWORD(hRsrc))
162 {
163 printf("16-bit hRsrcs not supported\n");
164 return 0;
165 }
166 hMem = PE_LoadResource( wm, hRsrc );
167
168 return hMem;
169 }
170
171 /**********************************************************************
172 * RES_LockResource
173 */
RES_LockResource(HGLOBAL handle)174 static LPVOID RES_LockResource( HGLOBAL handle )
175 {
176 LPVOID bits = NULL;
177
178 TRACE("(%08x, %s)\n", handle, "PE" );
179
180 bits = (LPVOID)handle;
181
182 return bits;
183 }
184
185 /**********************************************************************
186 * RES_FreeResource
187 */
RES_FreeResource(HGLOBAL handle)188 static WIN_BOOL RES_FreeResource( HGLOBAL handle )
189 {
190 HGLOBAL retv = handle;
191 return (WIN_BOOL)retv;
192 }
193
194 /**********************************************************************
195 * FindResourceA (KERNEL32.128)
196 */
FindResourceA(HMODULE hModule,LPCSTR name,LPCSTR type)197 HANDLE WINAPI FindResourceA( HMODULE hModule, LPCSTR name, LPCSTR type )
198 {
199 return RES_FindResource( hModule, type, name,
200 WINE_LanguageId, 0);
201 }
FindResourceW(HMODULE hModule,LPCWSTR name,LPCWSTR type)202 HANDLE WINAPI FindResourceW( HMODULE hModule, LPCWSTR name, LPCWSTR type )
203 {
204 return RES_FindResource( hModule, (LPCSTR)type, (LPCSTR)name,
205 WINE_LanguageId, 1);
206 }
207
208 /**********************************************************************
209 * FindResourceExA (KERNEL32.129)
210 */
FindResourceExA(HMODULE hModule,LPCSTR type,LPCSTR name,WORD lang)211 HANDLE WINAPI FindResourceExA( HMODULE hModule,
212 LPCSTR type, LPCSTR name, WORD lang )
213 {
214 return RES_FindResource( hModule, type, name,
215 lang, 0 );
216 }
217
FindResourceExW(HMODULE hModule,LPCWSTR type,LPCWSTR name,WORD lang)218 HANDLE WINAPI FindResourceExW( HMODULE hModule,
219 LPCWSTR type, LPCWSTR name, WORD lang )
220 {
221 return RES_FindResource( hModule, (LPCSTR)type, (LPCSTR)name,
222 lang, 1 );
223 }
224
225
226
227 /**********************************************************************
228 * LockResource (KERNEL32.384)
229 */
LockResource(HGLOBAL handle)230 LPVOID WINAPI LockResource( HGLOBAL handle )
231 {
232 return RES_LockResource( handle );
233 }
234
235
236 /**********************************************************************
237 * FreeResource (KERNEL32.145)
238 */
FreeResource(HGLOBAL handle)239 WIN_BOOL WINAPI FreeResource( HGLOBAL handle )
240 {
241 return RES_FreeResource( handle );
242 }
243
244
245 /**********************************************************************
246 * AccessResource (KERNEL32.64)
247 */
AccessResource(HMODULE hModule,HRSRC hRsrc)248 INT WINAPI AccessResource( HMODULE hModule, HRSRC hRsrc )
249 {
250 return RES_AccessResource( hModule, hRsrc );
251 }
252 /**********************************************************************
253 * SizeofResource (KERNEL32.522)
254 */
SizeofResource(HINSTANCE hModule,HRSRC hRsrc)255 DWORD WINAPI SizeofResource( HINSTANCE hModule, HRSRC hRsrc )
256 {
257 return RES_SizeofResource( hModule, hRsrc );
258 }
259
260
261 INT WINAPI LoadStringW( HINSTANCE instance, UINT resource_id,
262 LPWSTR buffer, INT buflen );
263
264 /**********************************************************************
265 * LoadStringA (USER32.375)
266 */
LoadStringA(HINSTANCE instance,UINT resource_id,LPSTR buffer,INT buflen)267 INT WINAPI LoadStringA( HINSTANCE instance, UINT resource_id,
268 LPSTR buffer, INT buflen )
269 {
270 INT retval;
271 INT wbuflen;
272 INT abuflen;
273 LPWSTR wbuf = NULL;
274 LPSTR abuf = NULL;
275
276 if ( buffer != NULL && buflen > 0 )
277 *buffer = 0;
278
279 wbuflen = LoadStringW(instance,resource_id,NULL,0);
280 if ( !wbuflen )
281 return 0;
282 wbuflen ++;
283
284 retval = 0;
285 wbuf = HeapAlloc( GetProcessHeap(), 0, wbuflen * sizeof(WCHAR) );
286 wbuflen = LoadStringW(instance,resource_id,wbuf,wbuflen);
287 if ( wbuflen > 0 )
288 {
289 abuflen = WideCharToMultiByte(CP_ACP,0,wbuf,wbuflen,NULL,0,NULL,NULL);
290 if ( abuflen > 0 )
291 {
292 if ( buffer == NULL || buflen == 0 )
293 retval = abuflen;
294 else
295 {
296 abuf = HeapAlloc( GetProcessHeap(), 0, abuflen * sizeof(CHAR) );
297 abuflen = WideCharToMultiByte(CP_ACP,0,wbuf,wbuflen,abuf,abuflen,NULL,NULL);
298 if ( abuflen > 0 )
299 {
300 abuflen = min(abuflen,buflen - 1);
301 memcpy( buffer, abuf, abuflen );
302 buffer[abuflen] = 0;
303 retval = abuflen;
304 }
305 HeapFree( GetProcessHeap(), 0, abuf );
306 }
307 }
308 }
309 HeapFree( GetProcessHeap(), 0, wbuf );
310
311 return retval;
312 }
313
314 /**********************************************************************
315 * LoadStringW (USER32.376)
316 */
LoadStringW(HINSTANCE instance,UINT resource_id,LPWSTR buffer,INT buflen)317 INT WINAPI LoadStringW( HINSTANCE instance, UINT resource_id,
318 LPWSTR buffer, INT buflen )
319 {
320 HGLOBAL hmem;
321 HRSRC hrsrc;
322 WCHAR *p;
323 int string_num;
324 int i;
325
326 if (HIWORD(resource_id)==0xFFFF) /* netscape 3 passes this */
327 resource_id = (UINT)(-((INT)resource_id));
328 TRACE("instance = %04x, id = %04x, buffer = %08x, "
329 "length = %d\n", instance, (int)resource_id, (int) buffer, buflen);
330
331 /* Use bits 4 - 19 (incremented by 1) as resourceid, mask out
332 * 20 - 31. */
333 hrsrc = FindResourceW( instance, (LPCWSTR)(((resource_id>>4)&0xffff)+1),
334 RT_STRINGW );
335 if (!hrsrc) return 0;
336 hmem = LoadResource( instance, hrsrc );
337 if (!hmem) return 0;
338
339 p = LockResource(hmem);
340 string_num = resource_id & 0x000f;
341 for (i = 0; i < string_num; i++)
342 p += *p + 1;
343
344 TRACE("strlen = %d\n", (int)*p );
345
346 if (buffer == NULL) return *p;
347 i = min(buflen - 1, *p);
348 if (i > 0) {
349 memcpy(buffer, p + 1, i * sizeof (WCHAR));
350 buffer[i] = (WCHAR) 0;
351 } else {
352 if (buflen > 1) {
353 buffer[0] = (WCHAR) 0;
354 return 0;
355 }
356 #if 0
357 WARN("Dont know why caller give buflen=%d *p=%d trying to obtain string '%s'\n", buflen, *p, p + 1);
358 #endif
359 }
360
361 TRACE("String loaded !\n");
362 return i;
363 }
364
365 /* Messages...used by FormatMessage32* (KERNEL32.something)
366 *
367 * They can be specified either directly or using a message ID and
368 * loading them from the resource.
369 *
370 * The resourcedata has following format:
371 * start:
372 * 0: DWORD nrofentries
373 * nrofentries * subentry:
374 * 0: DWORD firstentry
375 * 4: DWORD lastentry
376 * 8: DWORD offset from start to the stringentries
377 *
378 * (lastentry-firstentry) * stringentry:
379 * 0: WORD len (0 marks end)
380 * 2: WORD flags
381 * 4: CHAR[len-4]
382 * (stringentry i of a subentry refers to the ID 'firstentry+i')
383 *
384 * Yes, ANSI strings in win32 resources. Go figure.
385 */
386
387 /**********************************************************************
388 * LoadMessageA (internal)
389 */
LoadMessageA(HMODULE instance,UINT id,WORD lang,LPSTR buffer,INT buflen)390 INT WINAPI LoadMessageA( HMODULE instance, UINT id, WORD lang,
391 LPSTR buffer, INT buflen )
392 {
393 HGLOBAL hmem;
394 HRSRC hrsrc;
395 PMESSAGE_RESOURCE_DATA mrd;
396 PMESSAGE_RESOURCE_BLOCK mrb;
397 PMESSAGE_RESOURCE_ENTRY mre;
398 int i,slen;
399
400 TRACE("instance = %08lx, id = %08lx, buffer = %p, length = %ld\n", (DWORD)instance, (DWORD)id, buffer, (DWORD)buflen);
401
402 /*FIXME: I am not sure about the '1' ... But I've only seen those entries*/
403 hrsrc = FindResourceExW(instance,RT_MESSAGELISTW,(LPWSTR)1,lang);
404 if (!hrsrc) return 0;
405 hmem = LoadResource( instance, hrsrc );
406 if (!hmem) return 0;
407
408 mrd = (PMESSAGE_RESOURCE_DATA)LockResource(hmem);
409 mre = NULL;
410 mrb = &(mrd->Blocks[0]);
411 for (i=mrd->NumberOfBlocks;i--;) {
412 if ((id>=mrb->LowId) && (id<=mrb->HighId)) {
413 mre = (PMESSAGE_RESOURCE_ENTRY)(((char*)mrd)+mrb->OffsetToEntries);
414 id -= mrb->LowId;
415 break;
416 }
417 mrb++;
418 }
419 if (!mre)
420 return 0;
421 for (i=id;i--;) {
422 if (!mre->Length)
423 return 0;
424 mre = (PMESSAGE_RESOURCE_ENTRY)(((char*)mre)+(mre->Length));
425 }
426 slen=mre->Length;
427 TRACE(" - strlen=%d\n",slen);
428 i = min(buflen - 1, slen);
429 if (buffer == NULL)
430 return slen;
431 if (i>0) {
432 lstrcpynA(buffer,(char*)mre->Text,i);
433 buffer[i]=0;
434 } else {
435 if (buflen>1) {
436 buffer[0]=0;
437 return 0;
438 }
439 }
440 if (buffer)
441 TRACE("'%s' copied !\n", buffer);
442 return i;
443 }
444
445
446
447 /**********************************************************************
448 * EnumResourceTypesA (KERNEL32.90)
449 */
EnumResourceTypesA(HMODULE hmodule,ENUMRESTYPEPROCA lpfun,LONG lParam)450 WIN_BOOL WINAPI EnumResourceTypesA( HMODULE hmodule,ENUMRESTYPEPROCA lpfun,
451 LONG lParam)
452 {
453 /* FIXME: move WINE_MODREF stuff here */
454 return PE_EnumResourceTypesA(hmodule,lpfun,lParam);
455 }
456
457 /**********************************************************************
458 * EnumResourceNamesA (KERNEL32.88)
459 */
EnumResourceNamesA(HMODULE hmodule,LPCSTR type,ENUMRESNAMEPROCA lpfun,LONG lParam)460 WIN_BOOL WINAPI EnumResourceNamesA( HMODULE hmodule, LPCSTR type,
461 ENUMRESNAMEPROCA lpfun, LONG lParam )
462 {
463 /* FIXME: move WINE_MODREF stuff here */
464 return PE_EnumResourceNamesA(hmodule,type,lpfun,lParam);
465 }
466 /**********************************************************************
467 * EnumResourceLanguagesA (KERNEL32.86)
468 */
EnumResourceLanguagesA(HMODULE hmodule,LPCSTR type,LPCSTR name,ENUMRESLANGPROCA lpfun,LONG lParam)469 WIN_BOOL WINAPI EnumResourceLanguagesA( HMODULE hmodule, LPCSTR type,
470 LPCSTR name, ENUMRESLANGPROCA lpfun,
471 LONG lParam)
472 {
473 /* FIXME: move WINE_MODREF stuff here */
474 return PE_EnumResourceLanguagesA(hmodule,type,name,lpfun,lParam);
475 }
476 /**********************************************************************
477 * LoadResource (KERNEL32.370)
478 */
LoadResource(HINSTANCE hModule,HRSRC hRsrc)479 HGLOBAL WINAPI LoadResource( HINSTANCE hModule, HRSRC hRsrc )
480 {
481 return RES_LoadResource( hModule, hRsrc);
482 }
483