Blame SOURCES/wireshark-1.10.14-tvbuff.patch

affdba
diff --git a/epan/tvbuff.c b/epan/tvbuff.c
affdba
index 98968a0..db2cfd1 100644
affdba
--- a/epan/tvbuff.c
affdba
+++ b/epan/tvbuff.c
affdba
@@ -53,7 +53,7 @@
affdba
 
affdba
 static const guint8*
affdba
 ensure_contiguous_no_exception(tvbuff_t *tvb, const gint offset, const gint length,
affdba
-		int *exception);
affdba
+		int *pexception);
affdba
 
affdba
 static const guint8*
affdba
 ensure_contiguous(tvbuff_t *tvb, const gint offset, const gint length);
affdba
@@ -241,45 +241,48 @@ tvb_new_child_real_data(tvbuff_t *parent, const guint8* data, const guint length
affdba
 	return tvb;
affdba
 }
affdba
 
affdba
-/* Computes the absolute offset and length based on a possibly-negative offset
affdba
- * and a length that is possible -1 (which means "to the end of the data").
affdba
- * Returns TRUE/FALSE indicating whether the offset is in bounds or
affdba
- * not. The integer ptrs are modified with the new offset and length.
affdba
- * No exception is thrown.
affdba
+/*
affdba
+ * Check whether that offset goes more than one byte past the
affdba
+ * end of the buffer.
affdba
  *
affdba
- * XXX - we return TRUE, not FALSE, if the offset is positive and right
affdba
- * after the end of the tvbuff (i.e., equal to the length).  We do this
affdba
- * so that a dissector constructing a subset tvbuff for the next protocol
affdba
- * will get a zero-length tvbuff, not an exception, if there's no data
affdba
- * left for the next protocol - we want the next protocol to be the one
affdba
- * that gets an exception, so the error is reported as an error in that
affdba
- * protocol rather than the containing protocol.  */
affdba
-static gboolean
affdba
-compute_offset_length(const tvbuff_t *tvb,
affdba
-		      const gint offset, const gint length_val,
affdba
-		      guint *offset_ptr, guint *length_ptr, int *exception)
affdba
+ * If not, return 0; otherwise, return exception
affdba
+ */
affdba
+static inline int
affdba
+validate_offset(const tvbuff_t *tvb, const guint abs_offset)
affdba
 {
affdba
-	DISSECTOR_ASSERT(offset_ptr);
affdba
-	DISSECTOR_ASSERT(length_ptr);
affdba
+	int exception;
affdba
+
affdba
+	if (G_LIKELY(abs_offset <= tvb->length))
affdba
+		exception = 0;
affdba
+	else if (abs_offset <= tvb->reported_length)
affdba
+		exception = BoundsError;
affdba
+	else {
affdba
+		if (tvb->flags & TVBUFF_FRAGMENT)
affdba
+			exception = FragmentBoundsError;
affdba
+		else
affdba
+			exception = ReportedBoundsError;
affdba
+	}
affdba
+
affdba
+	return exception;
affdba
+}
affdba
+
affdba
+static int
affdba
+compute_offset(const tvbuff_t *tvb, const gint offset, guint *offset_ptr)
affdba
+{
affdba
+	int exception;
affdba
 
affdba
-	/* Compute the offset */
affdba
 	if (offset >= 0) {
affdba
 		/* Positive offset - relative to the beginning of the packet. */
affdba
 		if ((guint) offset > tvb->reported_length) {
affdba
-			if (exception) {
affdba
-				if (tvb->flags & TVBUFF_FRAGMENT) {
affdba
-					*exception = FragmentBoundsError;
affdba
-				} else {
affdba
-					*exception = ReportedBoundsError;
affdba
-				}
affdba
+			if (tvb->flags & TVBUFF_FRAGMENT) {
affdba
+				exception = FragmentBoundsError;
affdba
+			} else {
affdba
+				exception = ReportedBoundsError;
affdba
 			}
affdba
-			return FALSE;
affdba
+			return exception;
affdba
 		}
affdba
 		else if ((guint) offset > tvb->length) {
affdba
-			if (exception) {
affdba
-				*exception = BoundsError;
affdba
-			}
affdba
-			return FALSE;
affdba
+			return BoundsError;
affdba
 		}
affdba
 		else {
affdba
 			*offset_ptr = offset;
affdba
@@ -288,95 +291,90 @@ compute_offset_length(const tvbuff_t *tvb,
affdba
 	else {
affdba
 		/* Negative offset - relative to the end of the packet. */
affdba
 		if ((guint) -offset > tvb->reported_length) {
affdba
-			if (exception) {
affdba
-				if (tvb->flags & TVBUFF_FRAGMENT) {
affdba
-					*exception = FragmentBoundsError;
affdba
-				} else {
affdba
-					*exception = ReportedBoundsError;
affdba
-				}
affdba
+			if (tvb->flags & TVBUFF_FRAGMENT) {
affdba
+				exception = FragmentBoundsError;
affdba
+			} else {
affdba
+				exception = ReportedBoundsError;
affdba
 			}
affdba
-			return FALSE;
affdba
+			return exception;
affdba
 		}
affdba
 		else if ((guint) -offset > tvb->length) {
affdba
-			if (exception) {
affdba
-				*exception = BoundsError;
affdba
-			}
affdba
-			return FALSE;
affdba
+			return BoundsError;
affdba
 		}
affdba
 		else {
affdba
 			*offset_ptr = tvb->length + offset;
affdba
 		}
affdba
 	}
affdba
 
affdba
-	/* Compute the length */
affdba
-	if (length_val < -1) {
affdba
-		if (exception) {
affdba
-			/* XXX - ReportedBoundsError? */
affdba
-			*exception = BoundsError;
affdba
-		}
affdba
-		return FALSE;
affdba
-	}
affdba
-	else if (length_val == -1) {
affdba
-		*length_ptr = tvb->length - *offset_ptr;
affdba
-	}
affdba
-	else {
affdba
-		*length_ptr = length_val;
affdba
-	}
affdba
+	return 0;
affdba
+}
affdba
 
affdba
-	return TRUE;
affdba
+static int
affdba
+compute_offset_and_remaining(const tvbuff_t *tvb, const gint offset, guint *offset_ptr, guint *rem_len)
affdba
+{
affdba
+	int exception;
affdba
+
affdba
+	exception = compute_offset(tvb, offset, offset_ptr);
affdba
+	if (!exception)
affdba
+		*rem_len = tvb->length - *offset_ptr;
affdba
+
affdba
+	return exception;
affdba
 }
affdba
 
affdba
-static gboolean
affdba
+/* Computes the absolute offset and length based on a possibly-negative offset
affdba
+ * and a length that is possible -1 (which means "to the end of the data").
affdba
+ * Returns integer indicating whether the offset is in bounds (0) or
affdba
+ * not (exception number). The integer ptrs are modified with the new offset and length.
affdba
+ * No exception is thrown.
affdba
+ *
affdba
+ * XXX - we return success (0), if the offset is positive and right
affdba
+ * after the end of the tvbuff (i.e., equal to the length).  We do this
affdba
+ * so that a dissector constructing a subset tvbuff for the next protocol
affdba
+ * will get a zero-length tvbuff, not an exception, if there's no data
affdba
+ * left for the next protocol - we want the next protocol to be the one
affdba
+ * that gets an exception, so the error is reported as an error in that
affdba
+ * protocol rather than the containing protocol.  */
affdba
+static int
affdba
 check_offset_length_no_exception(const tvbuff_t *tvb,
affdba
 				 const gint offset, gint const length_val,
affdba
-				 guint *offset_ptr, guint *length_ptr, int *exception)
affdba
+				 guint *offset_ptr, guint *length_ptr)
affdba
 {
affdba
-	guint	end_offset;
affdba
+	guint end_offset;
affdba
+	int exception;
affdba
 
affdba
-	if (!compute_offset_length(tvb,
affdba
-				   offset, length_val, offset_ptr, length_ptr, exception)) {
affdba
-		return FALSE;
affdba
+	DISSECTOR_ASSERT(offset_ptr);
affdba
+	DISSECTOR_ASSERT(length_ptr);
affdba
+
affdba
+	/* Compute the offset */
affdba
+	exception = compute_offset(tvb, offset, offset_ptr);
affdba
+	if (exception)
affdba
+		return exception;
affdba
+
affdba
+	if (length_val < -1) {
affdba
+		/* XXX - ReportedBoundsError? */
affdba
+		return BoundsError;
affdba
 	}
affdba
 
affdba
+	/* Compute the length */
affdba
+	if (length_val == -1)
affdba
+		*length_ptr = tvb->length - *offset_ptr;
affdba
+	else
affdba
+		*length_ptr = length_val;
affdba
+
affdba
 	/*
affdba
 	 * Compute the offset of the first byte past the length.
affdba
 	 */
affdba
 	end_offset = *offset_ptr + *length_ptr;
affdba
 
affdba
 	/*
affdba
-	 * Check for an overflow, and clamp "end_offset" at the maximum
affdba
-	 * if we got an overflow - that should force us to indicate that
affdba
-	 * we're past the end of the tvbuff.
affdba
+	 * Check for an overflow
affdba
 	 */
affdba
 	if (end_offset < *offset_ptr)
affdba
-		end_offset = UINT_MAX;
affdba
-
affdba
-	/*
affdba
-	 * Check whether that offset goes more than one byte past the
affdba
-	 * end of the buffer.
affdba
-	 *
affdba
-	 * If not, return TRUE; otherwise, return FALSE and, if "exception"
affdba
-	 * is non-null, return the appropriate exception through it.
affdba
-	 */
affdba
-	if (end_offset <= tvb->length) {
affdba
-		return TRUE;
affdba
-	}
affdba
-	else {
affdba
-		if (exception) {
affdba
-			if (end_offset <= tvb->reported_length) {
affdba
-				*exception = BoundsError;
affdba
-			}
affdba
-			else {
affdba
-				if (tvb->flags & TVBUFF_FRAGMENT) {
affdba
-					*exception = FragmentBoundsError;
affdba
-				} else {
affdba
-					*exception = ReportedBoundsError;
affdba
-				}
affdba
-			}
affdba
-		}
affdba
+		exception = BoundsError;
affdba
+	else
affdba
+		exception = validate_offset(tvb, end_offset);
affdba
 
affdba
-		return FALSE;
affdba
-	}
affdba
+	return exception;
affdba
 }
affdba
 
affdba
 /* Checks (+/-) offset and length and throws an exception if
affdba
@@ -387,13 +385,11 @@ check_offset_length(const tvbuff_t *tvb,
affdba
 		    const gint offset, gint const length_val,
affdba
 		    guint *offset_ptr, guint *length_ptr)
affdba
 {
affdba
-	int exception = 0;
affdba
-
affdba
-	if (!check_offset_length_no_exception(tvb,
affdba
-					      offset, length_val, offset_ptr, length_ptr, &exception)) {
affdba
-		DISSECTOR_ASSERT(exception > 0);
affdba
+	int exception;
affdba
+	
affdba
+	exception = check_offset_length_no_exception(tvb, offset, length_val, offset_ptr, length_ptr);
affdba
+	if (exception)
affdba
 		THROW(exception);
affdba
-	}
affdba
 }
affdba
 
affdba
 static tvbuff_t *
affdba
@@ -538,7 +534,7 @@ tvb_new_octet_aligned(tvbuff_t *tvb, guint32 bit_offset, gint32 no_of_bits)
affdba
 	right = 8 - left; /* for right-shifting */
affdba
 
affdba
 	if (no_of_bits == -1) {
affdba
-		datalen = tvb_length_remaining(tvb, byte_offset);
affdba
+		datalen = tvb_captured_length_remaining(tvb, byte_offset);
affdba
 		remaining_bits = 0;
affdba
 	} else {
affdba
 		datalen = no_of_bits >> 3;
affdba
@@ -560,7 +556,7 @@ tvb_new_octet_aligned(tvbuff_t *tvb, guint32 bit_offset, gint32 no_of_bits)
affdba
  	* if non extra byte is available, the last shifted byte requires
affdba
  	* special treatment
affdba
  	*/
affdba
-	if (tvb_length_remaining(tvb, byte_offset) > datalen) {
affdba
+	if (tvb_captured_length_remaining(tvb, byte_offset) > datalen) {
affdba
 		data = tvb_get_ptr(tvb, byte_offset, datalen + 1);
affdba
 
affdba
 		/* Do this allocation AFTER tvb_get_ptr() (which could throw an exception) */
affdba
@@ -679,7 +675,7 @@ tvb_composite_finalize(tvbuff_t *tvb)
affdba
 
affdba
 
affdba
 guint
affdba
-tvb_length(const tvbuff_t *tvb)
affdba
+tvb_captured_length(const tvbuff_t *tvb)
affdba
 {
affdba
 	DISSECTOR_ASSERT(tvb && tvb->initialized);
affdba
 
affdba
@@ -687,32 +683,33 @@ tvb_length(const tvbuff_t *tvb)
affdba
 }
affdba
 
affdba
 gint
affdba
-tvb_length_remaining(const tvbuff_t *tvb, const gint offset)
affdba
+tvb_captured_length_remaining(const tvbuff_t *tvb, const gint offset)
affdba
 {
affdba
-	guint abs_offset, abs_length;
affdba
+	guint abs_offset, rem_length;
affdba
+	int exception;
affdba
 
affdba
 	DISSECTOR_ASSERT(tvb && tvb->initialized);
affdba
 
affdba
-	if (compute_offset_length(tvb, offset, -1, &abs_offset, &abs_length, NULL)) {
affdba
-		return abs_length;
affdba
-	}
affdba
-	else {
affdba
-		return -1;
affdba
-	}
affdba
+	exception = compute_offset_and_remaining(tvb, offset, &abs_offset, &rem_length);
affdba
+	if (exception)
affdba
+		return 0;
affdba
+
affdba
+	return rem_length;
affdba
 }
affdba
 
affdba
 guint
affdba
-tvb_ensure_length_remaining(const tvbuff_t *tvb, const gint offset)
affdba
+tvb_ensure_captured_length_remaining(const tvbuff_t *tvb, const gint offset)
affdba
 {
affdba
-	guint abs_offset, abs_length;
affdba
+	guint abs_offset, rem_length;
affdba
 	int   exception;
affdba
 
affdba
 	DISSECTOR_ASSERT(tvb && tvb->initialized);
affdba
 
affdba
-	if (!compute_offset_length(tvb, offset, -1, &abs_offset, &abs_length, &exception)) {
affdba
+	exception = compute_offset_and_remaining(tvb, offset, &abs_offset, &rem_length);
affdba
+	if (exception)
affdba
 		THROW(exception);
affdba
-	}
affdba
-	if (abs_length == 0) {
affdba
+
affdba
+	if (rem_length == 0) {
affdba
 		/*
affdba
 		 * This routine ensures there's at least one byte available.
affdba
 		 * There aren't any bytes available, so throw the appropriate
affdba
@@ -727,7 +724,7 @@ tvb_ensure_length_remaining(const tvbuff_t *tvb, const gint offset)
affdba
 		} else
affdba
 			THROW(BoundsError);
affdba
 	}
affdba
-	return abs_length;
affdba
+	return rem_length;
affdba
 }
affdba
 
affdba
 
affdba
@@ -739,18 +736,15 @@ gboolean
affdba
 tvb_bytes_exist(const tvbuff_t *tvb, const gint offset, const gint length)
affdba
 {
affdba
 	guint abs_offset, abs_length;
affdba
+	int exception;
affdba
 
affdba
 	DISSECTOR_ASSERT(tvb && tvb->initialized);
affdba
 
affdba
-	if (!compute_offset_length(tvb, offset, length, &abs_offset, &abs_length, NULL))
affdba
+	exception = check_offset_length_no_exception(tvb, offset, length, &abs_offset, &abs_length);
affdba
+	if (exception)
affdba
 		return FALSE;
affdba
 
affdba
-	if (abs_offset + abs_length <= tvb->length) {
affdba
-		return TRUE;
affdba
-	}
affdba
-	else {
affdba
-		return FALSE;
affdba
-	}
affdba
+	return TRUE;
affdba
 }
affdba
 
affdba
 /* Validates that 'length' bytes are available starting from
affdba
@@ -781,10 +775,13 @@ tvb_ensure_bytes_exist(const tvbuff_t *tvb, const gint offset, const gint length
affdba
 gboolean
affdba
 tvb_offset_exists(const tvbuff_t *tvb, const gint offset)
affdba
 {
affdba
-	guint abs_offset, abs_length;
affdba
+	guint abs_offset;
affdba
+	int exception;
affdba
 
affdba
 	DISSECTOR_ASSERT(tvb && tvb->initialized);
affdba
-	if (!compute_offset_length(tvb, offset, -1, &abs_offset, &abs_length, NULL))
affdba
+
affdba
+	exception = compute_offset(tvb, offset, &abs_offset);
affdba
+	if (exception)
affdba
 		return FALSE;
affdba
 
affdba
 	if (abs_offset < tvb->length) {
affdba
@@ -806,19 +803,19 @@ tvb_reported_length(const tvbuff_t *tvb)
affdba
 gint
affdba
 tvb_reported_length_remaining(const tvbuff_t *tvb, const gint offset)
affdba
 {
affdba
-	guint abs_offset, abs_length;
affdba
+	guint abs_offset;
affdba
+	int exception;
affdba
 
affdba
 	DISSECTOR_ASSERT(tvb && tvb->initialized);
affdba
 
affdba
-	if (compute_offset_length(tvb, offset, -1, &abs_offset, &abs_length, NULL)) {
affdba
-		if (tvb->reported_length >= abs_offset)
affdba
-			return tvb->reported_length - abs_offset;
affdba
-		else
affdba
-			return -1;
affdba
-	}
affdba
-	else {
affdba
-		return -1;
affdba
-	}
affdba
+	exception = compute_offset(tvb, offset, &abs_offset);
affdba
+	if (exception)
affdba
+		return 0;
affdba
+
affdba
+	if (tvb->reported_length >= abs_offset)
affdba
+		return tvb->reported_length - abs_offset;
affdba
+	else
affdba
+		return 0;
affdba
 }
affdba
 
affdba
 /* Set the reported length of a tvbuff to a given value; used for protocols
affdba
@@ -896,6 +893,7 @@ composite_ensure_contiguous_no_exception(tvbuff_t *tvb, const guint abs_offset,
affdba
 	tvbuff_t   *member_tvb = NULL;
affdba
 	guint	    member_offset, member_length;
affdba
 	GSList	   *slist;
affdba
+	int         exception;
affdba
 
affdba
 	DISSECTOR_ASSERT(tvb->type == TVBUFF_COMPOSITE);
affdba
 
affdba
@@ -913,10 +911,11 @@ composite_ensure_contiguous_no_exception(tvbuff_t *tvb, const guint abs_offset,
affdba
 	}
affdba
 	DISSECTOR_ASSERT(member_tvb);
affdba
 
affdba
-	if (check_offset_length_no_exception(member_tvb,
affdba
+	exception = check_offset_length_no_exception(member_tvb,
affdba
 					     abs_offset - composite->start_offsets[i],
affdba
-					     abs_length, &member_offset, &member_length, NULL)) {
affdba
+					     abs_length, &member_offset, &member_length);
affdba
 
affdba
+	if (!exception) {
affdba
 		/*
affdba
 		 * The range is, in fact, contiguous within member_tvb.
affdba
 		 */
affdba
@@ -932,12 +931,15 @@ composite_ensure_contiguous_no_exception(tvbuff_t *tvb, const guint abs_offset,
affdba
 }
affdba
 
affdba
 static const guint8*
affdba
-ensure_contiguous_no_exception(tvbuff_t *tvb, const gint offset, const gint length, int *exception)
affdba
+ensure_contiguous_no_exception(tvbuff_t *tvb, const gint offset, const gint length, int *pexception)
affdba
 {
affdba
 	guint abs_offset, abs_length;
affdba
+	int exception;
affdba
 
affdba
-	if (!check_offset_length_no_exception(tvb, offset, length,
affdba
-		&abs_offset, &abs_length, exception)) {
affdba
+	exception = check_offset_length_no_exception(tvb, offset, length, &abs_offset, &abs_length);
affdba
+	if (exception) {
affdba
+		if (pexception)
affdba
+			*pexception = exception;
affdba
 		return NULL;
affdba
 	}
affdba
 
affdba
@@ -1046,7 +1048,7 @@ composite_memcpy(tvbuff_t *tvb, guint8* target, guint abs_offset, size_t abs_len
affdba
 	tvb_comp_t *composite;
affdba
 	tvbuff_t   *member_tvb = NULL;
affdba
 	guint	    member_offset, member_length;
affdba
-	gboolean    retval;
affdba
+	int         exception;
affdba
 	GSList	   *slist;
affdba
 
affdba
 	DISSECTOR_ASSERT(tvb->type == TVBUFF_COMPOSITE);
affdba
@@ -1065,9 +1067,10 @@ composite_memcpy(tvbuff_t *tvb, guint8* target, guint abs_offset, size_t abs_len
affdba
 	}
affdba
 	DISSECTOR_ASSERT(member_tvb);
affdba
 
affdba
-	if (check_offset_length_no_exception(member_tvb, abs_offset - composite->start_offsets[i],
affdba
-				(gint) abs_length, &member_offset, &member_length, NULL)) {
affdba
-
affdba
+	exception = check_offset_length_no_exception(member_tvb, abs_offset - composite->start_offsets[i],
affdba
+				(gint) abs_length, &member_offset, &member_length);
affdba
+				
affdba
+	if (!exception) {
affdba
 		DISSECTOR_ASSERT(!tvb->real_data);
affdba
 		return tvb_memcpy(member_tvb, target, member_offset, member_length);
affdba
 	}
affdba
@@ -1077,9 +1080,9 @@ composite_memcpy(tvbuff_t *tvb, guint8* target, guint abs_offset, size_t abs_len
affdba
 		 * then iterate across the other member tvb's, copying their portions
affdba
 		 * until we have copied all data.
affdba
 		 */
affdba
-		retval = compute_offset_length(member_tvb, abs_offset - composite->start_offsets[i], -1,
affdba
-				&member_offset, &member_length, NULL);
affdba
-		DISSECTOR_ASSERT(retval);
affdba
+		exception = compute_offset_and_remaining(member_tvb, abs_offset - composite->start_offsets[i],
affdba
+				&member_offset, &member_length);
affdba
+		DISSECTOR_ASSERT(!exception);
affdba
 
affdba
 		/* composite_memcpy() can't handle a member_length of zero.  */
affdba
 		DISSECTOR_ASSERT(member_length);
affdba
@@ -1909,6 +1912,21 @@ tvb_get_bits(tvbuff_t *tvb, const guint bit_offset, const gint no_of_bits, const
affdba
 	return (guint32)_tvb_get_bits64(tvb, bit_offset, no_of_bits);
affdba
 }
affdba
 
affdba
+static gint
affdba
+tvb_find_guint8_generic(tvbuff_t *tvb, guint abs_offset, guint limit, guint8 needle)
affdba
+{
affdba
+	const guint8 *ptr;
affdba
+	const guint8 *result;
affdba
+
affdba
+	ptr = tvb_get_ptr(tvb, abs_offset, limit);
affdba
+
affdba
+	result = (const guint8 *) memchr(ptr, needle, limit);
affdba
+	if (!result)
affdba
+		return -1;
affdba
+
affdba
+	return (gint) ((result - ptr) + abs_offset);
affdba
+}
affdba
+
affdba
 /* Find first occurrence of needle in tvbuff, starting at offset. Searches
affdba
  * at most maxlength number of bytes; if maxlength is -1, searches to
affdba
  * end of tvbuff.
affdba
@@ -1920,16 +1938,15 @@ gint
affdba
 tvb_find_guint8(tvbuff_t *tvb, const gint offset, const gint maxlength, const guint8 needle)
affdba
 {
affdba
 	const guint8 *result;
affdba
-	guint	      abs_offset, junk_length;
affdba
+	guint	      abs_offset;
affdba
 	guint	      tvbufflen;
affdba
 	guint	      limit;
affdba
 
affdba
 	DISSECTOR_ASSERT(tvb && tvb->initialized);
affdba
 
affdba
-	check_offset_length(tvb, offset, 0, &abs_offset, &junk_length);
affdba
+	check_offset_length(tvb, offset, -1, &abs_offset, &tvbufflen);
affdba
 
affdba
 	/* Only search to end of tvbuff, w/o throwing exception. */
affdba
-	tvbufflen = tvb_length_remaining(tvb, abs_offset);
affdba
 	if (maxlength == -1) {
affdba
 		/* No maximum length specified; search to end of tvbuff. */
affdba
 		limit = tvbufflen;
affdba
@@ -1985,16 +2002,15 @@ gint
affdba
 tvb_pbrk_guint8(tvbuff_t *tvb, const gint offset, const gint maxlength, const guint8 *needles, guchar *found_needle)
affdba
 {
affdba
 	const guint8 *result;
affdba
-	guint	      abs_offset, junk_length;
affdba
+	guint	      abs_offset;
affdba
 	guint	      tvbufflen;
affdba
 	guint	      limit;
affdba
 
affdba
 	DISSECTOR_ASSERT(tvb && tvb->initialized);
affdba
 
affdba
-	check_offset_length(tvb, offset, 0, &abs_offset, &junk_length);
affdba
+	check_offset_length(tvb, offset, -1, &abs_offset, &tvbufflen);
affdba
 
affdba
 	/* Only search to end of tvbuff, w/o throwing exception. */
affdba
-	tvbufflen = tvb_length_remaining(tvb, abs_offset);
affdba
 	if (maxlength == -1) {
affdba
 		/* No maximum length specified; search to end of tvbuff. */
affdba
 		limit = tvbufflen;
affdba
@@ -2845,11 +2861,12 @@ static gint
affdba
 _tvb_get_nstringz(tvbuff_t *tvb, const gint offset, const guint bufsize, guint8* buffer, gint *bytes_copied)
affdba
 {
affdba
 	gint     stringlen;
affdba
-	guint    abs_offset, junk_length;
affdba
+	guint    abs_offset;
affdba
 	gint     limit, len;
affdba
 	gboolean decreased_max = FALSE;
affdba
 
affdba
-	check_offset_length(tvb, offset, 0, &abs_offset, &junk_length);
affdba
+	/* Only read to end of tvbuff, w/o throwing exception. */
affdba
+	check_offset_length(tvb, offset, -1, &abs_offset, &len;;
affdba
 
affdba
 	/* There must at least be room for the terminating NUL. */
affdba
 	DISSECTOR_ASSERT(bufsize != 0);
affdba
@@ -2861,9 +2878,6 @@ _tvb_get_nstringz(tvbuff_t *tvb, const gint offset, const guint bufsize, guint8*
affdba
 		return 0;
affdba
 	}
affdba
 
affdba
-	/* Only read to end of tvbuff, w/o throwing exception. */
affdba
-	len = tvb_length_remaining(tvb, abs_offset);
affdba
-
affdba
 	/* check_offset_length() won't throw an exception if we're
affdba
 	 * looking at the byte immediately after the end of the tvbuff. */
affdba
 	if (len == 0) {
affdba
@@ -2984,7 +2998,7 @@ tvb_find_line_end(tvbuff_t *tvb, const gint offset, int len, gint *next_offset,
affdba
 	guchar found_needle = 0;
affdba
 
affdba
 	if (len == -1)
affdba
-		len = tvb_length_remaining(tvb, offset);
affdba
+		len = tvb_captured_length_remaining(tvb, offset);
affdba
 	/*
affdba
 	 * XXX - what if "len" is still -1, meaning "offset is past the
affdba
 	 * end of the tvbuff"?
affdba
@@ -3100,7 +3114,7 @@ tvb_find_line_end_unquoted(tvbuff_t *tvb, const gint offset, int len, gint *next
affdba
 	int      linelen;
affdba
 
affdba
 	if (len == -1)
affdba
-		len = tvb_length_remaining(tvb, offset);
affdba
+		len = tvb_captured_length_remaining(tvb, offset);
affdba
 	/*
affdba
 	 * XXX - what if "len" is still -1, meaning "offset is past the
affdba
 	 * end of the tvbuff"?
affdba
@@ -3238,7 +3252,7 @@ tvb_skip_wsp(tvbuff_t *tvb, const gint offset, const gint maxlength)
affdba
 	guint8 tempchar;
affdba
 
affdba
 	/* Get the length remaining */
affdba
-	tvb_len = tvb_length(tvb);
affdba
+	tvb_len = tvb_captured_length(tvb);
affdba
 	end     = offset + maxlength;
affdba
 	if (end >= tvb_len)
affdba
 	{
affdba
@@ -3310,7 +3324,7 @@ tvb_bcd_dig_to_ep_str(tvbuff_t *tvb, const gint offset, const gint len, dgt_set_
affdba
 		dgt = &Dgt1_9_bcd;
affdba
 
affdba
 	if (len == -1) {
affdba
-		length = tvb_length(tvb);
affdba
+		length = tvb_captured_length(tvb);
affdba
 		if (length < offset) {
affdba
 			return "";
affdba
 		}
affdba
diff --git a/epan/tvbuff.h b/epan/tvbuff.h
affdba
index 8950feb..f815c1f 100644
affdba
--- a/epan/tvbuff.h
affdba
+++ b/epan/tvbuff.h
affdba
@@ -213,16 +213,28 @@ WS_DLL_PUBLIC tvbuff_t* tvb_new_composite(void);
affdba
 WS_DLL_PUBLIC void tvb_composite_finalize(tvbuff_t* tvb);
affdba
 
affdba
 
affdba
-/* Get total length of buffer */
affdba
-WS_DLL_PUBLIC guint tvb_length(const tvbuff_t*);
affdba
+/* Get amount of captured data in the buffer (which is *NOT* necessarily the
affdba
+ *  * length of the packet). You probably want tvb_reported_length instead. */
affdba
+WS_DLL_PUBLIC guint tvb_captured_length(const tvbuff_t *tvb);
affdba
+
affdba
+/* DEPRECATED, do not use in new code, call tvb_captured_length directly! */
affdba
+#define tvb_length tvb_captured_length
affdba
 
affdba
 /** Computes bytes to end of buffer, from offset (which can be negative,
affdba
- * to indicate bytes from end of buffer). Function returns -1 to
affdba
- * indicate that offset is out of bounds. No exception is thrown. */
affdba
-WS_DLL_PUBLIC gint tvb_length_remaining(const tvbuff_t*, const gint offset);
affdba
+ * to indicate bytes from end of buffer). Function returns 0 if offset is out
affdba
+ * of bounds. No exception is thrown. */
affdba
+WS_DLL_PUBLIC gint tvb_captured_length_remaining(const tvbuff_t *tvb, const gint offset);
affdba
+
affdba
+/* DEPRECATED, do not use in new code, call tvb_captured_length_remaining directly! */
affdba
+#define tvb_length_remaining tvb_captured_length_remaining
affdba
+
affdba
 
affdba
 /** Same as above, but throws an exception if the offset is out of bounds. */
affdba
-WS_DLL_PUBLIC guint tvb_ensure_length_remaining(const tvbuff_t*, const gint offset);
affdba
+WS_DLL_PUBLIC guint tvb_ensure_captured_length_remaining(const tvbuff_t *tvb,
affdba
+    const gint offset);
affdba
+
affdba
+/* DEPRECATED, do not use in new code, call tvb_ensure_captured_length_remaining directly! */
affdba
+#define tvb_ensure_length_remaining tvb_ensure_captured_length_remaining
affdba
 
affdba
 /* Checks (w/o throwing exception) that the bytes referred to by
affdba
  * 'offset'/'length' actually exist in the buffer */
affdba
@@ -240,8 +252,7 @@ WS_DLL_PUBLIC guint tvb_reported_length(const tvbuff_t*);
affdba
 
affdba
 /** Computes bytes of reported packet data to end of buffer, from offset
affdba
  * (which can be negative, to indicate bytes from end of buffer). Function
affdba
- * returns -1 to indicate that offset is out of bounds. No exception is
affdba
- * thrown. */
affdba
+ * returns 0 if offset is out of bounds. No exception is thrown. */
affdba
 WS_DLL_PUBLIC gint tvb_reported_length_remaining(const tvbuff_t *tvb, const gint offset);
affdba
 
affdba
 /** Set the reported length of a tvbuff to a given value; used for protocols