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