to_str: scope decode_bits_in_field

Mostly straightforward. The only complication was
proto_tree_add_split_bits_crumb which needed some manipulation to
guarantee a non-null tree so we could use its memory scope.

This is one of the last non-dissector uses of wmem_packet_scope!
This commit is contained in:
Evan Huus 2021-10-04 13:35:37 -04:00 committed by Wireshark GitLab Utility
parent 4661e1ddd9
commit f9ac0f40f7
7 changed files with 24 additions and 15 deletions

View File

@ -3959,7 +3959,7 @@ de_tp_epc_ellipsoid_point_with_alt(tvbuff_t *tvb, proto_tree *tree, packet_info
curr_offset += 3;
longitude = tvb_get_ntoh24(tvb, curr_offset);
proto_tree_add_int_format(tree, hf_gsm_a_dtap_epc_degrees_longitude, tvb, curr_offset, 3, longitude,
"%s = %s: %d", decode_bits_in_field(curr_offset<<3, 24, longitude, ENC_BIG_ENDIAN),
"%s = %s: %d", decode_bits_in_field(pinfo->pool, curr_offset<<3, 24, longitude, ENC_BIG_ENDIAN),
proto_registrar_get_name(hf_gsm_a_dtap_epc_degrees_longitude), longitude-8388608);
curr_offset += 3;
proto_tree_add_bits_item(tree, hf_gsm_a_dtap_epc_altitude_dir, tvb, curr_offset<<3, 1, ENC_BIG_ENDIAN);

View File

@ -1880,7 +1880,7 @@ de_gmm_ms_radio_acc_cap(tvbuff_t *tvb, proto_tree *tree, packet_info *pinfo, gui
/* decode_bits_in_field(gint bit_offset, gint no_of_bits, guint64 value)*/
proto_tree_add_uint_format(tf_tree, hf_gsm_a_gm_rf_power_capability, tvb, curr_offset-1-add_octets, 1+add_octets, value,
"%s RF Power Capability, GMSK Power Class: %s (%u)", decode_bits_in_field(bit_offset, 3, value, ENC_BIG_ENDIAN), str, value);
"%s RF Power Capability, GMSK Power Class: %s (%u)", decode_bits_in_field(pinfo->pool, bit_offset, 3, value, ENC_BIG_ENDIAN), str, value);
bit_offset += 3;
curr_bits_length -= bits_needed;
oct <<= bits_needed;
@ -1948,7 +1948,7 @@ de_gmm_ms_radio_acc_cap(tvbuff_t *tvb, proto_tree *tree, packet_info *pinfo, gui
str = "Not specified??";
proto_tree_add_uint_format(tf_tree, hf_gsm_a_gm_rf_power_capability, tvb, curr_offset-1-add_octets, 1+add_octets, value,
"%s RF Power Capability, GMSK Power Class: %s (%u)", decode_bits_in_field(bit_offset, 3, value, ENC_BIG_ENDIAN), str, value);
"%s RF Power Capability, GMSK Power Class: %s (%u)", decode_bits_in_field(pinfo->pool, bit_offset, 3, value, ENC_BIG_ENDIAN), str, value);
bit_offset += 3;
curr_bits_length -= bits_needed;

View File

@ -1599,7 +1599,7 @@ static void dissect_channel_list_n_range(tvbuff_t *tvb, proto_tree *tree, packet
for (i=1; i<=imax; i++) {
w[i] = (gint) tvb_get_bits(tvb, bit_offset, wsize, FALSE);
proto_tree_add_bytes_format(subtree, hf_gsm_a_rr_w_elements, tvb, bit_offset>>3, ((bit_offset+wsize-1)>>3) - (bit_offset>>3) + 1 , NULL, "%s W(%d): %d",
decode_bits_in_field(bit_offset, wsize, w[i], ENC_BIG_ENDIAN),
decode_bits_in_field(pinfo->pool, bit_offset, wsize, w[i], ENC_BIG_ENDIAN),
i,
w[i]);
bit_offset += wsize;

View File

@ -1334,7 +1334,7 @@ DEBUG_ENTRY("dissect_per_constrained_integer");
val = (guint32)tvb_get_bits64(tvb,offset,num_bits,ENC_BIG_ENDIAN);
if (display_internal_per_fields){
str = decode_bits_in_field((offset&0x07),num_bits,val,ENC_BIG_ENDIAN);
str = decode_bits_in_field(actx->pinfo->pool, (offset&0x07),num_bits,val,ENC_BIG_ENDIAN);
proto_tree_add_uint(tree, hf_per_internal_min, tvb, val_start,val_length, min);
proto_tree_add_uint64(tree, hf_per_internal_range, tvb, val_start, val_length, range);
proto_tree_add_uint(tree, hf_per_internal_num_bits, tvb, val_start, val_length, num_bits);
@ -2134,7 +2134,7 @@ static tvbuff_t *dissect_per_bit_string_display(tvbuff_t *tvb, guint32 offset, a
value = tvb_get_bits64(out_tvb, 0, length, ENC_BIG_ENDIAN);
}
proto_item_append_text(actx->created_item, ", %s decimal value %" G_GINT64_MODIFIER "u",
decode_bits_in_field(0, length, value, ENC_BIG_ENDIAN), value);
decode_bits_in_field(actx->pinfo->pool, 0, length, value, ENC_BIG_ENDIAN), value);
if (named_bits) {
const guint32 named_bits_bytelen = (num_named_bits + 7) / 8;
proto_tree *subtree = proto_item_add_subtree(actx->created_item, ett_per_named_bits);

View File

@ -12309,7 +12309,7 @@ _proto_tree_add_bits_ret_val(proto_tree *tree, const int hfindex, tvbuff_t *tvb,
CHECK_FOR_NULL_TREE(tree);
TRY_TO_FAKE_THIS_ITEM(tree, hfindex, hf_field);
bf_str = decode_bits_in_field(bit_offset, no_of_bits, value, encoding);
bf_str = decode_bits_in_field(PNODE_POOL(tree), bit_offset, no_of_bits, value, encoding);
switch (hf_field->type) {
case FT_BOOLEAN:
@ -12568,13 +12568,22 @@ proto_tree_add_split_bits_crumb(proto_tree *tree, const int hfindex, tvbuff_t *t
const crumb_spec_t *crumb_spec, guint16 crumb_index)
{
header_field_info *hfinfo;
gint start = bit_offset >> 3;
gint length = ((bit_offset + crumb_spec[crumb_index].crumb_bit_length - 1) >> 3) - (bit_offset >> 3) + 1;
/* We have to duplicate this length check from proto_tree_add_text_internal in order to check for a null tree
* so that we can use the tree's memory scope in calculating the string */
if (length == -1) {
tvb_captured_length(tvb) ? tvb_ensure_captured_length_remaining(tvb, start) : 0;
} else {
tvb_ensure_bytes_exist(tvb, start, length);
}
if (!tree) return;
PROTO_REGISTRAR_GET_NTH(hfindex, hfinfo);
proto_tree_add_text_internal(tree, tvb,
bit_offset >> 3,
((bit_offset + crumb_spec[crumb_index].crumb_bit_length - 1) >> 3) - (bit_offset >> 3) + 1,
proto_tree_add_text_internal(tree, tvb, start, length,
"%s crumb %d of %s (decoded above)",
decode_bits_in_field(bit_offset, crumb_spec[crumb_index].crumb_bit_length,
decode_bits_in_field(PNODE_POOL(tree), bit_offset, crumb_spec[crumb_index].crumb_bit_length,
tvb_get_bits(tvb,
bit_offset,
crumb_spec[crumb_index].crumb_bit_length,
@ -12648,7 +12657,7 @@ _proto_tree_add_bits_format_value(proto_tree *tree, const int hfindex,
return NULL;
}
str = decode_bits_in_field(bit_offset, no_of_bits, value, encoding);
str = decode_bits_in_field(PNODE_POOL(tree), bit_offset, no_of_bits, value, encoding);
(void) g_strlcat(str, " = ", 256+64);
(void) g_strlcat(str, hf_field->name, 256+64);

View File

@ -645,7 +645,7 @@ rel_time_to_secs_str(wmem_allocator_t *scope, const nstime_t *rel_time)
*/
char *
decode_bits_in_field(const guint bit_offset, const gint no_of_bits, const guint64 value, const guint encoding)
decode_bits_in_field(wmem_allocator_t *scope, const guint bit_offset, const gint no_of_bits, const guint64 value, const guint encoding)
{
guint64 mask;
char *str;
@ -667,7 +667,7 @@ decode_bits_in_field(const guint bit_offset, const gint no_of_bits, const guint6
}
/* Prepare the string, 256 pos for the bits and zero termination, + 64 for the spaces */
str=(char *)wmem_alloc0(wmem_packet_scope(), 256+64);
str=(char *)wmem_alloc0(scope, 256+64);
for(bit=0;bit<no_leading_dots;bit++){
if(bit&&(!(bit%4))){
str[str_p] = ' ';

View File

@ -149,7 +149,7 @@ WS_DLL_PUBLIC gchar *guid_to_str_buf(const e_guid_t *, gchar *, int);
WS_DLL_PUBLIC gchar *guid_to_str(wmem_allocator_t *scope, const e_guid_t *);
WS_DLL_PUBLIC char *decode_bits_in_field(const guint bit_offset, const gint no_of_bits, const guint64 value, const guint encoding);
WS_DLL_PUBLIC char *decode_bits_in_field(wmem_allocator_t *scope, const guint bit_offset, const gint no_of_bits, const guint64 value, const guint encoding);
#ifdef __cplusplus
}