Greenbone Vulnerability Management Libraries  22.8.0
xmlutils.h File Reference

Headers for simple XML reader. More...

#include "serverutils.h"
#include <glib.h>
#include <gnutls/gnutls.h>
#include <stdio.h>
Include dependency graph for xmlutils.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  context_data_t
 XML context. More...
 
struct  entity_s
 XML element. More...
 
struct  xml_search_data_t
 Data for xml search functions. More...
 

Typedefs

typedef GSList * entities_t
 Entities. More...
 
typedef struct entity_sentity_t
 
typedef struct _xmlNode * element_t
 

Functions

void xml_handle_start_element (context_data_t *, const gchar *, const gchar **, const gchar **)
 Handle the start of an OMP XML element. More...
 
void xml_handle_end_element (context_data_t *, const gchar *)
 Handle the end of an XML element. More...
 
void xml_handle_text (context_data_t *, const gchar *, gsize)
 Handle additional text of an XML element. More...
 
entities_t next_entities (entities_t)
 Return all the entities from an entities_t after the first. More...
 
entity_t first_entity (entities_t)
 Return the first entity from an entities_t. More...
 
entity_t add_entity (entities_t *, const char *, const char *)
 Add an XML entity to a tree of entities. More...
 
int compare_entities (entity_t, entity_t)
 Compare two XML entity. More...
 
entity_t entity_child (entity_t, const char *)
 Get a child of an entity. More...
 
const char * entity_attribute (entity_t, const char *)
 Get an attribute of an entity. More...
 
char * entity_name (entity_t entity)
 Get the name an entity. More...
 
char * entity_text (entity_t entity)
 Get the text an entity. More...
 
void free_entity (entity_t)
 Free an entity, recursively. More...
 
void print_entity (FILE *, entity_t)
 Print an XML entity. More...
 
void print_entity_format (entity_t, gpointer indentation)
 Print an XML entity to stdout, recursively printing its children. More...
 
int try_read_entity_and_string (gnutls_session_t *, int, entity_t *, GString **)
 Try read an XML entity tree from the manager. More...
 
int read_entity_and_string (gnutls_session_t *, entity_t *, GString **)
 Try read an XML entity tree from the manager. More...
 
int read_entity_and_string_c (gvm_connection_t *, entity_t *, GString **)
 Try read an XML entity tree from the manager. More...
 
int read_entity_and_text (gnutls_session_t *, entity_t *, char **)
 Read an XML entity tree from the manager. More...
 
int read_entity_and_text_c (gvm_connection_t *, entity_t *, char **)
 Read an XML entity tree from the manager. More...
 
int try_read_entity (gnutls_session_t *, int, entity_t *)
 Try read an XML entity tree from the manager. More...
 
int try_read_entity_c (gvm_connection_t *, int, entity_t *)
 Try read an XML entity tree from the manager. More...
 
int read_entity (gnutls_session_t *, entity_t *)
 Read an XML entity tree from the manager. More...
 
int read_entity_s (int, entity_t *)
 Read an XML entity tree from the socket. More...
 
int read_entity_c (gvm_connection_t *, entity_t *)
 Read an XML entity tree from the manager. More...
 
int read_string (gnutls_session_t *, GString **)
 Read entity and text. Free the entity immediately. More...
 
int read_string_c (gvm_connection_t *, GString **)
 Read entity and text. Free the entity immediately. More...
 
int read_text_c (gvm_connection_t *, char **)
 Read text from the server. More...
 
int parse_entity (const char *, entity_t *)
 Read an XML entity tree from a string. More...
 
void print_entity_to_string (entity_t entity, GString *string)
 Print an XML entity tree to a GString, appending it if string is not. More...
 
int xml_count_entities (entities_t)
 Count the number of entities. More...
 
void xml_string_append (GString *, const char *,...)
 Append formatted escaped XML to a string. More...
 
int find_element_in_xml_file (gchar *, gchar *, GHashTable *)
 Tests if an XML file contains an element with given attributes. More...
 
