]> git.sesse.net Git - vlc/blob - libs/loader/module.c
Test pointers against NULL instead of 0.
[vlc] / libs / loader / module.c
1 /*
2  * Modules
3  *
4  * $Id$
5  *
6  * Copyright 1995 Alexandre Julliard
7  *
8  * Originally distributed under LPGL 2.1 (or later) by the Wine project.
9  *
10  * Modified for use with MPlayer, detailed CVS changelog at
11  * http://www.mplayerhq.hu/cgi-bin/cvsweb.cgi/main/
12  *
13  * File now distributed as part of VLC media player with no modifications.
14  *
15  * This program is free software; you can redistribute it and/or modify
16  * it under the terms of the GNU General Public License as published by
17  * the Free Software Foundation; either version 2 of the License, or
18  * (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
28  */
29
30 // define for quicktime calls debugging and/or MacOS-level emulation:
31 #define EMU_QTX_API
32
33 // define for quicktime debugging (verbose logging):
34 //#define DEBUG_QTX_API
35
36 #include "config.h"
37
38 #include <assert.h>
39 #include <errno.h>
40 #include <fcntl.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45 #include <sys/mman.h>
46 #include <inttypes.h>
47
48 #include "wine/windef.h"
49 #include "wine/winerror.h"
50 #include "wine/heap.h"
51 #include "wine/module.h"
52 #include "wine/pe_image.h"
53 #include "wine/debugtools.h"
54
55 #undef HAVE_LIBDL
56
57 #ifdef HAVE_LIBDL
58 #include <dlfcn.h>
59 #include "wine/elfdll.h"
60 #endif
61 #include "win32.h"
62 #include "driver.h"
63
64 #ifdef EMU_QTX_API
65 #include "wrapper.h"
66 static int report_func(void *stack_base, int stack_size, reg386_t *reg, uint32_t *flags);
67 static int report_func_ret(void *stack_base, int stack_size, reg386_t *reg, uint32_t *flags);
68 #endif
69
70 //#undef TRACE
71 //#define TRACE printf
72
73 //WINE_MODREF *local_wm=NULL;
74 modref_list* local_wm=NULL;
75
76 HANDLE SegptrHeap;
77
78 WINE_MODREF* MODULE_FindModule(LPCSTR m)
79 {
80     modref_list* list=local_wm;
81     TRACE("FindModule: Module %s request\n", m);
82     if(list==NULL)
83         return NULL;
84 //    while(strcmp(m, list->wm->filename))
85     while(!strstr(list->wm->filename, m))
86     {
87         TRACE("%s: %x\n", list->wm->filename, list->wm->module);
88         list=list->prev;
89         if(list==NULL)
90             return NULL;
91     }
92     TRACE("Resolved to %s\n", list->wm->filename);
93     return list->wm;
94 }
95
96 static void MODULE_RemoveFromList(WINE_MODREF *mod)
97 {
98     modref_list* list=local_wm;
99     if(list==NULL)
100         return;
101     if(mod==NULL)
102         return;
103     if((list->prev==NULL)&&(list->next==NULL))
104     {
105         free(list);
106         local_wm=NULL;
107 //      uninstall_fs();
108         return;
109     }
110     for(;list;list=list->prev)
111     {
112         if(list->wm==mod)
113         {
114             if(list->prev)
115                 list->prev->next=list->next;
116             if(list->next)
117                 list->next->prev=list->prev;
118             if(list==local_wm)
119                 local_wm=list->prev;
120             free(list);
121             return;
122         }
123     }
124
125 }
126
127 WINE_MODREF *MODULE32_LookupHMODULE(HMODULE m)
128 {
129     modref_list* list=local_wm;
130     TRACE("LookupHMODULE: Module %X request\n", m);
131     if(list==NULL)
132     {
133         TRACE("LookupHMODULE failed\n");
134         return NULL;
135     }
136     while(m!=list->wm->module)
137     {
138 //      printf("Checking list %X wm %X module %X\n",
139 //      list, list->wm, list->wm->module);
140         list=list->prev;
141         if(list==NULL)
142         {
143             TRACE("LookupHMODULE failed\n");
144             return NULL;
145         }
146     }
147     TRACE("LookupHMODULE hit %p\n", list->wm);
148     return list->wm;
149 }
150
151 /*************************************************************************
152  *              MODULE_InitDll
153  */
154 static WIN_BOOL MODULE_InitDll( WINE_MODREF *wm, DWORD type, LPVOID lpReserved )
155 {
156     WIN_BOOL retv = TRUE;
157
158     static LPCSTR typeName[] = { "PROCESS_DETACH", "PROCESS_ATTACH",
159                                  "THREAD_ATTACH", "THREAD_DETACH" };
160     assert( wm );
161
162
163     /* Skip calls for modules loaded with special load flags */
164
165     if (    ( wm->flags & WINE_MODREF_DONT_RESOLVE_REFS )
166          || ( wm->flags & WINE_MODREF_LOAD_AS_DATAFILE ) )
167         return TRUE;
168
169
170     TRACE("(%s,%s,%p) - CALL\n", wm->modname, typeName[type], lpReserved );
171
172     /* Call the initialization routine */
173     switch ( wm->type )
174     {
175     case MODULE32_PE:
176         retv = PE_InitDLL( wm, type, lpReserved );
177         break;
178
179     case MODULE32_ELF:
180         /* no need to do that, dlopen() already does */
181         break;
182
183     default:
184         ERR("wine_modref type %d not handled.\n", wm->type );
185         retv = FALSE;
186         break;
187     }
188
189     /* The state of the module list may have changed due to the call
190        to PE_InitDLL. We cannot assume that this module has not been
191        deleted.  */
192     TRACE("(%p,%s,%p) - RETURN %d\n", wm, typeName[type], lpReserved, retv );
193
194     return retv;
195 }
196
197 /*************************************************************************
198  *              MODULE_DllProcessAttach
199  *
200  * Send the process attach notification to all DLLs the given module
201  * depends on (recursively). This is somewhat complicated due to the fact that
202  *
203  * - we have to respect the module dependencies, i.e. modules implicitly
204  *   referenced by another module have to be initialized before the module
205  *   itself can be initialized
206  *
207  * - the initialization routine of a DLL can itself call LoadLibrary,
208  *   thereby introducing a whole new set of dependencies (even involving
209  *   the 'old' modules) at any time during the whole process
210  *
211  * (Note that this routine can be recursively entered not only directly
212  *  from itself, but also via LoadLibrary from one of the called initialization
213  *  routines.)
214  *
215  * Furthermore, we need to rearrange the main WINE_MODREF list to allow
216  * the process *detach* notifications to be sent in the correct order.
217  * This must not only take into account module dependencies, but also
218  * 'hidden' dependencies created by modules calling LoadLibrary in their
219  * attach notification routine.
220  *
221  * The strategy is rather simple: we move a WINE_MODREF to the head of the
222  * list after the attach notification has returned.  This implies that the
223  * detach notifications are called in the reverse of the sequence the attach
224  * notifications *returned*.
225  *
226  * NOTE: Assumes that the process critical section is held!
227  *
228  */
229 static WIN_BOOL MODULE_DllProcessAttach( WINE_MODREF *wm, LPVOID lpReserved )
230 {
231     WIN_BOOL retv = TRUE;
232     int i;
233     assert( wm );
234
235     /* prevent infinite recursion in case of cyclical dependencies */
236     if (    ( wm->flags & WINE_MODREF_MARKER )
237          || ( wm->flags & WINE_MODREF_PROCESS_ATTACHED ) )
238         return retv;
239
240     TRACE("(%s,%p) - START\n", wm->modname, lpReserved );
241
242     /* Tag current MODREF to prevent recursive loop */
243     wm->flags |= WINE_MODREF_MARKER;
244
245     /* Recursively attach all DLLs this one depends on */
246 /*    for ( i = 0; retv && i < wm->nDeps; i++ )
247         if ( wm->deps[i] )
248             retv = MODULE_DllProcessAttach( wm->deps[i], lpReserved );
249 */
250     /* Call DLL entry point */
251
252     //local_wm=wm;
253     if(local_wm)
254     {
255         local_wm->next = (modref_list*) malloc(sizeof(modref_list));
256         local_wm->next->prev=local_wm;
257         local_wm->next->next=NULL;
258         local_wm->next->wm=wm;
259         local_wm=local_wm->next;
260     }
261     else
262     {
263         local_wm = (modref_list*)malloc(sizeof(modref_list));
264         local_wm->next=local_wm->prev=NULL;
265         local_wm->wm=wm;
266     }
267     /* Remove recursion flag */
268     wm->flags &= ~WINE_MODREF_MARKER;
269
270     if ( retv )
271     {
272         retv = MODULE_InitDll( wm, DLL_PROCESS_ATTACH, lpReserved );
273         if ( retv )
274             wm->flags |= WINE_MODREF_PROCESS_ATTACHED;
275     }
276
277
278     TRACE("(%s,%p) - END\n", wm->modname, lpReserved );
279
280     return retv;
281 }
282
283 /*************************************************************************
284  *              MODULE_DllProcessDetach
285  *
286  * Send DLL process detach notifications.  See the comment about calling
287  * sequence at MODULE_DllProcessAttach.  Unless the bForceDetach flag
288  * is set, only DLLs with zero refcount are notified.
289  */
290 static void MODULE_DllProcessDetach( WINE_MODREF* wm, WIN_BOOL bForceDetach, LPVOID lpReserved )
291 {
292     //    WINE_MODREF *wm=local_wm;
293     modref_list* l = local_wm;
294     wm->flags &= ~WINE_MODREF_PROCESS_ATTACHED;
295     MODULE_InitDll( wm, DLL_PROCESS_DETACH, lpReserved );
296 /*    while (l)
297     {
298         modref_list* f = l;
299         l = l->next;
300         free(f);
301     }
302     local_wm = 0;*/
303 }
304
305 /***********************************************************************
306  *      MODULE_LoadLibraryExA   (internal)
307  *
308  * Load a PE style module according to the load order.
309  *
310  * The HFILE parameter is not used and marked reserved in the SDK. I can
311  * only guess that it should force a file to be mapped, but I rather
312  * ignore the parameter because it would be extremely difficult to
313  * integrate this with different types of module represenations.
314  *
315  */
316 static WINE_MODREF *MODULE_LoadLibraryExA( LPCSTR libname, HFILE hfile, DWORD flags )
317 {
318         DWORD err = GetLastError();
319         WINE_MODREF *pwm;
320         int i;
321 //      module_loadorder_t *plo;
322
323         SetLastError( ERROR_FILE_NOT_FOUND );
324         TRACE("Trying native dll '%s'\n", libname);
325         pwm = PE_LoadLibraryExA(libname, flags);
326 #ifdef HAVE_LIBDL
327         if(!pwm)
328         {
329             TRACE("Trying ELF dll '%s'\n", libname);
330             pwm=(WINE_MODREF*)ELFDLL_LoadLibraryExA(libname, flags);
331         }
332 #endif
333 //              printf("0x%08x\n", pwm);
334 //              break;
335         if(pwm)
336         {
337                 /* Initialize DLL just loaded */
338                 TRACE("Loaded module '%s' at 0x%08x, \n", libname, pwm->module);
339                 /* Set the refCount here so that an attach failure will */
340                 /* decrement the dependencies through the MODULE_FreeLibrary call. */
341                 pwm->refCount++;
342
343                 SetLastError( err );  /* restore last error */
344                 return pwm;
345         }
346
347
348         WARN("Failed to load module '%s'; error=0x%08lx, \n", libname, GetLastError());
349         return NULL;
350 }
351
352 /***********************************************************************
353  *           MODULE_FreeLibrary
354  *
355  * NOTE: Assumes that the process critical section is held!
356  */
357 static WIN_BOOL MODULE_FreeLibrary( WINE_MODREF *wm )
358 {
359     TRACE("(%s) - START\n", wm->modname );
360
361     /* Recursively decrement reference counts */
362     //MODULE_DecRefCount( wm );
363
364     /* Call process detach notifications */
365     MODULE_DllProcessDetach( wm, FALSE, NULL );
366
367     PE_UnloadLibrary(wm);
368
369     TRACE("END\n");
370
371     return TRUE;
372 }
373
374 /***********************************************************************
375  *           LoadLibraryExA   (KERNEL32)
376  */
377 HMODULE WINAPI LoadLibraryExA(LPCSTR libname, HANDLE hfile, DWORD flags)
378 {
379         WINE_MODREF *wm = 0;
380         char* listpath[] = { "", "", "/usr/lib/win32", "/usr/local/lib/win32", "/usr/lib/codecs", "/usr/local/lib/codecs", 0 };
381         extern char* def_path;
382         char path[512];
383         char checked[2000];
384         int i = -1;
385
386         checked[0] = 0;
387         if(!libname)
388         {
389                 SetLastError(ERROR_INVALID_PARAMETER);
390                 return 0;
391         }
392
393         wm=MODULE_FindModule(libname);
394         if(wm) return wm->module;
395
396 //      if(fs_installed==0)
397 //          install_fs();
398
399         while (wm == NULL && listpath[++i])
400         {
401             if (i < 2)
402             {
403                 if (i == 0)
404                     /* check just original file name */
405                     strncpy(path, libname, 511);
406                 else
407                     /* check default user path */
408                     strncpy(path, def_path, 300);
409             }
410             else if (strcmp(def_path, listpath[i]))
411                 /* path from the list */
412                 strncpy(path, listpath[i], 300);
413             else
414                 continue;
415
416             if (i > 0)
417             {
418                 strcat(path, "/");
419                 strncat(path, libname, 100);
420             }
421             path[511] = 0;
422             wm = MODULE_LoadLibraryExA( path, hfile, flags );
423
424             if (!wm)
425             {
426                 if (checked[0])
427                     strcat(checked, ", ");
428                 strcat(checked, path);
429                 checked[1500] = 0;
430
431             }
432         }
433         if ( wm )
434         {
435                 if ( !MODULE_DllProcessAttach( wm, NULL ) )
436                 {
437                         WARN_(module)("Attach failed for module '%s', \n", libname);
438                         MODULE_FreeLibrary(wm);
439                         SetLastError(ERROR_DLL_INIT_FAILED);
440                         MODULE_RemoveFromList(wm);
441                         wm = NULL;
442                 }
443         }
444
445         if (!wm)
446             printf("Win32 LoadLibrary failed to load: %s\n", checked);
447
448         if (strstr(libname,"vp31vfw.dll") && wm)
449         {
450             int i;
451
452           // sse hack moved from patch dll into runtime patching
453           if (PE_FindExportedFunction(wm, "DriverProc", TRUE)==(void*)0x10001000) {
454             fprintf(stderr, "VP3 DLL found\n");
455             for (i=0;i<18;i++) ((char*)0x10004bd6)[i]=0x90;
456           }
457         }
458
459         // remove a few divs in the VP codecs that make trouble
460         if (strstr(libname,"vp5vfw.dll") && wm)
461         {
462           int i;
463           if (PE_FindExportedFunction(wm, "DriverProc", TRUE)==(void*)0x10003930) {
464             for (i=0;i<3;i++) ((char*)0x10004e86)[i]=0x90;
465             for (i=0;i<3;i++) ((char*)0x10005a23)[i]=0x90;
466             for (i=0;i<3;i++) ((char*)0x10005bff)[i]=0x90;
467           } else {
468             fprintf(stderr, "Unsupported VP5 version\n");
469             return 0;
470           }
471         }
472
473         if (strstr(libname,"vp6vfw.dll") && wm)
474         {
475           int i;
476           if (PE_FindExportedFunction(wm, "DriverProc", TRUE)==(void*)0x10003ef0) {
477             // looks like VP 6.1.0.2
478             for (i=0;i<6;i++) ((char*)0x10007268)[i]=0x90;
479             for (i=0;i<6;i++) ((char*)0x10007e83)[i]=0x90;
480             for (i=0;i<6;i++) ((char*)0x1000806a)[i]=0x90;
481           } else if (PE_FindExportedFunction(wm, "DriverProc", TRUE)==(void*)0x10004120) {
482             // looks like VP 6.2.0.10
483             for (i=0;i<6;i++) ((char*)0x10007688)[i]=0x90;
484             for (i=0;i<6;i++) ((char*)0x100082c3)[i]=0x90;
485             for (i=0;i<6;i++) ((char*)0x100084aa)[i]=0x90;
486           } else if (PE_FindExportedFunction(wm, "DriverProc", TRUE)==(void*)0x10003e70) {
487             // looks like VP 6.0.7.3
488             for (i=0;i<6;i++) ((char*)0x10007559)[i]=0x90;
489             for (i=0;i<6;i++) ((char*)0x100081c3)[i]=0x90;
490             for (i=0;i<6;i++) ((char*)0x1000839e)[i]=0x90;
491           } else {
492             fprintf(stderr, "Unsupported VP6 version\n");
493             return 0;
494           }
495         }
496
497         // Windows Media Video 9 Advanced
498         if (strstr(libname,"wmvadvd.dll") && wm)
499         {
500           // The codec calls IsRectEmpty with coords 0,0,0,0 => result is 0
501           // but it really wants the rectangle to be not empty
502           if (PE_FindExportedFunction(wm, "CreateInstance", TRUE)==(void*)0x08c4b812) {
503             // Dll version is 10.0.0.3645
504             *((char*)0x08c48b0f)=0xeb; // Jump always, ignoring IsRectEmpty result
505           } else {
506             fprintf(stderr, "Unsupported WMVA version\n");
507             return 0;
508           }
509         }
510
511         if (strstr(libname,"QuickTime.qts") && wm)
512         {
513             void** ptr;
514             void *dispatch_addr;
515             int i;
516
517 //          dispatch_addr = GetProcAddress(wm->module, "theQuickTimeDispatcher", TRUE);
518             dispatch_addr = PE_FindExportedFunction(wm, "theQuickTimeDispatcher", TRUE);
519             if (dispatch_addr == (void *)0x62924c30)
520             {
521                 fprintf(stderr, "QuickTime5 DLLs found\n");
522                 ptr = (void **)0x62b75ca4; // dispatch_ptr
523                 for (i=0;i<5;i++)  ((char*)0x6299e842)[i]=0x90; // make_new_region ?
524                 for (i=0;i<28;i++) ((char*)0x6299e86d)[i]=0x90; // call__call_CreateCompatibleDC ?
525                 for (i=0;i<5;i++)  ((char*)0x6299e898)[i]=0x90; // jmp_to_call_loadbitmap ?
526                 for (i=0;i<9;i++)  ((char*)0x6299e8ac)[i]=0x90; // call__calls_OLE_shit ?
527                 for (i=0;i<106;i++) ((char*)0x62a61b10)[i]=0x90; // disable threads
528 #if 0
529                 /* CreateThread callers */
530                 for (i=0;i<5;i++) ((char*)0x629487c5)[i]=0x90;
531                 for (i=0;i<5;i++) ((char*)0x6294b275)[i]=0x90;
532                 for (i=0;i<5;i++) ((char*)0x629a24b1)[i]=0x90;
533                 for (i=0;i<5;i++) ((char*)0x629afc5a)[i]=0x90;
534                 for (i=0;i<5;i++) ((char*)0x62af799c)[i]=0x90;
535                 for (i=0;i<5;i++) ((char*)0x62af7efe)[i]=0x90;
536                 for (i=0;i<5;i++) ((char*)0x62afa33e)[i]=0x90;
537 #endif
538
539 #if 0
540                 /* TerminateQTML fix */
541                 for (i=0;i<47;i++) ((char*)0x62afa3b8)[i]=0x90; // terminate thread
542                 for (i=0;i<47;i++) ((char*)0x62af7f78)[i]=0x90; // terminate thread
543                 for (i=0;i<77;i++) ((char*)0x629a13d5)[i]=0x90;
544                 ((char *)0x6288e0ae)[0] = 0xc3; // font/dc remover
545                 for (i=0;i<24;i++) ((char*)0x6287a1ad)[i]=0x90; // destroy window
546 #endif
547             } else if (dispatch_addr == (void *)0x6693b330)
548             {
549                 fprintf(stderr, "QuickTime6 DLLs found\n");
550                 ptr = (void **)0x66bb9524; // dispatcher_ptr
551                 for (i=0;i<5;i++)  ((char *)0x66a730cc)[i]=0x90; // make_new_region
552                 for (i=0;i<28;i++) ((char *)0x66a730f7)[i]=0x90; // call__call_CreateCompatibleDC
553                 for (i=0;i<5;i++)  ((char *)0x66a73122)[i]=0x90; // jmp_to_call_loadbitmap
554                 for (i=0;i<9;i++)  ((char *)0x66a73131)[i]=0x90; // call__calls_OLE_shit
555                 for (i=0;i<96;i++) ((char *)0x66aac852)[i]=0x90; // disable threads
556             } else if (dispatch_addr == (void *)0x6693c3e0)
557             {
558                 fprintf(stderr, "QuickTime6.3 DLLs found\n");
559                 ptr = (void **)0x66bca01c; // dispatcher_ptr
560                 for (i=0;i<5;i++)  ((char *)0x66a68f6c)[i]=0x90; // make_new_region
561                 for (i=0;i<28;i++) ((char *)0x66a68f97)[i]=0x90; // call__call_CreateCompatibleDC
562                 for (i=0;i<5;i++)  ((char *)0x66a68fc2)[i]=0x90; // jmp_to_call_loadbitmap
563                 for (i=0;i<9;i++)  ((char *)0x66a68fd1)[i]=0x90; // call__calls_OLE_shit
564                 for (i=0;i<96;i++) ((char *)0x66ab4722)[i]=0x90; // disable threads
565             } else
566             {
567                 fprintf(stderr, "Unsupported QuickTime version (%p)\n",
568                     dispatch_addr);
569                 return 0;
570             }
571
572             fprintf(stderr,"QuickTime.qts patched!!! old entry=%p\n",ptr[0]);
573
574 #ifdef EMU_QTX_API
575             report_entry = report_func;
576             report_ret   = report_func_ret;
577             wrapper_target=ptr[0];
578             ptr[0]=wrapper;
579 #endif
580         }
581
582         return wm ? wm->module : 0;
583 }
584
585
586 /***********************************************************************
587  *           LoadLibraryA         (KERNEL32)
588  */
589 HMODULE WINAPI LoadLibraryA(LPCSTR libname) {
590         return LoadLibraryExA(libname,0,0);
591 }
592
593 /***********************************************************************
594  *           FreeLibrary
595  */
596 WIN_BOOL WINAPI FreeLibrary(HINSTANCE hLibModule)
597 {
598     WIN_BOOL retv = FALSE;
599     WINE_MODREF *wm;
600
601     wm=MODULE32_LookupHMODULE(hLibModule);
602
603     if ( !wm || !hLibModule )
604     {
605         SetLastError( ERROR_INVALID_HANDLE );
606         return 0;
607     }
608     else
609         retv = MODULE_FreeLibrary( wm );
610
611     MODULE_RemoveFromList(wm);
612
613     /* garbage... */
614     if (local_wm == NULL) my_garbagecollection();
615
616     return retv;
617 }
618
619 /***********************************************************************
620  *           MODULE_DecRefCount
621  *
622  * NOTE: Assumes that the process critical section is held!
623  */
624 static void MODULE_DecRefCount( WINE_MODREF *wm )
625 {
626     int i;
627
628     if ( wm->flags & WINE_MODREF_MARKER )
629         return;
630
631     if ( wm->refCount <= 0 )
632         return;
633
634     --wm->refCount;
635     TRACE("(%s) refCount: %d\n", wm->modname, wm->refCount );
636
637     if ( wm->refCount == 0 )
638     {
639         wm->flags |= WINE_MODREF_MARKER;
640
641         for ( i = 0; i < wm->nDeps; i++ )
642             if ( wm->deps[i] )
643                 MODULE_DecRefCount( wm->deps[i] );
644
645         wm->flags &= ~WINE_MODREF_MARKER;
646     }
647 }
648
649 /***********************************************************************
650  *           GetProcAddress             (KERNEL32.257)
651  */
652 FARPROC WINAPI GetProcAddress( HMODULE hModule, LPCSTR function )
653 {
654     return MODULE_GetProcAddress( hModule, function, TRUE );
655 }
656
657 #ifdef DEBUG_QTX_API
658
659 /* 
660 http://lists.apple.com/archives/quicktime-api/2003/Jan/msg00278.html
661 */
662
663 struct ComponentParameters {
664     unsigned char                   flags;                      /* call modifiers: sync/async, deferred, immed, etc */
665     unsigned char                   paramSize;                  /* size in bytes of actual parameters passed to this call */
666     short                           what;                       /* routine selector, negative for Component management calls */
667     long                            params[1];                  /* actual parameters for the indicated routine */
668 };
669 typedef struct ComponentParameters      ComponentParameters;
670
671 static char* component_func(int what){
672 if (what < 0) // Range 0: Standard Component Calls
673 switch(what){
674 case -1: return "kComponentOpenSelect";
675 case -2: return "kComponentCloseSelect";
676 case -3: return "kComponentCanDoSelect";
677 case -4: return "kComponentVersionSelect";
678 case -5: return "kComponentRegisterSelect";
679 case -6: return "kComponentTargetSelect";
680 case -7: return "kComponentUnregisterSelect";
681 }
682
683 if (what >= 0 && what <= 0xff) // Range 1: Generic codecs
684 switch(what & 0xff){
685 case 0: return "kImageCodecGetCodecInfoSelect";
686 case 1: return "kImageCodecGetCompressionTimeSelect";
687 case 2: return "kImageCodecGetMaxCompressionSizeSelect";
688 case 3: return "kImageCodecPreCompressSelect";
689 case 4: return "kImageCodecBandCompressSelect";
690 case 5: return "kImageCodecPreDecompressSelect";
691 case 6: return "kImageCodecBandDecompressSelect";
692 case 7: return "kImageCodecBusySelect";
693 // finish this list from the above URL
694 case 0x10: return "kImageCodecIsImageDescriptionEquivalentSelect";
695 case 0x12: return "kImageCodecDisposeMemorySelect";
696 case 0x14: return "kImageCodecNewImageBufferMemorySelect";
697 case 0x28: return "kImageCodecRequestGammaLevelSelect";
698 }
699
700 //if (what >= 0x100 && what <= 0x1ff) // Range 2: Specific to QT Photo JPEG codecs
701
702 if (what >= 0x200 && what <= 0x2ff) // Range 3: Base Decompressor
703 switch(what & 0xff){
704 case 0: return "Preflight";
705 case 1: return "Initialize";
706 case 2: return "BeginBand";
707 case 3: return "DrawBand";
708 case 4: return "EndBand";
709 case 5: return "QueueStarting";
710 case 6: return "QueueStopping";
711 }
712
713 return "???";
714 }
715
716 static int c_level=0;
717
718 static int dump_component(char* name,int type,void* _orig, ComponentParameters *params,void** glob){
719     int ( *orig)(ComponentParameters *params, void** glob) = _orig;
720     int ret,i;
721
722     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));
723
724     for(i=0;i<params->paramSize/4;i++)
725         fprintf(stderr,"%*s param[%d] = 0x%X\n",3*c_level,"",i,params->params[i]);
726
727     ++c_level;
728     ret=orig(params,glob);
729     --c_level;
730     
731     if(ret>=0x1000)
732         fprintf(stderr,"%*s return=0x%X\n",3*c_level,"",ret);
733     else
734         fprintf(stderr,"%*s return=%d\n",3*c_level,"",ret);
735     return ret;
736 }
737
738 #define DECL_COMPONENT(sname,name,type) \
739     static void* real_ ## sname = NULL; \
740     static int fake_ ## sname(ComponentParameters *params,void** glob){ \
741         return dump_component(name,type,real_ ## sname, params, glob); \
742     }
743
744 #include "qt_comp.h"
745
746 #undef DECL_COMPONENT
747
748 #include "qt_fv.h"
749
750 #endif
751
752 #ifdef EMU_QTX_API
753
754 static uint32_t ret_array[4096];
755 static int ret_i=0;
756
757 static int report_func(void *stack_base, int stack_size, reg386_t *reg, uint32_t *flags)
758 {
759 #ifdef DEBUG_QTX_API
760   int i;
761   int* dptr;
762   void* pwrapper=NULL;
763   void* pptr=NULL;
764   char* pname=NULL;
765   int plen=-1;
766   // find the code:
767   
768   dptr=0x62b67ae0;dptr+=2*((reg->eax>>16)&255);
769 //  printf("FUNC: flag=%d ptr=%p\n",dptr[0],dptr[1]);
770   if(dptr[0]&255){
771       dptr=dptr[1];dptr+=4*(reg->eax&65535);
772 //      printf("FUNC: ptr2=%p  eax=%p  edx=%p\n",dptr[1],dptr[0],dptr[2]);
773       pwrapper=dptr[1]; pptr=dptr[0]; plen=dptr[2]; 
774   } else {
775       pwrapper=0x62924910;
776       switch(dptr[1]){
777       case 0x629248d0:
778           dptr=0x62b672c0;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       case 0x62924e40:
783           dptr=0x62b67c70;dptr+=2*(reg->eax&65535);
784 //          printf("FUNC: ptr2=%p  eax=%p  edx=%p\n",0x62924910,dptr[0],dptr[1]);
785           pptr=dptr[0]; plen=dptr[1]; 
786           break;
787       case 0x62924e60:
788           dptr=0x62b68108;if(reg->eax&0x8000) dptr+=2*(reg->eax|0xffff0000); else dptr+=2*(reg->eax&65535);
789 //          printf("FUNC: ptr2=%p  eax=%p  edx=%p\n",0x62924910,dptr[0],dptr[1]);
790           pptr=dptr[0]; plen=dptr[1]; 
791           break;
792       case 0x62924e80:
793           dptr=0x62b68108;if(reg->eax&0x8000) dptr+=2*(reg->eax|0xffff0000); else dptr+=2*(reg->eax&65535);
794 //          printf("FUNC: ptr2=%p  eax=%p  edx=%p\n",0x62924910,dptr[0],dptr[1]);
795           pptr=dptr[0]; plen=dptr[1]; 
796           break;
797       default:
798           printf("FUNC: unknown ptr & psize!\n");
799           pwrapper=dptr[1];
800       }
801   }
802
803   for(i=0;qt_fv_list[i].name;i++){
804           if(qt_fv_list[i].id==reg->eax){
805               pname=qt_fv_list[i].name;
806               break;
807           }
808   }
809
810   printf("FUNC[%X/%s]: wrapper=%p  func=%p  len=%d\n",reg->eax,
811       pname?pname:"???",pwrapper,pptr,plen);
812
813   printf("FUNC: caller=%p  ebx=%p\n",((uint32_t *)stack_base)[0],reg->ebx);
814
815   if(pname)
816       printf("%*sENTER(%d): %s(",ret_i*2,"",ret_i,pname);
817   else
818       printf("%*sENTER(%d): %X(",ret_i*2,"",ret_i,reg->eax);
819   for (i=0;i<plen/4;i++){
820     unsigned int val=((uint32_t *)stack_base)[1+i];
821     unsigned char* fcc=&val;
822     printf("%s0x%X", i?", ":"",val);
823     if(fcc[0]>=0x20 && fcc[0]<128 &&
824        fcc[1]>=0x20 && fcc[1]<128 &&
825        fcc[2]>=0x20 && fcc[2]<128 &&
826        fcc[3]>=0x20 && fcc[3]<128) printf("='%c%c%c%c'",fcc[3],fcc[2],fcc[1],fcc[0]);
827     else if(val>=8 && val<65536) printf("=%d",val);
828   }
829   printf(")\n");
830   fflush(stdout);
831
832 #endif
833   
834 #if 1
835   // emulate some functions:
836   switch(reg->eax){
837   // memory management:
838   case 0x150011: //NewPtrClear
839   case 0x150012: //NewPtrSysClear
840       reg->eax=(uint32_t)malloc(((uint32_t *)stack_base)[1]);
841       memset((void *)reg->eax,0,((uint32_t *)stack_base)[1]);
842 #ifdef DEBUG_QTX_API
843       printf("%*sLEAVE(%d): EMULATED! 0x%X\n",ret_i*2,"",ret_i, reg->eax);
844 #endif
845       return 1;
846   case 0x15000F: //NewPtr
847   case 0x150010: //NewPtrSys
848       reg->eax=(uint32_t)malloc(((uint32_t *)stack_base)[1]);
849 #ifdef DEBUG_QTX_API
850       printf("%*sLEAVE(%d): EMULATED! 0x%X\n",ret_i*2,"",ret_i, reg->eax);
851 #endif
852       return 1;
853   case 0x15002f: //DisposePtr
854       if(((uint32_t *)stack_base)[1]>=0x60000000)
855           printf("WARNING! Invalid Ptr handle!\n");
856       else
857           free((void *)((uint32_t *)stack_base)[1]);
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;
863   // mutexes:
864   case 0x1d0033: //QTMLCreateMutex
865       reg->eax=0xdeadbabe;
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 0x1d0034: //QTMLDestroyMutex
871   case 0x1d0035: //QTMLGrabMutex
872   case 0x1d0036: //QTMLReturnMutex
873   case 0x1d003d: //QTMLTryGrabMutex
874       reg->eax=0;
875 #ifdef DEBUG_QTX_API
876       printf("%*sLEAVE(%d): EMULATED! 0x%X\n",ret_i*2,"",ret_i, reg->eax);
877 #endif
878       return 1;
879   }
880 #endif
881
882 #if 0
883   switch(reg->eax){
884 //  case 0x00010000:
885 //      printf("FUNC: ImageCodecInitialize/ImageCodecGetCodecInfo(ci=%p,&icap=%p)\n",((uint32_t *)stack_base)[1],((uint32_t *)stack_base)[4]);
886 //      break;
887   case 0x00010003:
888       printf("FUNC: CountComponents(&desc=%p)\n",((uint32_t *)stack_base)[1]);
889       break;
890   case 0x00010004:
891       printf("FUNC: FindNextComponent(prev=%p,&desc=%p)\n",((uint32_t *)stack_base)[1],((uint32_t *)stack_base)[2]);
892       break;
893   case 0x00010007:
894       printf("FUNC: OpenComponent(prev=%p)\n",((uint32_t *)stack_base)[1]);
895       break;
896   case 0x0003008b:
897       printf("FUNC: QTNewGWorldFromPtr(&pts=%p,fourcc=%.4s,&rect=%p,x1=%p,x2=%p,x3=%p,plane=%p,stride=%d)\n",
898           ((uint32_t *)stack_base)[1],
899           &(((uint32_t *)stack_base)[2]),
900           ((uint32_t *)stack_base)[3],
901           ((uint32_t *)stack_base)[4],
902           ((uint32_t *)stack_base)[5],
903           ((uint32_t *)stack_base)[6],
904           ((uint32_t *)stack_base)[7],
905           ((uint32_t *)stack_base)[8]);
906       break;
907   case 0x001c0018:
908       printf("FUNC: GetGWorldPixMap(gworld=%p)\n",((uint32_t *)stack_base)[1]);
909       break;
910   case 0x00110001:
911       printf("FUNC: Gestalt(fourcc=%.4s, &ret=%p)\n",&(((uint32_t *)stack_base)[1]),((uint32_t *)stack_base)[2]);
912       break;
913   default: {
914       int i;
915       for(i=0;qt_fv_list[i].name;i++){
916           if(qt_fv_list[i].id==reg->eax){
917               printf("FUNC: %s\n",qt_fv_list[i].name);
918               break;
919           }
920       }
921       }
922   }
923
924   // print stack/reg information 
925   printf("ENTER(%d) stack = %d bytes @ %p\n"
926          "eax = 0x%08x edx = 0x%08x ebx = 0x%08x ecx = 0x%08x\n"
927          "esp = 0x%08x ebp = 0x%08x esi = 0x%08x edi = 0x%08x\n"
928          "flags = 0x%08x\n", ret_i,
929          stack_size, stack_base,
930          reg->eax, reg->edx, reg->ebx, reg->ecx,
931          reg->esp, reg->ebp, reg->esi, reg->edi,
932          *flags);
933 #endif
934
935   // save ret addr:
936   ret_array[ret_i]=((uint32_t *)stack_base)[0];
937   ++ret_i;
938
939 #if 0
940   // print first 7 longs in the stack (return address, arg[1], arg[2] ... ) 
941   printf("stack[] = { ");
942   for (i=0;i<7;i++) {
943     printf("%08x ", ((uint32_t *)stack_base)[i]);
944   }
945   printf("}\n\n");
946 #endif
947   
948 //  // mess with function parameters 
949 //  ((uint32_t *)stack_base)[1] = 0x66554433;
950
951 //  // mess with return address...
952 //  reg->eax = 0x11223344;
953     return 0;
954 }
955
956 static int report_func_ret(void *stack_base, int stack_size, reg386_t *reg, uint32_t *flags)
957 {
958   int i;
959   short err;
960
961   // restore ret addr:
962   --ret_i;
963   ((uint32_t *)stack_base)[0]=ret_array[ret_i];
964
965 #ifdef DEBUG_QTX_API
966
967 #if 1
968   printf("%*sLEAVE(%d): 0x%X",ret_i*2,"",ret_i, reg->eax);
969   err=reg->eax;
970   if(err && (reg->eax>>16)==0) printf(" = %d",err);
971   printf("\n");
972   fflush(stdout);
973 #else
974   // print stack/reg information 
975   printf("LEAVE(%d) stack = %d bytes @ %p\n"
976          "eax = 0x%08x edx = 0x%08x ebx = 0x%08x ecx = 0x%08x\n"
977          "esp = 0x%08x ebp = 0x%08x esi = 0x%08x edi = 0x%08x\n"
978          "flags = 0x%08x\n", ret_i,
979          stack_size, stack_base,
980          reg->eax, reg->edx, reg->ebx, reg->ecx,
981          reg->esp, reg->ebp, reg->esi, reg->edi,
982          *flags);
983 #endif
984
985 #if 0
986   // print first 7 longs in the stack (return address, arg[1], arg[2] ... ) 
987   printf("stack[] = { ");
988   for (i=0;i<7;i++) {
989     printf("%08x ", ((uint32_t *)stack_base)[i]);
990   }
991   printf("}\n\n");
992 #endif
993
994 #endif
995   
996 //  // mess with function parameters 
997 //  ((uint32_t *)stack_base)[1] = 0x66554433;
998
999 //  // mess with return address...
1000 //  reg->eax = 0x11223344;
1001     return 0;
1002 }
1003
1004 #endif
1005
1006 /***********************************************************************
1007  *           MODULE_GetProcAddress              (internal)
1008  */
1009 FARPROC MODULE_GetProcAddress(
1010         HMODULE hModule,        /* [in] current module handle */
1011         LPCSTR function,        /* [in] function to be looked up */
1012         WIN_BOOL snoop )
1013 {
1014     WINE_MODREF *wm = MODULE32_LookupHMODULE( hModule );
1015 //    WINE_MODREF *wm=local_wm;
1016     FARPROC     retproc;
1017
1018 #ifdef DEBUG_QTX_API
1019     if (HIWORD(function))
1020         fprintf(stderr,"XXX GetProcAddress(%08lx,%s)\n",(DWORD)hModule,function);
1021     else
1022         fprintf(stderr,"XXX GetProcAddress(%08lx,%p)\n",(DWORD)hModule,function);
1023 #endif
1024
1025 //      TRACE_(win32)("(%08lx,%s)\n",(DWORD)hModule,function);
1026 //    else
1027 //      TRACE_(win32)("(%08lx,%p)\n",(DWORD)hModule,function);
1028
1029     if (!wm) {
1030         SetLastError(ERROR_INVALID_HANDLE);
1031         return (FARPROC)0;
1032     }
1033     switch (wm->type)
1034     {
1035     case MODULE32_PE:
1036         retproc = PE_FindExportedFunction( wm, function, snoop );
1037         if (!retproc) SetLastError(ERROR_PROC_NOT_FOUND);
1038         break;
1039 #ifdef HAVE_LIBDL
1040     case MODULE32_ELF:
1041         retproc = (FARPROC) dlsym( (void*) wm->module, function);
1042         if (!retproc) SetLastError(ERROR_PROC_NOT_FOUND);
1043         return retproc;
1044 #endif
1045     default:
1046         ERR("wine_modref type %d not handled.\n",wm->type);
1047         SetLastError(ERROR_INVALID_HANDLE);
1048         return (FARPROC)0;
1049     }
1050
1051 #ifdef EMU_QTX_API
1052     if (HIWORD(function) && retproc){
1053
1054 #ifdef DEBUG_QTX_API
1055 #define DECL_COMPONENT(sname,name,type) \
1056     if(!strcmp(function,name)){ \
1057         fprintf(stderr,name "dispatcher catched -> %p\n",retproc); \
1058         real_ ## sname = retproc; retproc = fake_ ## sname; \
1059     }
1060 #include "qt_comp.h"
1061 #undef DECL_COMPONENT
1062 #endif
1063
1064     if(!strcmp(function,"theQuickTimeDispatcher")
1065 //      || !strcmp(function,"_CallComponentFunctionWithStorage")
1066 //      || !strcmp(function,"_CallComponent")
1067       ){
1068         fprintf(stderr,"theQuickTimeDispatcher catched -> %p\n",retproc);
1069       report_entry = report_func;
1070       report_ret   = report_func_ret;
1071       wrapper_target=(void(*)(void))retproc;
1072       retproc=(FARPROC)wrapper;
1073     }
1074
1075     }
1076 #endif
1077
1078     return retproc;
1079 }
1080
1081 static int acounter = 0;
1082 void CodecAlloc(void)
1083 {
1084     acounter++;
1085     //printf("**************CODEC ALLOC %d\n", acounter);
1086 }
1087
1088 void CodecRelease(void)
1089 {
1090     acounter--;
1091     //printf("**************CODEC RELEASE %d\n", acounter);
1092     if (acounter == 0)
1093     {
1094         for (;;)
1095         {
1096             modref_list* list = local_wm;
1097             if (!local_wm)
1098                 break;
1099             //printf("CODECRELEASE %p\n", list);
1100             MODULE_FreeLibrary(list->wm);
1101             MODULE_RemoveFromList(list->wm);
1102             if (local_wm == NULL)
1103                 my_garbagecollection();
1104         }
1105     }
1106 }