Greenbone Vulnerability Management Libraries
21.4.2
|
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>
Macros | |
#define | G_LOG_DOMAIN "libgvm util" |
GLib logging domain. More... | |
#define | BUFFER_SIZE 1048576 |
Size of the buffer for reading from the manager. More... | |
#define | XML_FILE_BUFFER_SIZE 1048576 |
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... | |
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. More... | |
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. More... | |
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. More... | |
static void | ignore_end_element (GMarkupParseContext *context, const gchar *element_name, gpointer user_data, GError **error) |
Handle the end of an XML element. More... | |
static void | handle_end_element (GMarkupParseContext *context, const gchar *element_name, gpointer user_data, GError **error) |
Handle the end of an XML element. More... | |
void | xml_handle_end_element (context_data_t *context, const gchar *element_name) |
Handle the end of an XML element. More... | |
static void | ignore_text (GMarkupParseContext *context, const gchar *text, gsize text_len, gpointer user_data, GError **error) |
Handle additional text of an XML element. More... | |
static 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 | xml_handle_text (context_data_t *context, const gchar *text, gsize text_len) |
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 | 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. 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_string_c (gvm_connection_t *connection, 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_entity_and_text_c (gvm_connection_t *connection, 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 | read_string_c (gvm_connection_t *connection, 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 | try_read_entity_c (gvm_connection_t *connection, 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 | read_entity_s (int socket, entity_t *entity) |
Read an XML entity tree from the socket. More... | |
int | read_entity_c (gvm_connection_t *connection, 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... | |
static void | foreach_print_entity_to_string (gpointer entity, gpointer string) |
Print an XML entity for g_slist_foreach to a GString. More... | |
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. 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... | |
static void | foreach_print_entity (gpointer entity, gpointer stream) |
Print an XML entity for g_slist_foreach. More... | |
static void | foreach_print_attribute (gpointer name, gpointer value, gpointer stream) |
Print an XML attribute for g_hash_table_foreach. More... | |
void | print_entity (FILE *stream, entity_t entity) |
Print an XML entity. More... | |
static void | foreach_print_attribute_format (gpointer name, gpointer value, gpointer none) |
Print an XML attribute for g_hash_table_foreach to stdout. More... | |
void | print_entity_format (entity_t entity, gpointer indent) |
Print an XML entity to stdout, recursively 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... | |
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. More... | |
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. More... | |
int | parse_element (const gchar *string, element_t *element) |
Read an XML element tree from a string. More... | |
void | element_free (element_t element) |
Free an entire element tree. More... | |
const gchar * | element_name (element_t element) |
Get the name of an element. More... | |
static element_t | find_child (element_t element, const gchar *name) |
Find child in an element. More... | |
element_t | element_child (element_t element, const gchar *name) |
Get a child of an element. More... | |
gchar * | element_text (element_t element) |
Get text of an element. More... | |
gchar * | element_attribute (element_t element, const gchar *name) |
Get an attribute of an element. More... | |
element_t | element_first_child (element_t element) |
Get the first child of an element. More... | |
element_t | element_next (element_t element) |
Get the next sibling of an element. More... | |
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.
#define BUFFER_SIZE 1048576 |
Size of the buffer for reading from the manager.
#define G_LOG_DOMAIN "libgvm util" |
GLib logging domain.
#define XML_FILE_BUFFER_SIZE 1048576 |
void add_attributes | ( | entity_t | entity, |
const gchar ** | names, | ||
const gchar ** | values | ||
) |
Add attributes from an XML callback to an entity.
[in] | entity | The entity. |
[in] | names | List of attribute names. |
[in] | values | List 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.
[in] | entities | The tree of entities |
[in] | name | Name of the entity. Copied, copy is freed by free_entity. |
[in] | text | Text of the entity. Copied, copy is freed by free_entity. |
Compare two XML entity.
[in] | entity1 | First entity. |
[in] | entity2 | First entity. |
int compare_entity_with_name | ( | gconstpointer | entity, |
gconstpointer | name | ||
) |
Compare a given name with the name of a given entity.
[in] | entity | Entity. |
[in] | name | Name. |
gboolean compare_find_attribute | ( | gpointer | key, |
gpointer | value, | ||
gpointer | attributes2 | ||
) |
Look for a key-value pair in a hash table.
[in] | key | Key. |
[in] | value | Value. |
[in] | attributes2 | The hash table. |
gchar* element_attribute | ( | element_t | element, |
const gchar * | name | ||
) |
Get an attribute of an element.
[in] | element | Element. |
[in] | name | Name of the attribute. |
Get a child of an element.
[in] | element | Element. |
[in] | name | Name of the child. |
Get the first child of an element.
[in] | element | Element. |
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.
[in] | element | Element. |
const gchar* element_name | ( | element_t | element | ) |
Get the name of an element.
[in] | element | Element. |
Get the next sibling of an element.
[in] | element | Element. |
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.
[in] | element | Element. |
const char* entity_attribute | ( | entity_t | entity, |
const char * | name | ||
) |
Get an attribute of an entity.
[in] | entity | Entity. |
[in] | name | Name of the attribute. |
Get a child of an entity.
[in] | entity | Entity. |
[in] | name | Name of the child. |
char* entity_name | ( | entity_t | entity | ) |
Get the name an entity.
[in] | entity | Entity. |
char* entity_text | ( | entity_t | entity | ) |
Get the text an entity.
[in] | entity | Entity. |
Find child in an element.
[in] | element | Element. |
[in] | name | Name of child. |
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.
[in] | file_path | Path of the XML file. |
[in] | find_element | Name of the element to find. |
[in] | find_attributes | GHashTable of attributes to find. |
entity_t first_entity | ( | entities_t | entities | ) |
Return the first entity from an entities_t.
[in] | entities | The list of entities. |
|
static |
Print an XML attribute for g_hash_table_foreach.
[in] | name | The attribute name. |
[in] | value | The attribute value. |
[in] | stream | The stream to which to print. |
|
static |
Print an XML attribute for g_hash_table_foreach to stdout.
[in] | name | The attribute name. |
[in] | value | The attribute value. |
[in] | none | (ignored). |
|
static |
Print an XML attribute for g_hash_table_foreach to a GString.
[in] | name | The attribute name. |
[in] | value | The attribute value. |
[in] | string | The string to which to print. |
|
static |
Print an XML entity for g_slist_foreach.
[in] | entity | The entity, as a gpointer. |
[in] | stream | The stream to which to print, as a gpointer. |
|
static |
Print an XML entity for g_slist_foreach to a GString.
[in] | entity | The entity, as a gpointer. |
[in] | string | The stream to which to print, as a gpointer. |
void free_entity | ( | entity_t | entity | ) |
Free an entity, recursively.
[in] | entity | The entity, can be NULL. |
|
static |
Handle the end of an XML element.
[in] | context | Parser context. |
[in] | element_name | XML element name. |
[in] | user_data | Dummy parameter. |
[in] | error | Error parameter. |
void handle_error | ( | GMarkupParseContext * | context, |
GError * | error, | ||
gpointer | user_data | ||
) |
Handle an OMP XML parsing error.
[in] | context | Parser context. |
[in] | error | The error. |
[in] | user_data | Dummy parameter. |
|
static |
Handle the start of an OMP XML element.
[in] | context | Parser context. |
[in] | element_name | XML element name. |
[in] | attribute_names | XML attribute name. |
[in] | attribute_values | XML attribute values. |
[in] | user_data | Dummy parameter. |
[in] | error | Error parameter. |
|
static |
Handle additional text of an XML element.
[in] | context | Parser context. |
[in] | text | The text. |
[in] | text_len | Length of the text. |
[in] | user_data | Dummy parameter. |
[in] | error | Error parameter. |
|
static |
Handle the end of an XML element.
[in] | context | Parser context. |
[in] | element_name | XML element name. |
[in] | user_data | Dummy parameter. |
[in] | error | Error parameter. |
|
static |
Handle the start of an OMP XML element.
[in] | context | Parser context. |
[in] | element_name | XML element name. |
[in] | attribute_names | XML attribute name. |
[in] | attribute_values | XML attribute values. |
[in] | user_data | Dummy parameter. |
[in] | error | Error parameter. |
|
static |
Handle additional text of an XML element.
[in] | context | Parser context. |
[in] | text | The text. |
[in] | text_len | Length of the text. |
[in] | user_data | Dummy parameter. |
[in] | error | Error parameter. |
entity_t make_entity | ( | const char * | name, |
const char * | text | ||
) |
Create an entity.
[in] | name | Name of the entity. Copied, freed by free_entity. |
[in] | text | Text of the entity. Copied, freed by free_entity. |
entities_t next_entities | ( | entities_t | entities | ) |
Return all the entities from an entities_t after the first.
[in] | entities | The list of entities. |
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.
[in] | string | Input string. |
[out] | element | Location for parsed element tree, or NULL if not required. If given, set to NULL on failure. Free with element_free. |
int parse_entity | ( | const char * | string, |
entity_t * | entity | ||
) |
Read an XML entity tree from a string.
[in] | string | Input string. |
[out] | entity | Pointer to an entity tree. |
void print_entity | ( | FILE * | stream, |
entity_t | entity | ||
) |
Print an XML entity.
[in] | entity | The entity. |
[in] | stream | The stream to which to print. |
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.
[in] | entity | The entity. |
[in] | indent | Indentation 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.
[in] | entity | Entity tree to print to string. |
[in,out] | string | String to write to. |
int read_entity | ( | gnutls_session_t * | session, |
entity_t * | entity | ||
) |
Read an XML entity tree from the manager.
[in] | session | Pointer to GNUTLS session. |
[out] | entity | Pointer to an entity tree. |
int read_entity_and_string | ( | gnutls_session_t * | session, |
entity_t * | entity, | ||
GString ** | string_return | ||
) |
Try read an XML entity tree from the manager.
[in] | session | Pointer to GNUTLS session. |
[out] | entity | Pointer to an entity tree. |
[out] | string_return | An 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. |
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.
[in] | connection | Connection. |
[out] | entity | Pointer to an entity tree. |
[out] | string_return | An 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. |
int read_entity_and_text | ( | gnutls_session_t * | session, |
entity_t * | entity, | ||
char ** | text | ||
) |
Read an XML entity tree from the manager.
[in] | session | Pointer to GNUTLS session. |
[out] | entity | Pointer to an entity tree. |
[out] | text | A 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. |
int read_entity_and_text_c | ( | gvm_connection_t * | connection, |
entity_t * | entity, | ||
char ** | text | ||
) |
Read an XML entity tree from the manager.
[in] | connection | Connection. |
[out] | entity | Entity tree. |
[out] | text | A 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. |
int read_entity_c | ( | gvm_connection_t * | connection, |
entity_t * | entity | ||
) |
Read an XML entity tree from the manager.
[in] | connection | Connection. |
[out] | entity | Pointer to an entity tree. |
int read_entity_s | ( | int | socket, |
entity_t * | entity | ||
) |
Read an XML entity tree from the socket.
[in] | socket | Socket to read from. |
[out] | entity | Pointer to an entity tree. |
int read_string | ( | gnutls_session_t * | session, |
GString ** | string | ||
) |
Read entity and text. Free the entity immediately.
[in] | session | Pointer to GNUTLS session to read from. |
[out] | string | Return location for the string. |
int read_string_c | ( | gvm_connection_t * | connection, |
GString ** | string | ||
) |
Read entity and text. Free the entity immediately.
[in] | connection | Connection. |
[out] | string | Return location for the string. |
int try_read_entity | ( | gnutls_session_t * | session, |
int | timeout, | ||
entity_t * | entity | ||
) |
Try read an XML entity tree from the manager.
[in] | session | Pointer to GNUTLS session. |
[in] | timeout | Server idle time before giving up, in seconds. 0 to wait forever. |
[out] | entity | Pointer to an entity tree. |
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.
[in] | session | Pointer to GNUTLS session. |
[in] | timeout | Server idle time before giving up, in seconds. 0 to wait forever. |
[out] | entity | Pointer to an entity tree. |
[out] | string_return | An 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. |
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.
[in] | socket | Socket to read from. |
[in] | timeout | Server idle time before giving up, in seconds. 0 to wait forever. |
[out] | entity | Pointer to an entity tree. |
[out] | string_return | An 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. |
int try_read_entity_c | ( | gvm_connection_t * | connection, |
int | timeout, | ||
entity_t * | entity | ||
) |
Try read an XML entity tree from the manager.
[in] | connection | Connection. |
[in] | timeout | Server idle time before giving up, in seconds. 0 to wait forever. |
[out] | entity | Pointer to an entity tree. |
int xml_count_entities | ( | entities_t | entities | ) |
Count the number of entities.
[in] | entities | Entities. |
void xml_handle_end_element | ( | context_data_t * | context, |
const gchar * | element_name | ||
) |
Handle the end of an XML element.
[in] | context | Parser context. |
[in] | element_name | XML element name. |
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.
[in] | context | Parser context. |
[in] | element_name | XML element name. |
[in] | attribute_names | XML attribute name. |
[in] | attribute_values | XML attribute values. |
void xml_handle_text | ( | context_data_t * | context, |
const gchar * | text, | ||
gsize | text_len | ||
) |
Handle additional text of an XML element.
[in] | context | Parser context. |
[in] | text | The text. |
[in] | text_len | Length of the text. |
|
static |
Handle the opening tag of an element in an XML search.
[in] | ctx | The parse context. |
[in] | element_name | The name of the element. |
[in] | attribute_names | NULL-terminated array of attribute names. |
[in] | attribute_values | NULL-terminated array of attribute values. |
[in] | data | The search data struct. |
[out] | error | Pointer to error output location. |
void xml_string_append | ( | GString * | xml, |
const char * | format, | ||
... | |||
) |
Append formatted escaped XML to a string.
[in] | xml | XML string. |
[in] | format | Format string. |
[in] | ... | Arguments for format string. |