tls: refactor tls_server_hello()
[siplcs.git] / src / core / sipe-xml.c
blob79ec677fa9a3313d6b77d632d836be7a2bceb218
1 /**
2 * @file sipe-xml.c
4 * pidgin-sipe
6 * Copyright (C) 2010-11 SIPE Project <http://sipe.sourceforge.net/>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * This code is loosely based on libpurple xmlnode.c
27 #include <stdarg.h>
28 #include <string.h>
29 #include <time.h>
31 #include "libxml/parser.h"
32 #include "libxml/c14n.h"
33 #include "libxml/xmlversion.h"
35 #include "glib.h"
37 #include "sipe-backend.h"
38 #include "sipe-utils.h"
39 #include "sipe-xml.h"
41 struct _sipe_xml {
42 gchar *name;
43 sipe_xml *parent;
44 sipe_xml *sibling;
45 sipe_xml *first;
46 sipe_xml *last;
47 GString *data;
48 GHashTable *attributes;
51 struct _parser_data {
52 sipe_xml *root;
53 sipe_xml *current;
54 gboolean error;
57 static void callback_start_element(void *user_data, const xmlChar *name, const xmlChar **attrs)
59 struct _parser_data *pd = user_data;
60 const char *tmp;
61 sipe_xml *node;
63 if (!name || pd->error) return;
65 node = g_new0(sipe_xml, 1);
67 if ((tmp = strchr((char *)name, ':')) != NULL) {
68 name = (xmlChar *)tmp + 1;
70 node->name = g_strdup((gchar *)name);
72 if (!pd->root) {
73 pd->root = node;
74 } else {
75 sipe_xml *current = pd->current;
77 node->parent = current;
78 if (current->last) {
79 current->last->sibling = node;
80 } else {
81 current->first = node;
83 current->last = node;
86 if (attrs) {
87 const xmlChar *key;
89 node->attributes = g_hash_table_new_full(g_str_hash,
90 (GEqualFunc) sipe_strcase_equal,
91 g_free, g_free);
92 while ((key = *attrs++) != NULL) {
93 if ((tmp = strchr((char *)key, ':')) != NULL) {
94 key = (xmlChar *)tmp + 1;
96 /* libxml2 decodes all entities except &amp;.
97 &amp; is replaced by the equivalent &#38; */
98 g_hash_table_insert(node->attributes,
99 g_strdup((gchar *) key),
100 replace((gchar *) *attrs++, "&#38;", "&"));
104 pd->current = node;
107 static void callback_end_element(void *user_data, const xmlChar *name)
109 struct _parser_data *pd = user_data;
111 if (!name || !pd->current || pd->error) return;
113 if (pd->current->parent)
114 pd->current = pd->current->parent;
117 static void callback_characters(void *user_data, const xmlChar *text, int text_len)
119 struct _parser_data *pd = user_data;
120 sipe_xml *node;
122 if (!pd->current || pd->error || !text || !text_len) return;
124 node = pd->current;
125 if (node->data)
126 node->data = g_string_append_len(node->data, (gchar *)text, text_len);
127 else
128 node->data = g_string_new_len((gchar *)text, text_len);
131 static void callback_error(void *user_data, const char *msg, ...)
133 struct _parser_data *pd = user_data;
134 gchar *errmsg;
135 va_list args;
137 pd->error = TRUE;
139 va_start(args, msg);
140 errmsg = g_strdup_vprintf(msg, args);
141 va_end(args);
143 SIPE_DEBUG_ERROR("error parsing xml string: %s", errmsg);
144 g_free(errmsg);
147 static void callback_serror(void *user_data, xmlErrorPtr error)
149 struct _parser_data *pd = user_data;
151 if (error && (error->level == XML_ERR_ERROR ||
152 error->level == XML_ERR_FATAL)) {
153 pd->error = TRUE;
154 SIPE_DEBUG_ERROR("XML parser error: Domain %i, code %i, level %i: %s",
155 error->domain, error->code, error->level,
156 error->message ? error->message : "(null)");
157 } else if (error) {
158 SIPE_DEBUG_WARNING("XML parser error: Domain %i, code %i, level %i: %s",
159 error->domain, error->code, error->level,
160 error->message ? error->message : "(null)");
161 } else {
162 /* *sigh* macro expects at least two parameters */
163 SIPE_DEBUG_WARNING_NOFORMAT("XML parser error");
167 /* API doesn't accept const data structure */
168 static xmlSAXHandler parser = {
169 NULL, /* internalSubset */
170 NULL, /* isStandalone */
171 NULL, /* hasInternalSubset */
172 NULL, /* hasExternalSubset */
173 NULL, /* resolveEntity */
174 NULL, /* getEntity */
175 NULL, /* entityDecl */
176 NULL, /* notationDecl */
177 NULL, /* attributeDecl */
178 NULL, /* elementDecl */
179 NULL, /* unparsedEntityDecl */
180 NULL, /* setDocumentLocator */
181 NULL, /* startDocument */
182 NULL, /* endDocument */
183 callback_start_element, /* startElement */
184 callback_end_element, /* endElement */
185 NULL, /* reference */
186 callback_characters, /* characters */
187 NULL, /* ignorableWhitespace */
188 NULL, /* processingInstruction */
189 NULL, /* comment */
190 NULL, /* warning */
191 callback_error, /* error */
192 NULL, /* fatalError */
193 NULL, /* getParameterEntity */
194 NULL, /* cdataBlock */
195 NULL, /* externalSubset */
196 XML_SAX2_MAGIC, /* initialized */
197 NULL, /* _private */
198 NULL, /* startElementNs */
199 NULL, /* endElementNs */
200 callback_serror, /* serror */
203 sipe_xml *sipe_xml_parse(const gchar *string, gsize length)
205 sipe_xml *result = NULL;
207 if (string && length) {
208 struct _parser_data *pd = g_new0(struct _parser_data, 1);
210 if (xmlSAXUserParseMemory(&parser, pd, string, length))
211 pd->error = TRUE;
213 if (pd->error) {
214 sipe_xml_free(pd->root);
215 } else {
216 result = pd->root;
219 g_free(pd);
222 return result;
225 void sipe_xml_free(sipe_xml *node)
227 sipe_xml *child;
229 if (!node) return;
231 /* we don't support partial tree deletion */
232 if (node->parent != NULL) {
233 SIPE_DEBUG_ERROR_NOFORMAT("sipe_xml_free: partial delete attempt! Expect crash or memory leaks...");
236 /* free children */
237 child = node->first;
238 while (child) {
239 sipe_xml *tmp = child->sibling;
240 child->parent = NULL; /* detach from tree, see above */
241 sipe_xml_free(child);
242 child = tmp;
245 /* free node */
246 g_free(node->name);
247 if (node->data) g_string_free(node->data, TRUE);
248 if (node->attributes) g_hash_table_destroy(node->attributes);
249 g_free(node);
252 static void sipe_xml_stringify_attribute(gpointer key, gpointer value,
253 gpointer user_data)
255 g_string_append_printf(user_data, " %s=\"%s\"",
256 (const gchar *) key, (const gchar *) value);
259 static void sipe_xml_stringify_node(GString *s, const sipe_xml *node)
261 g_string_append_printf(s, "<%s", node->name);
263 if (node->attributes) {
264 g_hash_table_foreach(node->attributes,
265 (GHFunc) sipe_xml_stringify_attribute,
269 if (node->data || node->first) {
270 const sipe_xml *child;
272 g_string_append_printf(s, ">%s",
273 node->data ? node->data->str : "");
275 for (child = node->first; child; child = child->sibling)
276 sipe_xml_stringify_node(s, child);
278 g_string_append_printf(s, "</%s>", node->name);
279 } else {
280 g_string_append(s, "/>");
284 gchar *sipe_xml_stringify(const sipe_xml *node)
286 GString *s;
288 if (!node) return NULL;
290 s = g_string_new("");
291 sipe_xml_stringify_node(s, node);
292 return g_string_free(s, FALSE);
295 const sipe_xml *sipe_xml_child(const sipe_xml *parent, const gchar *name)
297 gchar **names;
298 const sipe_xml *child = NULL;
300 if (!parent || !name) return NULL;
302 /* 0: child name */
303 /* 1: trailing path (optional) */
304 names = g_strsplit(name, "/", 2);
306 for (child = parent->first; child; child = child->sibling) {
307 if (sipe_strequal(names[0], child->name))
308 break;
311 /* recurse into path */
312 if (child && names[1])
313 child = sipe_xml_child(child, names[1]);
315 g_strfreev(names);
316 return child;
319 const sipe_xml *sipe_xml_twin(const sipe_xml *node)
321 sipe_xml *sibling;
323 if (!node) return NULL;
325 for (sibling = node->sibling; sibling; sibling = sibling->sibling) {
326 if (sipe_strequal(node->name, sibling->name))
327 return sibling;
329 return NULL;
332 const gchar *sipe_xml_name(const sipe_xml *node)
334 return(node ? node->name : NULL);
337 const gchar *sipe_xml_attribute(const sipe_xml *node, const gchar *attr)
339 if (!node || !attr || !node->attributes) return NULL;
340 return(g_hash_table_lookup(node->attributes, attr));
343 guint sipe_xml_int_attribute(const sipe_xml *node, const gchar *attr,
344 guint fallback)
346 const gchar *value = sipe_xml_attribute(node, attr);
347 return(value ? g_ascii_strtoll(value, NULL, 10) : fallback);
350 gchar *sipe_xml_data(const sipe_xml *node)
352 if (!node || !node->data || !node->data->str) return NULL;
353 return g_strdup(node->data->str);
357 * Set to 1 to enable debugging code and then add this line to your code:
359 * sipe_xml_dump(node, NULL);
361 #if 0
362 void sipe_xml_dump(const sipe_xml *node, const gchar *path)
364 const sipe_xml *child;
365 gchar *new_path;
366 if (!node) return;
367 new_path = g_strdup_printf("%s/%s", path ? path : "", node->name);
368 if (node->attributes) {
369 GList *attrs = g_hash_table_get_keys(node->attributes);
370 GString *buf = g_string_new("");
371 GList *entry = attrs;
372 while (entry) {
373 g_string_append_printf(buf, "%s ", (gchar *)entry->data);
374 entry = entry->next;
376 SIPE_DEBUG_INFO("%s [%s]", new_path, buf->str);
377 g_string_free(buf, TRUE);
378 g_list_free(attrs);
379 } else {
380 SIPE_DEBUG_INFO_NOFORMAT(new_path);
382 for (child = node->first; child; child = child->sibling)
383 sipe_xml_dump(child, new_path);
384 g_free(new_path);
386 #endif
389 * Other XML convenience functions not based on libpurple xmlnode.c
392 gchar *sipe_xml_exc_c14n(const gchar *string)
394 /* Parse string to XML document */
395 xmlDocPtr doc = xmlReadMemory(string, strlen(string), "", NULL, 0);
396 gchar *canon = NULL;
398 if (doc) {
399 xmlChar *buffer;
400 int size;
402 /* Apply canonicalization */
403 size = xmlC14NDocDumpMemory(doc,
404 NULL,
405 #if LIBXML_VERSION > 20703
406 /* new API: int mode (a xmlC14NMode) */
407 XML_C14N_EXCLUSIVE_1_0,
408 #else
409 /* old API: int exclusive */
411 #endif
412 NULL,
414 &buffer);
415 xmlFreeDoc(doc);
417 if (size >= 0) {
418 SIPE_DEBUG_INFO("sipe_xml_exc_c14n:\noriginal: %s\ncanonicalized: %s",
419 string, buffer);
420 canon = g_strndup((gchar *) buffer, size);
421 xmlFree(buffer);
422 } else {
423 SIPE_DEBUG_ERROR("sipe_xml_exc_c14n: failed to canonicalize xml string:\n%s",
424 string);
426 } else {
427 SIPE_DEBUG_ERROR("sipe_xml_exc_c14n: error parsing xml string:\n%s",
428 string);
431 return(canon);
435 Local Variables:
436 mode: c
437 c-file-style: "bsd"
438 indent-tabs-mode: t
439 tab-width: 8
440 End: