Create sign extension routines in <wsutil/sign_ext.h>, use it in few places.

svn path=/trunk/; revision=54197
This commit is contained in:
Jakub Zawadzki 2013-12-17 21:36:33 +00:00
parent 6db9eb0b73
commit 0de43ce2dd
3 changed files with 67 additions and 35 deletions

View 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);

View 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;
}

50
wsutil/sign_ext.h Normal file
View File

@ -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__ */