As suggested by Anders: back out 37112.
[obnox/wireshark/wip.git] / epan / pint.h
1 /* pint.h
2  * Definitions for extracting and translating integers safely and portably
3  * via pointers.
4  *
5  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
10  *
11  *
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.
16  *
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.
21  *
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.
25  */
26
27 #ifndef __PINT_H__
28 #define __PINT_H__
29
30 #include <glib.h>
31
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.
35  */
36
37 #define pntohs(p)   ((guint16)                       \
38                      ((guint16)*((const guint8 *)(p)+0)<<8|  \
39                       (guint16)*((const guint8 *)(p)+1)<<0))
40
41 #define pntoh24(p)  ((guint32)*((const guint8 *)(p)+0)<<16|  \
42                      (guint32)*((const guint8 *)(p)+1)<<8|   \
43                      (guint32)*((const guint8 *)(p)+2)<<0)
44
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
50 #define pntoh40(p)  ((guint64)*((const guint8 *)(p)+0)<<32|  \
51                      (guint64)*((const guint8 *)(p)+1)<<24|  \
52                      (guint64)*((const guint8 *)(p)+2)<<16|  \
53                      (guint64)*((const guint8 *)(p)+3)<<8|   \
54                      (guint64)*((const guint8 *)(p)+4)<<0)
55
56 #define pntoh48(p)  ((guint64)*((const guint8 *)(p)+0)<<40|  \
57                      (guint64)*((const guint8 *)(p)+1)<<32|  \
58                      (guint64)*((const guint8 *)(p)+2)<<24|  \
59                      (guint64)*((const guint8 *)(p)+3)<<16|  \
60                      (guint64)*((const guint8 *)(p)+4)<<8|   \
61                      (guint64)*((const guint8 *)(p)+5)<<0)
62
63 #define pntoh56(p)  ((guint64)*((const guint8 *)(p)+0)<<48|  \
64                      (guint64)*((const guint8 *)(p)+1)<<40|  \
65                      (guint64)*((const guint8 *)(p)+2)<<32|  \
66                      (guint64)*((const guint8 *)(p)+3)<<24|  \
67                      (guint64)*((const guint8 *)(p)+4)<<16|  \
68                      (guint64)*((const guint8 *)(p)+5)<<8|   \
69                      (guint64)*((const guint8 *)(p)+6)<<0)
70
71 #define pntoh64(p)  ((guint64)*((const guint8 *)(p)+0)<<56|  \
72                      (guint64)*((const guint8 *)(p)+1)<<48|  \
73                      (guint64)*((const guint8 *)(p)+2)<<40|  \
74                      (guint64)*((const guint8 *)(p)+3)<<32|  \
75                      (guint64)*((const guint8 *)(p)+4)<<24|  \
76                      (guint64)*((const guint8 *)(p)+5)<<16|  \
77                      (guint64)*((const guint8 *)(p)+6)<<8|   \
78                      (guint64)*((const guint8 *)(p)+7)<<0)
79
80
81 #define pletohs(p)  ((guint16)                       \
82                      ((guint16)*((const guint8 *)(p)+1)<<8|  \
83                       (guint16)*((const guint8 *)(p)+0)<<0))
84
85 #define pletoh24(p) ((guint32)*((const guint8 *)(p)+2)<<16|  \
86                      (guint32)*((const guint8 *)(p)+1)<<8|   \
87                      (guint32)*((const guint8 *)(p)+0)<<0)
88
89 #define pletohl(p)  ((guint32)*((const guint8 *)(p)+3)<<24|  \
90                      (guint32)*((const guint8 *)(p)+2)<<16|  \
91                      (guint32)*((const guint8 *)(p)+1)<<8|   \
92                      (guint32)*((const guint8 *)(p)+0)<<0)
93
94 #define pletoh40(p) ((guint64)*((const guint8 *)(p)+4)<<32|  \
95                      (guint64)*((const guint8 *)(p)+3)<<24|  \
96                      (guint64)*((const guint8 *)(p)+2)<<16|  \
97                      (guint64)*((const guint8 *)(p)+1)<<8|   \
98                      (guint64)*((const guint8 *)(p)+0)<<0)
99
100 #define pletoh48(p) ((guint64)*((const guint8 *)(p)+5)<<40|  \
101                      (guint64)*((const guint8 *)(p)+4)<<32|  \
102                      (guint64)*((const guint8 *)(p)+3)<<24|  \
103                      (guint64)*((const guint8 *)(p)+2)<<16|  \
104                      (guint64)*((const guint8 *)(p)+1)<<8|   \
105                      (guint64)*((const guint8 *)(p)+0)<<0)
106
107 #define pletoh56(p) ((guint64)*((const guint8 *)(p)+6)<<48|  \
108                      (guint64)*((const guint8 *)(p)+5)<<40|  \
109                      (guint64)*((const guint8 *)(p)+4)<<32|  \
110                      (guint64)*((const guint8 *)(p)+3)<<24|  \
111                      (guint64)*((const guint8 *)(p)+2)<<16|  \
112                      (guint64)*((const guint8 *)(p)+1)<<8|   \
113                      (guint64)*((const guint8 *)(p)+0)<<0)
114
115 #define pletoh64(p) ((guint64)*((const guint8 *)(p)+7)<<56|  \
116                      (guint64)*((const guint8 *)(p)+6)<<48|  \
117                      (guint64)*((const guint8 *)(p)+5)<<40|  \
118                      (guint64)*((const guint8 *)(p)+4)<<32|  \
119                      (guint64)*((const guint8 *)(p)+3)<<24|  \
120                      (guint64)*((const guint8 *)(p)+2)<<16|  \
121                      (guint64)*((const guint8 *)(p)+1)<<8|   \
122                      (guint64)*((const guint8 *)(p)+0)<<0)
123
124 /* Pointer routines to put items out in a particular byte order.
125  * These will work regardless of the byte alignment of the pointer.
126  */
127
128 #define phtons(p, v) \
129         {                               \
130         ((guint8*)(p))[0] = (guint8)((v) >> 8); \
131         ((guint8*)(p))[1] = (guint8)((v) >> 0); \
132         }
133
134 #define phtonl(p, v) \
135         {                               \
136         ((guint8*)(p))[0] = (guint8)((v) >> 24);        \
137         ((guint8*)(p))[1] = (guint8)((v) >> 16);        \
138         ((guint8*)(p))[2] = (guint8)((v) >> 8); \
139         ((guint8*)(p))[3] = (guint8)((v) >> 0); \
140         }
141
142
143 /* Macros to byte-swap 32-bit and 16-bit quantities. */
144 #define BSWAP32(x) \
145         ((((x)&0xFF000000)>>24) | \
146          (((x)&0x00FF0000)>>8) | \
147          (((x)&0x0000FF00)<<8) | \
148          (((x)&0x000000FF)<<24))
149 #define BSWAP16(x) \
150          ((((x)&0xFF00)>>8) | \
151           (((x)&0x00FF)<<8))
152
153 /* Turn host-byte-order values into little-endian values. */
154 #define htoles(s) GUINT16_TO_LE(s)
155 #define htolel(l) GUINT32_TO_LE(l)
156
157 #endif /* PINT_H */