Greenbone Vulnerability Management Libraries 22.32.0
xmlutils.c File Reference

Simple XML reader. More...

#include "xmlutils.h"
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <glib.h>
#include <glib/gtypes.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
Include dependency graph for xmlutils.c:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  xml_file_iterator_struct
 Opaque data structure for XML file iterator. More...

Macros

#define G_LOG_DOMAIN   "libgvm util"
 GLib logging domain.
#define BUFFER_SIZE   1048576
 Size of the buffer for reading from the manager.
#define XML_FILE_BUFFER_SIZE   1048576
#define XML_FILE_ITERATOR_BUFFER_SIZE   8192
 File read buffer size for an XML file iterator.

Functions

static entity_t make_entity (const char *name, const char *text)
 Create an entity.
entities_t next_entities (entities_t entities)
 Return all the entities from an entities_t after the first.
entity_t first_entity (entities_t entities)
 Return the first entity from an entities_t.
entity_t add_entity (entities_t *entities, const char *name, const char *text)
 Add an XML entity to a tree of entities.
void free_entity (entity_t entity)
 Free an entity, recursively.
char * entity_text (entity_t entity)
 Get the text an entity.
char * entity_name (entity_t entity)
 Get the name an entity.
static int compare_entity_with_name (gconstpointer entity, gconstpointer name)
 Compare a given name with the name of a given entity.
entity_t entity_child (entity_t entity, const char *name)
 Get a child of an entity.
const char * entity_attribute (entity_t entity, const char *name)
 Get an attribute of an entity.
static void add_attributes (entity_t entity, const gchar **names, const gchar **values)
 Add attributes from an XML callback to an entity.
static void ignore_start_element (GMarkupParseContext *context, const gchar *element_name, const gchar **attribute_names, const gchar **attribute_values, gpointer user_data, GError **error)
 Handle the start of an OMP XML element.
static void handle_start_element (GMarkupParseContext *context, const gchar *element_name, const gchar **attribute_names, const gchar **attribute_values, gpointer user_data, GError **error)
 Handle the start of an OMP XML element.
void xml_handle_start_element (context_data_t *context, const gchar *element_name, const gchar **attribute_names, const gchar **attribute_values)
 Handle the start of an OMP XML element.
static void ignore_end_element (GMarkupParseContext *context, const gchar *element_name, gpointer user_data, GError **error)
 Handle the end of an XML element.
static void handle_end_element (GMarkupParseContext *context, const gchar *element_name, gpointer user_data, GError **error)
 Handle the end of an XML element.
void xml_handle_end_element (context_data_t *context, const gchar *element_name)
 Handle the end of an XML element.
static void ignore_text (GMarkupParseContext *context, const gchar *text, gsize text_len, gpointer user_data, GError **error)
 Handle additional text of an XML element.
static void handle_text (GMarkupParseContext *context, const gchar *text, gsize text_len, gpointer user_data, GError **error)
 Handle additional text of an XML element.
void xml_handle_text (context_data_t *context, const gchar *text, gsize text_len)
 Handle additional text of an XML element.
static void handle_error (GMarkupParseContext *context, GError *error, gpointer user_data)
 Handle an OMP XML parsing error.
int try_read_entity_and_string (gnutls_session_t *session, int timeout, entity_t *entity, GString **string_return)
 Try read an XML entity tree from the manager.
static int try_read_string (gnutls_session_t *session, int timeout, GString **string_return)
 Try read a response from a TLS session.
static int try_read_string_s (int socket, int timeout, GString **string_return)
 Try read an XML entity tree from the socket.
static int try_read_entity_and_string_s (int socket, int timeout, entity_t *entity, GString **string_return)
 Try read an XML entity tree from the socket.
int read_entity_and_string (gnutls_session_t *session, entity_t *entity, GString **string_return)
 Try read an XML entity tree from the manager.
int read_entity_and_string_c (gvm_connection_t *connection, entity_t *entity, GString **string_return)
 Try read an XML entity tree from the manager.
int read_entity_and_text (gnutls_session_t *session, entity_t *entity, char **text)
 Read an XML entity tree from the manager.
int read_entity_and_text_c (gvm_connection_t *connection, entity_t *entity, char **text)
 Read an XML entity tree from the manager.
int read_text_c (gvm_connection_t *connection, char **text)
 Read text from the server.
int read_string (gnutls_session_t *session, GString **string)
 Read entity and text. Free the entity immediately.
int read_string_c (gvm_connection_t *connection, GString **string)
 Read entity and text. Free the entity immediately.
int try_read_entity (gnutls_session_t *session, int timeout, entity_t *entity)
 Try read an XML entity tree from the manager.
int try_read_entity_c (gvm_connection_t *connection, int timeout, entity_t *entity)
 Try read an XML entity tree from the manager.
int read_entity (gnutls_session_t *session, entity_t *entity)
 Read an XML entity tree from the manager.
int read_entity_s (int socket, entity_t *entity)
 Read an XML entity tree from the socket.
int read_entity_c (gvm_connection_t *connection, entity_t *entity)
 Read an XML entity tree from the manager.
int parse_entity (const char *string, entity_t *entity)
 Read an XML entity tree from a string.
static void foreach_print_entity_to_string (gpointer entity, gpointer string)
 Print an XML entity for g_slist_foreach to a GString.
static void foreach_print_attribute_to_string (gpointer name, gpointer value, gpointer string)
 Print an XML attribute for g_hash_table_foreach to a GString.
void print_entity_to_string (entity_t entity, GString *string)
 Print an XML entity tree to a GString, appending it if string is not.
static void foreach_print_entity (gpointer entity, gpointer stream)
 Print an XML entity for g_slist_foreach.
static void foreach_print_attribute (gpointer name, gpointer value, gpointer stream)
 Print an XML attribute for g_hash_table_foreach.
void print_entity (FILE *stream, entity_t entity)
 Print an XML entity.
static void foreach_print_attribute_format (gpointer name, gpointer value, gpointer none)
 Print an XML attribute for g_hash_table_foreach to stdout.
void print_entity_format (entity_t entity, gpointer indent)
 Print an XML entity to stdout, recursively printing its children.
static gboolean compare_find_attribute (gpointer key, gpointer value, gpointer attributes2)
 Look for a key-value pair in a hash table.
int compare_entities (entity_t entity1, entity_t entity2)
 Compare two XML entity.
int xml_count_entities (entities_t entities)
 Count the number of entities.
void xml_string_append (GString *xml, const char *format,...)
 Append formatted escaped XML to a string.
static void xml_search_handle_start_element (GMarkupParseContext *ctx, const gchar *element_name, const gchar **attribute_names, const gchar **attribute_values, gpointer data, GError **error)
 Handle the opening tag of an element in an XML search.
int find_element_in_xml_file (gchar *file_path, gchar *find_element, GHashTable *find_attributes)
 Tests if an XML file contains an element with given attributes.
int parse_element (const gchar *string, element_t *element)
 Read an XML element tree from a string.
void element_free (element_t element)
 Free an entire element tree.
const gchar * element_name (element_t element)
 Get the name of an element.
static element_t find_child (element_t element, const gchar *name)
 Find child in an element.
element_t element_child (element_t element, const gchar *name)
 Get a child of an element.
gchar * element_text (element_t element)
 Get text of an element.
gchar * element_attribute (element_t element, const gchar *name)
 Get an attribute of an element.
element_t element_first_child (element_t element)
 Get the first child of an element.
element_t element_next (element_t element)
 Get the next sibling of an element.
gchar * element_to_string (element_t element)
 Output the XML element as a string.
void print_element_to_string (element_t element, GString *string)
 Print an XML element tree to a GString, appending it if string is not.
static void xml_file_iterator_start_element_ns (void *ctx, const xmlChar *localname, const xmlChar *prefix, const xmlChar *URI, int nb_namespaces, const xmlChar **namespaces, int nb_attributes, int nb_defaulted, const xmlChar **attributes)
 XML file iterator parser callback for element start.
static void xml_file_iterator_end_element_ns (void *ctx, const xmlChar *localname, const xmlChar *prefix, const xmlChar *URI)
 XML file iterator parser callback for element end.
static void xml_file_iterator_internal_subset (void *ctx, const xmlChar *name, const xmlChar *ExternalID, const xmlChar *SystemID)
 XML file iterator parser callback for internal subset declaration.
static void xml_file_iterator_external_subset (void *ctx, const xmlChar *name, const xmlChar *ExternalID, const xmlChar *SystemID)
 XML file iterator parser callback for external subset declaration.
static int xml_file_iterator_is_standalone (void *ctx)
 XML file iterator parser callback for checking if doc is standalone.
static int xml_file_iterator_has_internal_subset (void *ctx)
 XML file iterator parser callback for checking if doc has an internal subset.
static int xml_file_iterator_has_external_subset (void *ctx)
 XML file iterator parser callback for checking if doc has an external subset.
static xmlParserInputPtr xml_file_iterator_resolve_entity (void *ctx, const xmlChar *publicId, const xmlChar *systemId)
 XML file iterator parser callback for resolving an entity.
static xmlEntityPtr xml_file_iterator_get_entity (void *ctx, const xmlChar *name)
 XML file iterator parser callback for getting an entity by name.
static xmlEntityPtr xml_file_iterator_get_parameter_entity (void *ctx, const xmlChar *name)
 XML file iterator parser callback for getting a parameter entity by name.
static void xml_file_iterator_entity_decl (void *ctx, const xmlChar *name, int type, const xmlChar *publicId, const xmlChar *systemId, xmlChar *content)
 XML file iterator parser callback for when an entity definition has been parsed.
static void xml_file_iterator_attribute_decl (void *ctx, const xmlChar *elem, const xmlChar *fullname, int type, int def, const xmlChar *defaultValue, xmlEnumerationPtr tree)
 XML file iterator parser callback for when an attribute definition has been parsed.
static void xml_file_iterator_element_decl (void *ctx, const xmlChar *name, int type, xmlElementContentPtr content)
 XML file iterator parser callback for when an element definition has been parsed.
static void xml_file_iterator_notation_decl (void *ctx, const xmlChar *name, const xmlChar *publicId, const xmlChar *systemId)
 XML file iterator parser callback for when a notation definition has been parsed.
static void xml_file_iterator_unparsed_entity_decl (void *ctx, const xmlChar *name, const xmlChar *publicId, const xmlChar *systemId, const xmlChar *notationName)
 XML file iterator parser callback for when an unparsed entity declaration has been parsed.
static void xml_file_iterator_set_document_locator (void *ctx, xmlSAXLocatorPtr loc)
 XML file iterator parser callback for setting the document locator.
static void xml_file_iterator_start_document (void *ctx)
 XML file iterator parser callback at the document start.
static void xml_file_iterator_end_document (void *ctx)
 XML file iterator parser callback at the document end.
static void xml_file_iterator_characters (void *ctx, const xmlChar *ch, int len)
 XML file iterator parser callback when receiving some chars from the parser.
static void xml_file_iterator_cdata_block (void *ctx, const xmlChar *ch, int len)
 XML file iterator parser callback when a cdata block has been parsed.
static void xml_file_iterator_processing_instruction (void *ctx, const xmlChar *target, const xmlChar *data)
 XML file iterator parser callback when a processing instruction has been parsed.
static void xml_file_iterator_comment (void *ctx, const xmlChar *value)
 XML file iterator parser callback when a comment has been parsed.
static void xml_file_iterator_init_sax_handler (xmlSAXHandlerPtr hdlr)
 Initializes a xmlSAXHandler data structure for SAX version 2 parsing, assigning all the XML file iterator callback functions.
xml_file_iterator_t xml_file_iterator_new (void)
 Allocates a new, uninitialized XML file iterator.
int xml_file_iterator_init_from_file_path (xml_file_iterator_t iterator, const char *file_path, int output_depth)
 Initializes an XML file iterator to read from a given path.
void xml_file_iterator_free (xml_file_iterator_t iterator)
 Frees an XML file iterator and all of its internal data structures.
int xml_file_iterator_rewind (xml_file_iterator_t iterator)
 Rewinds an XML file iterator by rewinding the file and creating a new XML parser context.
element_t xml_file_iterator_next (xml_file_iterator_t iterator, gchar **error)
 Get the next subelement from a XML file iterator.

Detailed Description

Simple XML reader.

This is a generic XML interface. The key function is read_entity.

There are examples of using this interface in omp.c.

Definition in file xmlutils.c.

Macro Definition Documentation

◆ BUFFER_SIZE

#define BUFFER_SIZE   1048576

Size of the buffer for reading from the manager.

Definition at line 37 of file xmlutils.c.

Referenced by try_read_entity_and_string(), try_read_entity_and_string_s(), try_read_string(), and try_read_string_s().

◆ G_LOG_DOMAIN

#define G_LOG_DOMAIN   "libgvm util"

GLib logging domain.

Definition at line 32 of file xmlutils.c.

◆ XML_FILE_BUFFER_SIZE

#define XML_FILE_BUFFER_SIZE   1048576

Definition at line 1931 of file xmlutils.c.

Referenced by find_element_in_xml_file().

◆ XML_FILE_ITERATOR_BUFFER_SIZE

#define XML_FILE_ITERATOR_BUFFER_SIZE   8192

File read buffer size for an XML file iterator.

Definition at line 2990 of file xmlutils.c.

Referenced by xml_file_iterator_next().

Function Documentation

◆ add_attributes()

void add_attributes ( entity_t entity,
const gchar ** names,
const gchar ** values )
static

Add attributes from an XML callback to an entity.

Parameters
[in]entityThe entity.
[in]namesList of attribute names.
[in]valuesList of attribute values.

Definition at line 234 of file xmlutils.c.

235{
236 if (names && values && *names && *values)
237 {
238 if (entity->attributes == NULL)
239 entity->attributes =
240 g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
241 while (*names && *values)
242 {
243 if (*values)
244 g_hash_table_insert (entity->attributes, g_strdup (*names),
245 g_strdup (*values));
246 names++;
247 values++;
248 }
249 }
250}
GHashTable * attributes
Attributes.
Definition xmlutils.h:55

References entity_s::attributes.

Referenced by handle_start_element().

Here is the caller graph for this function:

◆ add_entity()

entity_t add_entity ( entities_t * entities,
const char * name,
const char * text )

Add an XML entity to a tree of entities.

Parameters
[in]entitiesThe tree of entities
[in]nameName of the entity. Copied, copy is freed by free_entity.
[in]textText of the entity. Copied, copy is freed by free_entity.
Returns
The new entity.

Definition at line 101 of file xmlutils.c.

102{
103 entity_t entity = make_entity (name, text);
104 if (entities)
105 *entities = g_slist_append (*entities, entity);
106 return entity;
107}
static entity_t make_entity(const char *name, const char *text)
Create an entity.
Definition xmlutils.c:48
struct entity_s * entity_t
Definition xmlutils.h:58

References make_entity().

Referenced by handle_start_element().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ compare_entities()

int compare_entities ( entity_t entity1,
entity_t entity2 )

Compare two XML entity.

Parameters
[in]entity1First entity.
[in]entity2First entity.
Returns
0 if equal, 1 otherwise.

Definition at line 1765 of file xmlutils.c.

1766{
1767 if (entity1 == NULL)
1768 return entity2 == NULL ? 0 : 1;
1769 if (entity2 == NULL)
1770 return 1;
1771
1772 if (strcmp (entity1->name, entity2->name))
1773 {
1774 g_debug (" compare failed name: %s vs %s\n", entity1->name,
1775 entity2->name);
1776 return 1;
1777 }
1778 if (strcmp (entity1->text, entity2->text))
1779 {
1780 g_debug (" compare failed text %s vs %s (%s)\n", entity1->text,
1781 entity2->text, entity1->name);
1782 return 1;
1783 }
1784
1785 if (entity1->attributes == NULL)
1786 {
1787 if (entity2->attributes)
1788 return 1;
1789 }
1790 else
1791 {
1792 if (entity2->attributes == NULL)
1793 return 1;
1794 if (g_hash_table_find (entity1->attributes, compare_find_attribute,
1795 (gpointer) entity2->attributes))
1796 {
1797 g_debug (" compare failed attributes\n");
1798 return 1;
1799 }
1800 }
1801
1802 // FIX entities can be in any order
1803 GSList *list1 = entity1->entities;
1804 GSList *list2 = entity2->entities;
1805 while (list1 && list2)
1806 {
1807 if (compare_entities (list1->data, list2->data))
1808 {
1809 g_debug (" compare failed subentity\n");
1810 return 1;
1811 }
1812 list1 = g_slist_next (list1);
1813 list2 = g_slist_next (list2);
1814 }
1815 if (list1 == list2)
1816 return 0;
1817 /* More entities in one of the two. */
1818 g_debug (" compare failed number of entities (%s)\n", entity1->name);
1819 return 1;
1820}
entities_t entities
Children.
Definition xmlutils.h:56
char * text
Text.
Definition xmlutils.h:54
char * name
Name.
Definition xmlutils.h:53
static gboolean compare_find_attribute(gpointer key, gpointer value, gpointer attributes2)
Look for a key-value pair in a hash table.
Definition xmlutils.c:1747
int compare_entities(entity_t entity1, entity_t entity2)
Compare two XML entity.
Definition xmlutils.c:1765

