diff options
author | Hadriel Kaplan <hadrielk@yahoo.com> | 2014-04-12 23:20:15 -0400 |
---|---|---|
committer | Anders Broman <a.broman58@gmail.com> | 2014-04-17 14:04:19 +0000 |
commit | f52626cc83fa6362a286f3ffbda1d3d67392ac3f (patch) | |
tree | 3db5bc96ff71bdab2ebc881b3a54d70e868acc3f /epan/tvbuff.c | |
parent | c49be78f4e34124b64479aaaf6877f5839374305 (diff) |
Add tvb_get and proto_tree_add for string-encoded byte arrays
This commit adds tvb_get_string_bytes and proto_tree_add_bytes_item routines for
getting GByteArrays fields from the tvb when they are encoded in ASCII hex string form.
The proto_tree_add_bytes_item routine is also usable for normal
binary encoded byte arrays, and has the advantage of retrieving
the array values even if there's no proto tree.
It also exposes the routines to Lua, both so that a Lua script can take
advantage of this, but also so I can write a testsuite to test the functions.
Change-Id: I112a038653df6482a5d0ebe7c95708f207319e20
Reviewed-on: https://code.wireshark.org/review/1158
Reviewed-by: Hadriel Kaplan <hadrielk@yahoo.com>
Reviewed-by: Anders Broman <a.broman58@gmail.com>
Diffstat (limited to 'epan/tvbuff.c')
-rw-r--r-- | epan/tvbuff.c | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/epan/tvbuff.c b/epan/tvbuff.c index 668d1c0f8b..8612921bd5 100644 --- a/epan/tvbuff.c +++ b/epan/tvbuff.c @@ -1307,6 +1307,36 @@ validate_single_byte_ascii_encoding(const guint encoding) REPORT_DISSECTOR_BUG("No string encoding type passed to tvb_get_string_XXX"); } +GByteArray* +tvb_get_string_bytes(tvbuff_t *tvb, const gint offset, const gint length, + const guint encoding, GByteArray *bytes, gint *endoff) +{ + const gchar *ptr = (gchar*) tvb_get_raw_string(wmem_packet_scope(), tvb, offset, length); + const gchar *begin = ptr; + const gchar *end = NULL; + GByteArray* retval = NULL; + + errno = EDOM; + + validate_single_byte_ascii_encoding(encoding); + + if (endoff) *endoff = 0; + + while (*begin == ' ') begin++; + + if (*begin && bytes) { + if (hex_str_to_bytes_encoding(begin, bytes, &end, encoding, FALSE)) { + if (bytes->len > 0) { + if (endoff) *endoff = offset + (gint)(end - ptr); + errno = 0; + retval = bytes; + } + } + } + + return retval; +} + /* converts a broken down date representation, relative to UTC, * to a timestamp; it uses timegm() if it's available. * Copied from Glib source gtimer.c |