int parse_element (const gchar *, element_t *)
 Read an XML element tree from a string. More...
 
void element_free (element_t)
 Free an entire element tree. More...
 
const gchar * element_name (element_t)
 Get the name of an element. More...
 
gchar * element_attribute (element_t, const gchar *)
 Get an attribute of an element. More...
 
gchar * element_text (element_t)
 Get text of an element. More...
 
element_t element_child (element_t, const gchar *)
 Get a child of an element. More...
 
element_t element_first_child (element_t)
 Get the first child of an element. More...
 
element_t element_next (element_t)
 Get the next sibling of an element. More...
 
gchar * element_to_string (element_t element)
 Output the XML element as a string. More...
 
void print_element_to_string (element_t element, GString *string)
 Print an XML element tree to a GString, appending it if string is not. More...
 

Detailed Description

Headers for simple XML reader.

Definition in file xmlutils.h.

Typedef Documentation

◆ element_t

typedef struct _xmlNode* element_t

Definition at line 157 of file xmlutils.h.

◆ entities_t

typedef GSList* entities_t

Entities.

Definition at line 46 of file xmlutils.h.

◆ entity_t

typedef struct entity_s* entity_t

Definition at line 58 of file xmlutils.h.

Function Documentation

◆ 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 }

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 1761 of file xmlutils.c.

1762 {
1763  if (entity1 == NULL)
1764  return entity2 == NULL ? 0 : 1;
1765  if (entity2 == NULL)
1766  return 1;
1767 
1768  if (strcmp (entity1->name, entity2->name))
1769  {
1770  g_debug (" compare failed name: %s vs %s\n", entity1->name,
1771  entity2->name);
1772  return 1;
1773  }
1774  if (strcmp (entity1->text, entity2->text))
1775  {
1776  g_debug (" compare failed text %s vs %s (%s)\n", entity1->text,
1777  entity2->text, entity1->name);
1778  return 1;
1779  }
1780 
1781  if (entity1->attributes == NULL)
1782  {
1783  if (entity2->attributes)
1784  return 1;
1785  }
1786  else
1787  {
1788  if (entity2->attributes == NULL)
1789  return 1;
1790  if (g_hash_table_find (entity1->attributes, compare_find_attribute,
1791  (gpointer) entity2->attributes))
1792  {
1793  g_debug (" compare failed attributes\n");
1794  return 1;
1795  }
1796  }
1797 
1798  // FIX entities can be in any order
1799  GSList *list1 = entity1->entities;
1800  GSList *list2 = entity2->entities;
1801  while (list1 && list2)
1802  {
1803  if (compare_entities (list1->data, list2->data))
1804  {
1805  g_debug (" compare failed subentity\n");
1806  return 1;
1807  }
1808  list1 = g_slist_next (list1);
1809  list2 = g_slist_next (list2);
1810  }
1811  if (list1 == list2)
1812  return 0;
1813  /* More entities in one of the two. */
1814  g_debug (" compare failed number of entities (%s)\n", entity1->name);
1815  return 1;
1816 }

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:

◆ 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 2158 of file xmlutils.c.

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

Referenced by 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 2082 of file xmlutils.c.

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

References find_child().

Referenced by 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 2206 of file xmlutils.c.

2207 {
2208  if (element)
2209  {
2210  element = element->children;
2211  while (element && (element->type != XML_ELEMENT_NODE))
2212  element = element->next;
2213  return element;
2214  }
2215  return NULL;
2216 }

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 2031 of file xmlutils.c.

2032 {
2033  if (element)
2034  {
2035  assert (element->doc);
2036  xmlFreeDoc (element->doc);
2037  }
2038 }

Referenced by Ensure().

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 2048 of file xmlutils.c.

2049 {
2050  if (element && (element->type == XML_ELEMENT_NODE))
2051  return (const gchar *) element->name;
2052 
2053  return "";
2054 }

Referenced by 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 2226 of file xmlutils.c.

2227 {
2228  if (element)
2229  {
2230  element = element->next;
2231  while (element && (element->type != XML_ELEMENT_NODE))
2232  element = element->next;
2233  return element;
2234  }
2235  return NULL;
2236 }

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 2133 of file xmlutils.c.

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