References entity_s::attributes, compare_entities(), compare_find_attribute(), entity_s::entities, entity_s::name, and entity_s::text.

Referenced by compare_entities().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ compare_entity_with_name()

int compare_entity_with_name ( gconstpointer entity,
gconstpointer name )
static

Compare a given name with the name of a given entity.

Parameters
[in]entityEntity.
[in]nameName.
Returns
Zero if entity name matches name, otherwise a positive or negative number as from strcmp.

Definition at line 179 of file xmlutils.c.

180{
181 return strcmp (entity_name ((entity_t) entity), (char *) name);
182}
char * entity_name(entity_t entity)
Get the name an entity.
Definition xmlutils.c:161

References entity_name().

Referenced by entity_child().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ compare_find_attribute()

gboolean compare_find_attribute ( gpointer key,
gpointer value,
gpointer attributes2 )
static

Look for a key-value pair in a hash table.

Parameters
[in]keyKey.
[in]valueValue.
[in]attributes2The hash table.
Returns
FALSE if found, TRUE otherwise.

Definition at line 1747 of file xmlutils.c.

1748{
1749 gchar *value2 = g_hash_table_lookup (attributes2, key);
1750 if (value2 && strcmp (value, value2) == 0)
1751 return FALSE;
1752 g_debug (" compare failed attribute: %s\n", (char *) value);
1753 return TRUE;
1754}

Referenced by compare_entities().

Here is the caller graph for this function:

◆ element_attribute()

gchar * element_attribute ( element_t element,
const gchar * name )

Get an attribute of an element.

Parameters
[in]elementElement.
[in]nameName of the attribute.
Returns
Attribute value if found, else NULL. Caller must g_free.

Definition at line 2162 of file xmlutils.c.

2163{
2164 const gchar *stripped_name;
2165
2166 if (!element)
2167 return NULL;
2168
2169 stripped_name = strchr (name, ':');
2170 if (stripped_name)
2171 {
2172 gchar *attribute;
2173
2174 /* There was a namespace in the name.
2175 *
2176 * First try without the namespace, because libxml2 doesn't consider the
2177 * namespace in the name when the namespace is defined. */
2178
2179 stripped_name++;
2180
2181 if (*stripped_name == '\0')
2182 /* Don't search for child with empty stripped name, because we'll
2183 * find text nodes. But search with just the namespace for glib
2184 * compatibility. */
2185 return (gchar *) xmlGetProp (element, (const xmlChar *) name);
2186
2187 attribute =
2188 (gchar *) xmlGetProp (element, (const xmlChar *) stripped_name);
2189 if (attribute)
2190 return attribute;
2191
2192 /* Didn't find anything. */
2193 }
2194
2195 /* There was no namespace, or we didn't find anything without the namespace.
2196 *
2197 * Try with the full name. */
2198
2199 return (gchar *) xmlGetProp (element, (const xmlChar *) name);
2200}

Referenced by Ensure(), Ensure(), and Ensure().

Here is the caller graph for this function:

◆ element_child()

element_t element_child ( element_t element,
const gchar * name )

Get a child of an element.

Parameters
[in]elementElement.
[in]nameName of the child.
Returns
Element if found, else NULL.

Definition at line 2086 of file xmlutils.c.

2087{
2088 const gchar *stripped_name;
2089
2090 if (!element)
2091 return NULL;
2092
2093 stripped_name = strchr (name, ':');
2094 if (stripped_name)
2095 {
2096 element_t child;
2097
2098 /* There was a namespace in the name.
2099 *
2100 * First try without the namespace, because libxml2 doesn't consider the
2101 * namespace in the name when the namespace is defined. */
2102
2103 stripped_name++;
2104
2105 if (*stripped_name == '\0')
2106 /* Don't search for child with empty stripped name, because we'll
2107 * find text nodes. But search with just the namespace for glib
2108 * compatibility. */
2109 return find_child (element, name);
2110
2111 child = find_child (element, stripped_name);
2112 if (child)
2113 return child;
2114
2115 /* Didn't find anything. */
2116 }
2117
2118 /* There was no namespace, or we didn't find anything without the namespace.
2119 *
2120 * Try with the full name. */
2121
2122 return find_child (element, name);
2123}
static element_t find_child(element_t element, const gchar *name)
Find child in an element.
Definition xmlutils.c:2069
struct _xmlNode * element_t
Definition xmlutils.h:157

References find_child().

Referenced by Ensure(), Ensure(), Ensure(), Ensure(), Ensure(), Ensure(), Ensure(), Ensure(), and Ensure().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ element_first_child()

element_t element_first_child ( element_t element)

Get the first child of an element.

Parameters
[in]elementElement.
Returns
Child if there is one, else NULL.

Definition at line 2210 of file xmlutils.c.

2211{
2212 if (element)
2213 {
2214 element = element->children;
2215 while (element && (element->type != XML_ELEMENT_NODE))
2216 element = element->next;
2217 return element;
2218 }
2219 return NULL;
2220}

Referenced by Ensure(), and print_element_to_string().

Here is the caller graph for this function:

◆ element_free()

void element_free ( element_t element)

Free an entire element tree.

Beware that this frees the entire tree that element is part of, including any ancestors.

Parameters
[in]elementElement.

Definition at line 2035 of file xmlutils.c.

2036{
2037 if (element)
2038 {
2039 assert (element->doc);
2040 xmlFreeDoc (element->doc);
2041 }
2042}

Referenced by Ensure(), Ensure(), Ensure(), Ensure(), Ensure(), Ensure(), Ensure(), Ensure(), Ensure(), Ensure(), Ensure(), Ensure(), Ensure(), Ensure(), xml_file_iterator_free(), and xml_file_iterator_rewind().

Here is the caller graph for this function:

◆ element_name()

const gchar * element_name ( element_t element)

Get the name of an element.

Parameters
[in]elementElement.
Returns
Element name.

Definition at line 2052 of file xmlutils.c.

2053{
2054 if (element && (element->type == XML_ELEMENT_NODE))
2055 return (const gchar *) element->name;
2056
2057 return "";
2058}

Referenced by Ensure(), Ensure(), Ensure(), Ensure(), Ensure(), Ensure(), Ensure(), Ensure(), Ensure(), Ensure(), Ensure(), Ensure(), handle_end_element(), handle_start_element(), ignore_end_element(), ignore_start_element(), print_element_to_string(), xml_handle_end_element(), xml_handle_start_element(), and xml_search_handle_start_element().

Here is the caller graph for this function:

◆ element_next()

element_t element_next ( element_t element)

Get the next sibling of an element.

Parameters
[in]elementElement.
Returns
Next sibling element if there is one, else NULL.

Definition at line 2230 of file xmlutils.c.

2231{
2232 if (element)
2233 {
2234 element = element->next;
2235 while (element && (element->type != XML_ELEMENT_NODE))
2236 element = element->next;
2237 return element;
2238 }
2239 return NULL;
2240}

Referenced by Ensure(), and print_element_to_string().

Here is the caller graph for this function:

◆ element_text()

gchar * element_text ( element_t element)

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.

Parameters
[in]elementElement.
Returns
NULL if element is NULL, else the text. Caller must g_free.

Definition at line 2137 of file xmlutils.c.

2138{
2139 gchar *string;
2140
2141 if (!element)
2142 return NULL;
2143
2144 string =
2145 (gchar *) xmlNodeListGetString (element->doc, element->xmlChildrenNode, 1);
2146 if (string)
2147 return string;
2148 string = xmlMalloc (1);
2149 string[0] = '\0';
2150 return string;
2151}

Referenced by Ensure(), Ensure(), Ensure(), Ensure(), Ensure(), Ensure(), Ensure(), and print_element_to_string().

Here is the caller graph for this function:

◆ element_to_string()

gchar * element_to_string ( element_t element)

Output the XML element as a string.

The generated XML string will include namespace definitions from ancestor elements.

Parameters
[in]elementThe element to output as a string.
Returns
The newly allocated XML string.

Definition at line 2253 of file xmlutils.c.

2254{
2255 xmlBufferPtr buffer;
2256 char *xml_string;
2257
2258 // Copy element to ensure XML namespaces are included
2259 element_t element_copy;
2260 element_copy = xmlCopyNode (element, 1);
2261
2262 buffer = xmlBufferCreate ();
2263 xmlNodeDump (buffer, element_copy->doc, element_copy, 0, 0);
2264 xmlFreeNode (element_copy);
2265
2266 xml_string = g_strdup ((char *) xmlBufferContent (buffer));
2267
2268 xmlBufferFree (buffer);
2269 return xml_string;
2270}

◆ entity_attribute()

const char * entity_attribute ( entity_t entity,
const char * name )

Get an attribute of an entity.

Parameters
[in]entityEntity.
[in]nameName of the attribute.
Returns
Attribute if found, else NULL.

Definition at line 216 of file xmlutils.c.

217{
218 if (!entity)
219 return NULL;
220
221 if (entity->attributes)
222 return (const char *) g_hash_table_lookup (entity->attributes, name);
223 return NULL;
224}

References entity_s::attributes.

Referenced by check_response_c(), Ensure(), gmp_authenticate_info_ext(), gmp_authenticate_info_ext_c(), gmp_check_response(), gmp_get_report_ext(), gmp_get_tasks_ext(), gmp_ping(), gmp_ping_c(), gmp_read_create_response(), gmp_resume_task_report_c(), gmp_start_task_report_c(), osp_check_feed(), osp_delete_scan(), osp_get_performance_ext(), osp_get_scan_pop(), osp_get_scan_status_ext(), osp_get_scanner_details(), osp_get_vts_feed_info(), osp_get_vts_version(), osp_start_scan(), osp_start_scan_ext(), and osp_stop_scan().

Here is the caller graph for this function:

◆ entity_child()

entity_t entity_child ( entity_t entity,
const char * name )

Get a child of an entity.

Parameters
[in]entityEntity.
[in]nameName of the child.
Returns
Entity if found, else NULL.

Definition at line 193 of file xmlutils.c.

194{
195 if (!entity)
196 return NULL;
197
198 if (entity->entities)
199 {
200 entities_t match =
201 g_slist_find_custom (entity->entities, name, compare_entity_with_name);
202 return match ? (entity_t) match->data : NULL;
203 }
204 return NULL;
205}
static int compare_entity_with_name(gconstpointer entity, gconstpointer name)
Compare a given name with the name of a given entity.
Definition xmlutils.c:179
GSList * entities_t
Entities.
Definition xmlutils.h:46

References compare_entity_with_name(), and entity_s::entities.

Referenced by Ensure(), Ensure(), Ensure(), Ensure(), Ensure(), gmp_authenticate_info_ext(), gmp_authenticate_info_ext_c(), gmp_ping_c(), gmp_resume_task_report(), gmp_resume_task_report_c(), gmp_start_task_report(), gmp_start_task_report_c(), gmp_task_status(), osp_check_feed(), osp_get_scan_pop(), osp_get_scan_status_ext(), osp_get_scanner_details(), osp_get_version(), osp_get_vts_feed_info(), and osp_get_vts_version().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ entity_name()

char * entity_name ( entity_t entity)

Get the name an entity.

Parameters
[in]entityEntity.
Returns
Entity name, which is freed by free_entity.

Definition at line 161 of file xmlutils.c.

162{
163 if (!entity)
164 return NULL;
165
166 return entity->name;
167}

References entity_s::name.

Referenced by compare_entity_with_name(), Ensure(), Ensure(), Ensure(), Ensure(), and Ensure().

Here is the caller graph for this function:

◆ entity_text()

char * entity_text ( entity_t entity)

Get the text an entity.

Parameters
[in]entityEntity.
Returns
Entity text, which is freed by free_entity.

Definition at line 145 of file xmlutils.c.

146{
147 if (!entity)
148 return NULL;
149
150 return entity->text;
151}

References entity_s::text.

Referenced by Ensure(), Ensure(), Ensure(), Ensure(), Ensure(), gmp_authenticate_info_ext(), gmp_authenticate_info_ext_c(), gmp_ping_c(), gmp_resume_task_report(), gmp_resume_task_report_c(), gmp_start_task_report(), gmp_start_task_report_c(), gmp_task_status(), osp_check_feed(), osp_get_performance_ext(), osp_get_scanner_details(), and osp_get_version().

Here is the caller graph for this function:

◆ find_child()

element_t find_child ( element_t element,
const gchar * name )
static

Find child in an element.

Parameters
[in]elementElement.
[in]nameName of child.
Returns
Child if found, else NULL.

Definition at line 2069 of file xmlutils.c.

2070{
2071 for (xmlNode *node = element->children; node; node = node->next)
2072 if (xmlStrcmp (node->name, (const xmlChar *) name) == 0)
2073 return node;
2074 return NULL;
2075}

Referenced by element_child().

Here is the caller graph for this function:

◆ find_element_in_xml_file()

int find_element_in_xml_file ( gchar * file_path,
gchar * find_element,
GHashTable * find_attributes )

Tests if an XML file contains an element with given attributes.

Parameters
[in]file_pathPath of the XML file.
[in]find_elementName of the element to find.
[in]find_attributesGHashTable of attributes to find.
Returns
1 if element was found, 0 if not.

Definition at line 1942 of file xmlutils.c.

1944{
1945 gchar buffer[XML_FILE_BUFFER_SIZE];
1946 FILE *file;
1947 int read_len;
1948 GMarkupParser xml_parser;
1949 GMarkupParseContext *xml_context;
1950 xml_search_data_t search_data;
1951 GError *error = NULL;
1952
1953 search_data.find_element = find_element;
1954 search_data.find_attributes = find_attributes;
1955 search_data.found = 0;
1956
1957 /* Create the XML parser. */
1958 xml_parser.start_element = xml_search_handle_start_element;
1959 xml_parser.end_element = NULL;
1960 xml_parser.text = NULL;
1961 xml_parser.passthrough = NULL;
1962 xml_parser.error = NULL;
1963 xml_context = g_markup_parse_context_new (&xml_parser, 0, &search_data, NULL);
1964
1965 file = fopen (file_path, "r");
1966 if (file == NULL)
1967 {
1968 g_markup_parse_context_free (xml_context);
1969 g_warning ("%s: Failed to open '%s':", __func__, strerror (errno));
1970 return 0;
1971 }
1972
1973 while ((read_len = fread (&buffer, sizeof (char), XML_FILE_BUFFER_SIZE, file))
1974 && g_markup_parse_context_parse (xml_context, buffer, read_len, &error)
1975 && error == NULL)
1976 {
1977 }
1978 g_markup_parse_context_end_parse (xml_context, &error);
1979
1980 fclose (file);
1981
1982 g_markup_parse_context_free (xml_context);
1983 return search_data.found;
1984}
Data for xml search functions.
Definition xmlutils.h:64
gchar * find_element
Definition xmlutils.h:67
GHashTable * find_attributes
Definition xmlutils.h:68
static void xml_search_handle_start_element(GMarkupParseContext *ctx, const gchar *element_name, const gchar **attribute_names, const gchar **attribute_values, gpointer data, GError **error)
Handle the opening tag of an element in an XML search.
Definition xmlutils.c:1874
#define XML_FILE_BUFFER_SIZE
Definition xmlutils.c:1931

References xml_search_data_t::find_attributes, xml_search_data_t::find_element, xml_search_data_t::found, XML_FILE_BUFFER_SIZE, and xml_search_handle_start_element().

Here is the call graph for this function:

◆ first_entity()

entity_t first_entity ( entities_t entities)

Return the first entity from an entities_t.

Parameters
[in]entitiesThe list of entities.
Returns
The first entity.

Definition at line 82 of file xmlutils.c.

83{
84 if (entities)
85 return (entity_t) entities->data;
86 return NULL;
87}

Referenced by Ensure(), and xml_count_entities().

Here is the caller graph for this function:

◆ foreach_print_attribute()

void foreach_print_attribute ( gpointer name,
gpointer value,
gpointer stream )
static

Print an XML attribute for g_hash_table_foreach.

Parameters
[in]nameThe attribute name.
[in]valueThe attribute value.
[in]streamThe stream to which to print.

