diff options
author | Mikhail Zabaluev <mikhail.zabaluev@nokia.com> | 2009-11-05 18:43:10 +0200 |
---|---|---|
committer | Mikhail Zabaluev <mikhail.zabaluev@nokia.com> | 2009-11-05 18:43:10 +0200 |
commit | 1e4ed012b881b7acfc8d174fa6ae5a122b31408d (patch) | |
tree | fc06a10bac6eecdf00fff02b36189c4b22364352 /tpsip | |
parent | 1e6b61bf66e303a2b7c4cb414f9d580013775fcd (diff) |
Added tpsip_unquote_string()
Diffstat (limited to 'tpsip')
-rw-r--r-- | tpsip/util.c | 47 | ||||
-rw-r--r-- | tpsip/util.h | 2 |
2 files changed, 47 insertions, 2 deletions
diff --git a/tpsip/util.c b/tpsip/util.c index ae47096..c465651 100644 --- a/tpsip/util.c +++ b/tpsip/util.c @@ -86,8 +86,8 @@ tpsip_string_append_quoted (GString *buf, const gchar *text) * Formats the content of @text as a quoted string accordingly to SIP * or MIME syntax. * - * Returns: a newly allocated quoted string. The string is to be freed with - * g_free(). + * Returns: a newly allocated quoted string. + * The string is to be freed with g_free(). */ gchar * tpsip_quote_string (const gchar *src) @@ -100,3 +100,46 @@ tpsip_quote_string (const gchar *src) return g_string_free (buf, FALSE); } + +/** + * tpsip_unquote_string: + * @src: the quoted string, including the encompassing double quotes + * @len: length of the string @src in bytes, + * or -1 if the string is null-terminated + * + * Extracts text out of a quoted string literal accordingly to SIP + * or MIME syntax, unescaping characters preceded by backspaces. + * + * Returns: a newly allocated unquoted string. + * The string is to be freed with g_free(). + */ +gchar * +tpsip_unquote_string (const gchar *src, gsize len) +{ + gchar *res; + gchar *p; + gsize i; + + g_return_val_if_fail (src != NULL, NULL); + + if (len < 0) + len = strlen (src); + + g_return_val_if_fail (len >= 2, NULL); + g_return_val_if_fail (src[0] == '"' && src[len - 1] == '"', NULL); + + res = g_malloc (len - 2 + 1); + + p = res; + + for (i = 1; i < len - 1; ++i) + { + if (src[i] == '\\') + ++i; + *p++ = src[i]; + } + + *p = '\0'; + + return res; +} diff --git a/tpsip/util.h b/tpsip/util.h index 21e00a4..df52f2d 100644 --- a/tpsip/util.h +++ b/tpsip/util.h @@ -27,6 +27,8 @@ G_BEGIN_DECLS gchar * tpsip_quote_string (const gchar *src); +gchar * tpsip_unquote_string (const gchar *src, gsize len); + void tpsip_string_append_quoted (GString *buf, const gchar *text); G_END_DECLS |