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
This commit is contained in:
Ronnie Sahlberg 2005-07-24 01:56:01 +00:00
parent 37636903bc
commit 0d385f730f
2 changed files with 37 additions and 0 deletions

View File

@ -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

View File

@ -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