Definition at line 1651 of file xmlutils.c.

1652{
1653 fprintf ((FILE *) stream, " %s=\"%s\"", (char *) name, (char *) value);
1654}

Referenced by print_entity().

Here is the caller graph for this function:

◆ foreach_print_attribute_format()

void foreach_print_attribute_format ( gpointer name,
gpointer value,
gpointer none )
static

Print an XML attribute for g_hash_table_foreach to stdout.

Parameters
[in]nameThe attribute name.
[in]valueThe attribute value.
[in]none(ignored).

Definition at line 1688 of file xmlutils.c.

1689{
1690 (void) none;
1691 printf (" %s=\"%s\"", (char *) name, (char *) value);
1692}

Referenced by print_entity_format().

Here is the caller graph for this function:

◆ foreach_print_attribute_to_string()

void foreach_print_attribute_to_string ( gpointer name,
gpointer value,
gpointer string )
static

Print an XML attribute for g_hash_table_foreach to a GString.

Parameters
[in]nameThe attribute name.
[in]valueThe attribute value.
[in]stringThe string to which to print.

Definition at line 1598 of file xmlutils.c.

1600{
1601 gchar *text_escaped;
1602 text_escaped = g_markup_escape_text ((gchar *) value, -1);
1603 g_string_append_printf ((GString *) string, " %s=\"%s\"", (char *) name,
1604 text_escaped);
1605 g_free (text_escaped);
1606}

Referenced by print_entity_to_string().

Here is the caller graph for this function:

◆ foreach_print_entity()

void foreach_print_entity ( gpointer entity,
gpointer stream )
static

Print an XML entity for g_slist_foreach.

Parameters
[in]entityThe entity, as a gpointer.
[in]streamThe stream to which to print, as a gpointer.

Definition at line 1638 of file xmlutils.c.

1639{
1640 print_entity ((FILE *) stream, (entity_t) entity);
1641}
void print_entity(FILE *stream, entity_t entity)
Print an XML entity.
Definition xmlutils.c:1663

References print_entity().

Referenced by print_entity().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ foreach_print_entity_to_string()

void foreach_print_entity_to_string ( gpointer entity,
gpointer string )
static

Print an XML entity for g_slist_foreach to a GString.

Parameters
[in]entityThe entity, as a gpointer.
[in]stringThe stream to which to print, as a gpointer.

Definition at line 1585 of file xmlutils.c.

1586{
1587 print_entity_to_string ((entity_t) entity, (GString *) string);
1588}
void print_entity_to_string(entity_t entity, GString *string)
Print an XML entity tree to a GString, appending it if string is not.
Definition xmlutils.c:1616

References print_entity_to_string().

Referenced by print_entity_to_string().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ free_entity()

void free_entity ( entity_t entity)

Free an entity, recursively.

Parameters
[in]entityThe entity, can be NULL.

Definition at line 115 of file xmlutils.c.

116{
117 if (entity)
118 {
119 g_free (entity->name);
120 g_free (entity->text);
121 if (entity->attributes)
122 g_hash_table_destroy (entity->attributes);
123 if (entity->entities)
124 {
125 GSList *list = entity->entities;
126 while (list)
127 {
128 free_entity (list->data);
129 list = list->next;
130 }
131 g_slist_free (entity->entities);
132 }
133 g_free (entity);
134 }
135}
void free_entity(entity_t entity)
Free an entity, recursively.
Definition xmlutils.c:115

References entity_s::attributes, entity_s::entities, free_entity(), entity_s::name, and entity_s::text.

Referenced by check_response_c(), Ensure(), Ensure(), Ensure(), Ensure(), Ensure(), Ensure(), Ensure(), Ensure(), Ensure(), Ensure(), free_entity(), gmp_authenticate(), gmp_authenticate_info_ext(), gmp_authenticate_info_ext_c(), gmp_check_response(), gmp_delete_config_ext(), gmp_delete_lsc_credential_ext(), gmp_delete_port_list_ext(), gmp_delete_report(), gmp_delete_target_ext(), gmp_delete_task(), gmp_delete_task_ext(), gmp_get_report_ext(), gmp_get_tasks_ext(), gmp_modify_task_file(), gmp_ping(), gmp_ping_c(), gmp_read_create_response(), gmp_resume_task_report(), gmp_resume_task_report_c(), gmp_start_task_report(), gmp_start_task_report_c(), gmp_stop_task(), osp_check_feed(), osp_delete_scan(), osp_get_performance_ext(), osp_get_scan_pop(), osp_get_scan_status_ext(), osp_get_scanner_details(), osp_get_version(), osp_get_vts_feed_info(), osp_get_vts_version(), osp_start_scan(), osp_start_scan_ext(), osp_stop_scan(), parse_entity(), read_string(), try_read_entity_and_string(), and try_read_entity_and_string_s().

Here is the call graph for this function:

◆ handle_end_element()

void handle_end_element ( GMarkupParseContext * context,
const gchar * element_name,
gpointer user_data,
GError ** error )
static

Handle the end of an XML element.

Parameters
[in]contextParser context.
[in]element_nameXML element name.
[in]user_dataDummy parameter.
[in]errorError parameter.

Definition at line 366 of file xmlutils.c.

368{
369 context_data_t *data = (context_data_t *) user_data;
370
371 (void) context;
372 (void) error;
373 (void) element_name;
374 assert (data->current && data->first);
375 if (data->current == data->first)
376 {
377 assert (strcmp (element_name,
378 /* The name of the very first entity. */
379 ((entity_t) (data->first->data))->name)
380 == 0);
381 data->done = TRUE;
382 /* "Pop" the element. */
383 data->current = g_slist_next (data->current);
384 }
385 else if (data->current)
386 {
387 GSList *front;
388 /* "Pop" and free the element. */
389 front = data->current;
390 data->current = g_slist_next (data->current);
391 g_slist_free_1 (front);
392 }
393}
XML context.
Definition xmlutils.h:27
GSList * first
The very first entity.
Definition xmlutils.h:28
GSList * current
The element currently being parsed.
Definition xmlutils.h:29
gboolean done
Flag which is true when the first element is closed.
Definition xmlutils.h:30
const gchar * element_name(element_t element)
Get the name of an element.
Definition xmlutils.c:2052

References context_data_t::current, context_data_t::done, element_name(), and context_data_t::first.

Referenced by parse_entity(), try_read_entity_and_string(), try_read_entity_and_string_s(), and xml_handle_end_element().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ handle_error()

void handle_error ( GMarkupParseContext * context,
GError * error,
gpointer user_data )
static

Handle an OMP XML parsing error.

Parameters
[in]contextParser context.
[in]errorThe error.
[in]user_dataDummy parameter.

Definition at line 477 of file xmlutils.c.

478{
479 (void) context;
480 (void) user_data;
481 g_message (" Error: %s\n", error->message);
482}

Referenced by parse_entity(), try_read_entity_and_string(), and try_read_entity_and_string_s().

Here is the caller graph for this function:

◆ handle_start_element()

void handle_start_element ( GMarkupParseContext * context,
const gchar * element_name,
const gchar ** attribute_names,
const gchar ** attribute_values,
gpointer user_data,
GError ** error )
static

Handle the start of an OMP XML element.

Parameters
[in]contextParser context.
[in]element_nameXML element name.
[in]attribute_namesXML attribute name.
[in]attribute_valuesXML attribute values.
[in]user_dataDummy parameter.
[in]errorError parameter.

Definition at line 290 of file xmlutils.c.

294{
295 entity_t entity;
296 context_data_t *data = (context_data_t *) user_data;
297
298 (void) context;
299 (void) error;
300 if (data->current)
301 {
302 entity_t current = (entity_t) data->current->data;
303 entity = add_entity (&current->entities, element_name, NULL);
304 }
305 else
306 entity = add_entity (NULL, element_name, NULL);
307
308 add_attributes (entity, attribute_names, attribute_values);
309
310 /* "Push" the element. */
311 if (data->first == NULL)
312 data->current = data->first = g_slist_prepend (NULL, entity);
313 else
314 data->current = g_slist_prepend (data->current, entity);
315}
entity_t add_entity(entities_t *entities, const char *name, const char *text)
Add an XML entity to a tree of entities.
Definition xmlutils.c:101
static void add_attributes(entity_t entity, const gchar **names, const gchar **values)
Add attributes from an XML callback to an entity.
Definition xmlutils.c:234

References add_attributes(), add_entity(), context_data_t::current, element_name(), entity_s::entities, and context_data_t::first.

Referenced by parse_entity(), try_read_entity_and_string(), try_read_entity_and_string_s(), and xml_handle_start_element().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ handle_text()

void handle_text ( GMarkupParseContext * context,
const gchar * text,
gsize text_len,
gpointer user_data,
GError ** error )
static

Handle additional text of an XML element.

Parameters
[in]contextParser context.
[in]textThe text.
[in]text_lenLength of the text.
[in]user_dataDummy parameter.
[in]errorError parameter.

Definition at line 437 of file xmlutils.c.

439{
440 context_data_t *data = (context_data_t *) user_data;
441
442 (void) context;
443 (void) text_len;
444 (void) error;
445 entity_t current = (entity_t) data->current->data;
446 if (current->text)
447 {
448 gchar *old = current->text;
449 current->text = g_strconcat (current->text, text, NULL);
450 g_free (old);
451 }
452 else
453 current->text = g_strdup (text);
454}

References context_data_t::current, and entity_s::text.

Referenced by parse_entity(), try_read_entity_and_string(), try_read_entity_and_string_s(), and xml_handle_text().

Here is the caller graph for this function:

◆ ignore_end_element()

void ignore_end_element ( GMarkupParseContext * context,
const gchar * element_name,
gpointer user_data,
GError ** error )
static

Handle the end of an XML element.

Parameters
[in]contextParser context.
[in]element_nameXML element name.
[in]user_dataDummy parameter.
[in]errorError parameter.

Definition at line 343 of file xmlutils.c.

345{
346 context_data_t *data = (context_data_t *) user_data;
347
348 (void) context;
349 (void) element_name;
350 (void) error;
351
352 data->current = GINT_TO_POINTER (GPOINTER_TO_INT (data->current) - 1);
353 if (data->current == NULL)
354 data->done = TRUE;
355}

References context_data_t::current, context_data_t::done, and element_name().

Referenced by try_read_entity_and_string(), and try_read_entity_and_string_s().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ignore_start_element()

void ignore_start_element ( GMarkupParseContext * context,
const gchar * element_name,
const gchar ** attribute_names,
const gchar ** attribute_values,
gpointer user_data,
GError ** error )
static

Handle the start of an OMP XML element.

Parameters
[in]contextParser context.
[in]element_nameXML element name.
[in]attribute_namesXML attribute name.
[in]attribute_valuesXML attribute values.
[in]user_dataDummy parameter.
[in]errorError parameter.

Definition at line 263 of file xmlutils.c.

267{
268 context_data_t *data = (context_data_t *) user_data;
269
270 (void) context;
271 (void) element_name;
272 (void) attribute_names;
273 (void) attribute_values;
274 (void) error;
275
276 data->current = GINT_TO_POINTER (GPOINTER_TO_INT (data->current) + 1);
277}

References context_data_t::current, and element_name().

Referenced by try_read_entity_and_string(), and try_read_entity_and_string_s().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ignore_text()

void ignore_text ( GMarkupParseContext * context,
const gchar * text,
gsize text_len,
gpointer user_data,
GError ** error )
static

Handle additional text of an XML element.

Parameters
[in]contextParser context.
[in]textThe text.
[in]text_lenLength of the text.
[in]user_dataDummy parameter.
[in]errorError parameter.

Definition at line 417 of file xmlutils.c.

419{
420 (void) context;
421 (void) text;
422 (void) text_len;
423 (void) user_data;
424 (void) error;
425}

Referenced by try_read_entity_and_string(), and try_read_entity_and_string_s().

Here is the caller graph for this function:

◆ make_entity()

entity_t make_entity ( const char * name,
const char * text )
static

Create an entity.

Parameters
[in]nameName of the entity. Copied, freed by free_entity.
[in]textText of the entity. Copied, freed by free_entity.
Returns
A newly allocated entity.

Definition at line 48 of file xmlutils.c.

49{
50 entity_t entity;
51 entity = g_malloc (sizeof (*entity));
52 entity->name = g_strdup (name ? name : "");
53 entity->text = g_strdup (text ? text : "");
54 entity->entities = NULL;
55 entity->attributes = NULL;
56 return entity;
57}

References entity_s::attributes, entity_s::entities, entity_s::name, and entity_s::text.

Referenced by add_entity().

Here is the caller graph for this function:

◆ next_entities()

entities_t next_entities ( entities_t entities)

Return all the entities from an entities_t after the first.

Parameters
[in]entitiesThe list of entities.
Returns
All the entities that follow the first.

Definition at line 67 of file xmlutils.c.

68{
69 if (entities)
70 return (entities_t) entities->next;
71 return NULL;
72}

Referenced by Ensure(), osp_get_scanner_details(), and xml_count_entities().

Here is the caller graph for this function:

◆ parse_element()

int parse_element ( const gchar * string,
element_t * element )

Read an XML element tree from a string.

Caller must not free string until caller is finished using element.

Parameters
[in]stringInput string.
[out]elementLocation for parsed element tree, or NULL if not required. If given, set to NULL on failure. Free with element_free.
Returns
0 success, -1 read error, -2 parse error, -3 XML ended prematurely, -4 setup error.

Definition at line 2003 of file xmlutils.c.

2004{
2005 xmlDocPtr doc;
2006
2007 LIBXML_TEST_VERSION
2008
2009 if (element)
2010 *element = NULL;
2011
2012 if (xmlMemSetup (g_free, g_malloc, g_realloc, g_strdup))
2013 return -4;
2014
2015 doc =
2016 xmlReadMemory (string, strlen (string), "noname.xml", NULL, XML_PARSE_HUGE);
2017 if (doc == NULL)
2018 return -2;
2019
2020 if (element)
2021 *element = xmlDocGetRootElement (doc);
2022
2023 return 0;
2024}

Referenced by Ensure(), Ensure(), Ensure(), Ensure(), Ensure(), Ensure(), Ensure(), Ensure(), Ensure(), Ensure(), and Ensure().

Here is the caller graph for this function:

◆ parse_entity()

int parse_entity ( const char * string,
entity_t * entity )

Read an XML entity tree from a string.

Parameters
[in]stringInput string.
[out]entityPointer to an entity tree.
Returns
0 success, -1 read error, -2 parse error, -3 XML ended prematurely.

Definition at line 1511 of file xmlutils.c.

1512{
1513 GMarkupParser xml_parser;
1514 GError *error = NULL;
1515 GMarkupParseContext *xml_context;
1516 context_data_t context_data;
1517
1518 /* Create the XML parser. */
1519
1520 xml_parser.start_element = handle_start_element;
1521 xml_parser.end_element = handle_end_element;
1522 xml_parser.text = handle_text;
1523 xml_parser.passthrough = NULL;
1524 xml_parser.error = handle_error;
1525
1526 context_data.done = FALSE;
1527 context_data.first = NULL;
1528 context_data.current = NULL;
1529
1530 /* Setup the XML context. */
1531
1532 xml_context =
1533 g_markup_parse_context_new (&xml_parser, 0, &context_data, NULL);
1534
1535 /* Parse the string. */
1536
1537 g_markup_parse_context_parse (xml_context, string, strlen (string), &error);
1538 if (error)
1539 {
1540 g_error_free (error);
1541 if (context_data.first && context_data.first->data)
1542 {
1543 free_entity (context_data.first->data);
1544 g_slist_free_1 (context_data.first);
1545 }
1546 g_markup_parse_context_free (xml_context);
1547 return -2;
1548 }
1549 if (context_data.done)
1550 {
1551 g_markup_parse_context_end_parse (xml_context, &error);
1552 if (error)
1553 {
1554 g_warning (" End error: %s\n", error->message);
1555 g_error_free (error);
1556 if (context_data.first && context_data.first->data)
1557 {
1558 free_entity (context_data.first->data);
1559 g_slist_free_1 (context_data.first);
1560 }
1561 g_markup_parse_context_free (xml_context);
1562 return -2;
1563 }
1564 *entity = (entity_t) context_data.first->data;
1565 g_slist_free_1 (context_data.first);
1566 g_markup_parse_context_free (xml_context);
1567 return 0;
1568 }
1569 if (context_data.first && context_data.first->data)
1570 {
1571 free_entity (context_data.first->data);
1572 g_slist_free_1 (context_data.first);
1573 }
1574 g_markup_parse_context_free (xml_context);
1575 return -3;
1576}
static void handle_text(GMarkupParseContext *context, const gchar *text, gsize text_len, gpointer user_data, GError **error)
Handle additional text of an XML element.
Definition xmlutils.c:437
static void handle_end_element(GMarkupParseContext *context, const gchar *element_name, gpointer user_data, GError **error)
Handle the end of an XML element.
Definition xmlutils.c:366
static void handle_error(GMarkupParseContext *context, GError *error, gpointer user_data)
Handle an OMP XML parsing error.
Definition xmlutils.c:477
static void handle_start_element(GMarkupParseContext *context, const gchar *element_name, const gchar **attribute_names, const gchar **attribute_values, gpointer user_data, GError **error)
Handle the start of an OMP XML element.
Definition xmlutils.c:290

