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