2 * Definitions for extracting and translating integers safely and portably
7 * Wireshark - 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)
49 #define pntoh64(p) ((guint64)*((const guint8 *)(p)+0)<<56| \
50 (guint64)*((const guint8 *)(p)+1)<<48| \
51 (guint64)*((const guint8 *)(p)+2)<<40| \
52 (guint64)*((const guint8 *)(p)+3)<<32| \
53 (guint64)*((const guint8 *)(p)+4)<<24| \
54 (guint64)*((const guint8 *)(p)+5)<<16| \
55 (guint64)*((const guint8 *)(p)+6)<<8| \
56 (guint64)*((const guint8 *)(p)+7)<<0)
59 #define pletohs(p) ((guint16) \
60 ((guint16)*((const guint8 *)(p)+1)<<8| \
61 (guint16)*((const guint8 *)(p)+0)<<0))
63 #define pletoh24(p) ((guint32)*((const guint8 *)(p)+2)<<16| \
64 (guint32)*((const guint8 *)(p)+1)<<8| \
65 (guint32)*((const guint8 *)(p)+0)<<0)
67 #define pletohl(p) ((guint32)*((const guint8 *)(p)+3)<<24| \
68 (guint32)*((const guint8 *)(p)+2)<<16| \
69 (guint32)*((const guint8 *)(p)+1)<<8| \
70 (guint32)*((const guint8 *)(p)+0)<<0)
71 #define pletoh64(p) ((guint64)*((const guint8 *)(p)+7)<<56| \
72 (guint64)*((const guint8 *)(p)+6)<<48| \
73 (guint64)*((const guint8 *)(p)+5)<<40| \
74 (guint64)*((const guint8 *)(p)+4)<<32| \
75 (guint64)*((const guint8 *)(p)+3)<<24| \
76 (guint64)*((const guint8 *)(p)+2)<<16| \
77 (guint64)*((const guint8 *)(p)+1)<<8| \
78 (guint64)*((const guint8 *)(p)+0)<<0)
82 /* Macros to byte-swap 32-bit and 16-bit quantities. */
84 ((((x)&0xFF000000)>>24) | \
85 (((x)&0x00FF0000)>>8) | \
86 (((x)&0x0000FF00)<<8) | \
87 (((x)&0x000000FF)<<24))
89 ((((x)&0xFF00)>>8) | \
92 /* Turn host-byte-order values into little-endian values. */
93 #define htoles(s) GUINT16_TO_LE(s)
94 #define htolel(l) GUINT32_TO_LE(l)