Give Steve Sommars' real name and work e-mail address.
[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$
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 #include <epan/report_err.h>
32
33 /* XML Stub routines */
34 #define IN_XMLSTUB
35 #include "xmlstub.h"
36
37 /*
38  * This routine will dynamically load libxml2 and will populate the
39  * XmlStub pointer structure.
40  *
41  * On any error, it will return non-zero, and it should be assumed that
42  * the current platform does not have dynamic library support, or does
43  * not have libxml2 installed.
44  */
45 int
46 loadLibXML(void)
47 {
48         GModule *handle;
49         gpointer symbol;
50         int error=FALSE;
51
52         if (XmlStubInitialized) {
53                 /* Did you ever get the feeling you've been here before? */
54
55                 /*
56                  * This is not thread safe.  With threads, we'd need to
57                  * synchronize all this so two threads can't initialize at once.
58                  */
59                 return 0;
60         }
61
62         /* Check to see if gmodule is supported */
63         if (!g_module_supported()) {
64                 g_warning("XMLStub: Modules are not supported.  Not initializing XML Stub");
65                 return (-1);
66         }
67
68         /* open the dll.  Is this named something different
69          * under windows?  Perhaps we should check . . .
70          */
71         if ((handle = g_module_open(XML_LIBRARY, G_MODULE_BIND_LAZY)) == NULL) {
72                 report_failure("XMLStub: Unable to open module " XML_LIBRARY );
73                 return (-1);
74         }
75
76         /*
77          * Now that the library is open, copy all our relevant
78          * function pointers and integer pointers into our structure.
79          */
80         if (!g_module_symbol(handle, "xmlParseFile", &symbol)) {
81                 g_warning("Unable to find \"xmlParseFile\"");
82                 error=TRUE;
83         }
84         XmlStub.xmlParseFile= (xmlDocPtr(*)(const char *))symbol;
85
86         if (!g_module_symbol(handle, "xmlStrcmp", &symbol)) {
87                 g_warning("Unable to find \"xmlStrcmp\"");
88                 error=TRUE;
89         }
90         XmlStub.xmlStrcmp= (int (*)(const xmlChar *, const xmlChar *))symbol;
91
92         if (!g_module_symbol(handle, "xmlCreatePushParserCtxt", &symbol)) {
93                 g_warning("Unable to find \"xmlCreatePushParserCtxt\"");
94                 error=TRUE;
95         }
96     XmlStub.xmlCreatePushParserCtxt=(xmlParserCtxtPtr (*)
97                                                                          (xmlSAXHandlerPtr, void *, const char *,
98                                                                           int, const char *)) symbol;
99
100         if (!g_module_symbol(handle, "xmlParseChunk", &symbol)) {
101                 g_warning("Unable to find \"xmlParseChunk\"");
102                 error=TRUE;
103         }
104         XmlStub.xmlParseChunk=(int (*)(xmlParserCtxtPtr, const char *, int, int))symbol;
105
106         if (!g_module_symbol(handle, "xmlFreeParserCtxt", &symbol)) {
107                 g_warning("Unable to find \"xmlFreeParserCtxt\"");
108                 error=TRUE;
109         }
110         XmlStub.xmlFreeParserCtxt=(void (*)(xmlParserCtxtPtr))symbol;
111
112         if (!g_module_symbol(handle, "xmlDocGetRootElement", &symbol)) {
113                 g_warning("Unable to find \"xmlDocGetRootElement\"");
114                 error=TRUE;
115         }
116         XmlStub.xmlDocGetRootElement=(xmlNodePtr(*)(xmlDocPtr))symbol;
117
118         if (!g_module_symbol(handle, "xmlFreeDoc", &symbol)) {
119                 g_warning("Unable to find \"xmlFreeDoc\"");
120                 error=TRUE;
121         }
122         XmlStub.xmlFreeDoc=(void (*)(xmlDocPtr))symbol;
123
124         if (!g_module_symbol(handle, "xmlNodeListGetString", &symbol)) {
125                 g_warning("Unable to find \"xmlNodeListGetString\"");
126                 error=TRUE;
127         }
128         XmlStub.xmlNodeListGetString=(char * (*)(xmlDocPtr, xmlNodePtr, int))symbol;
129
130         if (!g_module_symbol(handle, "xmlGetProp", &symbol)) {
131                 g_warning("Unable to find \"xmlGetProp\"");
132                 error=TRUE;
133         }
134         XmlStub.xmlGetProp=(char * (*)(xmlNodePtr, char *))symbol;
135
136         if (!g_module_symbol(handle, "xmlKeepBlanksDefault", &symbol)) {
137                 g_warning("Unable to find \"xmlKeepBlanksDefault\"");
138                 error=TRUE;
139         }
140         XmlStub.xmlKeepBlanksDefault=(int(*)(int))symbol;
141
142         if (!g_module_symbol(handle, "xmlSubstituteEntitiesDefault", &symbol)) {
143                 g_warning("Unable to find \"xmlSubstituteEntitiesDefault\"");
144                 error=TRUE;
145         }
146         XmlStub.xmlSubstituteEntitiesDefault=(int(*)(int))symbol;
147
148         if (!g_module_symbol(handle, "xmlDoValidityCheckingDefaultValue", &symbol)) {
149                 g_warning("Unable to find \"xmlDoValidityCheckingDefaultValue\"");
150                 error=TRUE;
151         }
152         XmlStub.xmlDoValidityCheckingDefaultValue = (int *)symbol;
153
154         /*
155          * Return if any of the above functions set our error flag.
156          * A flag was used, instead of returning immediately, so
157          * that *all* unresolved symbols would be printed.
158          */
159         if (error) {
160                 g_module_close(handle);
161                 return (-1);
162         }
163         /* Set our global so that we don't try to load twice */
164         XmlStubInitialized=1;
165
166         return 0; /* Success! */
167
168 } /* loadLibXML */