diff --git a/doc/README.developer b/doc/README.developer index 7ce7b15175..be0279452e 100644 --- a/doc/README.developer +++ b/doc/README.developer @@ -1127,6 +1127,7 @@ String accessors: guint8 *tvb_get_string(tvbuff_t*, gint offset, gint length); guint8 *tvb_get_ephemeral_string(tvbuff_t*, gint offset, gint length); +guint8 *tvb_get_seasonal_string(tvbuff_t*, gint offset, gint length); Returns a null-terminated buffer containing data from the specified tvbuff, starting at the specified offset, and containing the specified @@ -1136,14 +1137,20 @@ as it includes a null character to terminate the string). tvb_get_string() returns a buffer allocated by g_malloc() so you must g_free() it when you are finished with the string. Failure to g_free() this buffer will lead to memory leaks. + tvb_get_ephemeral_string() returns a buffer allocated from a special heap with a lifetime until the next packet is dissected. You do not need to free() this buffer, it will happen automatically once the next packet is dissected. +tvb_get_seasonal_string() returns a buffer allocated from a special heap +with a lifetime of the current capture session. You do not need to +free() this buffer, it will happen automatically once the a new capture or +file is opened. guint8 *tvb_get_stringz(tvbuff_t *tvb, gint offset, gint *lengthp); guint8 *tvb_get_ephemeral_stringz(tvbuff_t *tvb, gint offset, gint *lengthp); +guint8 *tvb_get_seasonal_stringz(tvbuff_t *tvb, gint offset, gint *lengthp); Returns a null-terminated buffer, allocated with "g_malloc()", containing data from the specified tvbuff, starting at the @@ -1159,6 +1166,10 @@ with a lifetime until the next packet is dissected. You do not need to free() this buffer, it will happen automatically once the next packet is dissected. +tvb_get_seasonal_stringz() returns a buffer allocated from a special heap +with a lifetime of the current capture session. You do not need to +free() this buffer, it will happen automatically once the a new capture or +file is opened. guint8 *tvb_fake_unicode(tvbuff_t*, gint offset, gint length, gboolean little_endian); guint8 *tvb_get_ephemeral_faked_unicode(tvbuff_t*, gint offset, gint length, gboolean little_endian); diff --git a/epan/tvbuff.c b/epan/tvbuff.c index a47d7792e6..c3d4fb6c58 100644 --- a/epan/tvbuff.c +++ b/epan/tvbuff.c @@ -2144,6 +2144,34 @@ tvb_get_ephemeral_string(tvbuff_t *tvb, gint offset, gint length) 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 capture session lifetime. + * You do not have to free this buffer, it will be automatically freed + * when wireshark starts or opens a new capture. + */ +guint8 * +tvb_get_seasonal_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 = se_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 @@ -2192,6 +2220,31 @@ tvb_get_ephemeral_stringz(tvbuff_t *tvb, gint offset, gint *lengthp) 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 capture session lifetime. + * You do not have to free this buffer, it will be automatically freed + * when wireshark starts or opens a new capture. + */ +guint8 * +tvb_get_seasonal_stringz(tvbuff_t *tvb, gint offset, gint *lengthp) +{ + guint size; + guint8 *strptr; + + size = tvb_strsize(tvb, offset); + strptr = se_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. * Returns length of string (not including terminating NUL), or -1 if the string was diff --git a/epan/tvbuff.h b/epan/tvbuff.h index 7ddf58db72..84c4c9cbe6 100644 --- a/epan/tvbuff.h +++ b/epan/tvbuff.h @@ -460,9 +460,14 @@ extern gchar *tvb_format_stringzpad(tvbuff_t *tvb, gint offset, gint size); * tvb_get_ephemeral_string() returns a string that does not need to be freed, * instead it will automatically be freed once the next * packet is dissected. + * + * tvb_get_seasonal_string() returns a string that does not need to be freed, + * instead it will automatically be freed when a new capture + * or file is opened. */ extern guint8 *tvb_get_string(tvbuff_t *tvb, gint offset, gint length); extern guint8 *tvb_get_ephemeral_string(tvbuff_t *tvb, gint offset, gint length); +extern guint8 *tvb_get_seasonal_string(tvbuff_t *tvb, gint offset, gint length); /** @@ -480,9 +485,14 @@ extern guint8 *tvb_get_ephemeral_string(tvbuff_t *tvb, gint offset, gint length) * 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. + * + * tvb_get_seasonal_stringz() returns a string that does not need to be freed, + * instead it will automatically be freed when a new capture + * or file is opened. */ 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); +extern guint8 *tvb_get_seasonal_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.