vo_tga: Use the proper imgfmt names
[mplayer/glamo.git] / loader / module.c
blob6f3f0c3de96e667ec2a3ee68a3268e3697db33d6
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"
49 #include "path.h"
51 #ifdef EMU_QTX_API
52 #include "wrapper.h"
53 static int report_func(void *stack_base, int stack_size, reg386_t *reg, uint32_t *flags);
54 static int report_func_ret(void *stack_base, int stack_size, reg386_t *reg, uint32_t *flags);
55 #endif
57 //#undef TRACE
58 //#define TRACE printf
60 //WINE_MODREF *local_wm=NULL;
61 modref_list* local_wm=NULL;
63 HANDLE SegptrHeap;
65 WINE_MODREF* MODULE_FindModule(LPCSTR m)
67 modref_list* list=local_wm;
68 TRACE("FindModule: Module %s request\n", m);
69 if(list==NULL)
70 return NULL;
71 // while(strcmp(m, list->wm->filename))
72 while(!strstr(list->wm->filename, m))
74 TRACE("%s: %x\n", list->wm->filename, list->wm->module);
75 list=list->prev;
76 if(list==NULL)
77 return NULL;
79 TRACE("Resolved to %s\n", list->wm->filename);
80 return list->wm;
83 static void MODULE_RemoveFromList(WINE_MODREF *mod)
85 modref_list* list=local_wm;
86 if(list==0)
87 return;
88 if(mod==0)
89 return;
90 if((list->prev==NULL)&&(list->next==NULL))
92 free(list);
93 local_wm=NULL;
94 // uninstall_fs();
95 return;
97 for(;list;list=list->prev)
99 if(list->wm==mod)
101 if(list->prev)
102 list->prev->next=list->next;
103 if(list->next)
104 list->next->prev=list->prev;
105 if(list==local_wm)
106 local_wm=list->prev;
107 free(list);
108 return;
114 WINE_MODREF *MODULE32_LookupHMODULE(HMODULE m)
116 modref_list* list=local_wm;
117 TRACE("LookupHMODULE: Module %X request\n", m);
118 if(list==NULL)
120 TRACE("LookupHMODULE failed\n");
121 return NULL;
123 while(m!=list->wm->module)
125 // printf("Checking list %X wm %X module %X\n",
126 // list, list->wm, list->wm->module);
127 list=list->prev;
128 if(list==NULL)
130 TRACE("LookupHMODULE failed\n");
131 return NULL;
134 TRACE("LookupHMODULE hit %p\n", list->wm);
135 return list->wm;
138 /*************************************************************************
139 * MODULE_InitDll
141 static WIN_BOOL MODULE_InitDll( WINE_MODREF *wm, DWORD type, LPVOID lpReserved )
143 WIN_BOOL retv = TRUE;
145 #ifdef DEBUG
146 static LPCSTR typeName[] = { "PROCESS_DETACH", "PROCESS_ATTACH",
147 "THREAD_ATTACH", "THREAD_DETACH" };
148 #endif
150 assert( wm );
153 /* Skip calls for modules loaded with special load flags */
155 if ( ( wm->flags & WINE_MODREF_DONT_RESOLVE_REFS )
156 || ( wm->flags & WINE_MODREF_LOAD_AS_DATAFILE ) )
157 return TRUE;
160 TRACE("(%s,%s,%p) - CALL\n", wm->modname, typeName[type], lpReserved );
162 /* Call the initialization routine */
163 switch ( wm->type )
165 case MODULE32_PE:
166 retv = PE_InitDLL( wm, type, lpReserved );
167 break;
169 case MODULE32_ELF:
170 /* no need to do that, dlopen() already does */
171 break;
173 default:
174 ERR("wine_modref type %d not handled.\n", wm->type );
175 retv = FALSE;
176 break;
179 /* The state of the module list may have changed due to the call
180 to PE_InitDLL. We cannot assume that this module has not been
181 deleted. */
182 TRACE("(%p,%s,%p) - RETURN %d\n", wm, typeName[type], lpReserved, retv );
184 return retv;
187 /*************************************************************************
188 * MODULE_DllProcessAttach
190 * Send the process attach notification to all DLLs the given module
191 * depends on (recursively). This is somewhat complicated due to the fact that
193 * - we have to respect the module dependencies, i.e. modules implicitly
194 * referenced by another module have to be initialized before the module
195 * itself can be initialized
197 * - the initialization routine of a DLL can itself call LoadLibrary,
198 * thereby introducing a whole new set of dependencies (even involving
199 * the 'old' modules) at any time during the whole process
201 * (Note that this routine can be recursively entered not only directly
202 * from itself, but also via LoadLibrary from one of the called initialization
203 * routines.)
205 * Furthermore, we need to rearrange the main WINE_MODREF list to allow
206 * the process *detach* notifications to be sent in the correct order.
207 * This must not only take into account module dependencies, but also
208 * 'hidden' dependencies created by modules calling LoadLibrary in their
209 * attach notification routine.
211 * The strategy is rather simple: we move a WINE_MODREF to the head of the
212 * list after the attach notification has returned. This implies that the
213 * detach notifications are called in the reverse of the sequence the attach
214 * notifications *returned*.
216 * NOTE: Assumes that the process critical section is held!
219 static WIN_BOOL MODULE_DllProcessAttach( WINE_MODREF *wm, LPVOID lpReserved )
221 WIN_BOOL retv = TRUE;
222 //int i;
223 assert( wm );
225 /* prevent infinite recursion in case of cyclical dependencies */
226 if ( ( wm->flags & WINE_MODREF_MARKER )
227 || ( wm->flags & WINE_MODREF_PROCESS_ATTACHED ) )
228 return retv;
230 TRACE("(%s,%p) - START\n", wm->modname, lpReserved );
232 /* Tag current MODREF to prevent recursive loop */
233 wm->flags |= WINE_MODREF_MARKER;
235 /* Recursively attach all DLLs this one depends on */
236 /* for ( i = 0; retv && i < wm->nDeps; i++ )
237 if ( wm->deps[i] )
238 retv = MODULE_DllProcessAttach( wm->deps[i], lpReserved );
240 /* Call DLL entry point */
242 //local_wm=wm;
243 if(local_wm)
245 local_wm->next = malloc(sizeof(modref_list));
246 local_wm->next->prev=local_wm;
247 local_wm->next->next=NULL;
248 local_wm->next->wm=wm;
249 local_wm=local_wm->next;
251 else
253 local_wm = malloc(sizeof(modref_list));
254 local_wm->next=local_wm->prev=NULL;
255 local_wm->wm=wm;
257 /* Remove recursion flag */
258 wm->flags &= ~WINE_MODREF_MARKER;
260 if ( retv )
262 retv = MODULE_InitDll( wm, DLL_PROCESS_ATTACH, lpReserved );
263 if ( retv )
264 wm->flags |= WINE_MODREF_PROCESS_ATTACHED;
268 TRACE("(%s,%p) - END\n", wm->modname, lpReserved );
270 return retv;
273 /*************************************************************************
274 * MODULE_DllProcessDetach
276 * Send DLL process detach notifications. See the comment about calling
277 * sequence at MODULE_DllProcessAttach. Unless the bForceDetach flag
278 * is set, only DLLs with zero refcount are notified.
280 static void MODULE_DllProcessDetach( WINE_MODREF* wm, WIN_BOOL bForceDetach, LPVOID lpReserved )
282 // WINE_MODREF *wm=local_wm;
283 //modref_list* l = local_wm;
284 wm->flags &= ~WINE_MODREF_PROCESS_ATTACHED;
285 MODULE_InitDll( wm, DLL_PROCESS_DETACH, lpReserved );
286 /* while (l)
288 modref_list* f = l;
289 l = l->next;
290 free(f);
292 local_wm = 0;*/
295 /***********************************************************************
296 * MODULE_LoadLibraryExA (internal)
298 * Load a PE style module according to the load order.
300 * The HFILE parameter is not used and marked reserved in the SDK. I can
301 * only guess that it should force a file to be mapped, but I rather
302 * ignore the parameter because it would be extremely difficult to
303 * integrate this with different types of module represenations.
306 static WINE_MODREF *MODULE_LoadLibraryExA( LPCSTR libname, HFILE hfile, DWORD flags )
308 DWORD err = GetLastError();
309 WINE_MODREF *pwm;
310 // module_loadorder_t *plo;
312 SetLastError( ERROR_FILE_NOT_FOUND );
313 TRACE("Trying native dll '%s'\n", libname);
314 pwm = PE_LoadLibraryExA(libname, flags);
315 #ifdef HAVE_LIBDL
316 if(!pwm)
318 TRACE("Trying ELF dll '%s'\n", libname);
319 pwm=(WINE_MODREF*)ELFDLL_LoadLibraryExA(libname, flags);
321 #endif
322 // printf("0x%08x\n", pwm);
323 // break;
324 if(pwm)
326 /* Initialize DLL just loaded */
327 TRACE("Loaded module '%s' at 0x%08x, \n", libname, pwm->module);
328 /* Set the refCount here so that an attach failure will */
329 /* decrement the dependencies through the MODULE_FreeLibrary call. */
330 pwm->refCount++;
332 SetLastError( err ); /* restore last error */
333 return pwm;
337 WARN("Failed to load module '%s'; error=0x%08lx, \n", libname, GetLastError());
338 return NULL;
341 /***********************************************************************
342 * MODULE_FreeLibrary
344 * NOTE: Assumes that the process critical section is held!
346 static WIN_BOOL MODULE_FreeLibrary( WINE_MODREF *wm )
348 TRACE("(%s) - START\n", wm->modname );
350 /* Call process detach notifications */
351 MODULE_DllProcessDetach( wm, FALSE, NULL );
353 PE_UnloadLibrary(wm);
355 TRACE("END\n");
357 return TRUE;
360 /***********************************************************************
361 * LoadLibraryExA (KERNEL32)
363 HMODULE WINAPI LoadLibraryExA(LPCSTR libname, HANDLE hfile, DWORD flags)
365 WINE_MODREF *wm = 0;
366 char* listpath[] = { "", "", 0 };
367 char path[512];
368 char checked[2000];
369 int i = -1;
371 checked[0] = 0;
372 if(!libname)
374 SetLastError(ERROR_INVALID_PARAMETER);
375 return 0;
378 wm=MODULE_FindModule(libname);
379 if(wm) return wm->module;
381 // if(fs_installed==0)
382 // install_fs();
384 while (wm == 0 && listpath[++i])
386 if (i < 2)
388 if (i == 0)
389 /* check just original file name */
390 strncpy(path, libname, 511);
391 else
392 /* check default user path */
393 strncpy(path, codec_path, 300);
395 else if (strcmp(codec_path, listpath[i]))
396 /* path from the list */
397 strncpy(path, listpath[i], 300);
398 else
399 continue;
401 if (i > 0)
403 strcat(path, "/");
404 strncat(path, libname, 100);
406 path[511] = 0;
407 wm = MODULE_LoadLibraryExA( path, hfile, flags );
409 if (!wm)
411 if (checked[0])
412 strcat(checked, ", ");
413 strcat(checked, path);
414 checked[1500] = 0;
418 if ( wm )
420 if ( !MODULE_DllProcessAttach( wm, NULL ) )
422 WARN_(module)("Attach failed for module '%s', \n", libname);
423 MODULE_FreeLibrary(wm);
424 SetLastError(ERROR_DLL_INIT_FAILED);
425 MODULE_RemoveFromList(wm);
426 wm = NULL;
430 if (!wm && !strstr(checked, "avisynth.dll"))
431 printf("Win32 LoadLibrary failed to load: %s\n", checked);
433 #define RVA(x) ((char *)wm->module+(unsigned int)(x))
434 if (strstr(libname,"vp31vfw.dll") && wm)
436 int i;
438 // sse hack moved from patch dll into runtime patching
439 if (PE_FindExportedFunction(wm, "DriverProc", TRUE)==RVA(0x1000)) {
440 fprintf(stderr, "VP3 DLL found\n");
441 for (i=0;i<18;i++) RVA(0x4bd6)[i]=0x90;
445 // remove a few divs in the VP codecs that make trouble
446 if (strstr(libname,"vp5vfw.dll") && wm)
448 int i;
449 if (PE_FindExportedFunction(wm, "DriverProc", TRUE)==RVA(0x3930)) {
450 for (i=0;i<3;i++) RVA(0x4e86)[i]=0x90;
451 for (i=0;i<3;i++) RVA(0x5a23)[i]=0x90;
452 for (i=0;i<3;i++) RVA(0x5bff)[i]=0x90;
453 } else {
454 fprintf(stderr, "Unsupported VP5 version\n");
455 return 0;
459 if (strstr(libname,"vp6vfw.dll") && wm)
461 int i;
462 if (PE_FindExportedFunction(wm, "DriverProc", TRUE)==RVA(0x3ef0)) {
463 // looks like VP 6.1.0.2
464 for (i=0;i<6;i++) RVA(0x7268)[i]=0x90;
465 for (i=0;i<6;i++) RVA(0x7e83)[i]=0x90;
466 for (i=0;i<6;i++) RVA(0x806a)[i]=0x90;
467 } else if (PE_FindExportedFunction(wm, "DriverProc", TRUE)==RVA(0x4120)) {
468 // looks like VP 6.2.0.10
469 for (i=0;i<6;i++) RVA(0x7688)[i]=0x90;
470 for (i=0;i<6;i++) RVA(0x82c3)[i]=0x90;
471 for (i=0;i<6;i++) RVA(0x84aa)[i]=0x90;
472 for (i=0;i<6;i++) RVA(0x1d2cc)[i]=0x90;
473 for (i=0;i<6;i++) RVA(0x2179d)[i]=0x90;
474 for (i=0;i<6;i++) RVA(0x1977f)[i]=0x90;
475 } else if (PE_FindExportedFunction(wm, "DriverProc", TRUE)==RVA(0x3e70)) {
476 // looks like VP 6.0.7.3
477 for (i=0;i<6;i++) RVA(0x7559)[i]=0x90;
478 for (i=0;i<6;i++) RVA(0x81c3)[i]=0x90;
479 for (i=0;i<6;i++) RVA(0x839e)[i]=0x90;
480 } else {
481 fprintf(stderr, "Unsupported VP6 version\n");
482 return 0;
486 // Windows Media Video 9 Advanced
487 if (strstr(libname,"wmvadvd.dll") && wm)
489 // The codec calls IsRectEmpty with coords 0,0,0,0 => result is 0
490 // but it really wants the rectangle to be not empty
491 if (PE_FindExportedFunction(wm, "CreateInstance", TRUE)==RVA(0xb812)) {
492 // Dll version is 10.0.0.3645
493 *RVA(0x8b0f)=0xeb; // Jump always, ignoring IsRectEmpty result
494 } else {
495 fprintf(stderr, "Unsupported WMVA version\n");
496 return 0;
500 if (strstr(libname,"QuickTime.qts") && wm)
502 void** ptr;
503 void *dispatch_addr;
504 int i;
506 // dispatch_addr = GetProcAddress(wm->module, "theQuickTimeDispatcher", TRUE);
507 dispatch_addr = PE_FindExportedFunction(wm, "theQuickTimeDispatcher", TRUE);
508 if (dispatch_addr == RVA(0x124c30))
510 fprintf(stderr, "QuickTime5 DLLs found\n");
511 ptr = (void **)RVA(0x375ca4); // dispatch_ptr
512 for (i=0;i<5;i++) RVA(0x19e842)[i]=0x90; // make_new_region ?
513 for (i=0;i<28;i++) RVA(0x19e86d)[i]=0x90; // call__call_CreateCompatibleDC ?
514 for (i=0;i<5;i++) RVA(0x19e898)[i]=0x90; // jmp_to_call_loadbitmap ?
515 for (i=0;i<9;i++) RVA(0x19e8ac)[i]=0x90; // call__calls_OLE_shit ?
516 for (i=0;i<106;i++) RVA(0x261b10)[i]=0x90; // disable threads
517 #if 0
518 /* CreateThread callers */
519 for (i=0;i<5;i++) RVA(0x1487c5)[i]=0x90;
520 for (i=0;i<5;i++) RVA(0x14b275)[i]=0x90;
521 for (i=0;i<5;i++) RVA(0x1a24b1)[i]=0x90;
522 for (i=0;i<5;i++) RVA(0x1afc5a)[i]=0x90;
523 for (i=0;i<5;i++) RVA(0x2f799c)[i]=0x90;
524 for (i=0;i<5;i++) RVA(0x2f7efe)[i]=0x90;
525 for (i=0;i<5;i++) RVA(0x2fa33e)[i]=0x90;
526 #endif
528 #if 0
529 /* TerminateQTML fix */
530 for (i=0;i<47;i++) RVA(0x2fa3b8)[i]=0x90; // terminate thread
531 for (i=0;i<47;i++) RVA(0x2f7f78)[i]=0x90; // terminate thread
532 for (i=0;i<77;i++) RVA(0x1a13d5)[i]=0x90;
533 RVA(0x08e0ae)[0] = 0xc3; // font/dc remover
534 for (i=0;i<24;i++) RVA(0x07a1ad)[i]=0x90; // destroy window
535 #endif
536 } else if (dispatch_addr == RVA(0x13b330))
538 fprintf(stderr, "QuickTime6 DLLs found\n");
539 ptr = (void **)RVA(0x3b9524); // dispatcher_ptr
540 for (i=0;i<5;i++) RVA(0x2730cc)[i]=0x90; // make_new_region
541 for (i=0;i<28;i++) RVA(0x2730f7)[i]=0x90; // call__call_CreateCompatibleDC
542 for (i=0;i<5;i++) RVA(0x273122)[i]=0x90; // jmp_to_call_loadbitmap
543 for (i=0;i<9;i++) RVA(0x273131)[i]=0x90; // call__calls_OLE_shit
544 for (i=0;i<96;i++) RVA(0x2ac852)[i]=0x90; // disable threads
545 } else if (dispatch_addr == RVA(0x13c3e0))
547 fprintf(stderr, "QuickTime6.3 DLLs found\n");
548 ptr = (void **)RVA(0x3ca01c); // dispatcher_ptr
549 for (i=0;i<5;i++) RVA(0x268f6c)[i]=0x90; // make_new_region
550 for (i=0;i<28;i++) RVA(0x268f97)[i]=0x90; // call__call_CreateCompatibleDC
551 for (i=0;i<5;i++) RVA(0x268fc2)[i]=0x90; // jmp_to_call_loadbitmap
552 for (i=0;i<9;i++) RVA(0x268fd1)[i]=0x90; // call__calls_OLE_shit
553 for (i=0;i<96;i++) RVA(0x2b4722)[i]=0x90; // disable threads
554 } else
556 fprintf(stderr, "Unsupported QuickTime version (%p)\n",
557 dispatch_addr);
558 return 0;
561 fprintf(stderr,"QuickTime.qts patched!!! old entry=%p\n",ptr[0]);
563 #ifdef EMU_QTX_API
564 report_entry = report_func;
565 report_ret = report_func_ret;
566 wrapper_target=ptr[0];
567 ptr[0]=wrapper;
568 #endif
570 #undef RVA
572 return wm ? wm->module : 0;
576 /***********************************************************************
577 * LoadLibraryA (KERNEL32)
579 HMODULE WINAPI LoadLibraryA(LPCSTR libname) {
580 return LoadLibraryExA(libname,0,0);
583 /***********************************************************************
584 * FreeLibrary
586 WIN_BOOL WINAPI FreeLibrary(HINSTANCE hLibModule)
588 WIN_BOOL retv = FALSE;
589 WINE_MODREF *wm;
591 wm=MODULE32_LookupHMODULE(hLibModule);
593 if ( !wm || !hLibModule )
595 SetLastError( ERROR_INVALID_HANDLE );
596 return 0;
598 else
599 retv = MODULE_FreeLibrary( wm );
601 MODULE_RemoveFromList(wm);
603 /* garbage... */
604 if (local_wm == NULL) my_garbagecollection();
606 return retv;
609 /***********************************************************************
610 * GetProcAddress (KERNEL32.257)
612 FARPROC WINAPI GetProcAddress( HMODULE hModule, LPCSTR function )
614 return MODULE_GetProcAddress( hModule, function, TRUE );
617 #ifdef DEBUG_QTX_API
620 http://lists.apple.com/archives/quicktime-api/2003/Jan/msg00278.html
623 struct ComponentParameters {
624 unsigned char flags; /* call modifiers: sync/async, deferred, immed, etc */
625 unsigned char paramSize; /* size in bytes of actual parameters passed to this call */
626 short what; /* routine selector, negative for Component management calls */
627 long params[1]; /* actual parameters for the indicated routine */
629 typedef struct ComponentParameters ComponentParameters;
631 static char* component_func(int what){
632 if (what < 0) // Range 0: Standard Component Calls
633 switch(what){
634 case -1: return "kComponentOpenSelect";
635 case -2: return "kComponentCloseSelect";
636 case -3: return "kComponentCanDoSelect";
637 case -4: return "kComponentVersionSelect";
638 case -5: return "kComponentRegisterSelect";
639 case -6: return "kComponentTargetSelect";
640 case -7: return "kComponentUnregisterSelect";
643 if (what >= 0 && what <= 0xff) // Range 1: Generic codecs
644 switch(what & 0xff){
645 case 0: return "kImageCodecGetCodecInfoSelect";
646 case 1: return "kImageCodecGetCompressionTimeSelect";
647 case 2: return "kImageCodecGetMaxCompressionSizeSelect";
648 case 3: return "kImageCodecPreCompressSelect";
649 case 4: return "kImageCodecBandCompressSelect";
650 case 5: return "kImageCodecPreDecompressSelect";
651 case 6: return "kImageCodecBandDecompressSelect";
652 case 7: return "kImageCodecBusySelect";
653 // finish this list from the above URL
654 case 0x10: return "kImageCodecIsImageDescriptionEquivalentSelect";
655 case 0x12: return "kImageCodecDisposeMemorySelect";
656 case 0x14: return "kImageCodecNewImageBufferMemorySelect";
657 case 0x28: return "kImageCodecRequestGammaLevelSelect";
660 //if (what >= 0x100 && what <= 0x1ff) // Range 2: Specific to QT Photo JPEG codecs
662 if (what >= 0x200 && what <= 0x2ff) // Range 3: Base Decompressor
663 switch(what & 0xff){
664 case 0: return "Preflight";
665 case 1: return "Initialize";
666 case 2: return "BeginBand";
667 case 3: return "DrawBand";
668 case 4: return "EndBand";
669 case 5: return "QueueStarting";
670 case 6: return "QueueStopping";
673 return "???";
676 static int c_level=0;
678 static int dump_component(char* name, int type, void* orig, ComponentParameters *params,void** glob){
679 int ( *orig)(ComponentParameters *params, void** glob) = orig;
680 int ret,i;
682 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));
684 for(i=0;i<params->paramSize/4;i++)
685 fprintf(stderr,"%*s param[%d] = 0x%X\n",3*c_level,"",i,params->params[i]);
687 ++c_level;
688 ret=orig(params,glob);
689 --c_level;
691 if(ret>=0x1000)
692 fprintf(stderr,"%*s return=0x%X\n",3*c_level,"",ret);
693 else
694 fprintf(stderr,"%*s return=%d\n",3*c_level,"",ret);
695 return ret;
698 #define DECL_COMPONENT(sname,name,type) \
699 static void* real_ ## sname = NULL; \
700 static int fake_ ## sname(ComponentParameters *params,void** glob){ \
701 return dump_component(name,type,real_ ## sname, params, glob); \
704 #include "qt_comp_template.c"
706 #undef DECL_COMPONENT
708 #include "qt_fv.h"
710 #endif
712 #ifdef EMU_QTX_API
714 #ifdef __OS2__
715 uint32_t _System DosQueryMem(void *, uint32_t *, uint32_t *);
716 #endif
718 static int is_invalid_ptr_handle(void *p)
720 #ifdef __OS2__
721 uint32_t cb = 1;
722 uint32_t fl;
724 if(DosQueryMem(p, &cb, &fl))
725 return 1;
727 // Occasionally, ptr with 'EXEC' attr is passed.
728 // On OS/2, however, malloc() never sets 'EXEC' attr.
729 // So ptr with 'EXEC' attr is invalid.
730 if(fl & 0x04)
731 return 1;
733 return 0;
734 #else
735 return (uint32_t)p >= 0x60000000;
736 #endif
739 static uint32_t ret_array[4096];
740 static int ret_i=0;
742 static int report_func(void *stack_base, int stack_size, reg386_t *reg, uint32_t *flags)
744 #ifdef DEBUG_QTX_API
745 int i;
746 int* dptr;
747 void* pwrapper=NULL;
748 void* pptr=NULL;
749 char* pname=NULL;
750 int plen=-1;
751 // find the code:
753 dptr=0x62b67ae0;dptr+=2*((reg->eax>>16)&255);
754 // printf("FUNC: flag=%d ptr=%p\n",dptr[0],dptr[1]);
755 if(dptr[0]&255){
756 dptr=dptr[1];dptr+=4*(reg->eax&65535);
757 // printf("FUNC: ptr2=%p eax=%p edx=%p\n",dptr[1],dptr[0],dptr[2]);
758 pwrapper=dptr[1]; pptr=dptr[0]; plen=dptr[2];
759 } else {
760 pwrapper=0x62924910;
761 switch(dptr[1]){
762 case 0x629248d0:
763 dptr=0x62b672c0;dptr+=2*(reg->eax&65535);
764 // printf("FUNC: ptr2=%p eax=%p edx=%p\n",0x62924910,dptr[0],dptr[1]);
765 pptr=dptr[0]; plen=dptr[1];
766 break;
767 case 0x62924e40:
768 dptr=0x62b67c70;dptr+=2*(reg->eax&65535);
769 // printf("FUNC: ptr2=%p eax=%p edx=%p\n",0x62924910,dptr[0],dptr[1]);
770 pptr=dptr[0]; plen=dptr[1];
771 break;
772 case 0x62924e60:
773 dptr=0x62b68108;if(reg->eax&0x8000) dptr+=2*(reg->eax|0xffff0000); else dptr+=2*(reg->eax&65535);
774 // printf("FUNC: ptr2=%p eax=%p edx=%p\n",0x62924910,dptr[0],dptr[1]);
775 pptr=dptr[0]; plen=dptr[1];
776 break;
777 case 0x62924e80:
778 dptr=0x62b68108;if(reg->eax&0x8000) dptr+=2*(reg->eax|0xffff0000); else dptr+=2*(reg->eax&65535);
779 // printf("FUNC: ptr2=%p eax=%p edx=%p\n",0x62924910,dptr[0],dptr[1]);
780 pptr=dptr[0]; plen=dptr[1];
781 break;
782 default:
783 printf("FUNC: unknown ptr & psize!\n");
784 pwrapper=dptr[1];
788 for(i=0;qt_fv_list[i].name;i++){
789 if(qt_fv_list[i].id==reg->eax){
790 pname=qt_fv_list[i].name;
791 break;
795 printf("FUNC[%X/%s]: wrapper=%p func=%p len=%d\n",reg->eax,
796 pname?pname:"???",pwrapper,pptr,plen);
798 printf("FUNC: caller=%p ebx=%p\n",((uint32_t *)stack_base)[0],reg->ebx);
800 if(pname)
801 printf("%*sENTER(%d): %s(",ret_i*2,"",ret_i,pname);
802 else
803 printf("%*sENTER(%d): %X(",ret_i*2,"",ret_i,reg->eax);
804 for (i=0;i<plen/4;i++){
805 unsigned int val=((uint32_t *)stack_base)[1+i];
806 unsigned char* fcc=&val;
807 printf("%s0x%X", i?", ":"",val);
808 if(fcc[0]>=0x20 && fcc[0]<128 &&
809 fcc[1]>=0x20 && fcc[1]<128 &&
810 fcc[2]>=0x20 && fcc[2]<128 &&
811 fcc[3]>=0x20 && fcc[3]<128) printf("='%c%c%c%c'",fcc[3],fcc[2],fcc[1],fcc[0]);
812 else if(val>=8 && val<65536) printf("=%d",val);
814 printf(")\n");
815 fflush(stdout);
817 #endif
819 // emulate some functions:
820 switch(reg->eax){
821 // memory management:
822 case 0x150011: //NewPtrClear
823 case 0x150012: //NewPtrSysClear
824 reg->eax = malloc(((uint32_t *)stack_base)[1]);
825 memset((void *)reg->eax,0,((uint32_t *)stack_base)[1]);
826 #ifdef DEBUG_QTX_API
827 printf("%*sLEAVE(%d): EMULATED! 0x%X\n",ret_i*2,"",ret_i, reg->eax);
828 #endif
829 return 1;
830 case 0x15000F: //NewPtr
831 case 0x150010: //NewPtrSys
832 reg->eax = malloc(((uint32_t *)stack_base)[1]);
833 #ifdef DEBUG_QTX_API
834 printf("%*sLEAVE(%d): EMULATED! 0x%X\n",ret_i*2,"",ret_i, reg->eax);
835 #endif
836 return 1;
837 case 0x15002f: //DisposePtr
838 if(is_invalid_ptr_handle(((void **)stack_base)[1]))
839 printf("WARNING! Invalid Ptr handle!\n");
840 else
841 free(((void **)stack_base)[1]);
842 reg->eax=0;
843 #ifdef DEBUG_QTX_API
844 printf("%*sLEAVE(%d): EMULATED! 0x%X\n",ret_i*2,"",ret_i, reg->eax);
845 #endif
846 return 1;
847 // mutexes:
848 case 0x1d0033: //QTMLCreateMutex
849 reg->eax=0xdeadbabe;
850 #ifdef DEBUG_QTX_API
851 printf("%*sLEAVE(%d): EMULATED! 0x%X\n",ret_i*2,"",ret_i, reg->eax);
852 #endif
853 return 1;
854 case 0x1d0034: //QTMLDestroyMutex
855 case 0x1d0035: //QTMLGrabMutex
856 case 0x1d0036: //QTMLReturnMutex
857 case 0x1d003d: //QTMLTryGrabMutex
858 reg->eax=0;
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;
865 #if 0
866 switch(reg->eax){
867 // case 0x00010000:
868 // printf("FUNC: ImageCodecInitialize/ImageCodecGetCodecInfo(ci=%p,&icap=%p)\n",((uint32_t *)stack_base)[1],((uint32_t *)stack_base)[4]);
869 // break;
870 case 0x00010003:
871 printf("FUNC: CountComponents(&desc=%p)\n",((uint32_t *)stack_base)[1]);
872 break;
873 case 0x00010004:
874 printf("FUNC: FindNextComponent(prev=%p,&desc=%p)\n",((uint32_t *)stack_base)[1],((uint32_t *)stack_base)[2]);
875 break;
876 case 0x00010007:
877 printf("FUNC: OpenComponent(prev=%p)\n",((uint32_t *)stack_base)[1]);
878 break;
879 case 0x0003008b:
880 printf("FUNC: QTNewGWorldFromPtr(&pts=%p,fourcc=%.4s,&rect=%p,x1=%p,x2=%p,x3=%p,plane=%p,stride=%d)\n",
881 ((uint32_t *)stack_base)[1],
882 &(((uint32_t *)stack_base)[2]),
883 ((uint32_t *)stack_base)[3],
884 ((uint32_t *)stack_base)[4],
885 ((uint32_t *)stack_base)[5],
886 ((uint32_t *)stack_base)[6],
887 ((uint32_t *)stack_base)[7],
888 ((uint32_t *)stack_base)[8]);
889 break;
890 case 0x001c0018:
891 printf("FUNC: GetGWorldPixMap(gworld=%p)\n",((uint32_t *)stack_base)[1]);
892 break;
893 case 0x00110001:
894 printf("FUNC: Gestalt(fourcc=%.4s, &ret=%p)\n",&(((uint32_t *)stack_base)[1]),((uint32_t *)stack_base)[2]);
895 break;
896 default: {
897 int i;
898 for(i=0;qt_fv_list[i].name;i++){
899 if(qt_fv_list[i].id==reg->eax){
900 printf("FUNC: %s\n",qt_fv_list[i].name);
901 break;
907 // print stack/reg information
908 printf("ENTER(%d) stack = %d bytes @ %p\n"
909 "eax = 0x%08x edx = 0x%08x ebx = 0x%08x ecx = 0x%08x\n"
910 "esp = 0x%08x ebp = 0x%08x esi = 0x%08x edi = 0x%08x\n"
911 "flags = 0x%08x\n", ret_i,
912 stack_size, stack_base,
913 reg->eax, reg->edx, reg->ebx, reg->ecx,
914 reg->esp, reg->ebp, reg->esi, reg->edi,
915 *flags);
916 #endif
918 // save ret addr:
919 ret_array[ret_i]=((uint32_t *)stack_base)[0];
920 ++ret_i;
922 #if 0
923 // print first 7 longs in the stack (return address, arg[1], arg[2] ... )
924 printf("stack[] = { ");
925 for (i=0;i<7;i++) {
926 printf("%08x ", ((uint32_t *)stack_base)[i]);
928 printf("}\n\n");
929 #endif
931 // // mess with function parameters
932 // ((uint32_t *)stack_base)[1] = 0x66554433;
934 // // mess with return address...
935 // reg->eax = 0x11223344;
936 return 0;
939 static int report_func_ret(void *stack_base, int stack_size, reg386_t *reg, uint32_t *flags)
941 //int i;
942 #ifdef DEBUG_QTX_API
943 short err;
944 #endif
946 // restore ret addr:
947 --ret_i;
948 ((uint32_t *)stack_base)[0]=ret_array[ret_i];
950 #ifdef DEBUG_QTX_API
952 #if 1
953 printf("%*sLEAVE(%d): 0x%X",ret_i*2,"",ret_i, reg->eax);
954 err=reg->eax;
955 if(err && (reg->eax>>16)==0) printf(" = %d",err);
956 printf("\n");
957 fflush(stdout);
958 #else
959 // print stack/reg information
960 printf("LEAVE(%d) stack = %d bytes @ %p\n"
961 "eax = 0x%08x edx = 0x%08x ebx = 0x%08x ecx = 0x%08x\n"
962 "esp = 0x%08x ebp = 0x%08x esi = 0x%08x edi = 0x%08x\n"
963 "flags = 0x%08x\n", ret_i,
964 stack_size, stack_base,
965 reg->eax, reg->edx, reg->ebx, reg->ecx,
966 reg->esp, reg->ebp, reg->esi, reg->edi,
967 *flags);
968 #endif
970 #if 0
971 // print first 7 longs in the stack (return address, arg[1], arg[2] ... )
972 printf("stack[] = { ");
973 for (i=0;i<7;i++) {
974 printf("%08x ", ((uint32_t *)stack_base)[i]);
976 printf("}\n\n");
977 #endif
979 #endif
981 // // mess with function parameters
982 // ((uint32_t *)stack_base)[1] = 0x66554433;
984 // // mess with return address...
985 // reg->eax = 0x11223344;
986 return 0;
989 #endif
991 /***********************************************************************
992 * MODULE_GetProcAddress (internal)
994 FARPROC MODULE_GetProcAddress(
995 HMODULE hModule, /* [in] current module handle */
996 LPCSTR function, /* [in] function to be looked up */
997 WIN_BOOL snoop )
999 WINE_MODREF *wm = MODULE32_LookupHMODULE( hModule );
1000 // WINE_MODREF *wm=local_wm;
1001 FARPROC retproc;
1003 #ifdef DEBUG_QTX_API
1004 if (HIWORD(function))
1005 fprintf(stderr,"XXX GetProcAddress(%08lx,%s)\n",(DWORD)hModule,function);
1006 else
1007 fprintf(stderr,"XXX GetProcAddress(%08lx,%p)\n",(DWORD)hModule,function);
1008 #endif
1010 // TRACE_(win32)("(%08lx,%s)\n",(DWORD)hModule,function);
1011 // else
1012 // TRACE_(win32)("(%08lx,%p)\n",(DWORD)hModule,function);
1014 if (!wm) {
1015 SetLastError(ERROR_INVALID_HANDLE);
1016 return (FARPROC)0;
1018 switch (wm->type)
1020 case MODULE32_PE:
1021 retproc = PE_FindExportedFunction( wm, function, snoop );
1022 if (!retproc) SetLastError(ERROR_PROC_NOT_FOUND);
1023 break;
1024 #ifdef HAVE_LIBDL
1025 case MODULE32_ELF:
1026 retproc = (FARPROC) dlsym( (void*) wm->module, function);
1027 if (!retproc) SetLastError(ERROR_PROC_NOT_FOUND);
1028 return retproc;
1029 #endif
1030 default:
1031 ERR("wine_modref type %d not handled.\n",wm->type);
1032 SetLastError(ERROR_INVALID_HANDLE);
1033 return (FARPROC)0;
1036 #ifdef EMU_QTX_API
1037 if (HIWORD(function) && retproc){
1039 #ifdef DEBUG_QTX_API
1040 #define DECL_COMPONENT(sname,name,type) \
1041 if(!strcmp(function,name)){ \
1042 fprintf(stderr,name "dispatcher catched -> %p\n",retproc); \
1043 real_ ## sname = retproc; retproc = fake_ ## sname; \
1045 #include "qt_comp_template.c"
1046 #undef DECL_COMPONENT
1047 #endif
1049 if(!strcmp(function,"theQuickTimeDispatcher")
1050 // || !strcmp(function,"CallComponentFunctionWithStorage")
1051 // || !strcmp(function,"CallComponent")
1053 fprintf(stderr,"theQuickTimeDispatcher catched -> %p\n",retproc);
1054 report_entry = report_func;
1055 report_ret = report_func_ret;
1056 wrapper_target=(void(*)(void))retproc;
1057 retproc=(FARPROC)wrapper;
1061 #endif
1063 return retproc;
1066 static int acounter = 0;
1067 void CodecAlloc(void)
1069 acounter++;
1070 //printf("**************CODEC ALLOC %d\n", acounter);
1073 void CodecRelease(void)
1075 acounter--;
1076 //printf("**************CODEC RELEASE %d\n", acounter);
1077 if (acounter == 0)
1079 for (;;)
1081 modref_list* list = local_wm;
1082 if (!local_wm)
1083 break;
1084 //printf("CODECRELEASE %p\n", list);
1085 MODULE_FreeLibrary(list->wm);
1086 MODULE_RemoveFromList(list->wm);
1087 if (local_wm == NULL)
1088 my_garbagecollection();