[System.Runtime.Caching] Add missing locking when accessing cache entries
[mono.git] / mono / metadata / mono-config.c
1 /*
2  * mono-config.c
3  *
4  * Runtime and assembly configuration file support routines.
5  *
6  * Author: Paolo Molaro (lupus@ximian.com)
7  *
8  * Copyright 2002-2003 Ximian, Inc (http://www.ximian.com)
9  * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
10  */
11 #include "config.h"
12 #include <glib.h>
13 #include <string.h>
14
15 #include "mono/metadata/assembly.h"
16 #include "mono/metadata/loader.h"
17 #include "mono/metadata/mono-config.h"
18 #include "mono/metadata/metadata-internals.h"
19 #include "mono/metadata/object-internals.h"
20 #include "mono/utils/mono-logger-internals.h"
21
22 #if defined(TARGET_PS3)
23 #define CONFIG_OS "CellOS"
24 #elif defined(__linux__)
25 #define CONFIG_OS "linux"
26 #elif defined(__APPLE__)
27 #define CONFIG_OS "osx"
28 #elif defined(sun)
29 #define CONFIG_OS "solaris"
30 #elif defined(__FreeBSD__)
31 #define CONFIG_OS "freebsd"
32 #elif defined(__NetBSD__)
33 #define CONFIG_OS "netbsd"
34 #elif defined(__OpenBSD__)
35 #define CONFIG_OS "openbsd"
36 #elif defined(__WIN32__) || defined(TARGET_WIN32)
37 #define CONFIG_OS "windows"
38 #elif defined(_IBMR2)
39 #define CONFIG_OS "aix"
40 #elif defined(__hpux)
41 #define CONFIG_OS "hpux"
42 #elif defined(__HAIKU__)
43 #define CONFIG_OS "haiku"
44 #else
45 #warning Unknown operating system
46 #define CONFIG_OS "unknownOS"
47 #endif
48
49 #ifndef CONFIG_CPU
50 #if defined(__i386__) || defined(TARGET_X86)
51 #define CONFIG_CPU "x86"
52 #define CONFIG_WORDSIZE "32"
53 #elif defined(__x86_64__) || defined(TARGET_AMD64)
54 #define CONFIG_CPU "x86-64"
55 #define CONFIG_WORDSIZE "64"
56 #elif defined(sparc) || defined(__sparc__)
57 #define CONFIG_CPU "sparc"
58 #define CONFIG_WORDSIZE "32"
59 #elif defined(__ppc64__) || defined(__powerpc64__) || defined(TARGET_POWERPC)
60 #define CONFIG_WORDSIZE "64"
61 #ifdef __mono_ppc_ilp32__ 
62 #   define CONFIG_CPU "ppc64ilp32"
63 #else
64 #   define CONFIG_CPU "ppc64"
65 #endif
66 #elif defined(__ppc__) || defined(__powerpc__)
67 #define CONFIG_CPU "ppc"
68 #define CONFIG_WORDSIZE "32"
69 #elif defined(__s390x__)
70 #define CONFIG_CPU "s390x"
71 #define CONFIG_WORDSIZE "64"
72 #elif defined(__s390__)
73 #define CONFIG_CPU "s390"
74 #define CONFIG_WORDSIZE "32"
75 #elif defined(__arm__)
76 #define CONFIG_CPU "arm"
77 #define CONFIG_WORDSIZE "32"
78 #elif defined(__aarch64__)
79 #define CONFIG_CPU "armv8"
80 #define CONFIG_WORDSIZE "64"
81 #elif defined(__ia64__)
82 #define CONFIG_CPU "ia64"
83 #define CONFIG_WORDSIZE "64"
84 #elif defined(mips) || defined(__mips) || defined(_mips)
85 #define CONFIG_CPU "mips"
86 #define CONFIG_WORDSIZE "32"
87 #else
88 #error Unknown CPU
89 #define CONFIG_CPU "unknownCPU"
90 #endif
91 #endif
92
93 static void start_element (GMarkupParseContext *context, 
94                            const gchar         *element_name,
95                            const gchar        **attribute_names,
96                            const gchar        **attribute_values,
97                            gpointer             user_data,
98                            GError             **error);
99
100 static void end_element   (GMarkupParseContext *context,
101                            const gchar         *element_name,
102                            gpointer             user_data,
103                            GError             **error);
104
105 static void parse_text    (GMarkupParseContext *context,
106                            const gchar         *text,
107                            gsize                text_len,
108                            gpointer             user_data,
109                            GError             **error);
110
111 static void passthrough   (GMarkupParseContext *context,
112                            const gchar         *text,
113                            gsize                text_len,
114                            gpointer             user_data,
115                            GError             **error);
116
117 static void parse_error   (GMarkupParseContext *context,
118                            GError              *error,
119                            gpointer             user_data);
120
121 static const GMarkupParser 
122 mono_parser = {
123         start_element,
124         end_element,
125         parse_text,
126         passthrough,
127         parse_error
128 };
129
130 static GHashTable *config_handlers;
131
132 static const char *mono_cfg_dir = NULL;
133 static char *mono_cfg_dir_allocated = NULL;
134
135 /* when this interface is stable, export it. */
136 typedef struct MonoParseHandler MonoParseHandler;
137
138 struct MonoParseHandler {
139         const char *element_name;
140         void*(*init)   (MonoImage *assembly);
141         void (*start)  (gpointer user_data, const gchar *name,
142                         const gchar **attributes,
143                         const gchar **values);
144         void (*text)   (gpointer user_data, const char *text, gsize test_len);
145         void (*end)    (gpointer user_data, const char *name);
146         void (*finish) (gpointer user_data);
147 };
148
149 typedef struct {
150         MonoAssemblyBindingInfo *info;
151         void (*info_parsed)(MonoAssemblyBindingInfo *info, void *user_data);
152         void *user_data;
153 } ParserUserData;
154
155 typedef struct {
156         MonoParseHandler *current;
157         void *user_data;
158         MonoImage *assembly;
159         int inited;
160 } ParseState;
161
162 static void start_element (GMarkupParseContext *context, 
163                            const gchar         *element_name,
164                            const gchar        **attribute_names,
165                            const gchar        **attribute_values,
166                            gpointer             user_data,
167                            GError             **error)
168 {
169         ParseState *state = user_data;
170         if (!state->current) {
171                 state->current = g_hash_table_lookup (config_handlers, element_name);
172                 if (state->current && state->current->init)
173                         state->user_data = state->current->init (state->assembly);
174         }
175         if (state->current && state->current->start)
176                 state->current->start (state->user_data, element_name, attribute_names, attribute_values);
177 }
178
179 static void end_element   (GMarkupParseContext *context,
180                            const gchar         *element_name,
181                            gpointer             user_data,
182                            GError             **error)
183 {
184         ParseState *state = user_data;
185         if (state->current) {
186                 if (state->current->end)
187                         state->current->end (state->user_data, element_name);
188                 if (strcmp (state->current->element_name, element_name) == 0) {
189                         if (state->current->finish)
190                                 state->current->finish (state->user_data);
191                         state->current = NULL;
192                         state->user_data = NULL;
193                 }
194         }
195 }
196
197 static void parse_text    (GMarkupParseContext *context,
198                            const gchar         *text,
199                            gsize                text_len,
200                            gpointer             user_data,
201                            GError             **error)
202 {
203         ParseState *state = user_data;
204         if (state->current && state->current->text)
205                 state->current->text (state->user_data, text, text_len);
206 }
207
208 static void passthrough   (GMarkupParseContext *context,
209                            const gchar         *text,
210                            gsize                text_len,
211                            gpointer             user_data,
212                            GError             **error)
213 {
214         /* do nothing */
215 }
216
217 static void parse_error   (GMarkupParseContext *context,
218                            GError              *error,
219                            gpointer             user_data)
220 {
221         ParseState *state = user_data;
222         const gchar *msg;
223         const gchar *filename;
224
225         filename = state && state->user_data ? (gchar *) state->user_data : "<unknown>";
226         msg = error && error->message ? error->message : "";
227         g_warning ("Error parsing %s: %s", filename, msg);
228 }
229
230 static int
231 arch_matches (const char* arch, const char *value)
232 {
233         char **splitted, **p;
234         int found = FALSE;
235         if (value [0] == '!')
236                 return !arch_matches (arch, value + 1);
237         p = splitted = g_strsplit (value, ",", 0);
238         while (*p) {
239                 if (strcmp (arch, *p) == 0) {
240                         found = TRUE;
241                         break;
242                 }
243                 p++;
244         }
245         g_strfreev (splitted);
246         return found;
247 }
248
249 typedef struct {
250         char *dll;
251         char *target;
252         int ignore;
253         MonoImage *assembly;
254 } DllInfo;
255
256 static void*
257 dllmap_init (MonoImage *assembly) {
258         DllInfo *info = g_new0 (DllInfo, 1);
259         info->assembly = assembly;
260         return info;
261 }
262
263 static void
264 dllmap_start (gpointer user_data, 
265               const gchar         *element_name,
266               const gchar        **attribute_names,
267               const gchar        **attribute_values)
268 {
269         int i;
270         DllInfo *info = user_data;
271         
272         if (strcmp (element_name, "dllmap") == 0) {
273                 g_free (info->dll);
274                 g_free (info->target);
275                 info->dll = info->target = NULL;
276                 info->ignore = FALSE;
277                 for (i = 0; attribute_names [i]; ++i) {
278                         if (strcmp (attribute_names [i], "dll") == 0)
279                                 info->dll = g_strdup (attribute_values [i]);
280                         else if (strcmp (attribute_names [i], "target") == 0){
281                                 char *p = strstr (attribute_values [i], "$mono_libdir");
282                                 if (p != NULL){
283                                         const char *libdir = mono_assembly_getrootdir ();
284                                         size_t libdir_len = strlen (libdir);
285                                         char *result;
286                                         
287                                         result = g_malloc (libdir_len-strlen("$mono_libdir")+strlen(attribute_values[i])+1);
288                                         strncpy (result, attribute_names[i], p-attribute_values[i]);
289                                         strcat (result, libdir);
290                                         strcat (result, p+strlen("$mono_libdir"));
291                                         info->target = result;
292                                 } else 
293                                         info->target = g_strdup (attribute_values [i]);
294                         } else if (strcmp (attribute_names [i], "os") == 0 && !arch_matches (CONFIG_OS, attribute_values [i]))
295                                 info->ignore = TRUE;
296                         else if (strcmp (attribute_names [i], "cpu") == 0 && !arch_matches (CONFIG_CPU, attribute_values [i]))
297                                 info->ignore = TRUE;
298                         else if (strcmp (attribute_names [i], "wordsize") == 0 && !arch_matches (CONFIG_WORDSIZE, attribute_values [i]))
299                                 info->ignore = TRUE;
300                 }
301                 if (!info->ignore)
302                         mono_dllmap_insert (info->assembly, info->dll, NULL, info->target, NULL);
303         } else if (strcmp (element_name, "dllentry") == 0) {
304                 const char *name = NULL, *target = NULL, *dll = NULL;
305                 int ignore = FALSE;
306                 for (i = 0; attribute_names [i]; ++i) {
307                         if (strcmp (attribute_names [i], "dll") == 0)
308                                 dll = attribute_values [i];
309                         else if (strcmp (attribute_names [i], "target") == 0)
310                                 target = attribute_values [i];
311                         else if (strcmp (attribute_names [i], "name") == 0)
312                                 name = attribute_values [i];
313                         else if (strcmp (attribute_names [i], "os") == 0 && !arch_matches (CONFIG_OS, attribute_values [i]))
314                                 ignore = TRUE;
315                         else if (strcmp (attribute_names [i], "cpu") == 0 && !arch_matches (CONFIG_CPU, attribute_values [i]))
316                                 ignore = TRUE;
317                         else if (strcmp (attribute_names [i], "wordsize") == 0 && !arch_matches (CONFIG_WORDSIZE, attribute_values [i]))
318                                 ignore = TRUE;
319                 }
320                 if (!dll)
321                         dll = info->dll;
322                 if (!info->ignore && !ignore)
323                         mono_dllmap_insert (info->assembly, info->dll, name, dll, target);
324         }
325 }
326
327 static void
328 dllmap_finish (gpointer user_data)
329 {
330         DllInfo *info = user_data;
331
332         g_free (info->dll);
333         g_free (info->target);
334         g_free (info);
335 }
336
337 static const MonoParseHandler
338 dllmap_handler = {
339         "dllmap",
340         dllmap_init,
341         dllmap_start,
342         NULL, /* text */
343         NULL, /* end */
344         dllmap_finish
345 };
346
347 static void
348 legacyUEP_start (gpointer user_data, 
349               const gchar         *element_name,
350               const gchar        **attribute_names,
351               const gchar        **attribute_values) {
352         if ((strcmp (element_name, "legacyUnhandledExceptionPolicy") == 0) &&
353                         (attribute_names [0] != NULL) &&
354                         (strcmp (attribute_names [0], "enabled") == 0)) {
355                 if ((strcmp (attribute_values [0], "1") == 0) ||
356                                 (g_ascii_strcasecmp (attribute_values [0], "true") == 0)) {
357                         mono_runtime_unhandled_exception_policy_set (MONO_UNHANDLED_POLICY_LEGACY);
358                 }
359         }
360 }
361
362 static const MonoParseHandler
363 legacyUEP_handler = {
364         "legacyUnhandledExceptionPolicy",
365         NULL, /* init */
366         legacyUEP_start,
367         NULL, /* text */
368         NULL, /* end */
369         NULL, /* finish */
370 };
371
372 static void
373 aot_cache_start (gpointer user_data,
374                                  const gchar         *element_name,
375                                  const gchar        **attribute_names,
376                                  const gchar        **attribute_values)
377 {
378         int i;
379         MonoAotCacheConfig *config;
380
381         if (strcmp (element_name, "aotcache") != 0)
382                 return;
383
384         config = mono_get_aot_cache_config ();
385
386         /* Per-app configuration */
387         for (i = 0; attribute_names [i]; ++i) {
388                 if (!strcmp (attribute_names [i], "app")) {
389                         config->apps = g_slist_prepend (config->apps, g_strdup (attribute_values [i]));
390                 }
391         }
392
393         /* Global configuration */
394         for (i = 0; attribute_names [i]; ++i) {
395                 if (!strcmp (attribute_names [i], "assemblies")) {
396                         char **parts, **ptr;
397                         char *part;
398
399                         parts = g_strsplit (attribute_values [i], " ", -1);
400                         for (ptr = parts; ptr && *ptr; ptr ++) {
401                                 part = *ptr;
402                                 config->assemblies = g_slist_prepend (config->assemblies, g_strdup (part));
403                         }
404                         g_strfreev (parts);
405                 } else if (!strcmp (attribute_names [i], "options")) {
406                         config->aot_options = g_strdup (attribute_values [i]);
407                 }
408         }
409 }
410
411 static const MonoParseHandler
412 aot_cache_handler = {
413         "aotcache",
414         NULL, /* init */
415         aot_cache_start,
416         NULL, /* text */
417         NULL, /* end */
418         NULL, /* finish */
419 };
420
421 static int inited = 0;
422
423 static void
424 mono_config_init (void)
425 {
426         inited = 1;
427         config_handlers = g_hash_table_new (g_str_hash, g_str_equal);
428         g_hash_table_insert (config_handlers, (gpointer) dllmap_handler.element_name, (gpointer) &dllmap_handler);
429         g_hash_table_insert (config_handlers, (gpointer) legacyUEP_handler.element_name, (gpointer) &legacyUEP_handler);
430         g_hash_table_insert (config_handlers, (gpointer) aot_cache_handler.element_name, (gpointer) &aot_cache_handler);
431 }
432
433 void
434 mono_config_cleanup (void)
435 {
436         if (config_handlers)
437                 g_hash_table_destroy (config_handlers);
438         g_free (mono_cfg_dir_allocated);
439 }
440
441 /* FIXME: error handling */
442
443 static void
444 mono_config_parse_xml_with_context (ParseState *state, const char *text, gsize len)
445 {
446         GMarkupParseContext *context;
447
448         if (!inited)
449                 mono_config_init ();
450
451         context = g_markup_parse_context_new (&mono_parser, 0, state, NULL);
452         if (g_markup_parse_context_parse (context, text, len, NULL)) {
453                 g_markup_parse_context_end_parse (context, NULL);
454         }
455         g_markup_parse_context_free (context);
456 }
457
458 /* If assembly is NULL, parse in the global context */
459 static int
460 mono_config_parse_file_with_context (ParseState *state, const char *filename)
461 {
462         gchar *text;
463         gsize len;
464         gint offset;
465
466         mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_CONFIG,
467                         "Config attempting to parse: '%s'.", filename);
468
469         if (!g_file_get_contents (filename, &text, &len, NULL))
470                 return 0;
471
472         offset = 0;
473         if (len > 3 && text [0] == '\xef' && text [1] == (gchar) '\xbb' && text [2] == '\xbf')
474                 offset = 3; /* Skip UTF-8 BOM */
475         if (state->user_data == NULL)
476                 state->user_data = (gpointer) filename;
477         mono_config_parse_xml_with_context (state, text + offset, len - offset);
478         g_free (text);
479         return 1;
480 }
481
482 /**
483  * mono_config_parse_memory:
484  * @buffer: a pointer to an string XML representation of the configuration
485  *
486  * Parses the configuration from a buffer
487  */
488 void
489 mono_config_parse_memory (const char *buffer)
490 {
491         ParseState state = {NULL};
492
493         state.user_data = (gpointer) "<buffer>";
494         mono_config_parse_xml_with_context (&state, buffer, strlen (buffer));
495 }
496
497 static void
498 mono_config_parse_file (const char *filename)
499 {
500         ParseState state = {NULL};
501         state.user_data = (gpointer) filename;
502         mono_config_parse_file_with_context (&state, filename);
503 }
504
505 /* 
506  * use the equivalent lookup code from the GAC when available.
507  * Depending on state, this should give something like:
508  *      aname/version-pubtoken/
509  *      aname/version/
510  *      aname
511  */
512 static char*
513 get_assembly_filename (MonoImage *image, int state)
514 {
515         switch (state) {
516         case 0:
517                 return g_strdup (mono_image_get_name (image));
518         default:
519                 return NULL;
520         }
521 }
522
523 typedef struct _BundledConfig BundledConfig;
524
525 struct _BundledConfig {
526         BundledConfig *next;
527         const char* aname;
528         const char* config_xml;
529 };
530
531 static BundledConfig *bundled_configs = NULL;
532
533 static const char *bundled_machine_config = NULL;
534
535 void
536 mono_register_config_for_assembly (const char* assembly_name, const char* config_xml)
537 {
538         BundledConfig *bconfig;
539
540         bconfig = g_new0 (BundledConfig, 1);
541         bconfig->aname = assembly_name;
542         bconfig->config_xml = config_xml;
543         bconfig->next = bundled_configs;
544         bundled_configs = bconfig;
545 }
546
547 const char *
548 mono_config_string_for_assembly_file (const char *filename)
549 {
550         BundledConfig *bconfig;
551         
552         for (bconfig = bundled_configs; bconfig; bconfig = bconfig->next) {
553                 if (bconfig->aname && strcmp (bconfig->aname, filename) == 0)
554                         return bconfig->config_xml;
555         }
556         return NULL;
557 }
558
559 void 
560 mono_config_for_assembly (MonoImage *assembly)
561 {
562         ParseState state = {NULL};
563         int got_it = 0, i;
564         char *aname, *cfg, *cfg_name;
565         const char *bundled_config;
566         
567         state.assembly = assembly;
568
569         bundled_config = mono_config_string_for_assembly_file (assembly->module_name);
570         if (bundled_config) {
571                 state.user_data = (gpointer) "<bundled>";
572                 mono_config_parse_xml_with_context (&state, bundled_config, strlen (bundled_config));
573         }
574
575         cfg_name = g_strdup_printf ("%s.config", mono_image_get_filename (assembly));
576         mono_config_parse_file_with_context (&state, cfg_name);
577         g_free (cfg_name);
578
579         cfg_name = g_strdup_printf ("%s.config", mono_image_get_name (assembly));
580
581         for (i = 0; (aname = get_assembly_filename (assembly, i)) != NULL; ++i) {
582                 cfg = g_build_filename (mono_get_config_dir (), "mono", "assemblies", aname, cfg_name, NULL);
583                 got_it += mono_config_parse_file_with_context (&state, cfg);
584                 g_free (cfg);
585
586 #ifdef TARGET_WIN32
587                 const char *home = g_get_home_dir ();
588                 cfg = g_build_filename (home, ".mono", "assemblies", aname, cfg_name, NULL);
589                 got_it += mono_config_parse_file_with_context (&state, cfg);
590                 g_free (cfg);
591 #endif
592                 g_free (aname);
593                 if (got_it)
594                         break;
595         }
596         g_free (cfg_name);
597 }
598
599 /**
600  * mono_config_parse:
601  * @filename: the filename to load the configuration variables from.
602  *
603  * Pass a NULL filename to parse the default config files
604  * (or the file in the MONO_CONFIG env var).
605  */
606 void
607 mono_config_parse (const char *filename) {
608         const char *home;
609         char *mono_cfg;
610 #ifndef TARGET_WIN32
611         char *user_cfg;
612 #endif
613
614         if (filename) {
615                 mono_config_parse_file (filename);
616                 return;
617         }
618
619         home = g_getenv ("MONO_CONFIG");
620         if (home) {
621                 mono_config_parse_file (home);
622                 return;
623         }
624
625         mono_cfg = g_build_filename (mono_get_config_dir (), "mono", "config", NULL);
626         mono_config_parse_file (mono_cfg);
627         g_free (mono_cfg);
628
629 #if !defined(TARGET_WIN32) && !defined(__native_client__)
630         home = g_get_home_dir ();
631         user_cfg = g_strconcat (home, G_DIR_SEPARATOR_S, ".mono/config", NULL);
632         mono_config_parse_file (user_cfg);
633         g_free (user_cfg);
634 #endif
635 }
636
637 /* Invoked during startup */
638 void
639 mono_set_config_dir (const char *dir)
640 {
641         /* If this variable is set, overrides the directory computed */
642         mono_cfg_dir = g_getenv ("MONO_CFG_DIR");
643         if (mono_cfg_dir == NULL)
644                 mono_cfg_dir = mono_cfg_dir_allocated = g_strdup (dir);
645 }
646
647 const char* 
648 mono_get_config_dir (void)
649 {
650         if (mono_cfg_dir == NULL)
651                 mono_set_dirs (NULL, NULL);
652
653         return mono_cfg_dir;
654 }
655
656 void
657 mono_register_machine_config (const char *config_xml)
658 {
659         bundled_machine_config = config_xml;
660 }
661
662 const char *
663 mono_get_machine_config (void)
664 {
665         return bundled_machine_config;
666 }
667
668 static void
669 assembly_binding_end (gpointer user_data, const char *element_name)
670 {
671         ParserUserData *pud = user_data;
672
673         if (!strcmp (element_name, "dependentAssembly")) {
674                 if (pud->info_parsed && pud->info) {
675                         pud->info_parsed (pud->info, pud->user_data);
676                         g_free (pud->info->name);
677                         g_free (pud->info->culture);
678                 }
679         }
680 }
681
682 static void
683 publisher_policy_start (gpointer user_data,
684                 const gchar *element_name,
685                 const gchar **attribute_names,
686                 const gchar **attribute_values)
687 {
688         ParserUserData *pud;
689         MonoAssemblyBindingInfo *info;
690         int n;
691
692         pud = user_data;
693         info = pud->info;
694         if (!strcmp (element_name, "dependentAssembly")) {
695                 info->name = NULL;
696                 info->culture = NULL;
697                 info->has_old_version_bottom = FALSE;
698                 info->has_old_version_top = FALSE;
699                 info->has_new_version = FALSE;
700                 info->is_valid = FALSE;
701                 memset (&info->old_version_bottom, 0, sizeof (info->old_version_bottom));
702                 memset (&info->old_version_top, 0, sizeof (info->old_version_top));
703                 memset (&info->new_version, 0, sizeof (info->new_version));
704         } if (!strcmp (element_name, "assemblyIdentity")) {
705                 for (n = 0; attribute_names [n]; n++) {
706                         const gchar *attribute_name = attribute_names [n];
707                         
708                         if (!strcmp (attribute_name, "name"))
709                                 info->name = g_strdup (attribute_values [n]);
710                         else if (!strcmp (attribute_name, "publicKeyToken")) {
711                                 if (strlen (attribute_values [n]) == MONO_PUBLIC_KEY_TOKEN_LENGTH - 1)
712                                         g_strlcpy ((char *) info->public_key_token, attribute_values [n], MONO_PUBLIC_KEY_TOKEN_LENGTH);
713                         } else if (!strcmp (attribute_name, "culture")) {
714                                 if (!strcmp (attribute_values [n], "neutral"))
715                                         info->culture = g_strdup ("");
716                                 else
717                                         info->culture = g_strdup (attribute_values [n]);
718                         }
719                 }
720         } else if (!strcmp (element_name, "bindingRedirect")) {
721                 for (n = 0; attribute_names [n]; n++) {
722                         const gchar *attribute_name = attribute_names [n];
723
724                         if (!strcmp (attribute_name, "oldVersion")) {
725                                 gchar **numbers, **version, **versions;
726                                 gint major, minor, build, revision;
727
728                                 /* Invalid value */
729                                 if (!strcmp (attribute_values [n], ""))
730                                         return;
731                                 
732                                 versions = g_strsplit (attribute_values [n], "-", 2);
733                                 version = g_strsplit (*versions, ".", 4);
734
735                                 /* We assign the values to gint vars to do the checks */
736                                 numbers = version;
737                                 major = *numbers ? atoi (*numbers++) : -1;
738                                 minor = *numbers ? atoi (*numbers++) : -1;
739                                 build = *numbers ? atoi (*numbers++) : -1;
740                                 revision = *numbers ? atoi (*numbers) : -1;
741                                 g_strfreev (version);
742                                 if (major < 0 || minor < 0 || build < 0 || revision < 0) {
743                                         g_strfreev (versions);
744                                         return;
745                                 }
746
747                                 info->old_version_bottom.major = major;
748                                 info->old_version_bottom.minor = minor;
749                                 info->old_version_bottom.build = build;
750                                 info->old_version_bottom.revision = revision;
751                                 info->has_old_version_bottom = TRUE;
752
753                                 if (!*(versions + 1)) {
754                                         g_strfreev (versions);
755                                         continue;
756                                 }
757                                 
758                                 numbers = version = g_strsplit (*(versions + 1), ".", 4);
759                                 major = *numbers ? atoi (*numbers++) : -1;
760                                 minor = *numbers ? atoi (*numbers++) : -1;
761                                 build = *numbers ? atoi (*numbers++) : -1;
762                                 revision = *numbers ? atoi (*numbers) : 1;
763                                 g_strfreev (version);
764                                 if (major < 0 || minor < 0 || build < 0 || revision < 0) {
765                                         g_strfreev (versions);
766                                         return;
767                                 }
768
769                                 info->old_version_top.major = major;
770                                 info->old_version_top.minor = minor;
771                                 info->old_version_top.build = build;
772                                 info->old_version_top.revision = revision;
773                                 info->has_old_version_top = TRUE;
774
775                                 g_strfreev (versions);
776                         } else if (!strcmp (attribute_name, "newVersion")) {
777                                 gchar **numbers, **version;
778
779                                 /* Invalid value */
780                                 if (!strcmp (attribute_values [n], ""))
781                                         return;
782
783                                 numbers = version = g_strsplit (attribute_values [n], ".", 4);
784                                 info->new_version.major = *numbers ? atoi (*numbers++) : -1;
785                                 info->new_version.minor = *numbers ? atoi (*numbers++) : -1;
786                                 info->new_version.build = *numbers ? atoi (*numbers++) : -1;
787                                 info->new_version.revision = *numbers ? atoi (*numbers) : -1;
788                                 info->has_new_version = TRUE;
789                                 g_strfreev (version);
790                         }
791                 }
792         }
793 }
794
795 static MonoParseHandler
796 publisher_policy_parser = {
797         "", /* We don't need to use declare an xml element */
798         NULL,
799         publisher_policy_start,
800         NULL,
801         NULL,
802         NULL
803 };
804
805 void
806 mono_config_parse_publisher_policy (const gchar *filename, MonoAssemblyBindingInfo *info)
807 {
808         ParserUserData user_data = {
809                 info,
810                 NULL,
811                 NULL
812         };
813         ParseState state = {
814                 &publisher_policy_parser, /* MonoParseHandler */
815                 &user_data, /* user_data */
816                 NULL, /* MonoImage (we don't need it right now)*/
817                 TRUE /* We are already inited */
818         };
819         
820         mono_config_parse_file_with_context (&state, filename);
821 }
822
823 static MonoParseHandler
824 config_assemblybinding_parser = {
825         "", /* We don't need to use declare an xml element */
826         NULL,
827         publisher_policy_start,
828         NULL,
829         assembly_binding_end,
830         NULL
831 };
832
833 void
834 mono_config_parse_assembly_bindings (const char *filename, int amajor, int aminor, void *user_data, void (*infocb)(MonoAssemblyBindingInfo *info, void *user_data))
835 {
836         MonoAssemblyBindingInfo info;
837         ParserUserData pud;
838         ParseState state;
839
840         info.major = amajor;
841         info.minor = aminor;
842
843         pud.info = &info;
844         pud.info_parsed = infocb;
845         pud.user_data = user_data;
846
847         state.current = &config_assemblybinding_parser;  /* MonoParseHandler */
848         state.user_data = &pud;
849         state.assembly = NULL; /* MonoImage (we don't need it right now)*/
850         state.inited = TRUE; /* We are already inited */
851
852         mono_config_parse_file_with_context (&state, filename);
853 }
854
855 static mono_bool mono_server_mode = FALSE;
856
857 void
858 mono_config_set_server_mode (mono_bool server_mode)
859 {
860         mono_server_mode = server_mode;
861 }
862
863 mono_bool
864 mono_config_is_server_mode (void)
865 {
866         return mono_server_mode;
867 }
868