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