Create sign extension routines in <wsutil/sign_ext.h>, use it in few places.
authordarkjames <darkjames@f5534014-38df-0310-8fa8-9805f1628bb7>
Tue, 17 Dec 2013 21:36:33 +0000 (21:36 +0000)
committerdarkjames <darkjames@f5534014-38df-0310-8fa8-9805f1628bb7>
Tue, 17 Dec 2013 21:36:33 +0000 (21:36 +0000)
git-svn-id: http://anonsvn.wireshark.org/wireshark/trunk@54197 f5534014-38df-0310-8fa8-9805f1628bb7

epan/proto.c
epan/tvbuff.c
wsutil/sign_ext.h [new file with mode: 0644]

index 33ac875a9c06245d56daf2a78ef40d56e903dc02..43af9a4e09e534e1f0271e81d0971bd589bc4fe0 100644 (file)
@@ -32,6 +32,7 @@
 
 #include <wsutil/bits_ctz.h>
 #include <wsutil/bits_count_ones.h>
+#include <wsutil/sign_ext.h>
 
 #include <ftypes/ftypes-int.h>
 
@@ -2605,32 +2606,25 @@ proto_tree_set_int64_tvb(field_info *fi, tvbuff_t *tvb, gint start,
        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;
        }
 
@@ -3297,8 +3291,7 @@ proto_tree_set_int(field_info *fi, gint32 value)
                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);
index 7261239960352db1a0eca9af33118721ffa03ed5..95b7c4d3d9fd1102034a5b882f54419d013736c4 100644 (file)
@@ -40,6 +40,7 @@
 #include <string.h>
 
 #include "wsutil/pint.h"
+#include "wsutil/sign_ext.h"
 #include "tvbuff.h"
 #include "tvbuff-int.h"
 #include "strutil.h"
@@ -841,9 +842,7 @@ tvb_get_ntohi40(tvbuff_t *tvb, const gint offset)
 {
        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;
 }
@@ -862,9 +861,7 @@ tvb_get_ntohi48(tvbuff_t *tvb, const gint offset)
 {
        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;
 }
@@ -883,9 +880,7 @@ tvb_get_ntohi56(tvbuff_t *tvb, const gint offset)
 {
        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;
 }
@@ -1143,9 +1138,7 @@ tvb_get_letohi40(tvbuff_t *tvb, const gint offset)
 {
        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;
 }
@@ -1164,11 +1157,9 @@ tvb_get_letohi48(tvbuff_t *tvb, const gint offset)
 {
        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
@@ -1185,9 +1176,7 @@ tvb_get_letohi56(tvbuff_t *tvb, const gint offset)
 {
        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;
 }
diff --git a/wsutil/sign_ext.h b/wsutil/sign_ext.h
new file mode 100644 (file)
index 0000000..ef91182
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * 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__ */