diff options
author | guy <guy@f5534014-38df-0310-8fa8-9805f1628bb7> | 2003-06-12 08:33:32 +0000 |
---|---|---|
committer | guy <guy@f5534014-38df-0310-8fa8-9805f1628bb7> | 2003-06-12 08:33:32 +0000 |
commit | 910425338ab45c1f5caedb182243ba5f7fd2cb19 (patch) | |
tree | 22f7363da150c57eb593a2e5871033e8b8585437 /epan/tvbuff.h | |
parent | 5071071567be39aaae134fdb1e45d0e6905854ad (diff) |
Add new routines:
tvb_get_string() - takes a tvbuff, an offset, and a length as
arguments, allocates a buffer big enough to hold a string with
the specified number of bytes plus an added null terminator
(i.e., length+1), copies the specified number of bytes from the
tvbuff, at the specified offset, to that buffer and puts in a
null terminator, and returns a pointer to that buffer (or throws
an exception before allocating the buffer if that many bytes
aren't available in the tvbuff);
tvb_get_stringz() - takes a tvbuff, an offset, and a pointer to
a "gint" as arguments, gets the size of the null-terminated
string starting at the specified offset in the tvbuff (throwing
an exception if the null terminator isn't found), allocates a
buffer big enough to hold that string, copies the string to that
buffer, and returns a pointer to that buffer and stores the
length of the string (including the terminating null) in the
variable pointed to by the "gint" pointer.
Replace many pieces of code allocating a buffer and copying a string
with calls to "tvb_get_string()" (for one thing, "tvb_get_string()"
doesn't require you to remember that the argument to
"tvb_get_nstringz0()" is the size of the buffer into which you're
copying the string, which might be the length of the string to be copied
*plus 1*).
Don't use fixed-length buffers for null-terminated strings (even if the
code that generates those packets has a #define to limit the length of
the string). Use "tvb_get_stringz()", instead.
In some cases where a value is fetched but is only used to pass an
argument to a "proto_tree_add_XXX" routine, use "proto_tree_add_item()"
instead.
git-svn-id: http://anonsvn.wireshark.org/wireshark/trunk@7859 f5534014-38df-0310-8fa8-9805f1628bb7
Diffstat (limited to 'epan/tvbuff.h')
-rw-r--r-- | epan/tvbuff.h | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/epan/tvbuff.h b/epan/tvbuff.h index 5a08bac7b5..6dd756a03c 100644 --- a/epan/tvbuff.h +++ b/epan/tvbuff.h @@ -9,7 +9,7 @@ * the data of a backing tvbuff, or can be a composite of * other tvbuffs. * - * $Id: tvbuff.h,v 1.32 2003/05/19 03:23:12 gerald Exp $ + * $Id: tvbuff.h,v 1.33 2003/06/12 08:33:31 guy Exp $ * * Copyright (c) 2000 by Gilbert Ramirez <gram@alumni.rice.edu> * @@ -318,6 +318,26 @@ extern char *tvb_fake_unicode(tvbuff_t *tvb, int offset, int len, */ extern guint8 * tvb_format_text(tvbuff_t *tvb, gint offset, gint size); +/* + * 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. + */ +extern guint8 *tvb_get_string(tvbuff_t *tvb, gint offset, gint length); + +/* + * 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. + */ +extern guint8 *tvb_get_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. * Returns length of string (not including terminating NUL), or -1 if the string was |