From 5d9e6f550a6813c4ff30e5f77cb49ea717ccc689 Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Wed, 17 Feb 2010 10:15:24 +0000 Subject: Core: add a lasso_xmlnode_to_string function * lasso/xml/tools.c lasso/xml/private.h: lots of functions duplicate this code, so we factorized it there. It has two parameters, the xmlnode and boolean deciding whether to format the resulting content (good for reading but bad for signatures). --- lasso/xml/private.h | 1 + lasso/xml/tools.c | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/lasso/xml/private.h b/lasso/xml/private.h index a8711b70..4ca174d8 100644 --- a/lasso/xml/private.h +++ b/lasso/xml/private.h @@ -227,6 +227,7 @@ xmlSecKey* lasso_xmlsec_load_private_key(const char *filename_or_buffer, const c xmlDocPtr lasso_xml_parse_file(const char *filepath); xmlDocPtr lasso_xml_parse_memory_with_error(const char *buffer, int size, xmlError *error); xmlSecKeyPtr lasso_xmlsec_load_key_info(xmlNode *key_descriptor); +char* lasso_xmlnode_to_string(xmlNode *node, gboolean format); #ifdef __cplusplus } diff --git a/lasso/xml/tools.c b/lasso/xml/tools.c index 2202f3c6..deb06801 100644 --- a/lasso/xml/tools.c +++ b/lasso/xml/tools.c @@ -2100,3 +2100,35 @@ next: cleanup: return result; } + +/** + * lasso_xmlnode_to_string: + * @xmlnode: an #xmlNode structure + * @format: whether to allow formatting (it break XML signatures) + * + * Transform an XML node to a C string + * + * Return value: a newly allocated C string + */ +char* +lasso_xmlnode_to_string(xmlNode *node, gboolean format) +{ + xmlOutputBufferPtr buf; + xmlCharEncodingHandlerPtr handler = NULL; + xmlChar *buffer; + char *str; + + if (! node) + return NULL; + + handler = xmlFindCharEncodingHandler("utf-8"); + buf = xmlAllocOutputBuffer(handler); + xmlNodeDumpOutput(buf, NULL, node, 0, format ? 1 : 0, "utf-8"); + xmlOutputBufferFlush(buf); + buffer = buf->conv ? buf->conv->content : buf->buffer->content; + /* do not mix XML and GLib strings, so we must copy */ + str = g_strdup((char*)buffer); + xmlOutputBufferClose(buf); + + return str; +} -- cgit