35 #include <glib/gtypes.h>
36 #include <libxml/parser.h>
37 #include <libxml/tree.h>
46 #define G_LOG_DOMAIN "lib xml"
51 #define BUFFER_SIZE 1048576
65 entity = g_malloc (
sizeof (*entity));
66 entity->
name = g_strdup (name ? name :
"");
67 entity->
text = g_strdup (text ? text :
"");
119 *entities = g_slist_append (*entities, entity);
133 g_free (entity->
name);
134 g_free (entity->
text);
216 return match ? (
entity_t) match->data : NULL;
236 return (
const char *) g_hash_table_lookup (entity->
attributes, name);
250 if (names && values && *names && *values)
254 g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
255 while (*names && *values)
258 g_hash_table_insert (entity->
attributes, g_strdup (*names),
278 const gchar **attribute_names,
279 const gchar **attribute_values, gpointer user_data,
286 (void) attribute_names;
287 (void) attribute_values;
290 data->
current = GINT_TO_POINTER (GPOINTER_TO_INT (data->
current) + 1);
305 const gchar **attribute_names,
306 const gchar **attribute_values, gpointer user_data,
325 if (data->
first == NULL)
326 data->
current = data->
first = g_slist_prepend (NULL, entity);
341 const gchar **attribute_names,
342 const gchar **attribute_values)
345 attribute_values, context, NULL);
358 gpointer user_data, GError **error)
366 data->
current = GINT_TO_POINTER (GPOINTER_TO_INT (data->
current) - 1);
381 gpointer user_data, GError **error)
405 g_slist_free_1 (front);
431 ignore_text (GMarkupParseContext *context,
const gchar *text, gsize text_len,
432 gpointer user_data, GError **error)
451 handle_text (GMarkupParseContext *context,
const gchar *text, gsize text_len,
452 gpointer user_data, GError **error)
462 gchar *old = current->
text;
463 current->
text = g_strconcat (current->
text, text, NULL);
467 current->
text = g_strdup (text);
491 handle_error (GMarkupParseContext *context, GError *error, gpointer user_data)
495 g_message (
" Error: %s\n", error->message);
515 entity_t *entity, GString **string_return)
517 GMarkupParser xml_parser;
518 GError *error = NULL;
519 GMarkupParseContext *xml_context;
529 if (time (&last_time) == -1)
531 g_warning (
" failed to get current time: %s\n", strerror (errno));
539 socket = GPOINTER_TO_INT (gnutls_transport_get_ptr (*session));
540 if (fcntl (socket, F_SETFL, O_NONBLOCK) == -1)
551 if (string_return == NULL)
553 else if (*string_return == NULL)
554 string = g_string_new (
"");
556 string = *string_return;
572 xml_parser.passthrough = NULL;
576 context_data.
done = FALSE;
577 context_data.
first = NULL;
583 g_markup_parse_context_new (&xml_parser, 0, &context_data, NULL);
593 count = gnutls_record_recv (*session, buffer,
BUFFER_SIZE);
596 if (count == GNUTLS_E_INTERRUPTED)
599 if ((timeout > 0) && (count == GNUTLS_E_AGAIN))
602 if ((timeout - (time (NULL) - last_time)) <= 0)
604 g_warning (
" timeout\n");
605 if (fcntl (socket, F_SETFL, 0L) < 0)
606 g_warning (
"%s :failed to set socket flag: %s",
607 __FUNCTION__, strerror (errno));
608 g_markup_parse_context_free (xml_context);
614 if (count == GNUTLS_E_REHANDSHAKE)
617 if (context_data.
first && context_data.
first->data)
620 g_slist_free_1 (context_data.
first);
622 if (
string && *string_return == NULL)
623 g_string_free (
string, TRUE);
626 if (fcntl (socket, F_SETFL, 0L) < 0)
627 g_warning (
"%s :failed to set socket flag: %s",
628 __FUNCTION__, strerror (errno));
630 g_markup_parse_context_free (xml_context);
637 g_markup_parse_context_end_parse (xml_context, &error);
640 g_warning (
" End error: %s\n", error->message);
641 g_error_free (error);
643 if (context_data.
first && context_data.
first->data)
646 g_slist_free_1 (context_data.
first);
648 if (
string && *string_return == NULL)
649 g_string_free (
string, TRUE);
652 if (fcntl (socket, F_SETFL, 0L) < 0)
653 g_warning (
"%s :failed to set socket flag: %s",
654 __FUNCTION__, strerror (errno));
656 g_markup_parse_context_free (xml_context);
663 g_debug (
"<= %.*s\n", (
int) count, buffer);
666 g_string_append_len (
string, buffer, count);
668 g_markup_parse_context_parse (xml_context, buffer, count, &error);
671 g_error_free (error);
672 if (context_data.
first && context_data.
first->data)
675 g_slist_free_1 (context_data.
first);
677 if (
string && *string_return == NULL)
678 g_string_free (
string, TRUE);
681 if (fcntl (socket, F_SETFL, 0L) < 0)
682 g_warning (
"%s :failed to set socket flag: %s", __FUNCTION__,
685 g_markup_parse_context_free (xml_context);
689 if (context_data.
done)
691 g_markup_parse_context_end_parse (xml_context, &error);
694 g_warning (
" End error: %s\n", error->message);
695 g_error_free (error);
696 if (context_data.
first && context_data.
first->data)
699 g_slist_free_1 (context_data.
first);
702 fcntl (socket, F_SETFL, 0L);
703 g_markup_parse_context_free (xml_context);
710 *string_return = string;
712 fcntl (socket, F_SETFL, 0L);
713 g_markup_parse_context_free (xml_context);
718 if ((timeout > 0) && (time (&last_time) == -1))
720 g_warning (
" failed to get current time (1): %s\n",
722 if (fcntl (socket, F_SETFL, 0L) < 0)
723 g_warning (
"%s :failed to set socket flag: %s", __FUNCTION__,
725 g_markup_parse_context_free (xml_context);
749 GString **string_return)
751 GMarkupParser xml_parser;
752 GError *error = NULL;
753 GMarkupParseContext *xml_context;
761 if (time (&last_time) == -1)
763 g_warning (
" failed to get current time: %s\n", strerror (errno));
771 if (fcntl (socket, F_SETFL, O_NONBLOCK) == -1)
779 if (string_return == NULL)
781 else if (*string_return == NULL)
782 string = g_string_new (
"");
784 string = *string_return;
800 xml_parser.passthrough = NULL;
804 context_data.
done = FALSE;
805 context_data.
first = NULL;
811 g_markup_parse_context_new (&xml_parser, 0, &context_data, NULL);
832 if ((timeout - (time (NULL) - last_time)) <= 0)
834 g_warning (
" timeout\n");
835 if (fcntl (socket, F_SETFL, 0L) < 0)
836 g_warning (
"%s :failed to set socket flag: %s",
837 __FUNCTION__, strerror (errno));
838 g_markup_parse_context_free (xml_context);
840 if (
string && *string_return == NULL)
841 g_string_free (
string, TRUE);
847 if (context_data.
first && context_data.
first->data)
850 g_slist_free_1 (context_data.
first);
852 if (
string && *string_return == NULL)
853 g_string_free (
string, TRUE);
855 fcntl (socket, F_SETFL, 0L);
856 g_markup_parse_context_free (xml_context);
863 g_markup_parse_context_end_parse (xml_context, &error);
866 g_warning (
" End error: %s\n", error->message);
867 g_error_free (error);
869 if (context_data.
first && context_data.
first->data)
872 g_slist_free_1 (context_data.
first);
874 if (
string && *string_return == NULL)
875 g_string_free (
string, TRUE);
878 if (fcntl (socket, F_SETFL, 0L) < 0)
879 g_warning (
"%s :failed to set socket flag: %s",
880 __FUNCTION__, strerror (errno));
882 g_markup_parse_context_free (xml_context);
889 g_debug (
"<= %.*s\n", (
int) count, buffer);
892 g_string_append_len (
string, buffer, count);
894 g_markup_parse_context_parse (xml_context, buffer, count, &error);
897 g_error_free (error);
899 if (context_data.
first && context_data.
first->data)
902 g_slist_free_1 (context_data.
first);
904 if (
string && *string_return == NULL)
905 g_string_free (
string, TRUE);
908 if (fcntl (socket, F_SETFL, 0L) < 0)
909 g_warning (
"%s :failed to set socket flag: %s", __FUNCTION__,
912 g_markup_parse_context_free (xml_context);
916 if (context_data.
done)
918 g_markup_parse_context_end_parse (xml_context, &error);
921 g_warning (
" End error: %s\n", error->message);
922 g_error_free (error);
923 if (context_data.
first && context_data.
first->data)
926 g_slist_free_1 (context_data.
first);
929 fcntl (socket, F_SETFL, 0L);
930 g_markup_parse_context_free (xml_context);
932 if (
string && *string_return == NULL)
933 g_string_free (
string, TRUE);
939 *string_return = string;
941 fcntl (socket, F_SETFL, 0L);
942 g_slist_free (context_data.
first);
943 g_markup_parse_context_free (xml_context);
948 if ((timeout > 0) && (time (&last_time) == -1))
950 g_warning (
" failed to get current time (1): %s\n",
952 if (fcntl (socket, F_SETFL, 0L) < 0)
953 g_warning (
"%s :failed to set server socket flag: %s", __FUNCTION__,
955 g_markup_parse_context_free (xml_context);
957 if (
string && *string_return == NULL)
958 g_string_free (
string, TRUE);
979 GString **string_return)
999 GString **string_return)
1001 if (connection->
tls)
1025 GString *
string = NULL;
1030 g_string_free (
string, TRUE);
1033 *text = g_string_free (
string, FALSE);
1057 GString *
string = NULL;
1062 g_string_free (
string, TRUE);
1065 *text = g_string_free (
string, FALSE);
1134 if (connection->
tls)
1193 GMarkupParser xml_parser;
1194 GError *error = NULL;
1195 GMarkupParseContext *xml_context;
1203 xml_parser.passthrough = NULL;
1206 context_data.
done = FALSE;
1207 context_data.
first = NULL;
1213 g_markup_parse_context_new (&xml_parser, 0, &context_data, NULL);
1217 g_markup_parse_context_parse (xml_context,
string, strlen (
string), &error);
1220 g_error_free (error);
1221 if (context_data.
first && context_data.
first->data)
1224 g_slist_free_1 (context_data.
first);
1228 if (context_data.
done)
1230 g_markup_parse_context_end_parse (xml_context, &error);
1233 g_warning (
" End error: %s\n", error->message);
1234 g_error_free (error);
1235 if (context_data.
first && context_data.
first->data)
1238 g_slist_free_1 (context_data.
first);
1243 g_slist_free_1 (context_data.
first);
1246 if (context_data.
first && context_data.
first->data)
1249 g_slist_free_1 (context_data.
first);
1277 gchar *text_escaped;
1278 text_escaped = g_markup_escape_text ((gchar *) value, -1);
1279 g_string_append_printf ((GString *)
string,
" %s=\"%s\"", (
char *) name,
1281 g_free (text_escaped);
1294 gchar *text_escaped = NULL;
1295 g_string_append_printf (
string,
"<%s", entity->
name);
1299 g_string_append_printf (
string,
">");
1300 text_escaped = g_markup_escape_text (entity->
text, -1);
1301 g_string_append_printf (
string,
"%s", text_escaped);
1302 g_free (text_escaped);
1304 g_string_append_printf (
string,
"</%s>", entity->
name);
1329 fprintf ((FILE *) stream,
" %s=\"%s\"", (
char *) name, (
char *) value);
1341 gchar *text_escaped = NULL;
1342 fprintf (stream,
"<%s", entity->
name);
1345 fprintf (stream,
">");
1346 text_escaped = g_markup_escape_text (entity->
text, -1);
1347 fprintf (stream,
"%s", text_escaped);
1348 g_free (text_escaped);
1350 fprintf (stream,
"</%s>", entity->
name);
1367 printf (
" %s=\"%s\"", (
char *) name, (
char *) value);
1385 int indentation = GPOINTER_TO_INT (indent);
1386 gchar *text_escaped = NULL;
1388 for (i = 0; i < indentation; i++)
1391 printf (
"<%s", entity->
name);
1397 text_escaped = g_markup_escape_text (entity->
text, -1);
1398 printf (
"%s", text_escaped);
1399 g_free (text_escaped);
1405 GINT_TO_POINTER (indentation + 1));
1406 for (i = 0; i < indentation; i++)
1410 printf (
"</%s>\n", entity->
name);
1425 gchar *value2 = g_hash_table_lookup (attributes2, key);
1426 if (value2 && strcmp (value, value2) == 0)
1428 g_debug (
" compare failed attribute: %s\n", (
char *) value);
1443 if (entity1 == NULL)
1444 return entity2 == NULL ? 0 : 1;
1445 if (entity2 == NULL)
1448 if (strcmp (entity1->
name, entity2->
name))
1450 g_debug (
" compare failed name: %s vs %s\n", entity1->
name,
1454 if (strcmp (entity1->
text, entity2->
text))
1456 g_debug (
" compare failed text %s vs %s (%s)\n", entity1->
text,
1473 g_debug (
" compare failed attributes\n");
1481 while (list1 && list2)
1485 g_debug (
" compare failed subentity\n");
1488 list1 = g_slist_next (list1);
1489 list2 = g_slist_next (list2);
1494 g_debug (
" compare failed number of entities (%s)\n", entity1->
name);
1532 va_start (args, format);
1533 piece = g_markup_vprintf_escaped (format, args);
1535 g_string_append (xml, piece);
1554 const gchar **attribute_names,
1555 const gchar **attribute_values, gpointer data,
1564 && search_data->
found == 0)
1566 g_debug (
"%s: Found element <%s>", __FUNCTION__,
element_name);
1572 GHashTable *found_attributes;
1574 g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL);
1576 while (attribute_names[index])
1578 gchar *searched_value;
1579 searched_value = g_hash_table_lookup (
1582 && strcmp (searched_value, attribute_values[index]) == 0)
1584 g_debug (
"%s: Found attribute %s=\"%s\"", __FUNCTION__,
1585 attribute_names[index], searched_value);
1586 g_hash_table_add (found_attributes, searched_value);
1590 g_debug (
"%s: Found %d of %d attributes", __FUNCTION__,
1591 g_hash_table_size (found_attributes),
1594 if (g_hash_table_size (found_attributes)
1597 search_data->
found = 1;
1600 g_hash_table_destroy (found_attributes);
1604 search_data->
found = 1;
1609 #define XML_FILE_BUFFER_SIZE 1048576
1621 GHashTable *find_attributes)
1626 GMarkupParser xml_parser;
1627 GMarkupParseContext *xml_context;
1629 GError *error = NULL;
1633 search_data.
found = 0;
1637 xml_parser.end_element = NULL;
1638 xml_parser.text = NULL;
1639 xml_parser.passthrough = NULL;
1640 xml_parser.error = NULL;
1641 xml_context = g_markup_parse_context_new (&xml_parser, 0, &search_data, NULL);
1643 file = fopen (file_path,
"r");
1646 g_markup_parse_context_free (xml_context);
1647 g_warning (
"%s: Failed to open '%s':", __FUNCTION__, strerror (errno));
1652 && g_markup_parse_context_parse (xml_context, buffer, read_len, &error)
1656 g_markup_parse_context_end_parse (xml_context, &error);
1660 g_markup_parse_context_free (xml_context);
1661 return search_data.
found;
1663 #undef XML_FILE_BUFFER_SIZE
1691 if (xmlMemSetup (g_free, g_malloc, g_realloc, g_strdup))
1694 doc = xmlReadMemory (
string, strlen (
string),
"noname.xml", NULL, 0);
1699 *element = xmlDocGetRootElement (doc);
1717 assert (element->doc);
1718 xmlFreeDoc (element->doc);
1733 && (element->type == XML_ELEMENT_NODE))
1734 return (
const gchar *) element->name;
1750 for (xmlNode *node = element->children; node; node = node->next)
1751 if (xmlStrcmp (node->name, (
const xmlChar *) name) == 0)
1767 const gchar *stripped_name;
1772 stripped_name = strchr (name,
':');
1784 if (*stripped_name ==
'\0')
1823 string = (gchar *) xmlNodeListGetString (element->doc, element->xmlChildrenNode, 1);
1826 string = xmlMalloc (1);
1842 const gchar *stripped_name;
1847 stripped_name = strchr (name,
':');
1859 if (*stripped_name ==
'\0')
1863 return (gchar *) xmlGetProp (element, (
const xmlChar *) name);
1865 attribute = (gchar *) xmlGetProp (element, (
const xmlChar *) stripped_name);
1876 return (gchar *) xmlGetProp (element, (
const xmlChar *) name);
1890 return element->children;
1905 return element->next;