From 25a254823f21dee60f621b445e6689b2bbf9d9c4 Mon Sep 17 00:00:00 2001 From: Guy Harris Date: Wed, 16 Jun 2021 02:01:23 -0700 Subject: [PATCH] wsutil: add a header that defines some "round to power of 2" macros. Add macros to round to multiples of 2, 4, 8, 16, and 32. Use them instead of independently defined macros. (We don't define a general "round to a power of 2" macro to avoid the risk of somebody passing something other than a power of 2 to it.) --- epan/dissectors/packet-aeron.c | 3 ++- epan/dissectors/packet-alljoyn.c | 7 ++++--- epan/dissectors/packet-dbus.c | 3 ++- epan/dissectors/packet-ieee80211.c | 21 +++++++++------------ epan/dissectors/packet-iscsi.c | 3 ++- epan/dissectors/packet-netlink.c | 3 ++- epan/dissectors/packet-nflog.c | 3 ++- epan/dissectors/packet-openflow_v6.c | 7 ++++--- epan/dissectors/packet-ospf.c | 13 +++++++------ epan/dissectors/packet-pathport.c | 4 ++-- epan/dissectors/packet-pvfs2.c | 10 ++++------ epan/dissectors/packet-smb2.c | 5 +++-- epan/dissectors/packet-stun.c | 5 +++-- epan/dissectors/packet-usb.c | 5 +++-- epan/dissectors/packet-yami.c | 11 ++++++----- wiretap/pcapng.c | 3 ++- wiretap/snoop.c | 3 ++- wsutil/CMakeLists.txt | 1 + wsutil/ws_roundup.h | 22 ++++++++++++++++++++++ 19 files changed, 82 insertions(+), 50 deletions(-) create mode 100644 wsutil/ws_roundup.h diff --git a/epan/dissectors/packet-aeron.c b/epan/dissectors/packet-aeron.c index 71cfd1eb24..7adbca6ac4 100644 --- a/epan/dissectors/packet-aeron.c +++ b/epan/dissectors/packet-aeron.c @@ -18,6 +18,7 @@ #include #include #include +#include /* * The Aeron protocol is defined at @@ -55,7 +56,7 @@ typedef struct static int aeron_pos_roundup(int offset) { - return ((offset+31) & (~31)); + return WS_ROUNDUP_32(offset); } static int aeron_pos_compare(const aeron_pos_t * pos1, const aeron_pos_t * pos2) diff --git a/epan/dissectors/packet-alljoyn.c b/epan/dissectors/packet-alljoyn.c index c380e2eab7..e501605e0f 100644 --- a/epan/dissectors/packet-alljoyn.c +++ b/epan/dissectors/packet-alljoyn.c @@ -12,6 +12,7 @@ #include "config.h" #include #include +#include void proto_register_AllJoyn(void); void proto_reg_handoff_AllJoyn(void); @@ -245,9 +246,9 @@ static gint ett_alljoyn_mess_header = -1; static gint ett_alljoyn_mess_body_parameters = -1; static gint ett_alljoyn_ardp = -1; /* This is the top ARDP tree. */ -#define ROUND_TO_2BYTE(len) ((len + 1) & ~1) -#define ROUND_TO_4BYTE(len) ((len + 3) & ~3) -#define ROUND_TO_8BYTE(len) ((len + 7) & ~7) +#define ROUND_TO_2BYTE(len) WS_ROUNDUP_2(len) +#define ROUND_TO_4BYTE(len) WS_ROUNDUP_4(len) +#define ROUND_TO_8BYTE(len) WS_ROUNDUP_8(len) static const value_string endian_encoding_vals[] = { { 'B', "Big endian" }, diff --git a/epan/dissectors/packet-dbus.c b/epan/dissectors/packet-dbus.c index ceccac9720..5ea20a7dab 100644 --- a/epan/dissectors/packet-dbus.c +++ b/epan/dissectors/packet-dbus.c @@ -18,6 +18,7 @@ #include #include #include +#include #include "packet-tcp.h" #define DBUS_MAX_ARRAY_LEN (64 * 1024 * 1024) @@ -1212,7 +1213,7 @@ get_dbus_message_len(packet_info *pinfo _U_, tvbuff_t *tvb, } len_hdr = DBUS_HEADER_LEN + get_guint32(tvb, offset + 12); - len_hdr = (len_hdr + 7) & ~7; + len_hdr = WS_ROUNDUP_8(len_hdr); len_body = get_guint32(tvb, offset + 4); return len_hdr + len_body; diff --git a/epan/dissectors/packet-ieee80211.c b/epan/dissectors/packet-ieee80211.c index 036adff1e9..56681baac5 100644 --- a/epan/dissectors/packet-ieee80211.c +++ b/epan/dissectors/packet-ieee80211.c @@ -53,6 +53,7 @@ #include #include #include +#include #include #include #include @@ -135,10 +136,6 @@ sta_prop_equal_fn(gconstpointer v, gconstpointer w) return memcmp(k1, k2, 6) == 0; /* Compare each address for equality */ } -#ifndef roundup2 -#define roundup2(x, y) (((x)+((y)-1))&(~((y)-1))) /* if y is powers of two */ -#endif - /* bitmask for bits [l..h] * taken from kernel's include/linux/bitops.h */ @@ -8359,7 +8356,7 @@ add_mimo_csi_matrices_report(proto_tree *tree, tvbuff_t *tvb, int offset, mimo_c ns = get_mimo_ns(mimo_cntrl.chan_width, mimo_cntrl.grouping); csi_matrix_size = ns*(3+(2*mimo_cntrl.nc*mimo_cntrl.nr*mimo_cntrl.coefficient_size)); - csi_matrix_size = roundup2(csi_matrix_size, 8) / 8; + csi_matrix_size = WS_ROUNDUP_8(csi_matrix_size) / 8; proto_tree_add_item(snr_tree, hf_ieee80211_ff_mimo_csi_matrices, tvb, offset, csi_matrix_size, ENC_NA); offset += csi_matrix_size; return offset - start_offset; @@ -8387,7 +8384,7 @@ add_mimo_beamforming_feedback_report(proto_tree *tree, tvbuff_t *tvb, int offset ns = get_mimo_ns(mimo_cntrl.chan_width, mimo_cntrl.grouping); csi_matrix_size = ns*(2*mimo_cntrl.nc*mimo_cntrl.nr*mimo_cntrl.coefficient_size); - csi_matrix_size = roundup2(csi_matrix_size, 8) / 8; + csi_matrix_size = WS_ROUNDUP_8(csi_matrix_size) / 8; proto_tree_add_item(snr_tree, hf_ieee80211_ff_mimo_csi_bf_matrices, tvb, offset, csi_matrix_size, ENC_NA); offset += csi_matrix_size; return offset - start_offset; @@ -8430,7 +8427,7 @@ add_mimo_compressed_beamforming_feedback_report(proto_tree *tree, tvbuff_t *tvb, na = get_mimo_na(mimo_cntrl.nr, mimo_cntrl.nc); ns = get_mimo_ns(mimo_cntrl.chan_width, mimo_cntrl.grouping); csi_matrix_size = ns*(na*((mimo_cntrl.codebook_info+1)*2 + 2)/2); - csi_matrix_size = roundup2(csi_matrix_size, 8) / 8; + csi_matrix_size = WS_ROUNDUP_8(csi_matrix_size) / 8; proto_tree_add_item(snr_tree, hf_ieee80211_ff_mimo_csi_cbf_matrices, tvb, offset, csi_matrix_size, ENC_NA); offset += csi_matrix_size; return offset - start_offset; @@ -8495,7 +8492,7 @@ capture_ieee80211_common(const guchar * pd, int offset, int len, * is before the mesh header, possibly because it doesn't * recognize the mesh header. */ - hdr_length = roundup2(hdr_length, 4); + hdr_length = WS_ROUNDUP_4(hdr_length); } /* @@ -14365,7 +14362,7 @@ add_ff_vht_compressed_beamforming_report(proto_tree *tree, tvbuff_t *tvb, packet carry = 1; else carry = 0; - len = roundup2((pos + matrix_size), 8)/8 - roundup2(pos, 8)/8; + len = WS_ROUNDUP_8(pos + matrix_size)/8 - WS_ROUNDUP_8(pos)/8; scidx = vht_compressed_skip_scidx(chan_width, grouping, scidx); /* TODO : For certain values from na_arr, there is always going be a carry over or overflow from the previous or @@ -32555,7 +32552,7 @@ dissect_ieee80211_pv0(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, * is before the mesh header, possibly because it doesn't * recognize the mesh header. */ - hdr_len = roundup2(hdr_len, 4); + hdr_len = WS_ROUNDUP_4(hdr_len); } if (FCF_FRAME_TYPE (fcf) == DATA_FRAME) { @@ -33909,7 +33906,7 @@ dissect_ieee80211_pv0(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, msdu_length = tvb_get_ntohs(next_tvb, msdu_offset+12); parent_item = proto_tree_add_item(mpdu_tree, hf_ieee80211_amsdu_subframe, next_tvb, - msdu_offset, roundup2(msdu_offset+14+msdu_length, 4), ENC_NA); + msdu_offset, WS_ROUNDUP_4(msdu_offset+14+msdu_length), ENC_NA); proto_item_append_text(parent_item, " #%u", i); subframe_tree = proto_item_add_subtree(parent_item, ett_msdu_aggregation_subframe_tree); i += 1; @@ -33929,7 +33926,7 @@ dissect_ieee80211_pv0(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, msdu_offset += 14; msdu_tvb = tvb_new_subset_length(next_tvb, msdu_offset, msdu_length); call_dissector(llc_handle, msdu_tvb, pinfo, subframe_tree); - msdu_offset = roundup2(msdu_offset+msdu_length, 4); + msdu_offset = WS_ROUNDUP_4(msdu_offset+msdu_length); } while (tvb_reported_length_remaining(next_tvb, msdu_offset) > 14); } else { /* I guess some bridges take Netware Ethernet_802_3 frames, diff --git a/epan/dissectors/packet-iscsi.c b/epan/dissectors/packet-iscsi.c index 9a375c55e7..0bb8d31f0a 100644 --- a/epan/dissectors/packet-iscsi.c +++ b/epan/dissectors/packet-iscsi.c @@ -30,6 +30,7 @@ #include #include #include +#include void proto_register_iscsi(void); void proto_reg_handoff_iscsi(void); @@ -1035,7 +1036,7 @@ dissect_iscsi_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, guint off /* strip off padding bytes */ if(ahs_offset & 3){ - ahs_offset=(ahs_offset+3) & ~3; + ahs_offset=WS_ROUNDUP_4(ahs_offset); } } diff --git a/epan/dissectors/packet-netlink.c b/epan/dissectors/packet-netlink.c index 20c1c9f2d7..b3bad498b9 100644 --- a/epan/dissectors/packet-netlink.c +++ b/epan/dissectors/packet-netlink.c @@ -17,6 +17,7 @@ #include #include #include +#include #include "packet-netlink.h" @@ -348,7 +349,7 @@ dissect_netlink_attributes_common(tvbuff_t *tvb, header_field_info *hfi_type, in } /* Assume offset already aligned, next offset is rta_len plus alignment. */ - rta_len = MIN((rta_len + 3) & ~3, data_length); + rta_len = MIN(WS_ROUNDUP_4(rta_len), data_length); offset += rta_len - 4; /* Header was already skipped */ if (data_length < rta_len) THROW(ReportedBoundsError); diff --git a/epan/dissectors/packet-nflog.c b/epan/dissectors/packet-nflog.c index 3f3931c0ff..b60034d125 100644 --- a/epan/dissectors/packet-nflog.c +++ b/epan/dissectors/packet-nflog.c @@ -15,6 +15,7 @@ #include #include #include +#include #include "packet-netlink.h" @@ -280,7 +281,7 @@ dissect_nflog(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U if (tlv_type == WS_NFULA_PAYLOAD) next_tvb = tvb_new_subset_length(tvb, offset + 4, value_len); - offset += ((tlv_len + 3) & ~3); /* next TLV aligned to 4B */ + offset += WS_ROUNDUP_4(tlv_len); /* next TLV aligned to 4B */ } if (next_tvb && hw_protocol) { diff --git a/epan/dissectors/packet-openflow_v6.c b/epan/dissectors/packet-openflow_v6.c index 5cdf7e3251..ba59151bfb 100644 --- a/epan/dissectors/packet-openflow_v6.c +++ b/epan/dissectors/packet-openflow_v6.c @@ -19,6 +19,7 @@ #include #include #include +#include void proto_register_openflow_v6(void); void proto_reg_handoff_openflow_v6(void); @@ -1138,7 +1139,7 @@ dissect_openflow_stats_v6(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tre offset = oxs_end; } - padding = ((stats_length + 7) & ~7) - stats_length; + padding = WS_ROUNDUP_8(stats_length) - stats_length; if (padding) { proto_tree_add_item(tree, hf_openflow_v6_stats_pad, tvb, oxs_end, padding, ENC_NA); offset += padding; @@ -2863,7 +2864,7 @@ dissect_openflow_port_desc_prop_v6(tvbuff_t *tvb, packet_info *pinfo _U_, proto_ while(offset < fields_end) { offset = dissect_openflow_oxm_v6(tvb, pinfo, prop_tree, offset, length); } - offset+=((prop_length + 7) & ~7) - prop_length; + offset+=WS_ROUNDUP_8(prop_length) - prop_length; break; case OFPPDPT_RECIRCULATE: @@ -2872,7 +2873,7 @@ dissect_openflow_port_desc_prop_v6(tvbuff_t *tvb, packet_info *pinfo _U_, proto_ proto_tree_add_item(tree, hf_openflow_v6_port_desc_prop_recirculate_port_no, tvb, offset, 4, ENC_BIG_ENDIAN); offset += 4; } - offset+=((prop_length + 7) & ~7) - prop_length; + offset+=WS_ROUNDUP_8(prop_length) - prop_length; break; case OFPPDPT_EXPERIMENTER: diff --git a/epan/dissectors/packet-ospf.c b/epan/dissectors/packet-ospf.c index 5403c4634d..0785424ed5 100644 --- a/epan/dissectors/packet-ospf.c +++ b/epan/dissectors/packet-ospf.c @@ -62,6 +62,7 @@ #include #include #include +#include #include "packet-rsvp.h" void proto_register_ospf(void); @@ -2811,7 +2812,7 @@ dissect_ospf_lsa_opaque_ri(tvbuff_t *tvb, packet_info *pinfo, int offset, proto_ proto_tree_add_item(stlv_tree, hf_ospf_tlv_value, tvb, stlv_offset + 4, stlv_length, ENC_NA); break; } - stlv_offset += 4 + ((stlv_length + 3) & ~3); + stlv_offset += 4 + WS_ROUNDUP_4(stlv_length); } break; @@ -2863,7 +2864,7 @@ dissect_ospf_lsa_opaque_ri(tvbuff_t *tvb, packet_info *pinfo, int offset, proto_ * RFC 7770, section 2.3: 4-octet aligned, but type, length and padding * is not included in the length. * */ - offset += 4 + ((tlv_length + 3) & ~3); + offset += 4 + WS_ROUNDUP_4(tlv_length); } } @@ -3013,7 +3014,7 @@ dissect_ospf_lsa_ext_prefix(tvbuff_t *tvb, packet_info *pinfo, int offset, proto proto_tree_add_item(stlv_tree, hf_ospf_tlv_value, tvb, stlv_offset + 4, stlv_length, ENC_NA); break; } - stlv_offset += 4 + ((stlv_length + 3) & ~3); + stlv_offset += 4 + WS_ROUNDUP_4(stlv_length); } } @@ -3021,7 +3022,7 @@ dissect_ospf_lsa_ext_prefix(tvbuff_t *tvb, packet_info *pinfo, int offset, proto * RFC 7770, section 2.3: 4-octet aligned, but type, length and padding * is not included in the length. * */ - offset += 4 + ((tlv_length + 3) & ~3); + offset += 4 + WS_ROUNDUP_4(tlv_length); } } @@ -3180,7 +3181,7 @@ dissect_ospf_lsa_ext_link(tvbuff_t *tvb, packet_info *pinfo, int offset, proto_t proto_tree_add_item(stlv_tree, hf_ospf_tlv_value, tvb, stlv_offset + 4, stlv_length, ENC_NA); break; } - stlv_offset += 4 + ((stlv_length + 3) & ~3); + stlv_offset += 4 + WS_ROUNDUP_4(stlv_length); } break; @@ -3203,7 +3204,7 @@ dissect_ospf_lsa_ext_link(tvbuff_t *tvb, packet_info *pinfo, int offset, proto_t * RFC 7770, section 2.3: 4-octet aligned, but type, length and padding * is not included in the length. * */ - offset += 4 + ((tlv_length + 3) & ~3); + offset += 4 + WS_ROUNDUP_4(tlv_length); } } diff --git a/epan/dissectors/packet-pathport.c b/epan/dissectors/packet-pathport.c index 298d4ad576..61a6c7e9ea 100644 --- a/epan/dissectors/packet-pathport.c +++ b/epan/dissectors/packet-pathport.c @@ -12,7 +12,7 @@ #include "config.h" #include #include - +#include #define PATHPORT_UDP_PORT 3792 /* Not IANA registered */ #define PATHPORT_MIN_LENGTH 24 /* HEADER + 1 PDU */ @@ -26,7 +26,7 @@ #define PATHPORT_HEADER_END (PATHPORT_HEADER_OFFSET + PATHPORT_HEADER_LENGTH) /** Rounds the specified integer up to the next multiple of four. */ -#define roof4(a) (((a)+3)&~3) +#define roof4(a) WS_ROUNDUP_4(a) void proto_reg_handoff_pathport(void); void proto_register_pathport(void); diff --git a/epan/dissectors/packet-pvfs2.c b/epan/dissectors/packet-pvfs2.c index 258dcc3e51..c25bc7773d 100644 --- a/epan/dissectors/packet-pvfs2.c +++ b/epan/dissectors/packet-pvfs2.c @@ -25,6 +25,7 @@ #include #include #include +#include #include "packet-tcp.h" #define TCP_PORT_PVFS2 3334 /* Not IANA registered */ @@ -756,9 +757,6 @@ dissect_pvfs2_ds_type(tvbuff_t *tvb, proto_tree *tree, int offset, return offset; } -#define roundup4(x) (((x) + 3) & ~3) -#define roundup8(x) (((x) + 7) & ~7) - static int dissect_pvfs_opaque_data(tvbuff_t *tvb, int offset, proto_tree *tree, @@ -813,9 +811,9 @@ dissect_pvfs_opaque_data(tvbuff_t *tvb, int offset, */ if (!string_data) - string_length_full = roundup4(string_length); + string_length_full = WS_ROUNDUP_4(string_length); else - string_length_full = roundup8(4 + string_length); + string_length_full = WS_ROUNDUP_8(4 + string_length); if (string_length_captured < string_length) { /* truncated string */ @@ -1148,7 +1146,7 @@ dissect_pvfs_distribution(tvbuff_t *tvb, proto_tree *tree, int offset) tmpstr = (char *) tvb_get_string_enc(wmem_packet_scope(), tvb, offset + 4, distlen, ENC_ASCII); /* 'distlen' does not include the NULL terminator */ - total_len = roundup8(4 + distlen + 1); + total_len = WS_ROUNDUP_8(4 + distlen + 1); if (((distlen + 1) == PVFS_DIST_SIMPLE_STRIPE_NAME_SIZE) && (g_ascii_strncasecmp(tmpstr, PVFS_DIST_SIMPLE_STRIPE_NAME, diff --git a/epan/dissectors/packet-smb2.c b/epan/dissectors/packet-smb2.c index e1e021bf15..9617e0a597 100644 --- a/epan/dissectors/packet-smb2.c +++ b/epan/dissectors/packet-smb2.c @@ -41,6 +41,7 @@ #include "read_keytab_file.h" #include +#include //#define DEBUG_SMB2 #ifdef DEBUG_SMB2 @@ -5313,7 +5314,7 @@ dissect_smb2_negotiate_protocol_request(tvbuff_t *tvb, packet_info *pinfo, proto } for (i = 0; i < ncc; i++) { - offset = (offset + 7) & ~7; + offset = WS_ROUNDUP_8(offset); offset = dissect_smb2_negotiate_context(tvb, pinfo, tree, offset, si); } @@ -5430,7 +5431,7 @@ dissect_smb2_negotiate_protocol_response(tvbuff_t *tvb, packet_info *pinfo, prot } for (i = 0; i < ncc; i++) { - offset = (offset + 7) & ~7; + offset = WS_ROUNDUP_8(offset); offset = dissect_smb2_negotiate_context(tvb, pinfo, tree, offset, si); } diff --git a/epan/dissectors/packet-stun.c b/epan/dissectors/packet-stun.c index f7191a5ccd..8bbd3a620a 100644 --- a/epan/dissectors/packet-stun.c +++ b/epan/dissectors/packet-stun.c @@ -54,6 +54,7 @@ #include #include #include +#include #include "packet-tcp.h" void proto_register_stun(void); @@ -1068,7 +1069,7 @@ dissect_stun_message(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, gboole att_type = tvb_get_ntohs(tvb, offset); /* Attribute type field in attribute header */ att_length = tvb_get_ntohs(tvb, offset+2); /* Attribute length field in attribute header */ if (network_version >= NET_VER_5389) - att_length_pad = (att_length + 3) & ~3; /* Attribute length including padding */ + att_length_pad = WS_ROUNDUP_4(att_length); /* Attribute length including padding */ else att_length_pad = att_length; att_type_display = att_type; @@ -1304,7 +1305,7 @@ dissect_stun_message(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, gboole } } /* Hopefully, in case MS-TURN ever gets PASSWORD-ALGORITHM(S) support they will add it with padding */ - alg_param_len_pad = (alg_param_len + 3) & ~3; + alg_param_len_pad = WS_ROUNDUP_4(alg_param_len); if (alg_param_len < alg_param_len_pad) proto_tree_add_uint(att_tree, hf_stun_att_padding, tvb, loopoffset+alg_param_len, alg_param_len_pad-alg_param_len, alg_param_len_pad-alg_param_len); diff --git a/epan/dissectors/packet-usb.c b/epan/dissectors/packet-usb.c index ad0289a66a..754f09feb2 100644 --- a/epan/dissectors/packet-usb.c +++ b/epan/dissectors/packet-usb.c @@ -26,6 +26,7 @@ #include #include #include +#include #include "packet-usb.h" #include "packet-mausb.h" @@ -4554,7 +4555,7 @@ dissect_darwin_usb_iso_transfer(packet_info *pinfo _U_, proto_tree *tree, usb_he /* Padding to align the next header */ offset += frame_header_length; - offset = ((offset + 3) & ~3); + offset = WS_ROUNDUP_4(offset); iso_tree_start = offset; len -= frame_header_length; @@ -4685,7 +4686,7 @@ dissect_freebsd_usb(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent, void */ proto_tree_add_item(frame_tree, hf_usb_frame_data, tvb, offset, framelen, ENC_NA); - offset += (framelen + 3) & ~3; + offset += WS_ROUNDUP_4(framelen); } proto_item_set_end(ti, tvb, offset); } diff --git a/epan/dissectors/packet-yami.c b/epan/dissectors/packet-yami.c index 9d09a6ff43..6381d49cab 100644 --- a/epan/dissectors/packet-yami.c +++ b/epan/dissectors/packet-yami.c @@ -19,6 +19,7 @@ #include #include #include +#include #include "packet-tcp.h" void proto_reg_handoff_yami(void); @@ -145,7 +146,7 @@ dissect_yami_parameter(tvbuff_t *tvb, proto_tree *tree, int offset, proto_item * name = tvb_get_string_enc(wmem_packet_scope(), tvb, offset, name_len, ENC_ASCII | ENC_NA); proto_item_append_text(ti, ": %s", name); proto_item_append_text(par_ti, "%s, ", name); - offset += (name_len + 3) & ~3; + offset += WS_ROUNDUP_4(name_len); proto_tree_add_string(yami_param, &hfi_yami_param_name, tvb, name_offset, offset - name_offset, name); type = tvb_get_letohl(tvb, offset); @@ -201,7 +202,7 @@ dissect_yami_parameter(tvbuff_t *tvb, proto_tree *tree, int offset, proto_item * val = tvb_get_string_enc(wmem_packet_scope(), tvb, offset, val_len, ENC_ASCII | ENC_NA); proto_item_append_text(ti, ", Type: string, Value: \"%s\"", val); - offset += (val_len + 3) & ~3; + offset += WS_ROUNDUP_4(val_len); proto_tree_add_string(yami_param, &hfi_yami_param_value_str, tvb, val_offset, offset - val_offset, val); break; } @@ -220,7 +221,7 @@ dissect_yami_parameter(tvbuff_t *tvb, proto_tree *tree, int offset, proto_item * repr = bytes_to_str(wmem_packet_scope(), val, val_len); proto_item_append_text(ti, ", Type: binary, Value: %s", repr); - offset += (val_len + 3) & ~3; + offset += WS_ROUNDUP_4(val_len); proto_tree_add_bytes_format_value(yami_param, &hfi_yami_param_value_bin, tvb, val_offset, offset - val_offset, val, "%s", repr); break; } @@ -354,7 +355,7 @@ dissect_yami_parameter(tvbuff_t *tvb, proto_tree *tree, int offset, proto_item * proto_item_append_text(ti, "\"%s\", ", val); proto_tree_add_string(yami_param, &hfi_yami_param_value_str, tvb, val_offset, offset - val_offset, val); - offset += (val_len + 3) & ~3; + offset += WS_ROUNDUP_4(val_len); } proto_item_append_text(ti, "}"); break; @@ -384,7 +385,7 @@ dissect_yami_parameter(tvbuff_t *tvb, proto_tree *tree, int offset, proto_item * repr = bytes_to_str(wmem_packet_scope(), val, val_len); proto_item_append_text(ti, "%s, ", repr); - offset += (val_len + 3) & ~3; + offset += WS_ROUNDUP_4(val_len); proto_tree_add_bytes_format_value(yami_param, &hfi_yami_param_value_bin, tvb, val_offset, offset - val_offset, val, "%s", repr); } proto_item_append_text(ti, "}"); diff --git a/wiretap/pcapng.c b/wiretap/pcapng.c index af844827f4..5eb8c435b9 100644 --- a/wiretap/pcapng.c +++ b/wiretap/pcapng.c @@ -27,6 +27,7 @@ #include #include #include +#include #include "wtap-int.h" #include "file_wrappers.h" @@ -37,7 +38,7 @@ #include "pcapng_module.h" #include "secrets-types.h" -#define ROUND_TO_4BYTE(len) ((len + 3) & ~3) +#define ROUND_TO_4BYTE(len) WS_ROUNDUP_4(len) static gboolean pcapng_read(wtap *wth, wtap_rec *rec, Buffer *buf, int *err, diff --git a/wiretap/snoop.c b/wiretap/snoop.c index 89de11c32b..e5a9d966e0 100644 --- a/wiretap/snoop.c +++ b/wiretap/snoop.c @@ -14,6 +14,7 @@ #include "atm.h" #include "snoop.h" #include +#include /* See RFC 1761 for a description of the "snoop" file format. */ @@ -891,7 +892,7 @@ static gboolean snoop_dump(wtap_dumper *wdh, /* ... plus enough bytes to pad it to a 4-byte boundary. */ - padlen = ((reclen + 3) & ~3) - reclen; + padlen = WS_ROUNDUP_4(reclen) - reclen; reclen += padlen; /* Don't write anything we're not willing to read. */ diff --git a/wsutil/CMakeLists.txt b/wsutil/CMakeLists.txt index d08dba497f..f26674f603 100644 --- a/wsutil/CMakeLists.txt +++ b/wsutil/CMakeLists.txt @@ -71,6 +71,7 @@ set(WSUTIL_PUBLIC_HEADERS ws_mempbrk.h ws_mempbrk_int.h ws_pipe.h + ws_roundup.h wsjson.h wslog.h xtea.h diff --git a/wsutil/ws_roundup.h b/wsutil/ws_roundup.h new file mode 100644 index 0000000000..62b21628c6 --- /dev/null +++ b/wsutil/ws_roundup.h @@ -0,0 +1,22 @@ +/* ws_roundup.h + * + * Wireshark - Network traffic analyzer + * By Gerald Combs + * Copyright 1998 Gerald Combs + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#ifndef __WS_ROUNDUP_H__ +#define __WS_ROUNDUP_H__ + +/* + * Round up to various powers of 2. + */ +#define WS_ROUNDUP_2(n) (((n) + ((guint)(2U-1U))) & (~((guint)(2U-1U)))) +#define WS_ROUNDUP_4(n) (((n) + ((guint)(4U-1U))) & (~((guint)(4U-1U)))) +#define WS_ROUNDUP_8(n) (((n) + ((guint)(8U-1U))) & (~((guint)(8U-1U)))) +#define WS_ROUNDUP_16(n) (((n) + ((guint)(16U-1U))) & (~((guint)(16U-1U)))) +#define WS_ROUNDUP_32(n) (((n) + ((guint)(32U-1U))) & (~((guint)(32U-1U)))) + +#endif /* __WS_ROUNDUP_H__ */