mono: Use the relocated directory for finding support libraries in data/config
[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-internal.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 *match = strstr (attribute_values [i], "$mono_libdir");
282                                 if (match != NULL) {
283                                         /* substitude $mono_libdir */
284                                         const char *libdir = mono_assembly_getrootdir ();
285                                         const int libdir_len = strlen (libdir);
286                                         const int varname_len = strlen ("$mono_libdir");
287                                         const int pre_len = match - attribute_values [i];
288                                         const int post_len = strlen (match) - varname_len + 3;
289
290                                         char *result = g_malloc (pre_len + libdir_len + post_len + 1);
291                                         g_strlcpy (result, attribute_values [i], pre_len);
292                                         strncat (result, libdir, libdir_len);
293                                         strncat (result, match + varname_len, post_len);
294                                         info->target = result;
295                                 } else
296                                         info->target = g_strdup (attribute_values [i]);
297                         } else if (strcmp (attribute_names [i], "os") == 0 && !arch_matches (CONFIG_OS, attribute_values [i]))
298                                 info->ignore = TRUE;
299                         else if (strcmp (attribute_names [i], "cpu") == 0 && !arch_matches (CONFIG_CPU, attribute_values [i]))
300                                 info->ignore = TRUE;
301                         else if (strcmp (attribute_names [i], "wordsize") == 0 && !arch_matches (CONFIG_WORDSIZE, attribute_values [i]))
302                                 info->ignore = TRUE;
303                 }
304                 if (!info->ignore)
305                         mono_dllmap_insert (info->assembly, info->dll, NULL, info->target, NULL);
306         } else if (strcmp (element_name, "dllentry") == 0) {
307                 const char *name = NULL, *target = NULL, *dll = NULL;
308                 int ignore = FALSE;
309                 for (i = 0; attribute_names [i]; ++i) {
310                         if (strcmp (attribute_names [i], "dll") == 0)
311                                 dll = attribute_values [i];
312                         else if (strcmp (attribute_names [i], "target") == 0)
313                                 target = attribute_values [i];
314                         else if (strcmp (attribute_names [i], "name") == 0)
315                                 name = attribute_values [i];
316                         else if (strcmp (attribute_names [i], "os") == 0 && !arch_matches (CONFIG_OS, attribute_values [i]))
317                                 ignore = TRUE;
318                         else if (strcmp (attribute_names [i], "cpu") == 0 && !arch_matches (CONFIG_CPU, attribute_values [i]))
319                                 ignore = TRUE;
320                         else if (strcmp (attribute_names [i], "wordsize") == 0 && !arch_matches (CONFIG_WORDSIZE, attribute_values [i]))
321                                 ignore = TRUE;
322                 }
323                 if (!dll)
324                         dll = info->dll;
325                 if (!info->ignore && !ignore)
326                         mono_dllmap_insert (info->assembly, info->dll, name, dll, target);
327         }
328 }
329
330 static void
331 dllmap_finish (gpointer user_data)
332 {
333         DllInfo *info = user_data;
334
335         g_free (info->dll);
336         g_free (info->target);
337         g_free (info);
338 }
339
340 static const MonoParseHandler
341 dllmap_handler = {
342         "dllmap",
343         dllmap_init,
344         dllmap_start,
345         NULL, /* text */
346         NULL, /* end */
347         dllmap_finish
348 };
349
350 static void
351 legacyUEP_start (gpointer user_data, 
352               const gchar         *element_name,
353               const gchar        **attribute_names,
354               const gchar        **attribute_values) {
355         if ((strcmp (element_name, "legacyUnhandledExceptionPolicy") == 0) &&
356                         (attribute_names [0] != NULL) &&
357                         (strcmp (attribute_names [0], "enabled") == 0)) {
358                 if ((strcmp (attribute_values [0], "1") == 0) ||
359                                 (g_ascii_strcasecmp (attribute_values [0], "true") == 0)) {
360                         mono_runtime_unhandled_exception_policy_set (MONO_UNHANDLED_POLICY_LEGACY);
361                 }
362         }
363 }
364
365 static const MonoParseHandler
366 legacyUEP_handler = {
367         "legacyUnhandledExceptionPolicy",
368         NULL, /* init */
369         legacyUEP_start,
370         NULL, /* text */
371         NULL, /* end */
372         NULL, /* finish */
373 };
374
375 static void
376 aot_cache_start (gpointer user_data,
377                                  const gchar         *element_name,
378                                  const gchar        **attribute_names,
379                                  const gchar        **attribute_values)
380 {
381         int i;
382         MonoAotCacheConfig *config;
383
384         if (strcmp (element_name, "aotcache") != 0)
385                 return;
386
387         config = mono_get_aot_cache_config ();
388
389         /* Per-app configuration */
390         for (i = 0; attribute_names [i]; ++i) {
391                 if (!strcmp (attribute_names [i], "app")) {
392                         config->apps = g_slist_prepend (config->apps, g_strdup (attribute_values [i]));
393                 }
394         }
395
396         /* Global configuration */
397         for (i = 0; attribute_names [i]; ++i) {
398                 if (!strcmp (attribute_names [i], "assemblies")) {
399                         char **parts, **ptr;
400                         char *part;
401
402                         parts = g_strsplit (attribute_values [i], " ", -1);
403                         for (ptr = parts; ptr && *ptr; ptr ++) {
404                                 part = *ptr;
405                                 config->assemblies = g_slist_prepend (config->assemblies, g_strdup (part));
406                         }
407                         g_strfreev (parts);
408                 } else if (!strcmp (attribute_names [i], "options")) {
409                         config->aot_options = g_strdup (attribute_values [i]);
410                 }
411         }
412 }
413
414 static const MonoParseHandler
415 aot_cache_handler = {
416         "aotcache",
417         NULL, /* init */
418         aot_cache_start,
419         NULL, /* text */
420         NULL, /* end */
421         NULL, /* finish */
422 };
423
424 static int inited = 0;
425
426 static void
427 mono_config_init (void)
428 {
429         inited = 1;
430         config_handlers = g_hash_table_new (g_str_hash, g_str_equal);
431         g_hash_table_insert (config_handlers, (gpointer) dllmap_handler.element_name, (gpointer) &dllmap_handler);
432         g_hash_table_insert (config_handlers, (gpointer) legacyUEP_handler.element_name, (gpointer) &legacyUEP_handler);
433         g_hash_table_insert (config_handlers, (gpointer) aot_cache_handler.element_name, (gpointer) &aot_cache_handler);
434 }
435
436 void
437 mono_config_cleanup (void)
438 {
439         if (config_handlers)
440                 g_hash_table_destroy (config_handlers);
441         g_free (mono_cfg_dir_allocated);
442 }
443
444 /* FIXME: error handling */
445
446 static void
447 mono_config_parse_xml_with_context (ParseState *state, const char *text, gsize len)
448 {
449         GMarkupParseContext *context;
450
451         if (!inited)
452                 mono_config_init ();
453
454         context = g_markup_parse_context_new (&mono_parser, 0, state, NULL);
455         if (g_markup_parse_context_parse (context, text, len, NULL)) {
456                 g_markup_parse_context_end_parse (context, NULL);
457         }
458         g_markup_parse_context_free (context);
459 }
460
461 /* If assembly is NULL, parse in the global context */
462 static int
463 mono_config_parse_file_with_context (ParseState *state, const char *filename)
464 {
465         gchar *text;
466         gsize len;
467         gint offset;
468
469         mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_CONFIG,
470                         "Config attempting to parse: '%s'.", filename);
471
472         if (!g_file_get_contents (filename, &text, &len, NULL))
473                 return 0;
474
475         offset = 0;
476         if (len > 3 && text [0] == '\xef' && text [1] == (gchar) '\xbb' && text [2] == '\xbf')
477                 offset = 3; /* Skip UTF-8 BOM */
478         if (state->user_data == NULL)
479                 state->user_data = (gpointer) filename;
480         mono_config_parse_xml_with_context (state, text + offset, len - offset);
481         g_free (text);
482         return 1;
483 }
484
485 /**
486  * mono_config_parse_memory:
487  * @buffer: a pointer to an string XML representation of the configuration
488  *
489  * Parses the configuration from a buffer
490  */
491 void
492 mono_config_parse_memory (const char *buffer)
493 {
494         ParseState state = {NULL};
495
496         state.user_data = (gpointer) "<buffer>";
497         mono_config_parse_xml_with_context (&state, buffer, strlen (buffer));
498 }
499
500 static void
501 mono_config_parse_file (const char *filename)
502 {
503         ParseState state = {NULL};
504         state.user_data = (gpointer) filename;
505         mono_config_parse_file_with_context (&state, filename);
506 }
507
508 /* 
509  * use the equivalent lookup code from the GAC when available.
510  * Depending on state, this should give something like:
511  *      aname/version-pubtoken/
512  *      aname/version/
513  *      aname
514  */
515 static char*
516 get_assembly_filename (MonoImage *image, int state)
517 {
518         switch (state) {
519         case 0:
520                 return g_strdup (mono_image_get_name (image));
521         default:
522                 return NULL;
523         }
524 }
525
526 typedef struct _BundledConfig BundledConfig;
527
528 struct _BundledConfig {
529         BundledConfig *next;
530         const char* aname;
531         const char* config_xml;
532 };
533
534 static BundledConfig *bundled_configs = NULL;
535
536 static const char *bundled_machine_config = NULL;
537
538 void
539 mono_register_config_for_assembly (const char* assembly_name, const char* config_xml)
540 {
541         BundledConfig *bconfig;
542
543         bconfig = g_new0 (BundledConfig, 1);
544         bconfig->aname = assembly_name;
545         bconfig->config_xml = config_xml;
546         bconfig->next = bundled_configs;
547         bundled_configs = bconfig;
548 }
549
550 const char *
551 mono_config_string_for_assembly_file (const char *filename)
552 {
553         BundledConfig *bconfig;
554         
555         for (bconfig = bundled_configs; bconfig; bconfig = bconfig->next) {
556                 if (bconfig->aname && strcmp (bconfig->aname, filename) == 0)
557                         return bconfig->config_xml;
558         }
559         return NULL;
560 }
561
562 void 
563 mono_config_for_assembly (MonoImage *assembly)
564 {
565         ParseState state = {NULL};
566         int got_it = 0, i;
567         char *aname, *cfg, *cfg_name;
568         const char *bundled_config;
569         
570         state.assembly = assembly;
571
572         bundled_config = mono_config_string_for_assembly_file (assembly->module_name);
573         if (bundled_config) {
574                 state.user_data = (gpointer) "<bundled>";
575                 mono_config_parse_xml_with_context (&state, bundled_config, strlen (bundled_config));
576         }
577
578         cfg_name = g_strdup_printf ("%s.config", mono_image_get_filename (assembly));
579         mono_config_parse_file_with_context (&state, cfg_name);
580         g_free (cfg_name);
581
582         cfg_name = g_strdup_printf ("%s.config", mono_image_get_name (assembly));
583
584         for (i = 0; (aname = get_assembly_filename (assembly, i)) != NULL; ++i) {
585                 cfg = g_build_filename (mono_get_config_dir (), "mono", "assemblies", aname, cfg_name, NULL);
586                 got_it += mono_config_parse_file_with_context (&state, cfg);
587                 g_free (cfg);
588
589 #ifdef TARGET_WIN32
590                 const char *home = g_get_home_dir ();
591                 cfg = g_build_filename (home, ".mono", "assemblies", aname, cfg_name, NULL);
592                 got_it += mono_config_parse_file_with_context (&state, cfg);
593                 g_free (cfg);
594 #endif
595                 g_free (aname);
596                 if (got_it)
597                         break;
598         }
599         g_free (cfg_name);
600 }
601
602 /**
603  * mono_config_parse:
604  * @filename: the filename to load the configuration variables from.
605  *
606  * Pass a NULL filename to parse the default config files
607  * (or the file in the MONO_CONFIG env var).
608  */
609 void
610 mono_config_parse (const char *filename) {
611         const char *home;
612         char *mono_cfg;
613 #ifndef TARGET_WIN32
614         char *user_cfg;
615 #endif
616
617         if (filename) {
618                 mono_config_parse_file (filename);
619                 return;
620         }
621
622         home = g_getenv ("MONO_CONFIG");
623         if (home) {
624                 mono_config_parse_file (home);
625                 return;
626         }
627
628         mono_cfg = g_build_filename (mono_get_config_dir (), "mono", "config", NULL);
629         mono_config_parse_file (mono_cfg);
630         g_free (mono_cfg);
631
632 #if !defined(TARGET_WIN32) && !defined(__native_client__)
633         home = g_get_home_dir ();
634         user_cfg = g_strconcat (home, G_DIR_SEPARATOR_S, ".mono/config", NULL);
635         mono_config_parse_file (user_cfg);
636         g_free (user_cfg);
637 #endif
638 }
639
640 /* Invoked during startup */
641 void
642 mono_set_config_dir (const char *dir)
643 {
644         /* If this variable is set, overrides the directory computed */
645         mono_cfg_dir = g_getenv ("MONO_CFG_DIR");
646         if (mono_cfg_dir == NULL)
647                 mono_cfg_dir = mono_cfg_dir_allocated = g_strdup (dir);
648 }
649
650 const char* 
651 mono_get_config_dir (void)
652 {
653         if (mono_cfg_dir == NULL)
654                 mono_set_dirs (NULL, NULL);
655
656         return mono_cfg_dir;
657 }
658
659 void
660 mono_register_machine_config (const char *config_xml)
661 {
662         bundled_machine_config = config_xml;
663 }
664
665 const char *
666 mono_get_machine_config (void)
667 {
668         return bundled_machine_config;
669 }
670
671 static void
672 assembly_binding_end (gpointer user_data, const char *element_name)
673 {
674         ParserUserData *pud = user_data;
675
676         if (!strcmp (element_name, "dependentAssembly")) {
677                 if (pud->info_parsed && pud->info) {
678                         pud->info_parsed (pud->info, pud->user_data);
679                         g_free (pud->info->name);
680                         g_free (pud->info->culture);
681                 }
682         }
683 }
684
685 static void
686 publisher_policy_start (gpointer user_data,
687                 const gchar *element_name,
688                 const gchar **attribute_names,
689                 const gchar **attribute_values)
690 {
691         ParserUserData *pud;
692         MonoAssemblyBindingInfo *info;
693         int n;
694
695         pud = user_data;
696         info = pud->info;
697         if (!strcmp (element_name, "dependentAssembly")) {
698                 info->name = NULL;
699                 info->culture = NULL;
700                 info->has_old_version_bottom = FALSE;
701                 info->has_old_version_top = FALSE;
702                 info->has_new_version = FALSE;
703                 info->is_valid = FALSE;
704                 memset (&info->old_version_bottom, 0, sizeof (info->old_version_bottom));
705                 memset (&info->old_version_top, 0, sizeof (info->old_version_top));
706                 memset (&info->new_version, 0, sizeof (info->new_version));
707         } if (!strcmp (element_name, "assemblyIdentity")) {
708                 for (n = 0; attribute_names [n]; n++) {
709                         const gchar *attribute_name = attribute_names [n];
710                         
711                         if (!strcmp (attribute_name, "name"))
712                                 info->name = g_strdup (attribute_values [n]);
713                         else if (!strcmp (attribute_name, "publicKeyToken")) {
714                                 if (strlen (attribute_values [n]) == MONO_PUBLIC_KEY_TOKEN_LENGTH - 1)
715                                         g_strlcpy ((char *) info->public_key_token, attribute_values [n], MONO_PUBLIC_KEY_TOKEN_LENGTH);
716                         } else if (!strcmp (attribute_name, "culture")) {
717                                 if (!strcmp (attribute_values [n], "neutral"))
718                                         info->culture = g_strdup ("");
719                                 else
720                                         info->culture = g_strdup (attribute_values [n]);
721                         }
722                 }
723         } else if (!strcmp (element_name, "bindingRedirect")) {
724                 for (n = 0; attribute_names [n]; n++) {
725                         const gchar *attribute_name = attribute_names [n];
726
727                         if (!strcmp (attribute_name, "oldVersion")) {
728                                 gchar **numbers, **version, **versions;
729                                 gint major, minor, build, revision;
730
731                                 /* Invalid value */
732                                 if (!strcmp (attribute_values [n], ""))
733                                         return;
734                                 
735                                 versions = g_strsplit (attribute_values [n], "-", 2);
736                                 version = g_strsplit (*versions, ".", 4);
737
738                                 /* We assign the values to gint vars to do the checks */
739                                 numbers = version;
740                                 major = *numbers ? atoi (*numbers++) : -1;
741                                 minor = *numbers ? atoi (*numbers++) : -1;
742                                 build = *numbers ? atoi (*numbers++) : -1;
743                                 revision = *numbers ? atoi (*numbers) : -1;
744                                 g_strfreev (version);
745                                 if (major < 0 || minor < 0 || build < 0 || revision < 0) {
746                                         g_strfreev (versions);
747                                         return;
748                                 }
749
750                                 info->old_version_bottom.major = major;
751                                 info->old_version_bottom.minor = minor;
752                                 info->old_version_bottom.build = build;
753                                 info->old_version_bottom.revision = revision;
754                                 info->has_old_version_bottom = TRUE;
755
756                                 if (!*(versions + 1)) {
757                                         g_strfreev (versions);
758                                         continue;
759                                 }
760                                 
761                                 numbers = version = g_strsplit (*(versions + 1), ".", 4);
762                                 major = *numbers ? atoi (*numbers++) : -1;
763                                 minor = *numbers ? atoi (*numbers++) : -1;
764                                 build = *numbers ? atoi (*numbers++) : -1;
765                                 revision = *numbers ? atoi (*numbers) : 1;
766                                 g_strfreev (version);
767                                 if (major < 0 || minor < 0 || build < 0 || revision < 0) {
768                                         g_strfreev (versions);
769                                         return;
770                                 }
771
772                                 info->old_version_top.major = major;
773                                 info->old_version_top.minor = minor;
774                                 info->old_version_top.build = build;
775                                 info->old_version_top.revision = revision;
776                                 info->has_old_version_top = TRUE;
777
778                                 g_strfreev (versions);
779                         } else if (!strcmp (attribute_name, "newVersion")) {
780                                 gchar **numbers, **version;
781
782                                 /* Invalid value */
783                                 if (!strcmp (attribute_values [n], ""))
784                                         return;
785
786                                 numbers = version = g_strsplit (attribute_values [n], ".", 4);
787                                 info->new_version.major = *numbers ? atoi (*numbers++) : -1;
788                                 info->new_version.minor = *numbers ? atoi (*numbers++) : -1;
789                                 info->new_version.build = *numbers ? atoi (*numbers++) : -1;
790                                 info->new_version.revision = *numbers ? atoi (*numbers) : -1;
791                                 info->has_new_version = TRUE;
792                                 g_strfreev (version);
793                         }
794                 }
795         }
796 }
797
798 static MonoParseHandler
799 publisher_policy_parser = {
800         "", /* We don't need to use declare an xml element */
801         NULL,
802         publisher_policy_start,
803         NULL,
804         NULL,
805         NULL
806 };
807
808 void
809 mono_config_parse_publisher_policy (const gchar *filename, MonoAssemblyBindingInfo *info)
810 {
811         ParserUserData user_data = {
812                 info,
813                 NULL,
814                 NULL
815         };
816         ParseState state = {
817                 &publisher_policy_parser, /* MonoParseHandler */
818                 &user_data, /* user_data */
819                 NULL, /* MonoImage (we don't need it right now)*/
820                 TRUE /* We are already inited */
821         };
822         
823         mono_config_parse_file_with_context (&state, filename);
824 }
825
826 static MonoParseHandler
827 config_assemblybinding_parser = {
828         "", /* We don't need to use declare an xml element */
829         NULL,
830         publisher_policy_start,
831         NULL,
832         assembly_binding_end,
833         NULL
834 };
835
836 void
837 mono_config_parse_assembly_bindings (const char *filename, int amajor, int aminor, void *user_data, void (*infocb)(MonoAssemblyBindingInfo *info, void *user_data))
838 {
839         MonoAssemblyBindingInfo info;
840         ParserUserData pud;
841         ParseState state;
842
843         info.major = amajor;
844         info.minor = aminor;
845
846         pud.info = &info;
847         pud.info_parsed = infocb;
848         pud.user_data = user_data;
849
850         state.current = &config_assemblybinding_parser;  /* MonoParseHandler */
851         state.user_data = &pud;
852         state.assembly = NULL; /* MonoImage (we don't need it right now)*/
853         state.inited = TRUE; /* We are already inited */
854
855         mono_config_parse_file_with_context (&state, filename);
856 }
857
858 static mono_bool mono_server_mode = FALSE;
859
860 void
861 mono_config_set_server_mode (mono_bool server_mode)
862 {
863         mono_server_mode = server_mode;
864 }
865
866 mono_bool
867 mono_config_is_server_mode (void)
868 {
869         return mono_server_mode;
870 }
871