Referenced by 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 2249 of file xmlutils.c.

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

◆ 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 }

References compare_entity_with_name(), and entity_s::entities.

Referenced by 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(), 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(), 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_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 1938 of file xmlutils.c.

1940 {
1941  gchar buffer[XML_FILE_BUFFER_SIZE];
1942  FILE *file;
1943  int read_len;
1944  GMarkupParser xml_parser;
1945  GMarkupParseContext *xml_context;
1946  xml_search_data_t search_data;
1947  GError *error = NULL;
1948 
1949  search_data.find_element = find_element;
1950  search_data.find_attributes = find_attributes;
1951  search_data.found = 0;
1952 
1953  /* Create the XML parser. */
1954  xml_parser.start_element = xml_search_handle_start_element;
1955  xml_parser.end_element = NULL;
1956  xml_parser.text = NULL;
1957  xml_parser.passthrough = NULL;
1958  xml_parser.error = NULL;
1959  xml_context = g_markup_parse_context_new (&xml_parser, 0, &search_data, NULL);
1960 
1961  file = fopen (file_path, "r");
1962  if (file == NULL)
1963  {
1964  g_markup_parse_context_free (xml_context);
1965  g_warning ("%s: Failed to open '%s':", __func__, strerror (errno));
1966  return 0;
1967  }
1968 
1969  while ((read_len = fread (&buffer, sizeof (char), XML_FILE_BUFFER_SIZE, file))
1970  && g_markup_parse_context_parse (xml_context, buffer, read_len, &error)
1971  && error == NULL)
1972  {
1973  }
1974  g_markup_parse_context_end_parse (xml_context, &error);
1975 
1976  fclose (file);
1977 
1978  g_markup_parse_context_free (xml_context);
1979  return search_data.found;
1980 }

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:

◆ 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 }

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

Referenced by check_response_c(), 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:
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 1999 of file xmlutils.c.

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

Referenced by 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  return -2;
1547  }
1548  if (context_data.done)
1549  {
1550  g_markup_parse_context_end_parse (xml_context, &error);
1551  if (error)
1552  {
1553  g_warning (" End error: %s\n", error->message);
1554  g_error_free (error);
1555  if (context_data.first && context_data.first->data)
1556  {
1557  free_entity (context_data.first->data);
1558  g_slist_free_1 (context_data.first);
1559  }
1560  return -2;
1561  }
1562  *entity = (entity_t) context_data.first->data;
1563  g_slist_free_1 (context_data.first);
1564  return 0;
1565  }
1566  if (context_data.first && context_data.first->data)
1567  {
1568  free_entity (context_data.first->data);
1569  g_slist_free_1 (context_data.first);
1570  }
1571  return -3;
1572 }

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().

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 2276 of file xmlutils.c.

2277 {
2278  gchar *text_escaped, *text;
2279  element_t ch;
2280  xmlAttr *attribute;
2281 
2282  text_escaped = NULL;
2283 
2284  g_string_append_printf (string, "<%s", element_name (element));
2285 
2286  attribute = element->properties;
2287  while (attribute)
2288  {
2289  xmlChar *value;
2290 
2291  value = xmlNodeListGetString (element->doc, attribute->children, 1);
2292 
2293  text_escaped = g_markup_escape_text ((gchar *) value, -1);
2294  g_string_append_printf (string, " %s=\"%s\"", attribute->name,
2295  text_escaped);
2296  g_free (text_escaped);
2297 
2298  xmlFree (value);
2299 
2300  attribute = attribute->next;
2301  }
2302 
2303  g_string_append_printf (string, ">");
2304 
2305  text = element_text (element);
2306  text_escaped = g_markup_escape_text (text, -1);
2307  g_free (text);
2308  g_string_append_printf (string, "%s", text_escaped);
2309  g_free (text_escaped);
2310 
2311  ch = element_first_child (element);
2312  while (ch)
2313  {
2314  print_element_to_string (ch, string);
2315  ch = element_next (ch);
2316  }
2317 
2318  g_string_append_printf (string, "</%s>", element_name (element));
2319 }

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 1659 of file xmlutils.c.

