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