References context_data_t::current, context_data_t::done, context_data_t::first, free_entity(), handle_end_element(), handle_error(), handle_start_element(), and handle_text().

Referenced by Ensure(), Ensure(), Ensure(), Ensure(), Ensure(), and Ensure().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ print_element_to_string()

void print_element_to_string ( element_t element,
GString * string )

Print an XML element tree to a GString, appending it if string is not.

empty.

Parameters
[in]elementElement tree to print to string.
[in,out]stringString to write to.

Definition at line 2280 of file xmlutils.c.

2281{
2282 gchar *text_escaped, *text;
2283 element_t ch;
2284 xmlAttr *attribute;
2285
2286 text_escaped = NULL;
2287
2288 g_string_append_printf (string, "<%s", element_name (element));
2289
2290 attribute = element->properties;
2291 while (attribute)
2292 {
2293 xmlChar *value;
2294
2295 value = xmlNodeListGetString (element->doc, attribute->children, 1);
2296
2297 text_escaped = g_markup_escape_text ((gchar *) value, -1);
2298 g_string_append_printf (string, " %s=\"%s\"", attribute->name,
2299 text_escaped);
2300 g_free (text_escaped);
2301
2302 xmlFree (value);
2303
2304 attribute = attribute->next;
2305 }
2306
2307 g_string_append_printf (string, ">");
2308
2309 text = element_text (element);
2310 text_escaped = g_markup_escape_text (text, -1);
2311 g_free (text);
2312 g_string_append_printf (string, "%s", text_escaped);
2313 g_free (text_escaped);
2314
2315 ch = element_first_child (element);
2316 while (ch)
2317 {
2318 print_element_to_string (ch, string);
2319 ch = element_next (ch);
2320 }
2321
2322 g_string_append_printf (string, "</%s>", element_name (element));
2323}
gchar * element_text(element_t element)
Get text of an element.
Definition xmlutils.c:2137
void print_element_to_string(element_t element, GString *string)
Print an XML element tree to a GString, appending it if string is not.
Definition xmlutils.c:2280
element_t element_first_child(element_t element)
Get the first child of an element.
Definition xmlutils.c:2210
element_t element_next(element_t element)
Get the next sibling of an element.
Definition xmlutils.c:2230

References element_first_child(), element_name(), element_next(), element_text(), and print_element_to_string().

Referenced by Ensure(), and print_element_to_string().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ print_entity()

void print_entity ( FILE * stream,
entity_t entity )

Print an XML entity.

Parameters
[in]entityThe entity.
[in]streamThe stream to which to print.

Definition at line 1663 of file xmlutils.c.

1664{
1665 gchar *text_escaped = NULL;
1666 fprintf (stream, "<%s", entity->name);
1667 if (entity->attributes && g_hash_table_size (entity->attributes))
1668 g_hash_table_foreach (entity->attributes, foreach_print_attribute, stream);
1669 fprintf (stream, ">");
1670 text_escaped = g_markup_escape_text (entity->text, -1);
1671 fprintf (stream, "%s", text_escaped);
1672 g_free (text_escaped);
1673 g_slist_foreach (entity->entities, foreach_print_entity, stream);
1674 fprintf (stream, "</%s>", entity->name);
1675 fflush (stream);
1676}
static void foreach_print_entity(gpointer entity, gpointer stream)
Print an XML entity for g_slist_foreach.
Definition xmlutils.c:1638
static void foreach_print_attribute(gpointer name, gpointer value, gpointer stream)
Print an XML attribute for g_hash_table_foreach.
Definition xmlutils.c:1651

References entity_s::attributes, entity_s::entities, foreach_print_attribute(), foreach_print_entity(), entity_s::name, and entity_s::text.

Referenced by foreach_print_entity().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ print_entity_format()

void print_entity_format ( entity_t entity,
gpointer indent )

Print an XML entity to stdout, recursively printing its children.

Does very basic indentation for pretty printing.

This function is used as the (callback) GFunc in g_slist_foreach.

Parameters
[in]entityThe entity.
[in]indentIndentation level, indentation width is 2 spaces. Use GINT_TO_POINTER to convert a integer value when passing this parameter.

Definition at line 1706 of file xmlutils.c.

1707{
1708 int i = 0;
1709 int indentation = GPOINTER_TO_INT (indent);
1710 gchar *text_escaped = NULL;
1711
1712 for (i = 0; i < indentation; i++)
1713 printf (" ");
1714
1715 printf ("<%s", entity->name);
1716 if (entity->attributes && g_hash_table_size (entity->attributes))
1717 g_hash_table_foreach (entity->attributes, foreach_print_attribute_format,
1718 indent);
1719 printf (">");
1720
1721 text_escaped = g_markup_escape_text (entity->text, -1);
1722 printf ("%s", text_escaped);
1723 g_free (text_escaped);
1724
1725 if (entity->entities)
1726 {
1727 printf ("\n");
1728 g_slist_foreach (entity->entities, (GFunc) print_entity_format,
1729 GINT_TO_POINTER (indentation + 1));
1730 for (i = 0; i < indentation; i++)
1731 printf (" ");
1732 }
1733
1734 printf ("</%s>\n", entity->name);
1735}
static void foreach_print_attribute_format(gpointer name, gpointer value, gpointer none)
Print an XML attribute for g_hash_table_foreach to stdout.
Definition xmlutils.c:1688
void print_entity_format(entity_t entity, gpointer indent)
Print an XML entity to stdout, recursively printing its children.
Definition xmlutils.c:1706

References entity_s::attributes, entity_s::entities, foreach_print_attribute_format(), entity_s::name, print_entity_format(), and entity_s::text.

Referenced by print_entity_format().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ print_entity_to_string()

void print_entity_to_string ( entity_t entity,
GString * string )

Print an XML entity tree to a GString, appending it if string is not.

empty.

Parameters
[in]entityEntity tree to print to string.
[in,out]stringString to write to.

Definition at line 1616 of file xmlutils.c.

1617{
1618 gchar *text_escaped = NULL;
1619 g_string_append_printf (string, "<%s", entity->name);
1620 if (entity->attributes && g_hash_table_size (entity->attributes))
1621 g_hash_table_foreach (entity->attributes, foreach_print_attribute_to_string,
1622 string);
1623 g_string_append_printf (string, ">");
1624 text_escaped = g_markup_escape_text (entity->text, -1);
1625 g_string_append_printf (string, "%s", text_escaped);
1626 g_free (text_escaped);
1627 g_slist_foreach (entity->entities, foreach_print_entity_to_string, string);
1628 g_string_append_printf (string, "</%s>", entity->name);
1629}
static void foreach_print_entity_to_string(gpointer entity, gpointer string)
Print an XML entity for g_slist_foreach to a GString.
Definition xmlutils.c:1585
static void foreach_print_attribute_to_string(gpointer name, gpointer value, gpointer string)
Print an XML attribute for g_hash_table_foreach to a GString.
Definition xmlutils.c:1598

References entity_s::attributes, entity_s::entities, foreach_print_attribute_to_string(), foreach_print_entity_to_string(), entity_s::name, and entity_s::text.

Referenced by foreach_print_entity_to_string(), and osp_get_scan_pop().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ read_entity()

int read_entity ( gnutls_session_t * session,
entity_t * entity )

Read an XML entity tree from the manager.

Parameters
[in]sessionPointer to GNUTLS session.
[out]entityPointer to an entity tree.
Returns
0 success, -1 read error, -2 parse error, -3 end of file.

Definition at line 1469 of file xmlutils.c.

1470{
1471 return try_read_entity (session, 0, entity);
1472}
int try_read_entity(gnutls_session_t *session, int timeout, entity_t *entity)
Try read an XML entity tree from the manager.
Definition xmlutils.c:1436

References try_read_entity().

Referenced by gmp_check_response(), gmp_read_create_response(), and osp_send_command().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ read_entity_and_string()

int read_entity_and_string ( gnutls_session_t * session,
entity_t * entity,
GString ** string_return )

Try read an XML entity tree from the manager.

Parameters
[in]sessionPointer to GNUTLS session.
[out]entityPointer to an entity tree.
[out]string_returnAn optional return location for the text read from the session. If NULL then it simply remains NULL. If a pointer to NULL then it points to a freshly allocated GString on successful return. Otherwise it points to an existing GString onto which the text is appended.
Returns
0 success, -1 read error, -2 parse error, -3 end of file.

Definition at line 1262 of file xmlutils.c.

1264{
1265 return try_read_entity_and_string (session, 0, entity, string_return);
1266}
int try_read_entity_and_string(gnutls_session_t *session, int timeout, entity_t *entity, GString **string_return)
Try read an XML entity tree from the manager.
Definition xmlutils.c:501

References try_read_entity_and_string().

Referenced by read_entity_and_text(), and read_string().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ read_entity_and_string_c()

int read_entity_and_string_c ( gvm_connection_t * connection,
entity_t * entity,
GString ** string_return )

Try read an XML entity tree from the manager.

Parameters
[in]connectionConnection.
[out]entityPointer to an entity tree.
[out]string_returnAn optional return location for the text read from the session. If NULL then it simply remains NULL. If a pointer to NULL then it points to a freshly allocated GString on successful return. Otherwise it points to an existing GString onto which the text is appended.
Returns
0 success, -1 read error, -2 parse error, -3 end of file.

Definition at line 1282 of file xmlutils.c.

1284{
1285 if (connection->tls)
1286 return try_read_entity_and_string (&connection->session, 0, entity,
1287 string_return);
1288 return try_read_entity_and_string_s (connection->socket, 0, entity,
1289 string_return);
1290}
int tls
Whether uses TCP-TLS (vs UNIX socket).
Definition serverutils.h:31
int socket
Socket.
Definition serverutils.h:32
gnutls_session_t session
Session.
Definition serverutils.h:33
static int try_read_entity_and_string_s(int socket, int timeout, entity_t *entity, GString **string_return)
Try read an XML entity tree from the socket.
Definition xmlutils.c:1030

References gvm_connection_t::session, gvm_connection_t::socket, gvm_connection_t::tls, try_read_entity_and_string(), and try_read_entity_and_string_s().

Referenced by read_entity_and_text_c(), and read_string_c().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ read_entity_and_text()

int read_entity_and_text ( gnutls_session_t * session,
entity_t * entity,
char ** text )

Read an XML entity tree from the manager.

Parameters
[in]sessionPointer to GNUTLS session.
[out]entityPointer to an entity tree.
[out]textA pointer to a pointer, at which to store the address of a newly allocated string holding the text read from the session, if the text is required, else NULL.
Returns
0 success, -1 read error, -2 parse error, -3 end of file.

Definition at line 1305 of file xmlutils.c.

1306{
1307 if (text)
1308 {
1309 GString *string = NULL;
1310 int ret = read_entity_and_string (session, entity, &string);
1311 if (ret)
1312 {
1313 if (string)
1314 g_string_free (string, TRUE);
1315 return ret;
1316 }
1317 *text = g_string_free (string, FALSE);
1318 return 0;
1319 }
1320 return read_entity_and_string (session, entity, NULL);
1321}
int read_entity_and_string(gnutls_session_t *session, entity_t *entity, GString **string_return)
Try read an XML entity tree from the manager.
Definition xmlutils.c:1262

References read_entity_and_string().

Here is the call graph for this function:

◆ read_entity_and_text_c()

int read_entity_and_text_c ( gvm_connection_t * connection,
entity_t * entity,
char ** text )

Read an XML entity tree from the manager.

Parameters
[in]connectionConnection.
[out]entityEntity tree.
[out]textA pointer to a pointer, at which to store the address of a newly allocated string holding the text read from the session, if the text is required, else NULL.
Returns
0 success, -1 read error, -2 parse error, -3 end of file.

Definition at line 1336 of file xmlutils.c.

1338{
1339 if (text)
1340 {
1341 GString *string = NULL;
1342 int ret = read_entity_and_string_c (connection, entity, &string);
1343 if (ret)
1344 {
1345 if (string)
1346 g_string_free (string, TRUE);
1347 return ret;
1348 }
1349 *text = g_string_free (string, FALSE);
1350 return 0;
1351 }
1352 return read_entity_and_string_c (connection, entity, NULL);
1353}
int read_entity_and_string_c(gvm_connection_t *connection, entity_t *entity, GString **string_return)
Try read an XML entity tree from the manager.
Definition xmlutils.c:1282

References read_entity_and_string_c().

Here is the call graph for this function:

◆ read_entity_c()

int read_entity_c ( gvm_connection_t * connection,
entity_t * entity )

Read an XML entity tree from the manager.

Parameters
[in]connectionConnection.
[out]entityPointer to an entity tree.
Returns
0 success, -1 read error, -2 parse error, -3 end of file.

Definition at line 1497 of file xmlutils.c.

1498{
1499 return try_read_entity_c (connection, 0, entity);
1500}
int try_read_entity_c(gvm_connection_t *connection, int timeout, entity_t *entity)
Try read an XML entity tree from the manager.
Definition xmlutils.c:1452

References try_read_entity_c().

Referenced by check_response_c(), gmp_resume_task_report_c(), and gmp_start_task_report_c().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ read_entity_s()

int read_entity_s ( int socket,
entity_t * entity )

Read an XML entity tree from the socket.

Parameters
[in]socketSocket to read from.
[out]entityPointer to an entity tree.
Returns
0 success, -1 read error, -2 parse error, -3 end of file.

Definition at line 1483 of file xmlutils.c.

1484{
1485 return try_read_entity_and_string_s (socket, 0, entity, NULL);
1486}

References try_read_entity_and_string_s().

Referenced by osp_send_command().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ read_string()

int read_string ( gnutls_session_t * session,
GString ** string )

Read entity and text. Free the entity immediately.

Parameters
[in]sessionPointer to GNUTLS session to read from.
[out]stringReturn location for the string.
Returns
0 success, -1 read error, -2 parse error, -3 end of file.

Definition at line 1400 of file xmlutils.c.

1401{
1402 int ret = 0;
1403 entity_t entity;
1404
1405 if (!(ret = read_entity_and_string (session, &entity, string)))
1406 free_entity (entity);
1407
1408 return ret;
1409}

References free_entity(), and read_entity_and_string().

Here is the call graph for this function:

◆ read_string_c()

int read_string_c ( gvm_connection_t * connection,
GString ** string )

Read entity and text. Free the entity immediately.

Parameters
[in]connectionConnection.
[out]stringReturn location for the string.
Returns
0 success, -1 read error, -2 parse error, -3 end of file.

Definition at line 1420 of file xmlutils.c.

1421{
1422 return read_entity_and_string_c (connection, NULL, string);
1423}

References read_entity_and_string_c().

Here is the call graph for this function:

◆ read_text_c()

int read_text_c ( gvm_connection_t * connection,
char ** text )

Read text from the server.

Parameters
[in]connectionConnection.
[out]textA pointer to a pointer, at which to store the address of a newly allocated string holding the text read from the session.
Returns
0 success, -1 read error, -2 argument error.

Definition at line 1366 of file xmlutils.c.

1367{
1368 GString *string;
1369 int ret;
1370
1371 if (text == NULL)
1372 return -2;
1373
1374 string = NULL;
1375
1376 if (connection->tls)
1377 ret = try_read_string (&connection->session, 0, &string);
1378 else
1379 ret = try_read_string_s (connection->socket, 0, &string);
1380
1381 if (ret)
1382 {
1383 if (string)
1384 g_string_free (string, TRUE);
1385 return ret;
1386 }
1387 *text = g_string_free (string, FALSE);
1388 return 0;
1389}
static int try_read_string_s(int socket, int timeout, GString **string_return)
Try read an XML entity tree from the socket.
Definition xmlutils.c:900
static int try_read_string(gnutls_session_t *session, int timeout, GString **string_return)
Try read a response from a TLS session.
Definition xmlutils.c:752

References gvm_connection_t::session, gvm_connection_t::socket, gvm_connection_t::tls, try_read_string(), and try_read_string_s().

Referenced by osp_send_command_str().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ try_read_entity()

