21#include <glib/gtypes.h>
22#include <libxml/parser.h>
23#include <libxml/tree.h>
32#define G_LOG_DOMAIN "libgvm util"
37#define BUFFER_SIZE 1048576
51 entity = g_malloc (
sizeof (*entity));
52 entity->
name = g_strdup (name ? name :
"");
53 entity->
text = g_strdup (text ? text :
"");
105 *entities = g_slist_append (*entities, entity);
119 g_free (entity->
name);
120 g_free (entity->
text);
202 return match ? (
entity_t) match->data : NULL;
222 return (
const char *) g_hash_table_lookup (entity->
attributes, name);
236 if (names && values && *names && *values)
240 g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
241 while (*names && *values)
244 g_hash_table_insert (entity->
attributes, g_strdup (*names),
264 const gchar **attribute_names,
265 const gchar **attribute_values, gpointer user_data,
272 (void) attribute_names;
273 (void) attribute_values;
276 data->
current = GINT_TO_POINTER (GPOINTER_TO_INT (data->
current) + 1);
291 const gchar **attribute_names,
292 const gchar **attribute_values, gpointer user_data,
311 if (data->
first == NULL)
312 data->
current = data->
first = g_slist_prepend (NULL, entity);
327 const gchar **attribute_names,
328 const gchar **attribute_values)
344 gpointer user_data, GError **error)
352 data->
current = GINT_TO_POINTER (GPOINTER_TO_INT (data->
current) - 1);
367 gpointer user_data, GError **error)
391 g_slist_free_1 (front);
417ignore_text (GMarkupParseContext *context,
const gchar *text, gsize text_len,
418 gpointer user_data, GError **error)
437handle_text (GMarkupParseContext *context,
const gchar *text, gsize text_len,
438 gpointer user_data, GError **error)
448 gchar *old = current->
text;
449 current->
text = g_strconcat (current->
text, text, NULL);
453 current->
text = g_strdup (text);
477handle_error (GMarkupParseContext *context, GError *error, gpointer user_data)
481 g_message (
" Error: %s\n", error->message);
502 entity_t *entity, GString **string_return)
504 GMarkupParser xml_parser;
505 GError *error = NULL;
506 GMarkupParseContext *xml_context;
516 if (time (&last_time) == -1)
518 g_warning (
" failed to get current time: %s\n", strerror (errno));
526 socket = GPOINTER_TO_INT (gnutls_transport_get_ptr (*session));
527 if (fcntl (socket, F_SETFL, O_NONBLOCK) == -1)
540 if (string_return == NULL)
542 else if (*string_return == NULL)
543 string = g_string_new (
"");
545 string = *string_return;
561 xml_parser.passthrough = NULL;
565 context_data.
done = FALSE;
566 context_data.
first = NULL;
572 g_markup_parse_context_new (&xml_parser, 0, &context_data, NULL);
583 count = gnutls_record_recv (*session, buffer,
BUFFER_SIZE);
586 if (count == GNUTLS_E_INTERRUPTED)
589 if ((timeout > 0) && (count == GNUTLS_E_AGAIN))
592 if ((timeout - (time (NULL) - last_time)) <= 0)
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);
604 else if ((timeout == 0) && (count == GNUTLS_E_AGAIN))
612 retries = retries - 1;
617 if (count == GNUTLS_E_REHANDSHAKE)
620 if (context_data.
first && context_data.
first->data)
623 g_slist_free_1 (context_data.
first);
625 if (
string && *string_return == NULL)
626 g_string_free (
string, TRUE);
629 if (fcntl (socket, F_SETFL, 0L) < 0)
630 g_warning (
"%s :failed to set socket flag: %s", __func__,
633 g_markup_parse_context_free (xml_context);
640 g_markup_parse_context_end_parse (xml_context, &error);
643 g_warning (
" End error: %s\n", error->message);
644 g_error_free (error);
646 if (context_data.
first && context_data.
first->data)
649 g_slist_free_1 (context_data.
first);
651 if (
string && *string_return == NULL)
652 g_string_free (
string, TRUE);
655 if (fcntl (socket, F_SETFL, 0L) < 0)
656 g_warning (
"%s :failed to set socket flag: %s", __func__,
659 g_markup_parse_context_free (xml_context);
666 g_debug (
"<= %.*s\n", (
int) count, buffer);
669 g_string_append_len (
string, buffer, count);
671 g_markup_parse_context_parse (xml_context, buffer, count, &error);
674 g_error_free (error);
675 if (context_data.
first && context_data.
first->data)
678 g_slist_free_1 (context_data.
first);
680 if (
string && *string_return == NULL)
681 g_string_free (
string, TRUE);
684 if (fcntl (socket, F_SETFL, 0L) < 0)
685 g_warning (
"%s :failed to set socket flag: %s", __func__,
688 g_markup_parse_context_free (xml_context);
692 if (context_data.
done)
694 g_markup_parse_context_end_parse (xml_context, &error);
697 g_warning (
" End error: %s\n", error->message);
698 g_error_free (error);
699 if (context_data.
first && context_data.
first->data)
702 g_slist_free_1 (context_data.
first);
705 fcntl (socket, F_SETFL, 0L);
706 g_markup_parse_context_free (xml_context);
713 *string_return = string;
715 fcntl (socket, F_SETFL, 0L);
716 g_markup_parse_context_free (xml_context);
721 if ((timeout > 0) && (time (&last_time) == -1))
723 g_warning (
" failed to get current time (1): %s\n",
725 if (fcntl (socket, F_SETFL, 0L) < 0)
726 g_warning (
"%s :failed to set socket flag: %s", __func__,
728 g_markup_parse_context_free (xml_context);
753 GString **string_return)
762 if (time (&last_time) == -1)
764 g_warning (
" failed to get current time: %s\n", strerror (errno));
772 socket = GPOINTER_TO_INT (gnutls_transport_get_ptr (*session));
773 if (fcntl (socket, F_SETFL, O_NONBLOCK) == -1)
786 if (string_return == NULL)
788 else if (*string_return == NULL)
789 string = g_string_new (
"");
791 string = *string_return;
802 count = gnutls_record_recv (*session, buffer,
BUFFER_SIZE);
805 if (count == GNUTLS_E_INTERRUPTED)
808 if ((timeout > 0) && (count == GNUTLS_E_AGAIN))
811 if ((timeout - (time (NULL) - last_time)) <= 0)
813 g_warning (
" timeout\n");
814 if (fcntl (socket, F_SETFL, 0L) < 0)
815 g_warning (
"%s: failed to set socket flag: %s",
816 __func__, strerror (errno));
822 else if ((timeout == 0) && (count == GNUTLS_E_AGAIN))
830 retries = retries - 1;
835 if (count == GNUTLS_E_REHANDSHAKE)
838 if (
string && (*string_return == NULL))
839 g_string_free (
string, TRUE);
842 if (fcntl (socket, F_SETFL, 0L) < 0)
843 g_warning (
"%s: failed to set socket flag: %s", __func__,
854 if (fcntl (socket, F_SETFL, 0L) < 0)
855 g_warning (
"%s :failed to set socket flag: %s", __func__,
859 *string_return = string;
866 g_debug (
"<= %.*s\n", (
int) count, buffer);
869 g_string_append_len (
string, buffer, count);
871 if ((timeout > 0) && (time (&last_time) == -1))
873 g_warning (
" failed to get current time (1): %s\n",
875 if (fcntl (socket, F_SETFL, 0L) < 0)
876 g_warning (
"%s :failed to set socket flag: %s", __func__,
909 if (time (&last_time) == -1)
911 g_warning (
" failed to get current time: %s\n", strerror (errno));
919 if (fcntl (socket, F_SETFL, O_NONBLOCK) == -1)
929 if (string_return == NULL)
931 else if (*string_return == NULL)
932 string = g_string_sized_new (8192);
934 string = *string_return;
955 if ((timeout - (time (NULL) - last_time)) <= 0)
957 g_warning (
" timeout\n");
958 if (fcntl (socket, F_SETFL, 0L) < 0)
959 g_warning (
"%s :failed to set socket flag: %s",
960 __func__, strerror (errno));
962 if (
string && *string_return == NULL)
963 g_string_free (
string, TRUE);
969 if (
string && *string_return == NULL)
970 g_string_free (
string, TRUE);
972 fcntl (socket, F_SETFL, 0L);
981 if (fcntl (socket, F_SETFL, 0L) < 0)
982 g_warning (
"%s :failed to set socket flag: %s", __func__,
986 *string_return = string;
993 g_debug (
"<= %.*s\n", (
int) count, buffer);
996 g_string_append_len (
string, buffer, count);
998 if ((timeout > 0) && (time (&last_time) == -1))
1000 g_warning (
" failed to get current time (1): %s\n",
1002 if (fcntl (socket, F_SETFL, 0L) < 0)
1003 g_warning (
"%s :failed to set server socket flag: %s", __func__,
1006 if (
string && *string_return == NULL)
1007 g_string_free (
string, TRUE);
1031 GString **string_return)
1033 GMarkupParser xml_parser;
1034 GError *error = NULL;
1035 GMarkupParseContext *xml_context;
1043 if (time (&last_time) == -1)
1045 g_warning (
" failed to get current time: %s\n", strerror (errno));
1053 if (fcntl (socket, F_SETFL, O_NONBLOCK) == -1)
1063 if (string_return == NULL)
1065 else if (*string_return == NULL)
1066 string = g_string_new (
"");
1068 string = *string_return;
1084 xml_parser.passthrough = NULL;
1088 context_data.
done = FALSE;
1089 context_data.
first = NULL;
1095 g_markup_parse_context_new (&xml_parser, 0, &context_data, NULL);
1113 if (errno == EAGAIN)
1116 if ((timeout - (time (NULL) - last_time)) <= 0)
1118 g_warning (
" timeout\n");
1119 if (fcntl (socket, F_SETFL, 0L) < 0)
1120 g_warning (
"%s :failed to set socket flag: %s",
1121 __func__, strerror (errno));
1122 g_markup_parse_context_free (xml_context);
1124 if (
string && *string_return == NULL)
1125 g_string_free (
string, TRUE);
1131 if (context_data.
first && context_data.
first->data)
1134 g_slist_free_1 (context_data.
first);
1136 if (
string && *string_return == NULL)
1137 g_string_free (
string, TRUE);
1139 fcntl (socket, F_SETFL, 0L);
1140 g_markup_parse_context_free (xml_context);
1147 g_markup_parse_context_end_parse (xml_context, &error);
1150 g_warning (
" End error: %s\n", error->message);
1151 g_error_free (error);
1153 if (context_data.
first && context_data.
first->data)
1156 g_slist_free_1 (context_data.
first);
1158 if (
string && *string_return == NULL)
1159 g_string_free (
string, TRUE);
1162 if (fcntl (socket, F_SETFL, 0L) < 0)
1163 g_warning (
"%s :failed to set socket flag: %s", __func__,
1166 g_markup_parse_context_free (xml_context);
1173 g_debug (
"<= %.*s\n", (
int) count, buffer);
1176 g_string_append_len (
string, buffer, count);
1178 g_markup_parse_context_parse (xml_context, buffer, count, &error);
1181 g_error_free (error);
1183 if (context_data.
first && context_data.
first->data)
1186 g_slist_free_1 (context_data.
first);
1188 if (
string && *string_return == NULL)
1189 g_string_free (
string, TRUE);
1192 if (fcntl (socket, F_SETFL, 0L) < 0)
1193 g_warning (
"%s :failed to set socket flag: %s", __func__,
1196 g_markup_parse_context_free (xml_context);
1200 if (context_data.
done)
1202 g_markup_parse_context_end_parse (xml_context, &error);
1205 g_warning (
" End error: %s\n", error->message);
1206 g_error_free (error);
1207 if (context_data.
first && context_data.
first->data)
1210 g_slist_free_1 (context_data.
first);
1213 fcntl (socket, F_SETFL, 0L);
1214 g_markup_parse_context_free (xml_context);
1216 if (
string && *string_return == NULL)
1217 g_string_free (
string, TRUE);
1223 *string_return = string;
1225 fcntl (socket, F_SETFL, 0L);
1226 g_slist_free (context_data.
first);
1227 g_markup_parse_context_free (xml_context);
1232 if ((timeout > 0) && (time (&last_time) == -1))
1234 g_warning (
" failed to get current time (1): %s\n",
1236 if (fcntl (socket, F_SETFL, 0L) < 0)
1237 g_warning (
"%s :failed to set server socket flag: %s", __func__,
1239 g_markup_parse_context_free (xml_context);
1241 if (
string && *string_return == NULL)
1242 g_string_free (
string, TRUE);
1263 GString **string_return)
1283 GString **string_return)
1285 if (connection->
tls)
1309 GString *
string = NULL;
1314 g_string_free (
string, TRUE);
1317 *text = g_string_free (
string, FALSE);
1341 GString *
string = NULL;
1346 g_string_free (
string, TRUE);
1349 *text = g_string_free (
string, FALSE);
1376 if (connection->
tls)
1384 g_string_free (
string, TRUE);
1387 *text = g_string_free (
string, FALSE);
1454 if (connection->
tls)
1513 GMarkupParser xml_parser;
1514 GError *error = NULL;
1515 GMarkupParseContext *xml_context;
1523 xml_parser.passthrough = NULL;
1526 context_data.
done = FALSE;
1527 context_data.
first = NULL;
1533 g_markup_parse_context_new (&xml_parser, 0, &context_data, NULL);
1537 g_markup_parse_context_parse (xml_context,
string, strlen (
string), &error);
1540 g_error_free (error);
1541 if (context_data.
first && context_data.
first->data)
1544 g_slist_free_1 (context_data.
first);
1546 g_markup_parse_context_free (xml_context);
1549 if (context_data.
done)
1551 g_markup_parse_context_end_parse (xml_context, &error);
1554 g_warning (
" End error: %s\n", error->message);
1555 g_error_free (error);
1556 if (context_data.
first && context_data.
first->data)
1559 g_slist_free_1 (context_data.
first);
1561 g_markup_parse_context_free (xml_context);
1565 g_slist_free_1 (context_data.
first);
1566 g_markup_parse_context_free (xml_context);
1569 if (context_data.
first && context_data.
first->data)
1572 g_slist_free_1 (context_data.
first);
1574 g_markup_parse_context_free (xml_context);
1601 gchar *text_escaped;
1602 text_escaped = g_markup_escape_text ((gchar *) value, -1);
1603 g_string_append_printf ((GString *)
string,
" %s=\"%s\"", (
char *) name,
1605 g_free (text_escaped);
1618 gchar *text_escaped = NULL;
1619 g_string_append_printf (
string,
"<%s", entity->
name);
1623 g_string_append_printf (
string,
">");
1624 text_escaped = g_markup_escape_text (entity->
text, -1);
1625 g_string_append_printf (
string,
"%s", text_escaped);
1626 g_free (text_escaped);
1628 g_string_append_printf (
string,
"</%s>", entity->
name);
1653 fprintf ((FILE *) stream,
" %s=\"%s\"", (
char *) name, (
char *) value);
1665 gchar *text_escaped = NULL;
1666 fprintf (stream,
"<%s", entity->
name);
1669 fprintf (stream,
">");
1670 text_escaped = g_markup_escape_text (entity->
text, -1);
1671 fprintf (stream,
"%s", text_escaped);
1672 g_free (text_escaped);
1674 fprintf (stream,
"</%s>", entity->
name);
1691 printf (
" %s=\"%s\"", (
char *) name, (
char *) value);
1709 int indentation = GPOINTER_TO_INT (indent);
1710 gchar *text_escaped = NULL;
1712 for (i = 0; i < indentation; i++)
1715 printf (
"<%s", entity->
name);
1721 text_escaped = g_markup_escape_text (entity->
text, -1);
1722 printf (
"%s", text_escaped);
1723 g_free (text_escaped);
1729 GINT_TO_POINTER (indentation + 1));
1730 for (i = 0; i < indentation; i++)
1734 printf (
"</%s>\n", entity->
name);
1749 gchar *value2 = g_hash_table_lookup (attributes2, key);
1750 if (value2 && strcmp (value, value2) == 0)
1752 g_debug (
" compare failed attribute: %s\n", (
char *) value);
1767 if (entity1 == NULL)
1768 return entity2 == NULL ? 0 : 1;
1769 if (entity2 == NULL)
1772 if (strcmp (entity1->
name, entity2->
name))
1774 g_debug (
" compare failed name: %s vs %s\n", entity1->
name,
1778 if (strcmp (entity1->
text, entity2->
text))
1780 g_debug (
" compare failed text %s vs %s (%s)\n", entity1->
text,
1797 g_debug (
" compare failed attributes\n");
1805 while (list1 && list2)
1809 g_debug (
" compare failed subentity\n");
1812 list1 = g_slist_next (list1);
1813 list2 = g_slist_next (list2);
1818 g_debug (
" compare failed number of entities (%s)\n", entity1->
name);
1854 va_start (args, format);
1855 piece = g_markup_vprintf_escaped (format, args);
1857 g_string_append (xml, piece);
1876 const gchar **attribute_names,
1877 const gchar **attribute_values, gpointer data,
1886 && search_data->
found == 0)
1888 g_debug (
"%s: Found element <%s>", __func__,
element_name);
1894 GHashTable *found_attributes;
1896 g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL);
1898 while (attribute_names[index])
1900 gchar *searched_value;
1901 searched_value = g_hash_table_lookup (
1904 && strcmp (searched_value, attribute_values[index]) == 0)
1906 g_debug (
"%s: Found attribute %s=\"%s\"", __func__,
1907 attribute_names[index], searched_value);
1908 g_hash_table_add (found_attributes, searched_value);
1912 g_debug (
"%s: Found %d of %d attributes", __func__,
1913 g_hash_table_size (found_attributes),
1916 if (g_hash_table_size (found_attributes)
1919 search_data->
found = 1;
1922 g_hash_table_destroy (found_attributes);
1926 search_data->
found = 1;
1931#define XML_FILE_BUFFER_SIZE 1048576
1943 GHashTable *find_attributes)
1948 GMarkupParser xml_parser;
1949 GMarkupParseContext *xml_context;
1951 GError *error = NULL;
1955 search_data.
found = 0;
1959 xml_parser.end_element = NULL;
1960 xml_parser.text = NULL;
1961 xml_parser.passthrough = NULL;
1962 xml_parser.error = NULL;
1963 xml_context = g_markup_parse_context_new (&xml_parser, 0, &search_data, NULL);
1965 file = fopen (file_path,
"r");
1968 g_markup_parse_context_free (xml_context);
1969 g_warning (
"%s: Failed to open '%s':", __func__, strerror (errno));
1974 && g_markup_parse_context_parse (xml_context, buffer, read_len, &error)
1978 g_markup_parse_context_end_parse (xml_context, &error);
1982 g_markup_parse_context_free (xml_context);
1983 return search_data.
found;
1985#undef XML_FILE_BUFFER_SIZE
2012 if (xmlMemSetup (g_free, g_malloc, g_realloc, g_strdup))
2016 xmlReadMemory (
string, strlen (
string),
"noname.xml", NULL, XML_PARSE_HUGE);
2021 *element = xmlDocGetRootElement (doc);
2039 assert (element->doc);
2040 xmlFreeDoc (element->doc);
2054 if (element && (element->type == XML_ELEMENT_NODE))
2055 return (
const gchar *) element->name;
2071 for (xmlNode *node = element->children; node; node = node->next)
2072 if (xmlStrcmp (node->name, (
const xmlChar *) name) == 0)
2088 const gchar *stripped_name;
2093 stripped_name = strchr (name,
':');
2105 if (*stripped_name ==
'\0')
2145 (gchar *) xmlNodeListGetString (element->doc, element->xmlChildrenNode, 1);
2148 string = xmlMalloc (1);
2164 const gchar *stripped_name;
2169 stripped_name = strchr (name,
':');
2181 if (*stripped_name ==
'\0')
2185 return (gchar *) xmlGetProp (element, (
const xmlChar *) name);
2188 (gchar *) xmlGetProp (element, (
const xmlChar *) stripped_name);
2199 return (gchar *) xmlGetProp (element, (
const xmlChar *) name);
2214 element = element->children;
2215 while (element && (element->type != XML_ELEMENT_NODE))
2216 element = element->next;
2234 element = element->next;
2235 while (element && (element->type != XML_ELEMENT_NODE))
2236 element = element->next;
2255 xmlBufferPtr buffer;
2260 element_copy = xmlCopyNode (element, 1);
2262 buffer = xmlBufferCreate ();
2263 xmlNodeDump (buffer, element_copy->doc, element_copy, 0, 0);
2264 xmlFreeNode (element_copy);
2266 xml_string = g_strdup ((
char *) xmlBufferContent (buffer));
2268 xmlBufferFree (buffer);
2282 gchar *text_escaped, *text;
2286 text_escaped = NULL;
2288 g_string_append_printf (
string,
"<%s",
element_name (element));
2290 attribute = element->properties;
2295 value = xmlNodeListGetString (element->doc, attribute->children, 1);
2297 text_escaped = g_markup_escape_text ((gchar *) value, -1);
2298 g_string_append_printf (
string,
" %s=\"%s\"", attribute->name,
2300 g_free (text_escaped);
2304 attribute = attribute->next;
2307 g_string_append_printf (
string,
">");
2310 text_escaped = g_markup_escape_text (text, -1);
2312 g_string_append_printf (
string,
"%s", text_escaped);
2313 g_free (text_escaped);
2322 g_string_append_printf (
string,
"</%s>",
element_name (element));
2362 const xmlChar *prefix,
const xmlChar *URI,
2364 const xmlChar **namespaces,
2365 int nb_attributes,
int nb_defaulted,
2366 const xmlChar **attributes)
2369 xmlSAX2StartElementNs (iterator->
parser_ctxt, localname, prefix, URI,
2370 nb_namespaces, namespaces, nb_attributes, nb_defaulted,
2391 const xmlChar *prefix,
const xmlChar *URI)
2394 xmlSAX2EndElementNs (iterator->
parser_ctxt, localname, prefix, URI);
2398 xmlNodePtr parent, child;
2401 child = parent->children;
2404 if (child->type == XML_ELEMENT_NODE)
2406 xmlDocPtr new_doc = xmlNewDoc ((
const xmlChar *)
"1.0");
2408 child_copy = xmlCopyNode (child, 1);
2409 xmlDocSetRootElement (new_doc, child_copy);
2417 xmlUnlinkNode (child);
2418 xmlFreeNode (child);
2420 child = parent->children;
2438 const xmlChar *ExternalID,
2439 const xmlChar *SystemID)
2442 xmlSAX2InternalSubset (iterator->
parser_ctxt, name, ExternalID, SystemID);
2458 const xmlChar *ExternalID,
2459 const xmlChar *SystemID)
2462 xmlSAX2ExternalSubset (iterator->
parser_ctxt, name, ExternalID, SystemID);
2479 return xmlSAX2IsStandalone (iterator->
parser_ctxt);
2497 return xmlSAX2HasInternalSubset (iterator->
parser_ctxt);
2515 return xmlSAX2HasExternalSubset (iterator->
parser_ctxt);
2530static xmlParserInputPtr
2532 const xmlChar *systemId)
2535 return xmlSAX2ResolveEntity (iterator->
parser_ctxt, publicId, systemId);
2553 return xmlSAX2GetEntity (iterator->
parser_ctxt, name);
2572 return xmlSAX2GetParameterEntity (iterator->
parser_ctxt, name);
2591 const xmlChar *publicId,
const xmlChar *systemId,
2595 xmlSAX2EntityDecl (iterator->
parser_ctxt, name, type, publicId, systemId,
2616 const xmlChar *fullname,
int type,
int def,
2617 const xmlChar *defaultValue,
2618 xmlEnumerationPtr tree)
2621 xmlSAX2AttributeDecl (iterator->
parser_ctxt, elem, fullname, type, def,
2622 defaultValue, tree);
2641 xmlElementContentPtr content)
2644 xmlSAX2ElementDecl (iterator->
parser_ctxt, name, type, content);
2661 const xmlChar *publicId,
2662 const xmlChar *systemId)
2665 xmlSAX2NotationDecl (iterator->
parser_ctxt, name, publicId, systemId);
2683 const xmlChar *publicId,
2684 const xmlChar *systemId,
2685 const xmlChar *notationName)
2688 xmlSAX2UnparsedEntityDecl (iterator->
parser_ctxt, name, publicId, systemId,
2705 xmlSAX2SetDocumentLocator (iterator->
parser_ctxt, loc);
2753 xmlSAX2Characters (iterator->
parser_ctxt, ch, len);
2771 xmlSAX2CDataBlock (iterator->
parser_ctxt, ch, len);
2787 const xmlChar *data)
2790 xmlSAX2ProcessingInstruction (iterator->
parser_ctxt, target, data);
2821 hdlr->initialized = XML_SAX2_MAGIC;
2822 hdlr->startElement = NULL;
2823 hdlr->endElement = NULL;
2840 hdlr->reference = NULL;
2846 hdlr->warning = xmlParserWarning;
2847 hdlr->error = xmlParserError;
2848 hdlr->fatalError = xmlParserError;
2877 const char *file_path,
int output_depth)
2879 if (iterator == NULL)
2889 if (output_depth < 0)
2893 iterator->
file = fopen (file_path,
"rb");
2894 if (iterator->
file == NULL)
2899 iterator->
file_path = g_strdup (file_path);
2921 if (iterator == NULL)
2926 fclose (iterator->
file);
2958 if (iterator == NULL)
2963 rewind (iterator->
file);
2990#define XML_FILE_ITERATOR_BUFFER_SIZE 8192
3004 gboolean continue_read = TRUE;
3012 *error = g_strdup (
"iterator not initialized");
3016 while (continue_read && g_queue_is_empty (iterator->
element_queue))
3023 if (chars_read == 0)
3025 if (feof (iterator->
file))
3027 continue_read = FALSE;
3029 else if (ferror (iterator->
file))
3032 *error = g_strdup (
"error reading file");
3039 ret = xmlParseChunk (iterator->
parser_ctxt, buffer, chars_read,
3040 continue_read == 0);
3045 const xmlError *xml_error;
3046 xml_error = xmlCtxtGetLastError (iterator->
parser_ctxt);
3047 *error = g_strdup_printf (
"error parsing XML"
3048 " (line %d column %d): %s",
3049 xml_error->line, xml_error->int2,
3050 xml_error->message);
XML context.
Definition xmlutils.h:27
GSList * first
The very first entity.
Definition xmlutils.h:28
GSList * current
The element currently being parsed.
Definition xmlutils.h:29
gboolean done
Flag which is true when the first element is closed.
Definition xmlutils.h:30
entities_t entities
Children.
Definition xmlutils.h:56
char * text
Text.
Definition xmlutils.h:54
GHashTable * attributes
Attributes.
Definition xmlutils.h:55
char * name
Name.
Definition xmlutils.h:53
Connection.
Definition serverutils.h:30
int tls
Whether uses TCP-TLS (vs UNIX socket).
Definition serverutils.h:31
int socket
Socket.
Definition serverutils.h:32
gnutls_session_t session
Session.
Definition serverutils.h:33
Opaque data structure for XML file iterator.
Definition xmlutils.c:2331
xmlParserCtxtPtr parser_ctxt
Definition xmlutils.c:2336
int output_depth
Definition xmlutils.c:2333
GQueue * element_queue
Definition xmlutils.c:2334
int stack_depth
Definition xmlutils.c:2339
xmlSAXHandler sax_handler
Definition xmlutils.c:2335
int initialized
Definition xmlutils.c:2332
FILE * file
Definition xmlutils.c:2338
gchar * file_path
Definition xmlutils.c:2337
Data for xml search functions.
Definition xmlutils.h:64
gchar * find_element
Definition xmlutils.h:67
GHashTable * find_attributes
Definition xmlutils.h:68
int found
Definition xmlutils.h:65
static void handle_text(GMarkupParseContext *context, const gchar *text, gsize text_len, gpointer user_data, GError **error)
Handle additional text of an XML element.
Definition xmlutils.c:437
static 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
void element_free(element_t element)
Free an entire element tree.
Definition xmlutils.c:2035
int read_entity_c(gvm_connection_t *connection, entity_t *entity)
Read an XML entity tree from the manager.
Definition xmlutils.c:1497
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.
Definition xmlutils.c:326
const char * entity_attribute(entity_t entity, const char *name)
Get an attribute of an entity.
Definition xmlutils.c:216
int read_string(gnutls_session_t *session, GString **string)
Read entity and text. Free the entity immediately.
Definition xmlutils.c:1400
int read_string_c(gvm_connection_t *connection, GString **string)
Read entity and text. Free the entity immediately.
Definition xmlutils.c:1420
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
static void xml_file_iterator_characters(void *ctx, const xmlChar *ch, int len)
XML file iterator parser callback when receiving some chars from the parser.
Definition xmlutils.c:2750
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.
Definition xmlutils.c:1942
static void xml_file_iterator_unparsed_entity_decl(void *ctx, const xmlChar *name, const xmlChar *publicId, const xmlChar *systemId, const xmlChar *notationName)
XML file iterator parser callback for when an unparsed entity declaration has been parsed.
Definition xmlutils.c:2682
char * entity_name(entity_t entity)
Get the name an entity.
Definition xmlutils.c:161
char * entity_text(entity_t entity)
Get the text an entity.
Definition xmlutils.c:145
static void handle_end_element(GMarkupParseContext *context, const gchar *element_name, gpointer user_data, GError **error)
Handle the end of an XML element.
Definition xmlutils.c:366
static void xml_file_iterator_processing_instruction(void *ctx, const xmlChar *target, const xmlChar *data)
XML file iterator parser callback when a processing instruction has been parsed.
Definition xmlutils.c:2786
static void xml_file_iterator_entity_decl(void *ctx, const xmlChar *name, int type, const xmlChar *publicId, const xmlChar *systemId, xmlChar *content)
XML file iterator parser callback for when an entity definition has been parsed.
Definition xmlutils.c:2590
const gchar * element_name(element_t element)
Get the name of an element.
Definition xmlutils.c:2052
static void xml_file_iterator_init_sax_handler(xmlSAXHandlerPtr hdlr)
Initializes a xmlSAXHandler data structure for SAX version 2 parsing, assigning all the XML file iter...
Definition xmlutils.c:2816
static void xml_search_handle_start_element(GMarkupParseContext *ctx, const gchar *element_name, const gchar **attribute_names, const gchar **attribute_values, gpointer data, GError **error)
Handle the opening tag of an element in an XML search.
Definition xmlutils.c:1874
xml_file_iterator_t xml_file_iterator_new(void)
Allocates a new, uninitialized XML file iterator.
Definition xmlutils.c:2859
static int xml_file_iterator_is_standalone(void *ctx)
XML file iterator parser callback for checking if doc is standalone.
Definition xmlutils.c:2476
static void xml_file_iterator_start_document(void *ctx)
XML file iterator parser callback at the document start.
Definition xmlutils.c:2717
static 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
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
static xmlEntityPtr xml_file_iterator_get_entity(void *ctx, const xmlChar *name)
XML file iterator parser callback for getting an entity by name.
Definition xmlutils.c:2550
entity_t first_entity(entities_t entities)
Return the first entity from an entities_t.
Definition xmlutils.c:82
gchar * element_text(element_t element)
Get text of an element.
Definition xmlutils.c:2137
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
int xml_file_iterator_init_from_file_path(xml_file_iterator_t iterator, const char *file_path, int output_depth)
Initializes an XML file iterator to read from a given path.
Definition xmlutils.c:2876
static void handle_error(GMarkupParseContext *context, GError *error, gpointer user_data)
Handle an OMP XML parsing error.
Definition xmlutils.c:477
void print_element_to_string(element_t element, GString *string)
Print an XML element tree to a GString, appending it if string is not.
Definition xmlutils.c:2280
int read_entity(gnutls_session_t *session, entity_t *entity)
Read an XML entity tree from the manager.
Definition xmlutils.c:1469
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
static void xml_file_iterator_internal_subset(void *ctx, const xmlChar *name, const xmlChar *ExternalID, const xmlChar *SystemID)
XML file iterator parser callback for internal subset declaration.
Definition xmlutils.c:2437
int xml_file_iterator_rewind(xml_file_iterator_t iterator)
Rewinds an XML file iterator by rewinding the file and creating a new XML parser context.
Definition xmlutils.c:2956
static void foreach_print_entity_to_string(gpointer entity, gpointer string)
Print an XML entity for g_slist_foreach to a GString.
Definition xmlutils.c:1585
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
static void xml_file_iterator_comment(void *ctx, const xmlChar *value)
XML file iterator parser callback when a comment has been parsed.
Definition xmlutils.c:2803
static void foreach_print_entity(gpointer entity, gpointer stream)
Print an XML entity for g_slist_foreach.
Definition xmlutils.c:1638
entities_t next_entities(entities_t entities)
Return all the entities from an entities_t after the first.
Definition xmlutils.c:67
#define BUFFER_SIZE
Size of the buffer for reading from the manager.
Definition xmlutils.c:37
gchar * element_attribute(element_t element, const gchar *name)
Get an attribute of an element.
Definition xmlutils.c:2162
static void foreach_print_attribute_format(gpointer name, gpointer value, gpointer none)
Print an XML attribute for g_hash_table_foreach to stdout.
Definition xmlutils.c:1688
element_t element_first_child(element_t element)
Get the first child of an element.
Definition xmlutils.c:2210
void print_entity_format(entity_t entity, gpointer indent)
Print an XML entity to stdout, recursively printing its children.
Definition xmlutils.c:1706
element_t xml_file_iterator_next(xml_file_iterator_t iterator, gchar **error)
Get the next subelement from a XML file iterator.
Definition xmlutils.c:3002
void free_entity(entity_t entity)
Free an entity, recursively.
Definition xmlutils.c:115
static gboolean compare_find_attribute(gpointer key, gpointer value, gpointer attributes2)
Look for a key-value pair in a hash table.
Definition xmlutils.c:1747
static xmlEntityPtr xml_file_iterator_get_parameter_entity(void *ctx, const xmlChar *name)
XML file iterator parser callback for getting a parameter entity by name.
Definition xmlutils.c:2569
static void xml_file_iterator_end_element_ns(void *ctx, const xmlChar *localname, const xmlChar *prefix, const xmlChar *URI)
XML file iterator parser callback for element end.
Definition xmlutils.c:2390
void xml_file_iterator_free(xml_file_iterator_t iterator)
Frees an XML file iterator and all of its internal data structures.
Definition xmlutils.c:2919
static element_t find_child(element_t element, const gchar *name)
Find child in an element.
Definition xmlutils.c:2069
int read_text_c(gvm_connection_t *connection, char **text)
Read text from the server.
Definition xmlutils.c:1366
int read_entity_and_text_c(gvm_connection_t *connection, entity_t *entity, char **text)
Read an XML entity tree from the manager.
Definition xmlutils.c:1336
int parse_element(const gchar *string, element_t *element)
Read an XML element tree from a string.
Definition xmlutils.c:2003
static int xml_file_iterator_has_external_subset(void *ctx)
XML file iterator parser callback for checking if doc has an external subset.
Definition xmlutils.c:2512
static void xml_file_iterator_start_element_ns(void *ctx, const xmlChar *localname, const xmlChar *prefix, const xmlChar *URI, int nb_namespaces, const xmlChar **namespaces, int nb_attributes, int nb_defaulted, const xmlChar **attributes)
XML file iterator parser callback for element start.
Definition xmlutils.c:2361
int read_entity_and_text(gnutls_session_t *session, entity_t *entity, char **text)
Read an XML entity tree from the manager.
Definition xmlutils.c:1305
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
static xmlParserInputPtr xml_file_iterator_resolve_entity(void *ctx, const xmlChar *publicId, const xmlChar *systemId)
XML file iterator parser callback for resolving an entity.
Definition xmlutils.c:2531
static int compare_entity_with_name(gconstpointer entity, gconstpointer name)
Compare a given name with the name of a given entity.
Definition xmlutils.c:179
entity_t add_entity(entities_t *entities, const char *name, const char *text)
Add an XML entity to a tree of entities.
Definition xmlutils.c:101
static void xml_file_iterator_set_document_locator(void *ctx, xmlSAXLocatorPtr loc)
XML file iterator parser callback for setting the document locator.
Definition xmlutils.c:2702
static void xml_file_iterator_external_subset(void *ctx, const xmlChar *name, const xmlChar *ExternalID, const xmlChar *SystemID)
XML file iterator parser callback for external subset declaration.
Definition xmlutils.c:2457
int xml_count_entities(entities_t entities)
Count the number of entities.
Definition xmlutils.c:1830
static void xml_file_iterator_cdata_block(void *ctx, const xmlChar *ch, int len)
XML file iterator parser callback when a cdata block has been parsed.
Definition xmlutils.c:2768
entity_t entity_child(entity_t entity, const char *name)
Get a child of an entity.
Definition xmlutils.c:193
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
void xml_handle_end_element(context_data_t *context, const gchar *element_name)
Handle the end of an XML element.
Definition xmlutils.c:402
static void ignore_start_element(GMarkupParseContext *context, const gchar *element_name, const gchar **attribute_names, const gchar **attribute_values, gpointer user_data, GError **error)
Handle the start of an OMP XML element.
Definition xmlutils.c:263
static entity_t make_entity(const char *name, const char *text)
Create an entity.
Definition xmlutils.c:48
static void xml_file_iterator_attribute_decl(void *ctx, const xmlChar *elem, const xmlChar *fullname, int type, int def, const xmlChar *defaultValue, xmlEnumerationPtr tree)
XML file iterator parser callback for when an attribute definition has been parsed.
Definition xmlutils.c:2615
element_t element_next(element_t element)
Get the next sibling of an element.
Definition xmlutils.c:2230
gchar * element_to_string(element_t element)
Output the XML element as a string.
Definition xmlutils.c:2253
element_t element_child(element_t element, const gchar *name)
Get a child of an element.
Definition xmlutils.c:2086
int parse_entity(const char *string, entity_t *entity)
Read an XML entity tree from a string.
Definition xmlutils.c:1511
#define XML_FILE_BUFFER_SIZE
Definition xmlutils.c:1931
int compare_entities(entity_t entity1, entity_t entity2)
Compare two XML entity.
Definition xmlutils.c:1765
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
static void foreach_print_attribute(gpointer name, gpointer value, gpointer stream)
Print an XML attribute for g_hash_table_foreach.
Definition xmlutils.c:1651
int read_entity_s(int socket, entity_t *entity)
Read an XML entity tree from the socket.
Definition xmlutils.c:1483
static void xml_file_iterator_end_document(void *ctx)
XML file iterator parser callback at the document end.
Definition xmlutils.c:2732
void print_entity(FILE *stream, entity_t entity)
Print an XML entity.
Definition xmlutils.c:1663
static void xml_file_iterator_element_decl(void *ctx, const xmlChar *name, int type, xmlElementContentPtr content)
XML file iterator parser callback for when an element definition has been parsed.
Definition xmlutils.c:2640
static void 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
static void add_attributes(entity_t entity, const gchar **names, const gchar **values)
Add attributes from an XML callback to an entity.
Definition xmlutils.c:234
#define XML_FILE_ITERATOR_BUFFER_SIZE
File read buffer size for an XML file iterator.
Definition xmlutils.c:2990
static void foreach_print_attribute_to_string(gpointer name, gpointer value, gpointer string)
Print an XML attribute for g_hash_table_foreach to a GString.
Definition xmlutils.c:1598
void xml_handle_text(context_data_t *context, const gchar *text, gsize text_len)
Handle additional text of an XML element.
Definition xmlutils.c:464
static void xml_file_iterator_notation_decl(void *ctx, const xmlChar *name, const xmlChar *publicId, const xmlChar *systemId)
XML file iterator parser callback for when a notation definition has been parsed.
Definition xmlutils.c:2660
static int xml_file_iterator_has_internal_subset(void *ctx)
XML file iterator parser callback for checking if doc has an internal subset.
Definition xmlutils.c:2494
void xml_string_append(GString *xml, const char *format,...)
Append formatted escaped XML to a string.
Definition xmlutils.c:1849
void print_entity_to_string(entity_t entity, GString *string)
Print an XML entity tree to a GString, appending it if string is not.
Definition xmlutils.c:1616
Headers for simple XML reader.
struct entity_s * entity_t
Definition xmlutils.h:58
struct xml_file_iterator_struct * xml_file_iterator_t
Definition xmlutils.h:188
struct _xmlNode * element_t
Definition xmlutils.h:157
GSList * entities_t
Entities.
Definition xmlutils.h:46