Add an XML escaping routine: xml_escape()

svn path=/trunk/; revision=10759
This commit is contained in:
Olivier Biot 2004-05-01 20:46:24 +00:00
parent 8d29376f42
commit 2650b49679
2 changed files with 42 additions and 2 deletions

View File

@ -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.

View File

@ -1,7 +1,7 @@
/* strutil.h
* String utility definitions
*
* $Id: strutil.h,v 1.14 2004/01/25 16:58:25 jmayer Exp $
* $Id: strutil.h,v 1.15 2004/05/01 20:46:24 obiot Exp $
*
* Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@ethereal.com>
@ -35,6 +35,7 @@ gchar* format_text(const guchar *line, int len);
gchar* bytes_to_str(const guint8 *, int);
gchar* bytes_to_str_punct(const guint8 *, int, gchar punct);
gboolean hex_str_to_bytes(const char *hex_str, GByteArray *bytes);
gchar* xml_escape(const gchar *unescaped);
const guint8 * epan_memmem(const guint8 *haystack, guint haystack_len,
const guint8 *needle, guint needle_len);