aboutsummaryrefslogtreecommitdiffstats
path: root/epan
diff options
context:
space:
mode:
Diffstat (limited to 'epan')
-rw-r--r--epan/proto.c17
-rw-r--r--epan/tvbuff.c73
-rw-r--r--epan/tvbuff.h22
3 files changed, 90 insertions, 22 deletions
diff --git a/epan/proto.c b/epan/proto.c
index c04cc1d028..97050528a5 100644
--- a/epan/proto.c
+++ b/epan/proto.c
@@ -1,7 +1,7 @@
/* proto.c
* Routines for protocol tree
*
- * $Id: proto.c,v 1.92 2003/06/11 21:48:39 guy Exp $
+ * $Id: proto.c,v 1.93 2003/06/12 08:33:31 guy Exp $
*
* Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@ethereal.com>
@@ -711,7 +711,8 @@ proto_tree_add_item(proto_tree *tree, int hfindex, tvbuff_t *tvb,
/* This can throw an exception */
length = tvb_strsize(tvb, start);
- /* This g_strdup'ed memory is freed in proto_tree_free_node() */
+ /* This g_malloc'ed memory is freed
+ in proto_tree_free_node() */
string = g_malloc(length);
tvb_memcpy(tvb, string, start, length);
@@ -750,16 +751,10 @@ proto_tree_add_item(proto_tree *tree, int hfindex, tvbuff_t *tvb,
* rather than null-terminated.)
*/
- /* This g_strdup'ed memory is freed
+ /* This g_malloc'ed memory is freed
* in proto_tree_free_node() */
- string = g_malloc(length + 1);
-
- CLEANUP_PUSH(g_free, string);
-
- tvb_memcpy(tvb, string, start, length);
- string[length] = '\0';
-
- CLEANUP_POP;
+ string = tvb_get_string(tvb, start,
+ length);
new_fi->length = length;
}
proto_tree_set_string(new_fi, string, TRUE);
diff --git a/epan/tvbuff.c b/epan/tvbuff.c
index ec66db45d7..3961868723 100644
--- a/epan/tvbuff.c
+++ b/epan/tvbuff.c
@@ -9,7 +9,7 @@
* the data of a backing tvbuff, or can be a composite of
* other tvbuffs.
*
- * $Id: tvbuff.c,v 1.46 2003/06/09 07:27:42 guy Exp $
+ * $Id: tvbuff.c,v 1.47 2003/06/12 08:33:31 guy Exp $
*
* Copyright (c) 2000 by Gilbert Ramirez <gram@alumni.rice.edu>
*
@@ -107,6 +107,10 @@ struct tvbuff {
};
static guint8*
+ensure_contiguous_no_exception(tvbuff_t *tvb, gint offset, gint length,
+ int *exception);
+
+static guint8*
ensure_contiguous(tvbuff_t *tvb, gint offset, gint length);
/* We dole out tvbuff's from this memchunk. */
@@ -804,7 +808,8 @@ tvb_raw_offset(tvbuff_t *tvb)
}
static guint8*
-composite_ensure_contiguous(tvbuff_t *tvb, guint abs_offset, guint abs_length)
+composite_ensure_contiguous_no_exception(tvbuff_t *tvb, guint abs_offset,
+ guint abs_length)
{
guint i, num_members;
tvb_comp_t *composite;
@@ -831,8 +836,11 @@ composite_ensure_contiguous(tvbuff_t *tvb, guint abs_offset, guint abs_length)
if (check_offset_length_no_exception(member_tvb, abs_offset - composite->start_offsets[i],
abs_length, &member_offset, &member_length, NULL)) {
+ /*
+ * The range is, in fact, contiguous within member_tvb.
+ */
g_assert(!tvb->real_data);
- return ensure_contiguous(member_tvb, member_offset, member_length);
+ return ensure_contiguous_no_exception(member_tvb, member_offset, member_length, NULL);
}
else {
tvb->real_data = tvb_memdup(tvb, 0, -1);
@@ -854,6 +862,10 @@ ensure_contiguous_no_exception(tvbuff_t *tvb, gint offset, gint length,
return NULL;
}
+ /*
+ * We know that all the data is present in the tvbuff, so
+ * no exceptions should be thrown.
+ */
if (tvb->real_data) {
return tvb->real_data + abs_offset;
}
@@ -862,11 +874,11 @@ ensure_contiguous_no_exception(tvbuff_t *tvb, gint offset, gint length,
case TVBUFF_REAL_DATA:
g_assert_not_reached();
case TVBUFF_SUBSET:
- return ensure_contiguous(tvb->tvbuffs.subset.tvb,
+ return ensure_contiguous_no_exception(tvb->tvbuffs.subset.tvb,
abs_offset - tvb->tvbuffs.subset.offset,
- abs_length);
+ abs_length, NULL);
case TVBUFF_COMPOSITE:
- return composite_ensure_contiguous(tvb, abs_offset, abs_length);
+ return composite_ensure_contiguous_no_exception(tvb, abs_offset, abs_length);
}
}
@@ -1021,10 +1033,9 @@ tvb_memcpy(tvbuff_t *tvb, guint8* target, gint offset, gint length)
* "tvb_ensure_bytes_exist()" and then allocates a buffer and copies
* data to it.
*
- * Does anything depend on this routine treating -1 as meaning "to the
- * end of the buffer"? If so, perhaps we need two routines, one that
- * treats -1 as such, and one that checks for -1 and then calls this
- * routine.
+ * "composite_ensure_contiguous_no_exception()" depends on -1 not being
+ * an error; does anything else depend on this routine treating -1 as
+ * meaning "to the end of the buffer"?
*/
guint8*
tvb_memdup(tvbuff_t *tvb, gint offset, gint length)
@@ -1687,6 +1698,48 @@ 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.
+ */
+guint8 *
+tvb_get_string(tvbuff_t *tvb, gint offset, gint length)
+{
+ guint8 *ptr, *strbuf;
+
+ ptr = ensure_contiguous(tvb, offset, length);
+ strbuf = g_malloc(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
+ * 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.
+ */
+guint8 *
+tvb_get_stringz(tvbuff_t *tvb, gint offset, gint *lengthp)
+{
+ guint size;
+ guint8 *strptr;
+
+ size = tvb_strsize(tvb, offset);
+ strptr = g_malloc(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 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