aboutsummaryrefslogtreecommitdiffstats
path: root/epan/strutil.c
diff options
context:
space:
mode:
authorOlivier Biot <obiot.ethereal@gmail.com>2004-05-01 20:46:24 +0000
committerOlivier Biot <obiot.ethereal@gmail.com>2004-05-01 20:46:24 +0000
commit2650b496796d161c0c158dc83e49dcfeea30d74a (patch)
tree54b6828c286d2eba29f0b424c40f23535b65c5da /epan/strutil.c
parent8d29376f42afaa7175b5f8bf009c6d885e80e3d6 (diff)
Add an XML escaping routine: xml_escape()
svn path=/trunk/; revision=10759
Diffstat (limited to 'epan/strutil.c')
-rw-r--r--epan/strutil.c41
1 files changed, 40 insertions, 1 deletions
diff --git a/epan/strutil.c b/epan/strutil.c
index 31c288aae0..bbf5f1b941 100644
--- a/epan/strutil.c
+++ b/epan/strutil.c
@@ -1,7 +1,7 @@
/* strutil.c
* String utility routines
*
- * $Id: strutil.c,v 1.18 2004/02/05 09:42:26 guy Exp $
+ * $Id: strutil.c,v 1.19 2004/05/01 20:46:24 obiot Exp $
*
* Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@ethereal.com>
@@ -397,6 +397,45 @@ hex_str_to_bytes(const char *hex_str, GByteArray *bytes) {
}
+/* Return a XML escaped representation of the unescaped string.
+ * The returned string must be freed when no longer in use. */
+gchar *
+xml_escape(const gchar *unescaped)
+{
+ GString *buffer = g_string_sized_new(128);
+ const gchar *p;
+ gchar c;
+
+ p = unescaped;
+ while ( (c = *p++) ) {
+ switch (c) {
+ case '<':
+ g_string_append(buffer, "&lt;");
+ break;
+ case '>':
+ g_string_append(buffer, "&gt;");
+ break;
+ case '&':
+ g_string_append(buffer, "&amp;");
+ break;
+ case '\'':
+ g_string_append(buffer, "&apos;");
+ break;
+ case '"':
+ g_string_append(buffer, "&quot;");
+ break;
+ default:
+ g_string_append_c(buffer, c);
+ break;
+ }
+ }
+ /* Return the string value contained within the GString
+ * after getting rid of the GString structure.
+ * This is the way to do this, see the GLib reference. */
+ return g_string_free(buffer, FALSE);
+}
+
+
/* Return the first occurrence of needle in haystack.
* If not found, return NULL.
* If either haystack or needle has 0 length, return NULL.