Remove all $Id$ from top of file
[metze/wireshark/wip.git] / wsutil / pint.h
1 /* pint.h
2  * Definitions for extracting and translating integers safely and portably
3  * via pointers.
4  *
5  * Wireshark - Network traffic analyzer
6  * By Gerald Combs <gerald@wireshark.org>
7  * Copyright 1998 Gerald Combs
8  *
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  */
24
25 #ifndef __PINT_H__
26 #define __PINT_H__
27
28 #include <glib.h>
29
30 /* Pointer versions of g_ntohs and g_ntohl.  Given a pointer to a member of a
31  * byte array, returns the value of the two or four bytes at the pointer.
32  * The pletohXX versions return the little-endian representation.
33  */
34
35 #define pntoh16(p)  ((guint16)                       \
36                      ((guint16)*((const guint8 *)(p)+0)<<8|  \
37                       (guint16)*((const guint8 *)(p)+1)<<0))
38
39 #define pntoh24(p)  ((guint32)*((const guint8 *)(p)+0)<<16|  \
40                      (guint32)*((const guint8 *)(p)+1)<<8|   \
41                      (guint32)*((const guint8 *)(p)+2)<<0)
42
43 #define pntoh32(p)  ((guint32)*((const guint8 *)(p)+0)<<24|  \
44                      (guint32)*((const guint8 *)(p)+1)<<16|  \
45                      (guint32)*((const guint8 *)(p)+2)<<8|   \
46                      (guint32)*((const guint8 *)(p)+3)<<0)
47
48 #define pntoh40(p)  ((guint64)*((const guint8 *)(p)+0)<<32|  \
49                      (guint64)*((const guint8 *)(p)+1)<<24|  \
50                      (guint64)*((const guint8 *)(p)+2)<<16|  \
51                      (guint64)*((const guint8 *)(p)+3)<<8|   \
52                      (guint64)*((const guint8 *)(p)+4)<<0)
53
54 #define pntoh48(p)  ((guint64)*((const guint8 *)(p)+0)<<40|  \
55                      (guint64)*((const guint8 *)(p)+1)<<32|  \
56                      (guint64)*((const guint8 *)(p)+2)<<24|  \
57                      (guint64)*((const guint8 *)(p)+3)<<16|  \
58                      (guint64)*((const guint8 *)(p)+4)<<8|   \
59                      (guint64)*((const guint8 *)(p)+5)<<0)
60
61 #define pntoh56(p)  ((guint64)*((const guint8 *)(p)+0)<<48|  \
62                      (guint64)*((const guint8 *)(p)+1)<<40|  \
63                      (guint64)*((const guint8 *)(p)+2)<<32|  \
64                      (guint64)*((const guint8 *)(p)+3)<<24|  \
65                      (guint64)*((const guint8 *)(p)+4)<<16|  \
66                      (guint64)*((const guint8 *)(p)+5)<<8|   \
67                      (guint64)*((const guint8 *)(p)+6)<<0)
68
69 #define pntoh64(p)  ((guint64)*((const guint8 *)(p)+0)<<56|  \
70                      (guint64)*((const guint8 *)(p)+1)<<48|  \
71                      (guint64)*((const guint8 *)(p)+2)<<40|  \
72                      (guint64)*((const guint8 *)(p)+3)<<32|  \
73                      (guint64)*((const guint8 *)(p)+4)<<24|  \
74                      (guint64)*((const guint8 *)(p)+5)<<16|  \
75                      (guint64)*((const guint8 *)(p)+6)<<8|   \
76                      (guint64)*((const guint8 *)(p)+7)<<0)
77
78
79 #define pletoh16(p) ((guint16)                       \
80                      ((guint16)*((const guint8 *)(p)+1)<<8|  \
81                       (guint16)*((const guint8 *)(p)+0)<<0))
82
83 #define pletoh24(p) ((guint32)*((const guint8 *)(p)+2)<<16|  \
84                      (guint32)*((const guint8 *)(p)+1)<<8|   \
85                      (guint32)*((const guint8 *)(p)+0)<<0)
86
87 #define pletoh32(p) ((guint32)*((const guint8 *)(p)+3)<<24|  \
88                      (guint32)*((const guint8 *)(p)+2)<<16|  \
89                      (guint32)*((const guint8 *)(p)+1)<<8|   \
90                      (guint32)*((const guint8 *)(p)+0)<<0)
91
92 #define pletoh40(p) ((guint64)*((const guint8 *)(p)+4)<<32|  \
93                      (guint64)*((const guint8 *)(p)+3)<<24|  \
94                      (guint64)*((const guint8 *)(p)+2)<<16|  \
95                      (guint64)*((const guint8 *)(p)+1)<<8|   \
96                      (guint64)*((const guint8 *)(p)+0)<<0)
97
98 #define pletoh48(p) ((guint64)*((const guint8 *)(p)+5)<<40|  \
99                      (guint64)*((const guint8 *)(p)+4)<<32|  \
100                      (guint64)*((const guint8 *)(p)+3)<<24|  \
101                      (guint64)*((const guint8 *)(p)+2)<<16|  \
102                      (guint64)*((const guint8 *)(p)+1)<<8|   \
103                      (guint64)*((const guint8 *)(p)+0)<<0)
104
105 #define pletoh56(p) ((guint64)*((const guint8 *)(p)+6)<<48|  \
106                      (guint64)*((const guint8 *)(p)+5)<<40|  \
107                      (guint64)*((const guint8 *)(p)+4)<<32|  \
108                      (guint64)*((const guint8 *)(p)+3)<<24|  \
109                      (guint64)*((const guint8 *)(p)+2)<<16|  \
110                      (guint64)*((const guint8 *)(p)+1)<<8|   \
111                      (guint64)*((const guint8 *)(p)+0)<<0)
112
113 #define pletoh64(p) ((guint64)*((const guint8 *)(p)+7)<<56|  \
114                      (guint64)*((const guint8 *)(p)+6)<<48|  \
115                      (guint64)*((const guint8 *)(p)+5)<<40|  \
116                      (guint64)*((const guint8 *)(p)+4)<<32|  \
117                      (guint64)*((const guint8 *)(p)+3)<<24|  \
118                      (guint64)*((const guint8 *)(p)+2)<<16|  \
119                      (guint64)*((const guint8 *)(p)+1)<<8|   \
120                      (guint64)*((const guint8 *)(p)+0)<<0)
121
122 /* Pointer routines to put items out in a particular byte order.
123  * These will work regardless of the byte alignment of the pointer.
124  */
125
126 #define phton16(p, v) \
127         {                               \
128         ((guint8*)(p))[0] = (guint8)((v) >> 8); \
129         ((guint8*)(p))[1] = (guint8)((v) >> 0); \
130         }
131
132 #define phton32(p, v) \
133         {                               \
134         ((guint8*)(p))[0] = (guint8)((v) >> 24);        \
135         ((guint8*)(p))[1] = (guint8)((v) >> 16);        \
136         ((guint8*)(p))[2] = (guint8)((v) >> 8); \
137         ((guint8*)(p))[3] = (guint8)((v) >> 0); \
138         }
139
140 #endif /* PINT_H */