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