int try_read_entity ( gnutls_session_t * session,
int timeout,
entity_t * entity )

Try read an XML entity tree from the manager.

Parameters
[in]sessionPointer to GNUTLS session.
[in]timeoutServer idle time before giving up, in seconds. 0 to wait forever.
[out]entityPointer to an entity tree.
Returns
0 success, -1 read error, -2 parse error, -3 end of file, -4 timeout.

Definition at line 1436 of file xmlutils.c.

1437{
1438 return try_read_entity_and_string (session, timeout, entity, NULL);
1439}

References try_read_entity_and_string().

Referenced by gmp_authenticate_info_ext(), gmp_get_report_ext(), gmp_get_tasks_ext(), gmp_ping(), and read_entity().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ try_read_entity_and_string()

int try_read_entity_and_string ( gnutls_session_t * session,
int timeout,
entity_t * entity,
GString ** string_return )

Try read an XML entity tree from the manager.

Parameters
[in]sessionPointer to GNUTLS session.
[in]timeoutServer idle time before giving up, in seconds. 0 to wait forever.
[out]entityPointer to an entity tree.
[out]string_returnAn optional return location for the text read from the session. If NULL then it simply remains NULL. If a pointer to NULL then it points to a freshly allocated GString on successful return. Otherwise it points to an existing GString onto which the text is appended.
Returns
0 success, -1 read error, -2 parse error, -3 end of file, -4 timeout, -5 null buffer.

Definition at line 501 of file xmlutils.c.

503{
504 GMarkupParser xml_parser;
505 GError *error = NULL;
506 GMarkupParseContext *xml_context;
507 GString *string;
508 int socket;
509 time_t last_time;
510
511 // Buffer for reading from the manager.
512 char *buffer;
513
514 /* Record the start time. */
515
516 if (time (&last_time) == -1)
517 {
518 g_warning (" failed to get current time: %s\n", strerror (errno));
519 return -1;
520 }
521
522 if (timeout > 0)
523 {
524 /* Turn off blocking. */
525
526 socket = GPOINTER_TO_INT (gnutls_transport_get_ptr (*session));
527 if (fcntl (socket, F_SETFL, O_NONBLOCK) == -1)
528 return -1;
529 }
530 else
531 /* Quiet compiler. */
532 socket = 0;
533
534 buffer = g_malloc0 (BUFFER_SIZE);
535 if (!buffer)
536 return -5;
537
538 /* Setup return arg. */
539
540 if (string_return == NULL)
541 string = NULL;
542 else if (*string_return == NULL)
543 string = g_string_new ("");
544 else
545 string = *string_return;
546
547 /* Create the XML parser. */
548
549 if (entity)
550 {
551 xml_parser.start_element = handle_start_element;
552 xml_parser.end_element = handle_end_element;
553 xml_parser.text = handle_text;
554 }
555 else
556 {
557 xml_parser.start_element = ignore_start_element;
558 xml_parser.end_element = ignore_end_element;
559 xml_parser.text = ignore_text;
560 }
561 xml_parser.passthrough = NULL;
562 xml_parser.error = handle_error;
563
564 context_data_t context_data;
565 context_data.done = FALSE;
566 context_data.first = NULL;
567 context_data.current = NULL;
568
569 /* Setup the XML context. */
570
571 xml_context =
572 g_markup_parse_context_new (&xml_parser, 0, &context_data, NULL);
573
574 /* Read and parse, until encountering end of file or error. */
575
576 while (1)
577 {
578 ssize_t count;
579 int retries = 10;
580 while (1)
581 {
582 g_debug (" asking for %i\n", BUFFER_SIZE);
583 count = gnutls_record_recv (*session, buffer, BUFFER_SIZE);
584 if (count < 0)
585 {
586 if (count == GNUTLS_E_INTERRUPTED)
587 /* Interrupted, try read again. */
588 continue;
589 if ((timeout > 0) && (count == GNUTLS_E_AGAIN))
590 {
591 /* Server still busy, either timeout or try read again. */
592 if ((timeout - (time (NULL) - last_time)) <= 0)
593 {
594 g_warning (" timeout\n");
595 if (fcntl (socket, F_SETFL, 0L) < 0)
596 g_warning ("%s :failed to set socket flag: %s",
597 __func__, strerror (errno));
598 g_markup_parse_context_free (xml_context);
599 g_free (buffer);
600 return -4;
601 }
602 continue;
603 }
604 else if ((timeout == 0) && (count == GNUTLS_E_AGAIN))
605 {
606 /* Server still busy, try read again.
607 If there is no timeout set and the server is still not
608 ready, it will try up to 10 times before closing the
609 socket.*/
610 if (retries > 0)
611 {
612 retries = retries - 1;
613 continue;
614 }
615 }
616
617 if (count == GNUTLS_E_REHANDSHAKE)
618 /* Try again. TODO Rehandshake. */
619 continue;
620 if (context_data.first && context_data.first->data)
621 {
622 free_entity (context_data.first->data);
623 g_slist_free_1 (context_data.first);
624 }
625 if (string && *string_return == NULL)
626 g_string_free (string, TRUE);
627 if (timeout > 0)
628 {
629 if (fcntl (socket, F_SETFL, 0L) < 0)
630 g_warning ("%s :failed to set socket flag: %s", __func__,
631 strerror (errno));
632 }
633 g_markup_parse_context_free (xml_context);
634 g_free (buffer);
635 return -1;
636 }
637 if (count == 0)
638 {
639 /* End of file. */
640 g_markup_parse_context_end_parse (xml_context, &error);
641 if (error)
642 {
643 g_warning (" End error: %s\n", error->message);
644 g_error_free (error);
645 }
646 if (context_data.first && context_data.first->data)
647 {
648 free_entity (context_data.first->data);
649 g_slist_free_1 (context_data.first);
650 }
651 if (string && *string_return == NULL)
652 g_string_free (string, TRUE);
653 if (timeout > 0)
654 {
655 if (fcntl (socket, F_SETFL, 0L) < 0)
656 g_warning ("%s :failed to set socket flag: %s", __func__,
657 strerror (errno));
658 }
659 g_markup_parse_context_free (xml_context);
660 g_free (buffer);
661 return -3;
662 }
663 break;
664 }
665
666 g_debug ("<= %.*s\n", (int) count, buffer);
667
668 if (string)
669 g_string_append_len (string, buffer, count);
670
671 g_markup_parse_context_parse (xml_context, buffer, count, &error);
672 if (error)
673 {
674 g_error_free (error);
675 if (context_data.first && context_data.first->data)
676 {
677 free_entity (context_data.first->data);
678 g_slist_free_1 (context_data.first);
679 }
680 if (string && *string_return == NULL)
681 g_string_free (string, TRUE);
682 if (timeout > 0)
683 {
684 if (fcntl (socket, F_SETFL, 0L) < 0)
685 g_warning ("%s :failed to set socket flag: %s", __func__,
686 strerror (errno));
687 }
688 g_markup_parse_context_free (xml_context);
689 g_free (buffer);
690 return -2;
691 }
692 if (context_data.done)
693 {
694 g_markup_parse_context_end_parse (xml_context, &error);
695 if (error)
696 {
697 g_warning (" End error: %s\n", error->message);
698 g_error_free (error);
699 if (context_data.first && context_data.first->data)
700 {
701 free_entity (context_data.first->data);
702 g_slist_free_1 (context_data.first);
703 }
704 if (timeout > 0)
705 fcntl (socket, F_SETFL, 0L);
706 g_markup_parse_context_free (xml_context);
707 g_free (buffer);
708 return -2;
709 }
710 if (entity)
711 *entity = (entity_t) context_data.first->data;
712 if (string)
713 *string_return = string;
714 if (timeout > 0)
715 fcntl (socket, F_SETFL, 0L);
716 g_markup_parse_context_free (xml_context);
717 g_free (buffer);
718 return 0;
719 }
720
721 if ((timeout > 0) && (time (&last_time) == -1))
722 {
723 g_warning (" failed to get current time (1): %s\n",
724 strerror (errno));
725 if (fcntl (socket, F_SETFL, 0L) < 0)
726 g_warning ("%s :failed to set socket flag: %s", __func__,
727 strerror (errno));
728 g_markup_parse_context_free (xml_context);
729 g_free (buffer);
730 return -1;
731 }
732 }
733}
static void ignore_text(GMarkupParseContext *context, const gchar *text, gsize text_len, gpointer user_data, GError **error)
Handle additional text of an XML element.
Definition xmlutils.c:417
#define BUFFER_SIZE
Size of the buffer for reading from the manager.
Definition xmlutils.c:37
static void ignore_start_element(GMarkupParseContext *context, const gchar *element_name, const gchar **attribute_names, const gchar **attribute_values, gpointer user_data, GError **error)
Handle the start of an OMP XML element.
Definition xmlutils.c:263
static void ignore_end_element(GMarkupParseContext *context, const gchar *element_name, gpointer user_data, GError **error)
Handle the end of an XML element.
Definition xmlutils.c:343

References BUFFER_SIZE, context_data_t::current, context_data_t::done, context_data_t::first, free_entity(), handle_end_element(), handle_error(), handle_start_element(), handle_text(), ignore_end_element(), ignore_start_element(), and ignore_text().

Referenced by read_entity_and_string(), read_entity_and_string_c(), try_read_entity(), and try_read_entity_c().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ try_read_entity_and_string_s()

int try_read_entity_and_string_s ( int socket,
int timeout,
entity_t * entity,
GString ** string_return )
static

Try read an XML entity tree from the socket.

Parameters
[in]socketSocket to read from.
[in]timeoutServer idle time before giving up, in seconds. 0 to wait forever.
[out]entityPointer to an entity tree.
[out]string_returnAn optional return location for the text read from the session. If NULL then it simply remains NULL. If a pointer to NULL then it points to a freshly allocated GString on successful return. Otherwise it points to an existing GString onto which the text is appended.
Returns
0 success, -1 read error, -2 parse error, -3 end of file, -4 timeout, -5 null buffer.

Definition at line 1030 of file xmlutils.c.

1032{
1033 GMarkupParser xml_parser;
1034 GError *error = NULL;
1035 GMarkupParseContext *xml_context;
1036 GString *string;
1037 time_t last_time;
1038 /* Buffer for reading from the socket. */
1039 char *buffer;
1040
1041 /* Record the start time. */
1042
1043 if (time (&last_time) == -1)
1044 {
1045 g_warning (" failed to get current time: %s\n", strerror (errno));
1046 return -1;
1047 }
1048
1049 if (timeout > 0)
1050 {
1051 /* Turn off blocking. */
1052
1053 if (fcntl (socket, F_SETFL, O_NONBLOCK) == -1)
1054 return -1;
1055 }
1056
1057 buffer = g_malloc0 (BUFFER_SIZE);
1058 if (!buffer)
1059 return -5;
1060
1061 /* Setup return arg. */
1062
1063 if (string_return == NULL)
1064 string = NULL;
1065 else if (*string_return == NULL)
1066 string = g_string_new ("");
1067 else
1068 string = *string_return;
1069
1070 /* Create the XML parser. */
1071
1072 if (entity)
1073 {
1074 xml_parser.start_element = handle_start_element;
1075 xml_parser.end_element = handle_end_element;
1076 xml_parser.text = handle_text;
1077 }
1078 else
1079 {
1080 xml_parser.start_element = ignore_start_element;
1081 xml_parser.end_element = ignore_end_element;
1082 xml_parser.text = ignore_text;
1083 }
1084 xml_parser.passthrough = NULL;
1085 xml_parser.error = handle_error;
1086
1087 context_data_t context_data;
1088 context_data.done = FALSE;
1089 context_data.first = NULL;
1090 context_data.current = NULL;
1091
1092 /* Setup the XML context. */
1093
1094 xml_context =
1095 g_markup_parse_context_new (&xml_parser, 0, &context_data, NULL);
1096
1097 /* Read and parse, until encountering end of file or error. */
1098
1099 while (1)
1100 {
1101 int count;
1102 while (1)
1103 {
1104 g_debug (" asking for %i\n", BUFFER_SIZE);
1105 count = read (socket, buffer, BUFFER_SIZE);
1106 if (count < 0)
1107 {
1108 if (errno == EINTR)
1109 /* Interrupted, try read again. */
1110 continue;
1111 if (timeout > 0)
1112 {
1113 if (errno == EAGAIN)
1114 {
1115 /* Server still busy, either timeout or try read again. */
1116 if ((timeout - (time (NULL) - last_time)) <= 0)
1117 {
1118 g_warning (" timeout\n");
1119 if (fcntl (socket, F_SETFL, 0L) < 0)
1120 g_warning ("%s :failed to set socket flag: %s",
1121 __func__, strerror (errno));
1122 g_markup_parse_context_free (xml_context);
1123 g_free (buffer);
1124 if (string && *string_return == NULL)
1125 g_string_free (string, TRUE);
1126 return -4;
1127 }
1128 }
1129 continue;
1130 }
1131 if (context_data.first && context_data.first->data)
1132 {
1133 free_entity (context_data.first->data);
1134 g_slist_free_1 (context_data.first);
1135 }
1136 if (string && *string_return == NULL)
1137 g_string_free (string, TRUE);
1138 if (timeout > 0)
1139 fcntl (socket, F_SETFL, 0L);
1140 g_markup_parse_context_free (xml_context);
1141 g_free (buffer);
1142 return -1;
1143 }
1144 if (count == 0)
1145 {
1146 /* End of file. */
1147 g_markup_parse_context_end_parse (xml_context, &error);
1148 if (error)
1149 {
1150 g_warning (" End error: %s\n", error->message);
1151 g_error_free (error);
1152 }
1153 if (context_data.first && context_data.first->data)
1154 {
1155 free_entity (context_data.first->data);
1156 g_slist_free_1 (context_data.first);
1157 }
1158 if (string && *string_return == NULL)
1159 g_string_free (string, TRUE);
1160 if (timeout > 0)
1161 {
1162 if (fcntl (socket, F_SETFL, 0L) < 0)
1163 g_warning ("%s :failed to set socket flag: %s", __func__,
1164 strerror (errno));
1165 }
1166 g_markup_parse_context_free (xml_context);
1167 g_free (buffer);
1168 return -3;
1169 }
1170 break;
1171 }
1172
1173 g_debug ("<= %.*s\n", (int) count, buffer);
1174
1175 if (string)
1176 g_string_append_len (string, buffer, count);
1177
1178 g_markup_parse_context_parse (xml_context, buffer, count, &error);
1179 if (error)
1180 {
1181 g_error_free (error);
1182 // FIX there may be multiple entries in list
1183 if (context_data.first && context_data.first->data)
1184 {
1185 free_entity (context_data.first->data);
1186 g_slist_free_1 (context_data.first);
1187 }
1188 if (string && *string_return == NULL)
1189 g_string_free (string, TRUE);
1190 if (timeout > 0)
1191 {
1192 if (fcntl (socket, F_SETFL, 0L) < 0)
1193 g_warning ("%s :failed to set socket flag: %s", __func__,
1194 strerror (errno));
1195 }
1196 g_markup_parse_context_free (xml_context);
1197 g_free (buffer);
1198 return -2;
1199 }
1200 if (context_data.done)
1201 {
1202 g_markup_parse_context_end_parse (xml_context, &error);
1203 if (error)
1204 {
1205 g_warning (" End error: %s\n", error->message);
1206 g_error_free (error);
1207 if (context_data.first && context_data.first->data)
1208 {
1209 free_entity (context_data.first->data);
1210 g_slist_free_1 (context_data.first);
1211 }
1212 if (timeout > 0)
1213 fcntl (socket, F_SETFL, 0L);
1214 g_markup_parse_context_free (xml_context);
1215 g_free (buffer);
1216 if (string && *string_return == NULL)
1217 g_string_free (string, TRUE);
1218 return -2;
1219 }
1220 if (entity)
1221 *entity = (entity_t) context_data.first->data;
1222 if (string)
1223 *string_return = string;
1224 if (timeout > 0)
1225 fcntl (socket, F_SETFL, 0L);
1226 g_slist_free (context_data.first);
1227 g_markup_parse_context_free (xml_context);
1228 g_free (buffer);
1229 return 0;
1230 }
1231
1232 if ((timeout > 0) && (time (&last_time) == -1))
1233 {
1234 g_warning (" failed to get current time (1): %s\n",
1235 strerror (errno));
1236 if (fcntl (socket, F_SETFL, 0L) < 0)
1237 g_warning ("%s :failed to set server socket flag: %s", __func__,
1238 strerror (errno));
1239 g_markup_parse_context_free (xml_context);
1240 g_free (buffer);
1241 if (string && *string_return == NULL)
1242 g_string_free (string, TRUE);
1243 return -1;
1244 }
1245 }
1246}

