Fix vf_tcdump's compilation
[mplayer/kovensky.git] / loader / module.c
blob79c01270d3d236baf6ad95e97362fc0beb9012a8
1 /*
2 * Modules
4 * Copyright 1995 Alexandre Julliard
6 * Modified for use with MPlayer, detailed changelog at
7 * http://svn.mplayerhq.hu/mplayer/trunk/
9 */
11 // define for quicktime calls debugging and/or MacOS-level emulation:
12 #ifndef __APPLE__
13 #define EMU_QTX_API
14 #endif /* __APPLE__ */
16 // define for quicktime debugging (verbose logging):
17 //#define DEBUG_QTX_API
19 #include "config.h"
20 #include "debug.h"
22 #include <assert.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 #ifdef HAVE_SYS_MMAN_H
30 #include <sys/mman.h>
31 #endif
32 #include <inttypes.h>
34 #include "wine/windef.h"
35 #include "wine/winerror.h"
36 #include "wine/heap.h"
37 #include "wine/module.h"
38 #include "wine/pe_image.h"
39 #include "wine/debugtools.h"
41 #undef HAVE_LIBDL
43 #ifdef HAVE_LIBDL
44 #include <dlfcn.h>
45 #include "wine/elfdll.h"
46 #endif
47 #include "win32.h"
48 #include "drv.h"
50 #ifdef EMU_QTX_API
51 #include "wrapper.h"
52 static int report_func(void *stack_base, int stack_size, reg386_t *reg, uint32_t *flags);
53 static int report_func_ret(void *stack_base, int stack_size, reg386_t *reg, uint32_t *flags);
54 #endif
56 //#undef TRACE
57 //#define TRACE printf
59 //WINE_MODREF *local_wm=NULL;
60 modref_list* local_wm=NULL;
62 HANDLE SegptrHeap;
64 WINE_MODREF* MODULE_FindModule(LPCSTR m)
66 modref_list* list=local_wm;
67 TRACE("FindModule: Module %s request\n", m);
68 if(list==NULL)
69 return NULL;
70 // while(strcmp(m, list->wm->filename))
71 while(!strstr(list->wm->filename, m))
73 TRACE("%s: %x\n", list->wm->filename, list->wm->module);
74 list=list->prev;
75 if(list==NULL)
76 return NULL;
78 TRACE("Resolved to %s\n", list->wm->filename);
79 return list->wm;
82 static void MODULE_RemoveFromList(WINE_MODREF *mod)
84 modref_list* list=local_wm;
85 if(list==0)
86 return;
87 if(mod==0)
88 return;
89 if((list->prev==NULL)&&(list->next==NULL))
91 free(list);
92 local_wm=NULL;
93 // uninstall_fs();
94 return;
96 for(;list;list=list->prev)
98 if(list->wm==mod)
100 if(list->prev)
101 list->prev->next=list->next;
102 if(list->next)
103 list->next->prev=list->prev;
104 if(list==local_wm)
105 local_wm=list->prev;
106 free(list);
107 return;
113 WINE_MODREF *MODULE32_LookupHMODULE(HMODULE m)
115 modref_list* list=local_wm;
116 TRACE("LookupHMODULE: Module %X request\n", m);
117 if(list==NULL)
119 TRACE("LookupHMODULE failed\n");
120 return NULL;
122 while(m!=list->wm->module)
124 // printf("Checking list %X wm %X module %X\n",
125 // list, list->wm, list->wm->module);
126 list=list->prev;
127 if(list==NULL)
129 TRACE("LookupHMODULE failed\n");
130 return NULL;
133 TRACE("LookupHMODULE hit %p\n", list->wm);
134 return list->wm;
137 /*************************************************************************
138 * MODULE_InitDll
140 static WIN_BOOL MODULE_InitDll( WINE_MODREF *wm, DWORD type, LPVOID lpReserved )
142 WIN_BOOL retv = TRUE;
144 #ifdef DEBUG
145 static LPCSTR typeName[] = { "PROCESS_DETACH", "PROCESS_ATTACH",
146 "THREAD_ATTACH", "THREAD_DETACH" };
147 #endif
149 assert( wm );
152 /* Skip calls for modules loaded with special load flags */
154 if ( ( wm->flags & WINE_MODREF_DONT_RESOLVE_REFS )
155 || ( wm->flags & WINE_MODREF_LOAD_AS_DATAFILE ) )
156 return TRUE;
159 TRACE("(%s,%s,%p) - CALL\n", wm->modname, typeName[type], lpReserved );
161 /* Call the initialization routine */
162 switch ( wm->type )
164 case MODULE32_PE:
165 retv = PE_InitDLL( wm, type, lpReserved );
166 break;
168 case MODULE32_ELF:
169 /* no need to do that, dlopen() already does */
170 break;
172 default:
173 ERR("wine_modref type %d not handled.\n", wm->type );
174 retv = FALSE;
175 break;
178 /* The state of the module list may have changed due to the call
179 to PE_InitDLL. We cannot assume that this module has not been
180 deleted. */
181 TRACE("(%p,%s,%p) - RETURN %d\n", wm, typeName[type], lpReserved, retv );
183 return retv;
186 /*************************************************************************
187 * MODULE_DllProcessAttach
189 * Send the process attach notification to all DLLs the given module
190 * depends on (recursively). This is somewhat complicated due to the fact that
192 * - we have to respect the module dependencies, i.e. modules implicitly
193 * referenced by another module have to be initialized before the module
194 * itself can be initialized
196 * - the initialization routine of a DLL can itself call LoadLibrary,
197 * thereby introducing a whole new set of dependencies (even involving
198 * the 'old' modules) at any time during the whole process
200 * (Note that this routine can be recursively entered not only directly
201 * from itself, but also via LoadLibrary from one of the called initialization
202 * routines.)
204 * Furthermore, we need to rearrange the main WINE_MODREF list to allow
205 * the process *detach* notifications to be sent in the correct order.
206 * This must not only take into account module dependencies, but also
207 * 'hidden' dependencies created by modules calling LoadLibrary in their
208 * attach notification routine.
210 * The strategy is rather simple: we move a WINE_MODREF to the head of the
211 * list after the attach notification has returned. This implies that the
212 * detach notifications are called in the reverse of the sequence the attach
213 * notifications *returned*.
215 * NOTE: Assumes that the process critical section is held!
218 static WIN_BOOL MODULE_DllProcessAttach( WINE_MODREF *wm, LPVOID lpReserved )
220 WIN_BOOL retv = TRUE;
221 //int i;
222 assert( wm );
224 /* prevent infinite recursion in case of cyclical dependencies */
225 if ( ( wm->flags & WINE_MODREF_MARKER )
226 || ( wm->flags & WINE_MODREF_PROCESS_ATTACHED ) )
227 return retv;
229 TRACE("(%s,%p) - START\n", wm->modname, lpReserved );
231 /* Tag current MODREF to prevent recursive loop */
232 wm->flags |= WINE_MODREF_MARKER;
234 /* Recursively attach all DLLs this one depends on */
235 /* for ( i = 0; retv && i < wm->nDeps; i++ )
236 if ( wm->deps[i] )
237 retv = MODULE_DllProcessAttach( wm->deps[i], lpReserved );
239 /* Call DLL entry point */
241 //local_wm=wm;
242 if(local_wm)
244 local_wm->next = malloc(sizeof(modref_list));
245 local_wm->next->prev=local_wm;
246 local_wm->next->next=NULL;
247 local_wm->next->wm=wm;
248 local_wm=local_wm->next;
250 else
252 local_wm = malloc(sizeof(modref_list));
253 local_wm->next=local_wm->prev=NULL;
254 local_wm->wm=wm;
256 /* Remove recursion flag */
257 wm->flags &= ~WINE_MODREF_MARKER;
259 if ( retv )
261 retv = MODULE_InitDll( wm, DLL_PROCESS_ATTACH, lpReserved );
262 if ( retv )
263 wm->flags |= WINE_MODREF_PROCESS_ATTACHED;
267 TRACE("(%s,%p) - END\n", wm->modname, lpReserved );
269 return retv;
272 /*************************************************************************
273 * MODULE_DllProcessDetach
275 * Send DLL process detach notifications. See the comment about calling
276 * sequence at MODULE_DllProcessAttach. Unless the bForceDetach flag
277 * is set, only DLLs with zero refcount are notified.
279 static void MODULE_DllProcessDetach( WINE_MODREF* wm, WIN_BOOL bForceDetach, LPVOID lpReserved )
281 // WINE_MODREF *wm=local_wm;
282 //modref_list* l = local_wm;
283 wm->flags &= ~WINE_MODREF_PROCESS_ATTACHED;
284 MODULE_InitDll( wm, DLL_PROCESS_DETACH, lpReserved );
285 /* while (l)
287 modref_list* f = l;
288 l = l->next;
289 free(f);
291 local_wm = 0;*/
294 /***********************************************************************
295 * MODULE_LoadLibraryExA (internal)
297 * Load a PE style module according to the load order.
299 * The HFILE parameter is not used and marked reserved in the SDK. I can
300 * only guess that it should force a file to be mapped, but I rather
301 * ignore the parameter because it would be extremely difficult to
302 * integrate this with different types of module represenations.
305 static WINE_MODREF *MODULE_LoadLibraryExA( LPCSTR libname, HFILE hfile, DWORD flags )
307 DWORD err = GetLastError();
308 WINE_MODREF *pwm;
309 // module_loadorder_t *plo;
311 SetLastError( ERROR_FILE_NOT_FOUND );
312 TRACE("Trying native dll '%s'\n", libname);
313 pwm = PE_LoadLibraryExA(libname, flags);
314 #ifdef HAVE_LIBDL
315 if(!pwm)
317 TRACE("Trying ELF dll '%s'\n", libname);
318 pwm=(WINE_MODREF*)ELFDLL_LoadLibraryExA(libname, flags);
320 #endif
321 // printf("0x%08x\n", pwm);
322 // break;
323 if(pwm)
325 /* Initialize DLL just loaded */
326 TRACE("Loaded module '%s' at 0x%08x, \n", libname, pwm->module);
327 /* Set the refCount here so that an attach failure will */
328 /* decrement the dependencies through the MODULE_FreeLibrary call. */
329 pwm->refCount++;
331 SetLastError( err ); /* restore last error */
332 return pwm;
336 WARN("Failed to load module '%s'; error=0x%08lx, \n", libname, GetLastError());
337 return NULL;
340 /***********************************************************************
341 * MODULE_FreeLibrary
343 * NOTE: Assumes that the process critical section is held!
345 static WIN_BOOL MODULE_FreeLibrary( WINE_MODREF *wm )
347 TRACE("(%s) - START\n", wm->modname );
349 /* Recursively decrement reference counts */
350 //MODULE_DecRefCount( wm );
352 /* Call process detach notifications */
353 MODULE_DllProcessDetach( wm, FALSE, NULL );
355 PE_UnloadLibrary(wm);
357 TRACE("END\n");
359 return TRUE;
362 /***********************************************************************
363 * LoadLibraryExA (KERNEL32)
365 HMODULE WINAPI LoadLibraryExA(LPCSTR libname, HANDLE hfile, DWORD flags)
367 WINE_MODREF *wm = 0;
368 char* listpath[] = { "", "", "/usr/lib/win32", "/usr/local/lib/win32", 0 };
369 extern char* def_path;
370 char path[512];
371 char checked[2000];
372 int i = -1;
374 checked[0] = 0;
375 if(!libname)
377 SetLastError(ERROR_INVALID_PARAMETER);
378 return 0;
381 wm=MODULE_FindModule(libname);
382 if(wm) return wm->module;
384 // if(fs_installed==0)
385 // install_fs();
387 while (wm == 0 && listpath[++i])
389 if (i < 2)
391 if (i == 0)
392 /* check just original file name */
393 strncpy(path, libname, 511);
394 else
395 /* check default user path */
396 strncpy(path, def_path, 300);
398 else if (strcmp(def_path, listpath[i]))
399 /* path from the list */
400 strncpy(path, listpath[i], 300);
401 else
402 continue;
404 if (i > 0)
406 strcat(path, "/");
407 strncat(path, libname, 100);
409 path[511] = 0;
410 wm = MODULE_LoadLibraryExA( path, hfile, flags );
412 if (!wm)
414 if (checked[0])
415 strcat(checked, ", ");
416 strcat(checked, path);
417 checked[1500] = 0;
421 if ( wm )
423 if ( !MODULE_DllProcessAttach( wm, NULL ) )
425 WARN_(module)("Attach failed for module '%s', \n", libname);
426 MODULE_FreeLibrary(wm);
427 SetLastError(ERROR_DLL_INIT_FAILED);
428 MODULE_RemoveFromList(wm);
429 wm = NULL;
433 if (!wm && !strstr(checked, "avisynth.dll"))
434 printf("Win32 LoadLibrary failed to load: %s\n", checked);
436 #define RVA(x) ((char *)wm->module+(unsigned int)(x))
437 if (strstr(libname,"vp31vfw.dll") && wm)
439 int i;
441 // sse hack moved from patch dll into runtime patching
442 if (PE_FindExportedFunction(wm, "DriverProc", TRUE)==RVA(0x1000)) {
443 fprintf(stderr, "VP3 DLL found\n");
444 for (i=0;i<18;i++) RVA(0x4bd6)[i]=0x90;
448 // remove a few divs in the VP codecs that make trouble
449 if (strstr(libname,"vp5vfw.dll") && wm)
451 int i;
452 if (PE_FindExportedFunction(wm, "DriverProc", TRUE)==RVA(0x3930)) {
453 for (i=0;i<3;i++) RVA(0x4e86)[i]=0x90;
454 for (i=0;i<3;i++) RVA(0x5a23)[i]=0x90;
455 for (i=0;i<3;i++) RVA(0x5bff)[i]=0x90;
456 } else {
457 fprintf(stderr, "Unsupported VP5 version\n");
458 return 0;
462 if (strstr(libname,"vp6vfw.dll") && wm)
464 int i;
465 if (PE_FindExportedFunction(wm, "DriverProc", TRUE)==RVA(0x3ef0)) {
466 // looks like VP 6.1.0.2
467 for (i=0;i<6;i++) RVA(0x7268)[i]=0x90;
468 for (i=0;i<6;i++) RVA(0x7e83)[i]=0x90;
469 for (i=0;i<6;i++) RVA(0x806a)[i]=0x90;
470 } else if (PE_FindExportedFunction(wm, "DriverProc", TRUE)==RVA(0x4120)) {
471 // looks like VP 6.2.0.10
472 for (i=0;i<6;i++) RVA(0x7688)[i]=0x90;
473 for (i=0;i<6;i++) RVA(0x82c3)[i]=0x90;
474 for (i=0;i<6;i++) RVA(0x84aa)[i]=0x90;
475 for (i=0;i<6;i++) RVA(0x1d2cc)[i]=0x90;
476 for (i=0;i<6;i++) RVA(0x2179d)[i]=0x90;
477 for (i=0;i<6;i++) RVA(0x1977f)[i]=0x90;
478 } else if (PE_FindExportedFunction(wm, "DriverProc", TRUE)==RVA(0x3e70)) {
479 // looks like VP 6.0.7.3
480 for (i=0;i<6;i++) RVA(0x7559)[i]=0x90;
481 for (i=0;i<6;i++) RVA(0x81c3)[i]=0x90;
482 for (i=0;i<6;i++) RVA(0x839e)[i]=0x90;
483 } else {
484 fprintf(stderr, "Unsupported VP6 version\n");
485 return 0;
489 // Windows Media Video 9 Advanced
490 if (strstr(libname,"wmvadvd.dll") && wm)
492 // The codec calls IsRectEmpty with coords 0,0,0,0 => result is 0
493 // but it really wants the rectangle to be not empty
494 if (PE_FindExportedFunction(wm, "CreateInstance", TRUE)==RVA(0xb812)) {
495 // Dll version is 10.0.0.3645
496 *RVA(0x8b0f)=0xeb; // Jump always, ignoring IsRectEmpty result
497 } else {
498 fprintf(stderr, "Unsupported WMVA version\n");
499 return 0;
503 if (strstr(libname,"QuickTime.qts") && wm)
505 void** ptr;
506 void *dispatch_addr;
507 int i;
509 // dispatch_addr = GetProcAddress(wm->module, "theQuickTimeDispatcher", TRUE);
510 dispatch_addr = PE_FindExportedFunction(wm, "theQuickTimeDispatcher", TRUE);
511 if (dispatch_addr == RVA(0x124c30))
513 fprintf(stderr, "QuickTime5 DLLs found\n");
514 ptr = (void **)RVA(0x375ca4); // dispatch_ptr
515 for (i=0;i<5;i++) RVA(0x19e842)[i]=0x90; // make_new_region ?
516 for (i=0;i<28;i++) RVA(0x19e86d)[i]=0x90; // call__call_CreateCompatibleDC ?
517 for (i=0;i<5;i++) RVA(0x19e898)[i]=0x90; // jmp_to_call_loadbitmap ?
518 for (i=0;i<9;i++) RVA(0x19e8ac)[i]=0x90; // call__calls_OLE_shit ?
519 for (i=0;i<106;i++) RVA(0x261b10)[i]=0x90; // disable threads
520 #if 0
521 /* CreateThread callers */
522 for (i=0;i<5;i++) RVA(0x1487c5)[i]=0x90;
523 for (i=0;i<5;i++) RVA(0x14b275)[i]=0x90;
524 for (i=0;i<5;i++) RVA(0x1a24b1)[i]=0x90;
525 for (i=0;i<5;i++) RVA(0x1afc5a)[i]=0x90;
526 for (i=0;i<5;i++) RVA(0x2f799c)[i]=0x90;
527 for (i=0;i<5;i++) RVA(0x2f7efe)[i]=0x90;
528 for (i=0;i<5;i++) RVA(0x2fa33e)[i]=0x90;
529 #endif
531 #if 0
532 /* TerminateQTML fix */
533 for (i=0;i<47;i++) RVA(0x2fa3b8)[i]=0x90; // terminate thread
534 for (i=0;i<47;i++) RVA(0x2f7f78)[i]=0x90; // terminate thread
535 for (i=0;i<77;i++) RVA(0x1a13d5)[i]=0x90;
536 RVA(0x08e0ae)[0] = 0xc3; // font/dc remover
537 for (i=0;i<24;i++) RVA(0x07a1ad)[i]=0x90; // destroy window
538 #endif
539 } else if (dispatch_addr == RVA(0x13b330))
541 fprintf(stderr, "QuickTime6 DLLs found\n");
542 ptr = (void **)RVA(0x3b9524); // dispatcher_ptr
543 for (i=0;i<5;i++) RVA(0x2730cc)[i]=0x90; // make_new_region
544 for (i=0;i<28;i++) RVA(0x2730f7)[i]=0x90; // call__call_CreateCompatibleDC
545 for (i=0;i<5;i++) RVA(0x273122)[i]=0x90; // jmp_to_call_loadbitmap
546 for (i=0;i<9;i++) RVA(0x273131)[i]=0x90; // call__calls_OLE_shit
547 for (i=0;i<96;i++) RVA(0x2ac852)[i]=0x90; // disable threads
548 } else if (dispatch_addr == RVA(0x13c3e0))
550 fprintf(stderr, "QuickTime6.3 DLLs found\n");
551 ptr = (void **)RVA(0x3ca01c); // dispatcher_ptr
552 for (i=0;i<5;i++) RVA(0x268f6c)[i]=0x90; // make_new_region
553 for (i=0;i<28;i++) RVA(0x268f97)[i]=0x90; // call__call_CreateCompatibleDC
554 for (i=0;i<5;i++) RVA(0x268fc2)[i]=0x90; // jmp_to_call_loadbitmap
555 for (i=0;i<9;i++) RVA(0x268fd1)[i]=0x90; // call__calls_OLE_shit
556 for (i=0;i<96;i++) RVA(0x2b4722)[i]=0x90; // disable threads
557 } else
559 fprintf(stderr, "Unsupported QuickTime version (%p)\n",
560 dispatch_addr);
561 return 0;
564 fprintf(stderr,"QuickTime.qts patched!!! old entry=%p\n",ptr[0]);
566 #ifdef EMU_QTX_API
567 report_entry = report_func;
568 report_ret = report_func_ret;
569 wrapper_target=ptr[0];
570 ptr[0]=wrapper;
571 #endif
573 #undef RVA
575 return wm ? wm->module : 0;
579 /***********************************************************************
580 * LoadLibraryA (KERNEL32)
582 HMODULE WINAPI LoadLibraryA(LPCSTR libname) {
583 return LoadLibraryExA(libname,0,0);
586 /***********************************************************************
587 * FreeLibrary
589 WIN_BOOL WINAPI FreeLibrary(HINSTANCE hLibModule)
591 WIN_BOOL retv = FALSE;
592 WINE_MODREF *wm;
594 wm=MODULE32_LookupHMODULE(hLibModule);
596 if ( !wm || !hLibModule )
598 SetLastError( ERROR_INVALID_HANDLE );
599 return 0;
601 else
602 retv = MODULE_FreeLibrary( wm );
604 MODULE_RemoveFromList(wm);
606 /* garbage... */
607 if (local_wm == NULL) my_garbagecollection();
609 return retv;
612 /***********************************************************************
613 * MODULE_DecRefCount
615 * NOTE: Assumes that the process critical section is held!
617 static void MODULE_DecRefCount( WINE_MODREF *wm )
619 int i;
621 if ( wm->flags & WINE_MODREF_MARKER )
622 return;
624 if ( wm->refCount <= 0 )
625 return;
627 --wm->refCount;
628 TRACE("(%s) refCount: %d\n", wm->modname, wm->refCount );
630 if ( wm->refCount == 0 )
632 wm->flags |= WINE_MODREF_MARKER;
634 for ( i = 0; i < wm->nDeps; i++ )
635 if ( wm->deps[i] )
636 MODULE_DecRefCount( wm->deps[i] );
638 wm->flags &= ~WINE_MODREF_MARKER;
642 /***********************************************************************
643 * GetProcAddress (KERNEL32.257)
645 FARPROC WINAPI GetProcAddress( HMODULE hModule, LPCSTR function )
647 return MODULE_GetProcAddress( hModule, function, TRUE );
650 #ifdef DEBUG_QTX_API
653 http://lists.apple.com/archives/quicktime-api/2003/Jan/msg00278.html
656 struct ComponentParameters {
657 unsigned char flags; /* call modifiers: sync/async, deferred, immed, etc */
658 unsigned char paramSize; /* size in bytes of actual parameters passed to this call */
659 short what; /* routine selector, negative for Component management calls */
660 long params[1]; /* actual parameters for the indicated routine */
662 typedef struct ComponentParameters ComponentParameters;
664 static char* component_func(int what){
665 if (what < 0) // Range 0: Standard Component Calls
666 switch(what){
667 case -1: return "kComponentOpenSelect";
668 case -2: return "kComponentCloseSelect";
669 case -3: return "kComponentCanDoSelect";
670 case -4: return "kComponentVersionSelect";
671 case -5: return "kComponentRegisterSelect";
672 case -6: return "kComponentTargetSelect";
673 case -7: return "kComponentUnregisterSelect";
676 if (what >= 0 && what <= 0xff) // Range 1: Generic codecs
677 switch(what & 0xff){
678 case 0: return "kImageCodecGetCodecInfoSelect";
679 case 1: return "kImageCodecGetCompressionTimeSelect";
680 case 2: return "kImageCodecGetMaxCompressionSizeSelect";
681 case 3: return "kImageCodecPreCompressSelect";
682 case 4: return "kImageCodecBandCompressSelect";
683 case 5: return "kImageCodecPreDecompressSelect";
684 case 6: return "kImageCodecBandDecompressSelect";
685 case 7: return "kImageCodecBusySelect";
686 // finish this list from the above URL
687 case 0x10: return "kImageCodecIsImageDescriptionEquivalentSelect";
688 case 0x12: return "kImageCodecDisposeMemorySelect";
689 case 0x14: return "kImageCodecNewImageBufferMemorySelect";
690 case 0x28: return "kImageCodecRequestGammaLevelSelect";
693 //if (what >= 0x100 && what <= 0x1ff) // Range 2: Specific to QT Photo JPEG codecs
695 if (what >= 0x200 && what <= 0x2ff) // Range 3: Base Decompressor
696 switch(what & 0xff){
697 case 0: return "Preflight";
698 case 1: return "Initialize";
699 case 2: return "BeginBand";
700 case 3: return "DrawBand";
701 case 4: return "EndBand";
702 case 5: return "QueueStarting";
703 case 6: return "QueueStopping";
706 return "???";
709 static int c_level=0;
711 static int dump_component(char* name, int type, void* orig, ComponentParameters *params,void** glob){
712 int ( *orig)(ComponentParameters *params, void** glob) = orig;
713 int ret,i;
715 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));
717 for(i=0;i<params->paramSize/4;i++)
718 fprintf(stderr,"%*s param[%d] = 0x%X\n",3*c_level,"",i,params->params[i]);
720 ++c_level;
721 ret=orig(params,glob);
722 --c_level;
724 if(ret>=0x1000)
725 fprintf(stderr,"%*s return=0x%X\n",3*c_level,"",ret);
726 else
727 fprintf(stderr,"%*s return=%d\n",3*c_level,"",ret);
728 return ret;
731 #define DECL_COMPONENT(sname,name,type) \
732 static void* real_ ## sname = NULL; \
733 static int fake_ ## sname(ComponentParameters *params,void** glob){ \
734 return dump_component(name,type,real_ ## sname, params, glob); \
737 #include "qt_comp.h"
739 #undef DECL_COMPONENT
741 #include "qt_fv.h"
743 #endif
745 #ifdef EMU_QTX_API
747 #ifdef __OS2__
748 uint32_t _System DosQueryMem(void *, uint32_t *, uint32_t *);
749 #endif
751 static int is_invalid_ptr_handle(void *p)
753 #ifdef __OS2__
754 uint32_t cb = 1;
755 uint32_t fl;
757 if(DosQueryMem(p, &cb, &fl))
758 return 1;
760 // Occasionally, ptr with 'EXEC' attr is passed.
761 // On OS/2, however, malloc() never sets 'EXEC' attr.
762 // So ptr with 'EXEC' attr is invalid.
763 if(fl & 0x04)
764 return 1;
766 return 0;
767 #else
768 return (uint32_t)p >= 0x60000000;
769 #endif
772 static uint32_t ret_array[4096];
773 static int ret_i=0;
775 static int report_func(void *stack_base, int stack_size, reg386_t *reg, uint32_t *flags)
777 #ifdef DEBUG_QTX_API
778 int i;
779 int* dptr;
780 void* pwrapper=NULL;
781 void* pptr=NULL;
782 char* pname=NULL;
783 int plen=-1;
784 // find the code:
786 dptr=0x62b67ae0;dptr+=2*((reg->eax>>16)&255);
787 // printf("FUNC: flag=%d ptr=%p\n",dptr[0],dptr[1]);
788 if(dptr[0]&255){
789 dptr=dptr[1];dptr+=4*(reg->eax&65535);
790 // printf("FUNC: ptr2=%p eax=%p edx=%p\n",dptr[1],dptr[0],dptr[2]);
791 pwrapper=dptr[1]; pptr=dptr[0]; plen=dptr[2];
792 } else {
793 pwrapper=0x62924910;
794 switch(dptr[1]){
795 case 0x629248d0:
796 dptr=0x62b672c0;dptr+=2*(reg->eax&65535);
797 // printf("FUNC: ptr2=%p eax=%p edx=%p\n",0x62924910,dptr[0],dptr[1]);
798 pptr=dptr[0]; plen=dptr[1];
799 break;
800 case 0x62924e40:
801 dptr=0x62b67c70;dptr+=2*(reg->eax&65535);
802 // printf("FUNC: ptr2=%p eax=%p edx=%p\n",0x62924910,dptr[0],dptr[1]);
803 pptr=dptr[0]; plen=dptr[1];
804 break;
805 case 0x62924e60:
806 dptr=0x62b68108;if(reg->eax&0x8000) dptr+=2*(reg->eax|0xffff0000); else dptr+=2*(reg->eax&65535);
807 // printf("FUNC: ptr2=%p eax=%p edx=%p\n",0x62924910,dptr[0],dptr[1]);
808 pptr=dptr[0]; plen=dptr[1];
809 break;
810 case 0x62924e80:
811 dptr=0x62b68108;if(reg->eax&0x8000) dptr+=2*(reg->eax|0xffff0000); else dptr+=2*(reg->eax&65535);
812 // printf("FUNC: ptr2=%p eax=%p edx=%p\n",0x62924910,dptr[0],dptr[1]);
813 pptr=dptr[0]; plen=dptr[1];
814 break;
815 default:
816 printf("FUNC: unknown ptr & psize!\n");
817 pwrapper=dptr[1];
821 for(i=0;qt_fv_list[i].name;i++){
822 if(qt_fv_list[i].id==reg->eax){
823 pname=qt_fv_list[i].name;
824 break;
828 printf("FUNC[%X/%s]: wrapper=%p func=%p len=%d\n",reg->eax,
829 pname?pname:"???",pwrapper,pptr,plen);
831 printf("FUNC: caller=%p ebx=%p\n",((uint32_t *)stack_base)[0],reg->ebx);
833 if(pname)
834 printf("%*sENTER(%d): %s(",ret_i*2,"",ret_i,pname);
835 else
836 printf("%*sENTER(%d): %X(",ret_i*2,"",ret_i,reg->eax);
837 for (i=0;i<plen/4;i++){
838 unsigned int val=((uint32_t *)stack_base)[1+i];
839 unsigned char* fcc=&val;
840 printf("%s0x%X", i?", ":"",val);
841 if(fcc[0]>=0x20 && fcc[0]<128 &&
842 fcc[1]>=0x20 && fcc[1]<128 &&
843 fcc[2]>=0x20 && fcc[2]<128 &&
844 fcc[3]>=0x20 && fcc[3]<128) printf("='%c%c%c%c'",fcc[3],fcc[2],fcc[1],fcc[0]);
845 else if(val>=8 && val<65536) printf("=%d",val);
847 printf(")\n");
848 fflush(stdout);
850 #endif
852 // emulate some functions:
853 switch(reg->eax){
854 // memory management:
855 case 0x150011: //NewPtrClear
856 case 0x150012: //NewPtrSysClear
857 reg->eax = malloc(((uint32_t *)stack_base)[1]);
858 memset((void *)reg->eax,0,((uint32_t *)stack_base)[1]);
859 #ifdef DEBUG_QTX_API
860 printf("%*sLEAVE(%d): EMULATED! 0x%X\n",ret_i*2,"",ret_i, reg->eax);
861 #endif
862 return 1;
863 case 0x15000F: //NewPtr
864 case 0x150010: //NewPtrSys
865 reg->eax = malloc(((uint32_t *)stack_base)[1]);
866 #ifdef DEBUG_QTX_API
867 printf("%*sLEAVE(%d): EMULATED! 0x%X\n",ret_i*2,"",ret_i, reg->eax);
868 #endif
869 return 1;
870 case 0x15002f: //DisposePtr
871 if(is_invalid_ptr_handle(((void **)stack_base)[1]))
872 printf("WARNING! Invalid Ptr handle!\n");
873 else
874 free(((void **)stack_base)[1]);
875 reg->eax=0;
876 #ifdef DEBUG_QTX_API
877 printf("%*sLEAVE(%d): EMULATED! 0x%X\n",ret_i*2,"",ret_i, reg->eax);
878 #endif
879 return 1;
880 // mutexes:
881 case 0x1d0033: //QTMLCreateMutex
882 reg->eax=0xdeadbabe;
883 #ifdef DEBUG_QTX_API
884 printf("%*sLEAVE(%d): EMULATED! 0x%X\n",ret_i*2,"",ret_i, reg->eax);
885 #endif
886 return 1;
887 case 0x1d0034: //QTMLDestroyMutex
888 case 0x1d0035: //QTMLGrabMutex
889 case 0x1d0036: //QTMLReturnMutex
890 case 0x1d003d: //QTMLTryGrabMutex
891 reg->eax=0;
892 #ifdef DEBUG_QTX_API
893 printf("%*sLEAVE(%d): EMULATED! 0x%X\n",ret_i*2,"",ret_i, reg->eax);
894 #endif
895 return 1;
898 #if 0
899 switch(reg->eax){
900 // case 0x00010000:
901 // printf("FUNC: ImageCodecInitialize/ImageCodecGetCodecInfo(ci=%p,&icap=%p)\n",((uint32_t *)stack_base)[1],((uint32_t *)stack_base)[4]);
902 // break;
903 case 0x00010003:
904 printf("FUNC: CountComponents(&desc=%p)\n",((uint32_t *)stack_base)[1]);
905 break;
906 case 0x00010004:
907 printf("FUNC: FindNextComponent(prev=%p,&desc=%p)\n",((uint32_t *)stack_base)[1],((uint32_t *)stack_base)[2]);
908 break;
909 case 0x00010007:
910 printf("FUNC: OpenComponent(prev=%p)\n",((uint32_t *)stack_base)[1]);
911 break;
912 case 0x0003008b:
913 printf("FUNC: QTNewGWorldFromPtr(&pts=%p,fourcc=%.4s,&rect=%p,x1=%p,x2=%p,x3=%p,plane=%p,stride=%d)\n",
914 ((uint32_t *)stack_base)[1],
915 &(((uint32_t *)stack_base)[2]),
916 ((uint32_t *)stack_base)[3],
917 ((uint32_t *)stack_base)[4],
918 ((uint32_t *)stack_base)[5],
919 ((uint32_t *)stack_base)[6],
920 ((uint32_t *)stack_base)[7],
921 ((uint32_t *)stack_base)[8]);
922 break;
923 case 0x001c0018:
924 printf("FUNC: GetGWorldPixMap(gworld=%p)\n",((uint32_t *)stack_base)[1]);
925 break;
926 case 0x00110001:
927 printf("FUNC: Gestalt(fourcc=%.4s, &ret=%p)\n",&(((uint32_t *)stack_base)[1]),((uint32_t *)stack_base)[2]);
928 break;
929 default: {
930 int i;
931 for(i=0;qt_fv_list[i].name;i++){
932 if(qt_fv_list[i].id==reg->eax){
933 printf("FUNC: %s\n",qt_fv_list[i].name);
934 break;
940 // print stack/reg information
941 printf("ENTER(%d) stack = %d bytes @ %p\n"
942 "eax = 0x%08x edx = 0x%08x ebx = 0x%08x ecx = 0x%08x\n"
943 "esp = 0x%08x ebp = 0x%08x esi = 0x%08x edi = 0x%08x\n"
944 "flags = 0x%08x\n", ret_i,
945 stack_size, stack_base,
946 reg->eax, reg->edx, reg->ebx, reg->ecx,
947 reg->esp, reg->ebp, reg->esi, reg->edi,
948 *flags);
949 #endif
951 // save ret addr:
952 ret_array[ret_i]=((uint32_t *)stack_base)[0];
953 ++ret_i;
955 #if 0
956 // print first 7 longs in the stack (return address, arg[1], arg[2] ... )
957 printf("stack[] = { ");
958 for (i=0;i<7;i++) {
959 printf("%08x ", ((uint32_t *)stack_base)[i]);
961 printf("}\n\n");
962 #endif
964 // // mess with function parameters
965 // ((uint32_t *)stack_base)[1] = 0x66554433;
967 // // mess with return address...
968 // reg->eax = 0x11223344;
969 return 0;
972 static int report_func_ret(void *stack_base, int stack_size, reg386_t *reg, uint32_t *flags)
974 //int i;
975 #ifdef DEBUG_QTX_API
976 short err;
977 #endif
979 // restore ret addr:
980 --ret_i;
981 ((uint32_t *)stack_base)[0]=ret_array[ret_i];
983 #ifdef DEBUG_QTX_API
985 #if 1
986 printf("%*sLEAVE(%d): 0x%X",ret_i*2,"",ret_i, reg->eax);
987 err=reg->eax;
988 if(err && (reg->eax>>16)==0) printf(" = %d",err);
989 printf("\n");
990 fflush(stdout);
991 #else
992 // print stack/reg information
993 printf("LEAVE(%d) stack = %d bytes @ %p\n"
994 "eax = 0x%08x edx = 0x%08x ebx = 0x%08x ecx = 0x%08x\n"
995 "esp = 0x%08x ebp = 0x%08x esi = 0x%08x edi = 0x%08x\n"
996 "flags = 0x%08x\n", ret_i,
997 stack_size, stack_base,
998 reg->eax, reg->edx, reg->ebx, reg->ecx,
999 reg->esp, reg->ebp, reg->esi, reg->edi,
1000 *flags);
1001 #endif
1003 #if 0
1004 // print first 7 longs in the stack (return address, arg[1], arg[2] ... )
1005 printf("stack[] = { ");
1006 for (i=0;i<7;i++) {
1007 printf("%08x ", ((uint32_t *)stack_base)[i]);
1009 printf("}\n\n");
1010 #endif
1012 #endif
1014 // // mess with function parameters
1015 // ((uint32_t *)stack_base)[1] = 0x66554433;
1017 // // mess with return address...
1018 // reg->eax = 0x11223344;
1019 return 0;
1022 #endif
1024 /***********************************************************************
1025 * MODULE_GetProcAddress (internal)
1027 FARPROC MODULE_GetProcAddress(
1028 HMODULE hModule, /* [in] current module handle */
1029 LPCSTR function, /* [in] function to be looked up */
1030 WIN_BOOL snoop )
1032 WINE_MODREF *wm = MODULE32_LookupHMODULE( hModule );
1033 // WINE_MODREF *wm=local_wm;
1034 FARPROC retproc;
1036 #ifdef DEBUG_QTX_API
1037 if (HIWORD(function))
1038 fprintf(stderr,"XXX GetProcAddress(%08lx,%s)\n",(DWORD)hModule,function);
1039 else
1040 fprintf(stderr,"XXX GetProcAddress(%08lx,%p)\n",(DWORD)hModule,function);
1041 #endif
1043 // TRACE_(win32)("(%08lx,%s)\n",(DWORD)hModule,function);
1044 // else
1045 // TRACE_(win32)("(%08lx,%p)\n",(DWORD)hModule,function);
1047 if (!wm) {
1048 SetLastError(ERROR_INVALID_HANDLE);
1049 return (FARPROC)0;
1051 switch (wm->type)
1053 case MODULE32_PE:
1054 retproc = PE_FindExportedFunction( wm, function, snoop );
1055 if (!retproc) SetLastError(ERROR_PROC_NOT_FOUND);
1056 break;
1057 #ifdef HAVE_LIBDL
1058 case MODULE32_ELF:
1059 retproc = (FARPROC) dlsym( (void*) wm->module, function);
1060 if (!retproc) SetLastError(ERROR_PROC_NOT_FOUND);
1061 return retproc;
1062 #endif
1063 default:
1064 ERR("wine_modref type %d not handled.\n",wm->type);
1065 SetLastError(ERROR_INVALID_HANDLE);
1066 return (FARPROC)0;
1069 #ifdef EMU_QTX_API
1070 if (HIWORD(function) && retproc){
1072 #ifdef DEBUG_QTX_API
1073 #define DECL_COMPONENT(sname,name,type) \
1074 if(!strcmp(function,name)){ \
1075 fprintf(stderr,name "dispatcher catched -> %p\n",retproc); \
1076 real_ ## sname = retproc; retproc = fake_ ## sname; \
1078 #include "qt_comp.h"
1079 #undef DECL_COMPONENT
1080 #endif
1082 if(!strcmp(function,"theQuickTimeDispatcher")
1083 // || !strcmp(function,"CallComponentFunctionWithStorage")
1084 // || !strcmp(function,"CallComponent")
1086 fprintf(stderr,"theQuickTimeDispatcher catched -> %p\n",retproc);
1087 report_entry = report_func;
1088 report_ret = report_func_ret;
1089 wrapper_target=(void(*)(void))retproc;
1090 retproc=(FARPROC)wrapper;
1094 #endif
1096 return retproc;
1099 static int acounter = 0;
1100 void CodecAlloc(void)
1102 acounter++;
1103 //printf("**************CODEC ALLOC %d\n", acounter);
1106 void CodecRelease(void)
1108 acounter--;
1109 //printf("**************CODEC RELEASE %d\n", acounter);
1110 if (acounter == 0)
1112 for (;;)
1114 modref_list* list = local_wm;
1115 if (!local_wm)
1116 break;
1117 //printf("CODECRELEASE %p\n", list);
1118 MODULE_FreeLibrary(list->wm);
1119 MODULE_RemoveFromList(list->wm);
1120 if (local_wm == NULL)
1121 my_garbagecollection();