]> git.sesse.net Git - vlc/blob - src/misc/modules.c
3ad7db8c057175e31c2aeb4741a996982d0bef6e
[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 *, vlc_bool_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, p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->b_used );
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, VLC_TRUE ) )
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, VLC_TRUE );
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  * module_Exists: tell if a module exists.
745  *****************************************************************************
746  * This function is a boolean function that tells if a module exist or not.
747  *****************************************************************************/
748
749 vlc_bool_t __module_Exists(  vlc_object_t *p_this, const char * psz_name )
750 {
751     vlc_list_t *p_list;
752     int i;
753     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
754     for( i = 0 ; i < p_list->i_count; i++)
755     {
756         if (!strcmp(
757               ((module_t *) p_list->p_values[i].p_object)->psz_shortname ,
758               psz_name ) )
759         {
760             /* We can release the list, and return yes */
761             vlc_list_release( p_list ); return VLC_TRUE;
762         }
763     }
764     vlc_list_release( p_list ); return VLC_FALSE;
765 }
766
767
768 /*****************************************************************************
769  * Following functions are local.
770  *****************************************************************************/
771
772 /*****************************************************************************
773  * AllocateAllPlugins: load all plugin modules we can find.
774  *****************************************************************************/
775 #ifdef HAVE_DYNAMIC_PLUGINS
776 static void AllocateAllPlugins( vlc_object_t *p_this )
777 {
778     /* Yes, there are two NULLs because we replace one with "plugin-path". */
779 #if defined( WIN32 ) || defined( UNDER_CE )
780     const char *path[] = { "modules", "", "plugins", NULL, NULL };
781 #else
782     const char *path[] = { "modules", PLUGIN_PATH, "plugins", NULL, NULL };
783 #endif
784
785     const char *const *ppsz_path;
786
787     /* If the user provided a plugin path, we add it to the list */
788     char *userpath = config_GetPsz( p_this, "plugin-path" );
789     path[sizeof(path)/sizeof(path[0]) - 2] = userpath;
790
791     for (ppsz_path = path; *ppsz_path != NULL; ppsz_path++)
792     {
793         char *psz_fullpath;
794
795         if (!**ppsz_path) continue;
796
797 #if defined( SYS_BEOS ) || defined( __APPLE__ ) || defined( WIN32 )
798
799         /* Handle relative as well as absolute paths */
800 #ifdef WIN32
801         if( (*ppsz_path)[0] != '\\' && (*ppsz_path)[0] != '/' &&
802             (*ppsz_path)[1] != ':' )
803 #else
804         if( (*ppsz_path)[0] != '/' )
805 #endif
806         {
807             if( 0>= asprintf(&psz_fullpath, "%s"DIR_SEP"%s",
808                           p_this->p_libvlc_global->psz_vlcpath, *ppsz_path) )
809                 psz_fullpath = NULL;
810         }
811         else
812 #endif
813             psz_fullpath = strdup( *ppsz_path );
814
815         if( psz_fullpath == NULL )
816             continue;
817
818         msg_Dbg( p_this, "recursively browsing `%s'", psz_fullpath );
819
820         /* Don't go deeper than 5 subdirectories */
821         AllocatePluginDir( p_this, psz_fullpath, 5 );
822
823         free( psz_fullpath );
824     }
825
826     /* Free plugin-path */
827     if( userpath != NULL )
828         free( userpath );
829 }
830
831 /*****************************************************************************
832  * AllocatePluginDir: recursively parse a directory to look for plugins
833  *****************************************************************************/
834 static void AllocatePluginDir( vlc_object_t *p_this, const char *psz_dir,
835                                int i_maxdepth )
836 {
837 /* FIXME: Needs to be ported to wide char on ALL Windows builds */
838 #ifdef WIN32
839 # undef opendir
840 # undef closedir
841 # undef readdir
842 #endif
843 #if defined( UNDER_CE ) || defined( _MSC_VER )
844 #ifdef UNDER_CE
845     wchar_t psz_wpath[MAX_PATH + 256];
846     wchar_t psz_wdir[MAX_PATH];
847 #endif
848     char psz_path[MAX_PATH + 256];
849     WIN32_FIND_DATA finddata;
850     HANDLE handle;
851     int rc;
852 #else
853     int    i_dirlen;
854     DIR *  dir;
855     struct dirent * file;
856 #endif
857     char * psz_file;
858
859     if( p_this->p_libvlc->b_die || i_maxdepth < 0 )
860     {
861         return;
862     }
863
864 #if defined( UNDER_CE ) || defined( _MSC_VER )
865 #ifdef UNDER_CE
866     MultiByteToWideChar( CP_ACP, 0, psz_dir, -1, psz_wdir, MAX_PATH );
867
868     rc = GetFileAttributes( psz_wdir );
869     if( rc<0 || !(rc&FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
870
871     /* Parse all files in the directory */
872     swprintf( psz_wpath, L"%ls\\*", psz_wdir );
873 #else
874     rc = GetFileAttributes( psz_dir );
875     if( rc<0 || !(rc&FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
876 #endif
877
878     /* Parse all files in the directory */
879     sprintf( psz_path, "%s\\*", psz_dir );
880
881 #ifdef UNDER_CE
882     handle = FindFirstFile( psz_wpath, &finddata );
883 #else
884     handle = FindFirstFile( psz_path, &finddata );
885 #endif
886     if( handle == INVALID_HANDLE_VALUE )
887     {
888         /* Empty directory */
889         return;
890     }
891
892     /* Parse the directory and try to load all files it contains. */
893     do
894     {
895 #ifdef UNDER_CE
896         unsigned int i_len = wcslen( finddata.cFileName );
897         swprintf( psz_wpath, L"%ls\\%ls", psz_wdir, finddata.cFileName );
898         sprintf( psz_path, "%s\\%ls", psz_dir, finddata.cFileName );
899 #else
900         unsigned int i_len = strlen( finddata.cFileName );
901         sprintf( psz_path, "%s\\%s", psz_dir, finddata.cFileName );
902 #endif
903
904         /* Skip ".", ".." */
905         if( !*finddata.cFileName || !strcmp( finddata.cFileName, "." )
906          || !strcmp( finddata.cFileName, ".." ) )
907         {
908             if( !FindNextFile( handle, &finddata ) ) break;
909             continue;
910         }
911
912 #ifdef UNDER_CE
913         if( GetFileAttributes( psz_wpath ) & FILE_ATTRIBUTE_DIRECTORY )
914 #else
915         if( GetFileAttributes( psz_path ) & FILE_ATTRIBUTE_DIRECTORY )
916 #endif
917         {
918             AllocatePluginDir( p_this, psz_path, i_maxdepth - 1 );
919         }
920         else if( i_len > strlen( LIBEXT )
921                   /* We only load files ending with LIBEXT */
922                   && !strncasecmp( psz_path + strlen( psz_path)
923                                    - strlen( LIBEXT ),
924                                    LIBEXT, strlen( LIBEXT ) ) )
925         {
926             WIN32_FILE_ATTRIBUTE_DATA attrbuf;
927             int64_t i_time = 0, i_size = 0;
928
929 #ifdef UNDER_CE
930             if( GetFileAttributesEx( psz_wpath, GetFileExInfoStandard,
931                                      &attrbuf ) )
932 #else
933             if( GetFileAttributesEx( psz_path, GetFileExInfoStandard,
934                                      &attrbuf ) )
935 #endif
936             {
937                 i_time = attrbuf.ftLastWriteTime.dwHighDateTime;
938                 i_time <<= 32;
939                 i_time |= attrbuf.ftLastWriteTime.dwLowDateTime;
940                 i_size = attrbuf.nFileSizeHigh;
941                 i_size <<= 32;
942                 i_size |= attrbuf.nFileSizeLow;
943             }
944             psz_file = psz_path;
945
946             AllocatePluginFile( p_this, psz_file, i_time, i_size );
947         }
948     }
949     while( !p_this->p_libvlc->b_die && FindNextFile( handle, &finddata ) );
950
951     /* Close the directory */
952     FindClose( handle );
953
954 #else
955     dir = opendir( psz_dir );
956     if( !dir )
957     {
958         return;
959     }
960
961     i_dirlen = strlen( psz_dir );
962
963     /* Parse the directory and try to load all files it contains. */
964     while( !p_this->p_libvlc->b_die && (file = readdir( dir )) )
965     {
966         struct stat statbuf;
967         unsigned int i_len;
968         int i_stat;
969
970         /* Skip ".", ".." */
971         if( !*file->d_name || !strcmp( file->d_name, "." )
972          || !strcmp( file->d_name, ".." ) )
973         {
974             continue;
975         }
976
977         i_len = strlen( file->d_name );
978         psz_file = malloc( i_dirlen + 1 + i_len + 1 );
979 #ifdef WIN32
980         sprintf( psz_file, "%s\\%s", psz_dir, file->d_name );
981 #else
982         sprintf( psz_file, "%s/%s", psz_dir, file->d_name );
983 #endif
984
985         i_stat = stat( psz_file, &statbuf );
986         if( !i_stat && statbuf.st_mode & S_IFDIR )
987         {
988             AllocatePluginDir( p_this, psz_file, i_maxdepth - 1 );
989         }
990         else if( i_len > strlen( LIBEXT )
991                   /* We only load files ending with LIBEXT */
992                   && !strncasecmp( file->d_name + i_len - strlen( LIBEXT ),
993                                    LIBEXT, strlen( LIBEXT ) ) )
994         {
995             int64_t i_time = 0, i_size = 0;
996
997             if( !i_stat )
998             {
999                 i_time = statbuf.st_mtime;
1000                 i_size = statbuf.st_size;
1001             }
1002
1003             AllocatePluginFile( p_this, psz_file, i_time, i_size );
1004         }
1005
1006         free( psz_file );
1007     }
1008
1009     /* Close the directory */
1010     closedir( dir );
1011
1012 #endif
1013 }
1014
1015 /*****************************************************************************
1016  * AllocatePluginFile: load a module into memory and initialize it.
1017  *****************************************************************************
1018  * This function loads a dynamically loadable module and allocates a structure
1019  * for its information data. The module can then be handled by module_Need
1020  * and module_Unneed. It can be removed by DeleteModule.
1021  *****************************************************************************/
1022 static int AllocatePluginFile( vlc_object_t * p_this, char * psz_file,
1023                                int64_t i_file_time, int64_t i_file_size )
1024 {
1025     module_t * p_module = NULL;
1026     module_cache_t *p_cache_entry = NULL;
1027
1028     /*
1029      * Check our plugins cache first then load plugin if needed
1030      */
1031     p_cache_entry =
1032         CacheFind( p_this, psz_file, i_file_time, i_file_size );
1033
1034     if( !p_cache_entry )
1035     {
1036         p_module = AllocatePlugin( p_this, psz_file );
1037     }
1038     else
1039     {
1040         /* If junk dll, don't try to load it */
1041         if( p_cache_entry->b_junk )
1042         {
1043             p_module = NULL;
1044         }
1045         else
1046         {
1047             module_config_t *p_item = NULL, *p_end = NULL;
1048
1049             p_module = p_cache_entry->p_module;
1050             p_module->b_loaded = VLC_FALSE;
1051
1052             /* For now we force loading if the module's config contains
1053              * callbacks or actions.
1054              * Could be optimized by adding an API call.*/
1055             for( p_item = p_module->p_config, p_end = p_item + p_module->confsize;
1056                  p_item < p_end; p_item++ )
1057             {
1058                 if( p_item->pf_callback || p_item->i_action )
1059                 {
1060                     p_module = AllocatePlugin( p_this, psz_file );
1061                     break;
1062                 }
1063             }
1064             if( p_module == p_cache_entry->p_module )
1065                 p_cache_entry->b_used = VLC_TRUE;
1066         }
1067     }
1068
1069     if( p_module )
1070     {
1071         /* Everything worked fine !
1072          * The module is ready to be added to the list. */
1073         p_module->b_builtin = VLC_FALSE;
1074
1075         /* msg_Dbg( p_this, "plugin \"%s\", %s",
1076                     p_module->psz_object_name, p_module->psz_longname ); */
1077
1078         vlc_object_attach( p_module, p_this->p_libvlc_global->p_module_bank );
1079
1080         if( !p_this->p_libvlc_global->p_module_bank->b_cache )
1081             return 0;
1082
1083         /* Add entry to cache */
1084 #define p_bank p_this->p_libvlc_global->p_module_bank
1085         p_bank->pp_cache =
1086             realloc( p_bank->pp_cache, (p_bank->i_cache + 1) * sizeof(void *) );
1087         p_bank->pp_cache[p_bank->i_cache] = malloc( sizeof(module_cache_t) );
1088         if( !p_bank->pp_cache[p_bank->i_cache] )
1089             return -1;
1090         p_bank->pp_cache[p_bank->i_cache]->psz_file = strdup( psz_file );
1091         p_bank->pp_cache[p_bank->i_cache]->i_time = i_file_time;
1092         p_bank->pp_cache[p_bank->i_cache]->i_size = i_file_size;
1093         p_bank->pp_cache[p_bank->i_cache]->b_junk = p_module ? 0 : 1;
1094         p_bank->pp_cache[p_bank->i_cache]->b_used = VLC_TRUE;
1095         p_bank->pp_cache[p_bank->i_cache]->p_module = p_module;
1096         p_bank->i_cache++;
1097     }
1098
1099     return p_module ? 0 : -1;
1100 }
1101
1102 /*****************************************************************************
1103  * AllocatePlugin: load a module into memory and initialize it.
1104  *****************************************************************************
1105  * This function loads a dynamically loadable module and allocates a structure
1106  * for its information data. The module can then be handled by module_Need
1107  * and module_Unneed. It can be removed by DeleteModule.
1108  *****************************************************************************/
1109 static module_t * AllocatePlugin( vlc_object_t * p_this, char * psz_file )
1110 {
1111     module_t * p_module = NULL;
1112     module_handle_t handle;
1113
1114     if( LoadModule( p_this, psz_file, &handle ) )
1115         return NULL;
1116
1117     /* Now that we have successfully loaded the module, we can
1118      * allocate a structure for it */
1119     p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
1120     if( p_module == NULL )
1121     {
1122         msg_Err( p_this, "out of memory" );
1123         CloseModule( handle );
1124         return NULL;
1125     }
1126
1127     /* We need to fill these since they may be needed by CallEntry() */
1128     p_module->psz_filename = psz_file;
1129     p_module->handle = handle;
1130 #ifndef HAVE_SHARED_LIBVLC
1131     p_module->p_symbols = &p_this->p_libvlc_global->p_module_bank->symbols;
1132 #endif
1133     p_module->b_loaded = VLC_TRUE;
1134
1135     /* Initialize the module: fill p_module, default config */
1136     if( CallEntry( p_module ) != 0 )
1137     {
1138         /* We couldn't call module_init() */
1139         vlc_object_destroy( p_module );
1140         CloseModule( handle );
1141         return NULL;
1142     }
1143
1144     DupModule( p_module );
1145     p_module->psz_filename = strdup( p_module->psz_filename );
1146
1147     /* Everything worked fine ! The module is ready to be added to the list. */
1148     p_module->b_builtin = VLC_FALSE;
1149
1150     return p_module;
1151 }
1152
1153 /*****************************************************************************
1154  * DupModule: make a plugin module standalone.
1155  *****************************************************************************
1156  * This function duplicates all strings in the module, so that the dynamic
1157  * object can be unloaded. It acts recursively on submodules.
1158  *****************************************************************************/
1159 static void DupModule( module_t *p_module )
1160 {
1161     const char **pp_shortcut;
1162     int i_submodule;
1163
1164     for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1165     {
1166         *pp_shortcut = strdup( *pp_shortcut );
1167     }
1168
1169     /* We strdup() these entries so that they are still valid when the
1170      * module is unloaded. */
1171     p_module->psz_object_name = strdup( p_module->psz_object_name );
1172     p_module->psz_capability = strdup( p_module->psz_capability );
1173     p_module->psz_shortname = p_module->psz_shortname ?
1174                                  strdup( p_module->psz_shortname ) : NULL;
1175     p_module->psz_longname = strdup( p_module->psz_longname );
1176
1177     if( p_module->psz_program != NULL )
1178     {
1179         p_module->psz_program = strdup( p_module->psz_program );
1180     }
1181
1182     for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1183     {
1184         DupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1185     }
1186 }
1187
1188 /*****************************************************************************
1189  * UndupModule: free a duplicated module.
1190  *****************************************************************************
1191  * This function frees the allocations done in DupModule().
1192  *****************************************************************************/
1193 static void UndupModule( module_t *p_module )
1194 {
1195     const char **pp_shortcut;
1196     int i_submodule;
1197
1198     for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1199     {
1200         UndupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1201     }
1202
1203     for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1204     {
1205         free( (void *)*pp_shortcut );
1206     }
1207
1208     free( (void *)p_module->psz_object_name );
1209     free( (void *)p_module->psz_capability );
1210     if( p_module->psz_shortname ) free( (void *)p_module->psz_shortname );
1211     free( (void *)p_module->psz_longname );
1212
1213     if( p_module->psz_program != NULL )
1214     {
1215         free( (void *)p_module->psz_program );
1216     }
1217 }
1218
1219 #endif /* HAVE_DYNAMIC_PLUGINS */
1220
1221 /*****************************************************************************
1222  * AllocateBuiltinModule: initialize a builtin module.
1223  *****************************************************************************
1224  * This function registers a builtin module and allocates a structure
1225  * for its information data. The module can then be handled by module_Need
1226  * and module_Unneed. It can be removed by DeleteModule.
1227  *****************************************************************************/
1228 static int AllocateBuiltinModule( vlc_object_t * p_this,
1229                                   int ( *pf_entry ) ( module_t * ) )
1230 {
1231     module_t * p_module;
1232
1233     /* Now that we have successfully loaded the module, we can
1234      * allocate a structure for it */
1235     p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
1236     if( p_module == NULL )
1237     {
1238         msg_Err( p_this, "out of memory" );
1239         return -1;
1240     }
1241
1242     /* Initialize the module : fill p_module->psz_object_name, etc. */
1243     if( pf_entry( p_module ) != 0 )
1244     {
1245         /* With a well-written module we shouldn't have to print an
1246          * additional error message here, but just make sure. */
1247         msg_Err( p_this, "failed calling entry point in builtin module" );
1248         vlc_object_destroy( p_module );
1249         return -1;
1250     }
1251
1252     /* Everything worked fine ! The module is ready to be added to the list. */
1253     p_module->b_builtin = VLC_TRUE;
1254
1255     /* msg_Dbg( p_this, "builtin \"%s\", %s",
1256                 p_module->psz_object_name, p_module->psz_longname ); */
1257
1258     vlc_object_attach( p_module, p_this->p_libvlc_global->p_module_bank );
1259
1260     return 0;
1261 }
1262
1263 /*****************************************************************************
1264  * DeleteModule: delete a module and its structure.
1265  *****************************************************************************
1266  * This function can only be called if the module isn't being used.
1267  *****************************************************************************/
1268 static int DeleteModule( module_t * p_module, vlc_bool_t b_detach )
1269 {
1270     if( !p_module ) return VLC_EGENERIC;
1271     if( b_detach )
1272         vlc_object_detach( p_module );
1273
1274     /* We free the structures that we strdup()ed in Allocate*Module(). */
1275 #ifdef HAVE_DYNAMIC_PLUGINS
1276     if( !p_module->b_builtin )
1277     {
1278         if( p_module->b_loaded && p_module->b_unloadable )
1279         {
1280             CloseModule( p_module->handle );
1281         }
1282         UndupModule( p_module );
1283         free( p_module->psz_filename );
1284     }
1285 #endif
1286
1287     /* Free and detach the object's children */
1288     while( p_module->i_children )
1289     {
1290         vlc_object_t *p_this = p_module->pp_children[0];
1291         vlc_object_detach( p_this );
1292         vlc_object_destroy( p_this );
1293     }
1294
1295     config_Free( p_module );
1296     vlc_object_destroy( p_module );
1297     p_module = NULL;
1298     return 0;
1299 }
1300
1301 #ifdef HAVE_DYNAMIC_PLUGINS
1302 /*****************************************************************************
1303  * CallEntry: call an entry point.
1304  *****************************************************************************
1305  * This function calls a symbol given its name and a module structure. The
1306  * symbol MUST refer to a function returning int and taking a module_t* as
1307  * an argument.
1308  *****************************************************************************/
1309 static int CallEntry( module_t * p_module )
1310 {
1311     static const char * const psz_name = "vlc_entry" MODULE_SUFFIX;
1312     int (* pf_symbol) ( module_t * p_module );
1313
1314     /* Try to resolve the symbol */
1315     pf_symbol = (int (*)(module_t *)) GetSymbol( p_module->handle, psz_name );
1316
1317     if( pf_symbol == NULL )
1318     {
1319 #if defined(HAVE_DL_DYLD) || defined(HAVE_DL_BEOS)
1320         msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s'",
1321                             psz_name, p_module->psz_filename );
1322 #elif defined(HAVE_DL_WINDOWS)
1323         char *psz_error = GetWindowsError();
1324         msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1325                             psz_name, p_module->psz_filename, psz_error );
1326         free( psz_error );
1327 #elif defined(HAVE_DL_DLOPEN)
1328         msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1329                             psz_name, p_module->psz_filename, dlerror() );
1330 #elif defined(HAVE_DL_SHL_LOAD)
1331         msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1332                             psz_name, p_module->psz_filename, strerror(errno) );
1333 #else
1334 #   error "Something is wrong in modules.c"
1335 #endif
1336         return -1;
1337     }
1338
1339     /* We can now try to call the symbol */
1340     if( pf_symbol( p_module ) != 0 )
1341     {
1342         /* With a well-written module we shouldn't have to print an
1343          * additional error message here, but just make sure. */
1344         msg_Err( p_module, "Failed to call symbol \"%s\" in file `%s'",
1345                            psz_name, p_module->psz_filename );
1346         return -1;
1347     }
1348
1349     /* Everything worked fine, we can return */
1350     return 0;
1351 }
1352
1353 /*****************************************************************************
1354  * LoadModule: loads a dynamic library
1355  *****************************************************************************
1356  * This function loads a dynamically linked library using a system dependant
1357  * method. Will return 0 on success as well as the module handle.
1358  *****************************************************************************/
1359 static int LoadModule( vlc_object_t *p_this, char *psz_file,
1360                        module_handle_t *p_handle )
1361 {
1362     module_handle_t handle;
1363
1364 #if defined(HAVE_DL_DYLD)
1365     NSObjectFileImage image;
1366     NSObjectFileImageReturnCode ret;
1367
1368     ret = NSCreateObjectFileImageFromFile( psz_file, &image );
1369
1370     if( ret != NSObjectFileImageSuccess )
1371     {
1372         msg_Warn( p_this, "cannot create image from `%s'", psz_file );
1373         return -1;
1374     }
1375
1376     /* Open the dynamic module */
1377     handle = NSLinkModule( image, psz_file,
1378                            NSLINKMODULE_OPTION_RETURN_ON_ERROR );
1379
1380     if( !handle )
1381     {
1382         NSLinkEditErrors errors;
1383         const char *psz_file, *psz_err;
1384         int i_errnum;
1385         NSLinkEditError( &errors, &i_errnum, &psz_file, &psz_err );
1386         msg_Warn( p_this, "cannot link module `%s' (%s)", psz_file, psz_err );
1387         NSDestroyObjectFileImage( image );
1388         return -1;
1389     }
1390
1391     /* Destroy our image, we won't need it */
1392     NSDestroyObjectFileImage( image );
1393
1394 #elif defined(HAVE_DL_BEOS)
1395     handle = load_add_on( psz_file );
1396     if( handle < 0 )
1397     {
1398         msg_Warn( p_this, "cannot load module `%s'", psz_file );
1399         return -1;
1400     }
1401
1402 #elif defined(HAVE_DL_WINDOWS)
1403 #ifdef UNDER_CE
1404     {
1405         wchar_t psz_wfile[MAX_PATH];
1406         MultiByteToWideChar( CP_ACP, 0, psz_file, -1, psz_wfile, MAX_PATH );
1407         handle = LoadLibrary( psz_wfile );
1408     }
1409 #else
1410     handle = LoadLibrary( psz_file );
1411 #endif
1412     if( handle == NULL )
1413     {
1414         char *psz_err = GetWindowsError();
1415         msg_Warn( p_this, "cannot load module `%s' (%s)", psz_file, psz_err );
1416         free( psz_err );
1417         return -1;
1418     }
1419
1420 #elif defined(HAVE_DL_DLOPEN) && defined(RTLD_NOW)
1421     /* static is OK, we are called atomically */
1422
1423 #   if defined(SYS_LINUX)
1424     /* XXX HACK #1 - we should NOT open modules with RTLD_GLOBAL, or we
1425      * are going to get namespace collisions when two modules have common
1426      * public symbols, but ALSA is being a pest here. */
1427     if( strstr( psz_file, "alsa_plugin" ) )
1428     {
1429         handle = dlopen( psz_file, RTLD_NOW | RTLD_GLOBAL );
1430         if( handle == NULL )
1431         {
1432             msg_Warn( p_this, "cannot load module `%s' (%s)",
1433                               psz_file, dlerror() );
1434             return -1;
1435         }
1436     }
1437 #   endif
1438
1439     handle = dlopen( psz_file, RTLD_NOW );
1440     if( handle == NULL )
1441     {
1442         msg_Warn( p_this, "cannot load module `%s' (%s)",
1443                           psz_file, dlerror() );
1444         return -1;
1445     }
1446
1447 #elif defined(HAVE_DL_DLOPEN)
1448 #   if defined(DL_LAZY)
1449     handle = dlopen( psz_file, DL_LAZY );
1450 #   else
1451     handle = dlopen( psz_file, 0 );
1452 #   endif
1453     if( handle == NULL )
1454     {
1455         msg_Warn( p_this, "cannot load module `%s' (%s)",
1456                           psz_file, dlerror() );
1457         return -1;
1458     }
1459
1460 #elif defined(HAVE_DL_SHL_LOAD)
1461     handle = shl_load( psz_file, BIND_IMMEDIATE | BIND_NONFATAL, NULL );
1462     if( handle == NULL )
1463     {
1464         msg_Warn( p_this, "cannot load module `%s' (%s)",
1465                           psz_file, strerror(errno) );
1466         return -1;
1467     }
1468
1469 #else
1470 #   error "Something is wrong in modules.c"
1471
1472 #endif
1473
1474     *p_handle = handle;
1475     return 0;
1476 }
1477
1478 /*****************************************************************************
1479  * CloseModule: unload a dynamic library
1480  *****************************************************************************
1481  * This function unloads a previously opened dynamically linked library
1482  * using a system dependant method. No return value is taken in consideration,
1483  * since some libraries sometimes refuse to close properly.
1484  *****************************************************************************/
1485 static void CloseModule( module_handle_t handle )
1486 {
1487 #if defined(HAVE_DL_DYLD)
1488     NSUnLinkModule( handle, FALSE );
1489
1490 #elif defined(HAVE_DL_BEOS)
1491     unload_add_on( handle );
1492
1493 #elif defined(HAVE_DL_WINDOWS)
1494     FreeLibrary( handle );
1495
1496 #elif defined(HAVE_DL_DLOPEN)
1497     dlclose( handle );
1498
1499 #elif defined(HAVE_DL_SHL_LOAD)
1500     shl_unload( handle );
1501
1502 #endif
1503     return;
1504 }
1505
1506 /*****************************************************************************
1507  * GetSymbol: get a symbol from a dynamic library
1508  *****************************************************************************
1509  * This function queries a loaded library for a symbol specified in a
1510  * string, and returns a pointer to it. We don't check for dlerror() or
1511  * similar functions, since we want a non-NULL symbol anyway.
1512  *****************************************************************************/
1513 static void * _module_getsymbol( module_handle_t, const char * );
1514
1515 static void * GetSymbol( module_handle_t handle, const char * psz_function )
1516 {
1517     void * p_symbol = _module_getsymbol( handle, psz_function );
1518
1519     /* MacOS X dl library expects symbols to begin with "_". So do
1520      * some other operating systems. That's really lame, but hey, what
1521      * can we do ? */
1522     if( p_symbol == NULL )
1523     {
1524         char *psz_call = malloc( strlen( psz_function ) + 2 );
1525
1526         strcpy( psz_call + 1, psz_function );
1527         psz_call[ 0 ] = '_';
1528         p_symbol = _module_getsymbol( handle, psz_call );
1529         free( psz_call );
1530     }
1531
1532     return p_symbol;
1533 }
1534
1535 static void * _module_getsymbol( module_handle_t handle,
1536                                  const char * psz_function )
1537 {
1538 #if defined(HAVE_DL_DYLD)
1539     NSSymbol sym = NSLookupSymbolInModule( handle, psz_function );
1540     return NSAddressOfSymbol( sym );
1541
1542 #elif defined(HAVE_DL_BEOS)
1543     void * p_symbol;
1544     if( B_OK == get_image_symbol( handle, psz_function,
1545                                   B_SYMBOL_TYPE_TEXT, &p_symbol ) )
1546     {
1547         return p_symbol;
1548     }
1549     else
1550     {
1551         return NULL;
1552     }
1553
1554 #elif defined(HAVE_DL_WINDOWS) && defined(UNDER_CE)
1555     wchar_t psz_real[256];
1556     MultiByteToWideChar( CP_ACP, 0, psz_function, -1, psz_real, 256 );
1557
1558     return (void *)GetProcAddress( handle, psz_real );
1559
1560 #elif defined(HAVE_DL_WINDOWS) && defined(WIN32)
1561     return (void *)GetProcAddress( handle, (char *)psz_function );
1562
1563 #elif defined(HAVE_DL_DLOPEN)
1564     return dlsym( handle, psz_function );
1565
1566 #elif defined(HAVE_DL_SHL_LOAD)
1567     void *p_sym;
1568     shl_findsym( &handle, psz_function, TYPE_UNDEFINED, &p_sym );
1569     return p_sym;
1570
1571 #endif
1572 }
1573
1574 #if defined(HAVE_DL_WINDOWS)
1575 static char * GetWindowsError( void )
1576 {
1577 #if defined(UNDER_CE)
1578     wchar_t psz_tmp[MAX_PATH];
1579     char * psz_buffer = malloc( MAX_PATH );
1580 #else
1581     char * psz_tmp = malloc( MAX_PATH );
1582 #endif
1583     int i = 0, i_error = GetLastError();
1584
1585     FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
1586                    NULL, i_error, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
1587                    (LPTSTR)psz_tmp, MAX_PATH, NULL );
1588
1589     /* Go to the end of the string */
1590     while( psz_tmp[i] && psz_tmp[i] != _T('\r') && psz_tmp[i] != _T('\n') )
1591     {
1592         i++;
1593     }
1594
1595     if( psz_tmp[i] )
1596     {
1597 #if defined(UNDER_CE)
1598         swprintf( psz_tmp + i, L" (error %i)", i_error );
1599         psz_tmp[ 255 ] = L'\0';
1600 #else
1601         snprintf( psz_tmp + i, 256 - i, " (error %i)", i_error );
1602         psz_tmp[ 255 ] = '\0';
1603 #endif
1604     }
1605
1606 #if defined(UNDER_CE)
1607     wcstombs( psz_buffer, psz_tmp, MAX_PATH );
1608     return psz_buffer;
1609 #else
1610     return psz_tmp;
1611 #endif
1612 }
1613 #endif /* HAVE_DL_WINDOWS */
1614
1615 /*****************************************************************************
1616  * LoadPluginsCache: loads the plugins cache file
1617  *****************************************************************************
1618  * This function will load the plugin cache if present and valid. This cache
1619  * will in turn be queried by AllocateAllPlugins() to see if it needs to
1620  * actually load the dynamically loadable module.
1621  * This allows us to only fully load plugins when they are actually used.
1622  *****************************************************************************/
1623 static void CacheLoad( vlc_object_t *p_this )
1624 {
1625     char *psz_filename, *psz_homedir;
1626     FILE *file;
1627     int i, j, i_size, i_read;
1628     char p_cachestring[sizeof(PLUGINSCACHE_DIR COPYRIGHT_MESSAGE)];
1629     char p_cachelang[6], p_lang[6];
1630     int i_cache;
1631     module_cache_t **pp_cache = 0;
1632     int32_t i_file_size, i_marker;
1633
1634     psz_homedir = p_this->p_libvlc->psz_homedir;
1635     if( !psz_homedir )
1636     {
1637         msg_Err( p_this, "psz_homedir is null" );
1638         return;
1639     }
1640
1641     i_size = asprintf( &psz_filename, "%s/%s/%s/%s", psz_homedir, CONFIG_DIR,
1642                        PLUGINSCACHE_DIR, CacheName() );
1643     if( i_size <= 0 )
1644     {
1645         msg_Err( p_this, "out of memory" );
1646         return;
1647     }
1648
1649     if( p_this->p_libvlc_global->p_module_bank->b_cache_delete )
1650     {
1651 #if !defined( UNDER_CE )
1652         unlink( psz_filename );
1653 #else
1654         wchar_t psz_wf[MAX_PATH];
1655         MultiByteToWideChar( CP_ACP, 0, psz_filename, -1, psz_wf, MAX_PATH );
1656         DeleteFile( psz_wf );
1657 #endif
1658         msg_Dbg( p_this, "removing plugins cache file %s", psz_filename );
1659         free( psz_filename );
1660         return;
1661     }
1662
1663     msg_Dbg( p_this, "loading plugins cache file %s", psz_filename );
1664
1665     file = utf8_fopen( psz_filename, "rb" );
1666     if( !file )
1667     {
1668         msg_Warn( p_this, "could not open plugins cache file %s for reading",
1669                   psz_filename );
1670         free( psz_filename );
1671         return;
1672     }
1673     free( psz_filename );
1674
1675     /* Check the file size */
1676     i_read = fread( &i_file_size, sizeof(char), sizeof(i_file_size), file );
1677     if( i_read != sizeof(i_file_size) )
1678     {
1679         msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1680                   "(too short)" );
1681         fclose( file );
1682         return;
1683     }
1684
1685     fseek( file, 0, SEEK_END );
1686     if( ftell( file ) != i_file_size )
1687     {
1688         msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1689                   "(corrupted size)" );
1690         fclose( file );
1691         return;
1692     }
1693     fseek( file, sizeof(i_file_size), SEEK_SET );
1694
1695     /* Check the file is a plugins cache */
1696     i_size = sizeof(PLUGINSCACHE_DIR COPYRIGHT_MESSAGE) - 1;
1697     i_read = fread( p_cachestring, sizeof(char), i_size, file );
1698     if( i_read != i_size ||
1699         memcmp( p_cachestring, PLUGINSCACHE_DIR COPYRIGHT_MESSAGE, i_size ) )
1700     {
1701         msg_Warn( p_this, "This doesn't look like a valid plugins cache" );
1702         fclose( file );
1703         return;
1704     }
1705
1706     /* Check Sub-version number */
1707     i_read = fread( &i_marker, sizeof(char), sizeof(i_marker), file );
1708     if( i_read != sizeof(i_marker) || i_marker != CACHE_SUBVERSION_NUM )
1709     {
1710         msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1711                   "(corrupted header)" );
1712         fclose( file );
1713         return;
1714     }
1715
1716     /* Check the language hasn't changed */
1717     sprintf( p_lang, "%5.5s", _("C") ); i_size = 5;
1718     i_read = fread( p_cachelang, sizeof(char), i_size, file );
1719     if( i_read != i_size || memcmp( p_cachelang, p_lang, i_size ) )
1720     {
1721         msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1722                   "(language changed)" );
1723         fclose( file );
1724         return;
1725     }
1726
1727     /* Check header marker */
1728     i_read = fread( &i_marker, sizeof(char), sizeof(i_marker), file );
1729     if( i_read != sizeof(i_marker) ||
1730         i_marker != ftell( file ) - (int)sizeof(i_marker) )
1731     {
1732         msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1733                   "(corrupted header)" );
1734         fclose( file );
1735         return;
1736     }
1737
1738     p_this->p_libvlc_global->p_module_bank->i_loaded_cache = 0;
1739     fread( &i_cache, sizeof(char), sizeof(i_cache), file );
1740     if( i_cache )
1741         pp_cache = p_this->p_libvlc_global->p_module_bank->pp_loaded_cache =
1742                    malloc( i_cache * sizeof(void *) );
1743
1744 #define LOAD_IMMEDIATE(a) \
1745     if( fread( (void *)&a, sizeof(char), sizeof(a), file ) != sizeof(a) ) goto error
1746 #define LOAD_STRING(a) \
1747 { \
1748     a = NULL; \
1749     if( ( fread( &i_size, sizeof(i_size), 1, file ) != 1 ) \
1750      || ( i_size > 16384 ) ) \
1751         goto error; \
1752     if( i_size ) { \
1753         char *psz = malloc( i_size ); \
1754         if( fread( psz, i_size, 1, file ) != 1 ) { \
1755             free( psz ); \
1756             goto error; \
1757         } \
1758         if( psz[i_size-1] ) { \
1759             free( psz ); \
1760             goto error; \
1761         } \
1762         a = psz; \
1763     } \
1764 }
1765
1766     for( i = 0; i < i_cache; i++ )
1767     {
1768         uint16_t i_size;
1769         int i_submodules;
1770
1771         pp_cache[i] = malloc( sizeof(module_cache_t) );
1772         p_this->p_libvlc_global->p_module_bank->i_loaded_cache++;
1773
1774         /* Load common info */
1775         LOAD_STRING( pp_cache[i]->psz_file );
1776         LOAD_IMMEDIATE( pp_cache[i]->i_time );
1777         LOAD_IMMEDIATE( pp_cache[i]->i_size );
1778         LOAD_IMMEDIATE( pp_cache[i]->b_junk );
1779         pp_cache[i]->b_used = VLC_FALSE;
1780
1781         if( pp_cache[i]->b_junk ) continue;
1782
1783         pp_cache[i]->p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
1784
1785         /* Load additional infos */
1786         LOAD_STRING( pp_cache[i]->p_module->psz_object_name );
1787         LOAD_STRING( pp_cache[i]->p_module->psz_shortname );
1788         LOAD_STRING( pp_cache[i]->p_module->psz_longname );
1789         LOAD_STRING( pp_cache[i]->p_module->psz_program );
1790         for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
1791         {
1792             LOAD_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
1793         }
1794         LOAD_STRING( pp_cache[i]->p_module->psz_capability );
1795         LOAD_IMMEDIATE( pp_cache[i]->p_module->i_score );
1796         LOAD_IMMEDIATE( pp_cache[i]->p_module->i_cpu );
1797         LOAD_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
1798         LOAD_IMMEDIATE( pp_cache[i]->p_module->b_reentrant );
1799         LOAD_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
1800
1801         /* Config stuff */
1802         if( CacheLoadConfig( pp_cache[i]->p_module, file ) != VLC_SUCCESS )
1803             goto error;
1804
1805         LOAD_STRING( pp_cache[i]->p_module->psz_filename );
1806
1807         LOAD_IMMEDIATE( i_submodules );
1808
1809         while( i_submodules-- )
1810         {
1811             module_t *p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE);
1812             vlc_object_attach( p_module, pp_cache[i]->p_module );
1813             p_module->b_submodule = VLC_TRUE;
1814
1815             LOAD_STRING( p_module->psz_object_name );
1816             LOAD_STRING( p_module->psz_shortname );
1817             LOAD_STRING( p_module->psz_longname );
1818             LOAD_STRING( p_module->psz_program );
1819             for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
1820             {
1821                 LOAD_STRING( p_module->pp_shortcuts[j] ); // FIX
1822             }
1823             LOAD_STRING( p_module->psz_capability );
1824             LOAD_IMMEDIATE( p_module->i_score );
1825             LOAD_IMMEDIATE( p_module->i_cpu );
1826             LOAD_IMMEDIATE( p_module->b_unloadable );
1827             LOAD_IMMEDIATE( p_module->b_reentrant );
1828         }
1829     }
1830
1831     fclose( file );
1832     return;
1833
1834  error:
1835
1836     msg_Warn( p_this, "plugins cache not loaded (corrupted)" );
1837
1838     /* TODO: cleanup */
1839     p_this->p_libvlc_global->p_module_bank->i_loaded_cache = 0;
1840
1841     fclose( file );
1842     return;
1843 }
1844
1845
1846 int CacheLoadConfig( module_t *p_module, FILE *file )
1847 {
1848     uint32_t i_lines;
1849     uint16_t i_size;
1850
1851     /* Calculate the structure length */
1852     LOAD_IMMEDIATE( p_module->i_config_items );
1853     LOAD_IMMEDIATE( p_module->i_bool_items );
1854
1855     LOAD_IMMEDIATE( i_lines );
1856
1857     /* Allocate memory */
1858     if (i_lines)
1859     {
1860         p_module->p_config =
1861             (module_config_t *)calloc( i_lines, sizeof(module_config_t) );
1862         if( p_module->p_config == NULL )
1863         {
1864             p_module->confsize = 0;
1865             msg_Err( p_module, "config error: can't duplicate p_config" );
1866             return VLC_ENOMEM;
1867         }
1868     }
1869     p_module->confsize = i_lines;
1870
1871     /* Do the duplication job */
1872     for (size_t i = 0; i < i_lines; i++ )
1873     {
1874         LOAD_IMMEDIATE( p_module->p_config[i] );
1875
1876         LOAD_STRING( p_module->p_config[i].psz_type );
1877         LOAD_STRING( p_module->p_config[i].psz_name );
1878         LOAD_STRING( p_module->p_config[i].psz_text );
1879         LOAD_STRING( p_module->p_config[i].psz_longtext );
1880         LOAD_STRING( p_module->p_config[i].psz_current );
1881
1882         if (IsConfigStringType (p_module->p_config[i].i_type))
1883         {
1884             LOAD_STRING (p_module->p_config[i].orig.psz);
1885             p_module->p_config[i].value.psz =
1886                     (p_module->p_config[i].orig.psz != NULL)
1887                         ? strdup (p_module->p_config[i].orig.psz) : NULL;
1888             p_module->p_config[i].saved.psz = NULL;
1889         }
1890         else
1891         {
1892             memcpy (&p_module->p_config[i].value, &p_module->p_config[i].orig,
1893                     sizeof (p_module->p_config[i].value));
1894             memcpy (&p_module->p_config[i].saved, &p_module->p_config[i].orig,
1895                     sizeof (p_module->p_config[i].saved));
1896         }
1897
1898         p_module->p_config[i].b_dirty = VLC_FALSE;
1899
1900         p_module->p_config[i].p_lock = &p_module->object_lock;
1901
1902         if( p_module->p_config[i].i_list )
1903         {
1904             if( p_module->p_config[i].ppsz_list )
1905             {
1906                 int j;
1907                 p_module->p_config[i].ppsz_list =
1908                     malloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
1909                 if( p_module->p_config[i].ppsz_list )
1910                 {
1911                     for( j = 0; j < p_module->p_config[i].i_list; j++ )
1912                         LOAD_STRING( p_module->p_config[i].ppsz_list[j] );
1913                     p_module->p_config[i].ppsz_list[j] = NULL;
1914                 }
1915             }
1916             if( p_module->p_config[i].ppsz_list_text )
1917             {
1918                 int j;
1919                 p_module->p_config[i].ppsz_list_text =
1920                     malloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
1921                 if( p_module->p_config[i].ppsz_list_text )
1922                 {
1923                   for( j = 0; j < p_module->p_config[i].i_list; j++ )
1924                       LOAD_STRING( p_module->p_config[i].ppsz_list_text[j] );
1925                   p_module->p_config[i].ppsz_list_text[j] = NULL;
1926                 }
1927             }
1928             if( p_module->p_config[i].pi_list )
1929             {
1930                 p_module->p_config[i].pi_list =
1931                     malloc( (p_module->p_config[i].i_list + 1) * sizeof(int) );
1932                 if( p_module->p_config[i].pi_list )
1933                 {
1934                     for (int j = 0; j < p_module->p_config[i].i_list; j++)
1935                         LOAD_IMMEDIATE( p_module->p_config[i].pi_list[j] );
1936                 }
1937             }
1938         }
1939
1940         if( p_module->p_config[i].i_action )
1941         {
1942             p_module->p_config[i].ppf_action =
1943                 malloc( p_module->p_config[i].i_action * sizeof(void *) );
1944             p_module->p_config[i].ppsz_action_text =
1945                 malloc( p_module->p_config[i].i_action * sizeof(char *) );
1946
1947             for (int j = 0; j < p_module->p_config[i].i_action; j++)
1948             {
1949                 p_module->p_config[i].ppf_action[j] = 0;
1950                 LOAD_STRING( p_module->p_config[i].ppsz_action_text[j] );
1951             }
1952         }
1953
1954         LOAD_IMMEDIATE( p_module->p_config[i].pf_callback );
1955     }
1956
1957     return VLC_SUCCESS;
1958
1959  error:
1960
1961     return VLC_EGENERIC;
1962 }
1963
1964 /*****************************************************************************
1965  * SavePluginsCache: saves the plugins cache to a file
1966  *****************************************************************************/
1967 static void CacheSave( vlc_object_t *p_this )
1968 {
1969     static char const psz_tag[] =
1970         "Signature: 8a477f597d28d172789f06886806bc55\r\n"
1971         "# This file is a cache directory tag created by VLC.\r\n"
1972         "# For information about cache directory tags, see:\r\n"
1973         "#   http://www.brynosaurus.com/cachedir/\r\n";
1974
1975     char *psz_filename, *psz_homedir;
1976     FILE *file;
1977     int i, j, i_cache;
1978     module_cache_t **pp_cache;
1979     int32_t i_file_size = 0;
1980
1981     psz_homedir = p_this->p_libvlc->psz_homedir;
1982     if( !psz_homedir )
1983     {
1984         msg_Err( p_this, "psz_homedir is null" );
1985         return;
1986     }
1987     psz_filename =
1988        (char *)malloc( sizeof("/" CONFIG_DIR "/" PLUGINSCACHE_DIR "/" ) +
1989                        strlen(psz_homedir) + strlen(CacheName()) );
1990
1991     if( !psz_filename )
1992     {
1993         msg_Err( p_this, "out of memory" );
1994         return;
1995     }
1996
1997     sprintf( psz_filename, "%s/%s", psz_homedir, CONFIG_DIR );
1998
1999     config_CreateDir( p_this, psz_filename );
2000
2001     strcat( psz_filename, "/" PLUGINSCACHE_DIR );
2002
2003     config_CreateDir( p_this, psz_filename );
2004
2005     strcat( psz_filename, "/CACHEDIR.TAG" );
2006
2007     file = utf8_fopen( psz_filename, "wb" );
2008     if( file )
2009     {
2010         fwrite( psz_tag, 1, strlen(psz_tag), file );
2011         fclose( file );
2012     }
2013
2014     sprintf( psz_filename, "%s/%s/%s/%s", psz_homedir, CONFIG_DIR,
2015              PLUGINSCACHE_DIR, CacheName() );
2016
2017     msg_Dbg( p_this, "saving plugins cache file %s", psz_filename );
2018
2019     file = utf8_fopen( psz_filename, "wb" );
2020     if( !file )
2021     {
2022         msg_Warn( p_this, "could not open plugins cache file %s for writing",
2023                   psz_filename );
2024         free( psz_filename );
2025         return;
2026     }
2027     free( psz_filename );
2028
2029     /* Empty space for file size */
2030     fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
2031
2032     /* Contains version number */
2033     fprintf( file, "%s", PLUGINSCACHE_DIR COPYRIGHT_MESSAGE );
2034
2035     /* Sub-version number (to avoid breakage in the dev version when cache
2036      * structure changes) */
2037     i_file_size = CACHE_SUBVERSION_NUM;
2038     fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
2039
2040     /* Language */
2041     fprintf( file, "%5.5s", _("C") );
2042
2043     /* Header marker */
2044     i_file_size = ftell( file );
2045     fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
2046
2047     i_cache = p_this->p_libvlc_global->p_module_bank->i_cache;
2048     pp_cache = p_this->p_libvlc_global->p_module_bank->pp_cache;
2049
2050     fwrite( &i_cache, sizeof(char), sizeof(i_cache), file );
2051
2052 #define SAVE_IMMEDIATE(a) \
2053     fwrite( &a, sizeof(char), sizeof(a), file )
2054 #define SAVE_STRING(a) \
2055     { i_size = a ? strlen( a ) + 1 : 0; \
2056       fwrite( &i_size, sizeof(char), sizeof(i_size), file ); \
2057       if( a ) fwrite( a, sizeof(char), i_size, file ); \
2058     } while(0)
2059
2060     for( i = 0; i < i_cache; i++ )
2061     {
2062         uint16_t i_size;
2063         uint32_t i_submodule;
2064
2065         /* Save common info */
2066         SAVE_STRING( pp_cache[i]->psz_file );
2067         SAVE_IMMEDIATE( pp_cache[i]->i_time );
2068         SAVE_IMMEDIATE( pp_cache[i]->i_size );
2069         SAVE_IMMEDIATE( pp_cache[i]->b_junk );
2070
2071         if( pp_cache[i]->b_junk ) continue;
2072
2073         /* Save additional infos */
2074         SAVE_STRING( pp_cache[i]->p_module->psz_object_name );
2075         SAVE_STRING( pp_cache[i]->p_module->psz_shortname );
2076         SAVE_STRING( pp_cache[i]->p_module->psz_longname );
2077         SAVE_STRING( pp_cache[i]->p_module->psz_program );
2078         for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
2079         {
2080             SAVE_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
2081         }
2082         SAVE_STRING( pp_cache[i]->p_module->psz_capability );
2083         SAVE_IMMEDIATE( pp_cache[i]->p_module->i_score );
2084         SAVE_IMMEDIATE( pp_cache[i]->p_module->i_cpu );
2085         SAVE_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
2086         SAVE_IMMEDIATE( pp_cache[i]->p_module->b_reentrant );
2087         SAVE_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
2088
2089         /* Config stuff */
2090         CacheSaveConfig( pp_cache[i]->p_module, file );
2091
2092         SAVE_STRING( pp_cache[i]->p_module->psz_filename );
2093
2094         i_submodule = pp_cache[i]->p_module->i_children;
2095         SAVE_IMMEDIATE( i_submodule );
2096         for( i_submodule = 0;
2097              i_submodule < (unsigned)pp_cache[i]->p_module->i_children;
2098              i_submodule++ )
2099         {
2100             module_t *p_module =
2101                 (module_t *)pp_cache[i]->p_module->pp_children[i_submodule];
2102
2103             SAVE_STRING( p_module->psz_object_name );
2104             SAVE_STRING( p_module->psz_shortname );
2105             SAVE_STRING( p_module->psz_longname );
2106             SAVE_STRING( p_module->psz_program );
2107             for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
2108             {
2109                 SAVE_STRING( p_module->pp_shortcuts[j] ); // FIX
2110             }
2111             SAVE_STRING( p_module->psz_capability );
2112             SAVE_IMMEDIATE( p_module->i_score );
2113             SAVE_IMMEDIATE( p_module->i_cpu );
2114             SAVE_IMMEDIATE( p_module->b_unloadable );
2115             SAVE_IMMEDIATE( p_module->b_reentrant );
2116         }
2117     }
2118
2119     /* Fill-up file size */
2120     i_file_size = ftell( file );
2121     fseek( file, 0, SEEK_SET );
2122     fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
2123
2124     fclose( file );
2125
2126     return;
2127 }
2128
2129 void CacheSaveConfig( module_t *p_module, FILE *file )
2130 {
2131     uint32_t i_lines = p_module->confsize;
2132     uint16_t i_size;
2133
2134     SAVE_IMMEDIATE( p_module->i_config_items );
2135     SAVE_IMMEDIATE( p_module->i_bool_items );
2136     SAVE_IMMEDIATE( i_lines );
2137
2138     for (size_t i = 0; i < i_lines ; i++)
2139     {
2140         SAVE_IMMEDIATE( p_module->p_config[i] );
2141
2142         SAVE_STRING( p_module->p_config[i].psz_type );
2143         SAVE_STRING( p_module->p_config[i].psz_name );
2144         SAVE_STRING( p_module->p_config[i].psz_text );
2145         SAVE_STRING( p_module->p_config[i].psz_longtext );
2146         SAVE_STRING( p_module->p_config[i].psz_current );
2147         if (IsConfigStringType (p_module->p_config[i].i_type))
2148             SAVE_STRING( p_module->p_config[i].orig.psz );
2149
2150         if( p_module->p_config[i].i_list )
2151         {
2152             if( p_module->p_config[i].ppsz_list )
2153             {
2154                 for (int j = 0; j < p_module->p_config[i].i_list; j++)
2155                     SAVE_STRING( p_module->p_config[i].ppsz_list[j] );
2156             }
2157
2158             if( p_module->p_config[i].ppsz_list_text )
2159             {
2160                 for (int j = 0; j < p_module->p_config[i].i_list; j++)
2161                     SAVE_STRING( p_module->p_config[i].ppsz_list_text[j] );
2162             }
2163             if( p_module->p_config[i].pi_list )
2164             {
2165                 for (int j = 0; j < p_module->p_config[i].i_list; j++)
2166                     SAVE_IMMEDIATE( p_module->p_config[i].pi_list[j] );
2167             }
2168         }
2169
2170         for (int j = 0; j < p_module->p_config[i].i_action; j++)
2171             SAVE_STRING( p_module->p_config[i].ppsz_action_text[j] );
2172
2173         SAVE_IMMEDIATE( p_module->p_config[i].pf_callback );
2174     }
2175 }
2176
2177 /*****************************************************************************
2178  * CacheName: Return the cache file name for this platform.
2179  *****************************************************************************/
2180 static char *CacheName( void )
2181 {
2182     static char psz_cachename[32];
2183
2184     /* Code int size, pointer size and endianness in the filename */
2185     int32_t x = 0xbe00001e;
2186     sprintf( psz_cachename, "plugins-%.2x%.2x%.2x.dat", (int)sizeof(int),
2187              (int)sizeof(void *), (unsigned int)((unsigned char *)&x)[0] );
2188     return psz_cachename;
2189 }
2190
2191 /*****************************************************************************
2192  * CacheMerge: Merge a cache module descriptor with a full module descriptor.
2193  *****************************************************************************/
2194 static void CacheMerge( vlc_object_t *p_this, module_t *p_cache,
2195                         module_t *p_module )
2196 {
2197     int i_submodule;
2198
2199     p_cache->pf_activate = p_module->pf_activate;
2200     p_cache->pf_deactivate = p_module->pf_deactivate;
2201 #ifndef HAVE_SHARED_LIBVLC
2202     p_cache->p_symbols = p_module->p_symbols;
2203 #endif
2204     p_cache->handle = p_module->handle;
2205
2206     for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
2207     {
2208         module_t *p_child = (module_t*)p_module->pp_children[i_submodule];
2209         module_t *p_cchild = (module_t*)p_cache->pp_children[i_submodule];
2210         p_cchild->pf_activate = p_child->pf_activate;
2211         p_cchild->pf_deactivate = p_child->pf_deactivate;
2212 #ifndef HAVE_SHARED_LIBVLC
2213         p_cchild->p_symbols = p_child->p_symbols;
2214 #endif
2215     }
2216
2217     p_cache->b_loaded = VLC_TRUE;
2218     p_module->b_loaded = VLC_FALSE;
2219 }
2220
2221 /*****************************************************************************
2222  * CacheFind: finds the cache entry corresponding to a file
2223  *****************************************************************************/
2224 static module_cache_t *CacheFind( vlc_object_t *p_this, char *psz_file,
2225                                   int64_t i_time, int64_t i_size )
2226 {
2227     module_cache_t **pp_cache;
2228     int i_cache, i;
2229
2230     pp_cache = p_this->p_libvlc_global->p_module_bank->pp_loaded_cache;
2231     i_cache = p_this->p_libvlc_global->p_module_bank->i_loaded_cache;
2232
2233     for( i = 0; i < i_cache; i++ )
2234     {
2235         if( !strcmp( pp_cache[i]->psz_file, psz_file ) &&
2236             pp_cache[i]->i_time == i_time &&
2237             pp_cache[i]->i_size == i_size ) return pp_cache[i];
2238     }
2239
2240     return NULL;
2241 }
2242
2243 #endif /* HAVE_DYNAMIC_PLUGINS */