aboutsummaryrefslogtreecommitdiffstats
path: root/epan/tvbuff.c
diff options
context:
space:
mode:
authorMichael Mann <mmann78@netscape.net>2018-12-02 22:54:12 -0500
committerAnders Broman <a.broman58@gmail.com>2018-12-09 07:17:21 +0000
commitdf9378ed3f8ad283efb4f8562bcb7a23cf2b2152 (patch)
tree8d3cb30d81a8eb989d4078e5f4ce6f88fd032765 /epan/tvbuff.c
parent0b79b9c2db3581191dd4d6a9a6e65e40f0471657 (diff)
Add tvb_get_token_len
This is intended to be a replacement for get_token_len (from strutil.h) when its used on a tvb. It should be a little safer and remove the need for a dissector to use tvb_get_ptr. Change-Id: Ib2d4a79718b6fba4eb9acc0129b13be6c8199a43 Reviewed-on: https://code.wireshark.org/review/30892 Petri-Dish: Michael Mann <mmann78@netscape.net> Tested-by: Petri Dish Buildbot Reviewed-by: Anders Broman <a.broman58@gmail.com>
Diffstat (limited to 'epan/tvbuff.c')
-rw-r--r--epan/tvbuff.c67
1 files changed, 67 insertions, 0 deletions
diff --git a/epan/tvbuff.c b/epan/tvbuff.c
index 9d3ca4b82f..110c437437 100644
--- a/epan/tvbuff.c
+++ b/epan/tvbuff.c
@@ -3705,6 +3705,73 @@ tvb_skip_guint8(tvbuff_t *tvb, int offset, const int maxlength, const guint8 ch)
return offset;
}
+static ws_mempbrk_pattern pbrk_whitespace;
+
+int tvb_get_token_len(tvbuff_t *tvb, const gint offset, int len, gint *next_offset, const gboolean desegment)
+{
+ gint eob_offset;
+ gint eot_offset;
+ int tokenlen;
+ guchar found_needle = 0;
+ static gboolean compiled = FALSE;
+
+ DISSECTOR_ASSERT(tvb && tvb->initialized);
+
+ if (len == -1) {
+ len = _tvb_captured_length_remaining(tvb, offset);
+ /* if offset is past the end of the tvbuff, len is now 0 */
+ }
+
+ eob_offset = offset + len;
+
+ if (!compiled) {
+ ws_mempbrk_compile(&pbrk_whitespace, " \r\n");
+ compiled = TRUE;
+ }
+
+ /*
+ * Look either for a space, CR, or LF.
+ */
+ eot_offset = tvb_ws_mempbrk_pattern_guint8(tvb, offset, len, &pbrk_whitespace, &found_needle);
+ if (eot_offset == -1) {
+ /*
+ * No space, CR or LF - token is presumably continued in next packet.
+ */
+ if (desegment) {
+ /*
+ * Tell our caller we saw no whitespace, so they can
+ * try to desegment and get the entire line
+ * into one tvbuff.
+ */
+ return -1;
+ }
+ else {
+ /*
+ * Pretend the token runs to the end of the tvbuff.
+ */
+ tokenlen = eob_offset - offset;
+ if (next_offset)
+ *next_offset = eob_offset;
+ }
+ }
+ else {
+ /*
+ * Find the number of bytes between the starting offset
+ * and the space, CR or LF.
+ */
+ tokenlen = eot_offset - offset;
+
+ /*
+ * Return the offset of the character after the last
+ * character in the line, skipping over the last character
+ * in the line terminator.
+ */
+ if (next_offset)
+ *next_offset = eot_offset + 1;
+ }
+ return tokenlen;
+}
+
/*
* Format a bunch of data from a tvbuff as bytes, returning a pointer
* to the string with the formatted data, with "punct" as a byte