2 * Definitions for extracting and translating integers safely and portably
5 * Wireshark - Network traffic analyzer
6 * By Gerald Combs <gerald@wireshark.org>
7 * Copyright 1998 Gerald Combs
9 * SPDX-License-Identifier: GPL-2.0-or-later
17 /* Routines that take a possibly-unaligned pointer to a 16-bit, 24-bit,
18 * 32-bit, 40-bit, ... 64-bit integral quantity, in a particular byte
19 * order, and fetch the value and return it in host byte order.
21 * The pntohN() routines fetch big-endian values; the pletohN() routines
22 * fetch little-endian values.
25 static inline guint16 pntoh16(const void *p)
27 return (guint16)*((const guint8 *)(p)+0)<<8|
28 (guint16)*((const guint8 *)(p)+1)<<0;
31 static inline guint32 pntoh24(const void *p)
33 return (guint32)*((const guint8 *)(p)+0)<<16|
34 (guint32)*((const guint8 *)(p)+1)<<8|
35 (guint32)*((const guint8 *)(p)+2)<<0;
38 static inline guint32 pntoh32(const void *p)
40 return (guint32)*((const guint8 *)(p)+0)<<24|
41 (guint32)*((const guint8 *)(p)+1)<<16|
42 (guint32)*((const guint8 *)(p)+2)<<8|
43 (guint32)*((const guint8 *)(p)+3)<<0;
46 static inline guint64 pntoh40(const void *p)
48 return (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;
55 static inline guint64 pntoh48(const void *p)
57 return (guint64)*((const guint8 *)(p)+0)<<40|
58 (guint64)*((const guint8 *)(p)+1)<<32|
59 (guint64)*((const guint8 *)(p)+2)<<24|
60 (guint64)*((const guint8 *)(p)+3)<<16|
61 (guint64)*((const guint8 *)(p)+4)<<8|
62 (guint64)*((const guint8 *)(p)+5)<<0;
65 static inline guint64 pntoh56(const void *p)
67 return (guint64)*((const guint8 *)(p)+0)<<48|
68 (guint64)*((const guint8 *)(p)+1)<<40|
69 (guint64)*((const guint8 *)(p)+2)<<32|
70 (guint64)*((const guint8 *)(p)+3)<<24|
71 (guint64)*((const guint8 *)(p)+4)<<16|
72 (guint64)*((const guint8 *)(p)+5)<<8|
73 (guint64)*((const guint8 *)(p)+6)<<0;
76 static inline guint64 pntoh64(const void *p)
78 return (guint64)*((const guint8 *)(p)+0)<<56|
79 (guint64)*((const guint8 *)(p)+1)<<48|
80 (guint64)*((const guint8 *)(p)+2)<<40|
81 (guint64)*((const guint8 *)(p)+3)<<32|
82 (guint64)*((const guint8 *)(p)+4)<<24|
83 (guint64)*((const guint8 *)(p)+5)<<16|
84 (guint64)*((const guint8 *)(p)+6)<<8|
85 (guint64)*((const guint8 *)(p)+7)<<0;
88 static inline guint16 pletoh16(const void *p)
90 return (guint16)*((const guint8 *)(p)+1)<<8|
91 (guint16)*((const guint8 *)(p)+0)<<0;
94 static inline guint32 pletoh24(const void *p)
96 return (guint32)*((const guint8 *)(p)+2)<<16|
97 (guint32)*((const guint8 *)(p)+1)<<8|
98 (guint32)*((const guint8 *)(p)+0)<<0;
101 static inline guint32 pletoh32(const void *p)
103 return (guint32)*((const guint8 *)(p)+3)<<24|
104 (guint32)*((const guint8 *)(p)+2)<<16|
105 (guint32)*((const guint8 *)(p)+1)<<8|
106 (guint32)*((const guint8 *)(p)+0)<<0;
109 static inline guint64 pletoh40(const void *p)
111 return (guint64)*((const guint8 *)(p)+4)<<32|
112 (guint64)*((const guint8 *)(p)+3)<<24|
113 (guint64)*((const guint8 *)(p)+2)<<16|
114 (guint64)*((const guint8 *)(p)+1)<<8|
115 (guint64)*((const guint8 *)(p)+0)<<0;
118 static inline guint64 pletoh48(const void *p)
120 return (guint64)*((const guint8 *)(p)+5)<<40|
121 (guint64)*((const guint8 *)(p)+4)<<32|
122 (guint64)*((const guint8 *)(p)+3)<<24|
123 (guint64)*((const guint8 *)(p)+2)<<16|
124 (guint64)*((const guint8 *)(p)+1)<<8|
125 (guint64)*((const guint8 *)(p)+0)<<0;
128 static inline guint64 pletoh56(const void *p)
130 return (guint64)*((const guint8 *)(p)+6)<<48|
131 (guint64)*((const guint8 *)(p)+5)<<40|
132 (guint64)*((const guint8 *)(p)+4)<<32|
133 (guint64)*((const guint8 *)(p)+3)<<24|
134 (guint64)*((const guint8 *)(p)+2)<<16|
135 (guint64)*((const guint8 *)(p)+1)<<8|
136 (guint64)*((const guint8 *)(p)+0)<<0;
139 static inline guint64 pletoh64(const void *p)
141 return (guint64)*((const guint8 *)(p)+7)<<56|
142 (guint64)*((const guint8 *)(p)+6)<<48|
143 (guint64)*((const guint8 *)(p)+5)<<40|
144 (guint64)*((const guint8 *)(p)+4)<<32|
145 (guint64)*((const guint8 *)(p)+3)<<24|
146 (guint64)*((const guint8 *)(p)+2)<<16|
147 (guint64)*((const guint8 *)(p)+1)<<8|
148 (guint64)*((const guint8 *)(p)+0)<<0;
150 /* Pointer routines to put items out in a particular byte order.
151 * These will work regardless of the byte alignment of the pointer.
154 static inline void phton16(guint8 *p, guint16 v)
156 p[0] = (guint8)(v >> 8);
157 p[1] = (guint8)(v >> 0);
160 static inline void phton32(guint8 *p, guint32 v)
162 p[0] = (guint8)(v >> 24);
163 p[1] = (guint8)(v >> 16);
164 p[2] = (guint8)(v >> 8);
165 p[3] = (guint8)(v >> 0);
168 static inline void phton64(guint8 *p, guint64 v) {
169 p[0] = (guint8)(v >> 56);
170 p[1] = (guint8)(v >> 48);
171 p[2] = (guint8)(v >> 40);
172 p[3] = (guint8)(v >> 32);
173 p[4] = (guint8)(v >> 24);
174 p[5] = (guint8)(v >> 16);
175 p[6] = (guint8)(v >> 8);
176 p[7] = (guint8)(v >> 0);
179 static inline void phtole32(guint8 *p, guint32 v) {
180 p[0] = (guint8)(v >> 0);
181 p[1] = (guint8)(v >> 8);
182 p[2] = (guint8)(v >> 16);
183 p[3] = (guint8)(v >> 24);
186 static inline void phtole64(guint8 *p, guint64 v) {
187 p[0] = (guint8)(v >> 0);
188 p[1] = (guint8)(v >> 8);
189 p[2] = (guint8)(v >> 16);
190 p[3] = (guint8)(v >> 24);
191 p[4] = (guint8)(v >> 32);
192 p[5] = (guint8)(v >> 40);
193 p[6] = (guint8)(v >> 48);
194 p[7] = (guint8)(v >> 56);
197 /* Subtract two guint32s with respect to wraparound */
198 #define guint32_wraparound_diff(higher, lower) ((higher>lower)?(higher-lower):(higher+0xffffffff-lower+1))
203 * Editor modelines - https://www.wireshark.org/tools/modelines.html
208 * indent-tabs-mode: nil
211 * ex: set shiftwidth=4 tabstop=8 expandtab:
212 * :indentSize=4:tabSize=8:noTabs=true: