"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "util/xmlutils.c" between
gvm-libs-11.0.0.tar.gz and gvm-libs-11.0.1.tar.gz

About: GVM Libraries for the Greenbone Vulnerability Management (GVM-10) framework (derived from the former openvas-libraries).

xmlutils.c  (gvm-libs-11.0.0):xmlutils.c  (gvm-libs-11.0.1)
skipping to change at line 36 skipping to change at line 36
* There are examples of using this interface in omp.c. * There are examples of using this interface in omp.c.
*/ */
#include "xmlutils.h" #include "xmlutils.h"
#include <assert.h> /* for assert */ #include <assert.h> /* for assert */
#include <errno.h> /* for errno, EAGAIN, EINTR */ #include <errno.h> /* for errno, EAGAIN, EINTR */
#include <fcntl.h> /* for fcntl, F_SETFL, O_NONBLOCK */ #include <fcntl.h> /* for fcntl, F_SETFL, O_NONBLOCK */
#include <glib.h> /* for g_free, GSList, g_markup_parse_context_free */ #include <glib.h> /* for g_free, GSList, g_markup_parse_context_free */
#include <glib/gtypes.h> /* for GPOINTER_TO_INT, GINT_TO_POINTER, gsize */ #include <glib/gtypes.h> /* for GPOINTER_TO_INT, GINT_TO_POINTER, gsize */
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <string.h> /* for strcmp, strerror, strlen */ #include <string.h> /* for strcmp, strerror, strlen */
#include <time.h> /* for time, time_t */ #include <time.h> /* for time, time_t */
#include <unistd.h> /* for ssize_t */ #include <unistd.h> /* for ssize_t */
#undef G_LOG_DOMAIN #undef G_LOG_DOMAIN
/** /**
* @brief GLib log domain. * @brief GLib log domain.
*/ */
#define G_LOG_DOMAIN "lib xml" #define G_LOG_DOMAIN "lib xml"
skipping to change at line 275 skipping to change at line 277
* @brief Handle the start of an OMP XML element. * @brief Handle the start of an OMP XML element.
* *
* @param[in] context Parser context. * @param[in] context Parser context.
* @param[in] element_name XML element name. * @param[in] element_name XML element name.
* @param[in] attribute_names XML attribute name. * @param[in] attribute_names XML attribute name.
* @param[in] attribute_values XML attribute values. * @param[in] attribute_values XML attribute values.
* @param[in] user_data Dummy parameter. * @param[in] user_data Dummy parameter.
* @param[in] error Error parameter. * @param[in] error Error parameter.
*/ */
static void static void
ignore_start_element (GMarkupParseContext *context, const gchar *element_name,
const gchar **attribute_names,
const gchar **attribute_values, gpointer user_data,
GError **error)
{
context_data_t *data = (context_data_t *) user_data;
(void) context;
(void) element_name;
(void) attribute_names;
(void) attribute_values;
(void) error;
data->current = GINT_TO_POINTER (GPOINTER_TO_INT (data->current) + 1);
}
/**
* @brief Handle the start of an OMP XML element.
*
* @param[in] context Parser context.
* @param[in] element_name XML element name.
* @param[in] attribute_names XML attribute name.
* @param[in] attribute_values XML attribute values.
* @param[in] user_data Dummy parameter.
* @param[in] error Error parameter.
*/
static void
handle_start_element (GMarkupParseContext *context, const gchar *element_name, handle_start_element (GMarkupParseContext *context, const gchar *element_name,
const gchar **attribute_names, const gchar **attribute_names,
const gchar **attribute_values, gpointer user_data, const gchar **attribute_values, gpointer user_data,
GError **error) GError **error)
{ {
entity_t entity; entity_t entity;
context_data_t *data = (context_data_t *) user_data; context_data_t *data = (context_data_t *) user_data;
(void) context; (void) context;
(void) error; (void) error;
skipping to change at line 328 skipping to change at line 357
/** /**
* @brief Handle the end of an XML element. * @brief Handle the end of an XML element.
* *
* @param[in] context Parser context. * @param[in] context Parser context.
* @param[in] element_name XML element name. * @param[in] element_name XML element name.
* @param[in] user_data Dummy parameter. * @param[in] user_data Dummy parameter.
* @param[in] error Error parameter. * @param[in] error Error parameter.
*/ */
static void static void
ignore_end_element (GMarkupParseContext *context, const gchar *element_name,
gpointer user_data, GError **error)
{
context_data_t *data = (context_data_t *) user_data;
(void) context;
(void) element_name;
(void) error;
data->current = GINT_TO_POINTER (GPOINTER_TO_INT (data->current) - 1);
if (data->current == NULL)
data->done = TRUE;
}
/**
* @brief Handle the end of an XML element.
*
* @param[in] context Parser context.
* @param[in] element_name XML element name.
* @param[in] user_data Dummy parameter.
* @param[in] error Error parameter.
*/
static void
handle_end_element (GMarkupParseContext *context, const gchar *element_name, handle_end_element (GMarkupParseContext *context, const gchar *element_name,
gpointer user_data, GError **error) gpointer user_data, GError **error)
{ {
context_data_t *data = (context_data_t *) user_data; context_data_t *data = (context_data_t *) user_data;
(void) context; (void) context;
(void) error; (void) error;
(void) element_name; (void) element_name;
assert (data->current && data->first); assert (data->current && data->first);
if (data->current == data->first) if (data->current == data->first)
skipping to change at line 379 skipping to change at line 431
/** /**
* @brief Handle additional text of an XML element. * @brief Handle additional text of an XML element.
* *
* @param[in] context Parser context. * @param[in] context Parser context.
* @param[in] text The text. * @param[in] text The text.
* @param[in] text_len Length of the text. * @param[in] text_len Length of the text.
* @param[in] user_data Dummy parameter. * @param[in] user_data Dummy parameter.
* @param[in] error Error parameter. * @param[in] error Error parameter.
*/ */
static void static void
ignore_text (GMarkupParseContext *context, const gchar *text, gsize text_len,
gpointer user_data, GError **error)
{
(void) context;
(void) text;
(void) text_len;
(void) user_data;
(void) error;
}
/**
* @brief Handle additional text of an XML element.
*
* @param[in] context Parser context.
* @param[in] text The text.
* @param[in] text_len Length of the text.
* @param[in] user_data Dummy parameter.
* @param[in] error Error parameter.
*/
static void
handle_text (GMarkupParseContext *context, const gchar *text, gsize text_len, handle_text (GMarkupParseContext *context, const gchar *text, gsize text_len,
gpointer user_data, GError **error) gpointer user_data, GError **error)
{ {
context_data_t *data = (context_data_t *) user_data; context_data_t *data = (context_data_t *) user_data;
(void) context; (void) context;
(void) text_len; (void) text_len;
(void) error; (void) error;
entity_t current = (entity_t) data->current->data; entity_t current = (entity_t) data->current->data;
if (current->text) if (current->text)
skipping to change at line 488 skipping to change at line 560
if (string_return == NULL) if (string_return == NULL)
string = NULL; string = NULL;
else if (*string_return == NULL) else if (*string_return == NULL)
string = g_string_new (""); string = g_string_new ("");
else else
string = *string_return; string = *string_return;
/* Create the XML parser. */ /* Create the XML parser. */
xml_parser.start_element = handle_start_element; if (entity)
xml_parser.end_element = handle_end_element; {
xml_parser.text = handle_text; xml_parser.start_element = handle_start_element;
xml_parser.end_element = handle_end_element;
xml_parser.text = handle_text;
}
else
{
xml_parser.start_element = ignore_start_element;
xml_parser.end_element = ignore_end_element;
xml_parser.text = ignore_text;
}
xml_parser.passthrough = NULL; xml_parser.passthrough = NULL;
xml_parser.error = handle_error; xml_parser.error = handle_error;
context_data_t context_data; context_data_t context_data;
context_data.done = FALSE; context_data.done = FALSE;
context_data.first = NULL; context_data.first = NULL;
context_data.current = NULL; context_data.current = NULL;
/* Setup the XML context. */ /* Setup the XML context. */
skipping to change at line 626 skipping to change at line 707
{ {
free_entity (context_data.first->data); free_entity (context_data.first->data);
g_slist_free_1 (context_data.first); g_slist_free_1 (context_data.first);
} }
if (timeout > 0) if (timeout > 0)
fcntl (socket, F_SETFL, 0L); fcntl (socket, F_SETFL, 0L);
g_markup_parse_context_free (xml_context); g_markup_parse_context_free (xml_context);
g_free (buffer); g_free (buffer);
return -2; return -2;
} }
*entity = (entity_t) context_data.first->data; if (entity)
*entity = (entity_t) context_data.first->data;
if (string) if (string)
*string_return = string; *string_return = string;
if (timeout > 0) if (timeout > 0)
fcntl (socket, F_SETFL, 0L); fcntl (socket, F_SETFL, 0L);
g_markup_parse_context_free (xml_context); g_markup_parse_context_free (xml_context);
g_free (buffer); g_free (buffer);
return 0; return 0;
} }
if ((timeout > 0) && (time (&last_time) == -1)) if ((timeout > 0) && (time (&last_time) == -1))
skipping to change at line 706 skipping to change at line 788
if (string_return == NULL) if (string_return == NULL)
string = NULL; string = NULL;
else if (*string_return == NULL) else if (*string_return == NULL)
string = g_string_new (""); string = g_string_new ("");
else else
string = *string_return; string = *string_return;
/* Create the XML parser. */ /* Create the XML parser. */
xml_parser.start_element = handle_start_element; if (entity)
xml_parser.end_element = handle_end_element; {
xml_parser.text = handle_text; xml_parser.start_element = handle_start_element;
xml_parser.end_element = handle_end_element;
xml_parser.text = handle_text;
}
else
{
xml_parser.start_element = ignore_start_element;
xml_parser.end_element = ignore_end_element;
xml_parser.text = ignore_text;
}
xml_parser.passthrough = NULL; xml_parser.passthrough = NULL;
xml_parser.error = handle_error; xml_parser.error = handle_error;
context_data_t context_data; context_data_t context_data;
context_data.done = FALSE; context_data.done = FALSE;
context_data.first = NULL; context_data.first = NULL;
context_data.current = NULL; context_data.current = NULL;
/* Setup the XML context. */ /* Setup the XML context. */
skipping to change at line 749 skipping to change at line 840
{ {
/* Server still busy, either timeout or try read again. */ /* Server still busy, either timeout or try read again. */
if ((timeout - (time (NULL) - last_time)) <= 0) if ((timeout - (time (NULL) - last_time)) <= 0)
{ {
g_warning (" timeout\n"); g_warning (" timeout\n");
if (fcntl (socket, F_SETFL, 0L) < 0) if (fcntl (socket, F_SETFL, 0L) < 0)
g_warning ("%s :failed to set socket flag: %s", g_warning ("%s :failed to set socket flag: %s",
__FUNCTION__, strerror (errno)); __FUNCTION__, strerror (errno));
g_markup_parse_context_free (xml_context); g_markup_parse_context_free (xml_context);
g_free (buffer); g_free (buffer);
if (string && *string_return == NULL)
g_string_free (string, TRUE);
return -4; return -4;
} }
} }
continue; continue;
} }
if (context_data.first && context_data.first->data) if (context_data.first && context_data.first->data)
{ {
free_entity (context_data.first->data); free_entity (context_data.first->data);
g_slist_free_1 (context_data.first); g_slist_free_1 (context_data.first);
} }
skipping to change at line 839 skipping to change at line 932
g_error_free (error); g_error_free (error);
if (context_data.first && context_data.first->data) if (context_data.first && context_data.first->data)
{ {
free_entity (context_data.first->data); free_entity (context_data.first->data);
g_slist_free_1 (context_data.first); g_slist_free_1 (context_data.first);
} }
if (timeout > 0) if (timeout > 0)
fcntl (socket, F_SETFL, 0L); fcntl (socket, F_SETFL, 0L);
g_markup_parse_context_free (xml_context); g_markup_parse_context_free (xml_context);
g_free (buffer); g_free (buffer);
if (string && *string_return == NULL)
g_string_free (string, TRUE);
return -2; return -2;
} }
*entity = (entity_t) context_data.first->data; if (entity)
*entity = (entity_t) context_data.first->data;
if (string) if (string)
*string_return = string; *string_return = string;
if (timeout > 0) if (timeout > 0)
fcntl (socket, F_SETFL, 0L); fcntl (socket, F_SETFL, 0L);
g_slist_free (context_data.first); g_slist_free (context_data.first);
g_markup_parse_context_free (xml_context); g_markup_parse_context_free (xml_context);
g_free (buffer); g_free (buffer);
return 0; return 0;
} }
if ((timeout > 0) && (time (&last_time) == -1)) if ((timeout > 0) && (time (&last_time) == -1))
{ {
g_warning (" failed to get current time (1): %s\n", g_warning (" failed to get current time (1): %s\n",
strerror (errno)); strerror (errno));
if (fcntl (socket, F_SETFL, 0L) < 0) if (fcntl (socket, F_SETFL, 0L) < 0)
g_warning ("%s :failed to set server socket flag: %s", __FUNCTION__, g_warning ("%s :failed to set server socket flag: %s", __FUNCTION__,
strerror (errno)); strerror (errno));
g_markup_parse_context_free (xml_context); g_markup_parse_context_free (xml_context);
g_free (buffer); g_free (buffer);
if (string && *string_return == NULL)
g_string_free (string, TRUE);
return -1; return -1;
} }
} }
} }
/** /**
* @brief Try read an XML entity tree from the manager. * @brief Try read an XML entity tree from the manager.
* *
* @param[in] session Pointer to GNUTLS session. * @param[in] session Pointer to GNUTLS session.
* @param[out] entity Pointer to an entity tree. * @param[out] entity Pointer to an entity tree.
skipping to change at line 1004 skipping to change at line 1102
* @brief Read entity and text. Free the entity immediately. * @brief Read entity and text. Free the entity immediately.
* *
* @param[in] connection Connection. * @param[in] connection Connection.
* @param[out] string Return location for the string. * @param[out] string Return location for the string.
* *
* @return 0 success, -1 read error, -2 parse error, -3 end of file. * @return 0 success, -1 read error, -2 parse error, -3 end of file.
*/ */
int int
read_string_c (gvm_connection_t *connection, GString **string) read_string_c (gvm_connection_t *connection, GString **string)
{ {
int ret = 0; return read_entity_and_string_c (connection, NULL, string);
entity_t entity;
if (!(ret = read_entity_and_string_c (connection, &entity, string)))
free_entity (entity);
return ret;
} }
/** /**
* @brief Try read an XML entity tree from the manager. * @brief Try read an XML entity tree from the manager.
* *
* @param[in] session Pointer to GNUTLS session. * @param[in] session Pointer to GNUTLS session.
* @param[in] timeout Server idle time before giving up, in seconds. 0 to * @param[in] timeout Server idle time before giving up, in seconds. 0 to
* wait forever. * wait forever.
* @param[out] entity Pointer to an entity tree. * @param[out] entity Pointer to an entity tree.
* *
skipping to change at line 1185 skipping to change at line 1277
* @brief Print an XML attribute for g_hash_table_foreach to a GString. * @brief Print an XML attribute for g_hash_table_foreach to a GString.
* *
* @param[in] name The attribute name. * @param[in] name The attribute name.
* @param[in] value The attribute value. * @param[in] value The attribute value.
* @param[in] string The string to which to print. * @param[in] string The string to which to print.
*/ */
static void static void
foreach_print_attribute_to_string (gpointer name, gpointer value, foreach_print_attribute_to_string (gpointer name, gpointer value,
gpointer string) gpointer string)
{ {
gchar *text_escaped;
text_escaped = g_markup_escape_text ((gchar *) value, -1);
g_string_append_printf ((GString *) string, " %s=\"%s\"", (char *) name, g_string_append_printf ((GString *) string, " %s=\"%s\"", (char *) name,
(char *) value); text_escaped);
g_free (text_escaped);
} }
/** /**
* @brief Print an XML entity tree to a GString, appending it if string is not * @brief Print an XML entity tree to a GString, appending it if string is not
* @brief empty. * @brief empty.
* *
* @param[in] entity Entity tree to print to string. * @param[in] entity Entity tree to print to string.
* @param[in,out] string String to write to. * @param[in,out] string String to write to.
*/ */
void void
skipping to change at line 1569 skipping to change at line 1664
{ {
} }
g_markup_parse_context_end_parse (xml_context, &error); g_markup_parse_context_end_parse (xml_context, &error);
fclose (file); fclose (file);
g_markup_parse_context_free (xml_context); g_markup_parse_context_free (xml_context);
return search_data.found; return search_data.found;
} }
#undef XML_FILE_BUFFER_SIZE #undef XML_FILE_BUFFER_SIZE
/* The new faster parser that uses libxml2. */
/**
* @brief Read an XML element tree from a string.
*
* Caller must not free string until caller is finished using element.
*
* @param[in] string Input string.
* @param[out] element Location for parsed element tree, or NULL if not
* required. If given, set to NULL on failure.
* Free with element_free.
*
* @return 0 success, -1 read error, -2 parse error, -3 XML ended prematurely,
* -4 setup error.
*/
int
parse_element (const gchar *string, element_t *element)
{
xmlDocPtr doc;
LIBXML_TEST_VERSION
if (element)
*element = NULL;
if (xmlMemSetup (g_free, g_malloc, g_realloc, g_strdup))
return -4;
doc = xmlReadMemory (string, strlen (string), "noname.xml", NULL, 0);
if (doc == NULL)
return -2;
if (element)
*element = xmlDocGetRootElement (doc);
return 0;
}
/**
* @brief Free an entire element tree.
*
* Beware that this frees the entire tree that element is part of, including
* any ancestors.
*
* @param[in] element Element.
*/
void
element_free (element_t element)
{
if (element)
{
assert (element->doc);
xmlFreeDoc (element->doc);
}
}
/**
* @brief Get the name of an element.
*
* @param[in] element Element.
*
* @return Element name.
*/
const gchar *
element_name (element_t element)
{
if (element
&& (element->type == XML_ELEMENT_NODE))
return (const gchar *) element->name;
return "";
}
/**
* @brief Find child in an element.
*
* @param[in] element Element.
* @param[in] name Name of child.
*
* @return Child if found, else NULL.
*/
static element_t
find_child (element_t element, const gchar *name)
{
for (xmlNode *node = element->children; node; node = node->next)
if (xmlStrcmp (node->name, (const xmlChar *) name) == 0)
return node;
return NULL;
}
/**
* @brief Get a child of an element.
*
* @param[in] element Element.
* @param[in] name Name of the child.
*
* @return Element if found, else NULL.
*/
element_t
element_child (element_t element, const gchar *name)
{
const gchar *stripped_name;
if (!element)
return NULL;
stripped_name = strchr (name, ':');
if (stripped_name)
{
element_t child;
/* There was a namespace in the name.
*
* First try without the namespace, because libxml2 doesn't consider the
* namespace in the name when the namespace is defined. */
stripped_name++;
if (*stripped_name == '\0')
/* Don't search for child with empty stripped name, because we'll
* find text nodes. But search with just the namespace for glib
* compatibility. */
return find_child (element, name);
child = find_child (element, stripped_name);
if (child)
return child;
/* Didn't find anything. */
}
/* There was no namespace, or we didn't find anything without the namespace.
*
* Try with the full name. */
return find_child (element, name);
}
/**
* @brief Get text of an element.
*
* If element is not NULL then the return is guaranteed to be a string.
* So if the caller has NULL checked element then there is no need for
* the caller to NULL check the return.
*
* @param[in] element Element.
*
* @return NULL if element is NULL, else the text. Caller must g_free.
*/
gchar *
element_text (element_t element)
{
gchar *string;
if (!element)
return NULL;
string = (gchar *) xmlNodeListGetString (element->doc, element->xmlChildrenNod
e, 1);
if (string)
return string;
string = xmlMalloc (1);
string[0] = '\0';
return string;
}
/**
* @brief Get an attribute of an element.
*
* @param[in] element Element.
* @param[in] name Name of the attribute.
*
* @return Attribute value if found, else NULL. Caller must g_free.
*/
gchar *
element_attribute (element_t element, const gchar *name)
{
const gchar *stripped_name;
if (!element)
return NULL;
stripped_name = strchr (name, ':');
if (stripped_name)
{
gchar *attribute;
/* There was a namespace in the name.
*
* First try without the namespace, because libxml2 doesn't consider the
* namespace in the name when the namespace is defined. */
stripped_name++;
if (*stripped_name == '\0')
/* Don't search for child with empty stripped name, because we'll
* find text nodes. But search with just the namespace for glib
* compatibility. */
return (gchar *) xmlGetProp (element, (const xmlChar *) name);
attribute = (gchar *) xmlGetProp (element, (const xmlChar *) stripped_name
);
if (attribute)
return attribute;
/* Didn't find anything. */
}
/* There was no namespace, or we didn't find anything without the namespace.
*
* Try with the full name. */
return (gchar *) xmlGetProp (element, (const xmlChar *) name);
}
/**
* @brief Get the first child of an element.
*
* @param[in] element Element.
*
* @return Child if there is one, else NULL.
*/
element_t
element_first_child (element_t element)
{
if (element)
return element->children;
return NULL;
}
/**
* @brief Get the next sibling of an element
*
* @param[in] element Element.
*
* @return Next sibling element if there is one, else NULL.
*/
element_t
element_next (element_t element)
{
if (element)
return element->next;
return NULL;
}
 End of changes. 15 change blocks. 
16 lines changed or deleted 111 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)