References BUFFER_SIZE, context_data_t::current, context_data_t::done, context_data_t::first, free_entity(), handle_end_element(), handle_error(), handle_start_element(), handle_text(), ignore_end_element(), ignore_start_element(), and ignore_text().

Referenced by read_entity_and_string_c(), read_entity_s(), and try_read_entity_c().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ try_read_entity_c()

int try_read_entity_c ( gvm_connection_t * connection,
int timeout,
entity_t * entity )

Try read an XML entity tree from the manager.

Parameters
[in]connectionConnection.
[in]timeoutServer idle time before giving up, in seconds. 0 to wait forever.
[out]entityPointer to an entity tree.
Returns
0 success, -1 read error, -2 parse error, -3 end of file, -4 timeout.

Definition at line 1452 of file xmlutils.c.

1453{
1454 if (connection->tls)
1455 return try_read_entity_and_string (&connection->session, 0, entity, NULL);
1456 return try_read_entity_and_string_s (connection->socket, timeout, entity,
1457 NULL);
1458}

References gvm_connection_t::session, gvm_connection_t::socket, gvm_connection_t::tls, try_read_entity_and_string(), and try_read_entity_and_string_s().

Referenced by gmp_authenticate_info_ext_c(), gmp_ping_c(), and read_entity_c().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ try_read_string()

int try_read_string ( gnutls_session_t * session,
int timeout,
GString ** string_return )
static

Try read a response from a TLS session.

Parameters
[in]sessionPointer to GNUTLS session.
[in]timeoutServer idle time before giving up, in seconds. 0 to wait forever.
[out]string_returnAn optional return location for the text read from the session.

If string_return is NULL then it simply remains NULL. If it is pointer to NULL then it points to a freshly allocated GString on successful return. Otherwise it must point to an existing GString onto which the text is appended.

Returns
0 success, -1 read error, -4 timeout, -5 null buffer.

Definition at line 752 of file xmlutils.c.

754{
755 GString *string;
756 int socket;
757 time_t last_time;
758 char *buffer; // Buffer for reading from the server.
759
760 /* Record the start time. */
761
762 if (time (&last_time) == -1)
763 {
764 g_warning (" failed to get current time: %s\n", strerror (errno));
765 return -1;
766 }
767
768 if (timeout > 0)
769 {
770 /* Turn off blocking. */
771
772 socket = GPOINTER_TO_INT (gnutls_transport_get_ptr (*session));
773 if (fcntl (socket, F_SETFL, O_NONBLOCK) == -1)
774 return -1;
775 }
776 else
777 /* Quiet compiler. */
778 socket = 0;
779
780 buffer = g_malloc0 (BUFFER_SIZE);
781 if (!buffer)
782 return -5;
783
784 /* Setup return arg. */
785
786 if (string_return == NULL)
787 string = NULL;
788 else if (*string_return == NULL)
789 string = g_string_new ("");
790 else
791 string = *string_return;
792
793 /* Read until encountering end of file or error. */
794
795 while (1)
796 {
797 ssize_t count;
798 int retries = 10;
799 while (1)
800 {
801 g_debug (" asking for %i\n", BUFFER_SIZE);
802 count = gnutls_record_recv (*session, buffer, BUFFER_SIZE);
803 if (count < 0)
804 {
805 if (count == GNUTLS_E_INTERRUPTED)
806 /* Interrupted, try read again. */
807 continue;
808 if ((timeout > 0) && (count == GNUTLS_E_AGAIN))
809 {
810 /* Server still busy, either timeout or try read again. */
811 if ((timeout - (time (NULL) - last_time)) <= 0)
812 {
813 g_warning (" timeout\n");
814 if (fcntl (socket, F_SETFL, 0L) < 0)
815 g_warning ("%s: failed to set socket flag: %s",
816 __func__, strerror (errno));
817 g_free (buffer);
818 return -4;
819 }
820 continue;
821 }
822 else if ((timeout == 0) && (count == GNUTLS_E_AGAIN))
823 {
824 /* Server still busy, try read again.
825 * If there is no timeout set and the server is still not
826 * ready, it will try up to 10 times before closing the
827 * socket. */
828 if (retries > 0)
829 {
830 retries = retries - 1;
831 continue;
832 }
833 }
834
835 if (count == GNUTLS_E_REHANDSHAKE)
836 /* Try again. TODO Rehandshake. */
837 continue;
838 if (string && (*string_return == NULL))
839 g_string_free (string, TRUE);
840 if (timeout > 0)
841 {
842 if (fcntl (socket, F_SETFL, 0L) < 0)
843 g_warning ("%s: failed to set socket flag: %s", __func__,
844 strerror (errno));
845 }
846 g_free (buffer);
847 return -1;
848 }
849 if (count == 0)
850 {
851 /* End of file. */
852 if (timeout > 0)
853 {
854 if (fcntl (socket, F_SETFL, 0L) < 0)
855 g_warning ("%s :failed to set socket flag: %s", __func__,
856 strerror (errno));
857 }
858 if (string)
859 *string_return = string;
860 g_free (buffer);
861 return 0;
862 }
863 break;
864 }
865
866 g_debug ("<= %.*s\n", (int) count, buffer);
867
868 if (string)
869 g_string_append_len (string, buffer, count);
870
871 if ((timeout > 0) && (time (&last_time) == -1))
872 {
873 g_warning (" failed to get current time (1): %s\n",
874 strerror (errno));
875 if (fcntl (socket, F_SETFL, 0L) < 0)
876 g_warning ("%s :failed to set socket flag: %s", __func__,
877 strerror (errno));
878 g_free (buffer);
879 return -1;
880 }
881 }
882}

References BUFFER_SIZE.

Referenced by read_text_c().

Here is the caller graph for this function:

◆ try_read_string_s()

int try_read_string_s ( int socket,
int timeout,
GString ** string_return )
static

Try read an XML entity tree from the socket.

Parameters
[in]socketSocket to read from.
[in]timeoutServer idle time before giving up, in seconds. 0 to wait forever.
[out]string_returnAn optional return location for the text read from the socket. If NULL then it simply remains NULL. If a pointer to NULL then it points to a freshly allocated GString on successful return. Otherwise it points to an existing GString onto which the text is appended.
Returns
0 success, -1 read error, -2 parse error, -3 end of file, -4 timeout, -5 null buffer.

Definition at line 900 of file xmlutils.c.

901{
902 GString *string;
903 time_t last_time;
904 /* Buffer for reading from the socket. */
905 char *buffer;
906
907 /* Record the start time. */
908
909 if (time (&last_time) == -1)
910 {
911 g_warning (" failed to get current time: %s\n", strerror (errno));
912 return -1;
913 }
914
915 if (timeout > 0)
916 {
917 /* Turn off blocking. */
918
919 if (fcntl (socket, F_SETFL, O_NONBLOCK) == -1)
920 return -1;
921 }
922
923 buffer = g_malloc0 (BUFFER_SIZE);
924 if (!buffer)
925 return -5;
926
927 /* Setup return arg. */
928
929 if (string_return == NULL)
930 string = NULL;
931 else if (*string_return == NULL)
932 string = g_string_sized_new (8192);
933 else
934 string = *string_return;
935
936 /* Read until encountering end of file or error. */
937
938 while (1)
939 {
940 int count;
941 while (1)
942 {
943 g_debug (" asking for %i\n", BUFFER_SIZE);
944 count = read (socket, buffer, BUFFER_SIZE);
945 if (count < 0)
946 {
947 if (errno == EINTR)
948 /* Interrupted, try read again. */
949 continue;
950 if (timeout > 0)
951 {
952 if (errno == EAGAIN)
953 {
954 /* Server still busy, either timeout or try read again. */
955 if ((timeout - (time (NULL) - last_time)) <= 0)
956 {
957 g_warning (" timeout\n");
958 if (fcntl (socket, F_SETFL, 0L) < 0)
959 g_warning ("%s :failed to set socket flag: %s",
960 __func__, strerror (errno));
961 g_free (buffer);
962 if (string && *string_return == NULL)
963 g_string_free (string, TRUE);
964 return -4;
965 }
966 }
967 continue;
968 }
969 if (string && *string_return == NULL)
970 g_string_free (string, TRUE);
971 if (timeout > 0)
972 fcntl (socket, F_SETFL, 0L);
973 g_free (buffer);
974 return -1;
975 }
976 if (count == 0)
977 {
978 /* End of file. */
979 if (timeout > 0)
980 {
981 if (fcntl (socket, F_SETFL, 0L) < 0)
982 g_warning ("%s :failed to set socket flag: %s", __func__,
983 strerror (errno));
984 }
985 if (string)
986 *string_return = string;
987 g_free (buffer);
988 return 0;
989 }
990 break;
991 }
992
993 g_debug ("<= %.*s\n", (int) count, buffer);
994
995 if (string)
996 g_string_append_len (string, buffer, count);
997
998 if ((timeout > 0) && (time (&last_time) == -1))
999 {
1000 g_warning (" failed to get current time (1): %s\n",
1001 strerror (errno));
1002 if (fcntl (socket, F_SETFL, 0L) < 0)
1003 g_warning ("%s :failed to set server socket flag: %s", __func__,
1004 strerror (errno));
1005 g_free (buffer);
1006 if (string && *string_return == NULL)
1007 g_string_free (string, TRUE);
1008 return -1;
1009 }
1010 }
1011}

References BUFFER_SIZE.

Referenced by read_text_c().

Here is the caller graph for this function:

◆ xml_count_entities()

int xml_count_entities ( entities_t entities)

Count the number of entities.

Parameters
[in]entitiesEntities.
Returns
Number of entities.

Definition at line 1830 of file xmlutils.c.

1831{
1832 int count = 0;
1833 while (first_entity (entities))
1834 {
1835 entities = next_entities (entities);
1836 count++;
1837 }
1838 return count;
1839}
entity_t first_entity(entities_t entities)
Return the first entity from an entities_t.
Definition xmlutils.c:82
entities_t next_entities(entities_t entities)
Return all the entities from an entities_t after the first.
Definition xmlutils.c:67

References first_entity(), and next_entities().

Here is the call graph for this function:

◆ xml_file_iterator_attribute_decl()

void xml_file_iterator_attribute_decl ( void * ctx,
const xmlChar * elem,
const xmlChar * fullname,
int type,
int def,
const xmlChar * defaultValue,
xmlEnumerationPtr tree )
static

XML file iterator parser callback for when an attribute definition has been parsed.

This is just a wrapper for the libXML xmlSAX2AttributeDecl getting the libXML parser context from the iterator struct passed as user data.

Parameters
[in]ctxparser context data / iterator data structure
[in]namethe name of the element
[in]fullnamethe attribute name
[in]typethe attribute type
[in]defthe type of default value
[in]defaultValuethe attribute default value
[in]treethe tree of enumerated value set

Definition at line 2615 of file xmlutils.c.

2619{
2621 xmlSAX2AttributeDecl (iterator->parser_ctxt, elem, fullname, type, def,
2622 defaultValue, tree);
2623}
xmlParserCtxtPtr parser_ctxt
Definition xmlutils.c:2336
struct xml_file_iterator_struct * xml_file_iterator_t
Definition xmlutils.h:188

References xml_file_iterator_struct::parser_ctxt.

Referenced by xml_file_iterator_init_sax_handler().

Here is the caller graph for this function:

◆ xml_file_iterator_cdata_block()

void xml_file_iterator_cdata_block ( void * ctx,
const xmlChar * ch,
int len )
static

XML file iterator parser callback when a cdata block has been parsed.

This is just a wrapper for the libXML xmlSAX2CDataBlock getting the libXML parser context from the iterator struct passed as user data.

Parameters
[in]ctxparser context data / iterator data structure
[in]chThe cdata content
[in]lenthe block length

Definition at line 2768 of file xmlutils.c.

2769{
2771 xmlSAX2CDataBlock (iterator->parser_ctxt, ch, len);
2772}

References xml_file_iterator_struct::parser_ctxt.

Referenced by xml_file_iterator_init_sax_handler().

Here is the caller graph for this function:

◆ xml_file_iterator_characters()

void xml_file_iterator_characters ( void * ctx,
const xmlChar * ch,
int len )
static

XML file iterator parser callback when receiving some chars from the parser.

This is just a wrapper for the libXML xmlSAX2Characters getting the libXML parser context from the iterator struct passed as user data.

Parameters
[in]ctxparser context data / iterator data structure
[in]cha xmlChar string
[in]lenthe number of xmlChar

Definition at line 2750 of file xmlutils.c.

2751{
2753 xmlSAX2Characters (iterator->parser_ctxt, ch, len);
2754}

References xml_file_iterator_struct::parser_ctxt.

Referenced by xml_file_iterator_init_sax_handler().

Here is the caller graph for this function:

◆ xml_file_iterator_comment()

void xml_file_iterator_comment ( void * ctx,
const xmlChar * value )
static

XML file iterator parser callback when a comment has been parsed.

This is just a wrapper for the libXML xmlSAX2Comment getting the libXML parser context from the iterator struct passed as user data.

Parameters
[in]ctxparser context data / iterator data structure
[in]valuethe comment content

Definition at line 2803 of file xmlutils.c.

2804{
2806 xmlSAX2Comment (iterator->parser_ctxt, value);
2807}

References xml_file_iterator_struct::parser_ctxt.

Referenced by xml_file_iterator_init_sax_handler().

Here is the caller graph for this function:

◆ xml_file_iterator_element_decl()

void xml_file_iterator_element_decl ( void * ctx,
const xmlChar * name,
int type,
xmlElementContentPtr content )
static

XML file iterator parser callback for when an element definition has been parsed.

This is just a wrapper for the libXML xmlSAX2ElementDecl getting the libXML parser context from the iterator struct passed as user data.

Parameters
[in]ctxparser context data / iterator data structure
[in]namethe element name
[in]typethe element type
[in]defthe type of default value
[in]defaultValuethe attribute default value
[in]contentthe element value tree

Definition at line 2640 of file xmlutils.c.

2642{
2644 xmlSAX2ElementDecl (iterator->parser_ctxt, name, type, content);
2645}

References xml_file_iterator_struct::parser_ctxt.

Referenced by xml_file_iterator_init_sax_handler().

Here is the caller graph for this function:

◆ xml_file_iterator_end_document()

void xml_file_iterator_end_document ( void * ctx)
static

XML file iterator parser callback at the document end.

This is just a wrapper for the libXML xmlSAX2EndDocument getting the libXML parser context from the iterator struct passed as user data.

Parameters
[in]ctxparser context data / iterator data structure

Definition at line 2732 of file xmlutils.c.

2733{
2735 xmlSAX2EndDocument (iterator->parser_ctxt);
2736}

References xml_file_iterator_struct::parser_ctxt.

Referenced by xml_file_iterator_init_sax_handler().

Here is the caller graph for this function:

◆ xml_file_iterator_end_element_ns()

void xml_file_iterator_end_element_ns ( void * ctx,
const xmlChar * localname,
const xmlChar * prefix,
const xmlChar * URI )
static

XML file iterator parser callback for element end.

This uses xmlSAX2EndElementNs to finish parsing the element to the document in the libXML parser context of the iterator. If the element is at the output tree depth defined in the iterator then it is removed from the document and added to the element queue of the iterator.

Parameters
[in]ctxparser context data / iterator data structure
[in]localnamethe local name of the element
[in]prefixthe element namespace prefix if available
[in]URIthe element namespace name if available

Definition at line 2390 of file xmlutils.c.

2392{
2394 xmlSAX2EndElementNs (iterator->parser_ctxt, localname, prefix, URI);
2395 iterator->stack_depth--;
2396 if (iterator->stack_depth == iterator->output_depth)
2397 {
2398 xmlNodePtr parent, child;
2399 parent = iterator->parser_ctxt->node;
2400
2401 child = parent->children;
2402 while (child)
2403 {
2404 if (child->type == XML_ELEMENT_NODE)
2405 {
2406 xmlDocPtr new_doc = xmlNewDoc ((const xmlChar *) "1.0");
2407 element_t child_copy;
2408 child_copy = xmlCopyNode (child, 1);
2409 xmlDocSetRootElement (new_doc, child_copy);
2410
2411 if (child_copy)
2412 {
2413 g_queue_push_tail (iterator->element_queue, child_copy);
2414 }
2415 }
2416
2417 xmlUnlinkNode (child);
2418 xmlFreeNode (child);
2419
2420 child = parent->children;
2421 }
2422 }
2423}

References xml_file_iterator_struct::element_queue, xml_file_iterator_struct::output_depth, xml_file_iterator_struct::parser_ctxt, and xml_file_iterator_struct::stack_depth.

