2 * Definitions for extracting and translating integers safely and portably
5 * $Id: pint.h,v 1.8 2003/07/29 20:50:39 guy Exp $
7 * Ethereal - Network traffic analyzer
8 * By Gerald Combs <gerald@zing.org>
9 * Copyright 1998 Gerald Combs
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
32 /* Pointer versions of g_ntohs and g_ntohl. Given a pointer to a member of a
33 * byte array, returns the value of the two or four bytes at the pointer.
34 * The pletoh[sl] versions return the little-endian representation.
37 #define pntohs(p) ((guint16) \
38 ((guint16)*((const guint8 *)(p)+0)<<8| \
39 (guint16)*((const guint8 *)(p)+1)<<0))
41 #define pntoh24(p) ((guint32)*((const guint8 *)(p)+0)<<16| \
42 (guint32)*((const guint8 *)(p)+1)<<8| \
43 (guint32)*((const guint8 *)(p)+2)<<0)
45 #define pntohl(p) ((guint32)*((const guint8 *)(p)+0)<<24| \
46 (guint32)*((const guint8 *)(p)+1)<<16| \
47 (guint32)*((const guint8 *)(p)+2)<<8| \
48 (guint32)*((const guint8 *)(p)+3)<<0)
51 #define pletohs(p) ((guint16) \
52 ((guint16)*((const guint8 *)(p)+1)<<8| \
53 (guint16)*((const guint8 *)(p)+0)<<0))
55 #define pletoh24(p) ((guint32)*((const guint8 *)(p)+2)<<16| \
56 (guint32)*((const guint8 *)(p)+1)<<8| \
57 (guint32)*((const guint8 *)(p)+0)<<0)
59 #define pletohl(p) ((guint32)*((const guint8 *)(p)+3)<<24| \
60 (guint32)*((const guint8 *)(p)+2)<<16| \
61 (guint32)*((const guint8 *)(p)+1)<<8| \
62 (guint32)*((const guint8 *)(p)+0)<<0)
66 /* Macros to byte-swap 32-bit and 16-bit quantities. */
68 ((((x)&0xFF000000)>>24) | \
69 (((x)&0x00FF0000)>>8) | \
70 (((x)&0x0000FF00)<<8) | \
71 (((x)&0x000000FF)<<24))
73 ((((x)&0xFF00)>>8) | \
76 /* Turn host-byte-order values into little-endian values. */
77 #define htoles(s) GUINT16_TO_LE(s)
78 #define htolel(l) GUINT32_TO_LE(l)