From Jiri Engelthaler via
authorEvan Huus <eapache@gmail.com>
Mon, 26 Aug 2013 16:23:55 +0000 (16:23 -0000)
committerEvan Huus <eapache@gmail.com>
Mon, 26 Aug 2013 16:23:55 +0000 (16:23 -0000)
https://bugs.wireshark.org/bugzilla/show_bug.cgi?id=9055

Add support for bitmask set for signed integer types.

svn path=/trunk/; revision=51522

AUTHORS
epan/proto.c
wsutil/CMakeLists.txt
wsutil/Makefile.common
wsutil/swar.c [new file with mode: 0644]
wsutil/swar.h [new file with mode: 0644]

diff --git a/AUTHORS b/AUTHORS
index ce6de6b51a0adf7ae7df813057c1bf0fc2dec2df..bdb14014ce6edfdd94388a196f6b0035bc127c16 100644 (file)
--- a/AUTHORS
+++ b/AUTHORS
@@ -3755,10 +3755,11 @@ Chas Williams           <chas[AT]cmf.nrl.navy.mil>
 Javier Godoy           <uce[AT]rjgodoy.com.ar>
 Matt Texier            <mtexier[AT]arbor.net>
 Linas Vepstas          <linasvepstas[AT]gmail.com>
-Simon Zhong                    <szhong[AT]juniper.net>
+Simon Zhong            <szhong[AT]juniper.net>
 Bart Van Assche                <bvanassche[AT]acm.org>
 Peter Lemenkov         <lemenkov[AT]gmail.com>
-Karl Beldan                    <karl.beldan[AT]gmail.com>
+Karl Beldan            <karl.beldan[AT]gmail.com>
+Jiri Engelthaler       <engycz[AT]gmail.com>
 
 Dan Lasley <dlasley[AT]promus.com> gave permission for his
 dumpit() hex-dump routine to be used.
index ed37f560404e154ee9a33ccd0ff58083efa460cb..fc1b0f7afcb102cc12d9ab1070208adb966b3294 100644 (file)
@@ -29,6 +29,7 @@
 #include <ctype.h>
 #include <glib.h>
 #include <float.h>
+#include <wsutil/swar.h>
 
 #include "packet.h"
 #include "ptvcursor.h"
