2000-08-30 02:50:18 +00:00
|
|
|
/* pint.h
|
|
|
|
* Definitions for extracting and translating integers safely and portably
|
|
|
|
* via pointers.
|
|
|
|
*
|
2006-05-21 05:12:17 +00:00
|
|
|
* Wireshark - Network traffic analyzer
|
2008-01-08 22:54:51 +00:00
|
|
|
* By Gerald Combs <gerald@wireshark.org>
|
2000-08-30 02:50:18 +00:00
|
|
|
* Copyright 1998 Gerald Combs
|
|
|
|
*
|
2018-02-07 11:26:45 +00:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later
|
2000-08-30 02:50:18 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __PINT_H__
|
|
|
|
#define __PINT_H__
|
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
|
2002-08-02 21:29:45 +00:00
|
|
|
/* Pointer versions of g_ntohs and g_ntohl. Given a pointer to a member of a
|
2000-08-30 02:50:18 +00:00
|
|
|
* byte array, returns the value of the two or four bytes at the pointer.
|
2013-11-29 18:59:06 +00:00
|
|
|
* The pletohXX versions return the little-endian representation.
|
2000-08-30 02:50:18 +00:00
|
|
|
*/
|
|
|
|
|
2013-11-29 18:59:06 +00:00
|
|
|
#define pntoh16(p) ((guint16) \
|
2002-11-27 22:41:00 +00:00
|
|
|
((guint16)*((const guint8 *)(p)+0)<<8| \
|
|
|
|
(guint16)*((const guint8 *)(p)+1)<<0))
|
2000-08-30 02:50:18 +00:00
|
|
|
|
2002-11-27 22:41:00 +00:00
|
|
|
#define pntoh24(p) ((guint32)*((const guint8 *)(p)+0)<<16| \
|
2011-01-14 19:02:38 +00:00
|
|
|
(guint32)*((const guint8 *)(p)+1)<<8| \
|
2002-11-27 22:41:00 +00:00
|
|
|
(guint32)*((const guint8 *)(p)+2)<<0)
|
2000-08-30 02:50:18 +00:00
|
|
|
|
2013-11-29 18:59:06 +00:00
|
|
|
#define pntoh32(p) ((guint32)*((const guint8 *)(p)+0)<<24| \
|
2002-11-27 22:41:00 +00:00
|
|
|
(guint32)*((const guint8 *)(p)+1)<<16| \
|
|
|
|
(guint32)*((const guint8 *)(p)+2)<<8| \
|
|
|
|
(guint32)*((const guint8 *)(p)+3)<<0)
|
2011-01-14 19:02:38 +00:00
|
|
|
|
|
|
|
#define pntoh40(p) ((guint64)*((const guint8 *)(p)+0)<<32| \
|
|
|
|
(guint64)*((const guint8 *)(p)+1)<<24| \
|
|
|
|
(guint64)*((const guint8 *)(p)+2)<<16| \
|
|
|
|
(guint64)*((const guint8 *)(p)+3)<<8| \
|
|
|
|
(guint64)*((const guint8 *)(p)+4)<<0)
|
|
|
|
|
|
|
|
#define pntoh48(p) ((guint64)*((const guint8 *)(p)+0)<<40| \
|
|
|
|
(guint64)*((const guint8 *)(p)+1)<<32| \
|
|
|
|
(guint64)*((const guint8 *)(p)+2)<<24| \
|
|
|
|
(guint64)*((const guint8 *)(p)+3)<<16| \
|
|
|
|
(guint64)*((const guint8 *)(p)+4)<<8| \
|
|
|
|
(guint64)*((const guint8 *)(p)+5)<<0)
|
|
|
|
|
|
|
|
#define pntoh56(p) ((guint64)*((const guint8 *)(p)+0)<<48| \
|
|
|
|
(guint64)*((const guint8 *)(p)+1)<<40| \
|
|
|
|
(guint64)*((const guint8 *)(p)+2)<<32| \
|
|
|
|
(guint64)*((const guint8 *)(p)+3)<<24| \
|
|
|
|
(guint64)*((const guint8 *)(p)+4)<<16| \
|
|
|
|
(guint64)*((const guint8 *)(p)+5)<<8| \
|
|
|
|
(guint64)*((const guint8 *)(p)+6)<<0)
|
|
|
|
|
2004-08-22 00:31:58 +00:00
|
|
|
#define pntoh64(p) ((guint64)*((const guint8 *)(p)+0)<<56| \
|
|
|
|
(guint64)*((const guint8 *)(p)+1)<<48| \
|
|
|
|
(guint64)*((const guint8 *)(p)+2)<<40| \
|
|
|
|
(guint64)*((const guint8 *)(p)+3)<<32| \
|
|
|
|
(guint64)*((const guint8 *)(p)+4)<<24| \
|
|
|
|
(guint64)*((const guint8 *)(p)+5)<<16| \
|
|
|
|
(guint64)*((const guint8 *)(p)+6)<<8| \
|
|
|
|
(guint64)*((const guint8 *)(p)+7)<<0)
|
2000-08-30 02:50:18 +00:00
|
|
|
|
|
|
|
|
2013-11-29 18:59:06 +00:00
|
|
|
#define pletoh16(p) ((guint16) \
|
2002-11-27 22:41:00 +00:00
|
|
|
((guint16)*((const guint8 *)(p)+1)<<8| \
|
|
|
|
(guint16)*((const guint8 *)(p)+0)<<0))
|
2000-08-30 02:50:18 +00:00
|
|
|
|
2002-11-27 22:41:00 +00:00
|
|
|
#define pletoh24(p) ((guint32)*((const guint8 *)(p)+2)<<16| \
|
2011-01-14 19:02:38 +00:00
|
|
|
(guint32)*((const guint8 *)(p)+1)<<8| \
|
2002-11-27 22:41:00 +00:00
|
|
|
(guint32)*((const guint8 *)(p)+0)<<0)
|
2000-08-30 02:50:18 +00:00
|
|
|
|
2013-11-29 18:59:06 +00:00
|
|
|
#define pletoh32(p) ((guint32)*((const guint8 *)(p)+3)<<24| \
|
2002-11-27 22:41:00 +00:00
|
|
|
(guint32)*((const guint8 *)(p)+2)<<16| \
|
|
|
|
(guint32)*((const guint8 *)(p)+1)<<8| \
|
|
|
|
(guint32)*((const guint8 *)(p)+0)<<0)
|
2011-01-14 19:02:38 +00:00
|
|
|
|
|
|
|
#define pletoh40(p) ((guint64)*((const guint8 *)(p)+4)<<32| \
|
|
|
|
(guint64)*((const guint8 *)(p)+3)<<24| \
|
|
|
|
(guint64)*((const guint8 *)(p)+2)<<16| \
|
|
|
|
(guint64)*((const guint8 *)(p)+1)<<8| \
|
|
|
|
(guint64)*((const guint8 *)(p)+0)<<0)
|
|
|
|
|
|
|
|
#define pletoh48(p) ((guint64)*((const guint8 *)(p)+5)<<40| \
|
|
|
|
(guint64)*((const guint8 *)(p)+4)<<32| \
|
|
|
|
(guint64)*((const guint8 *)(p)+3)<<24| \
|
|
|
|
(guint64)*((const guint8 *)(p)+2)<<16| \
|
|
|
|
(guint64)*((const guint8 *)(p)+1)<<8| \
|
|
|
|
(guint64)*((const guint8 *)(p)+0)<<0)
|
|
|
|
|
|
|
|
#define pletoh56(p) ((guint64)*((const guint8 *)(p)+6)<<48| \
|
|
|
|
(guint64)*((const guint8 *)(p)+5)<<40| \
|
|
|
|
(guint64)*((const guint8 *)(p)+4)<<32| \
|
|
|
|
(guint64)*((const guint8 *)(p)+3)<<24| \
|
|
|
|
(guint64)*((const guint8 *)(p)+2)<<16| \
|
|
|
|
(guint64)*((const guint8 *)(p)+1)<<8| \
|
|
|
|
(guint64)*((const guint8 *)(p)+0)<<0)
|
|
|
|
|
2004-08-22 00:31:58 +00:00
|
|
|
#define pletoh64(p) ((guint64)*((const guint8 *)(p)+7)<<56| \
|
|
|
|
(guint64)*((const guint8 *)(p)+6)<<48| \
|
|
|
|
(guint64)*((const guint8 *)(p)+5)<<40| \
|
|
|
|
(guint64)*((const guint8 *)(p)+4)<<32| \
|
|
|
|
(guint64)*((const guint8 *)(p)+3)<<24| \
|
|
|
|
(guint64)*((const guint8 *)(p)+2)<<16| \
|
|
|
|
(guint64)*((const guint8 *)(p)+1)<<8| \
|
|
|
|
(guint64)*((const guint8 *)(p)+0)<<0)
|
2000-08-30 02:50:18 +00:00
|
|
|
|
2007-12-28 13:01:55 +00:00
|
|
|
/* Pointer routines to put items out in a particular byte order.
|
|
|
|
* These will work regardless of the byte alignment of the pointer.
|
|
|
|
*/
|
|
|
|
|
2013-11-29 18:59:06 +00:00
|
|
|
#define phton16(p, v) \
|
2017-02-04 04:06:48 +00:00
|
|
|
{ \
|
|
|
|
((guint8*)(p))[0] = (guint8)((v) >> 8); \
|
|
|
|
((guint8*)(p))[1] = (guint8)((v) >> 0); \
|
|
|
|
}
|
2007-12-28 13:01:55 +00:00
|
|
|
|
2013-11-29 18:59:06 +00:00
|
|
|
#define phton32(p, v) \
|
2017-02-04 04:06:48 +00:00
|
|
|
{ \
|
|
|
|
((guint8*)(p))[0] = (guint8)((v) >> 24); \
|
|
|
|
((guint8*)(p))[1] = (guint8)((v) >> 16); \
|
|
|
|
((guint8*)(p))[2] = (guint8)((v) >> 8); \
|
|
|
|
((guint8*)(p))[3] = (guint8)((v) >> 0); \
|
|
|
|
}
|
2000-08-30 02:50:18 +00:00
|
|
|
|
2017-01-27 21:30:34 +00:00
|
|
|
static inline void phton64(guint8 *p, guint64 v) {
|
|
|
|
p[0] = (guint8)(v >> 56);
|
|
|
|
p[1] = (guint8)(v >> 48);
|
|
|
|
p[2] = (guint8)(v >> 40);
|
|
|
|
p[3] = (guint8)(v >> 32);
|
|
|
|
p[4] = (guint8)(v >> 24);
|
|
|
|
p[5] = (guint8)(v >> 16);
|
|
|
|
p[6] = (guint8)(v >> 8);
|
|
|
|
p[7] = (guint8)(v >> 0);
|
|
|
|
}
|
|
|
|
|
2016-12-13 12:28:30 +00:00
|
|
|
/* Subtract two guint32s with respect to wraparound */
|
|
|
|
#define guint32_wraparound_diff(higher, lower) ((higher>lower)?(higher-lower):(higher+0xffffffff-lower+1))
|
|
|
|
|
2000-08-30 02:50:18 +00:00
|
|
|
#endif /* PINT_H */
|
2017-02-04 04:06:48 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Editor modelines - http://www.wireshark.org/tools/modelines.html
|
|
|
|
*
|
|
|
|
* Local Variables:
|
|
|
|
* c-basic-offset: 4
|
|
|
|
* tab-width: 8
|
|
|
|
* indent-tabs-mode: nil
|
|
|
|
* End:
|
|
|
|
*
|
|
|
|
* ex: set shiftwidth=4 tabstop=8 expandtab:
|
|
|
|
* :indentSize=4:tabSize=8:noTabs=true:
|
|
|
|
*/
|