diff options
author | Ronnie Sahlberg <ronnie_sahlberg@ozemail.com.au> | 2005-08-10 14:25:59 +0000 |
---|---|---|
committer | Ronnie Sahlberg <ronnie_sahlberg@ozemail.com.au> | 2005-08-10 14:25:59 +0000 |
commit | 9599cf9e3b88e57f22f149c2db2956b779ba864f (patch) | |
tree | bd6dc25e40e463e67fe7b295e144b027909a62cc /epan | |
parent | efa1f430059f40d054d9f85fb0869583ffb76d2b (diff) |
add new function tvb_get_ephemeral_stringz()
svn path=/trunk/; revision=15273
Diffstat (limited to 'epan')
-rw-r--r-- | epan/tvbuff.c | 26 | ||||
-rw-r--r-- | epan/tvbuff.h | 9 |
2 files changed, 35 insertions, 0 deletions
diff --git a/epan/tvbuff.c b/epan/tvbuff.c index dab173e9ae..f799e25c19 100644 --- a/epan/tvbuff.c +++ b/epan/tvbuff.c @@ -1839,6 +1839,32 @@ tvb_get_stringz(tvbuff_t *tvb, gint offset, gint *lengthp) *lengthp = size; return strptr; } +/* + * Given a tvbuff and an offset, with the offset assumed to refer to + * a null-terminated string, find the length of that string (and throw + * an exception if the tvbuff ends before we find the null), allocate + * a buffer big enough to hold the string, copy the string into it, + * and return a pointer to the string. Also return the length of the + * string (including the terminating null) through a pointer. + * + * 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 * +tvb_get_ephemeral_stringz(tvbuff_t *tvb, gint offset, gint *lengthp) +{ + guint size; + guint8 *strptr; + + size = tvb_strsize(tvb, offset); + strptr = ep_alloc(size); + tvb_memcpy(tvb, strptr, offset, size); + *lengthp = size; + return strptr; +} /* Looks for a stringz (NUL-terminated string) in tvbuff and copies * no more than bufsize number of bytes, including terminating NUL, to buffer. diff --git a/epan/tvbuff.h b/epan/tvbuff.h index d76f9b2395..29504ebd5f 100644 --- a/epan/tvbuff.h +++ b/epan/tvbuff.h @@ -444,8 +444,17 @@ extern guint8 *tvb_get_ephemeral_string(tvbuff_t *tvb, gint offset, gint length) * a buffer big enough to hold the string, copy the string into it, * and return a pointer to the string. Also return the length of the * string (including the terminating null) through a pointer. + * + * tvb_get_stringz() returns a string allocated by g_malloc() and therefore + * MUST be g_free() by the caller in order not to leak + * memory. + * + * tvb_get_ephemeral_stringz() returns a string that does not need to be freed, + * instead it will automatically be freed once the next + * packet is dissected. */ extern guint8 *tvb_get_stringz(tvbuff_t *tvb, gint offset, gint *lengthp); +extern guint8 *tvb_get_ephemeral_stringz(tvbuff_t *tvb, gint offset, gint *lengthp); /** Looks for a stringz (NUL-terminated string) in tvbuff and copies * no more than bufsize number of bytes, including terminating NUL, to buffer. |