Define macros to calculate (2^N)^M, and use them in more places.

Change-Id: I4df1b35d8d2233c301f0ba9e119d012aebe9cd17
Reviewed-on: https://code.wireshark.org/review/25913
Reviewed-by: Guy Harris <guy@alum.mit.edu>
This commit is contained in:
Guy Harris 2018-02-19 21:27:03 -08:00
parent e4787a9190
commit dc6b9dfcd6
7 changed files with 51 additions and 17 deletions

View File

@ -34,6 +34,8 @@
#include <epan/proto_data.h>
#include <epan/strutil.h>
#include <wsutil/pow2.h>
#include "packet-ber.h"
#include "packet-dns.h"
#include "packet-x509af.h"
@ -2995,7 +2997,7 @@ dissect_icmpv6_rpl_opt(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree
}
}
proto_item_append_text(ti_opt_lifetime, " (%u sec)", 1U << ((lt_mr_nh & RPL_OPT_ROUTE_DISCOVERY_L) >> 6)*2);
proto_item_append_text(ti_opt_lifetime, " (%u sec)", pow4(guint32, (lt_mr_nh & RPL_OPT_ROUTE_DISCOVERY_L) >> 6));
if (!(lt_mr_nh & RPL_OPT_ROUTE_DISCOVERY_MR_NH)) {
proto_item_append_text(ti_opt_mr_nh, " (Infinity)");

View File

@ -25,15 +25,16 @@
#include "config.h"
#include <math.h>
#include <epan/packet.h>
#include <epan/expert.h>
#include <epan/conversation.h>
#include <epan/tfs.h>
#include <epan/reassemble.h>
#include <wiretap/wtap.h>
#include <epan/crc16-tvb.h>
#include <wiretap/wtap.h>
#include <wsutil/pow2.h>
/* Proximity Integrated Circuit Card, i.e. the smartcard */
#define ADDR_PICC "PICC"
@ -541,8 +542,8 @@ dissect_iso14443_cmd_type_wupb(tvbuff_t *tvb, packet_info *pinfo,
col_set_str(pinfo->cinfo, COL_INFO, msg_type);
proto_item_append_text(ti, ": %s", msg_type);
proto_tree_add_uint_bits_format_value(tree, hf_iso14443_n,
tvb, offset*8+5, 3, (guint8)pow(2, param&0x07),
"%d", (guint8)pow(2, param&0x07));
tvb, offset*8+5, 3, pow2(guint32, param&0x07),
"%u", pow2(guint32, param&0x07));
offset++;
if (!crc_dropped) {

View File

@ -14,13 +14,13 @@
#include "config.h"
#include <math.h>
#include <epan/packet.h>
#include <epan/asn1.h>
#include <epan/prefs.h>
#include <epan/expert.h>
#include <epan/exceptions.h>
#include <epan/show_exception.h>
#include <wsutil/pow2.h>
#include "packet-gsm_map.h"
#include "packet-gsm_a_common.h"
#include "packet-e212.h"
@ -3538,16 +3538,16 @@ get_ext_ambr_unit(guint32 byte, const char **unit_str)
mult = 0;
*unit_str = "";
} else if (byte <= 0x06) {
mult = (guint32)pow(4, byte-0x02);
mult = pow4(guint32, byte-0x02);
*unit_str = "Mbps";
} else if (byte <= 0x0b) {
mult = (guint32)pow(4, byte-0x07);
mult = pow4(guint32, byte-0x07);
*unit_str = "Gbps";
} else if (byte <= 0x10) {
mult = (guint32)pow(4, byte-0x0c);
mult = pow4(guint32, byte-0x0c);
*unit_str = "Tbps";
} else if (byte <= 0x15) {
mult = (guint32)pow(4, byte-0x11);
mult = pow4(guint32, byte-0x11);
*unit_str = "Pbps";
} else {
mult = 256;
@ -3622,16 +3622,16 @@ get_ext_eps_qos_unit(guint32 byte, const char **unit_str)
mult = 200;
*unit_str = "kbps";
} else if (byte <= 0x06) {
mult = (guint32)pow(4, byte-0x02);
mult = pow4(guint32, byte-0x02);
*unit_str = "Mbps";
} else if (byte <= 0x0b) {
mult = (guint32)pow(4, byte-0x07);
mult = pow4(guint32, byte-0x07);
*unit_str = "Gbps";
} else if (byte <= 0x10) {
mult = (guint32)pow(4, byte-0x0c);
mult = pow4(guint32, byte-0x0c);
*unit_str = "Tbps";
} else if (byte <= 0x15) {
mult = (guint32)pow(4, byte-0x11);
mult = pow4(guint32, byte-0x11);
*unit_str = "Pbps";
} else {
mult = 256;

View File

@ -18,7 +18,6 @@
#include "config.h"
#include <math.h>
#include <epan/packet.h>
#include <epan/prefs.h>
#include <epan/expert.h>
@ -28,6 +27,7 @@
#include <wsutil/wsgcrypt.h>
#include <wsutil/crc16.h>
#include <wsutil/pow2.h>
void proto_register_sigcomp(void);
void proto_reg_handoff_sigcomp(void);
@ -6340,7 +6340,7 @@ dissect_udvm_multitype_operand(tvbuff_t *udvm_tvb, proto_tree *sigcomp_udvm_tree
if ( display_udvm_bytecode )
proto_tree_add_uint(sigcomp_udvm_tree, hf_udvm_multitype_bytecode,
udvm_tvb, offset, 1, display_bytecode);
result = (guint32)pow(2,( bytecode & 0x07) + 8);
result = pow2(guint32, (bytecode & 0x07) + 8);
operand = result & 0xffff;
*start_offset = offset;
*value = operand;
@ -6355,7 +6355,7 @@ dissect_udvm_multitype_operand(tvbuff_t *udvm_tvb, proto_tree *sigcomp_udvm_tree
if ( display_udvm_bytecode )
proto_tree_add_uint(sigcomp_udvm_tree, hf_udvm_multitype_bytecode,
udvm_tvb, offset, 1, display_bytecode);
result = (guint32)pow(2,( bytecode & 0x01) + 6);
result = pow2(guint32, (bytecode & 0x01) + 6);
operand = result & 0xffff;
*start_offset = offset;
*value = operand;

View File

@ -61,6 +61,7 @@ set(WSUTIL_PUBLIC_HEADERS
os_version_info.h
pint.h
plugins.h
pow2.h
privileges.h
processes.h
report_message.h

View File

@ -68,6 +68,7 @@ WSUTIL_PUBLIC_INCLUDES = \
os_version_info.h \
pint.h \
plugins.h \
pow2.h \
privileges.h \
processes.h \
report_message.h \

29
wsutil/pow2.h Normal file
View File

@ -0,0 +1,29 @@
/* ws_pow2.h
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <gerald@wireshark.org>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef __WS_POW2_H__
#define __WS_POW2_H__
/*
* Macros to calculate pow2^M, for various power-of-2 values and positive
* integer values of M. That's (2^N)^M, i.e. 2^(N*M).
*
* The first argument is the type of the desired result; the second
* argument is M.
*/
#define pow2(type, m) (((type)1U) << (m))
#define pow4(type, m) (((type)1U) << (2*(m)))
#define pow8(type, m) (((type)1U) << (3*(m)))
#define pow16(type, m) (((type)1U) << (4*(m)))
#define pow32(type, m) (((type)1U) << (5*(m)))
#define pow64(type, m) (((type)1U) << (6*(m)))
#define pow128(type, m) (((type)1U) << (7*(m)))
#define pow256(type, m) (((type)1U) << (8*(m)))
#endif /* __WS_POW2_H__ */