Ethereal-dev: [Ethereal-dev] Dynamically loading libxml2

Note: This archive is from the project's previous web site, ethereal.com. This list is no longer active.

From: David Frascone <dave@xxxxxxxxxxxx>
Date: Wed, 30 May 2001 23:52:34 -0500
Ok, I'm almost finished with my testing.  The diameter dissector is going to
depend on libxml2 (from http://www.xmlsoft.org) to operate at it's fullest
extent.  It will load the library with the code below.  That way, it will
build without depending on libxml, but will not decode the AVPs very 
nicely without it.

Any comments?


Code to dynamically load libxml2.  (xmlstub.h is all of the types defined
in the real libxml2 header files.
----------------------------------------------------------
#include "stubxml.h"

xmlDocPtr	(*xmlParseFile)(const char *filename);
int	(*xmlStrcmp		)(const xmlChar *str1, const xmlChar *str2);
xmlParserCtxtPtr (*xmlCreatePushParserCtxt)(xmlSAXHandlerPtr, void *, const char *,
											int, const char *);
int (*xmlParseChunk)(xmlParserCtxtPtr, const char *, int, int);
void (*xmlFreeParserCtxt)(xmlParserCtxtPtr);
xmlNodePtr (*xmlDocGetRootElement)(xmlDocPtr);
void (*xmlFreeDoc)(xmlDocPtr);
char *(*xmlNodeListGetString)(xmlDocPtr, xmlNodePtr, int);
char *(*xmlGetProp)(xmlNodePtr, char *);
int (*xmlKeepBlanksDefault)(int);

int *xmlDoValidityCheckingDefaultValue;

static int
loadLibXML()
{
	void *handle;

	if ((handle = dlopen("libxml2.so", RTLD_LAZY)) == NULL) {
		fprintf(stderr, "Unable to dlopen \"libxml2\"\n");
		return (-1);
	}

	if ((xmlParseFile= (xmlDocPtr(*)(const char *))
		 dlsym(handle, "xmlParseFile")) == NULL) {
		fprintf(stderr, "Unable to find \"xmlParseFile\"\n");
		dlclose(handle);
		return (-1);
	}
	
	if ((xmlStrcmp= (int (*)(const xmlChar *, const xmlChar *))
		 dlsym(handle, "xmlStrcmp")) == NULL) {
		fprintf(stderr, "Unable to find \"xmlStrcmp\"\n");
		dlclose(handle);
		return (-1);
	}

	if ((xmlCreatePushParserCtxt=(xmlParserCtxtPtr (*)
								  (xmlSAXHandlerPtr, void *, const char *,
								   int, const char *))
		 dlsym(handle, "xmlCreatePushParserCtxt")) == NULL) {
		fprintf(stderr, "Unable to find \"xmlCreatePushParserCtxt\"\n");
		dlclose(handle);
		return (-1);
	}

	if ((xmlParseChunk=(int (*)(xmlParserCtxtPtr, const char *, int, int))
		 dlsym(handle, "xmlParseChunk")) == NULL) {
		fprintf(stderr, "Unable to find \"xmlParseChunk\"\n");
		dlclose(handle);
		return (-1);
	}

	if ((xmlFreeParserCtxt=(void (*)(xmlParserCtxtPtr))
		 dlsym(handle, "xmlFreeParserCtxt")) == NULL) {
		fprintf(stderr, "Unable to find \"xmlFreeParserCtxt\"\n");
		dlclose(handle);
		return (-1);
	}

	if ((xmlDocGetRootElement=(xmlNodePtr(*)(xmlDocPtr))
		 dlsym(handle, "xmlDocGetRootElement")) == NULL) {
		fprintf(stderr, "Unable to find \"xmlDocGetRootElement\"\n");
		dlclose(handle);
		return (-1);
	}

	if ((xmlFreeDoc=(void (*)(xmlDocPtr))
		 dlsym(handle, "xmlFreeDoc")) == NULL) {
		fprintf(stderr, "Unable to find \"xmlFreeDoc\"\n");
		dlclose(handle);
		return (-1);
	}

	if ((xmlNodeListGetString=(char * (*)(xmlDocPtr, xmlNodePtr, int))
		 dlsym(handle, "xmlNodeListGetString")) == NULL) {
		fprintf(stderr, "Unable to find \"xmlNodeListGetString\"\n");
		dlclose(handle);
		return (-1);
	}

	if ((xmlGetProp=(char * (*)(xmlNodePtr, char *))
		 dlsym(handle, "xmlGetProp")) == NULL) {
		fprintf(stderr, "Unable to find \"xmlGetProp\"\n");
		dlclose(handle);
		return (-1);
	}

	if ((xmlKeepBlanksDefault=(int(*)(int))
		 dlsym(handle, "xmlKeepBlanksDefault")) == NULL) {
		fprintf(stderr, "Unable to find \"xmlKeepBlanksDefault\"\n");
		dlclose(handle);
		return (-1);
	}
	if ((xmlDoValidityCheckingDefaultValue = (int *)
		 dlsym(handle, "xmlDoValidityCheckingDefaultValue")) == NULL) {
		fprintf(stderr, "Unable to find \"xmlDoValidityCheckingDefaultValue\"\n");
		dlclose(handle);
		return (-1);
	}

	return 0;

} /* loadLibXML */