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