summaryrefslogtreecommitdiff
path: root/epan/tvbuff.c
diff options
context:
space:
mode:
authorRonnie Sahlberg <ronnie_sahlberg@ozemail.com.au>2005-07-24 01:56:01 +0000
committerRonnie Sahlberg <ronnie_sahlberg@ozemail.com.au>2005-07-24 01:56:01 +0000
commit0d385f730fd973a7e5087f457507dd2b445de58c (patch)
tree98495a546c84437fd3f1ce424194ae59e23b7739 /epan/tvbuff.c
parent37636903bccb02c0d2e5841a209112e057a9449c (diff)
downloadwireshark-0d385f730fd973a7e5087f457507dd2b445de58c.tar.gz
add
ep_tvb_get_string that acts the same as tvb_get_string but the buffer returned need not be freed. svn path=/trunk/; revision=15024
Diffstat (limited to 'epan/tvbuff.c')
-rw-r--r--epan/tvbuff.c32
1 files changed, 32 insertions, 0 deletions
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