diff options
author | Jakub Zawadzki <darkjames-ws@darkjames.pl> | 2012-06-14 19:08:19 +0000 |
---|---|---|
committer | Jakub Zawadzki <darkjames-ws@darkjames.pl> | 2012-06-14 19:08:19 +0000 |
commit | 0711ac874fc909ffb6943c7ac1c186519f404f83 (patch) | |
tree | 149ddb451203c438bd92d4e7488ae3b1a0c15791 /epan/tvbuff.c | |
parent | 7931a2baf60a62d5e41f36008d232e3ccf48c951 (diff) | |
download | wireshark-0711ac874fc909ffb6943c7ac1c186519f404f83.tar.gz |
Optimize tvb_*_unicode functions.
- Allocate tmpbuf on stack. g_unichar_to_utf8[1] requires maximum 6B + 1 byte for NUL, align it to 8B.
- Use g_string_append_unichar()
[1] http://developer.gnome.org/glib/2.26/glib-Unicode-Manipulation.html#g-unichar-to-utf8
svn path=/trunk/; revision=43263
Diffstat (limited to 'epan/tvbuff.c')
-rw-r--r-- | epan/tvbuff.c | 45 |
1 files changed, 5 insertions, 40 deletions
diff --git a/epan/tvbuff.c b/epan/tvbuff.c index 25cf2a7c6c..29e9e81428 100644 --- a/epan/tvbuff.c +++ b/epan/tvbuff.c @@ -2340,10 +2340,8 @@ tvb_get_string(tvbuff_t *tvb, const gint offset, const gint length) gchar * tvb_get_unicode_string(tvbuff_t *tvb, const gint offset, gint length, const guint encoding) { - gchar *tmpbuf = NULL; gunichar2 uchar; gint i; /* Byte counter for tvbuff */ - gint tmpbuf_len; GString *strbuf = NULL; strbuf = g_string_new(NULL); @@ -2355,22 +2353,7 @@ tvb_get_unicode_string(tvbuff_t *tvb, const gint offset, gint length, const guin else uchar = tvb_get_letohs(tvb, offset + i); - /* Calculate how much space is needed to store UTF-16 character - * in UTF-8 */ - tmpbuf_len = g_unichar_to_utf8(uchar, NULL); - - tmpbuf = g_malloc(tmpbuf_len + 1); /* + 1 to make room for null - * terminator */ - - g_unichar_to_utf8(uchar, tmpbuf); - - /* NULL terminate the tmpbuf so g_string_append knows where - * to stop */ - tmpbuf[tmpbuf_len] = '\0'; - - g_string_append(strbuf, tmpbuf); - - g_free(tmpbuf); + g_string_append_unichar(strbuf, uchar); } return g_string_free(strbuf, FALSE); @@ -2490,7 +2473,7 @@ tvb_get_ephemeral_string(tvbuff_t *tvb, const gint offset, const gint length) gchar * tvb_get_ephemeral_unicode_string(tvbuff_t *tvb, const gint offset, gint length, const guint encoding) { - gchar *tmpbuf = NULL; + gchar tmpbuf[8]; gunichar2 uchar; gint i; /* Byte counter for tvbuff */ gint tmpbuf_len; @@ -2505,22 +2488,13 @@ tvb_get_ephemeral_unicode_string(tvbuff_t *tvb, const gint offset, gint length, else uchar = tvb_get_letohs(tvb, offset + i); - /* Calculate how much space is needed to store UTF-16 character - * in UTF-8 */ - tmpbuf_len = g_unichar_to_utf8(uchar, NULL); - - tmpbuf = g_malloc(tmpbuf_len + 1); /* + 1 to make room for null - * terminator */ - - g_unichar_to_utf8(uchar, tmpbuf); + tmpbuf_len = g_unichar_to_utf8(uchar, tmpbuf); /* NULL terminate the tmpbuf so ep_strbuf_append knows where * to stop */ tmpbuf[tmpbuf_len] = '\0'; ep_strbuf_append(strbuf, tmpbuf); - - g_free(tmpbuf); } return strbuf->str; @@ -2722,7 +2696,7 @@ tvb_get_ephemeral_stringz(tvbuff_t *tvb, const gint offset, gint *lengthp) gchar * tvb_get_ephemeral_unicode_stringz(tvbuff_t *tvb, const gint offset, gint *lengthp, const guint encoding) { - gchar *tmpbuf = NULL; + gchar tmpbuf[8]; gunichar2 uchar; gint size; /* Number of UTF-16 characters */ gint i; /* Byte counter for tvbuff */ @@ -2740,22 +2714,13 @@ tvb_get_ephemeral_unicode_stringz(tvbuff_t *tvb, const gint offset, gint *length else uchar = tvb_get_letohs(tvb, offset + i); - /* Calculate how much space is needed to store UTF-16 character - * in UTF-8 */ - tmpbuf_len = g_unichar_to_utf8(uchar, NULL); - - tmpbuf = g_malloc(tmpbuf_len + 1); /* + 1 to make room for null - * terminator */ - - g_unichar_to_utf8(uchar, tmpbuf); + tmpbuf_len = g_unichar_to_utf8(uchar, tmpbuf); /* NULL terminate the tmpbuf so ep_strbuf_append knows where * to stop */ tmpbuf[tmpbuf_len] = '\0'; ep_strbuf_append(strbuf, tmpbuf); - - g_free(tmpbuf); } if (lengthp) |