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