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