Fix another bunch of shutdown leaks.
[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__)
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__)
51 #define CONFIG_CPU "x86"
52 #define CONFIG_WORDSIZE "32"
53 #elif defined(__x86_64__)
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         g_hash_table_destroy (config_handlers);
378         g_free (mono_cfg_dir_allocated);
379 }
380
381 /* FIXME: error handling */
382
383 static void
384 mono_config_parse_xml_with_context (ParseState *state, const char *text, gsize len)
385 {
386         GMarkupParseContext *context;
387
388         if (!inited)
389                 mono_config_init ();
390
391         context = g_markup_parse_context_new (&mono_parser, 0, state, NULL);
392         if (g_markup_parse_context_parse (context, text, len, NULL)) {
393                 g_markup_parse_context_end_parse (context, NULL);
394         }
395         g_markup_parse_context_free (context);
396 }
397
398 /* If assembly is NULL, parse in the global context */
399 static int
400 mono_config_parse_file_with_context (ParseState *state, const char *filename)
401 {
402         gchar *text;
403         gsize len;
404         gint offset;
405
406         mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_CONFIG,
407                         "Config attempting to parse: '%s'.", filename);
408
409         if (!g_file_get_contents (filename, &text, &len, NULL))
410                 return 0;
411
412
413         offset = 0;
414         if (len > 3 && text [0] == '\xef' && text [1] == (gchar) '\xbb' && text [2] == '\xbf')
415                 offset = 3; /* Skip UTF-8 BOM */
416         if (state->user_data == NULL)
417                 state->user_data = (gpointer) filename;
418         mono_config_parse_xml_with_context (state, text + offset, len - offset);
419         g_free (text);
420         return 1;
421 }
422
423 /**
424  * mono_config_parse_memory:
425  * @buffer: a pointer to an string XML representation of the configuration
426  *
427  * Parses the configuration from a buffer
428  */
429 void
430 mono_config_parse_memory (const char *buffer)
431 {
432         ParseState state = {NULL};
433
434         state.user_data = (gpointer) "<buffer>";
435         mono_config_parse_xml_with_context (&state, buffer, strlen (buffer));
436 }
437
438 static void
439 mono_config_parse_file (const char *filename)
440 {
441         ParseState state = {NULL};
442         state.user_data = (gpointer) filename;
443         mono_config_parse_file_with_context (&state, filename);
444 }
445
446 /* 
447  * use the equivalent lookup code from the GAC when available.
448  * Depending on state, this should give something like:
449  *      aname/version-pubtoken/
450  *      aname/version/
451  *      aname
452  */
453 static char*
454 get_assembly_filename (MonoImage *image, int state)
455 {
456         switch (state) {
457         case 0:
458                 return g_strdup (mono_image_get_name (image));
459         default:
460                 return NULL;
461         }
462 }
463
464 typedef struct _BundledConfig BundledConfig;
465
466 struct _BundledConfig {
467         BundledConfig *next;
468         const char* aname;
469         const char* config_xml;
470 };
471
472 static BundledConfig *bundled_configs = NULL;
473
474 static const char *bundled_machine_config = NULL;
475
476 void
477 mono_register_config_for_assembly (const char* assembly_name, const char* config_xml)
478 {
479         BundledConfig *bconfig;
480
481         bconfig = g_new0 (BundledConfig, 1);
482         bconfig->aname = assembly_name;
483         bconfig->config_xml = config_xml;
484         bconfig->next = bundled_configs;
485         bundled_configs = bconfig;
486 }
487
488 const char *
489 mono_config_string_for_assembly_file (const char *filename)
490 {
491         BundledConfig *bconfig;
492         
493         for (bconfig = bundled_configs; bconfig; bconfig = bconfig->next) {
494                 if (bconfig->aname && strcmp (bconfig->aname, filename) == 0)
495                         return bconfig->config_xml;
496         }
497         return NULL;
498 }
499
500 void 
501 mono_config_for_assembly (MonoImage *assembly)
502 {
503         ParseState state = {NULL};
504         int got_it = 0, i;
505         char *aname, *cfg, *cfg_name;
506         const char *bundled_config;
507         const char *home;
508         
509         state.assembly = assembly;
510
511         bundled_config = mono_config_string_for_assembly_file (assembly->module_name);
512         if (bundled_config) {
513                 state.user_data = (gpointer) "<bundled>";
514                 mono_config_parse_xml_with_context (&state, bundled_config, strlen (bundled_config));
515         }
516
517         cfg_name = g_strdup_printf ("%s.config", mono_image_get_filename (assembly));
518         mono_config_parse_file_with_context (&state, cfg_name);
519         g_free (cfg_name);
520
521         cfg_name = g_strdup_printf ("%s.config", mono_image_get_name (assembly));
522
523         home = g_get_home_dir ();
524
525         for (i = 0; (aname = get_assembly_filename (assembly, i)) != NULL; ++i) {
526                 cfg = g_build_filename (mono_get_config_dir (), "mono", "assemblies", aname, cfg_name, NULL);
527                 got_it += mono_config_parse_file_with_context (&state, cfg);
528                 g_free (cfg);
529
530 #ifdef TARGET_WIN32
531                 cfg = g_build_filename (home, ".mono", "assemblies", aname, cfg_name, NULL);
532                 got_it += mono_config_parse_file_with_context (&state, cfg);
533                 g_free (cfg);
534 #endif
535                 g_free (aname);
536                 if (got_it)
537                         break;
538         }
539         g_free (cfg_name);
540 }
541
542 /**
543  * mono_config_parse:
544  * @filename: the filename to load the configuration variables from.
545  *
546  * Pass a NULL filename to parse the default config files
547  * (or the file in the MONO_CONFIG env var).
548  */
549 void
550 mono_config_parse (const char *filename) {
551         const char *home;
552         char *mono_cfg;
553 #ifndef TARGET_WIN32
554         char *user_cfg;
555 #endif
556
557         if (filename) {
558                 mono_config_parse_file (filename);
559                 return;
560         }
561
562         home = g_getenv ("MONO_CONFIG");
563         if (home) {
564                 mono_config_parse_file (home);
565                 return;
566         }
567
568         mono_cfg = g_build_filename (mono_get_config_dir (), "mono", "config", NULL);
569         mono_config_parse_file (mono_cfg);
570         g_free (mono_cfg);
571
572 #ifndef TARGET_WIN32
573         home = g_get_home_dir ();
574         user_cfg = g_strconcat (home, G_DIR_SEPARATOR_S, ".mono/config", NULL);
575         mono_config_parse_file (user_cfg);
576         g_free (user_cfg);
577 #endif
578 }
579
580 /* Invoked during startup */
581 void
582 mono_set_config_dir (const char *dir)
583 {
584         /* If this variable is set, overrides the directory computed */
585         mono_cfg_dir = g_getenv ("MONO_CFG_DIR");
586         if (mono_cfg_dir == NULL)
587                 mono_cfg_dir = mono_cfg_dir_allocated = g_strdup (dir);
588 }
589
590 const char* 
591 mono_get_config_dir (void)
592 {
593         if (mono_cfg_dir == NULL)
594                 mono_set_dirs (NULL, NULL);
595
596         return mono_cfg_dir;
597 }
598
599 void
600 mono_register_machine_config (const char *config_xml)
601 {
602         bundled_machine_config = config_xml;
603 }
604
605 const char *
606 mono_get_machine_config (void)
607 {
608         return bundled_machine_config;
609 }
610
611 static void
612 assembly_binding_end (gpointer user_data, const char *element_name)
613 {
614         ParserUserData *pud = user_data;
615
616         if (!strcmp (element_name, "dependentAssembly")) {
617                 if (pud->info_parsed && pud->info) {
618                         pud->info_parsed (pud->info, pud->user_data);
619                         g_free (pud->info->name);
620                         g_free (pud->info->culture);
621                 }
622         }
623 }
624
625 static void
626 publisher_policy_start (gpointer user_data,
627                 const gchar *element_name,
628                 const gchar **attribute_names,
629                 const gchar **attribute_values)
630 {
631         ParserUserData *pud;
632         MonoAssemblyBindingInfo *info;
633         int n;
634
635         pud = user_data;
636         info = pud->info;
637         if (!strcmp (element_name, "dependentAssembly")) {
638                 info->name = NULL;
639                 info->culture = NULL;
640                 info->has_old_version_bottom = FALSE;
641                 info->has_old_version_top = FALSE;
642                 info->has_new_version = FALSE;
643                 info->is_valid = FALSE;
644                 memset (&info->old_version_bottom, 0, sizeof (info->old_version_bottom));
645                 memset (&info->old_version_top, 0, sizeof (info->old_version_top));
646                 memset (&info->new_version, 0, sizeof (info->new_version));
647         } if (!strcmp (element_name, "assemblyIdentity")) {
648                 for (n = 0; attribute_names [n]; n++) {
649                         const gchar *attribute_name = attribute_names [n];
650                         
651                         if (!strcmp (attribute_name, "name"))
652                                 info->name = g_strdup (attribute_values [n]);
653                         else if (!strcmp (attribute_name, "publicKeyToken")) {
654                                 if (strlen (attribute_values [n]) == MONO_PUBLIC_KEY_TOKEN_LENGTH - 1)
655                                         g_strlcpy ((char *) info->public_key_token, attribute_values [n], MONO_PUBLIC_KEY_TOKEN_LENGTH);
656                         } else if (!strcmp (attribute_name, "culture")) {
657                                 if (!strcmp (attribute_values [n], "neutral"))
658                                         info->culture = g_strdup ("");
659                                 else
660                                         info->culture = g_strdup (attribute_values [n]);
661                         }
662                 }
663         } else if (!strcmp (element_name, "bindingRedirect")) {
664                 for (n = 0; attribute_names [n]; n++) {
665                         const gchar *attribute_name = attribute_names [n];
666
667                         if (!strcmp (attribute_name, "oldVersion")) {
668                                 gchar **numbers, **version, **versions;
669                                 gint major, minor, build, revision;
670
671                                 /* Invalid value */
672                                 if (!strcmp (attribute_values [n], ""))
673                                         return;
674                                 
675                                 versions = g_strsplit (attribute_values [n], "-", 2);
676                                 version = g_strsplit (*versions, ".", 4);
677
678                                 /* We assign the values to gint vars to do the checks */
679                                 numbers = version;
680                                 major = *numbers ? atoi (*numbers++) : -1;
681                                 minor = *numbers ? atoi (*numbers++) : -1;
682                                 build = *numbers ? atoi (*numbers++) : -1;
683                                 revision = *numbers ? atoi (*numbers) : -1;
684                                 g_strfreev (version);
685                                 if (major < 0 || minor < 0 || build < 0 || revision < 0) {
686                                         g_strfreev (versions);
687                                         return;
688                                 }
689
690                                 info->old_version_bottom.major = major;
691                                 info->old_version_bottom.minor = minor;
692                                 info->old_version_bottom.build = build;
693                                 info->old_version_bottom.revision = revision;
694                                 info->has_old_version_bottom = TRUE;
695
696                                 if (!*(versions + 1)) {
697                                         g_strfreev (versions);
698                                         continue;
699                                 }
700                                 
701                                 numbers = version = g_strsplit (*(versions + 1), ".", 4);
702                                 major = *numbers ? atoi (*numbers++) : -1;
703                                 minor = *numbers ? atoi (*numbers++) : -1;
704                                 build = *numbers ? atoi (*numbers++) : -1;
705                                 revision = *numbers ? atoi (*numbers) : 1;
706                                 g_strfreev (version);
707                                 if (major < 0 || minor < 0 || build < 0 || revision < 0) {
708                                         g_strfreev (versions);
709                                         return;
710                                 }
711
712                                 info->old_version_top.major = major;
713                                 info->old_version_top.minor = minor;
714                                 info->old_version_top.build = build;
715                                 info->old_version_top.revision = revision;
716                                 info->has_old_version_top = TRUE;
717
718                                 g_strfreev (versions);
719                         } else if (!strcmp (attribute_name, "newVersion")) {
720                                 gchar **numbers, **version;
721
722                                 /* Invalid value */
723                                 if (!strcmp (attribute_values [n], ""))
724                                         return;
725
726                                 numbers = version = g_strsplit (attribute_values [n], ".", 4);
727                                 info->new_version.major = *numbers ? atoi (*numbers++) : -1;
728                                 info->new_version.minor = *numbers ? atoi (*numbers++) : -1;
729                                 info->new_version.build = *numbers ? atoi (*numbers++) : -1;
730                                 info->new_version.revision = *numbers ? atoi (*numbers) : -1;
731                                 info->has_new_version = TRUE;
732                                 g_strfreev (version);
733                         }
734                 }
735         }
736 }
737
738 static MonoParseHandler
739 publisher_policy_parser = {
740         "", /* We don't need to use declare an xml element */
741         NULL,
742         publisher_policy_start,
743         NULL,
744         NULL,
745         NULL
746 };
747
748 void
749 mono_config_parse_publisher_policy (const gchar *filename, MonoAssemblyBindingInfo *info)
750 {
751         ParserUserData user_data = {
752                 info,
753                 NULL,
754                 NULL
755         };
756         ParseState state = {
757                 &publisher_policy_parser, /* MonoParseHandler */
758                 &user_data, /* user_data */
759                 NULL, /* MonoImage (we don't need it right now)*/
760                 TRUE /* We are already inited */
761         };
762         
763         mono_config_parse_file_with_context (&state, filename);
764 }
765
766 static MonoParseHandler
767 config_assemblybinding_parser = {
768         "", /* We don't need to use declare an xml element */
769         NULL,
770         publisher_policy_start,
771         NULL,
772         assembly_binding_end,
773         NULL
774 };
775
776 void
777 mono_config_parse_assembly_bindings (const char *filename, int amajor, int aminor, void *user_data, void (*infocb)(MonoAssemblyBindingInfo *info, void *user_data))
778 {
779         MonoAssemblyBindingInfo info;
780         ParserUserData pud;
781         ParseState state;
782
783         info.major = amajor;
784         info.minor = aminor;
785
786         pud.info = &info;
787         pud.info_parsed = infocb;
788         pud.user_data = user_data;
789
790         state.current = &config_assemblybinding_parser;  /* MonoParseHandler */
791         state.user_data = &pud;
792         state.assembly = NULL; /* MonoImage (we don't need it right now)*/
793         state.inited = TRUE; /* We are already inited */
794
795         mono_config_parse_file_with_context (&state, filename);
796 }
797