As with "file_write_error_message()", so with
[obnox/wireshark/wip.git] / xmlstub.c
1 /* xmlstub.c
2  * Routines to parse XML files using libxml2.  This stub
3  * exists so that the library can be loaded on systems that
4  * have it.
5  *
6  * $Id: xmlstub.c,v 1.3 2004/01/18 16:19:15 jmayer Exp $
7  *
8  * Copyright (c) 2001 by David Frascone <dave@frascone.com>
9  *
10  * Ethereal - Network traffic analyzer
11  * By Gerald Combs <gerald@ethereal.com>
12  * Copyright 1998-2001 Gerald Combs
13  *
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * as published by the Free Software Foundation; either version 2
17  * of the License, or (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
27  */
28
29 #include <glib.h>
30 #include <gmodule.h>
31
32 /* XML Stub routines */
33 #define IN_XMLSTUB
34 #include "xmlstub.h"
35
36 /*
37  * This routine will dynamically load libxml2 and will populate the
38  * XmlStub pointer structure.
39  *
40  * On any error, it will return non-zero, and it should be assumed that
41  * the current platform does not have dynamic library support, or does
42  * not have libxml2 installed.
43  */
44 int
45 loadLibXML(void)
46 {
47         GModule *handle;
48         gpointer symbol;
49         int error=FALSE;
50
51         if (XmlStubInitialized) {
52                 /* Did you ever get the feeling you've been here before? */
53
54                 /*
55                  * This is not thread safe.  With threads, we'd need to
56                  * synchronize all this so two threads can't initialize at once.
57                  */
58                 return 0;
59         }
60
61         /* Check to see if gmodule is supported */
62         if (!g_module_supported()) {
63                 g_warning("XMLStub: Modules are not supported.  Not initializing XML Stub");
64                 return (-1);
65         }
66
67         /* open the dll.  Is this named something different
68          * under windows?  Perhaps we should check . . .
69          */
70         if ((handle = g_module_open(XML_LIBRARY, G_MODULE_BIND_LAZY)) == NULL) {
71                 g_warning("XMLStub: Unable to open module " XML_LIBRARY);
72                 return (-1);
73         }
74
75         /*
76          * Now that the library is open, copy all our relevant
77          * function pointers and integer pointers into our structure.
78          */
79         if (!g_module_symbol(handle, "xmlParseFile", &symbol)) {
80                 g_warning("Unable to find \"xmlParseFile\"");
81                 error=TRUE;
82         }
83         XmlStub.xmlParseFile= (xmlDocPtr(*)(const char *))symbol;
84
85         if (!g_module_symbol(handle, "xmlStrcmp", &symbol)) {
86                 g_warning("Unable to find \"xmlStrcmp\"");
87                 error=TRUE;
88         }
89         XmlStub.xmlStrcmp= (int (*)(const xmlChar *, const xmlChar *))symbol;
90
91         if (!g_module_symbol(handle, "xmlCreatePushParserCtxt", &symbol)) {
92                 g_warning("Unable to find \"xmlCreatePushParserCtxt\"");
93                 error=TRUE;
94         }
95     XmlStub.xmlCreatePushParserCtxt=(xmlParserCtxtPtr (*)
96                                                                          (xmlSAXHandlerPtr, void *, const char *,
97                                                                           int, const char *)) symbol;
98
99         if (!g_module_symbol(handle, "xmlParseChunk", &symbol)) {
100                 g_warning("Unable to find \"xmlParseChunk\"");
101                 error=TRUE;
102         }
103         XmlStub.xmlParseChunk=(int (*)(xmlParserCtxtPtr, const char *, int, int))symbol;
104
105         if (!g_module_symbol(handle, "xmlFreeParserCtxt", &symbol)) {
106                 g_warning("Unable to find \"xmlFreeParserCtxt\"");
107                 error=TRUE;
108         }
109         XmlStub.xmlFreeParserCtxt=(void (*)(xmlParserCtxtPtr))symbol;
110
111         if (!g_module_symbol(handle, "xmlDocGetRootElement", &symbol)) {
112                 g_warning("Unable to find \"xmlDocGetRootElement\"");
113                 error=TRUE;
114         }
115         XmlStub.xmlDocGetRootElement=(xmlNodePtr(*)(xmlDocPtr))symbol;
116
117         if (!g_module_symbol(handle, "xmlFreeDoc", &symbol)) {
118                 g_warning("Unable to find \"xmlFreeDoc\"");
119                 error=TRUE;
120         }
121         XmlStub.xmlFreeDoc=(void (*)(xmlDocPtr))symbol;
122
123         if (!g_module_symbol(handle, "xmlNodeListGetString", &symbol)) {
124                 g_warning("Unable to find \"xmlNodeListGetString\"");
125                 error=TRUE;
126         }
127         XmlStub.xmlNodeListGetString=(char * (*)(xmlDocPtr, xmlNodePtr, int))symbol;
128
129         if (!g_module_symbol(handle, "xmlGetProp", &symbol)) {
130                 g_warning("Unable to find \"xmlGetProp\"");
131                 error=TRUE;
132         }
133         XmlStub.xmlGetProp=(char * (*)(xmlNodePtr, char *))symbol;
134
135         if (!g_module_symbol(handle, "xmlKeepBlanksDefault", &symbol)) {
136                 g_warning("Unable to find \"xmlKeepBlanksDefault\"");
137                 error=TRUE;
138         }
139         XmlStub.xmlKeepBlanksDefault=(int(*)(int))symbol;
140
141         if (!g_module_symbol(handle, "xmlSubstituteEntitiesDefault", &symbol)) {
142                 g_warning("Unable to find \"xmlSubstituteEntitiesDefault\"");
143                 error=TRUE;
144         }
145         XmlStub.xmlSubstituteEntitiesDefault=(int(*)(int))symbol;
146
147         if (!g_module_symbol(handle, "xmlDoValidityCheckingDefaultValue", &symbol)) {
148                 g_warning("Unable to find \"xmlDoValidityCheckingDefaultValue\"");
149                 error=TRUE;
150         }
151         XmlStub.xmlDoValidityCheckingDefaultValue = (int *)symbol;
152
153         /*
154          * Return if any of the above functions set our error flag.
155          * A flag was used, instead of returning immediately, so
156          * that *all* unresolved symbols would be printed.
157          */
158         if (error) {
159                 g_module_close(handle);
160                 return (-1);
161         }
162         /* Set our global so that we don't try to load twice */
163         XmlStubInitialized=1;
164
165         return 0; /* Success! */
166
167 } /* loadLibXML */