@@ -126,7 +127,7 @@ struct ptvcursor {
 static const char *hf_try_val_to_str(guint32 value, const header_field_info *hfinfo);
 
 static void fill_label_boolean(field_info *fi, gchar *label_str);
-static void fill_label_bitfield(field_info *fi, gchar *label_str);
+static void fill_label_bitfield(field_info *fi, gchar *label_str, gboolean is_signed);
 static void fill_label_number(field_info *fi, gchar *label_str, gboolean is_signed);
 static void fill_label_number64(field_info *fi, gchar *label_str, gboolean is_signed);
 
@@ -3128,6 +3129,7 @@ proto_tree_set_int(field_info *fi, gint32 value)
 {
        header_field_info *hfinfo;
        guint32            integer;
+       gint               no_of_bits;
 
        hfinfo = fi->hfinfo;
        integer = (guint32) value;
@@ -3138,6 +3140,10 @@ proto_tree_set_int(field_info *fi, gint32 value)
 
                /* Shift bits */
                integer >>= hfinfo_bitshift(hfinfo);
+
+               no_of_bits = swar_count_bits(hfinfo->bitmask);
+               if (integer & (1 << (no_of_bits-1)))
+                       integer |= (-1 << no_of_bits);
        }
 
        fvalue_set_sinteger(&fi->value, integer);
@@ -5208,7 +5214,7 @@ proto_item_fill_label(field_info *fi, gchar *label_str)
                case FT_UINT24:
                case FT_UINT32:
                        if (hfinfo->bitmask) {
-                               fill_label_bitfield(fi, label_str);
+                               fill_label_bitfield(fi, label_str, FALSE);
                        } else {
                                fill_label_number(fi, label_str, FALSE);
                        }
@@ -5226,8 +5232,11 @@ proto_item_fill_label(field_info *fi, gchar *label_str)
                case FT_INT16:
                case FT_INT24:
                case FT_INT32:
-                       DISSECTOR_ASSERT(!hfinfo->bitmask);
-                       fill_label_number(fi, label_str, TRUE);
+                       if (hfinfo->bitmask) {
+                               fill_label_bitfield(fi, label_str, TRUE);
+                       } else {
+                               fill_label_number(fi, label_str, TRUE);
+                       }
                        break;
 
                case FT_INT64:
@@ -5423,7 +5432,7 @@ hf_try_val64_to_str_const(guint64 value, const header_field_info *hfinfo, const
 
 /* Fills data for bitfield ints with val_strings */
 static void
-fill_label_bitfield(field_info *fi, gchar *label_str)
+fill_label_bitfield(field_info *fi, gchar *label_str, gboolean is_signed)
 {
        char       *p;
        int         bitfield_byte_length, bitwidth;
@@ -5439,7 +5448,11 @@ fill_label_bitfield(field_info *fi, gchar *label_str)
        bitwidth = hfinfo_bitwidth(hfinfo);
 
        /* Un-shift bits */
-       unshifted_value = fvalue_get_uinteger(&fi->value);
+       if (is_signed)
+               unshifted_value = fvalue_get_sinteger(&fi->value);
+       else
+               unshifted_value = fvalue_get_uinteger(&fi->value);
+
        value = unshifted_value;
        if (hfinfo->bitmask) {
                unshifted_value <<= hfinfo_bitshift(hfinfo);
index d40e2cc630ac217a2c67724fc5103a3f2b9c23f6..7b9093c22eba8ed979819be2f4bc0e71a5f0582f 100644 (file)
@@ -60,6 +60,7 @@ set(WSUTIL_FILES
   str_util.c
   rc4.c
   report_err.c
+  swar.c
   tempfile.c
   type_util.c
   u3.c
index ba181e95d655ef781c4e1af8b5c107dfb6d86def..7332d97d8e24f519e6561c3000ae9c512f6ca299 100644 (file)
@@ -52,6 +52,7 @@ LIBWSUTIL_SRC =       \
        sha1.c          \
        strnatcmp.c     \
        str_util.c      \
+       swar.c          \
        rc4.c           \
        report_err.c    \
        tempfile.c      \
@@ -85,6 +86,7 @@ LIBWSUTIL_INCLUDES =  \
        pint.h          \
        rc4.h           \
        report_err.h    \
+       swar.h          \
        tempfile.h      \
        type_util.h     \
        u3.h
diff --git a/wsutil/swar.c b/wsutil/swar.c
new file mode 100644 (file)
index 0000000..c784ea4
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ *  swar.c
+ *
+ * $Id$
+ *
+ * Wireshark - Network traffic analyzer
+ * By Gerald Combs <gerald@wireshark.org>
+ * Copyright 1998 Gerald Combs
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+
+#include "config.h"
+
+#include <glib.h>
+
+#include "swar.h"
+
+/*
+ * The variable-precision SWAR algorithm is an interesting way to count 
+ * the number of bits set in an integer. While its performance is very 
+ * good (two times faster than gcc's __builtin_popcount [1] and 
+ * 16 instructions when compiled with gcc -O3)
+ * http://playingwithpointers.com/swar.html
+ */
+
+int swar_count_bits(const guint32 bitmask)
+{
+       int bits = bitmask;
+
+       bits = bits - ((bits >> 1) & 0x55555555);
+       bits = (bits & 0x33333333) + ((bits >> 2) & 0x33333333);
+       bits = (bits + (bits >> 4)) & 0x0F0F0F0F;
+
+       return (bits * 0x01010101) >> 24;
+}
diff --git a/wsutil/swar.h b/wsutil/swar.h
new file mode 100644 (file)
index 0000000..6541308
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+ *  swar.h
+ *
+ * $Id$
+ *
+ * Wireshark - Network traffic analyzer
+ * By Gerald Combs <gerald@wireshark.org>
+ * Copyright 1998 Gerald Combs
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+#ifndef __SWAR_H__
+#define __SWAR_H__
+
+#include "ws_symbol_export.h"
+
+WS_DLL_PUBLIC int swar_count_bits(const guint32 bitmask);
+
+#endif /* __SWAR_H__ */