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