diff options
author | etxrab <etxrab@f5534014-38df-0310-8fa8-9805f1628bb7> | 2011-09-26 15:11:14 +0000 |
---|---|---|
committer | etxrab <etxrab@f5534014-38df-0310-8fa8-9805f1628bb7> | 2011-09-26 15:11:14 +0000 |
commit | 9c526561bbac6c9f6f043c8dc800e56aea1aeb4f (patch) | |
tree | 6c939077945eb170eed1ef6dadbe542c0e193748 /epan | |
parent | 491c4179c5db77adc9e6bed2dc761c95e8533733 (diff) |
Move tvb_new_octet_aligned() to tvbuff.c and use it.
git-svn-id: http://anonsvn.wireshark.org/wireshark/trunk@39150 f5534014-38df-0310-8fa8-9805f1628bb7
Diffstat (limited to 'epan')
-rw-r--r-- | epan/dissectors/packet-per.c | 87 | ||||
-rw-r--r-- | epan/dissectors/packet-per.h | 1 | ||||
-rw-r--r-- | epan/dissectors/packet-rrc.c | 4 | ||||
-rw-r--r-- | epan/dissectors/packet-umts_mac.c | 48 | ||||
-rw-r--r-- | epan/libwireshark.def | 6 | ||||
-rw-r--r-- | epan/tvbuff.c | 48 | ||||
-rw-r--r-- | epan/tvbuff.h | 7 |
7 files changed, 61 insertions, 140 deletions
diff --git a/epan/dissectors/packet-per.c b/epan/dissectors/packet-per.c index 8c815e62b4..24822fb36a 100644 --- a/epan/dissectors/packet-per.c +++ b/epan/dissectors/packet-per.c @@ -180,91 +180,6 @@ static tvbuff_t *new_octet_aligned_subset(tvbuff_t *tvb, guint32 offset, asn1_ct return sub_tvb; } -static const guint16 bit_mask16_unaligned[] = { - 0xff00, - 0x8000, - 0xc000, - 0xe000, - 0xf000, - 0xf800, - 0xfc00, - 0xfe00 -}; - -/* Fetch the number of bits left adjusted to a new tvb with a size of the nearest number of bytes. - * pad the remaining bits with 0 - */ -tvbuff_t *new_octet_aligned_subset_bits(tvbuff_t *tvb, guint32 boffset, asn1_ctx_t *actx, guint32 no_of_bits) -{ - tvbuff_t *sub_tvb = NULL; - /* offset is offset in bytes boffset is in bits */ - guint32 offset = boffset >> 3; - unsigned int i, shift0, shift1; - guint8 octet0, octet1, *buf; - guint16 word; - - guint32 new_length, check_length; - guint32 remainderval, tvb_bits; - - /* Calculate the size required */ - - new_length = no_of_bits/8; - remainderval = no_of_bits % 8; - - if(remainderval){ - new_length++; - }else{ - /* Number of bits = even number of octets */ - return new_octet_aligned_subset(tvb, boffset, actx, new_length); - } - - /* The bits can be contained in two "extra octets" .... .xxx [xxxx]*n xx... ....*/ - tvb_bits = (boffset & 7)+ no_of_bits; - check_length = tvb_bits/8; - if(tvb_bits % 8){ - check_length++; - } - - - /* Throw an exception if we're asked to display more bits than exist. - * We check now to ensure we don't cause g_malloc() to abort because - * we asked for entirely too much memory. - */ - if (new_length > check_length) - THROW(ReportedBoundsError); /* indicate that the packet is malformed */ - tvb_ensure_bytes_exist(tvb, offset, check_length); - buf = g_malloc(new_length); - - i = 0; - shift1 = boffset & 0x07; - shift0 = 8 - shift1; - - if (new_length > 1){ - octet0 = tvb_get_guint8(tvb, offset); - for (; i < new_length-1; i++) { - octet1 = octet0; - octet0 = tvb_get_guint8(tvb, offset + i + 1); - buf[i] = (octet1 << shift1) | (octet0 >> shift0); - } - } - /* get the 'odd' bits */ - if ((no_of_bits - 8*i) > shift0){ - word = tvb_get_ntohs(tvb,offset+i) << shift1; - }else{ - word = tvb_get_guint8(tvb,offset+i) << (shift1 + 8); - } - word = word & bit_mask16_unaligned[remainderval]; - word = word >> 8; - buf[i] = (guint8) (word & 0x00ff); - - sub_tvb = tvb_new_child_real_data(tvb, buf, new_length, new_length); - tvb_set_free_cb(sub_tvb, g_free); - add_new_data_source(actx->pinfo, sub_tvb, "Bitstring tvb"); - - return sub_tvb; -} - - /* 10 Encoding procedures -------------------------------------------------- */ /* 10.2 Open type fields --------------------------------------------------- */ @@ -2065,7 +1980,7 @@ static tvbuff_t *dissect_per_bit_string_display(tvbuff_t *tvb, guint32 offset, a guint32 pad_length=0; guint64 value; - out_tvb = new_octet_aligned_subset_bits(tvb, offset, actx, length); + out_tvb = tvb_new_octet_aligned(tvb, offset, length); if (hfi) { actx->created_item = proto_tree_add_item(tree, hf_index, out_tvb, 0, -1, FALSE); diff --git a/epan/dissectors/packet-per.h b/epan/dissectors/packet-per.h index 9bf5fb547c..54e0abd2c1 100644 --- a/epan/dissectors/packet-per.h +++ b/epan/dissectors/packet-per.h @@ -132,5 +132,4 @@ extern gboolean get_size_constraint_from_stack(asn1_ctx_t *actx, const gchar *na extern guint32 dissect_per_length_determinant(tvbuff_t *tvb, guint32 offset, asn1_ctx_t *actx _U_, proto_tree *tree, int hf_index, guint32 *length); -extern tvbuff_t *new_octet_aligned_subset_bits(tvbuff_t *tvb, guint32 offset, asn1_ctx_t *actx, guint32 no_of_bits); #endif /* __PACKET_PER_H__ */ diff --git a/epan/dissectors/packet-rrc.c b/epan/dissectors/packet-rrc.c index c7c018f9f3..81d85879a2 100644 --- a/epan/dissectors/packet-rrc.c +++ b/epan/dissectors/packet-rrc.c @@ -49215,7 +49215,7 @@ dissect_rrc_T_single_GSM_Message_r3(tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_ bits_remaining = 8*tvb_length(tvb) - offset; whole_octets_remaining = bits_remaining / 8; - gsm_message_tvb = new_octet_aligned_subset_bits(tvb, offset, actx, 8*whole_octets_remaining); + gsm_message_tvb = tvb_new_octet_aligned(tvb, offset, 8*whole_octets_remaining); if (gsm_message_tvb) call_dissector(gsm_a_dtap_handle,gsm_message_tvb,actx->pinfo, tree); @@ -49524,7 +49524,7 @@ dissect_rrc_T_single_GSM_Message_r6(tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_ bits_remaining = 8*tvb_length(tvb) - offset; whole_octets_remaining = bits_remaining / 8; - gsm_message_tvb = new_octet_aligned_subset_bits(tvb, offset, actx, 8*whole_octets_remaining); + gsm_message_tvb = tvb_new_octet_aligned(tvb, offset, 8*whole_octets_remaining); if (gsm_message_tvb) call_dissector(gsm_a_dtap_handle,gsm_message_tvb,actx->pinfo, tree); diff --git a/epan/dissectors/packet-umts_mac.c b/epan/dissectors/packet-umts_mac.c index a87fe9b06e..5c719f78ba 100644 --- a/epan/dissectors/packet-umts_mac.c +++ b/epan/dissectors/packet-umts_mac.c @@ -104,54 +104,6 @@ static const value_string mac_logical_channel_vals[] = { { 0, NULL } }; -static tvbuff_t * -tvb_new_octet_aligned(tvbuff_t *tvb, guint32 bit_offset, gint32 no_of_bits) -{ - tvbuff_t *sub_tvb = NULL; - guint32 byte_offset; - gint32 datalen, i; - guint8 left, right, *buf; - const guint8 *data; - - byte_offset = bit_offset >> 3; - left = bit_offset % 8; /* for left-shifting */ - right = 8 - left; /* for right-shifting */ - - if (no_of_bits == -1) { - datalen = tvb_length_remaining(tvb, byte_offset); - } else { - datalen = no_of_bits >> 3; - if (no_of_bits % 8) datalen++; - } - - /* already aligned -> shortcut */ - if (left == 0) { - return tvb_new_subset(tvb, byte_offset, datalen, -1); - } - - buf = ep_alloc0(datalen); - - /* if at least one trailing byte is available, we must use the content - * of that byte for the last shift (i.e. tvb_get_ptr() must use datalen + 1 - * if non extra byte is available, the last shifted byte requires - * special treatment - */ - if (tvb_length_remaining(tvb, byte_offset) > datalen) { - data = tvb_get_ptr(tvb, byte_offset, datalen + 1); - } else { - data = tvb_get_ptr(tvb, byte_offset, datalen); - datalen--; /* correct 'datalen' for 'for' loop */ - buf[datalen] = data[datalen] << left; /* set last octet */ - } - /* shift tvb data bit_offset bits to the left */ - for (i = 0; i < datalen; i++) - buf[i] = (data[i] << left) | (data[i+1] >> right); - - sub_tvb = tvb_new_real_data(buf, datalen, datalen); - tvb_set_child_real_data_tvbuff(tvb, sub_tvb); - - return sub_tvb; -} static guint8 fach_fdd_tctf(guint8 hdr, guint16 *bit_offs) { diff --git a/epan/libwireshark.def b/epan/libwireshark.def index 1a3933a22a..62fa47fece 100644 --- a/epan/libwireshark.def +++ b/epan/libwireshark.def @@ -655,7 +655,6 @@ md5_hmac md5_init mtp3_addr_to_str_buf mtp3_service_indicator_code_short_vals DATA -new_octet_aligned_subset_bits new_create_dissector_handle new_register_dissector next_tvb_init @@ -1144,12 +1143,13 @@ tvb_ip6_to_str tvb_ip_to_str tvb_length tvb_length_remaining -tvb_new_composite tvb_memcpy tvb_memdup tvb_memeql -tvb_new_real_data tvb_new_child_real_data +tvb_new_composite +tvb_new_octet_aligned +tvb_new_real_data tvb_new_subset tvb_new_subset_remaining tvb_offset_exists diff --git a/epan/tvbuff.c b/epan/tvbuff.c index 0862ccf0e6..6e36c23397 100644 --- a/epan/tvbuff.c +++ b/epan/tvbuff.c @@ -115,6 +115,54 @@ tvb_new(const tvbuff_type type) return tvb; } +tvbuff_t * +tvb_new_octet_aligned(tvbuff_t *tvb, guint32 bit_offset, gint32 no_of_bits) +{ + tvbuff_t *sub_tvb = NULL; + guint32 byte_offset; + gint32 datalen, i; + guint8 left, right, *buf; + const guint8 *data; + + byte_offset = bit_offset >> 3; + left = bit_offset % 8; /* for left-shifting */ + right = 8 - left; /* for right-shifting */ + + if (no_of_bits == -1) { + datalen = tvb_length_remaining(tvb, byte_offset); + } else { + datalen = no_of_bits >> 3; + if (no_of_bits % 8) datalen++; + } + + /* already aligned -> shortcut */ + if (left == 0) { + return tvb_new_subset(tvb, byte_offset, datalen, -1); + } + + buf = ep_alloc0(datalen); + + /* if at least one trailing byte is available, we must use the content + * of that byte for the last shift (i.e. tvb_get_ptr() must use datalen + 1 + * if non extra byte is available, the last shifted byte requires + * special treatment + */ + if (tvb_length_remaining(tvb, byte_offset) > datalen) { + data = tvb_get_ptr(tvb, byte_offset, datalen + 1); + } else { + data = tvb_get_ptr(tvb, byte_offset, datalen); + datalen--; /* correct 'datalen' for 'for' loop */ + buf[datalen] = data[datalen] << left; /* set last octet */ + } + /* shift tvb data bit_offset bits to the left */ + for (i = 0; i < datalen; i++) + buf[i] = (data[i] << left) | (data[i+1] >> right); + + sub_tvb = tvb_new_child_real_data(tvb, buf, datalen, datalen); + + return sub_tvb; +} + static tvbuff_t* tvb_new_with_subset(const guint subset_tvb_offset, const guint subset_tvb_length) { diff --git a/epan/tvbuff.h b/epan/tvbuff.h index 5e75bce146..896b2d3042 100644 --- a/epan/tvbuff.h +++ b/epan/tvbuff.h @@ -86,6 +86,13 @@ typedef void (*tvbuff_free_cb_t)(void*); * require further initialization via the appropriate functions */ extern tvbuff_t* tvb_new(tvbuff_type); +/** Extracs from bit offset number of bits and + * Returns a pointer to a newly initialized tvbuff. with the bits + * octet aligned. + */ +extern tvbuff_t* tvb_new_octet_aligned(tvbuff_t *tvb, guint32 bit_offset, gint32 no_of_bits); + + /** Marks a tvbuff for freeing. The guint8* data of a TVBUFF_REAL_DATA * is *never* freed by the tvbuff routines. The tvbuff itself is actually freed * once its usage count drops to 0. |