Referenced by xml_file_iterator_init_sax_handler().

Here is the caller graph for this function:

◆ xml_file_iterator_entity_decl()

void xml_file_iterator_entity_decl ( void * ctx,
const xmlChar * name,
int type,
const xmlChar * publicId,
const xmlChar * systemId,
xmlChar * content )
static

XML file iterator parser callback for when an entity definition has been parsed.

This is just a wrapper for the libXML xmlSAX2EntityDecl getting the libXML parser context from the iterator struct passed as user data.

Parameters
[in]ctxparser context data / iterator data structure
[in]namethe entity name
[in]typethe entity type
[in]publicIdThe public ID of the entity
[in]systemIdThe system ID of the entity
[in]contentthe entity value (without processing)

Definition at line 2590 of file xmlutils.c.

2593{
2595 xmlSAX2EntityDecl (iterator->parser_ctxt, name, type, publicId, systemId,
2596 content);
2597}

References xml_file_iterator_struct::parser_ctxt.

Referenced by xml_file_iterator_init_sax_handler().

Here is the caller graph for this function:

◆ xml_file_iterator_external_subset()

void xml_file_iterator_external_subset ( void * ctx,
const xmlChar * name,
const xmlChar * ExternalID,
const xmlChar * SystemID )
static

XML file iterator parser callback for external subset declaration.

This is just a wrapper for the libXML xmlSAX2ExternalSubset getting the libXML parser context from the iterator struct passed as user data.

Parameters
[in]ctxparser context data / iterator data structure
[in]namethe root element name
[in]ExternalIDthe external ID
[in]SystemIDthe SYSTEM ID (e.g. filename or URL)

Definition at line 2457 of file xmlutils.c.

2460{
2462 xmlSAX2ExternalSubset (iterator->parser_ctxt, name, ExternalID, SystemID);
2463}

References xml_file_iterator_struct::parser_ctxt.

Referenced by xml_file_iterator_init_sax_handler().

Here is the caller graph for this function:

◆ xml_file_iterator_free()

void xml_file_iterator_free ( xml_file_iterator_t iterator)

Frees an XML file iterator and all of its internal data structures.

Parameters
[in]iteratorThe XML file iterator to free.

Definition at line 2919 of file xmlutils.c.

2920{
2921 if (iterator == NULL)
2922 return;
2923
2924 if (iterator->file)
2925 {
2926 fclose (iterator->file);
2927 }
2928
2929 g_free (iterator->file_path);
2930
2931 if (iterator->element_queue)
2932 {
2933 g_queue_free_full (iterator->element_queue,
2934 (GDestroyNotify) (element_free));
2935 }
2936
2937 if (iterator->parser_ctxt)
2938 {
2939 if (iterator->parser_ctxt->myDoc)
2940 xmlFreeDoc (iterator->parser_ctxt->myDoc);
2941 xmlFreeParserCtxt (iterator->parser_ctxt);
2942 }
2943
2944 g_free (iterator);
2945}
void element_free(element_t element)
Free an entire element tree.
Definition xmlutils.c:2035

References element_free(), xml_file_iterator_struct::element_queue, xml_file_iterator_struct::file, xml_file_iterator_struct::file_path, and xml_file_iterator_struct::parser_ctxt.

Referenced by Ensure(), Ensure(), and Ensure().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ xml_file_iterator_get_entity()

xmlEntityPtr xml_file_iterator_get_entity ( void * ctx,
const xmlChar * name )
static

XML file iterator parser callback for getting an entity by name.

This is just a wrapper for the libXML xmlSAX2GetEntity getting the libXML parser context from the iterator struct passed as user data.

Parameters
[in]ctxparser context data / iterator data structure
[in]nameThe entity name
Returns
the xmlEntityPtr if found

Definition at line 2550 of file xmlutils.c.

2551{
2553 return xmlSAX2GetEntity (iterator->parser_ctxt, name);
2554}

References xml_file_iterator_struct::parser_ctxt.

Referenced by xml_file_iterator_init_sax_handler().

Here is the caller graph for this function:

◆ xml_file_iterator_get_parameter_entity()

xmlEntityPtr xml_file_iterator_get_parameter_entity ( void * ctx,
const xmlChar * name )
static

XML file iterator parser callback for getting a parameter entity by name.

This is just a wrapper for the libXML xmlSAX2GetParameterEntity getting the libXML parser context from the iterator struct passed as user data.

Parameters
[in]ctxparser context data / iterator data structure
[in]nameThe entity name
Returns
the xmlEntityPtr if found

Definition at line 2569 of file xmlutils.c.

2570{
2572 return xmlSAX2GetParameterEntity (iterator->parser_ctxt, name);
2573}

References xml_file_iterator_struct::parser_ctxt.

Referenced by xml_file_iterator_init_sax_handler().

Here is the caller graph for this function:

◆ xml_file_iterator_has_external_subset()

int xml_file_iterator_has_external_subset ( void * ctx)
static

XML file iterator parser callback for checking if doc has an external subset.

This is just a wrapper for the libXML xmlSAX2HasExternalSubset getting the libXML parser context from the iterator struct passed as user data.

Parameters
[in]ctxparser context data / iterator data structure
Returns
1 if true

Definition at line 2512 of file xmlutils.c.

2513{
2515 return xmlSAX2HasExternalSubset (iterator->parser_ctxt);
2516}

References xml_file_iterator_struct::parser_ctxt.

Referenced by xml_file_iterator_init_sax_handler().

Here is the caller graph for this function:

◆ xml_file_iterator_has_internal_subset()

int xml_file_iterator_has_internal_subset ( void * ctx)
static

XML file iterator parser callback for checking if doc has an internal subset.

This is just a wrapper for the libXML xmlSAX2HasInternalSubset getting the libXML parser context from the iterator struct passed as user data.

Parameters
[in]ctxparser context data / iterator data structure
Returns
1 if true

Definition at line 2494 of file xmlutils.c.

2495{
2497 return xmlSAX2HasInternalSubset (iterator->parser_ctxt);
2498}

References xml_file_iterator_struct::parser_ctxt.

Referenced by xml_file_iterator_init_sax_handler().

Here is the caller graph for this function:

◆ xml_file_iterator_init_from_file_path()

int xml_file_iterator_init_from_file_path ( xml_file_iterator_t iterator,
const char * file_path,
int output_depth )

Initializes an XML file iterator to read from a given path.

Parameters
[in]iteratorPointer to the iterator to initialize.
[in]file_pathPath to the file to read from.
[in]output_depthXML tree depth at which to return elements.
Returns
-1 error, 0 success, 1 already initialized, 2 error opening file (errno is set to reason), 3 error creating parser context

Definition at line 2876 of file xmlutils.c.

2878{
2879 if (iterator == NULL)
2880 return -1;
2881
2882 if (iterator->initialized)
2883 return 1;
2884
2885 memset (iterator, 0, sizeof (struct xml_file_iterator_struct));
2886
2887 LIBXML_TEST_VERSION
2888
2889 if (output_depth < 0)
2890 output_depth = 0;
2891 iterator->output_depth = output_depth;
2892
2893 iterator->file = fopen (file_path, "rb");
2894 if (iterator->file == NULL)
2895 return 2;
2896
2897 iterator->element_queue = g_queue_new ();
2898
2899 iterator->file_path = g_strdup (file_path);
2900
2902 iterator->parser_ctxt = xmlCreatePushParserCtxt (
2903 &(iterator->sax_handler), iterator, NULL, 0, iterator->file_path);
2904 if (iterator->parser_ctxt == NULL)
2905 return 3;
2906
2907 iterator->initialized = 1;
2908 iterator->stack_depth = 0;
2909
2910 return 0;
2911}
Opaque data structure for XML file iterator.
Definition xmlutils.c:2331
xmlSAXHandler sax_handler
Definition xmlutils.c:2335
static void xml_file_iterator_init_sax_handler(xmlSAXHandlerPtr hdlr)
Initializes a xmlSAXHandler data structure for SAX version 2 parsing, assigning all the XML file iter...
Definition xmlutils.c:2816

References xml_file_iterator_struct::element_queue, xml_file_iterator_struct::file, xml_file_iterator_struct::file_path, xml_file_iterator_struct::initialized, xml_file_iterator_struct::output_depth, xml_file_iterator_struct::parser_ctxt, xml_file_iterator_struct::sax_handler, xml_file_iterator_struct::stack_depth, and xml_file_iterator_init_sax_handler().

Referenced by Ensure(), Ensure(), and Ensure().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ xml_file_iterator_init_sax_handler()

void xml_file_iterator_init_sax_handler ( xmlSAXHandlerPtr hdlr)
static

Initializes a xmlSAXHandler data structure for SAX version 2 parsing, assigning all the XML file iterator callback functions.

Parameters
[in]hdlrThe xmlSAXHandler to initialize

Definition at line 2816 of file xmlutils.c.

2817{
2818 hdlr->startElementNs = xml_file_iterator_start_element_ns;
2819 hdlr->endElementNs = xml_file_iterator_end_element_ns;
2820 hdlr->error = NULL;
2821 hdlr->initialized = XML_SAX2_MAGIC;
2822 hdlr->startElement = NULL;
2823 hdlr->endElement = NULL;
2824 hdlr->internalSubset = xml_file_iterator_internal_subset;
2825 hdlr->externalSubset = xml_file_iterator_external_subset;
2826 hdlr->isStandalone = xml_file_iterator_is_standalone;
2827 hdlr->hasInternalSubset = xml_file_iterator_has_internal_subset;
2828 hdlr->hasExternalSubset = xml_file_iterator_has_external_subset;
2829 hdlr->resolveEntity = xml_file_iterator_resolve_entity;
2830 hdlr->getEntity = xml_file_iterator_get_entity;
2831 hdlr->getParameterEntity = xml_file_iterator_get_parameter_entity;
2832 hdlr->entityDecl = xml_file_iterator_entity_decl;
2833 hdlr->attributeDecl = xml_file_iterator_attribute_decl;
2834 hdlr->elementDecl = xml_file_iterator_element_decl;
2835 hdlr->notationDecl = xml_file_iterator_notation_decl;
2836 hdlr->unparsedEntityDecl = xml_file_iterator_unparsed_entity_decl;
2837 hdlr->setDocumentLocator = xml_file_iterator_set_document_locator;
2838 hdlr->startDocument = xml_file_iterator_start_document;
2839 hdlr->endDocument = xml_file_iterator_end_document;
2840 hdlr->reference = NULL;
2841 hdlr->characters = xml_file_iterator_characters;
2842 hdlr->cdataBlock = xml_file_iterator_cdata_block;
2843 hdlr->ignorableWhitespace = xml_file_iterator_characters;
2844 hdlr->processingInstruction = xml_file_iterator_processing_instruction;
2845 hdlr->comment = xml_file_iterator_comment;
2846 hdlr->warning = xmlParserWarning;
2847 hdlr->error = xmlParserError;
2848 hdlr->fatalError = xmlParserError;
2849}
static void xml_file_iterator_characters(void *ctx, const xmlChar *ch, int len)
XML file iterator parser callback when receiving some chars from the parser.
Definition xmlutils.c:2750
static void xml_file_iterator_unparsed_entity_decl(void *ctx, const xmlChar *name, const xmlChar *publicId, const xmlChar *systemId, const xmlChar *notationName)
XML file iterator parser callback for when an unparsed entity declaration has been parsed.
Definition xmlutils.c:2682
static void xml_file_iterator_processing_instruction(void *ctx, const xmlChar *target, const xmlChar *data)
XML file iterator parser callback when a processing instruction has been parsed.
Definition xmlutils.c:2786
static void xml_file_iterator_entity_decl(void *ctx, const xmlChar *name, int type, const xmlChar *publicId, const xmlChar *systemId, xmlChar *content)
XML file iterator parser callback for when an entity definition has been parsed.
Definition xmlutils.c:2590
static int xml_file_iterator_is_standalone(void *ctx)
XML file iterator parser callback for checking if doc is standalone.
Definition xmlutils.c:2476
static void xml_file_iterator_start_document(void *ctx)
XML file iterator parser callback at the document start.
Definition xmlutils.c:2717
static xmlEntityPtr xml_file_iterator_get_entity(void *ctx, const xmlChar *name)
XML file iterator parser callback for getting an entity by name.
Definition xmlutils.c:2550
static void xml_file_iterator_internal_subset(void *ctx, const xmlChar *name, const xmlChar *ExternalID, const xmlChar *SystemID)
XML file iterator parser callback for internal subset declaration.
Definition xmlutils.c:2437
static void xml_file_iterator_comment(void *ctx, const xmlChar *value)
XML file iterator parser callback when a comment has been parsed.
Definition xmlutils.c:2803
static xmlEntityPtr xml_file_iterator_get_parameter_entity(void *ctx, const xmlChar *name)
XML file iterator parser callback for getting a parameter entity by name.
Definition xmlutils.c:2569
static void xml_file_iterator_end_element_ns(void *ctx, const xmlChar *localname, const xmlChar *prefix, const xmlChar *URI)
XML file iterator parser callback for element end.
Definition xmlutils.c:2390
static int xml_file_iterator_has_external_subset(void *ctx)
XML file iterator parser callback for checking if doc has an external subset.
Definition xmlutils.c:2512
static void xml_file_iterator_start_element_ns(void *ctx, const xmlChar *localname, const xmlChar *prefix, const xmlChar *URI, int nb_namespaces, const xmlChar **namespaces, int nb_attributes, int nb_defaulted, const xmlChar **attributes)
XML file iterator parser callback for element start.
Definition xmlutils.c:2361
static xmlParserInputPtr xml_file_iterator_resolve_entity(void *ctx, const xmlChar *publicId, const xmlChar *systemId)
XML file iterator parser callback for resolving an entity.
Definition xmlutils.c:2531
static void xml_file_iterator_set_document_locator(void *ctx, xmlSAXLocatorPtr loc)
XML file iterator parser callback for setting the document locator.
Definition xmlutils.c:2702
static void xml_file_iterator_external_subset(void *ctx, const xmlChar *name, const xmlChar *ExternalID, const xmlChar *SystemID)
XML file iterator parser callback for external subset declaration.
Definition xmlutils.c:2457
static void xml_file_iterator_cdata_block(void *ctx, const xmlChar *ch, int len)
XML file iterator parser callback when a cdata block has been parsed.
Definition xmlutils.c:2768
static void xml_file_iterator_attribute_decl(void *ctx, const xmlChar *elem, const xmlChar *fullname, int type, int def, const xmlChar *defaultValue, xmlEnumerationPtr tree)
XML file iterator parser callback for when an attribute definition has been parsed.
Definition xmlutils.c:2615
static void xml_file_iterator_end_document(void *ctx)
XML file iterator parser callback at the document end.
Definition xmlutils.c:2732
static void xml_file_iterator_element_decl(void *ctx, const xmlChar *name, int type, xmlElementContentPtr content)
XML file iterator parser callback for when an element definition has been parsed.
Definition xmlutils.c:2640
static void xml_file_iterator_notation_decl(void *ctx, const xmlChar *name, const xmlChar *publicId, const xmlChar *systemId)
XML file iterator parser callback for when a notation definition has been parsed.
Definition xmlutils.c:2660
static int xml_file_iterator_has_internal_subset(void *ctx)
XML file iterator parser callback for checking if doc has an internal subset.
Definition xmlutils.c:2494

References xml_file_iterator_attribute_decl(), xml_file_iterator_cdata_block(), xml_file_iterator_characters(), xml_file_iterator_comment(), xml_file_iterator_element_decl(), xml_file_iterator_end_document(), xml_file_iterator_end_element_ns(), xml_file_iterator_entity_decl(), xml_file_iterator_external_subset(), xml_file_iterator_get_entity(), xml_file_iterator_get_parameter_entity(), xml_file_iterator_has_external_subset(), xml_file_iterator_has_internal_subset(), xml_file_iterator_internal_subset(), xml_file_iterator_is_standalone(), xml_file_iterator_notation_decl(), xml_file_iterator_processing_instruction(), xml_file_iterator_resolve_entity(), xml_file_iterator_set_document_locator(), xml_file_iterator_start_document(), xml_file_iterator_start_element_ns(), and xml_file_iterator_unparsed_entity_decl().

Referenced by xml_file_iterator_init_from_file_path().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ xml_file_iterator_internal_subset()

void xml_file_iterator_internal_subset ( void * ctx,
const xmlChar * name,
const xmlChar * ExternalID,
const xmlChar * SystemID )
static

XML file iterator parser callback for internal subset declaration.

This is just a wrapper for the libXML xmlSAX2InternalSubset getting the libXML parser context from the iterator struct passed as user data.

