Wireshark  2.9.0-477-g68ec514b
The Wireshark network protocol analyzer
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  * SPDX-License-Identifier: GPL-2.0-or-later
10  */
11 
12 #ifndef __PINT_H__
13 #define __PINT_H__
14 
15 #include <glib.h>
16 
17 /* Pointer versions of g_ntohs and g_ntohl. Given a pointer to a member of a
18  * byte array, returns the value of the two or four bytes at the pointer.
19  * The pletohXX versions return the little-endian representation.
20  */
21 
22 #define pntoh16(p) ((guint16) \
23  ((guint16)*((const guint8 *)(p)+0)<<8| \
24  (guint16)*((const guint8 *)(p)+1)<<0))
25 
26 #define pntoh24(p) ((guint32)*((const guint8 *)(p)+0)<<16| \
27  (guint32)*((const guint8 *)(p)+1)<<8| \
28  (guint32)*((const guint8 *)(p)+2)<<0)
29 
30 #define pntoh32(p) ((guint32)*((const guint8 *)(p)+0)<<24| \
31  (guint32)*((const guint8 *)(p)+1)<<16| \
32  (guint32)*((const guint8 *)(p)+2)<<8| \
33  (guint32)*((const guint8 *)(p)+3)<<0)
34 
35 #define pntoh40(p) ((guint64)*((const guint8 *)(p)+0)<<32| \
36  (guint64)*((const guint8 *)(p)+1)<<24| \
37  (guint64)*((const guint8 *)(p)+2)<<16| \
38  (guint64)*((const guint8 *)(p)+3)<<8| \
39  (guint64)*((const guint8 *)(p)+4)<<0)
40 
41 #define pntoh48(p) ((guint64)*((const guint8 *)(p)+0)<<40| \
42  (guint64)*((const guint8 *)(p)+1)<<32| \
43  (guint64)*((const guint8 *)(p)+2)<<24| \
44  (guint64)*((const guint8 *)(p)+3)<<16| \
45  (guint64)*((const guint8 *)(p)+4)<<8| \
46  (guint64)*((const guint8 *)(p)+5)<<0)
47 
48 #define pntoh56(p) ((guint64)*((const guint8 *)(p)+0)<<48| \
49  (guint64)*((const guint8 *)(p)+1)<<40| \
50  (guint64)*((const guint8 *)(p)+2)<<32| \
51  (guint64)*((const guint8 *)(p)+3)<<24| \
52  (guint64)*((const guint8 *)(p)+4)<<16| \
53  (guint64)*((const guint8 *)(p)+5)<<8| \
54  (guint64)*((const guint8 *)(p)+6)<<0)
55 
56 #define pntoh64(p) ((guint64)*((const guint8 *)(p)+0)<<56| \
57  (guint64)*((const guint8 *)(p)+1)<<48| \
58  (guint64)*((const guint8 *)(p)+2)<<40| \
59  (guint64)*((const guint8 *)(p)+3)<<32| \
60  (guint64)*((const guint8 *)(p)+4)<<24| \
61  (guint64)*((const guint8 *)(p)+5)<<16| \
62  (guint64)*((const guint8 *)(p)+6)<<8| \
63  (guint64)*((const guint8 *)(p)+7)<<0)
64 
65 
66 #define pletoh16(p) ((guint16) \
67  ((guint16)*((const guint8 *)(p)+1)<<8| \
68  (guint16)*((const guint8 *)(p)+0)<<0))
69 
70 #define pletoh24(p) ((guint32)*((const guint8 *)(p)+2)<<16| \
71  (guint32)*((const guint8 *)(p)+1)<<8| \
72  (guint32)*((const guint8 *)(p)+0)<<0)
73 
74 #define pletoh32(p) ((guint32)*((const guint8 *)(p)+3)<<24| \
75  (guint32)*((const guint8 *)(p)+2)<<16| \
76  (guint32)*((const guint8 *)(p)+1)<<8| \
77  (guint32)*((const guint8 *)(p)+0)<<0)
78 
79 #define pletoh40(p) ((guint64)*((const guint8 *)(p)+4)<<32| \
80  (guint64)*((const guint8 *)(p)+3)<<24| \
81  (guint64)*((const guint8 *)(p)+2)<<16| \
82  (guint64)*((const guint8 *)(p)+1)<<8| \
83  (guint64)*((const guint8 *)(p)+0)<<0)
84 
85 #define pletoh48(p) ((guint64)*((const guint8 *)(p)+5)<<40| \
86  (guint64)*((const guint8 *)(p)+4)<<32| \
87  (guint64)*((const guint8 *)(p)+3)<<24| \
88  (guint64)*((const guint8 *)(p)+2)<<16| \
89  (guint64)*((const guint8 *)(p)+1)<<8| \
90  (guint64)*((const guint8 *)(p)+0)<<0)
91 
92 #define pletoh56(p) ((guint64)*((const guint8 *)(p)+6)<<48| \
93  (guint64)*((const guint8 *)(p)+5)<<40| \
94  (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 pletoh64(p) ((guint64)*((const guint8 *)(p)+7)<<56| \
101  (guint64)*((const guint8 *)(p)+6)<<48| \
102  (guint64)*((const guint8 *)(p)+5)<<40| \
103  (guint64)*((const guint8 *)(p)+4)<<32| \
104  (guint64)*((const guint8 *)(p)+3)<<24| \
105  (guint64)*((const guint8 *)(p)+2)<<16| \
106  (guint64)*((const guint8 *)(p)+1)<<8| \
107  (guint64)*((const guint8 *)(p)+0)<<0)
108 
109 /* Pointer routines to put items out in a particular byte order.
110  * These will work regardless of the byte alignment of the pointer.
111  */
112 
113 #define phton16(p, v) \
114  { \
115  ((guint8*)(p))[0] = (guint8)((v) >> 8); \
116  ((guint8*)(p))[1] = (guint8)((v) >> 0); \
117  }
118 
119 #define phton32(p, v) \
120  { \
121  ((guint8*)(p))[0] = (guint8)((v) >> 24); \
122  ((guint8*)(p))[1] = (guint8)((v) >> 16); \
123  ((guint8*)(p))[2] = (guint8)((v) >> 8); \
124  ((guint8*)(p))[3] = (guint8)((v) >> 0); \
125  }
126 
127 static inline void phton64(guint8 *p, guint64 v) {
128  p[0] = (guint8)(v >> 56);
129  p[1] = (guint8)(v >> 48);
130  p[2] = (guint8)(v >> 40);
131  p[3] = (guint8)(v >> 32);
132  p[4] = (guint8)(v >> 24);
133  p[5] = (guint8)(v >> 16);
134  p[6] = (guint8)(v >> 8);
135  p[7] = (guint8)(v >> 0);
136 }
137 
138 /* Subtract two guint32s with respect to wraparound */
139 #define guint32_wraparound_diff(higher, lower) ((higher>lower)?(higher-lower):(higher+0xffffffff-lower+1))
140 
141 #endif /* PINT_H */
142 
143 /*
144  * Editor modelines - http://www.wireshark.org/tools/modelines.html
145  *
146  * Local Variables:
147  * c-basic-offset: 4
148  * tab-width: 8
149  * indent-tabs-mode: nil
150  * End:
151  *
152  * ex: set shiftwidth=4 tabstop=8 expandtab:
153  * :indentSize=4:tabSize=8:noTabs=true:
154  */