]> git.sesse.net Git - vlc/blob - src/misc/modules.c
* src/misc/modules.c: some error checking when loading the cache plugins file.
[vlc] / src / misc / modules.c
1 /*****************************************************************************
2  * modules.c : Builtin and plugin modules management functions
3  *****************************************************************************
4  * Copyright (C) 2001-2004 VideoLAN
5  * $Id$
6  *
7  * Authors: Sam Hocevar <sam@zoy.org>
8  *          Ethan C. Baldridge <BaldridgeE@cadmus.com>
9  *          Hans-Peter Jansen <hpj@urpla.net>
10  *          Gildas Bazin <gbazin@videolan.org>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
25  *****************************************************************************/
26
27 /* Some faulty libcs have a broken struct dirent when _FILE_OFFSET_BITS
28  * is set to 64. Don't try to be cleverer. */
29 #ifdef _FILE_OFFSET_BITS
30 #undef _FILE_OFFSET_BITS
31 #endif
32
33 #include <stdlib.h>                                      /* free(), strtol() */
34 #include <stdio.h>                                              /* sprintf() */
35 #include <string.h>                                              /* strdup() */
36
37 #include <vlc/vlc.h>
38
39 #ifdef HAVE_DIRENT_H
40 #   include <dirent.h>
41 #elif defined( UNDER_CE )
42 #   include <windows.h>                               /* GetFileAttributes() */
43 #else
44 #   include "../extras/dirent.h"
45 #endif
46
47 #ifdef HAVE_SYS_TYPES_H
48 #   include <sys/types.h>
49 #endif
50 #ifdef HAVE_SYS_STAT_H
51 #   include <sys/stat.h>
52 #endif
53 #ifdef HAVE_UNISTD_H
54 #   include <unistd.h>
55 #endif
56
57 #define HAVE_DYNAMIC_PLUGINS
58 #if defined(HAVE_DL_DYLD)
59 #   if defined(HAVE_MACH_O_DYLD_H)
60 #       include <mach-o/dyld.h>
61 #   endif
62 #elif defined(HAVE_DL_BEOS)
63 #   if defined(HAVE_IMAGE_H)
64 #       include <image.h>
65 #   endif
66 #elif defined(HAVE_DL_WINDOWS)
67 #   include <windows.h>
68 #elif defined(HAVE_DL_DLOPEN)
69 #   if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */
70 #       include <dlfcn.h>
71 #   endif
72 #   if defined(HAVE_SYS_DL_H)
73 #       include <sys/dl.h>
74 #   endif
75 #elif defined(HAVE_DL_SHL_LOAD)
76 #   if defined(HAVE_DL_H)
77 #       include <dl.h>
78 #   endif
79 #else
80 #   undef HAVE_DYNAMIC_PLUGINS
81 #endif
82
83 #include "vlc_error.h"
84
85 #include "vlc_interface.h"
86 #include "intf_eject.h"
87
88 #include "stream_control.h"
89 #include "input_ext-intf.h"
90 #include "input_ext-dec.h"
91 #include "input_ext-plugins.h"
92 #include "ninput.h"
93
94 #include "vlc_playlist.h"
95
96 #include "vlc_video.h"
97 #include "video_output.h"
98 #include "vout_synchro.h"
99
100 #include "audio_output.h"
101 #include "aout_internal.h"
102
103 #include "stream_output.h"
104 #include "osd.h"
105 #include "vlc_httpd.h"
106
107 #include "iso_lang.h"
108 #include "charset.h"
109
110 #include "vlc_block.h"
111
112 #include "vlc_vlm.h"
113
114 #ifdef HAVE_DYNAMIC_PLUGINS
115 #   include "modules_plugin.h"
116 #endif
117
118 #if defined( UNDER_CE )
119 #    include "modules_builtin_evc.h"
120 #elif defined( _MSC_VER )
121 #    include "modules_builtin_msvc.h"
122 #else
123 #    include "modules_builtin.h"
124 #endif
125 #include "network.h"
126
127 #if defined( WIN32) || defined( UNDER_CE )
128     /* Avoid name collisions */
129 #   define LoadModule(a,b,c) _LoadModule(a,b,c)
130 #endif
131
132 /*****************************************************************************
133  * Local prototypes
134  *****************************************************************************/
135 #ifdef HAVE_DYNAMIC_PLUGINS
136 static void AllocateAllPlugins  ( vlc_object_t * );
137 static void AllocatePluginDir   ( vlc_object_t *, const char *, int );
138 static int  AllocatePluginFile  ( vlc_object_t *, char *, int64_t, int64_t );
139 static module_t * AllocatePlugin( vlc_object_t *, char * );
140 #endif
141 static int  AllocateBuiltinModule( vlc_object_t *, int ( * ) ( module_t * ) );
142 static int  DeleteModule ( module_t * );
143 #ifdef HAVE_DYNAMIC_PLUGINS
144 static void   DupModule        ( module_t * );
145 static void   UndupModule      ( module_t * );
146 static int    CallEntry        ( module_t * );
147 static int    LoadModule       ( vlc_object_t *, char *, module_handle_t * );
148 static void   CloseModule      ( module_handle_t );
149 static void * GetSymbol        ( module_handle_t, const char * );
150 static void   CacheLoad        ( vlc_object_t * );
151 static int    CacheLoadConfig  ( module_t *, FILE * );
152 static void   CacheSave        ( vlc_object_t * );
153 static void   CacheSaveConfig  ( module_t *, FILE * );
154 static void   CacheMerge       ( vlc_object_t *, module_t *, module_t * );
155 static module_cache_t * CacheFind( vlc_object_t *, char *, int64_t, int64_t );
156
157 #if defined(HAVE_DL_WINDOWS)
158 static char * GetWindowsError  ( void );
159 #endif
160 #endif
161
162 /*****************************************************************************
163  * module_InitBank: create the module bank.
164  *****************************************************************************
165  * This function creates a module bank structure which will be filled later
166  * on with all the modules found.
167  *****************************************************************************/
168 void __module_InitBank( vlc_object_t *p_this )
169 {
170     module_bank_t *p_bank;
171     vlc_value_t  lockval;
172
173     var_Create( p_this->p_libvlc, "libvlc", VLC_VAR_MUTEX );
174     var_Get( p_this->p_libvlc, "libvlc", &lockval );
175     vlc_mutex_lock( lockval.p_address );
176     if( p_this->p_libvlc->p_module_bank )
177     {
178         p_this->p_libvlc->p_module_bank->i_usage++;
179         vlc_mutex_unlock( lockval.p_address );
180         var_Destroy( p_this->p_libvlc, "libvlc" );
181         return;
182     }
183     vlc_mutex_unlock( lockval.p_address );
184     var_Destroy( p_this->p_libvlc, "libvlc" );
185
186     p_bank = vlc_object_create( p_this, sizeof(module_bank_t) );
187     p_bank->psz_object_name = "module bank";
188     p_bank->i_usage = 1;
189     p_bank->i_cache = p_bank->i_loaded_cache = 0;
190     p_bank->pp_cache = p_bank->pp_loaded_cache = 0;
191     p_bank->b_cache = p_bank->b_cache_dirty =
192         p_bank->b_cache_delete = VLC_FALSE;
193
194     /*
195      * Store the symbols to be exported
196      */
197 #ifdef HAVE_DYNAMIC_PLUGINS
198     STORE_SYMBOLS( &p_bank->symbols );
199 #endif
200
201     /* Everything worked, attach the object */
202     p_this->p_libvlc->p_module_bank = p_bank;
203     vlc_object_attach( p_bank, p_this->p_libvlc );
204
205     module_LoadMain( p_this );
206
207     return;
208 }
209
210 /*****************************************************************************
211  * module_ResetBank: reset the module bank.
212  *****************************************************************************
213  * This function resets the module bank by unloading all unused plugin
214  * modules.
215  *****************************************************************************/
216 void __module_ResetBank( vlc_object_t *p_this )
217 {
218     msg_Err( p_this, "FIXME: module_ResetBank unimplemented" );
219     return;
220 }
221
222 /*****************************************************************************
223  * module_EndBank: empty the module bank.
224  *****************************************************************************
225  * This function unloads all unused plugin modules and empties the module
226  * bank in case of success.
227  *****************************************************************************/
228 void __module_EndBank( vlc_object_t *p_this )
229 {
230     module_t * p_next;
231     vlc_value_t  lockval;
232
233     var_Create( p_this->p_libvlc, "libvlc", VLC_VAR_MUTEX );
234     var_Get( p_this->p_libvlc, "libvlc", &lockval );
235     vlc_mutex_lock( lockval.p_address );
236     if( !p_this->p_libvlc->p_module_bank )
237     {
238         vlc_mutex_unlock( lockval.p_address );
239         var_Destroy( p_this->p_libvlc, "libvlc" );
240         return;
241     }
242     if( --p_this->p_libvlc->p_module_bank->i_usage )
243     {
244         vlc_mutex_unlock( lockval.p_address );
245         var_Destroy( p_this->p_libvlc, "libvlc" );
246         return;
247     }
248     vlc_mutex_unlock( lockval.p_address );
249     var_Destroy( p_this->p_libvlc, "libvlc" );
250
251 #ifdef HAVE_DYNAMIC_PLUGINS
252     if( p_this->p_libvlc->p_module_bank->b_cache ) CacheSave( p_this );
253 #endif
254
255     vlc_object_detach( p_this->p_libvlc->p_module_bank );
256
257     while( p_this->p_libvlc->p_module_bank->i_children )
258     {
259         p_next = (module_t *)p_this->p_libvlc->p_module_bank->pp_children[0];
260
261         if( DeleteModule( p_next ) )
262         {
263             /* Module deletion failed */
264             msg_Err( p_this, "module \"%s\" can't be removed, trying harder",
265                      p_next->psz_object_name );
266
267             /* We just free the module by hand. Niahahahahaha. */
268             vlc_object_detach( p_next );
269             vlc_object_destroy( p_next );
270         }
271     }
272
273     vlc_object_destroy( p_this->p_libvlc->p_module_bank );
274
275     return;
276 }
277
278 /*****************************************************************************
279  * module_LoadMain: load the main program info into the module bank.
280  *****************************************************************************
281  * This function fills the module bank structure with the main module infos.
282  * This is very useful as it will allow us to consider the main program just
283  * as another module, and for instance the configuration options of main will
284  * be available in the module bank structure just as for every other module.
285  *****************************************************************************/
286 void __module_LoadMain( vlc_object_t *p_this )
287 {
288     AllocateBuiltinModule( p_this, vlc_entry__main );
289 }
290
291 /*****************************************************************************
292  * module_LoadBuiltins: load all modules which we built with.
293  *****************************************************************************
294  * This function fills the module bank structure with the builtin modules.
295  *****************************************************************************/
296 void __module_LoadBuiltins( vlc_object_t * p_this )
297 {
298     msg_Dbg( p_this, "checking builtin modules" );
299     ALLOCATE_ALL_BUILTINS();
300 }
301
302 /*****************************************************************************
303  * module_LoadPlugins: load all plugin modules we can find.
304  *****************************************************************************
305  * This function fills the module bank structure with the plugin modules.
306  *****************************************************************************/
307 void __module_LoadPlugins( vlc_object_t * p_this )
308 {
309 #ifdef HAVE_DYNAMIC_PLUGINS
310     msg_Dbg( p_this, "checking plugin modules" );
311
312     if( config_GetInt( p_this, "plugins-cache" ) )
313         p_this->p_libvlc->p_module_bank->b_cache = VLC_TRUE;
314
315     if( p_this->p_libvlc->p_module_bank->b_cache ||
316         p_this->p_libvlc->p_module_bank->b_cache_delete ) CacheLoad( p_this );
317
318     AllocateAllPlugins( p_this );
319 #endif
320 }
321
322 /*****************************************************************************
323  * module_Need: return the best module function, given a capability list.
324  *****************************************************************************
325  * This function returns the module that best fits the asked capabilities.
326  *****************************************************************************/
327 module_t * __module_Need( vlc_object_t *p_this, const char *psz_capability,
328                           const char *psz_name, vlc_bool_t b_strict )
329 {
330     typedef struct module_list_t module_list_t;
331
332     struct module_list_t
333     {
334         module_t *p_module;
335         int i_score;
336         module_list_t *p_next;
337     };
338
339     module_list_t *p_list, *p_first, *p_tmp;
340     vlc_list_t *p_all;
341
342     int i_which_module, i_index = 0;
343     vlc_bool_t b_intf = VLC_FALSE;
344
345     module_t *p_module;
346
347     int   i_shortcuts = 0;
348     char *psz_shortcuts = NULL, *psz_var = NULL;
349
350     msg_Dbg( p_this, "looking for %s module", psz_capability );
351
352     /* Deal with variables */
353     if( psz_name && psz_name[0] == '$' )
354     {
355         vlc_value_t val;
356         var_Create( p_this, psz_name + 1, VLC_VAR_MODULE | VLC_VAR_DOINHERIT );
357         var_Get( p_this, psz_name + 1, &val );
358         psz_var = val.psz_string;
359         psz_name = psz_var;
360     }
361
362     /* Count how many different shortcuts were asked for */
363     if( psz_name && *psz_name )
364     {
365         char *psz_parser, *psz_last_shortcut;
366
367         /* If the user wants none, give him none. */
368         if( !strcmp( psz_name, "none" ) )
369         {
370             if( psz_var ) free( psz_var );
371             return NULL;
372         }
373
374         i_shortcuts++;
375         psz_shortcuts = psz_last_shortcut = strdup( psz_name );
376
377         for( psz_parser = psz_shortcuts; *psz_parser; psz_parser++ )
378         {
379             if( *psz_parser == ',' )
380             {
381                  *psz_parser = '\0';
382                  i_shortcuts++;
383                  psz_last_shortcut = psz_parser + 1;
384             }
385         }
386
387         /* Check if the user wants to override the "strict" mode */
388         if( psz_last_shortcut )
389         {
390             if( !strcmp(psz_last_shortcut, "none") )
391             {
392                 b_strict = VLC_TRUE;
393                 i_shortcuts--;
394             }
395             else if( !strcmp(psz_last_shortcut, "any") )
396             {
397                 b_strict = VLC_FALSE;
398                 i_shortcuts--;
399             }
400         }
401     }
402
403     /* Sort the modules and test them */
404     p_all = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
405     p_list = malloc( p_all->i_count * sizeof( module_list_t ) );
406     p_first = NULL;
407
408     /* Parse the module list for capabilities and probe each of them */
409     for( i_which_module = 0; i_which_module < p_all->i_count; i_which_module++ )
410     {
411         module_t * p_submodule = NULL;
412         int i_shortcut_bonus = 0, i_submodule;
413
414         p_module = (module_t *)p_all->p_values[i_which_module].p_object;
415
416         /* Test that this module can do what we need */
417         if( strcmp( p_module->psz_capability, psz_capability ) )
418         {
419             for( i_submodule = 0;
420                  i_submodule < p_module->i_children;
421                  i_submodule++ )
422             {
423                 if( !strcmp( ((module_t*)p_module->pp_children[ i_submodule ])
424                                            ->psz_capability, psz_capability ) )
425                 {
426                     p_submodule =
427                             (module_t*)p_module->pp_children[ i_submodule ];
428                     break;
429                 }
430             }
431
432             if( p_submodule == NULL )
433             {
434                 continue;
435             }
436
437             p_module = p_submodule;
438         }
439
440         /* Test if we have the required CPU */
441         if( (p_module->i_cpu & p_this->p_libvlc->i_cpu) != p_module->i_cpu )
442         {
443             continue;
444         }
445
446         /* If we required a shortcut, check this plugin provides it. */
447         if( i_shortcuts > 0 )
448         {
449             vlc_bool_t b_trash;
450             int i_dummy, i_short = i_shortcuts;
451             char *psz_name = psz_shortcuts;
452
453             /* Let's drop modules with a 0 score (unless they are
454              * explicitly requested) */
455             b_trash = !p_module->i_score;
456
457             while( i_short > 0 )
458             {
459                 for( i_dummy = 0; p_module->pp_shortcuts[i_dummy]; i_dummy++ )
460                 {
461                     if( !strcasecmp( psz_name,
462                                      p_module->pp_shortcuts[i_dummy] ) )
463                     {
464                         /* Found it */
465                         b_trash = VLC_FALSE;
466                         i_shortcut_bonus = i_short * 10000;
467                         break;
468                     }
469                 }
470
471                 if( i_shortcut_bonus )
472                 {
473                     /* We found it... remember ? */
474                     break;
475                 }
476
477                 /* Go to the next shortcut... This is so lame! */
478                 while( *psz_name )
479                 {
480                     psz_name++;
481                 }
482                 psz_name++;
483                 i_short--;
484             }
485
486             /* If we are in "strict" mode and we couldn't
487              * find the module in the list of provided shortcuts,
488              * then kick the bastard out of here!!! */
489             if( i_short == 0 && b_strict )
490             {
491                 b_trash = VLC_TRUE;
492             }
493
494             if( b_trash )
495             {
496                 continue;
497             }
498         }
499         /* If we didn't require a shortcut, trash zero-scored plugins */
500         else if( !p_module->i_score )
501         {
502             continue;
503         }
504
505         /* Special case: test if we requested a particular intf plugin */
506         if( !i_shortcuts && p_module->psz_program
507              && !strcmp( p_module->psz_program,
508                          p_this->p_vlc->psz_object_name ) )
509         {
510             if( !b_intf )
511             {
512                 /* Remove previous non-matching plugins */
513                 i_index = 0;
514                 b_intf = VLC_TRUE;
515             }
516         }
517         else if( b_intf )
518         {
519             /* This one doesn't match */
520             continue;
521         }
522
523         /* Store this new module */
524         p_list[ i_index ].p_module = p_module;
525         p_list[ i_index ].i_score = p_module->i_score + i_shortcut_bonus;
526
527         /* Add it to the modules-to-probe list */
528         if( i_index == 0 )
529         {
530             p_list[ 0 ].p_next = NULL;
531             p_first = p_list;
532         }
533         else
534         {
535             /* Ok, so at school you learned that quicksort is quick, and
536              * bubble sort sucks raw eggs. But that's when dealing with
537              * thousands of items. Here we have barely 50. */
538             module_list_t *p_newlist = p_first;
539
540             if( p_first->i_score < p_list[ i_index ].i_score )
541             {
542                 p_list[ i_index ].p_next = p_first;
543                 p_first = &p_list[ i_index ];
544             }
545             else
546             {
547                 while( p_newlist->p_next != NULL &&
548                     p_newlist->p_next->i_score >= p_list[ i_index ].i_score )
549                 {
550                     p_newlist = p_newlist->p_next;
551                 }
552
553                 p_list[ i_index ].p_next = p_newlist->p_next;
554                 p_newlist->p_next = &p_list[ i_index ];
555             }
556         }
557
558         i_index++;
559     }
560
561     msg_Dbg( p_this, "probing %i candidate%s",
562                      i_index, i_index == 1 ? "" : "s" );
563
564     /* Lock all candidate modules */
565     p_tmp = p_first;
566     while( p_tmp != NULL )
567     {
568         vlc_object_yield( p_tmp->p_module );
569         p_tmp = p_tmp->p_next;
570     }
571
572     /* We can release the list, interesting modules were yielded */
573     vlc_list_release( p_all );
574
575     /* Parse the linked list and use the first successful module */
576     p_tmp = p_first;
577     while( p_tmp != NULL )
578     {
579 #ifdef HAVE_DYNAMIC_PLUGINS
580         /* Make sure the module is loaded in mem */
581         module_t *p_module = p_tmp->p_module->b_submodule ?
582             (module_t *)p_tmp->p_module->p_parent : p_tmp->p_module;
583         if( !p_module->b_builtin && !p_module->b_loaded )
584         {
585             module_t *p_new_module =
586                 AllocatePlugin( p_this, p_module->psz_filename );
587             if( p_new_module )
588             {
589                 CacheMerge( p_this, p_module, p_new_module );
590                 vlc_object_attach( p_new_module, p_module );
591                 DeleteModule( p_new_module );
592             }
593         }
594 #endif
595
596         if( p_tmp->p_module->pf_activate
597              && p_tmp->p_module->pf_activate( p_this ) == VLC_SUCCESS )
598         {
599             break;
600         }
601
602         vlc_object_release( p_tmp->p_module );
603         p_tmp = p_tmp->p_next;
604     }
605
606     /* Store the locked module value */
607     if( p_tmp != NULL )
608     {
609         p_module = p_tmp->p_module;
610         p_tmp = p_tmp->p_next;
611     }
612     else
613     {
614         p_module = NULL;
615     }
616
617     /* Unlock the remaining modules */
618     while( p_tmp != NULL )
619     {
620         vlc_object_release( p_tmp->p_module );
621         p_tmp = p_tmp->p_next;
622     }
623
624     free( p_list );
625
626     if( p_module != NULL )
627     {
628         msg_Dbg( p_module, "using %s module \"%s\"",
629                  psz_capability, p_module->psz_object_name );
630     }
631     else if( p_first == NULL )
632     {
633         msg_Err( p_this, "no %s module matched \"%s\"",
634                  psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
635     }
636     else if( psz_name != NULL && *psz_name )
637     {
638         msg_Warn( p_this, "no %s module matching \"%s\" could be loaded",
639                   psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
640     }
641
642     if( psz_shortcuts )
643     {
644         free( psz_shortcuts );
645     }
646
647     if( psz_var )
648     {
649         free( psz_var );
650     }
651
652     /* Don't forget that the module is still locked */
653     return p_module;
654 }
655
656 /*****************************************************************************
657  * module_Unneed: decrease the usage count of a module.
658  *****************************************************************************
659  * This function must be called by the thread that called module_Need, to
660  * decrease the reference count and allow for hiding of modules.
661  *****************************************************************************/
662 void __module_Unneed( vlc_object_t * p_this, module_t * p_module )
663 {
664     /* Use the close method */
665     if( p_module->pf_deactivate )
666     {
667         p_module->pf_deactivate( p_this );
668     }
669
670     msg_Dbg( p_module, "unlocking module \"%s\"", p_module->psz_object_name );
671
672     vlc_object_release( p_module );
673
674     return;
675 }
676
677 /*****************************************************************************
678  * Following functions are local.
679  *****************************************************************************/
680
681 /*****************************************************************************
682  * AllocateAllPlugins: load all plugin modules we can find.
683  *****************************************************************************/
684 #ifdef HAVE_DYNAMIC_PLUGINS
685 static void AllocateAllPlugins( vlc_object_t *p_this )
686 {
687     /* Yes, there are two NULLs because we replace one with "plugin-path". */
688     char *          path[] = { "modules", PLUGIN_PATH, "plugins", NULL,
689                                NULL };
690
691     char **         ppsz_path = path;
692     char *          psz_fullpath;
693
694     /* If the user provided a plugin path, we add it to the list */
695     path[ sizeof(path)/sizeof(char*) - 2 ] =
696         config_GetPsz( p_this, "plugin-path" );
697
698     for( ; *ppsz_path != NULL ; ppsz_path++ )
699     {
700 #if defined( SYS_BEOS ) || defined( SYS_DARWIN ) \
701      || ( defined( WIN32 ) && !defined( UNDER_CE ) )
702
703         /* Handle relative as well as absolute paths */
704 #ifdef WIN32
705         if( !(*ppsz_path)[0] || (*ppsz_path)[1] != ':' )
706 #else
707         if( (*ppsz_path)[0] != '/' )
708 #endif
709         {
710             int i_dirlen = strlen( *ppsz_path );
711             i_dirlen += strlen( p_this->p_libvlc->psz_vlcpath ) + 2;
712
713             psz_fullpath = malloc( i_dirlen );
714             if( psz_fullpath == NULL )
715             {
716                 continue;
717             }
718 #ifdef WIN32
719             sprintf( psz_fullpath, "%s\\%s",
720                      p_this->p_libvlc->psz_vlcpath, *ppsz_path );
721 #else
722             sprintf( psz_fullpath, "%s/%s",
723                      p_this->p_libvlc->psz_vlcpath, *ppsz_path );
724 #endif
725         }
726         else
727 #endif
728         {
729             psz_fullpath = strdup( *ppsz_path );
730         }
731
732         msg_Dbg( p_this, "recursively browsing `%s'", psz_fullpath );
733
734         /* Don't go deeper than 5 subdirectories */
735         AllocatePluginDir( p_this, psz_fullpath, 5 );
736
737         free( psz_fullpath );
738     }
739
740     /* Free plugin-path */
741     if( path[ sizeof(path)/sizeof(char*) - 2 ] )
742         free( path[ sizeof(path)/sizeof(char*) - 2 ] );
743     path[ sizeof(path)/sizeof(char*) - 2 ] = NULL;
744 }
745
746 /*****************************************************************************
747  * AllocatePluginDir: recursively parse a directory to look for plugins
748  *****************************************************************************/
749 static void AllocatePluginDir( vlc_object_t *p_this, const char *psz_dir,
750                                int i_maxdepth )
751 {
752 #if defined( UNDER_CE ) || defined( _MSC_VER )
753 #ifdef UNDER_CE
754     wchar_t psz_path[MAX_PATH + 256];
755     wchar_t psz_wdir[MAX_PATH];
756 #else
757     char psz_path[MAX_PATH + 256];
758 #endif
759     WIN32_FIND_DATA finddata;
760     HANDLE handle;
761     unsigned int rc;
762 #else
763     int    i_dirlen;
764     DIR *  dir;
765     struct dirent * file;
766 #endif
767     char * psz_file;
768
769     if( p_this->p_vlc->b_die || i_maxdepth < 0 )
770     {
771         return;
772     }
773
774 #if defined( UNDER_CE ) || defined( _MSC_VER )
775     MultiByteToWideChar( CP_ACP, 0, psz_dir, -1, psz_wdir, MAX_PATH );
776
777     rc = GetFileAttributes( psz_wdir );
778     if( !(rc & FILE_ATTRIBUTE_DIRECTORY) )
779     {
780         /* Not a directory */
781         return;
782     }
783
784     /* Parse all files in the directory */
785 #ifdef UNDER_CE
786     swprintf( psz_path, L"%s\\*.*", psz_dir );
787 #else
788     sprintf( psz_path, "%s\\*.*", psz_dir );
789 #endif
790     handle = FindFirstFile( psz_path, &finddata );
791     if( handle == INVALID_HANDLE_VALUE )
792     {
793         /* Empty directory */
794         return;
795     }
796
797     /* Parse the directory and try to load all files it contains. */
798     do
799     {
800 #ifdef UNDER_CE
801         unsigned int i_len = wcslen( finddata.cFileName );
802         swprintf( psz_path, L"%s\\%s", psz_dir, finddata.cFileName );
803 #else
804         unsigned int i_len = strlen( finddata.cFileName );
805         /* Skip ".", ".." and anything starting with "." */
806         if( !*finddata.cFileName || *finddata.cFileName == '.' )
807         {
808             if( !FindNextFile( handle, &finddata ) ) break;
809             continue;
810         }
811         sprintf( psz_path, "%s\\%s", psz_dir, finddata.cFileName );
812 #endif
813
814         if( GetFileAttributes( psz_path ) & FILE_ATTRIBUTE_DIRECTORY )
815         {
816             AllocatePluginDir( p_this, psz_path, i_maxdepth - 1 );
817         }
818         else if( i_len > strlen( LIBEXT )
819 #ifdef UNDER_CE
820                 )
821 #else
822                   /* We only load files ending with LIBEXT */
823                   && !strncasecmp( psz_path + strlen( psz_path)
824                                    - strlen( LIBEXT ),
825                                    LIBEXT, strlen( LIBEXT ) ) )
826 #endif
827         {
828 #ifdef UNDER_CE
829             char psz_filename[MAX_PATH];
830             WideCharToMultiByte( CP_ACP, WC_DEFAULTCHAR, psz_path, -1,
831                                  psz_filename, MAX_PATH, NULL, NULL );
832             psz_file = psz_filename;
833 #else
834             psz_file = psz_path;
835 #endif
836
837             AllocatePluginFile( p_this, psz_file, 0, 0 );
838         }
839     }
840     while( !p_this->p_vlc->b_die && FindNextFile( handle, &finddata ) );
841
842     /* Close the directory */
843     FindClose( handle );
844
845 #else
846     dir = opendir( psz_dir );
847     if( !dir )
848     {
849         return;
850     }
851
852     i_dirlen = strlen( psz_dir );
853
854     /* Parse the directory and try to load all files it contains. */
855     while( !p_this->p_vlc->b_die && (file = readdir( dir )) )
856     {
857         struct stat statbuf;
858         unsigned int i_len;
859         int i_stat;
860
861         /* Skip ".", ".." and anything starting with "." */
862         if( !*file->d_name || *file->d_name == '.' )
863         {
864             continue;
865         }
866
867         i_len = strlen( file->d_name );
868         psz_file = malloc( i_dirlen + 1 + i_len + 1 );
869 #ifdef WIN32
870         sprintf( psz_file, "%s\\%s", psz_dir, file->d_name );
871 #else
872         sprintf( psz_file, "%s/%s", psz_dir, file->d_name );
873 #endif
874
875         i_stat = stat( psz_file, &statbuf );
876         if( !i_stat && statbuf.st_mode & S_IFDIR )
877         {
878             AllocatePluginDir( p_this, psz_file, i_maxdepth - 1 );
879         }
880         else if( i_len > strlen( LIBEXT )
881                   /* We only load files ending with LIBEXT */
882                   && !strncasecmp( file->d_name + i_len - strlen( LIBEXT ),
883                                    LIBEXT, strlen( LIBEXT ) ) )
884         {
885             int64_t i_time = 0, i_size = 0;
886
887             if( !i_stat )
888             {
889                 i_time = statbuf.st_mtime;
890                 i_size = statbuf.st_size;
891             }
892
893             AllocatePluginFile( p_this, psz_file, i_time, i_size );
894         }
895
896         free( psz_file );
897     }
898
899     /* Close the directory */
900     closedir( dir );
901
902 #endif
903 }
904
905 /*****************************************************************************
906  * AllocatePluginFile: load a module into memory and initialize it.
907  *****************************************************************************
908  * This function loads a dynamically loadable module and allocates a structure
909  * for its information data. The module can then be handled by module_Need
910  * and module_Unneed. It can be removed by DeleteModule.
911  *****************************************************************************/
912 static int AllocatePluginFile( vlc_object_t * p_this, char * psz_file,
913                                int64_t i_file_time, int64_t i_file_size )
914 {
915     module_t * p_module;
916     module_cache_t *p_cache_entry = NULL;
917
918     /*
919      * Check our plugins cache first then load plugin if needed
920      */
921     p_cache_entry =
922         CacheFind( p_this, psz_file, i_file_time, i_file_size );
923
924     if( !p_cache_entry )
925     {
926         p_module = AllocatePlugin( p_this, psz_file );
927     }
928     else
929     {
930         /* If junk dll, don't try to load it */
931         if( p_cache_entry->b_junk )
932         {
933             p_module = NULL;
934         }
935         else
936         {
937             module_config_t *p_item;
938
939             p_module = p_cache_entry->p_module;
940             p_module->b_loaded = VLC_FALSE;
941
942             /* For now we force loading if the module's config contains
943              * callbacks or actions.
944              * Could be optimized by adding an API call.*/
945             for( p_item = p_module->p_config;
946                  p_item->i_type != CONFIG_HINT_END; p_item++ )
947             {
948                 if( p_item->pf_callback || p_item->i_action )
949                     p_module = AllocatePlugin( p_this, psz_file );
950             }
951         }
952     }
953
954     if( p_module )
955     {
956         /* Everything worked fine !
957          * The module is ready to be added to the list. */
958         p_module->b_builtin = VLC_FALSE;
959
960         /* msg_Dbg( p_this, "plugin \"%s\", %s",
961                     p_module->psz_object_name, p_module->psz_longname ); */
962
963         vlc_object_attach( p_module, p_this->p_libvlc->p_module_bank );
964     }
965
966     if( !p_this->p_libvlc->p_module_bank->b_cache ) return 0;
967
968     /* Add entry to cache */
969 #define p_bank p_this->p_libvlc->p_module_bank
970     p_bank->pp_cache =
971         realloc( p_bank->pp_cache, (p_bank->i_cache + 1) * sizeof(void *) );
972     p_bank->pp_cache[p_bank->i_cache] = malloc( sizeof(module_cache_t) );
973     p_bank->pp_cache[p_bank->i_cache]->psz_file = strdup( psz_file );
974     p_bank->pp_cache[p_bank->i_cache]->i_time = i_file_time;
975     p_bank->pp_cache[p_bank->i_cache]->i_size = i_file_size;
976     p_bank->pp_cache[p_bank->i_cache]->b_junk = p_module ? 0 : 1;
977     p_bank->pp_cache[p_bank->i_cache]->p_module = p_module;
978     p_bank->i_cache++;
979
980     return p_module ? 0 : -1;
981 }
982
983 /*****************************************************************************
984  * AllocatePlugin: load a module into memory and initialize it.
985  *****************************************************************************
986  * This function loads a dynamically loadable module and allocates a structure
987  * for its information data. The module can then be handled by module_Need
988  * and module_Unneed. It can be removed by DeleteModule.
989  *****************************************************************************/
990 static module_t * AllocatePlugin( vlc_object_t * p_this, char * psz_file )
991 {
992     module_t * p_module;
993     module_handle_t handle;
994
995     if( LoadModule( p_this, psz_file, &handle ) ) return NULL;
996
997     /* Now that we have successfully loaded the module, we can
998      * allocate a structure for it */
999     p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
1000     if( p_module == NULL )
1001     {
1002         msg_Err( p_this, "out of memory" );
1003         CloseModule( handle );
1004         return NULL;
1005     }
1006
1007     /* We need to fill these since they may be needed by CallEntry() */
1008     p_module->psz_filename = psz_file;
1009     p_module->handle = handle;
1010     p_module->p_symbols = &p_this->p_libvlc->p_module_bank->symbols;
1011     p_module->b_loaded = VLC_TRUE;
1012
1013     /* Initialize the module: fill p_module, default config */
1014     if( CallEntry( p_module ) != 0 )
1015     {
1016         /* We couldn't call module_init() */
1017         vlc_object_destroy( p_module );
1018         CloseModule( handle );
1019         return NULL;
1020     }
1021
1022     DupModule( p_module );
1023     p_module->psz_filename = strdup( p_module->psz_filename );
1024     p_module->psz_longname = strdup( p_module->psz_longname );
1025
1026     /* Everything worked fine ! The module is ready to be added to the list. */
1027     p_module->b_builtin = VLC_FALSE;
1028
1029     return p_module;
1030 }
1031
1032 /*****************************************************************************
1033  * DupModule: make a plugin module standalone.
1034  *****************************************************************************
1035  * This function duplicates all strings in the module, so that the dynamic
1036  * object can be unloaded. It acts recursively on submodules.
1037  *****************************************************************************/
1038 static void DupModule( module_t *p_module )
1039 {
1040     char **pp_shortcut;
1041     int i_submodule;
1042
1043     for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1044     {
1045         *pp_shortcut = strdup( *pp_shortcut );
1046     }
1047
1048     /* We strdup() these entries so that they are still valid when the
1049      * module is unloaded. */
1050     p_module->psz_object_name = strdup( p_module->psz_object_name );
1051     p_module->psz_capability = strdup( p_module->psz_capability );
1052
1053     if( p_module->psz_program != NULL )
1054     {
1055         p_module->psz_program = strdup( p_module->psz_program );
1056     }
1057
1058     for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1059     {
1060         DupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1061     }
1062 }
1063
1064 /*****************************************************************************
1065  * UndupModule: free a duplicated module.
1066  *****************************************************************************
1067  * This function frees the allocations done in DupModule().
1068  *****************************************************************************/
1069 static void UndupModule( module_t *p_module )
1070 {
1071     char **pp_shortcut;
1072     int i_submodule;
1073
1074     for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1075     {
1076         UndupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1077     }
1078
1079     for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1080     {
1081         free( *pp_shortcut );
1082     }
1083
1084     free( p_module->psz_object_name );
1085     free( p_module->psz_capability );
1086
1087     if( p_module->psz_program != NULL )
1088     {
1089         free( p_module->psz_program );
1090     }
1091 }
1092
1093 #endif /* HAVE_DYNAMIC_PLUGINS */
1094
1095 /*****************************************************************************
1096  * AllocateBuiltinModule: initialize a builtin module.
1097  *****************************************************************************
1098  * This function registers a builtin module and allocates a structure
1099  * for its information data. The module can then be handled by module_Need
1100  * and module_Unneed. It can be removed by DeleteModule.
1101  *****************************************************************************/
1102 static int AllocateBuiltinModule( vlc_object_t * p_this,
1103                                   int ( *pf_entry ) ( module_t * ) )
1104 {
1105     module_t * p_module;
1106
1107     /* Now that we have successfully loaded the module, we can
1108      * allocate a structure for it */
1109     p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
1110     if( p_module == NULL )
1111     {
1112         msg_Err( p_this, "out of memory" );
1113         return -1;
1114     }
1115
1116     /* Initialize the module : fill p_module->psz_object_name, etc. */
1117     if( pf_entry( p_module ) != 0 )
1118     {
1119         /* With a well-written module we shouldn't have to print an
1120          * additional error message here, but just make sure. */
1121         msg_Err( p_this, "failed calling entry point in builtin module" );
1122         vlc_object_destroy( p_module );
1123         return -1;
1124     }
1125
1126     /* Everything worked fine ! The module is ready to be added to the list. */
1127     p_module->b_builtin = VLC_TRUE;
1128
1129     /* msg_Dbg( p_this, "builtin \"%s\", %s",
1130                 p_module->psz_object_name, p_module->psz_longname ); */
1131
1132     vlc_object_attach( p_module, p_this->p_libvlc->p_module_bank );
1133
1134     return 0;
1135 }
1136
1137 /*****************************************************************************
1138  * DeleteModule: delete a module and its structure.
1139  *****************************************************************************
1140  * This function can only be called if the module isn't being used.
1141  *****************************************************************************/
1142 static int DeleteModule( module_t * p_module )
1143 {
1144     vlc_object_detach( p_module );
1145
1146     /* We free the structures that we strdup()ed in Allocate*Module(). */
1147 #ifdef HAVE_DYNAMIC_PLUGINS
1148     if( !p_module->b_builtin )
1149     {
1150         if( p_module->b_loaded && p_module->b_unloadable )
1151         {
1152             CloseModule( p_module->handle );
1153         }
1154         UndupModule( p_module );
1155         free( p_module->psz_filename );
1156         free( p_module->psz_longname );
1157     }
1158 #endif
1159
1160     /* Free and detach the object's children */
1161     while( p_module->i_children )
1162     {
1163         vlc_object_t *p_this = p_module->pp_children[0];
1164         vlc_object_detach( p_this );
1165         vlc_object_destroy( p_this );
1166     }
1167
1168     config_Free( p_module );
1169     vlc_object_destroy( p_module );
1170
1171     return 0;
1172 }
1173
1174 #ifdef HAVE_DYNAMIC_PLUGINS
1175 /*****************************************************************************
1176  * CallEntry: call an entry point.
1177  *****************************************************************************
1178  * This function calls a symbol given its name and a module structure. The
1179  * symbol MUST refer to a function returning int and taking a module_t* as
1180  * an argument.
1181  *****************************************************************************/
1182 static int CallEntry( module_t * p_module )
1183 {
1184     static char *psz_name = "vlc_entry" MODULE_SUFFIX;
1185     int (* pf_symbol) ( module_t * p_module );
1186
1187     /* Try to resolve the symbol */
1188     pf_symbol = (int (*)(module_t *)) GetSymbol( p_module->handle, psz_name );
1189
1190     if( pf_symbol == NULL )
1191     {
1192 #if defined(HAVE_DL_DYLD) || defined(HAVE_DL_BEOS)
1193         msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s'",
1194                             psz_name, p_module->psz_filename );
1195 #elif defined(HAVE_DL_WINDOWS)
1196         char *psz_error = GetWindowsError();
1197         msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1198                             psz_name, p_module->psz_filename, psz_error );
1199         free( psz_error );
1200 #elif defined(HAVE_DL_DLOPEN)
1201         msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1202                             psz_name, p_module->psz_filename, dlerror() );
1203 #elif defined(HAVE_DL_SHL_LOAD)
1204         msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1205                             psz_name, p_module->psz_filename, strerror(errno) );
1206 #else
1207 #   error "Something is wrong in modules.c"
1208 #endif
1209         return -1;
1210     }
1211
1212     /* We can now try to call the symbol */
1213     if( pf_symbol( p_module ) != 0 )
1214     {
1215         /* With a well-written module we shouldn't have to print an
1216          * additional error message here, but just make sure. */
1217         msg_Err( p_module, "failed calling symbol \"%s\" in file `%s'",
1218                            psz_name, p_module->psz_filename );
1219         return -1;
1220     }
1221
1222     /* Everything worked fine, we can return */
1223     return 0;
1224 }
1225
1226 /*****************************************************************************
1227  * LoadModule: loads a dynamic library
1228  *****************************************************************************
1229  * This function loads a dynamically linked library using a system dependant
1230  * method. Will return 0 on success as well as the module handle.
1231  *****************************************************************************/
1232 static int LoadModule( vlc_object_t *p_this, char *psz_file,
1233                        module_handle_t *p_handle )
1234 {
1235     module_handle_t handle;
1236
1237 #if defined(HAVE_DL_DYLD)
1238     NSObjectFileImage image;
1239     NSObjectFileImageReturnCode ret;
1240
1241     ret = NSCreateObjectFileImageFromFile( psz_file, &image );
1242
1243     if( ret != NSObjectFileImageSuccess )
1244     {
1245         msg_Warn( p_this, "cannot create image from `%s'", psz_file );
1246         return -1;
1247     }
1248
1249     /* Open the dynamic module */
1250     handle = NSLinkModule( image, psz_file,
1251                            NSLINKMODULE_OPTION_RETURN_ON_ERROR );
1252
1253     if( !handle )
1254     {
1255         NSLinkEditErrors errors;
1256         const char *psz_file, *psz_err;
1257         int i_errnum;
1258         NSLinkEditError( &errors, &i_errnum, &psz_file, &psz_err );
1259         msg_Warn( p_this, "cannot link module `%s' (%s)", psz_file, psz_err );
1260         NSDestroyObjectFileImage( image );
1261         return -1;
1262     }
1263
1264     /* Destroy our image, we won't need it */
1265     NSDestroyObjectFileImage( image );
1266
1267 #elif defined(HAVE_DL_BEOS)
1268     handle = load_add_on( psz_file );
1269     if( handle < 0 )
1270     {
1271         msg_Warn( p_this, "cannot load module `%s'", psz_file );
1272         return -1;
1273     }
1274
1275 #elif defined(HAVE_DL_WINDOWS)
1276     handle = LoadLibrary( psz_file );
1277     if( handle == NULL )
1278     {
1279         char *psz_err = GetWindowsError();
1280         msg_Warn( p_this, "cannot load module `%s' (%s)", psz_file, psz_err );
1281         free( psz_err );
1282     }
1283
1284 #elif defined(HAVE_DL_DLOPEN) && defined(RTLD_NOW)
1285     /* static is OK, we are called atomically */
1286     static vlc_bool_t b_kde = VLC_FALSE;
1287
1288 #   if defined(SYS_LINUX)
1289     /* XXX HACK #1 - we should NOT open modules with RTLD_GLOBAL, or we
1290      * are going to get namespace collisions when two modules have common
1291      * public symbols, but ALSA is being a pest here. */
1292     if( strstr( psz_file, "alsa_plugin" ) )
1293     {
1294         handle = dlopen( psz_file, RTLD_NOW | RTLD_GLOBAL );
1295         if( handle == NULL )
1296         {
1297             msg_Warn( p_this, "cannot load module `%s' (%s)",
1298                               psz_file, dlerror() );
1299             return -1;
1300         }
1301     }
1302 #   endif
1303     /* XXX HACK #2 - the ugly KDE workaround. It seems that libkdewhatever
1304      * causes dlopen() to segfault if libstdc++ is not loaded in the caller,
1305      * so we just load libstdc++. Bwahahaha! ph34r! -- Sam. */
1306     /* Update: FYI, this is Debian bug #180505, and seems to be fixed. */
1307     if( !b_kde && !strstr( psz_file, "kde" ) )
1308     {
1309         dlopen( "libstdc++.so.6", RTLD_NOW )
1310          || dlopen( "libstdc++.so.5", RTLD_NOW )
1311          || dlopen( "libstdc++.so.4", RTLD_NOW )
1312          || dlopen( "libstdc++.so.3", RTLD_NOW );
1313         b_kde = VLC_TRUE;
1314     }
1315
1316     handle = dlopen( psz_file, RTLD_NOW );
1317     if( handle == NULL )
1318     {
1319         msg_Warn( p_this, "cannot load module `%s' (%s)",
1320                           psz_file, dlerror() );
1321         return -1;
1322     }
1323
1324 #elif defined(HAVE_DL_DLOPEN)
1325 #   if defined(DL_LAZY)
1326     handle = dlopen( psz_file, DL_LAZY );
1327 #   else
1328     handle = dlopen( psz_file, 0 );
1329 #   endif
1330     if( handle == NULL )
1331     {
1332         msg_Warn( p_this, "cannot load module `%s' (%s)",
1333                           psz_file, dlerror() );
1334         return -1;
1335     }
1336
1337 #elif defined(HAVE_DL_SHL_LOAD)
1338     handle = shl_load( psz_file, BIND_IMMEDIATE | BIND_NONFATAL, NULL );
1339     if( handle == NULL )
1340     {
1341         msg_Warn( p_this, "cannot load module `%s' (%s)",
1342                           psz_file, strerror(errno) );
1343         return -1;
1344     }
1345
1346 #else
1347 #   error "Something is wrong in modules.c"
1348
1349 #endif
1350
1351     *p_handle = handle;
1352     return 0;
1353 }
1354
1355 /*****************************************************************************
1356  * CloseModule: unload a dynamic library
1357  *****************************************************************************
1358  * This function unloads a previously opened dynamically linked library
1359  * using a system dependant method. No return value is taken in consideration,
1360  * since some libraries sometimes refuse to close properly.
1361  *****************************************************************************/
1362 static void CloseModule( module_handle_t handle )
1363 {
1364 #if defined(HAVE_DL_DYLD)
1365     NSUnLinkModule( handle, FALSE );
1366
1367 #elif defined(HAVE_DL_BEOS)
1368     unload_add_on( handle );
1369
1370 #elif defined(HAVE_DL_WINDOWS)
1371     FreeLibrary( handle );
1372
1373 #elif defined(HAVE_DL_DLOPEN)
1374     dlclose( handle );
1375
1376 #elif defined(HAVE_DL_SHL_LOAD)
1377     shl_unload( handle );
1378
1379 #endif
1380     return;
1381 }
1382
1383 /*****************************************************************************
1384  * GetSymbol: get a symbol from a dynamic library
1385  *****************************************************************************
1386  * This function queries a loaded library for a symbol specified in a
1387  * string, and returns a pointer to it. We don't check for dlerror() or
1388  * similar functions, since we want a non-NULL symbol anyway.
1389  *****************************************************************************/
1390 static void * _module_getsymbol( module_handle_t, const char * );
1391
1392 static void * GetSymbol( module_handle_t handle, const char * psz_function )
1393 {
1394     void * p_symbol = _module_getsymbol( handle, psz_function );
1395
1396     /* MacOS X dl library expects symbols to begin with "_". So do
1397      * some other operating systems. That's really lame, but hey, what
1398      * can we do ? */
1399     if( p_symbol == NULL )
1400     {
1401         char *psz_call = malloc( strlen( psz_function ) + 2 );
1402
1403         strcpy( psz_call + 1, psz_function );
1404         psz_call[ 0 ] = '_';
1405         p_symbol = _module_getsymbol( handle, psz_call );
1406         free( psz_call );
1407     }
1408
1409     return p_symbol;
1410 }
1411
1412 static void * _module_getsymbol( module_handle_t handle,
1413                                  const char * psz_function )
1414 {
1415 #if defined(HAVE_DL_DYLD)
1416     NSSymbol sym = NSLookupSymbolInModule( handle, psz_function );
1417     return NSAddressOfSymbol( sym );
1418
1419 #elif defined(HAVE_DL_BEOS)
1420     void * p_symbol;
1421     if( B_OK == get_image_symbol( handle, psz_function,
1422                                   B_SYMBOL_TYPE_TEXT, &p_symbol ) )
1423     {
1424         return p_symbol;
1425     }
1426     else
1427     {
1428         return NULL;
1429     }
1430
1431 #elif defined(HAVE_DL_WINDOWS) && defined(UNDER_CE)
1432     wchar_t psz_real[256];
1433     MultiByteToWideChar( CP_ACP, 0, psz_function, -1, psz_real, 256 );
1434
1435     return (void *)GetProcAddress( handle, psz_real );
1436
1437 #elif defined(HAVE_DL_WINDOWS) && defined(WIN32)
1438     return (void *)GetProcAddress( handle, (char *)psz_function );
1439
1440 #elif defined(HAVE_DL_DLOPEN)
1441     return dlsym( handle, psz_function );
1442
1443 #elif defined(HAVE_DL_SHL_LOAD)
1444     void *p_sym;
1445     shl_findsym( &handle, psz_function, TYPE_UNDEFINED, &p_sym );
1446     return p_sym;
1447
1448 #endif
1449 }
1450
1451 #if defined(HAVE_DL_WINDOWS)
1452 static char * GetWindowsError( void )
1453 {
1454 #if defined(UNDER_CE)
1455     wchar_t psz_tmp[256];
1456     char * psz_buffer = malloc( 256 );
1457 #else
1458     char * psz_tmp = malloc( 256 );
1459 #endif
1460     int i = 0, i_error = GetLastError();
1461
1462     FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
1463                    NULL, i_error, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
1464                    (LPTSTR) psz_tmp, 256, NULL );
1465
1466     /* Go to the end of the string */
1467 #if defined(UNDER_CE)
1468     while( psz_tmp[i] && psz_tmp[i] != L'\r' && psz_tmp[i] != L'\n' )
1469 #else
1470     while( psz_tmp[i] && psz_tmp[i] != '\r' && psz_tmp[i] != '\n' )
1471 #endif
1472     {
1473         i++;
1474     }
1475
1476     if( psz_tmp[i] )
1477     {
1478 #if defined(UNDER_CE)
1479         swprintf( psz_tmp + i, L" (error %i)", i_error );
1480         psz_tmp[ 255 ] = L'\0';
1481 #else
1482         snprintf( psz_tmp + i, 256 - i, " (error %i)", i_error );
1483         psz_tmp[ 255 ] = '\0';
1484 #endif
1485     }
1486
1487 #if defined(UNDER_CE)
1488     WideCharToMultiByte( CP_ACP, WC_DEFAULTCHAR, psz_tmp, -1,
1489                          psz_buffer, 256, NULL, NULL );
1490     return psz_buffer;
1491 #else
1492     return psz_tmp;
1493 #endif
1494 }
1495 #endif /* HAVE_DL_WINDOWS */
1496
1497 /*****************************************************************************
1498  * LoadPluginsCache: loads the plugins cache file
1499  *****************************************************************************
1500  * This function will load the plugin cache if present and valid. This cache
1501  * will in turn be queried by AllocateAllPlugins() to see if it needs to
1502  * actually load the dynamically loadable module.
1503  * This allows us to only fully load plugins when they are actually used.
1504  *****************************************************************************/
1505 static void CacheLoad( vlc_object_t *p_this )
1506 {
1507     char *psz_filename, *psz_homedir;
1508     FILE *file;
1509     int i, j, i_size, i_read;
1510     char p_cachestring[sizeof(PLUGINSCACHE_FILE COPYRIGHT_MESSAGE)];
1511     int i_cache;
1512     module_cache_t **pp_cache = 0;
1513     int32_t i_file_size;
1514
1515     psz_homedir = p_this->p_vlc->psz_homedir;
1516     if( !psz_homedir )
1517     {
1518         msg_Err( p_this, "psz_homedir is null" );
1519         return;
1520     }
1521     psz_filename =
1522         (char *)malloc( sizeof("/" CONFIG_DIR "/" PLUGINSCACHE_FILE) +
1523                         strlen(psz_homedir) );
1524
1525     if( psz_filename )
1526         sprintf( psz_filename, "%s/" CONFIG_DIR "/" PLUGINSCACHE_FILE,
1527                  psz_homedir );
1528
1529     if( !psz_filename )
1530     {
1531         msg_Err( p_this, "out of memory" );
1532         return;
1533     }
1534
1535     if( p_this->p_libvlc->p_module_bank->b_cache_delete )
1536     {
1537         msg_Dbg( p_this, "removing plugins cache file %s", psz_filename );
1538         unlink( psz_filename );
1539         return;
1540     }
1541
1542     msg_Dbg( p_this, "loading plugins cache file %s", psz_filename );
1543
1544     file = fopen( psz_filename, "r" );
1545     if( !file )
1546     {
1547         msg_Warn( p_this, "could not open plugins cache file %s for reading",
1548                   psz_filename );
1549         free( psz_filename );
1550         return;
1551     }
1552     free( psz_filename );
1553
1554     /* Check the file size */
1555     i_read = fread( &i_file_size, sizeof(char), sizeof(i_file_size), file );
1556     if( i_read != sizeof(i_file_size) )
1557     {
1558         msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1559                   "(too short)" );
1560         fclose( file );
1561         return;
1562     }
1563
1564     fseek( file, 0, SEEK_END );
1565     if( ftell( file ) != i_file_size )
1566     {
1567         msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1568                   "(corrupted size)" );
1569         fclose( file );
1570         return;
1571     }
1572     fseek( file, sizeof(i_file_size), SEEK_SET );
1573
1574     /* Check the file is a plugins cache */
1575     i_size = sizeof(PLUGINSCACHE_FILE COPYRIGHT_MESSAGE) - 1;
1576     i_read = fread( p_cachestring, sizeof(char), i_size, file );
1577     if( i_read != i_size ||
1578         memcmp( p_cachestring, PLUGINSCACHE_FILE COPYRIGHT_MESSAGE, i_size ) )
1579     {
1580         msg_Warn( p_this, "This doesn't look like a valid plugins cache" );
1581         fclose( file );
1582         return;
1583     }
1584
1585     p_this->p_libvlc->p_module_bank->i_loaded_cache = 0;
1586     fread( &i_cache, sizeof(char), sizeof(i_cache), file );
1587     pp_cache = p_this->p_libvlc->p_module_bank->pp_loaded_cache =
1588         malloc( i_cache * sizeof(void *) );
1589
1590 #define LOAD_IMMEDIATE(a) \
1591     if( fread( &a, sizeof(char), sizeof(a), file ) != sizeof(a) ) goto error
1592 #define LOAD_STRING(a) \
1593     { if( fread( &i_size, sizeof(char), sizeof(i_size), file ) \
1594           != sizeof(i_size) ) goto error; \
1595       if( i_size ) { \
1596           a = malloc( i_size ); \
1597           if( fread( a, sizeof(char), i_size, file ) != (size_t)i_size ) \
1598               goto error; \
1599       } else a = 0; \
1600     } while(0)
1601
1602
1603     for( i = 0; i < i_cache; i++ )
1604     {
1605         int16_t i_size;
1606         int i_submodules;
1607
1608         pp_cache[i] = malloc( sizeof(module_cache_t) );
1609         p_this->p_libvlc->p_module_bank->i_loaded_cache++;
1610
1611         /* Save common info */
1612         LOAD_STRING( pp_cache[i]->psz_file );
1613         LOAD_IMMEDIATE( pp_cache[i]->i_time );
1614         LOAD_IMMEDIATE( pp_cache[i]->i_size );
1615         LOAD_IMMEDIATE( pp_cache[i]->b_junk );
1616
1617         if( pp_cache[i]->b_junk ) continue;
1618
1619         pp_cache[i]->p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
1620
1621         /* Save additional infos */
1622         LOAD_STRING( pp_cache[i]->p_module->psz_object_name );
1623         LOAD_STRING( pp_cache[i]->p_module->psz_shortname );
1624         LOAD_STRING( pp_cache[i]->p_module->psz_longname );
1625         LOAD_STRING( pp_cache[i]->p_module->psz_program );
1626         for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
1627         {
1628             LOAD_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
1629         }
1630         LOAD_STRING( pp_cache[i]->p_module->psz_capability );
1631         LOAD_IMMEDIATE( pp_cache[i]->p_module->i_score );
1632         LOAD_IMMEDIATE( pp_cache[i]->p_module->i_cpu );
1633         LOAD_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
1634         LOAD_IMMEDIATE( pp_cache[i]->p_module->b_reentrant );
1635         LOAD_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
1636
1637         /* Config stuff */
1638         if( CacheLoadConfig( pp_cache[i]->p_module, file ) != VLC_SUCCESS )
1639             goto error;
1640
1641         LOAD_STRING( pp_cache[i]->p_module->psz_filename );
1642
1643         LOAD_IMMEDIATE( i_submodules );
1644
1645         while( i_submodules-- )
1646         {
1647             module_t *p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE);
1648             vlc_object_attach( p_module, pp_cache[i]->p_module );
1649             p_module->b_submodule = VLC_TRUE; 
1650
1651             LOAD_STRING( p_module->psz_object_name );
1652             LOAD_STRING( p_module->psz_shortname );
1653             LOAD_STRING( p_module->psz_longname );
1654             LOAD_STRING( p_module->psz_program );
1655             for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
1656             {
1657                 LOAD_STRING( p_module->pp_shortcuts[j] ); // FIX
1658             }
1659             LOAD_STRING( p_module->psz_capability );
1660             LOAD_IMMEDIATE( p_module->i_score );
1661             LOAD_IMMEDIATE( p_module->i_cpu );
1662             LOAD_IMMEDIATE( p_module->b_unloadable );
1663             LOAD_IMMEDIATE( p_module->b_reentrant );
1664         }
1665     }
1666
1667     fclose( file );
1668     return;
1669
1670  error:
1671
1672     msg_Warn( p_this, "plugins cache not loaded (corrupted)" );
1673
1674     /* TODO: cleanup */
1675     p_this->p_libvlc->p_module_bank->i_loaded_cache = 0;
1676
1677     fclose( file );
1678     return;
1679 }
1680
1681 int CacheLoadConfig( module_t *p_module, FILE *file )
1682 {
1683     int i, j, i_lines;
1684     int16_t i_size;
1685
1686     /* Calculate the structure length */
1687     LOAD_IMMEDIATE( p_module->i_config_items );
1688     LOAD_IMMEDIATE( p_module->i_bool_items );
1689
1690     LOAD_IMMEDIATE( i_lines );
1691
1692     /* Allocate memory */
1693     p_module->p_config =
1694         (module_config_t *)malloc( sizeof(module_config_t) * (i_lines + 1));
1695     if( p_module->p_config == NULL )
1696     {
1697         msg_Err( p_module, "config error: can't duplicate p_config" );
1698         return VLC_ENOMEM;
1699     }
1700
1701     /* Do the duplication job */
1702     for( i = 0; i < i_lines ; i++ )
1703     {
1704         LOAD_IMMEDIATE( p_module->p_config[i] );
1705
1706         LOAD_STRING( p_module->p_config[i].psz_type );
1707         LOAD_STRING( p_module->p_config[i].psz_name );
1708         LOAD_STRING( p_module->p_config[i].psz_text );
1709         LOAD_STRING( p_module->p_config[i].psz_longtext );
1710         LOAD_STRING( p_module->p_config[i].psz_value_orig );
1711
1712         p_module->p_config[i].psz_value =
1713             p_module->p_config[i].psz_value_orig ?
1714                 strdup( p_module->p_config[i].psz_value_orig ) : 0;
1715         p_module->p_config[i].i_value = p_module->p_config[i].i_value_orig;
1716         p_module->p_config[i].f_value = p_module->p_config[i].f_value_orig;
1717
1718         p_module->p_config[i].p_lock = &p_module->object_lock;
1719
1720         if( p_module->p_config[i].i_list )
1721         {
1722             if( p_module->p_config[i].ppsz_list )
1723             {
1724                 p_module->p_config[i].ppsz_list =
1725                     malloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
1726                 if( p_module->p_config[i].ppsz_list )
1727                 {
1728                     for( j = 0; j < p_module->p_config[i].i_list; j++ )
1729                         LOAD_STRING( p_module->p_config[i].ppsz_list[j] );
1730                     p_module->p_config[i].ppsz_list[j] = NULL;
1731                 }
1732             }
1733             if( p_module->p_config[i].ppsz_list_text )
1734             {
1735                 p_module->p_config[i].ppsz_list_text =
1736                     malloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
1737                 if( p_module->p_config[i].ppsz_list_text )
1738                 {
1739                   for( j = 0; j < p_module->p_config[i].i_list; j++ )
1740                       LOAD_STRING( p_module->p_config[i].ppsz_list_text[j] );
1741                   p_module->p_config[i].ppsz_list_text[j] = NULL;
1742                 }
1743             }
1744             if( p_module->p_config[i].pi_list )
1745             {
1746                 p_module->p_config[i].pi_list =
1747                     malloc( (p_module->p_config[i].i_list + 1) * sizeof(int) );
1748                 if( p_module->p_config[i].pi_list )
1749                 {
1750                     for( j = 0; j < p_module->p_config[i].i_list; j++ )
1751                         LOAD_IMMEDIATE( p_module->p_config[i].pi_list[j] );
1752                 }
1753             }
1754         }
1755
1756         if( p_module->p_config[i].i_action )
1757         {
1758             p_module->p_config[i].ppf_action =
1759                 malloc( p_module->p_config[i].i_action * sizeof(void *) );
1760             p_module->p_config[i].ppsz_action_text =
1761                 malloc( p_module->p_config[i].i_action * sizeof(char *) );
1762
1763             for( j = 0; j < p_module->p_config[i].i_action; j++ )
1764             {
1765                 p_module->p_config[i].ppf_action[j] = 0;
1766                 LOAD_STRING( p_module->p_config[i].ppsz_action_text[j] );
1767             }
1768         }
1769
1770         p_module->p_config[i].pf_callback = 0;
1771     }
1772
1773     p_module->p_config[i].i_type = CONFIG_HINT_END;
1774
1775     return VLC_SUCCESS;
1776
1777  error:
1778
1779     return VLC_EGENERIC;
1780 }
1781
1782 /*****************************************************************************
1783  * SavePluginsCache: saves the plugins cache to a file
1784  *****************************************************************************/
1785 static void CacheSave( vlc_object_t *p_this )
1786 {
1787     char *psz_filename, *psz_homedir;
1788     FILE *file;
1789     int i, j, i_cache;
1790     module_cache_t **pp_cache;
1791     int32_t i_file_size = 0;
1792
1793     psz_homedir = p_this->p_vlc->psz_homedir;
1794     if( !psz_homedir )
1795     {
1796         msg_Err( p_this, "psz_homedir is null" );
1797         return;
1798     }
1799     psz_filename =
1800        (char *)malloc( sizeof("/" CONFIG_DIR "/" PLUGINSCACHE_FILE) +
1801                        strlen(psz_homedir) );
1802
1803     if( psz_filename )
1804         sprintf( psz_filename, "%s/" CONFIG_DIR, psz_homedir );
1805
1806     if( !psz_filename )
1807     {
1808         msg_Err( p_this, "out of memory" );
1809         return;
1810     }
1811
1812     config_CreateDir( p_this, psz_filename );
1813
1814     strcat( psz_filename, "/" PLUGINSCACHE_FILE );
1815
1816     msg_Dbg( p_this, "saving plugins cache file %s", psz_filename );
1817
1818     file = fopen( psz_filename, "w" );
1819     if( !file )
1820     {
1821         msg_Warn( p_this, "could not open plugins cache file %s for writing",
1822                   psz_filename );
1823         free( psz_filename );
1824         return;
1825     }
1826     free( psz_filename );
1827
1828     /* Empty space for file size */
1829     fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
1830
1831     /* Contains version number */
1832     fprintf( file, PLUGINSCACHE_FILE COPYRIGHT_MESSAGE );
1833
1834     i_cache = p_this->p_libvlc->p_module_bank->i_cache;
1835     pp_cache = p_this->p_libvlc->p_module_bank->pp_cache;
1836
1837     fwrite( &i_cache, sizeof(char), sizeof(i_cache), file );
1838
1839 #define SAVE_IMMEDIATE(a) \
1840     fwrite( &a, sizeof(char), sizeof(a), file )
1841 #define SAVE_STRING(a) \
1842     { i_size = a ? strlen( a ) + 1 : 0; \
1843       fwrite( &i_size, sizeof(char), sizeof(i_size), file ); \
1844       if( a ) fwrite( a, sizeof(char), i_size, file ); \
1845     } while(0)
1846
1847     for( i = 0; i < i_cache; i++ )
1848     {
1849         int16_t i_size;
1850         int32_t i_submodule;
1851
1852         /* Save common info */
1853         SAVE_STRING( pp_cache[i]->psz_file );
1854         SAVE_IMMEDIATE( pp_cache[i]->i_time );
1855         SAVE_IMMEDIATE( pp_cache[i]->i_size );
1856         SAVE_IMMEDIATE( pp_cache[i]->b_junk );
1857
1858         if( pp_cache[i]->b_junk ) continue;
1859
1860         /* Save additional infos */
1861         SAVE_STRING( pp_cache[i]->p_module->psz_object_name );
1862         SAVE_STRING( pp_cache[i]->p_module->psz_shortname );
1863         SAVE_STRING( pp_cache[i]->p_module->psz_longname );
1864         SAVE_STRING( pp_cache[i]->p_module->psz_program );
1865         for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
1866         {
1867             SAVE_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
1868         }
1869         SAVE_STRING( pp_cache[i]->p_module->psz_capability );
1870         SAVE_IMMEDIATE( pp_cache[i]->p_module->i_score );
1871         SAVE_IMMEDIATE( pp_cache[i]->p_module->i_cpu );
1872         SAVE_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
1873         SAVE_IMMEDIATE( pp_cache[i]->p_module->b_reentrant );
1874         SAVE_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
1875
1876         /* Config stuff */
1877         CacheSaveConfig( pp_cache[i]->p_module, file );
1878
1879         SAVE_STRING( pp_cache[i]->p_module->psz_filename );
1880
1881         i_submodule = pp_cache[i]->p_module->i_children;
1882         SAVE_IMMEDIATE( i_submodule );
1883         for( i_submodule = 0; i_submodule < pp_cache[i]->p_module->i_children;
1884              i_submodule++ )
1885         {
1886             module_t *p_module =
1887                 (module_t *)pp_cache[i]->p_module->pp_children[i_submodule];
1888
1889             SAVE_STRING( p_module->psz_object_name );
1890             SAVE_STRING( p_module->psz_shortname );
1891             SAVE_STRING( p_module->psz_longname );
1892             SAVE_STRING( p_module->psz_program );
1893             for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
1894             {
1895                 SAVE_STRING( p_module->pp_shortcuts[j] ); // FIX
1896             }
1897             SAVE_STRING( p_module->psz_capability );
1898             SAVE_IMMEDIATE( p_module->i_score );
1899             SAVE_IMMEDIATE( p_module->i_cpu );
1900             SAVE_IMMEDIATE( p_module->b_unloadable );
1901             SAVE_IMMEDIATE( p_module->b_reentrant );
1902         }
1903     }
1904
1905     /* Fill-up file size */
1906     i_file_size = ftell( file );
1907     fseek( file, 0, SEEK_SET );
1908     fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
1909
1910     fclose( file );
1911
1912     return;
1913 }
1914
1915 void CacheSaveConfig( module_t *p_module, FILE *file )
1916 {
1917     int i, j, i_lines = 0;
1918     module_config_t *p_item;
1919     int16_t i_size;
1920
1921     SAVE_IMMEDIATE( p_module->i_config_items );
1922     SAVE_IMMEDIATE( p_module->i_bool_items );
1923
1924     for( p_item = p_module->p_config; p_item->i_type != CONFIG_HINT_END;
1925          p_item++ ) i_lines++;
1926
1927     SAVE_IMMEDIATE( i_lines );
1928
1929     for( i = 0; i < i_lines ; i++ )
1930     {
1931         SAVE_IMMEDIATE( p_module->p_config[i] );
1932
1933         SAVE_STRING( p_module->p_config[i].psz_type );
1934         SAVE_STRING( p_module->p_config[i].psz_name );
1935         SAVE_STRING( p_module->p_config[i].psz_text );
1936         SAVE_STRING( p_module->p_config[i].psz_longtext );
1937         SAVE_STRING( p_module->p_config[i].psz_value_orig );
1938
1939         if( p_module->p_config[i].i_list )
1940         {
1941             if( p_module->p_config[i].ppsz_list )
1942             {
1943                 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1944                     SAVE_STRING( p_module->p_config[i].ppsz_list[j] );
1945             }
1946
1947             if( p_module->p_config[i].ppsz_list_text )
1948             {
1949                 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1950                     SAVE_STRING( p_module->p_config[i].ppsz_list_text[j] );
1951             }
1952             if( p_module->p_config[i].pi_list )
1953             {
1954                 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1955                     SAVE_IMMEDIATE( p_module->p_config[i].pi_list[j] );
1956             }
1957         }
1958
1959         for( j = 0; j < p_module->p_config[i].i_action; j++ )
1960             SAVE_STRING( p_module->p_config[i].ppsz_action_text[j] );
1961     }
1962 }
1963
1964 /*****************************************************************************
1965  * CacheMerge: Merge a cache module descriptor with a full module descriptor.
1966  *****************************************************************************/
1967 static void CacheMerge( vlc_object_t *p_this, module_t *p_cache,
1968                         module_t *p_module )
1969 {
1970     int i_submodule;
1971
1972     p_cache->pf_activate = p_module->pf_activate;
1973     p_cache->pf_deactivate = p_module->pf_deactivate;
1974     p_cache->p_symbols = p_module->p_symbols;
1975     p_cache->handle = p_module->handle;
1976
1977     for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1978     {
1979         module_t *p_child = (module_t*)p_module->pp_children[i_submodule];
1980         module_t *p_cchild = (module_t*)p_cache->pp_children[i_submodule];
1981         p_cchild->pf_activate = p_child->pf_activate;
1982         p_cchild->pf_deactivate = p_child->pf_deactivate;
1983         p_cchild->p_symbols = p_child->p_symbols;
1984     }
1985
1986     p_cache->b_loaded = VLC_TRUE;
1987     p_module->b_loaded = VLC_FALSE;
1988 }
1989
1990 /*****************************************************************************
1991  * FindPluginCache: finds the cache entry corresponding to a file
1992  *****************************************************************************/
1993 static module_cache_t *CacheFind( vlc_object_t *p_this, char *psz_file,
1994                                   int64_t i_time, int64_t i_size )
1995 {
1996     module_cache_t **pp_cache;
1997     int i_cache, i;
1998
1999     pp_cache = p_this->p_libvlc->p_module_bank->pp_loaded_cache;
2000     i_cache = p_this->p_libvlc->p_module_bank->i_loaded_cache;
2001
2002     for( i = 0; i < i_cache; i++ )
2003     {
2004         if( !strcmp( pp_cache[i]->psz_file, psz_file ) &&
2005             pp_cache[i]->i_time == i_time &&
2006             pp_cache[i]->i_size == i_size ) return pp_cache[i];
2007     }
2008
2009     return NULL;
2010 }
2011
2012 #endif /* HAVE_DYNAMIC_PLUGINS */