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