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.
#include <ctype.h>
#include <glib.h>
#include <float.h>
+#include <wsutil/swar.h>
#include "packet.h"
#include "ptvcursor.h"
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);
{
header_field_info *hfinfo;
guint32 integer;
+ gint no_of_bits;
hfinfo = fi->hfinfo;
integer = (guint32) 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);
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);
}
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:
/* 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;
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);
str_util.c
rc4.c
report_err.c
+ swar.c
tempfile.c
type_util.c
u3.c
sha1.c \
strnatcmp.c \
str_util.c \
+ swar.c \
rc4.c \
report_err.c \
tempfile.c \
pint.h \
rc4.h \
report_err.h \
+ swar.h \
tempfile.h \
type_util.h \
u3.h
--- /dev/null
+/*
+ * 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;
+}
--- /dev/null
+/*
+ * 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__ */