OpenVAS Libraries  8.0.3
Data Structures | Macros | Functions
xml.c File Reference

Simple XML reader. More...

#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <glib.h>
#include <string.h>
#include <unistd.h>
#include "xml.h"

Data Structures

struct  context_data_t
 XML context. More...
 

Macros

#define G_LOG_DOMAIN   "lib xml"
 GLib log domain. More...
 
#define BUFFER_SIZE   1048576
 Size of the buffer for reading from the manager. More...
 

Functions

entity_t make_entity (const char *name, const char *text)
 Create an entity. More...
 
entities_t next_entities (entities_t entities)
 Return all the entities from an entities_t after the first. More...
 
entity_t first_entity (entities_t entities)
 Return the first entity from an entities_t. More...
 
entity_t add_entity (entities_t *entities, const char *name, const char *text)
 Add an XML entity to a tree of entities. More...
 
void free_entity (entity_t entity)
 Free an entity, recursively. More...
 
char * entity_text (entity_t entity)
 Get the text an entity. More...
 
char * entity_name (entity_t entity)
 Get the name an entity. More...
 
int compare_entity_with_name (gconstpointer entity, gconstpointer name)
 Compare a given name with the name of a given entity. More...
 
entity_t entity_child (entity_t entity, const char *name)
 Get a child of an entity. More...
 
const char * entity_attribute (entity_t entity, const char *name)
 Get an attribute of an entity. More...
 
void add_attributes (entity_t entity, const gchar **names, const gchar **values)
 Add attributes from an XML callback to an entity. More...
 
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. More...
 
void handle_end_element (GMarkupParseContext *context, const gchar *element_name, gpointer user_data, GError **error)
 Handle the end of an XML element. More...
 
void handle_text (GMarkupParseContext *context, const gchar *text, gsize text_len, gpointer user_data, GError **error)
 Handle additional text of an XML element. More...
 
void handle_error (GMarkupParseContext *context, GError *error, gpointer user_data)
 Handle an OMP XML parsing error. More...
 
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. More...
 
int read_entity_and_string (gnutls_session_t *session, entity_t *entity, GString **string_return)
 Try read an XML entity tree from the manager. More...
 
int read_entity_and_text (gnutls_session_t *session, entity_t *entity, char **text)
 Read an XML entity tree from the manager. More...
 
int read_string (gnutls_session_t *session, GString **string)
 Read entity and text. Free the entity immediately. More...
 
int try_read_entity (gnutls_session_t *session, int timeout, entity_t *entity)
 Try read an XML entity tree from the manager. More...
 
int read_entity (gnutls_session_t *session, entity_t *entity)
 Read an XML entity tree from the manager. More...
 
int parse_entity (const char *string, entity_t *entity)
 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...
 
void print_entity (FILE *stream, entity_t entity)
 Print an XML entity. More...
 
void print_entity_format (entity_t entity, gpointer indent)
 Print an XML entity to stdout, recusively printing its children. More...
 
gboolean compare_find_attribute (gpointer key, gpointer value, gpointer attributes2)
 Look for a key-value pair in a hash table. More...
 
int compare_entities (entity_t entity1, entity_t entity2)
 Compare two XML entity. More...
 
int xml_count_entities (entities_t entities)
 Count the number of entities. More...
 
void xml_string_append (GString *xml, const char *format,...)
 Append formatted escaped XML to a string. More...
 

Detailed Description

Simple XML reader.

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

The openvas-manager tests use this interface to read and handle the XML returned by the manager. The OMP part of openvas-client does the same.

There are examples of using this interface in omp.c and in the openvas-manager tests.

Macro Definition Documentation

#define BUFFER_SIZE   1048576

Size of the buffer for reading from the manager.

#define G_LOG_DOMAIN   "lib xml"

GLib log domain.

Function Documentation

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

Add attributes from an XML callback to an entity.

Parameters
[in]entityThe entity.
[in]namesList of attribute names.
[in]valuesList of attribute values.
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.
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.
int compare_entity_with_name ( gconstpointer  entity,
gconstpointer  name 
)

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.
gboolean compare_find_attribute ( gpointer  key,
gpointer  value,
gpointer  attributes2 
)

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.
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.
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.
char* entity_name ( entity_t  entity)

Get the name an entity.

Parameters
[in]entityEntity.
Returns
Entity name, which is freed by free_entity.
char* entity_text ( entity_t  entity)

Get the text an entity.

Parameters
[in]entityEntity.
Returns
Entity text, which is freed by free_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.
void free_entity ( entity_t  entity)

Free an entity, recursively.

Parameters
[in]entityThe entity, can be NULL.
void handle_end_element ( GMarkupParseContext *  context,
const gchar *  element_name,
gpointer  user_data,
GError **  error 
)

Handle the end of an XML element.

Parameters
[in]contextParser context.
[in]element_nameXML element name.
[in]user_dataDummy parameter.
[in]errorError parameter.
void handle_error ( GMarkupParseContext *  context,
GError *  error,
gpointer  user_data 
)

Handle an OMP XML parsing error.

Parameters
[in]contextParser context.
[in]errorThe error.
[in]user_dataDummy parameter.
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.

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.
void handle_text ( GMarkupParseContext *  context,
const gchar *  text,
gsize  text_len,
gpointer  user_data,
GError **  error 
)

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.
entity_t make_entity ( const char *  name,
const char *  text 
)

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.
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.
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.
void print_entity ( FILE *  stream,
entity_t  entity 
)

Print an XML entity.

Parameters
[in]entityThe entity.
[in]streamThe stream to which to print.
void print_entity_format ( entity_t  entity,
gpointer  indent 
)

Print an XML entity to stdout, recusively 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.
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 (will be created if NULL).
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.
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.
[in]timeoutSeconds or server idle time before giving up. 0 to wait forever.
[out]entityPointer to an entity tree.
[out]stringAn 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.
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.
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.
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.
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]stringAn 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.
int xml_count_entities ( entities_t  entities)

Count the number of entities.

Parameters
[in]entitiesEntities.
Returns
Number of entities.
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.
Returns
Result of XSL transformation.