From 82ab7557480913b40fba4c25ed0f49e616edb01f Mon Sep 17 00:00:00 2001 From: sahlberg Date: Sun, 24 Jul 2005 01:56:01 +0000 Subject: [PATCH] add ep_tvb_get_string that acts the same as tvb_get_string but the buffer returned need not be freed. git-svn-id: http://anonsvn.wireshark.org/wireshark/trunk@15024 f5534014-38df-0310-8fa8-9805f1628bb7 --- epan/tvbuff.c | 32 ++++++++++++++++++++++++++++++++ epan/tvbuff.h | 5 +++++ 2 files changed, 37 insertions(+) diff --git a/epan/tvbuff.c b/epan/tvbuff.c index e3878bf76e..b63f568f69 100644 --- a/epan/tvbuff.c +++ b/epan/tvbuff.c @@ -48,6 +48,7 @@ #include "pint.h" #include "tvbuff.h" #include "strutil.h" +#include "emem.h" static const guint8* ensure_contiguous_no_exception(tvbuff_t *tvb, gint offset, gint length, @@ -1723,6 +1724,37 @@ tvb_get_string(tvbuff_t *tvb, gint offset, gint length) strbuf[length] = '\0'; return strbuf; } +/* + * Given a tvbuff, an offset, and a length, allocate a buffer big enough + * to hold a non-null-terminated string of that length at that offset, + * plus a trailing '\0', copy the string into it, and return a pointer + * to the string. + * + * Throws an exception if the tvbuff ends before the string does. + * + * This function allocates memory from a buffer with packet lifetime. + * You do not have to free this buffer, it will be automatically freed + * when ethereal starts decoding the next packet. + * Do not use this function if you want the allocated memory to be persistent + * after the current packet has been dissected. + */ +guint8 * +ep_tvb_get_string(tvbuff_t *tvb, gint offset, gint length) +{ + const guint8 *ptr; + guint8 *strbuf = NULL; + + tvb_ensure_bytes_exist(tvb, offset, length); + + ptr = ensure_contiguous(tvb, offset, length); + strbuf = ep_alloc(length + 1); + if (length != 0) { + memcpy(strbuf, ptr, length); + } + strbuf[length] = '\0'; + return strbuf; +} + /* * Given a tvbuff and an offset, with the offset assumed to refer to diff --git a/epan/tvbuff.h b/epan/tvbuff.h index 2a038926d2..82c76a569d 100644 --- a/epan/tvbuff.h +++ b/epan/tvbuff.h @@ -410,6 +410,11 @@ extern gchar *tvb_format_stringzpad(tvbuff_t *tvb, gint offset, gint size); * Throws an exception if the tvbuff ends before the string does. */ extern guint8 *tvb_get_string(tvbuff_t *tvb, gint offset, gint length); +/* Same as above but the buffer returned from this function does not have to + * be freed. It will be automatically freed after the packet is dissected. + * Buffers allocated by this function are NOT persistent. + */ +extern guint8 *ep_tvb_get_string(tvbuff_t *tvb, gint offset, gint length); /** * Given a tvbuff and an offset, with the offset assumed to refer to -- 2.34.1