Get rid of code I neither know nor use anymore.
[mplayer/glamo.git] / loader / module.c
blob8f87180627aa250aeda66ee9c21284b6eb8c7dea
1 /*
2 * Modules
4 * Copyright 1995 Alexandre Julliard
6 * Modified for use with MPlayer, detailed changelog at
7 * http://svn.mplayerhq.hu/mplayer/trunk/
8 * $Id$
12 // define for quicktime calls debugging and/or MacOS-level emulation:
13 #define EMU_QTX_API
15 // define for quicktime debugging (verbose logging):
16 //#define DEBUG_QTX_API
18 #include "config.h"
20 #include <assert.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <sys/mman.h>
28 #include <inttypes.h>
30 #include "wine/windef.h"
31 #include "wine/winerror.h"
32 #include "wine/heap.h"
33 #include "wine/module.h"
34 #include "wine/pe_image.h"
35 #include "wine/debugtools.h"
37 #undef HAVE_LIBDL
39 #ifdef HAVE_LIBDL
40 #include <dlfcn.h>
41 #include "wine/elfdll.h"
42 #endif
43 #include "win32.h"
44 #include "driver.h"
46 #ifdef EMU_QTX_API
47 #include "wrapper.h"
48 static int report_func(void *stack_base, int stack_size, reg386_t *reg, uint32_t *flags);
49 static int report_func_ret(void *stack_base, int stack_size, reg386_t *reg, uint32_t *flags);
50 #endif
52 //#undef TRACE
53 //#define TRACE printf
55 //WINE_MODREF *local_wm=NULL;
56 modref_list* local_wm=NULL;
58 HANDLE SegptrHeap;
60 WINE_MODREF* MODULE_FindModule(LPCSTR m)
62 modref_list* list=local_wm;
63 TRACE("FindModule: Module %s request\n", m);
64 if(list==NULL)
65 return NULL;
66 // while(strcmp(m, list->wm->filename))
67 while(!strstr(list->wm->filename, m))
69 TRACE("%s: %x\n", list->wm->filename, list->wm->module);
70 list=list->prev;
71 if(list==NULL)
72 return NULL;
74 TRACE("Resolved to %s\n", list->wm->filename);
75 return list->wm;
78 static void MODULE_RemoveFromList(WINE_MODREF *mod)
80 modref_list* list=local_wm;
81 if(list==0)
82 return;
83 if(mod==0)
84 return;
85 if((list->prev==NULL)&&(list->next==NULL))
87 free(list);
88 local_wm=NULL;
89 // uninstall_fs();
90 return;
92 for(;list;list=list->prev)
94 if(list->wm==mod)
96 if(list->prev)
97 list->prev->next=list->next;
98 if(list->next)
99 list->next->prev=list->prev;
100 if(list==local_wm)
101 local_wm=list->prev;
102 free(list);
103 return;
109 WINE_MODREF *MODULE32_LookupHMODULE(HMODULE m)
111 modref_list* list=local_wm;
112 TRACE("LookupHMODULE: Module %X request\n", m);
113 if(list==NULL)
115 TRACE("LookupHMODULE failed\n");
116 return NULL;
118 while(m!=list->wm->module)
120 // printf("Checking list %X wm %X module %X\n",
121 // list, list->wm, list->wm->module);
122 list=list->prev;
123 if(list==NULL)
125 TRACE("LookupHMODULE failed\n");
126 return NULL;
129 TRACE("LookupHMODULE hit %p\n", list->wm);
130 return list->wm;
133 /*************************************************************************
134 * MODULE_InitDll
136 static WIN_BOOL MODULE_InitDll( WINE_MODREF *wm, DWORD type, LPVOID lpReserved )
138 WIN_BOOL retv = TRUE;
140 static LPCSTR typeName[] = { "PROCESS_DETACH", "PROCESS_ATTACH",
141 "THREAD_ATTACH", "THREAD_DETACH" };
142 assert( wm );
145 /* Skip calls for modules loaded with special load flags */
147 if ( ( wm->flags & WINE_MODREF_DONT_RESOLVE_REFS )
148 || ( wm->flags & WINE_MODREF_LOAD_AS_DATAFILE ) )
149 return TRUE;
152 TRACE("(%s,%s,%p) - CALL\n", wm->modname, typeName[type], lpReserved );
154 /* Call the initialization routine */
155 switch ( wm->type )
157 case MODULE32_PE:
158 retv = PE_InitDLL( wm, type, lpReserved );
159 break;
161 case MODULE32_ELF:
162 /* no need to do that, dlopen() already does */
163 break;
165 default:
166 ERR("wine_modref type %d not handled.\n", wm->type );
167 retv = FALSE;
168 break;
171 /* The state of the module list may have changed due to the call
172 to PE_InitDLL. We cannot assume that this module has not been
173 deleted. */
174 TRACE("(%p,%s,%p) - RETURN %d\n", wm, typeName[type], lpReserved, retv );
176 return retv;
179 /*************************************************************************
180 * MODULE_DllProcessAttach
182 * Send the process attach notification to all DLLs the given module
183 * depends on (recursively). This is somewhat complicated due to the fact that
185 * - we have to respect the module dependencies, i.e. modules implicitly
186 * referenced by another module have to be initialized before the module
187 * itself can be initialized
189 * - the initialization routine of a DLL can itself call LoadLibrary,
190 * thereby introducing a whole new set of dependencies (even involving
191 * the 'old' modules) at any time during the whole process
193 * (Note that this routine can be recursively entered not only directly
194 * from itself, but also via LoadLibrary from one of the called initialization
195 * routines.)
197 * Furthermore, we need to rearrange the main WINE_MODREF list to allow
198 * the process *detach* notifications to be sent in the correct order.
199 * This must not only take into account module dependencies, but also
200 * 'hidden' dependencies created by modules calling LoadLibrary in their
201 * attach notification routine.
203 * The strategy is rather simple: we move a WINE_MODREF to the head of the
204 * list after the attach notification has returned. This implies that the
205 * detach notifications are called in the reverse of the sequence the attach
206 * notifications *returned*.
208 * NOTE: Assumes that the process critical section is held!
211 static WIN_BOOL MODULE_DllProcessAttach( WINE_MODREF *wm, LPVOID lpReserved )
213 WIN_BOOL retv = TRUE;
214 int i;
215 assert( wm );
217 /* prevent infinite recursion in case of cyclical dependencies */
218 if ( ( wm->flags & WINE_MODREF_MARKER )
219 || ( wm->flags & WINE_MODREF_PROCESS_ATTACHED ) )
220 return retv;
222 TRACE("(%s,%p) - START\n", wm->modname, lpReserved );
224 /* Tag current MODREF to prevent recursive loop */
225 wm->flags |= WINE_MODREF_MARKER;
227 /* Recursively attach all DLLs this one depends on */
228 /* for ( i = 0; retv && i < wm->nDeps; i++ )
229 if ( wm->deps[i] )
230 retv = MODULE_DllProcessAttach( wm->deps[i], lpReserved );
232 /* Call DLL entry point */
234 //local_wm=wm;
235 if(local_wm)
237 local_wm->next = (modref_list*) malloc(sizeof(modref_list));
238 local_wm->next->prev=local_wm;
239 local_wm->next->next=NULL;
240 local_wm->next->wm=wm;
241 local_wm=local_wm->next;
243 else
245 local_wm = malloc(sizeof(modref_list));
246 local_wm->next=local_wm->prev=NULL;
247 local_wm->wm=wm;
249 /* Remove recursion flag */
250 wm->flags &= ~WINE_MODREF_MARKER;
252 if ( retv )
254 retv = MODULE_InitDll( wm, DLL_PROCESS_ATTACH, lpReserved );
255 if ( retv )
256 wm->flags |= WINE_MODREF_PROCESS_ATTACHED;
260 TRACE("(%s,%p) - END\n", wm->modname, lpReserved );
262 return retv;
265 /*************************************************************************
266 * MODULE_DllProcessDetach
268 * Send DLL process detach notifications. See the comment about calling
269 * sequence at MODULE_DllProcessAttach. Unless the bForceDetach flag
270 * is set, only DLLs with zero refcount are notified.
272 static void MODULE_DllProcessDetach( WINE_MODREF* wm, WIN_BOOL bForceDetach, LPVOID lpReserved )
274 // WINE_MODREF *wm=local_wm;
275 modref_list* l = local_wm;
276 wm->flags &= ~WINE_MODREF_PROCESS_ATTACHED;
277 MODULE_InitDll( wm, DLL_PROCESS_DETACH, lpReserved );
278 /* while (l)
280 modref_list* f = l;
281 l = l->next;
282 free(f);
284 local_wm = 0;*/
287 /***********************************************************************
288 * MODULE_LoadLibraryExA (internal)
290 * Load a PE style module according to the load order.
292 * The HFILE parameter is not used and marked reserved in the SDK. I can
293 * only guess that it should force a file to be mapped, but I rather
294 * ignore the parameter because it would be extremely difficult to
295 * integrate this with different types of module represenations.
298 static WINE_MODREF *MODULE_LoadLibraryExA( LPCSTR libname, HFILE hfile, DWORD flags )
300 DWORD err = GetLastError();
301 WINE_MODREF *pwm;
302 int i;
303 // module_loadorder_t *plo;
305 SetLastError( ERROR_FILE_NOT_FOUND );
306 TRACE("Trying native dll '%s'\n", libname);
307 pwm = PE_LoadLibraryExA(libname, flags);
308 #ifdef HAVE_LIBDL
309 if(!pwm)
311 TRACE("Trying ELF dll '%s'\n", libname);
312 pwm=(WINE_MODREF*)ELFDLL_LoadLibraryExA(libname, flags);
314 #endif
315 // printf("0x%08x\n", pwm);
316 // break;
317 if(pwm)
319 /* Initialize DLL just loaded */
320 TRACE("Loaded module '%s' at 0x%08x, \n", libname, pwm->module);
321 /* Set the refCount here so that an attach failure will */
322 /* decrement the dependencies through the MODULE_FreeLibrary call. */
323 pwm->refCount++;
325 SetLastError( err ); /* restore last error */
326 return pwm;
330 WARN("Failed to load module '%s'; error=0x%08lx, \n", libname, GetLastError());
331 return NULL;
334 /***********************************************************************
335 * MODULE_FreeLibrary
337 * NOTE: Assumes that the process critical section is held!
339 static WIN_BOOL MODULE_FreeLibrary( WINE_MODREF *wm )
341 TRACE("(%s) - START\n", wm->modname );
343 /* Recursively decrement reference counts */
344 //MODULE_DecRefCount( wm );
346 /* Call process detach notifications */
347 MODULE_DllProcessDetach( wm, FALSE, NULL );
349 PE_UnloadLibrary(wm);
351 TRACE("END\n");
353 return TRUE;
356 /***********************************************************************
357 * LoadLibraryExA (KERNEL32)
359 HMODULE WINAPI LoadLibraryExA(LPCSTR libname, HANDLE hfile, DWORD flags)
361 WINE_MODREF *wm = 0;
362 char* listpath[] = { "", "", "/usr/lib/win32", "/usr/local/lib/win32", 0 };
363 extern char* def_path;
364 char path[512];
365 char checked[2000];
366 int i = -1;
368 checked[0] = 0;
369 if(!libname)
371 SetLastError(ERROR_INVALID_PARAMETER);
372 return 0;
375 wm=MODULE_FindModule(libname);
376 if(wm) return wm->module;
378 // if(fs_installed==0)
379 // install_fs();
381 while (wm == 0 && listpath[++i])
383 if (i < 2)
385 if (i == 0)
386 /* check just original file name */
387 strncpy(path, libname, 511);
388 else
389 /* check default user path */
390 strncpy(path, def_path, 300);
392 else if (strcmp(def_path, listpath[i]))
393 /* path from the list */
394 strncpy(path, listpath[i], 300);
395 else
396 continue;
398 if (i > 0)
400 strcat(path, "/");
401 strncat(path, libname, 100);
403 path[511] = 0;
404 wm = MODULE_LoadLibraryExA( path, hfile, flags );
406 if (!wm)
408 if (checked[0])
409 strcat(checked, ", ");
410 strcat(checked, path);
411 checked[1500] = 0;
415 if ( wm )
417 if ( !MODULE_DllProcessAttach( wm, NULL ) )
419 WARN_(module)("Attach failed for module '%s', \n", libname);
420 MODULE_FreeLibrary(wm);
421 SetLastError(ERROR_DLL_INIT_FAILED);
422 MODULE_RemoveFromList(wm);
423 wm = NULL;
427 if (!wm)
428 printf("Win32 LoadLibrary failed to load: %s\n", checked);
430 #define RVA(x) ((char *)wm->module+(unsigned int)(x))
431 if (strstr(libname,"vp31vfw.dll") && wm)
433 int i;
435 // sse hack moved from patch dll into runtime patching
436 if (PE_FindExportedFunction(wm, "DriverProc", TRUE)==RVA(0x1000)) {
437 fprintf(stderr, "VP3 DLL found\n");
438 for (i=0;i<18;i++) RVA(0x4bd6)[i]=0x90;
442 // remove a few divs in the VP codecs that make trouble
443 if (strstr(libname,"vp5vfw.dll") && wm)
445 int i;
446 if (PE_FindExportedFunction(wm, "DriverProc", TRUE)==RVA(0x3930)) {
447 for (i=0;i<3;i++) RVA(0x4e86)[i]=0x90;
448 for (i=0;i<3;i++) RVA(0x5a23)[i]=0x90;
449 for (i=0;i<3;i++) RVA(0x5bff)[i]=0x90;
450 } else {
451 fprintf(stderr, "Unsupported VP5 version\n");
452 return 0;
456 if (strstr(libname,"vp6vfw.dll") && wm)
458 int i;
459 if (PE_FindExportedFunction(wm, "DriverProc", TRUE)==RVA(0x3ef0)) {
460 // looks like VP 6.1.0.2
461 for (i=0;i<6;i++) RVA(0x7268)[i]=0x90;
462 for (i=0;i<6;i++) RVA(0x7e83)[i]=0x90;
463 for (i=0;i<6;i++) RVA(0x806a)[i]=0x90;
464 } else if (PE_FindExportedFunction(wm, "DriverProc", TRUE)==RVA(0x4120)) {
465 // looks like VP 6.2.0.10
466 for (i=0;i<6;i++) RVA(0x7688)[i]=0x90;
467 for (i=0;i<6;i++) RVA(0x82c3)[i]=0x90;
468 for (i=0;i<6;i++) RVA(0x84aa)[i]=0x90;
469 for (i=0;i<6;i++) RVA(0x1d2cc)[i]=0x90;
470 for (i=0;i<6;i++) RVA(0x2179d)[i]=0x90;
471 for (i=0;i<6;i++) RVA(0x1977f)[i]=0x90;
472 } else if (PE_FindExportedFunction(wm, "DriverProc", TRUE)==RVA(0x3e70)) {
473 // looks like VP 6.0.7.3
474 for (i=0;i<6;i++) RVA(0x7559)[i]=0x90;
475 for (i=0;i<6;i++) RVA(0x81c3)[i]=0x90;
476 for (i=0;i<6;i++) RVA(0x839e)[i]=0x90;
477 } else {
478 fprintf(stderr, "Unsupported VP6 version\n");
479 return 0;
483 // Windows Media Video 9 Advanced
484 if (strstr(libname,"wmvadvd.dll") && wm)
486 // The codec calls IsRectEmpty with coords 0,0,0,0 => result is 0
487 // but it really wants the rectangle to be not empty
488 if (PE_FindExportedFunction(wm, "CreateInstance", TRUE)==RVA(0xb812)) {
489 // Dll version is 10.0.0.3645
490 *RVA(0x8b0f)=0xeb; // Jump always, ignoring IsRectEmpty result
491 } else {
492 fprintf(stderr, "Unsupported WMVA version\n");
493 return 0;
497 if (strstr(libname,"QuickTime.qts") && wm)
499 void** ptr;
500 void *dispatch_addr;
501 int i;
503 // dispatch_addr = GetProcAddress(wm->module, "theQuickTimeDispatcher", TRUE);
504 dispatch_addr = PE_FindExportedFunction(wm, "theQuickTimeDispatcher", TRUE);
505 if (dispatch_addr == RVA(0x124c30))
507 fprintf(stderr, "QuickTime5 DLLs found\n");
508 ptr = (void **)RVA(0x375ca4); // dispatch_ptr
509 for (i=0;i<5;i++) RVA(0x19e842)[i]=0x90; // make_new_region ?
510 for (i=0;i<28;i++) RVA(0x19e86d)[i]=0x90; // call__call_CreateCompatibleDC ?
511 for (i=0;i<5;i++) RVA(0x19e898)[i]=0x90; // jmp_to_call_loadbitmap ?
512 for (i=0;i<9;i++) RVA(0x19e8ac)[i]=0x90; // call__calls_OLE_shit ?
513 for (i=0;i<106;i++) RVA(0x261b10)[i]=0x90; // disable threads
514 #if 0
515 /* CreateThread callers */
516 for (i=0;i<5;i++) RVA(0x1487c5)[i]=0x90;
517 for (i=0;i<5;i++) RVA(0x14b275)[i]=0x90;
518 for (i=0;i<5;i++) RVA(0x1a24b1)[i]=0x90;
519 for (i=0;i<5;i++) RVA(0x1afc5a)[i]=0x90;
520 for (i=0;i<5;i++) RVA(0x2f799c)[i]=0x90;
521 for (i=0;i<5;i++) RVA(0x2f7efe)[i]=0x90;
522 for (i=0;i<5;i++) RVA(0x2fa33e)[i]=0x90;
523 #endif
525 #if 0
526 /* TerminateQTML fix */
527 for (i=0;i<47;i++) RVA(0x2fa3b8)[i]=0x90; // terminate thread
528 for (i=0;i<47;i++) RVA(0x2f7f78)[i]=0x90; // terminate thread
529 for (i=0;i<77;i++) RVA(0x1a13d5)[i]=0x90;
530 RVA(0x08e0ae)[0] = 0xc3; // font/dc remover
531 for (i=0;i<24;i++) RVA(0x07a1ad)[i]=0x90; // destroy window
532 #endif
533 } else if (dispatch_addr == RVA(0x13b330))
535 fprintf(stderr, "QuickTime6 DLLs found\n");
536 ptr = (void **)RVA(0x3b9524); // dispatcher_ptr
537 for (i=0;i<5;i++) RVA(0x2730cc)[i]=0x90; // make_new_region
538 for (i=0;i<28;i++) RVA(0x2730f7)[i]=0x90; // call__call_CreateCompatibleDC
539 for (i=0;i<5;i++) RVA(0x273122)[i]=0x90; // jmp_to_call_loadbitmap
540 for (i=0;i<9;i++) RVA(0x273131)[i]=0x90; // call__calls_OLE_shit
541 for (i=0;i<96;i++) RVA(0x2ac852)[i]=0x90; // disable threads
542 } else if (dispatch_addr == RVA(0x13c3e0))
544 fprintf(stderr, "QuickTime6.3 DLLs found\n");
545 ptr = (void **)RVA(0x3ca01c); // dispatcher_ptr
546 for (i=0;i<5;i++) RVA(0x268f6c)[i]=0x90; // make_new_region
547 for (i=0;i<28;i++) RVA(0x268f97)[i]=0x90; // call__call_CreateCompatibleDC
548 for (i=0;i<5;i++) RVA(0x268fc2)[i]=0x90; // jmp_to_call_loadbitmap
549 for (i=0;i<9;i++) RVA(0x268fd1)[i]=0x90; // call__calls_OLE_shit
550 for (i=0;i<96;i++) RVA(0x2b4722)[i]=0x90; // disable threads
551 } else
553 fprintf(stderr, "Unsupported QuickTime version (%p)\n",
554 dispatch_addr);
555 return 0;
558 fprintf(stderr,"QuickTime.qts patched!!! old entry=%p\n",ptr[0]);
560 #ifdef EMU_QTX_API
561 report_entry = report_func;
562 report_ret = report_func_ret;
563 wrapper_target=ptr[0];
564 ptr[0]=wrapper;
565 #endif
567 #undef RVA
569 return wm ? wm->module : 0;
573 /***********************************************************************
574 * LoadLibraryA (KERNEL32)
576 HMODULE WINAPI LoadLibraryA(LPCSTR libname) {
577 return LoadLibraryExA(libname,0,0);
580 /***********************************************************************
581 * FreeLibrary
583 WIN_BOOL WINAPI FreeLibrary(HINSTANCE hLibModule)
585 WIN_BOOL retv = FALSE;
586 WINE_MODREF *wm;
588 wm=MODULE32_LookupHMODULE(hLibModule);
590 if ( !wm || !hLibModule )
592 SetLastError( ERROR_INVALID_HANDLE );
593 return 0;
595 else
596 retv = MODULE_FreeLibrary( wm );
598 MODULE_RemoveFromList(wm);
600 /* garbage... */
601 if (local_wm == NULL) my_garbagecollection();
603 return retv;
606 /***********************************************************************
607 * MODULE_DecRefCount
609 * NOTE: Assumes that the process critical section is held!
611 static void MODULE_DecRefCount( WINE_MODREF *wm )
613 int i;
615 if ( wm->flags & WINE_MODREF_MARKER )
616 return;
618 if ( wm->refCount <= 0 )
619 return;
621 --wm->refCount;
622 TRACE("(%s) refCount: %d\n", wm->modname, wm->refCount );
624 if ( wm->refCount == 0 )
626 wm->flags |= WINE_MODREF_MARKER;
628 for ( i = 0; i < wm->nDeps; i++ )
629 if ( wm->deps[i] )
630 MODULE_DecRefCount( wm->deps[i] );
632 wm->flags &= ~WINE_MODREF_MARKER;
636 /***********************************************************************
637 * GetProcAddress (KERNEL32.257)
639 FARPROC WINAPI GetProcAddress( HMODULE hModule, LPCSTR function )
641 return MODULE_GetProcAddress( hModule, function, TRUE );
644 #ifdef DEBUG_QTX_API
647 http://lists.apple.com/archives/quicktime-api/2003/Jan/msg00278.html
650 struct ComponentParameters {
651 unsigned char flags; /* call modifiers: sync/async, deferred, immed, etc */
652 unsigned char paramSize; /* size in bytes of actual parameters passed to this call */
653 short what; /* routine selector, negative for Component management calls */
654 long params[1]; /* actual parameters for the indicated routine */
656 typedef struct ComponentParameters ComponentParameters;
658 static char* component_func(int what){
659 if (what < 0) // Range 0: Standard Component Calls
660 switch(what){
661 case -1: return "kComponentOpenSelect";
662 case -2: return "kComponentCloseSelect";
663 case -3: return "kComponentCanDoSelect";
664 case -4: return "kComponentVersionSelect";
665 case -5: return "kComponentRegisterSelect";
666 case -6: return "kComponentTargetSelect";
667 case -7: return "kComponentUnregisterSelect";
670 if (what >= 0 && what <= 0xff) // Range 1: Generic codecs
671 switch(what & 0xff){
672 case 0: return "kImageCodecGetCodecInfoSelect";
673 case 1: return "kImageCodecGetCompressionTimeSelect";
674 case 2: return "kImageCodecGetMaxCompressionSizeSelect";
675 case 3: return "kImageCodecPreCompressSelect";
676 case 4: return "kImageCodecBandCompressSelect";
677 case 5: return "kImageCodecPreDecompressSelect";
678 case 6: return "kImageCodecBandDecompressSelect";
679 case 7: return "kImageCodecBusySelect";
680 // finish this list from the above URL
681 case 0x10: return "kImageCodecIsImageDescriptionEquivalentSelect";
682 case 0x12: return "kImageCodecDisposeMemorySelect";
683 case 0x14: return "kImageCodecNewImageBufferMemorySelect";
684 case 0x28: return "kImageCodecRequestGammaLevelSelect";
687 //if (what >= 0x100 && what <= 0x1ff) // Range 2: Specific to QT Photo JPEG codecs
689 if (what >= 0x200 && what <= 0x2ff) // Range 3: Base Decompressor
690 switch(what & 0xff){
691 case 0: return "Preflight";
692 case 1: return "Initialize";
693 case 2: return "BeginBand";
694 case 3: return "DrawBand";
695 case 4: return "EndBand";
696 case 5: return "QueueStarting";
697 case 6: return "QueueStopping";
700 return "???";
703 static int c_level=0;
705 static int dump_component(char* name,int type,void* _orig, ComponentParameters *params,void** glob){
706 int ( *orig)(ComponentParameters *params, void** glob) = _orig;
707 int ret,i;
709 fprintf(stderr,"%*sComponentCall: %s flags=0x%X size=%d what=0x%X %s\n",3*c_level,"",name,params->flags, params->paramSize, params->what, component_func(params->what));
711 for(i=0;i<params->paramSize/4;i++)
712 fprintf(stderr,"%*s param[%d] = 0x%X\n",3*c_level,"",i,params->params[i]);
714 ++c_level;
715 ret=orig(params,glob);
716 --c_level;
718 if(ret>=0x1000)
719 fprintf(stderr,"%*s return=0x%X\n",3*c_level,"",ret);
720 else
721 fprintf(stderr,"%*s return=%d\n",3*c_level,"",ret);
722 return ret;
725 #define DECL_COMPONENT(sname,name,type) \
726 static void* real_ ## sname = NULL; \
727 static int fake_ ## sname(ComponentParameters *params,void** glob){ \
728 return dump_component(name,type,real_ ## sname, params, glob); \
731 #include "qt_comp.h"
733 #undef DECL_COMPONENT
735 #include "qt_fv.h"
737 #endif
739 #ifdef EMU_QTX_API
741 static uint32_t ret_array[4096];
742 static int ret_i=0;
744 static int report_func(void *stack_base, int stack_size, reg386_t *reg, uint32_t *flags)
746 #ifdef DEBUG_QTX_API
747 int i;
748 int* dptr;
749 void* pwrapper=NULL;
750 void* pptr=NULL;
751 char* pname=NULL;
752 int plen=-1;
753 // find the code:
755 dptr=0x62b67ae0;dptr+=2*((reg->eax>>16)&255);
756 // printf("FUNC: flag=%d ptr=%p\n",dptr[0],dptr[1]);
757 if(dptr[0]&255){
758 dptr=dptr[1];dptr+=4*(reg->eax&65535);
759 // printf("FUNC: ptr2=%p eax=%p edx=%p\n",dptr[1],dptr[0],dptr[2]);
760 pwrapper=dptr[1]; pptr=dptr[0]; plen=dptr[2];
761 } else {
762 pwrapper=0x62924910;
763 switch(dptr[1]){
764 case 0x629248d0:
765 dptr=0x62b672c0;dptr+=2*(reg->eax&65535);
766 // printf("FUNC: ptr2=%p eax=%p edx=%p\n",0x62924910,dptr[0],dptr[1]);
767 pptr=dptr[0]; plen=dptr[1];
768 break;
769 case 0x62924e40:
770 dptr=0x62b67c70;dptr+=2*(reg->eax&65535);
771 // printf("FUNC: ptr2=%p eax=%p edx=%p\n",0x62924910,dptr[0],dptr[1]);
772 pptr=dptr[0]; plen=dptr[1];
773 break;
774 case 0x62924e60:
775 dptr=0x62b68108;if(reg->eax&0x8000) dptr+=2*(reg->eax|0xffff0000); else dptr+=2*(reg->eax&65535);
776 // printf("FUNC: ptr2=%p eax=%p edx=%p\n",0x62924910,dptr[0],dptr[1]);
777 pptr=dptr[0]; plen=dptr[1];
778 break;
779 case 0x62924e80:
780 dptr=0x62b68108;if(reg->eax&0x8000) dptr+=2*(reg->eax|0xffff0000); else dptr+=2*(reg->eax&65535);
781 // printf("FUNC: ptr2=%p eax=%p edx=%p\n",0x62924910,dptr[0],dptr[1]);
782 pptr=dptr[0]; plen=dptr[1];
783 break;
784 default:
785 printf("FUNC: unknown ptr & psize!\n");
786 pwrapper=dptr[1];
790 for(i=0;qt_fv_list[i].name;i++){
791 if(qt_fv_list[i].id==reg->eax){
792 pname=qt_fv_list[i].name;
793 break;
797 printf("FUNC[%X/%s]: wrapper=%p func=%p len=%d\n",reg->eax,
798 pname?pname:"???",pwrapper,pptr,plen);
800 printf("FUNC: caller=%p ebx=%p\n",((uint32_t *)stack_base)[0],reg->ebx);
802 if(pname)
803 printf("%*sENTER(%d): %s(",ret_i*2,"",ret_i,pname);
804 else
805 printf("%*sENTER(%d): %X(",ret_i*2,"",ret_i,reg->eax);
806 for (i=0;i<plen/4;i++){
807 unsigned int val=((uint32_t *)stack_base)[1+i];
808 unsigned char* fcc=&val;
809 printf("%s0x%X", i?", ":"",val);
810 if(fcc[0]>=0x20 && fcc[0]<128 &&
811 fcc[1]>=0x20 && fcc[1]<128 &&
812 fcc[2]>=0x20 && fcc[2]<128 &&
813 fcc[3]>=0x20 && fcc[3]<128) printf("='%c%c%c%c'",fcc[3],fcc[2],fcc[1],fcc[0]);
814 else if(val>=8 && val<65536) printf("=%d",val);
816 printf(")\n");
817 fflush(stdout);
819 #endif
821 #if 1
822 // emulate some functions:
823 switch(reg->eax){
824 // memory management:
825 case 0x150011: //NewPtrClear
826 case 0x150012: //NewPtrSysClear
827 reg->eax=(uint32_t)malloc(((uint32_t *)stack_base)[1]);
828 memset((void *)reg->eax,0,((uint32_t *)stack_base)[1]);
829 #ifdef DEBUG_QTX_API
830 printf("%*sLEAVE(%d): EMULATED! 0x%X\n",ret_i*2,"",ret_i, reg->eax);
831 #endif
832 return 1;
833 case 0x15000F: //NewPtr
834 case 0x150010: //NewPtrSys
835 reg->eax=(uint32_t)malloc(((uint32_t *)stack_base)[1]);
836 #ifdef DEBUG_QTX_API
837 printf("%*sLEAVE(%d): EMULATED! 0x%X\n",ret_i*2,"",ret_i, reg->eax);
838 #endif
839 return 1;
840 case 0x15002f: //DisposePtr
841 if(((uint32_t *)stack_base)[1]>=0x60000000)
842 printf("WARNING! Invalid Ptr handle!\n");
843 else
844 free((void *)((uint32_t *)stack_base)[1]);
845 reg->eax=0;
846 #ifdef DEBUG_QTX_API
847 printf("%*sLEAVE(%d): EMULATED! 0x%X\n",ret_i*2,"",ret_i, reg->eax);
848 #endif
849 return 1;
850 // mutexes:
851 case 0x1d0033: //QTMLCreateMutex
852 reg->eax=0xdeadbabe;
853 #ifdef DEBUG_QTX_API
854 printf("%*sLEAVE(%d): EMULATED! 0x%X\n",ret_i*2,"",ret_i, reg->eax);
855 #endif
856 return 1;
857 case 0x1d0034: //QTMLDestroyMutex
858 case 0x1d0035: //QTMLGrabMutex
859 case 0x1d0036: //QTMLReturnMutex
860 case 0x1d003d: //QTMLTryGrabMutex
861 reg->eax=0;
862 #ifdef DEBUG_QTX_API
863 printf("%*sLEAVE(%d): EMULATED! 0x%X\n",ret_i*2,"",ret_i, reg->eax);
864 #endif
865 return 1;
867 #endif
869 #if 0
870 switch(reg->eax){
871 // case 0x00010000:
872 // printf("FUNC: ImageCodecInitialize/ImageCodecGetCodecInfo(ci=%p,&icap=%p)\n",((uint32_t *)stack_base)[1],((uint32_t *)stack_base)[4]);
873 // break;
874 case 0x00010003:
875 printf("FUNC: CountComponents(&desc=%p)\n",((uint32_t *)stack_base)[1]);
876 break;
877 case 0x00010004:
878 printf("FUNC: FindNextComponent(prev=%p,&desc=%p)\n",((uint32_t *)stack_base)[1],((uint32_t *)stack_base)[2]);
879 break;
880 case 0x00010007:
881 printf("FUNC: OpenComponent(prev=%p)\n",((uint32_t *)stack_base)[1]);
882 break;
883 case 0x0003008b:
884 printf("FUNC: QTNewGWorldFromPtr(&pts=%p,fourcc=%.4s,&rect=%p,x1=%p,x2=%p,x3=%p,plane=%p,stride=%d)\n",
885 ((uint32_t *)stack_base)[1],
886 &(((uint32_t *)stack_base)[2]),
887 ((uint32_t *)stack_base)[3],
888 ((uint32_t *)stack_base)[4],
889 ((uint32_t *)stack_base)[5],
890 ((uint32_t *)stack_base)[6],
891 ((uint32_t *)stack_base)[7],
892 ((uint32_t *)stack_base)[8]);
893 break;
894 case 0x001c0018:
895 printf("FUNC: GetGWorldPixMap(gworld=%p)\n",((uint32_t *)stack_base)[1]);
896 break;
897 case 0x00110001:
898 printf("FUNC: Gestalt(fourcc=%.4s, &ret=%p)\n",&(((uint32_t *)stack_base)[1]),((uint32_t *)stack_base)[2]);
899 break;
900 default: {
901 int i;
902 for(i=0;qt_fv_list[i].name;i++){
903 if(qt_fv_list[i].id==reg->eax){
904 printf("FUNC: %s\n",qt_fv_list[i].name);
905 break;
911 // print stack/reg information
912 printf("ENTER(%d) stack = %d bytes @ %p\n"
913 "eax = 0x%08x edx = 0x%08x ebx = 0x%08x ecx = 0x%08x\n"
914 "esp = 0x%08x ebp = 0x%08x esi = 0x%08x edi = 0x%08x\n"
915 "flags = 0x%08x\n", ret_i,
916 stack_size, stack_base,
917 reg->eax, reg->edx, reg->ebx, reg->ecx,
918 reg->esp, reg->ebp, reg->esi, reg->edi,
919 *flags);
920 #endif
922 // save ret addr:
923 ret_array[ret_i]=((uint32_t *)stack_base)[0];
924 ++ret_i;
926 #if 0
927 // print first 7 longs in the stack (return address, arg[1], arg[2] ... )
928 printf("stack[] = { ");
929 for (i=0;i<7;i++) {
930 printf("%08x ", ((uint32_t *)stack_base)[i]);
932 printf("}\n\n");
933 #endif
935 // // mess with function parameters
936 // ((uint32_t *)stack_base)[1] = 0x66554433;
938 // // mess with return address...
939 // reg->eax = 0x11223344;
940 return 0;
943 static int report_func_ret(void *stack_base, int stack_size, reg386_t *reg, uint32_t *flags)
945 int i;
946 short err;
948 // restore ret addr:
949 --ret_i;
950 ((uint32_t *)stack_base)[0]=ret_array[ret_i];
952 #ifdef DEBUG_QTX_API
954 #if 1
955 printf("%*sLEAVE(%d): 0x%X",ret_i*2,"",ret_i, reg->eax);
956 err=reg->eax;
957 if(err && (reg->eax>>16)==0) printf(" = %d",err);
958 printf("\n");
959 fflush(stdout);
960 #else
961 // print stack/reg information
962 printf("LEAVE(%d) stack = %d bytes @ %p\n"
963 "eax = 0x%08x edx = 0x%08x ebx = 0x%08x ecx = 0x%08x\n"
964 "esp = 0x%08x ebp = 0x%08x esi = 0x%08x edi = 0x%08x\n"
965 "flags = 0x%08x\n", ret_i,
966 stack_size, stack_base,
967 reg->eax, reg->edx, reg->ebx, reg->ecx,
968 reg->esp, reg->ebp, reg->esi, reg->edi,
969 *flags);
970 #endif
972 #if 0
973 // print first 7 longs in the stack (return address, arg[1], arg[2] ... )
974 printf("stack[] = { ");
975 for (i=0;i<7;i++) {
976 printf("%08x ", ((uint32_t *)stack_base)[i]);
978 printf("}\n\n");
979 #endif
981 #endif
983 // // mess with function parameters
984 // ((uint32_t *)stack_base)[1] = 0x66554433;
986 // // mess with return address...
987 // reg->eax = 0x11223344;
988 return 0;
991 #endif
993 /***********************************************************************
994 * MODULE_GetProcAddress (internal)
996 FARPROC MODULE_GetProcAddress(
997 HMODULE hModule, /* [in] current module handle */
998 LPCSTR function, /* [in] function to be looked up */
999 WIN_BOOL snoop )
1001 WINE_MODREF *wm = MODULE32_LookupHMODULE( hModule );
1002 // WINE_MODREF *wm=local_wm;
1003 FARPROC retproc;
1005 #ifdef DEBUG_QTX_API
1006 if (HIWORD(function))
1007 fprintf(stderr,"XXX GetProcAddress(%08lx,%s)\n",(DWORD)hModule,function);
1008 else
1009 fprintf(stderr,"XXX GetProcAddress(%08lx,%p)\n",(DWORD)hModule,function);
1010 #endif
1012 // TRACE_(win32)("(%08lx,%s)\n",(DWORD)hModule,function);
1013 // else
1014 // TRACE_(win32)("(%08lx,%p)\n",(DWORD)hModule,function);
1016 if (!wm) {
1017 SetLastError(ERROR_INVALID_HANDLE);
1018 return (FARPROC)0;
1020 switch (wm->type)
1022 case MODULE32_PE:
1023 retproc = PE_FindExportedFunction( wm, function, snoop );
1024 if (!retproc) SetLastError(ERROR_PROC_NOT_FOUND);
1025 break;
1026 #ifdef HAVE_LIBDL
1027 case MODULE32_ELF:
1028 retproc = (FARPROC) dlsym( (void*) wm->module, function);
1029 if (!retproc) SetLastError(ERROR_PROC_NOT_FOUND);
1030 return retproc;
1031 #endif
1032 default:
1033 ERR("wine_modref type %d not handled.\n",wm->type);
1034 SetLastError(ERROR_INVALID_HANDLE);
1035 return (FARPROC)0;
1038 #ifdef EMU_QTX_API
1039 if (HIWORD(function) && retproc){
1041 #ifdef DEBUG_QTX_API
1042 #define DECL_COMPONENT(sname,name,type) \
1043 if(!strcmp(function,name)){ \
1044 fprintf(stderr,name "dispatcher catched -> %p\n",retproc); \
1045 real_ ## sname = retproc; retproc = fake_ ## sname; \
1047 #include "qt_comp.h"
1048 #undef DECL_COMPONENT
1049 #endif
1051 if(!strcmp(function,"theQuickTimeDispatcher")
1052 // || !strcmp(function,"_CallComponentFunctionWithStorage")
1053 // || !strcmp(function,"_CallComponent")
1055 fprintf(stderr,"theQuickTimeDispatcher catched -> %p\n",retproc);
1056 report_entry = report_func;
1057 report_ret = report_func_ret;
1058 wrapper_target=(void(*)(void))retproc;
1059 retproc=(FARPROC)wrapper;
1063 #endif
1065 return retproc;
1068 static int acounter = 0;
1069 void CodecAlloc(void)
1071 acounter++;
1072 //printf("**************CODEC ALLOC %d\n", acounter);
1075 void CodecRelease(void)
1077 acounter--;
1078 //printf("**************CODEC RELEASE %d\n", acounter);
1079 if (acounter == 0)
1081 for (;;)
1083 modref_list* list = local_wm;
1084 if (!local_wm)
1085 break;
1086 //printf("CODECRELEASE %p\n", list);
1087 MODULE_FreeLibrary(list->wm);
1088 MODULE_RemoveFromList(list->wm);
1089 if (local_wm == NULL)
1090 my_garbagecollection();