#include <wsutil/bits_ctz.h>
#include <wsutil/bits_count_ones.h>
+#include <wsutil/sign_ext.h>
#include <ftypes/ftypes-int.h>
switch(length)
{
case 7:
- if (value & 0x80000000000000LL) /* account for sign bit */
- value |= 0xFF00000000000000LL;
+ value = ws_sign_ext64(value, 56);
break;
case 6:
- if (value & 0x800000000000LL) /* account for sign bit */
- value |= 0xFFFF000000000000LL;
+ value = ws_sign_ext64(value, 48);
break;
case 5:
- if (value & 0x8000000000LL) /* account for sign bit */
- value |= 0xFFFFFF0000000000LL;
+ value = ws_sign_ext64(value, 40);
break;
case 4:
- if (value & 0x80000000LL) /* account for sign bit */
- value |= 0xFFFFFFFF00000000LL;
+ value = ws_sign_ext64(value, 32);
break;
case 3:
- if (value & 0x800000LL) /* account for sign bit */
- value |= 0xFFFFFFFFFF000000LL;
+ value = ws_sign_ext64(value, 24);
break;
case 2:
- if (value & 0x8000LL) /* account for sign bit */
- value |= 0xFFFFFFFFFFFF0000LL;
+ value = ws_sign_ext64(value, 16);
break;
case 1:
- if (value & 0x80LL) /* account for sign bit */
- value |= 0xFFFFFFFFFFFFFF00LL;
+ value = ws_sign_ext64(value, 8);
break;
}
integer >>= hfinfo_bitshift(hfinfo);
no_of_bits = ws_count_ones(hfinfo->bitmask);
- if (integer & (1 << (no_of_bits-1)))
- integer |= (-1 << no_of_bits);
+ integer = ws_sign_ext32(integer, no_of_bits);
}
fvalue_set_sinteger(&fi->value, integer);
#include <string.h>
#include "wsutil/pint.h"
+#include "wsutil/sign_ext.h"
#include "tvbuff.h"
#include "tvbuff-int.h"
#include "strutil.h"
{
guint64 ret;
- ret = tvb_get_ntoh40(tvb, offset);
- if (ret & 0x8000000000LL) /* account for sign bit */
- ret |= 0xFFFFFF0000000000LL;
+ ret = ws_sign_ext64(tvb_get_ntoh40(tvb, offset), 40);
return (gint64)ret;
}
{
guint64 ret;
- ret = tvb_get_ntoh48(tvb, offset);
- if (ret & 0x800000000000LL) /* account for sign bit */
- ret |= 0xFFFF000000000000LL;
+ ret = ws_sign_ext64(tvb_get_ntoh48(tvb, offset), 48);
return (gint64)ret;
}
{
guint64 ret;
- ret = tvb_get_ntoh56(tvb, offset);
- if (ret & 0x80000000000000LL) /* account for sign bit */
- ret |= 0xFF00000000000000LL;
+ ret = ws_sign_ext64(tvb_get_ntoh56(tvb, offset), 56);
return (gint64)ret;
}
{
guint64 ret;
- ret = tvb_get_letoh40(tvb, offset);
- if (ret & 0x8000000000LL) /* account for sign bit */
- ret |= 0xFFFFFF0000000000LL;
+ ret = ws_sign_ext64(tvb_get_letoh40(tvb, offset), 40);
return (gint64)ret;
}
{
guint64 ret;
- ret = tvb_get_letoh48(tvb, offset);
- if (ret & 0x800000000000LL) /* account for sign bit */
- ret |= 0xFFFF000000000000LL;
+ ret = ws_sign_ext64(tvb_get_letoh48(tvb, offset), 48);
- return (gint64)ret;
+ return (gint64)ret;
}
guint64
{
guint64 ret;
- ret = tvb_get_letoh56(tvb, offset);
- if (ret & 0x80000000000000LL) /* account for sign bit */
- ret |= 0xFF00000000000000LL;
+ ret = ws_sign_ext64(tvb_get_letoh56(tvb, offset), 56);
return (gint64)ret;
}
--- /dev/null
+/*
+ * sign_ext.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 __WSUTIL_SIGN_EXT_H__
+#define __WSUTIL_SIGN_EXT_H__
+
+#include <glib.h>
+
+/* sign extension routines */
+
+static inline guint32
+ws_sign_ext32(guint32 val, int no_of_bits)
+{
+ if (val & (1 << (no_of_bits-1)))
+ val |= (-1 << no_of_bits);
+
+ return val;
+} _U_
+
+static inline guint64
+ws_sign_ext64(guint64 val, int no_of_bits)
+{
+ if (val & (1LL << (no_of_bits-1)))
+ val |= (-1 << no_of_bits);
+
+ return val;
+} _U_
+
+#endif /* __WSUTIL_SIGN_EXT_H__ */