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