From 8109ba78d80021e60075066aa20d81905f6dec70 Mon Sep 17 00:00:00 2001 From: Jakub Adam Date: Sat, 21 Jul 2012 19:34:02 +0200 Subject: [PATCH] webticket: move extract_raw_xml() to sipe-xml.c --- src/core/sipe-webticket.c | 34 ++++------------------------------ src/core/sipe-xml.c | 25 +++++++++++++++++++++++++ src/core/sipe-xml.h | 13 +++++++++++++ 3 files changed, 42 insertions(+), 30 deletions(-) diff --git a/src/core/sipe-webticket.c b/src/core/sipe-webticket.c index 4e157478..c74672da 100644 --- a/src/core/sipe-webticket.c +++ b/src/core/sipe-webticket.c @@ -94,36 +94,10 @@ static gchar *extract_raw_xml_attribute(const gchar *xml, return(data); } -static gchar *extract_raw_xml(const gchar *xml, - const gchar *tag, - gboolean include_tag) -{ - gchar *tag_start = g_strdup_printf("<%s", tag); - gchar *tag_end = g_strdup_printf("", tag); - gchar *data = NULL; - const gchar *start = strstr(xml, tag_start); - - if (start) { - const gchar *end = strstr(start + strlen(tag_start), tag_end); - if (end) { - if (include_tag) { - data = g_strndup(start, end + strlen(tag_end) - start); - } else { - const gchar *tmp = strchr(start + strlen(tag_start), '>') + 1; - data = g_strndup(tmp, end - tmp); - } - } - } - - g_free(tag_end); - g_free(tag_start); - return(data); -} - static gchar *generate_timestamp(const gchar *raw, const gchar *lifetime_tag) { - gchar *lifetime = extract_raw_xml(raw, lifetime_tag, FALSE); + gchar *lifetime = sipe_xml_extract_raw(raw, lifetime_tag, FALSE); gchar *timestamp = NULL; if (lifetime) timestamp = g_strdup_printf("%s", @@ -135,7 +109,7 @@ static gchar *generate_timestamp(const gchar *raw, static gchar *generate_fedbearer_wsse(const gchar *raw) { gchar *timestamp = generate_timestamp(raw, "wst:Lifetime"); - gchar *keydata = extract_raw_xml(raw, "EncryptedData", TRUE); + gchar *keydata = sipe_xml_extract_raw(raw, "EncryptedData", TRUE); gchar *wsse_security = NULL; if (timestamp && keydata) { @@ -152,7 +126,7 @@ static gchar *generate_sha1_proof_wsse(const gchar *raw, struct sipe_tls_random *entropy) { gchar *timestamp = generate_timestamp(raw, "Lifetime"); - gchar *keydata = extract_raw_xml(raw, "saml:Assertion", TRUE); + gchar *keydata = sipe_xml_extract_raw(raw, "saml:Assertion", TRUE); gchar *wsse_security = NULL; if (timestamp && keydata) { @@ -170,7 +144,7 @@ static gchar *generate_sha1_proof_wsse(const gchar *raw, * * key = P_SHA1(Entropy_REQ, Entropy_RES)" */ - gchar *entropy_res_base64 = extract_raw_xml(raw, "BinarySecret", FALSE); + gchar *entropy_res_base64 = sipe_xml_extract_raw(raw, "BinarySecret", FALSE); gsize entropy_res_length; guchar *entropy_response = g_base64_decode(entropy_res_base64, &entropy_res_length); diff --git a/src/core/sipe-xml.c b/src/core/sipe-xml.c index 32fb31e7..1e913430 100644 --- a/src/core/sipe-xml.c +++ b/src/core/sipe-xml.c @@ -431,6 +431,31 @@ gchar *sipe_xml_exc_c14n(const gchar *string) return(canon); } +gchar *sipe_xml_extract_raw(const gchar *xml, const gchar *tag, + gboolean include_tag) +{ + gchar *tag_start = g_strdup_printf("<%s", tag); + gchar *tag_end = g_strdup_printf("", tag); + gchar *data = NULL; + const gchar *start = strstr(xml, tag_start); + + if (start) { + const gchar *end = strstr(start + strlen(tag_start), tag_end); + if (end) { + if (include_tag) { + data = g_strndup(start, end + strlen(tag_end) - start); + } else { + const gchar *tmp = strchr(start + strlen(tag_start), '>') + 1; + data = g_strndup(tmp, end - tmp); + } + } + } + + g_free(tag_end); + g_free(tag_start); + return data; +} + /* Local Variables: mode: c diff --git a/src/core/sipe-xml.h b/src/core/sipe-xml.h index 00fbee01..3023f68e 100644 --- a/src/core/sipe-xml.h +++ b/src/core/sipe-xml.h @@ -128,3 +128,16 @@ void sipe_xml_dump(const sipe_xml *node, const gchar *path); * @return canonicalized XML string. Must be @c g_free()'d. */ gchar *sipe_xml_exc_c14n(const gchar *string); + +/** + * Extracts raw data between a pair of XML tags. + * + * @param xml XML document + * @param tag XML tag enclosing the data + * @param include_tag whether the enclosing tags should be included in the result + * + * @return a first substring from the XML document enclosed by @c tag. + * Must be @c g_free()'d. + */ +gchar *sipe_xml_extract_raw(const gchar *xml, const gchar *tag, + gboolean include_tag); -- 2.11.4.GIT