1660 {
1661  gchar *text_escaped = NULL;
1662  fprintf (stream, "<%s", entity->name);
1663  if (entity->attributes && g_hash_table_size (entity->attributes))
1664  g_hash_table_foreach (entity->attributes, foreach_print_attribute, stream);
1665  fprintf (stream, ">");
1666  text_escaped = g_markup_escape_text (entity->text, -1);
1667  fprintf (stream, "%s", text_escaped);
1668  g_free (text_escaped);
1669  g_slist_foreach (entity->entities, foreach_print_entity, stream);
1670  fprintf (stream, "</%s>", entity->name);
1671  fflush (stream);
1672 }

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 1702 of file xmlutils.c.

1703 {
1704  int i = 0;
1705  int indentation = GPOINTER_TO_INT (indent);
1706  gchar *text_escaped = NULL;
1707 
1708  for (i = 0; i < indentation; i++)
1709  printf (" ");
1710 
1711  printf ("<%s", entity->name);
1712  if (entity->attributes && g_hash_table_size (entity->attributes))
1713  g_hash_table_foreach (entity->attributes, foreach_print_attribute_format,
1714  indent);
1715  printf (">");
1716 
1717  text_escaped = g_markup_escape_text (entity->text, -1);
1718  printf ("%s", text_escaped);
1719  g_free (text_escaped);
1720 
1721  if (entity->entities)
1722  {
1723  printf ("\n");
1724  g_slist_foreach (entity->entities, (GFunc) print_entity_format,
1725  GINT_TO_POINTER (indentation + 1));
1726  for (i = 0; i < indentation; i++)
1727  printf (" ");
1728  }
1729 
1730  printf ("</%s>\n", entity->name);
1731 }

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 1612 of file xmlutils.c.

1613 {
1614  gchar *text_escaped = NULL;
1615  g_string_append_printf (string, "<%s", entity->name);
1616  if (entity->attributes && g_hash_table_size (entity->attributes))
1617  g_hash_table_foreach (entity->attributes, foreach_print_attribute_to_string,
1618  string);
1619  g_string_append_printf (string, ">");
1620  text_escaped = g_markup_escape_text (entity->text, -1);
1621  g_string_append_printf (string, "%s", text_escaped);
1622  g_free (text_escaped);
1623  g_slist_foreach (entity->entities, foreach_print_entity_to_string, string);
1624  g_string_append_printf (string, "</%s>", entity->name);
1625 }

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 }

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 }

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 }

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 }

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 }

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 }

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 }

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 }

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_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:

◆ 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 1826 of file xmlutils.c.

1827 {
1828  int count = 0;
1829  while (first_entity (entities))
1830  {
1831  entities = next_entities (entities);
1832  count++;
1833  }
1834  return count;
1835 }

References first_entity(), and next_entities().

Here is the call 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_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 1845 of file xmlutils.c.

1846 {
1847  gchar *piece;
1848  va_list args;
1849 
1850  va_start (args, format);
1851  piece = g_markup_vprintf_escaped (format, args);
1852  va_end (args);
1853  g_string_append (xml, piece);
1854  g_free (piece);
1855 }

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:
element_text
gchar * element_text(element_t element)
Get text of an element.
Definition: xmlutils.c:2133
xml_search_handle_start_element
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:1870
BUFFER_SIZE
#define BUFFER_SIZE
Size of the buffer for reading from the manager.
Definition: xmlutils.c:37
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.
Definition: xmlutils.c:501
context_data_t
XML context.
Definition: xmlutils.h:27
handle_end_element
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
first_entity
entity_t first_entity(entities_t entities)
Return the first entity from an entities_t.
Definition: xmlutils.c:82
compare_find_attribute
static gboolean compare_find_attribute(gpointer key, gpointer value, gpointer attributes2)
Look for a key-value pair in a hash table.
Definition: xmlutils.c:1743
entity_s::entities
entities_t entities
Children.
Definition: xmlutils.h:56
foreach_print_entity_to_string
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:1581
entity_s::name
char * name
Name.
Definition: xmlutils.h:53
print_entity_format
void print_entity_format(entity_t entity, gpointer indent)
Print an XML entity to stdout, recursively printing its children.
Definition: xmlutils.c:1702
handle_text
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
element_first_child
element_t element_first_child(element_t element)
Get the first child of an element.
Definition: xmlutils.c:2206
entity_s::attributes
GHashTable * attributes
Attributes.
Definition: xmlutils.h:55
free_entity
void free_entity(entity_t entity)
Free an entity, recursively.
Definition: xmlutils.c:115
compare_entity_with_name
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
XML_FILE_BUFFER_SIZE
#define XML_FILE_BUFFER_SIZE
Definition: xmlutils.c:1927
next_entities
entities_t next_entities(entities_t entities)
Return all the entities from an entities_t after the first.
Definition: xmlutils.c:67
foreach_print_attribute_to_string
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:1594
xml_search_data_t::find_attributes
GHashTable * find_attributes
Definition: xmlutils.h:68
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.
Definition: xmlutils.c:1282
foreach_print_attribute_format
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:1684
gvm_connection_t::socket
int socket
Socket.
Definition: serverutils.h:32
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.
Definition: xmlutils.c:1436
element_t
struct _xmlNode * element_t
Definition: xmlutils.h:157
gvm_connection_t::tls
int tls
Whether uses TCP-TLS (vs UNIX socket).
Definition: serverutils.h:31
foreach_print_attribute
static void foreach_print_attribute(gpointer name, gpointer value, gpointer stream)
Print an XML attribute for g_hash_table_foreach.
Definition: xmlutils.c:1647
handle_start_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.
Definition: xmlutils.c:290
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.
Definition: xmlutils.c:2276
xml_search_data_t::find_element
gchar * find_element
Definition: xmlutils.h:67
try_read_string_s
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
context_data_t::current
GSList * current
The element currently being parsed.
Definition: xmlutils.h:29
try_read_string
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
ignore_start_element
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
context_data_t::done
gboolean done
Flag which is true when the first element is closed.
Definition: xmlutils.h:30
find_child
static element_t find_child(element_t element, const gchar *name)
Find child in an element.
Definition: xmlutils.c:2065
element_name
const gchar * element_name(element_t element)
Get the name of an element.
Definition: xmlutils.c:2048
foreach_print_entity
static void foreach_print_entity(gpointer entity, gpointer stream)
Print an XML entity for g_slist_foreach.
Definition: xmlutils.c:1634
make_entity
static entity_t make_entity(const char *name, const char *text)
Create an entity.
Definition: xmlutils.c:48
entity_s
XML element.
Definition: xmlutils.h:52
try_read_entity_and_string_s
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
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.
Definition: xmlutils.c:1262
element_next
element_t element_next(element_t element)
Get the next sibling of an element.
Definition: xmlutils.c:2226
handle_error
static void handle_error(GMarkupParseContext *context, GError *error, gpointer user_data)
Handle an OMP XML parsing error.
Definition: xmlutils.c:477
entity_s::text
char * text
Text.
Definition: xmlutils.h:54
xml_search_data_t
Data for xml search functions.
Definition: xmlutils.h:64
xml_search_data_t::found
int found
Definition: xmlutils.h:65
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.
Definition: xmlutils.c:1452
ignore_text
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
ignore_end_element
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
compare_entities
int compare_entities(entity_t entity1, entity_t entity2)
Compare two XML entity.
Definition: xmlutils.c:1761
context_data_t::first
GSList * first
The very first entity.
Definition: xmlutils.h:28
entity_t
struct entity_s * entity_t
Definition: xmlutils.h:58
gvm_connection_t::session
gnutls_session_t session
Session.
Definition: serverutils.h:33
entities_t
GSList * entities_t
Entities.
Definition: xmlutils.h:46