diff options
author | Guy Harris <guy@alum.mit.edu> | 2018-02-19 21:27:03 -0800 |
---|---|---|
committer | Guy Harris <guy@alum.mit.edu> | 2018-02-20 05:27:38 +0000 |
commit | dc6b9dfcd635a1ca97a218bcdaf16a2ee5f7b693 (patch) | |
tree | 49ce29aca0c5cce7f14b06b7bd89d7a1a35614b4 | |
parent | e4787a9190c82dadae7e57c0847e2529bb2a9bbf (diff) |
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>
-rw-r--r-- | epan/dissectors/packet-icmpv6.c | 4 | ||||
-rw-r--r-- | epan/dissectors/packet-iso14443.c | 9 | ||||
-rw-r--r-- | epan/dissectors/packet-nas_eps.c | 18 | ||||
-rw-r--r-- | epan/dissectors/packet-sigcomp.c | 6 | ||||
-rw-r--r-- | wsutil/CMakeLists.txt | 1 | ||||
-rw-r--r-- | wsutil/Makefile.am | 1 | ||||
-rw-r--r-- | wsutil/pow2.h | 29 |
7 files changed, 51 insertions, 17 deletions
diff --git a/epan/dissectors/packet-icmpv6.c b/epan/dissectors/packet-icmpv6.c index b4d570e13f..56633a6ee3 100644 --- a/epan/dissectors/packet-icmpv6.c +++ b/epan/dissectors/packet-icmpv6.c @@ -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)"); diff --git a/epan/dissectors/packet-iso14443.c b/epan/dissectors/packet-iso14443.c index 76050832c9..4e2b3e5536 100644 --- a/epan/dissectors/packet-iso14443.c +++ b/epan/dissectors/packet-iso14443.c @@ -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) { diff --git a/epan/dissectors/packet-nas_eps.c b/epan/dissectors/packet-nas_eps.c index 980779a421..63018053d4 100644 --- a/epan/dissectors/packet-nas_eps.c +++ b/epan/dissectors/packet-nas_eps.c @@ -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; diff --git a/epan/dissectors/packet-sigcomp.c b/epan/dissectors/packet-sigcomp.c index 5c9f1adf18..cbd3eaf626 100644 --- a/epan/dissectors/packet-sigcomp.c +++ b/epan/dissectors/packet-sigcomp.c @@ -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; diff --git a/wsutil/CMakeLists.txt b/wsutil/CMakeLists.txt index bb75fd4dfd..c8e685e2da 100644 --- a/wsutil/CMakeLists.txt +++ b/wsutil/CMakeLists.txt @@ -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 diff --git a/wsutil/Makefile.am b/wsutil/Makefile.am index 008a50110d..0ce957f4ce 100644 --- a/wsutil/Makefile.am +++ b/wsutil/Makefile.am @@ -68,6 +68,7 @@ WSUTIL_PUBLIC_INCLUDES = \ os_version_info.h \ pint.h \ plugins.h \ + pow2.h \ privileges.h \ processes.h \ report_message.h \ diff --git a/wsutil/pow2.h b/wsutil/pow2.h new file mode 100644 index 0000000000..f72b09133e --- /dev/null +++ b/wsutil/pow2.h @@ -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__ */ |