diff options
author | Guy Harris <guy@alum.mit.edu> | 2019-07-24 11:22:33 -0700 |
---|---|---|
committer | Guy Harris <guy@alum.mit.edu> | 2019-07-24 19:05:13 +0000 |
commit | 2c7e0a93e5f205bb61fb675d7de1b0ca9b5c0746 (patch) | |
tree | 8ae6e291975cd548f0908b9cfd46525b5ebe451f /epan/tvbuff.c | |
parent | c55780951cfd44420d1a40d5e6d68ee20af98338 (diff) |
Add a routine to fetch raw bytes into a fixed-length buffer as a string.
That's what the remaining calls to tvb_get_nstringz() and
tvb_get_nstringz0() are being used to do, even though those routines
were not intended for that purpose - the calls are extracting from a
text protcool, meaning that the strings are *not* null-terminate in the
packet.
Strings - even null-terminated ones - should, in almost all cases, be
extracted by tvb_get_string_enc() or routines that call it, so that an
encoding is specified. In the few cases where we're fetching strings
only to be compared to ASCII constants, or to parse as numbers, we can
get away with this.
Change-Id: I29f0532902c4ade2207de7f06db69c32eafd4132
Reviewed-on: https://code.wireshark.org/review/34072
Petri-Dish: Guy Harris <guy@alum.mit.edu>
Tested-by: Petri Dish Buildbot
Reviewed-by: Guy Harris <guy@alum.mit.edu>
Diffstat (limited to 'epan/tvbuff.c')
-rw-r--r-- | epan/tvbuff.c | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/epan/tvbuff.c b/epan/tvbuff.c index 07c0b23472..6bbdbd271e 100644 --- a/epan/tvbuff.c +++ b/epan/tvbuff.c @@ -3443,6 +3443,38 @@ tvb_get_nstringz0(tvbuff_t *tvb, const gint offset, const guint bufsize, guint8* } } +/* + * Given a tvbuff, an offset into the tvbuff, a buffer, and a buffer size, + * extract as many raw bytes from the tvbuff, starting at the offset, + * as 1) are available in the tvbuff and 2) will fit in the buffer, leaving + * room for a terminating NUL. + */ +gint +tvb_get_raw_bytes_as_string(tvbuff_t *tvb, const gint offset, char *buffer, size_t bufsize) +{ + gint len = 0; + + DISSECTOR_ASSERT(tvb && tvb->initialized); + + /* There must be room for the string and the terminating NUL. */ + DISSECTOR_ASSERT(bufsize > 0); + + DISSECTOR_ASSERT(bufsize - 1 < G_MAXINT); + + len = tvb_captured_length_remaining(tvb, offset); + if (len <= 0) { + buffer[0] = '\0'; + return 0; + } + if (len > (gint)(bufsize - 1)) + len = (gint)(bufsize - 1); + + /* Copy the string to buffer */ + tvb_memcpy(tvb, buffer, offset, len); + buffer[len] = '\0'; + return len; +} + gboolean tvb_ascii_isprint(tvbuff_t *tvb, const gint offset, const gint length) { const guint8* buf = tvb_get_ptr(tvb, offset, length); |