Parameters
[in]ctxparser context data / iterator data structure
[in]namethe root element name
[in]ExternalIDthe external ID
[in]SystemIDthe SYSTEM ID (e.g. filename or URL)

Definition at line 2437 of file xmlutils.c.

2440{
2442 xmlSAX2InternalSubset (iterator->parser_ctxt, name, ExternalID, SystemID);
2443}

References xml_file_iterator_struct::parser_ctxt.

Referenced by xml_file_iterator_init_sax_handler().

Here is the caller graph for this function:

◆ xml_file_iterator_is_standalone()

int xml_file_iterator_is_standalone ( void * ctx)
static

XML file iterator parser callback for checking if doc is standalone.

This is just a wrapper for the libXML xmlSAX2IsStandalone getting the libXML parser context from the iterator struct passed as user data.

Parameters
[in]ctxparser context data / iterator data structure
Returns
1 if true

Definition at line 2476 of file xmlutils.c.

2477{
2479 return xmlSAX2IsStandalone (iterator->parser_ctxt);
2480}

References xml_file_iterator_struct::parser_ctxt.

Referenced by xml_file_iterator_init_sax_handler().

Here is the caller graph for this function:

◆ xml_file_iterator_new()

xml_file_iterator_t xml_file_iterator_new ( void )

Allocates a new, uninitialized XML file iterator.

Free with xml_file_iterator_free.

Returns
Opaque pointer to the XML file iterator data structure.

Definition at line 2859 of file xmlutils.c.

2860{
2861 return g_malloc0 (sizeof (struct xml_file_iterator_struct));
2862}

Referenced by Ensure(), Ensure(), and Ensure().

Here is the caller graph for this function:

◆ xml_file_iterator_next()

element_t xml_file_iterator_next ( xml_file_iterator_t iterator,
gchar ** error )

Get the next subelement from a XML file iterator.

Parameters
[in]iteratorThe XML file iterator to get the element from.
[out]errorError message output, set to NULL on success / EOF
Returns
The next subelement (free with element_free) or NULL if finished or on error.

Definition at line 3002 of file xmlutils.c.

3003{
3004 gboolean continue_read = TRUE;
3005
3006 if (error)
3007 *error = NULL;
3008
3009 if (iterator->initialized == 0)
3010 {
3011 if (error)
3012 *error = g_strdup ("iterator not initialized");
3013 return NULL;
3014 }
3015
3016 while (continue_read && g_queue_is_empty (iterator->element_queue))
3017 {
3018 int chars_read;
3019 char buffer[XML_FILE_ITERATOR_BUFFER_SIZE];
3020
3021 chars_read =
3022 fread (buffer, 1, XML_FILE_ITERATOR_BUFFER_SIZE, iterator->file);
3023 if (chars_read == 0)
3024 {
3025 if (feof (iterator->file))
3026 {
3027 continue_read = FALSE;
3028 }
3029 else if (ferror (iterator->file))
3030 {
3031 if (error)
3032 *error = g_strdup ("error reading file");
3033 return NULL;
3034 }
3035 }
3036 else
3037 {
3038 int ret;
3039 ret = xmlParseChunk (iterator->parser_ctxt, buffer, chars_read,
3040 continue_read == 0);
3041 if (ret)
3042 {
3043 if (error)
3044 {
3045 const xmlError *xml_error;
3046 xml_error = xmlCtxtGetLastError (iterator->parser_ctxt);
3047 *error = g_strdup_printf ("error parsing XML"
3048 " (line %d column %d): %s",
3049 xml_error->line, xml_error->int2,
3050 xml_error->message);
3051 }
3052
3053 return NULL;
3054 }
3055 }
3056 }
3057
3058 if (!g_queue_is_empty (iterator->element_queue))
3059 {
3060 return g_queue_pop_head (iterator->element_queue);
3061 }
3062
3063 return NULL;
3064}
#define XML_FILE_ITERATOR_BUFFER_SIZE
File read buffer size for an XML file iterator.
Definition xmlutils.c:2990

References xml_file_iterator_struct::element_queue, xml_file_iterator_struct::file, xml_file_iterator_struct::initialized, xml_file_iterator_struct::parser_ctxt, and XML_FILE_ITERATOR_BUFFER_SIZE.

Referenced by Ensure(), Ensure(), and Ensure().

Here is the caller graph for this function:

◆ xml_file_iterator_notation_decl()

void xml_file_iterator_notation_decl ( void * ctx,
const xmlChar * name,
const xmlChar * publicId,
const xmlChar * systemId )
static

XML file iterator parser callback for when a notation definition has been parsed.

This is just a wrapper for the libXML xmlSAX2NotationDecl getting the libXML parser context from the iterator struct passed as user data.

Parameters
[in]ctxparser context data / iterator data structure
[in]nameThe name of the notation
[in]publicIdThe public ID of the entity
[in]systemIdThe system ID of the entity

Definition at line 2660 of file xmlutils.c.

2663{
2665 xmlSAX2NotationDecl (iterator->parser_ctxt, name, publicId, systemId);
2666}

References xml_file_iterator_struct::parser_ctxt.

Referenced by xml_file_iterator_init_sax_handler().

Here is the caller graph for this function:

◆ xml_file_iterator_processing_instruction()

void xml_file_iterator_processing_instruction ( void * ctx,
const xmlChar * target,
const xmlChar * data )
static

XML file iterator parser callback when a processing instruction has been parsed.

This is just a wrapper for the libXML xmlSAX2ProcessingInstruction getting the libXML parser context from the iterator struct passed as user data.

Parameters
[in]ctxparser context data / iterator data structure
[in]targetThe target name
[in]datathe PI data

Definition at line 2786 of file xmlutils.c.

2788{
2790 xmlSAX2ProcessingInstruction (iterator->parser_ctxt, target, data);
2791}

References xml_file_iterator_struct::parser_ctxt.

Referenced by xml_file_iterator_init_sax_handler().

Here is the caller graph for this function:

◆ xml_file_iterator_resolve_entity()

xmlParserInputPtr xml_file_iterator_resolve_entity ( void * ctx,
const xmlChar * publicId,
const xmlChar * systemId )
static

XML file iterator parser callback for resolving an entity.

This is just a wrapper for the libXML xmlSAX2ResolveEntity getting the libXML parser context from the iterator struct passed as user data.

Parameters
[in]ctxparser context data / iterator data structure
[in]publicIdThe public ID of the entity
[in]systemIdThe systemID of the entity
Returns
the xmlParserInputPtr if inlined or NULL for DOM behaviour

Definition at line 2531 of file xmlutils.c.

2533{
2535 return xmlSAX2ResolveEntity (iterator->parser_ctxt, publicId, systemId);
2536}

References xml_file_iterator_struct::parser_ctxt.

Referenced by xml_file_iterator_init_sax_handler().

Here is the caller graph for this function:

◆ xml_file_iterator_rewind()

int xml_file_iterator_rewind ( xml_file_iterator_t iterator)

Rewinds an XML file iterator by rewinding the file and creating a new XML parser context.

Parameters
[in]iteratorThe XML file iterator to rewind.
Returns
0 success, 1 error creating new parser context

Definition at line 2956 of file xmlutils.c.

2957{
2958 if (iterator == NULL)
2959 return 0;
2960
2961 if (iterator->file)
2962 {
2963 rewind (iterator->file);
2964 }
2965
2966 if (iterator->element_queue)
2967 {
2968 g_queue_clear_full (iterator->element_queue,
2969 (GDestroyNotify) (element_free));
2970 }
2971
2972 if (iterator->parser_ctxt)
2973 {
2974 if (iterator->parser_ctxt->myDoc)
2975 xmlFreeDoc (iterator->parser_ctxt->myDoc);
2976 xmlFreeParserCtxt (iterator->parser_ctxt);
2977 iterator->parser_ctxt = xmlCreatePushParserCtxt (
2978 &(iterator->sax_handler), iterator, NULL, 0, iterator->file_path);
2979 if (iterator->parser_ctxt == NULL)
2980 return 1;
2981 }
2982 iterator->stack_depth = 0;
2983
2984 return 0;
2985}

References element_free(), xml_file_iterator_struct::element_queue, xml_file_iterator_struct::file, xml_file_iterator_struct::file_path, xml_file_iterator_struct::parser_ctxt, xml_file_iterator_struct::sax_handler, and xml_file_iterator_struct::stack_depth.

Referenced by Ensure().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ xml_file_iterator_set_document_locator()

void xml_file_iterator_set_document_locator ( void * ctx,
xmlSAXLocatorPtr loc )
static

XML file iterator parser callback for setting the document locator.

This is just a wrapper for the libXML xmlSAX2SetDocumentLocator getting the libXML parser context from the iterator struct passed as user data.

Parameters
[in]ctxparser context data / iterator data structure
[in]locA SAX Locator

Definition at line 2702 of file xmlutils.c.

2703{
2705 xmlSAX2SetDocumentLocator (iterator->parser_ctxt, loc);
2706}

References xml_file_iterator_struct::parser_ctxt.

Referenced by xml_file_iterator_init_sax_handler().

Here is the caller graph for this function:

◆ xml_file_iterator_start_document()

void xml_file_iterator_start_document ( void * ctx)
static

XML file iterator parser callback at the document start.

This is just a wrapper for the libXML xmlSAX2StartDocument getting the libXML parser context from the iterator struct passed as user data.

Parameters
[in]ctxparser context data / iterator data structure

Definition at line 2717 of file xmlutils.c.

2718{
2720 xmlSAX2StartDocument (iterator->parser_ctxt);
2721}

References xml_file_iterator_struct::parser_ctxt.

Referenced by xml_file_iterator_init_sax_handler().

Here is the caller graph for this function:

◆ xml_file_iterator_start_element_ns()

void xml_file_iterator_start_element_ns ( void * ctx,
const xmlChar * localname,
const xmlChar * prefix,
const xmlChar * URI,
int nb_namespaces,
const xmlChar ** namespaces,
int nb_attributes,
int nb_defaulted,
const xmlChar ** attributes )
static

XML file iterator parser callback for element start.

This is just a wrapper for the libXML xmlSAX2StartElementNs getting the libXML parser context from the iterator struct passed as user data.

Parameters
[in]ctxparser context data / iterator data structure
[in]localnamethe local name of the element
[in]prefixthe element namespace prefix if available
[in]URIthe element namespace name if available
[in]nb_namespacesnumber of namespace definitions on that node
[in]namespacespointer to the array of prefix/URI pairs namespace definitions
[in]nb_attributesthe number of attributes on that node
[in]nb_defaultedthe number of defaulted attributes
[in]attributespointer to the array of (localname/prefix/URI/value/end) attribute values

Definition at line 2361 of file xmlutils.c.

2367{
2369 xmlSAX2StartElementNs (iterator->parser_ctxt, localname, prefix, URI,
2370 nb_namespaces, namespaces, nb_attributes, nb_defaulted,
2371 attributes);
2372 iterator->stack_depth++;
2373}

References xml_file_iterator_struct::parser_ctxt, and xml_file_iterator_struct::stack_depth.

Referenced by xml_file_iterator_init_sax_handler().

Here is the caller graph for this function:

◆ xml_file_iterator_unparsed_entity_decl()

void xml_file_iterator_unparsed_entity_decl ( void * ctx,
const xmlChar * name,
const xmlChar * publicId,
const xmlChar * systemId,
const xmlChar * notationName )
static

XML file iterator parser callback for when an unparsed entity declaration has been parsed.

This is just a wrapper for the libXML xmlSAX2UnparsedEntityDecl getting the libXML parser context from the iterator struct passed as user data.

Parameters
[in]ctxparser context data / iterator data structure
[in]nameThe name of the notation
[in]publicIdThe public ID of the entity
[in]systemIdThe system ID of the entity
[in]notationNameThe name of the notation

Definition at line 2682 of file xmlutils.c.

2686{
2688 xmlSAX2UnparsedEntityDecl (iterator->parser_ctxt, name, publicId, systemId,
2689 notationName);
2690}

References xml_file_iterator_struct::parser_ctxt.

Referenced by xml_file_iterator_init_sax_handler().

Here is the caller graph for this function:

◆ xml_handle_end_element()

void xml_handle_end_element ( context_data_t * context,
const gchar * element_name )

Handle the end of an XML element.

Parameters
[in]contextParser context.
[in]element_nameXML element name.

Definition at line 402 of file xmlutils.c.

403{
404 handle_end_element (NULL, element_name, context, NULL);
405}

References element_name(), and handle_end_element().

Here is the call graph for this function:

◆ xml_handle_start_element()

void xml_handle_start_element ( context_data_t * context,
const gchar * element_name,
const gchar ** attribute_names,
const gchar ** attribute_values )

Handle the start of an OMP XML element.

Parameters
[in]contextParser context.
[in]element_nameXML element name.
[in]attribute_namesXML attribute name.
[in]attribute_valuesXML attribute values.

Definition at line 326 of file xmlutils.c.

329{
330 handle_start_element (NULL, element_name, attribute_names, attribute_values,
331 context, NULL);
332}

References element_name(), and handle_start_element().

Here is the call graph for this function:

◆ xml_handle_text()

void xml_handle_text ( context_data_t * context,
const gchar * text,
gsize text_len )

Handle additional text of an XML element.

Parameters
[in]contextParser context.
[in]textThe text.
[in]text_lenLength of the text.

Definition at line 464 of file xmlutils.c.

465{
466 handle_text (NULL, text, text_len, context, NULL);
467}

References handle_text().

Here is the call graph for this function:

◆ xml_search_handle_start_element()

void xml_search_handle_start_element ( GMarkupParseContext * ctx,
const gchar * element_name,
const gchar ** attribute_names,
const gchar ** attribute_values,
gpointer data,
GError ** error )
static

Handle the opening tag of an element in an XML search.

Parameters
[in]ctxThe parse context.
[in]element_nameThe name of the element.
[in]attribute_namesNULL-terminated array of attribute names.
[in]attribute_valuesNULL-terminated array of attribute values.
[in]dataThe search data struct.
[out]errorPointer to error output location.

Definition at line 1874 of file xmlutils.c.

1879{
1880 (void) ctx;
1881 (void) error;
1882
1883 xml_search_data_t *search_data = ((xml_search_data_t *) data);
1884
1885 if (strcmp (element_name, search_data->find_element) == 0
1886 && search_data->found == 0)
1887 {
1888 g_debug ("%s: Found element <%s>", __func__, element_name);
1889
1890 if (search_data->find_attributes
1891 && g_hash_table_size (search_data->find_attributes))
1892 {
1893 int index;
1894 GHashTable *found_attributes;
1895 found_attributes =
1896 g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL);
1897 index = 0;
1898 while (attribute_names[index])
1899 {
1900 gchar *searched_value;
1901 searched_value = g_hash_table_lookup (
1902 search_data->find_attributes, attribute_names[index]);
1903 if (searched_value
1904 && strcmp (searched_value, attribute_values[index]) == 0)
1905 {
1906 g_debug ("%s: Found attribute %s=\"%s\"", __func__,
1907 attribute_names[index], searched_value);
1908 g_hash_table_add (found_attributes, searched_value);
1909 }
1910 index++;
1911 }
1912 g_debug ("%s: Found %d of %d attributes", __func__,
1913 g_hash_table_size (found_attributes),
1914 g_hash_table_size (search_data->find_attributes));
1915
1916 if (g_hash_table_size (found_attributes)
1917 == g_hash_table_size (search_data->find_attributes))
1918 {
1919 search_data->found = 1;
1920 }
1921
1922 g_hash_table_destroy (found_attributes);
1923 }
1924 else
1925 {
1926 search_data->found = 1;
1927 }
1928 }
1929}

References element_name(), xml_search_data_t::find_attributes, xml_search_data_t::find_element, and xml_search_data_t::found.

Referenced by find_element_in_xml_file().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ xml_string_append()

void xml_string_append ( GString * xml,
const char * format,
... )

Append formatted escaped XML to a string.

Parameters
[in]xmlXML string.
[in]formatFormat string.
[in]...Arguments for format string.

Definition at line 1849 of file xmlutils.c.

1850{
1851 gchar *piece;
1852 va_list args;
1853
1854 va_start (args, format);
1855 piece = g_markup_vprintf_escaped (format, args);
1856 va_end (args);
1857 g_string_append (xml, piece);
1858 g_free (piece);
1859}

Referenced by credential_append_as_xml(), gmp_get_system_reports_ext(), osp_start_scan_ext(), target_append_as_xml(), vt_group_append_as_xml(), vt_single_append_as_xml(), and vt_value_append_as_xml().

Here is the caller graph for this function: