From 2d0320e87db503b02be47f13fdc3d33e01b73032 Mon Sep 17 00:00:00 2001
From: Anders Broman <anders.broman@ericsson.com>
Date: Wed, 26 Dec 2012 09:15:16 +0000
Subject: [PATCH 10/16] Copy over:Revision 46705, Revision 43266, Revision
 43263

svn path=/trunk-1.8/; revision=46760
---
 epan/tvbuff.c |   49 ++++++++-----------------------------------------
 1 file changed, 8 insertions(+), 41 deletions(-)

diff --git a/epan/tvbuff.c b/epan/tvbuff.c
index 35620e2..d73e533 100644
--- a/epan/tvbuff.c
+++ b/epan/tvbuff.c
@@ -32,7 +32,7 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
 #ifdef HAVE_CONFIG_H
@@ -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;
 
 	tvb_ensure_bytes_exist(tvb, offset, length);
@@ -2357,22 +2355,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);
@@ -2492,7 +2475,8 @@ 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;
+	/* Longest UTF-8 character takes 6 bytes + 1 byte for NUL, round it to 8B */
+	gchar          tmpbuf[8];
 	gunichar2      uchar;
 	gint           i;       /* Byte counter for tvbuff */
 	gint           tmpbuf_len;
@@ -2509,22 +2493,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;
@@ -2726,7 +2701,8 @@ 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;
+	/* Longest UTF-8 character takes 6 bytes + 1 byte for NUL, round it to 8B */
+	gchar          tmpbuf[8];
 	gunichar2      uchar;
 	gint           size;    /* Number of UTF-16 characters */
 	gint           i;       /* Byte counter for tvbuff */
@@ -2744,22 +2720,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)
-- 
1.7.10.4

