diff options
author | Jakub Zawadzki <darkjames@darkjames.pl> | 2014-07-03 21:24:34 +0200 |
---|---|---|
committer | Anders Broman <a.broman58@gmail.com> | 2014-07-03 21:09:31 +0000 |
commit | 60a6f8582a229fe4c8028656f5fe5a1a319ca7f7 (patch) | |
tree | 259bac019d3b33ab01f68c8714109effb4dcd7a0 /epan/tvbuff.c | |
parent | c62a9051871896a7443e58579ceae93a6966e7d9 (diff) | |
download | wireshark-60a6f8582a229fe4c8028656f5fe5a1a319ca7f7.tar.gz |
Manually revert "Make COMPUTE_OFFSET and COMPUTE_OFFSET_AND_REMAINING macros."
This reverts commit b136182ad41b0552421992f36f3b70fa497fd658
This function are already marked inline, and profiler don't show much difference in performance,
revert as previous version is cleaner.
Change-Id: I1ac2c30a91b46278730ceee127efa086c7fbc6d6
Reviewed-on: https://code.wireshark.org/review/2828
Reviewed-by: Anders Broman <a.broman58@gmail.com>
Diffstat (limited to 'epan/tvbuff.c')
-rw-r--r-- | epan/tvbuff.c | 164 |
1 files changed, 79 insertions, 85 deletions
diff --git a/epan/tvbuff.c b/epan/tvbuff.c index b892ec8b36..1ad77b7cf9 100644 --- a/epan/tvbuff.c +++ b/epan/tvbuff.c @@ -164,59 +164,67 @@ tvb_add_to_chain(tvbuff_t *parent, tvbuff_t *child) } } -#define COMPUTE_OFFSET(tvb, offset, offset_ptr, exception) \ - if (offset >= 0) { \ - /* Positive offset - relative to the beginning of the packet. */ \ - if ((guint) offset <= tvb->length) { \ - *offset_ptr = offset; \ - } else if ((guint) offset <= tvb->reported_length) { \ - exception = BoundsError; \ - } else if (tvb->flags & TVBUFF_FRAGMENT) { \ - exception = FragmentBoundsError; \ - } else { \ - exception = ReportedBoundsError; \ - } \ - } \ - else { \ - /* Negative offset - relative to the end of the packet. */ \ - if ((guint) -offset <= tvb->length) { \ - *offset_ptr = tvb->length + offset; \ - } else if ((guint) -offset <= tvb->reported_length) { \ - exception = BoundsError; \ - } else if (tvb->flags & TVBUFF_FRAGMENT) { \ - exception = FragmentBoundsError; \ - } else { \ - exception = ReportedBoundsError; \ - } \ - } \ - -#define COMPUTE_OFFSET_AND_REMAINING(tvb, offset, offset_ptr, rem_len, exception) \ - if (offset >= 0) { \ - /* Positive offset - relative to the beginning of the packet. */ \ - if ((guint) offset <= tvb->length) { \ - *offset_ptr = offset; \ - } else if ((guint) offset <= tvb->reported_length) { \ - exception = BoundsError; \ - } else if (tvb->flags & TVBUFF_FRAGMENT) { \ - exception = FragmentBoundsError; \ - } else { \ - exception = ReportedBoundsError; \ - } \ - } \ - else { \ - /* Negative offset - relative to the end of the packet. */ \ - if ((guint) -offset <= tvb->length) { \ - *offset_ptr = tvb->length + offset; \ - } else if ((guint) -offset <= tvb->reported_length) { \ - exception = BoundsError; \ - } else if (tvb->flags & TVBUFF_FRAGMENT) { \ - exception = FragmentBoundsError; \ - } else { \ - exception = ReportedBoundsError; \ - } \ - } \ - if (!exception) \ - rem_len = tvb->length - *offset_ptr; \ +/* + * Check whether that offset goes more than one byte past the + * end of the buffer. + * + * If not, return 0; otherwise, return exception + */ +static inline int +validate_offset(const tvbuff_t *tvb, const guint abs_offset) +{ + if (G_LIKELY(abs_offset <= tvb->length)) + return 0; + else if (abs_offset <= tvb->reported_length) + return BoundsError; + else if (tvb->flags & TVBUFF_FRAGMENT) + return FragmentBoundsError; + else + return ReportedBoundsError; +} + +static inline int +compute_offset(const tvbuff_t *tvb, const gint offset, guint *offset_ptr) +{ + if (offset >= 0) { + /* Positive offset - relative to the beginning of the packet. */ + if ((guint) offset <= tvb->length) { + *offset_ptr = offset; + } else if ((guint) offset <= tvb->reported_length) { + return BoundsError; + } else if (tvb->flags & TVBUFF_FRAGMENT) { + return FragmentBoundsError; + } else { + return ReportedBoundsError; + } + } + else { + /* Negative offset - relative to the end of the packet. */ + if ((guint) -offset <= tvb->length) { + *offset_ptr = tvb->length + offset; + } else if ((guint) -offset <= tvb->reported_length) { + return BoundsError; + } else if (tvb->flags & TVBUFF_FRAGMENT) { + return FragmentBoundsError; + } else { + return ReportedBoundsError; + } + } + + return 0; +} + +static inline int +compute_offset_and_remaining(const tvbuff_t *tvb, const gint offset, guint *offset_ptr, guint *rem_len) +{ + int exception; + + exception = compute_offset(tvb, offset, offset_ptr); + if (!exception) + *rem_len = tvb->length - *offset_ptr; + + return exception; +} /* Computes the absolute offset and length based on a possibly-negative offset * and a length that is possible -1 (which means "to the end of the data"). @@ -237,13 +245,13 @@ check_offset_length_no_exception(const tvbuff_t *tvb, guint *offset_ptr, guint *length_ptr) { guint end_offset; - int exception = 0; + int exception; DISSECTOR_ASSERT(offset_ptr); DISSECTOR_ASSERT(length_ptr); /* Compute the offset */ - COMPUTE_OFFSET(tvb, offset, offset_ptr, exception); + exception = compute_offset(tvb, offset, offset_ptr); if (exception) return exception; @@ -269,21 +277,7 @@ check_offset_length_no_exception(const tvbuff_t *tvb, if (end_offset < *offset_ptr) return BoundsError; - /* - * Check whether that offset goes more than one byte past the - * end of the buffer. - * - * If not, return 0; otherwise, return exception - */ - if (G_LIKELY(end_offset <= tvb->length)) - return 0; - else if (end_offset <= tvb->reported_length) - return BoundsError; - else if (tvb->flags & TVBUFF_FRAGMENT) - return FragmentBoundsError; - else - return ReportedBoundsError; - + return validate_offset(tvb, end_offset); } /* Checks (+/-) offset and length and throws an exception if @@ -434,9 +428,9 @@ static inline gint _tvb_captured_length_remaining(const tvbuff_t *tvb, const gint offset) { guint abs_offset, rem_length; - int exception = 0; + int exception; - COMPUTE_OFFSET_AND_REMAINING(tvb, offset, &abs_offset, rem_length, exception); + exception = compute_offset_and_remaining(tvb, offset, &abs_offset, &rem_length); if (exception) return 0; @@ -447,11 +441,11 @@ gint tvb_captured_length_remaining(const tvbuff_t *tvb, const gint offset) { guint abs_offset, rem_length; - int exception = 0; + int exception; DISSECTOR_ASSERT(tvb && tvb->initialized); - COMPUTE_OFFSET_AND_REMAINING(tvb, offset, &abs_offset, rem_length, exception); + exception = compute_offset_and_remaining(tvb, offset, &abs_offset, &rem_length); if (exception) return 0; @@ -462,11 +456,11 @@ guint tvb_ensure_captured_length_remaining(const tvbuff_t *tvb, const gint offset) { guint abs_offset, rem_length; - int exception = 0; + int exception; DISSECTOR_ASSERT(tvb && tvb->initialized); - COMPUTE_OFFSET_AND_REMAINING(tvb, offset, &abs_offset, rem_length, exception); + exception = compute_offset_and_remaining(tvb, offset, &abs_offset, &rem_length); if (exception) THROW(exception); @@ -585,19 +579,19 @@ tvb_ensure_bytes_exist(const tvbuff_t *tvb, const gint offset, const gint length gboolean tvb_offset_exists(const tvbuff_t *tvb, const gint offset) { - guint offset_ptr; - int exception = 0; + guint abs_offset; + int exception; DISSECTOR_ASSERT(tvb && tvb->initialized); - COMPUTE_OFFSET(tvb, offset, &offset_ptr, exception); + exception = compute_offset(tvb, offset, &abs_offset); if (exception) return FALSE; /* compute_offset only throws an exception on >, not >= because of the * comment above check_offset_length_no_exception, but here we want the * opposite behaviour so we check ourselves... */ - if (offset_ptr < tvb->length) { + if (abs_offset < tvb->length) { return TRUE; } else { @@ -616,17 +610,17 @@ tvb_reported_length(const tvbuff_t *tvb) gint tvb_reported_length_remaining(const tvbuff_t *tvb, const gint offset) { - guint offset_ptr; - int exception = 0; + guint abs_offset; + int exception; DISSECTOR_ASSERT(tvb && tvb->initialized); - COMPUTE_OFFSET(tvb, offset, &offset_ptr, exception); + exception = compute_offset(tvb, offset, &abs_offset); if (exception) return 0; - if (tvb->reported_length >= offset_ptr) - return tvb->reported_length - offset_ptr; + if (tvb->reported_length >= abs_offset) + return tvb->reported_length - abs_offset; else return 0; } |