xref: /aosp_15_r20/external/libpcap/extract.h (revision 8b26181f966a6af5cf6981a6f474313de533bb28)
1*8b26181fSAndroid Build Coastguard Worker /*
2*8b26181fSAndroid Build Coastguard Worker  * Copyright (c) 1992, 1993, 1994, 1995, 1996
3*8b26181fSAndroid Build Coastguard Worker  *	The Regents of the University of California.  All rights reserved.
4*8b26181fSAndroid Build Coastguard Worker  *
5*8b26181fSAndroid Build Coastguard Worker  * Redistribution and use in source and binary forms, with or without
6*8b26181fSAndroid Build Coastguard Worker  * modification, are permitted provided that: (1) source code distributions
7*8b26181fSAndroid Build Coastguard Worker  * retain the above copyright notice and this paragraph in its entirety, (2)
8*8b26181fSAndroid Build Coastguard Worker  * distributions including binary code include the above copyright notice and
9*8b26181fSAndroid Build Coastguard Worker  * this paragraph in its entirety in the documentation or other materials
10*8b26181fSAndroid Build Coastguard Worker  * provided with the distribution, and (3) all advertising materials mentioning
11*8b26181fSAndroid Build Coastguard Worker  * features or use of this software display the following acknowledgement:
12*8b26181fSAndroid Build Coastguard Worker  * ``This product includes software developed by the University of California,
13*8b26181fSAndroid Build Coastguard Worker  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14*8b26181fSAndroid Build Coastguard Worker  * the University nor the names of its contributors may be used to endorse
15*8b26181fSAndroid Build Coastguard Worker  * or promote products derived from this software without specific prior
16*8b26181fSAndroid Build Coastguard Worker  * written permission.
17*8b26181fSAndroid Build Coastguard Worker  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18*8b26181fSAndroid Build Coastguard Worker  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19*8b26181fSAndroid Build Coastguard Worker  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20*8b26181fSAndroid Build Coastguard Worker  */
21*8b26181fSAndroid Build Coastguard Worker 
22*8b26181fSAndroid Build Coastguard Worker #ifndef _WIN32
23*8b26181fSAndroid Build Coastguard Worker #include <arpa/inet.h>
24*8b26181fSAndroid Build Coastguard Worker #endif
25*8b26181fSAndroid Build Coastguard Worker 
26*8b26181fSAndroid Build Coastguard Worker #include <pcap/pcap-inttypes.h>
27*8b26181fSAndroid Build Coastguard Worker #include <pcap/compiler-tests.h>
28*8b26181fSAndroid Build Coastguard Worker #include "portability.h"
29*8b26181fSAndroid Build Coastguard Worker 
30*8b26181fSAndroid Build Coastguard Worker /*
31*8b26181fSAndroid Build Coastguard Worker  * If we have versions of GCC or Clang that support an __attribute__
32*8b26181fSAndroid Build Coastguard Worker  * to say "if we're building with unsigned behavior sanitization,
33*8b26181fSAndroid Build Coastguard Worker  * don't complain about undefined behavior in this function", we
34*8b26181fSAndroid Build Coastguard Worker  * label these functions with that attribute - we *know* it's undefined
35*8b26181fSAndroid Build Coastguard Worker  * in the C standard, but we *also* know it does what we want with
36*8b26181fSAndroid Build Coastguard Worker  * the ISA we're targeting and the compiler we're using.
37*8b26181fSAndroid Build Coastguard Worker  *
38*8b26181fSAndroid Build Coastguard Worker  * For GCC 4.9.0 and later, we use __attribute__((no_sanitize_undefined));
39*8b26181fSAndroid Build Coastguard Worker  * pre-5.0 GCC doesn't have __has_attribute, and I'm not sure whether
40*8b26181fSAndroid Build Coastguard Worker  * GCC or Clang first had __attribute__((no_sanitize(XXX)).
41*8b26181fSAndroid Build Coastguard Worker  *
42*8b26181fSAndroid Build Coastguard Worker  * For Clang, we check for __attribute__((no_sanitize(XXX)) with
43*8b26181fSAndroid Build Coastguard Worker  * __has_attribute, as there are versions of Clang that support
44*8b26181fSAndroid Build Coastguard Worker  * __attribute__((no_sanitize("undefined")) but don't support
45*8b26181fSAndroid Build Coastguard Worker  * __attribute__((no_sanitize_undefined)).
46*8b26181fSAndroid Build Coastguard Worker  *
47*8b26181fSAndroid Build Coastguard Worker  * We define this here, rather than in funcattrs.h, because we
48*8b26181fSAndroid Build Coastguard Worker  * only want it used here, we don't want it to be broadly used.
49*8b26181fSAndroid Build Coastguard Worker  * (Any printer will get this defined, but this should at least
50*8b26181fSAndroid Build Coastguard Worker  * make it harder for people to find.)
51*8b26181fSAndroid Build Coastguard Worker  */
52*8b26181fSAndroid Build Coastguard Worker #if defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 409)
53*8b26181fSAndroid Build Coastguard Worker #define UNALIGNED_OK	__attribute__((no_sanitize_undefined))
54*8b26181fSAndroid Build Coastguard Worker #elif __has_attribute(no_sanitize)
55*8b26181fSAndroid Build Coastguard Worker #define UNALIGNED_OK	__attribute__((no_sanitize("undefined")))
56*8b26181fSAndroid Build Coastguard Worker #else
57*8b26181fSAndroid Build Coastguard Worker #define UNALIGNED_OK
58*8b26181fSAndroid Build Coastguard Worker #endif
59*8b26181fSAndroid Build Coastguard Worker 
60*8b26181fSAndroid Build Coastguard Worker #if (defined(__i386__) || defined(_M_IX86) || defined(__X86__) || defined(__x86_64__) || defined(_M_X64)) || \
61*8b26181fSAndroid Build Coastguard Worker     (defined(__m68k__) && (!defined(__mc68000__) && !defined(__mc68010__))) || \
62*8b26181fSAndroid Build Coastguard Worker     (defined(__ppc__) || defined(__ppc64__) || defined(_M_PPC) || defined(_ARCH_PPC) || defined(_ARCH_PPC64)) || \
63*8b26181fSAndroid Build Coastguard Worker     (defined(__s390__) || defined(__s390x__) || defined(__zarch__))
64*8b26181fSAndroid Build Coastguard Worker /*
65*8b26181fSAndroid Build Coastguard Worker  * The processor natively handles unaligned loads, so we can just
66*8b26181fSAndroid Build Coastguard Worker  * cast the pointer and fetch through it.
67*8b26181fSAndroid Build Coastguard Worker  *
68*8b26181fSAndroid Build Coastguard Worker  * XXX - are those all the x86 tests we need?
69*8b26181fSAndroid Build Coastguard Worker  * XXX - are those the only 68k tests we need not to generated
70*8b26181fSAndroid Build Coastguard Worker  * unaligned accesses if the target is the 68000 or 68010?
71*8b26181fSAndroid Build Coastguard Worker  * XXX - are there any tests we don't need, because some definitions are for
72*8b26181fSAndroid Build Coastguard Worker  * compilers that also predefine the GCC symbols?
73*8b26181fSAndroid Build Coastguard Worker  * XXX - do we need to test for both 32-bit and 64-bit versions of those
74*8b26181fSAndroid Build Coastguard Worker  * architectures in all cases?
75*8b26181fSAndroid Build Coastguard Worker  */
76*8b26181fSAndroid Build Coastguard Worker UNALIGNED_OK static inline uint16_t
EXTRACT_BE_U_2(const void * p)77*8b26181fSAndroid Build Coastguard Worker EXTRACT_BE_U_2(const void *p)
78*8b26181fSAndroid Build Coastguard Worker {
79*8b26181fSAndroid Build Coastguard Worker 	return ((uint16_t)ntohs(*(const uint16_t *)(p)));
80*8b26181fSAndroid Build Coastguard Worker }
81*8b26181fSAndroid Build Coastguard Worker 
82*8b26181fSAndroid Build Coastguard Worker UNALIGNED_OK static inline int16_t
EXTRACT_BE_S_2(const void * p)83*8b26181fSAndroid Build Coastguard Worker EXTRACT_BE_S_2(const void *p)
84*8b26181fSAndroid Build Coastguard Worker {
85*8b26181fSAndroid Build Coastguard Worker 	return ((int16_t)ntohs(*(const int16_t *)(p)));
86*8b26181fSAndroid Build Coastguard Worker }
87*8b26181fSAndroid Build Coastguard Worker 
88*8b26181fSAndroid Build Coastguard Worker UNALIGNED_OK static inline uint32_t
EXTRACT_BE_U_4(const void * p)89*8b26181fSAndroid Build Coastguard Worker EXTRACT_BE_U_4(const void *p)
90*8b26181fSAndroid Build Coastguard Worker {
91*8b26181fSAndroid Build Coastguard Worker 	return ((uint32_t)ntohl(*(const uint32_t *)(p)));
92*8b26181fSAndroid Build Coastguard Worker }
93*8b26181fSAndroid Build Coastguard Worker 
94*8b26181fSAndroid Build Coastguard Worker UNALIGNED_OK static inline int32_t
EXTRACT_BE_S_4(const void * p)95*8b26181fSAndroid Build Coastguard Worker EXTRACT_BE_S_4(const void *p)
96*8b26181fSAndroid Build Coastguard Worker {
97*8b26181fSAndroid Build Coastguard Worker 	return ((int32_t)ntohl(*(const int32_t *)(p)));
98*8b26181fSAndroid Build Coastguard Worker }
99*8b26181fSAndroid Build Coastguard Worker 
100*8b26181fSAndroid Build Coastguard Worker UNALIGNED_OK static inline uint64_t
EXTRACT_BE_U_8(const void * p)101*8b26181fSAndroid Build Coastguard Worker EXTRACT_BE_U_8(const void *p)
102*8b26181fSAndroid Build Coastguard Worker {
103*8b26181fSAndroid Build Coastguard Worker 	return ((uint64_t)(((uint64_t)ntohl(*((const uint32_t *)(p) + 0))) << 32 |
104*8b26181fSAndroid Build Coastguard Worker 		((uint64_t)ntohl(*((const uint32_t *)(p) + 1))) << 0));
105*8b26181fSAndroid Build Coastguard Worker 
106*8b26181fSAndroid Build Coastguard Worker }
107*8b26181fSAndroid Build Coastguard Worker 
108*8b26181fSAndroid Build Coastguard Worker UNALIGNED_OK static inline int64_t
EXTRACT_BE_S_8(const void * p)109*8b26181fSAndroid Build Coastguard Worker EXTRACT_BE_S_8(const void *p)
110*8b26181fSAndroid Build Coastguard Worker {
111*8b26181fSAndroid Build Coastguard Worker 	return ((int64_t)(((int64_t)ntohl(*((const uint32_t *)(p) + 0))) << 32 |
112*8b26181fSAndroid Build Coastguard Worker 		((uint64_t)ntohl(*((const uint32_t *)(p) + 1))) << 0));
113*8b26181fSAndroid Build Coastguard Worker 
114*8b26181fSAndroid Build Coastguard Worker }
115*8b26181fSAndroid Build Coastguard Worker #elif PCAP_IS_AT_LEAST_GNUC_VERSION(2,0) && \
116*8b26181fSAndroid Build Coastguard Worker     (defined(__alpha) || defined(__alpha__) || \
117*8b26181fSAndroid Build Coastguard Worker      defined(__mips) || defined(__mips__))
118*8b26181fSAndroid Build Coastguard Worker /*
119*8b26181fSAndroid Build Coastguard Worker  * This is MIPS or Alpha, which don't natively handle unaligned loads,
120*8b26181fSAndroid Build Coastguard Worker  * but which have instructions that can help when doing unaligned
121*8b26181fSAndroid Build Coastguard Worker  * loads, and this is GCC 2.0 or later or a compiler that claims to
122*8b26181fSAndroid Build Coastguard Worker  * be GCC 2.0 or later, which we assume that mean we have
123*8b26181fSAndroid Build Coastguard Worker  * __attribute__((packed)), which we can use to convince the compiler
124*8b26181fSAndroid Build Coastguard Worker  * to generate those instructions.
125*8b26181fSAndroid Build Coastguard Worker  *
126*8b26181fSAndroid Build Coastguard Worker  * Declare packed structures containing a uint16_t and a uint32_t,
127*8b26181fSAndroid Build Coastguard Worker  * cast the pointer to point to one of those, and fetch through it;
128*8b26181fSAndroid Build Coastguard Worker  * the GCC manual doesn't appear to explicitly say that
129*8b26181fSAndroid Build Coastguard Worker  * __attribute__((packed)) causes the compiler to generate unaligned-safe
130*8b26181fSAndroid Build Coastguard Worker  * code, but it appears to do so.
131*8b26181fSAndroid Build Coastguard Worker  *
132*8b26181fSAndroid Build Coastguard Worker  * We do this in case the compiler can generate code using those
133*8b26181fSAndroid Build Coastguard Worker  * instructions to do an unaligned load and pass stuff to "ntohs()" or
134*8b26181fSAndroid Build Coastguard Worker  * "ntohl()", which might be better than the code to fetch the
135*8b26181fSAndroid Build Coastguard Worker  * bytes one at a time and assemble them.  (That might not be the
136*8b26181fSAndroid Build Coastguard Worker  * case on a little-endian platform, such as DEC's MIPS machines and
137*8b26181fSAndroid Build Coastguard Worker  * Alpha machines, where "ntohs()" and "ntohl()" might not be done
138*8b26181fSAndroid Build Coastguard Worker  * inline.)
139*8b26181fSAndroid Build Coastguard Worker  *
140*8b26181fSAndroid Build Coastguard Worker  * We do this only for specific architectures because, for example,
141*8b26181fSAndroid Build Coastguard Worker  * at least some versions of GCC, when compiling for 64-bit SPARC,
142*8b26181fSAndroid Build Coastguard Worker  * generate code that assumes alignment if we do this.
143*8b26181fSAndroid Build Coastguard Worker  *
144*8b26181fSAndroid Build Coastguard Worker  * XXX - add other architectures and compilers as possible and
145*8b26181fSAndroid Build Coastguard Worker  * appropriate.
146*8b26181fSAndroid Build Coastguard Worker  *
147*8b26181fSAndroid Build Coastguard Worker  * HP's C compiler, indicated by __HP_cc being defined, supports
148*8b26181fSAndroid Build Coastguard Worker  * "#pragma unaligned N" in version A.05.50 and later, where "N"
149*8b26181fSAndroid Build Coastguard Worker  * specifies a number of bytes at which the typedef on the next
150*8b26181fSAndroid Build Coastguard Worker  * line is aligned, e.g.
151*8b26181fSAndroid Build Coastguard Worker  *
152*8b26181fSAndroid Build Coastguard Worker  *	#pragma unalign 1
153*8b26181fSAndroid Build Coastguard Worker  *	typedef uint16_t unaligned_uint16_t;
154*8b26181fSAndroid Build Coastguard Worker  *
155*8b26181fSAndroid Build Coastguard Worker  * to define unaligned_uint16_t as a 16-bit unaligned data type.
156*8b26181fSAndroid Build Coastguard Worker  * This could be presumably used, in sufficiently recent versions of
157*8b26181fSAndroid Build Coastguard Worker  * the compiler, with macros similar to those below.  This would be
158*8b26181fSAndroid Build Coastguard Worker  * useful only if that compiler could generate better code for PA-RISC
159*8b26181fSAndroid Build Coastguard Worker  * or Itanium than would be generated by a bunch of shifts-and-ORs.
160*8b26181fSAndroid Build Coastguard Worker  *
161*8b26181fSAndroid Build Coastguard Worker  * DEC C, indicated by __DECC being defined, has, at least on Alpha,
162*8b26181fSAndroid Build Coastguard Worker  * an __unaligned qualifier that can be applied to pointers to get the
163*8b26181fSAndroid Build Coastguard Worker  * compiler to generate code that does unaligned loads and stores when
164*8b26181fSAndroid Build Coastguard Worker  * dereferencing the pointer in question.
165*8b26181fSAndroid Build Coastguard Worker  *
166*8b26181fSAndroid Build Coastguard Worker  * XXX - what if the native C compiler doesn't support
167*8b26181fSAndroid Build Coastguard Worker  * __attribute__((packed))?  How can we get it to generate unaligned
168*8b26181fSAndroid Build Coastguard Worker  * accesses for *specific* items?
169*8b26181fSAndroid Build Coastguard Worker  */
170*8b26181fSAndroid Build Coastguard Worker typedef struct {
171*8b26181fSAndroid Build Coastguard Worker 	uint16_t	val;
172*8b26181fSAndroid Build Coastguard Worker } __attribute__((packed)) unaligned_uint16_t;
173*8b26181fSAndroid Build Coastguard Worker 
174*8b26181fSAndroid Build Coastguard Worker typedef struct {
175*8b26181fSAndroid Build Coastguard Worker 	int16_t		val;
176*8b26181fSAndroid Build Coastguard Worker } __attribute__((packed)) unaligned_int16_t;
177*8b26181fSAndroid Build Coastguard Worker 
178*8b26181fSAndroid Build Coastguard Worker typedef struct {
179*8b26181fSAndroid Build Coastguard Worker 	uint32_t	val;
180*8b26181fSAndroid Build Coastguard Worker } __attribute__((packed)) unaligned_uint32_t;
181*8b26181fSAndroid Build Coastguard Worker 
182*8b26181fSAndroid Build Coastguard Worker typedef struct {
183*8b26181fSAndroid Build Coastguard Worker 	int32_t		val;
184*8b26181fSAndroid Build Coastguard Worker } __attribute__((packed)) unaligned_int32_t;
185*8b26181fSAndroid Build Coastguard Worker 
186*8b26181fSAndroid Build Coastguard Worker UNALIGNED_OK static inline uint16_t
EXTRACT_BE_U_2(const void * p)187*8b26181fSAndroid Build Coastguard Worker EXTRACT_BE_U_2(const void *p)
188*8b26181fSAndroid Build Coastguard Worker {
189*8b26181fSAndroid Build Coastguard Worker 	return ((uint16_t)ntohs(((const unaligned_uint16_t *)(p))->val));
190*8b26181fSAndroid Build Coastguard Worker }
191*8b26181fSAndroid Build Coastguard Worker 
192*8b26181fSAndroid Build Coastguard Worker UNALIGNED_OK static inline int16_t
EXTRACT_BE_S_2(const void * p)193*8b26181fSAndroid Build Coastguard Worker EXTRACT_BE_S_2(const void *p)
194*8b26181fSAndroid Build Coastguard Worker {
195*8b26181fSAndroid Build Coastguard Worker 	return ((int16_t)ntohs(((const unaligned_int16_t *)(p))->val));
196*8b26181fSAndroid Build Coastguard Worker }
197*8b26181fSAndroid Build Coastguard Worker 
198*8b26181fSAndroid Build Coastguard Worker UNALIGNED_OK static inline uint32_t
EXTRACT_BE_U_4(const void * p)199*8b26181fSAndroid Build Coastguard Worker EXTRACT_BE_U_4(const void *p)
200*8b26181fSAndroid Build Coastguard Worker {
201*8b26181fSAndroid Build Coastguard Worker 	return ((uint32_t)ntohl(((const unaligned_uint32_t *)(p))->val));
202*8b26181fSAndroid Build Coastguard Worker }
203*8b26181fSAndroid Build Coastguard Worker 
204*8b26181fSAndroid Build Coastguard Worker UNALIGNED_OK static inline int32_t
EXTRACT_BE_S_4(const void * p)205*8b26181fSAndroid Build Coastguard Worker EXTRACT_BE_S_4(const void *p)
206*8b26181fSAndroid Build Coastguard Worker {
207*8b26181fSAndroid Build Coastguard Worker 	return ((int32_t)ntohl(((const unaligned_int32_t *)(p))->val));
208*8b26181fSAndroid Build Coastguard Worker }
209*8b26181fSAndroid Build Coastguard Worker 
210*8b26181fSAndroid Build Coastguard Worker UNALIGNED_OK static inline uint64_t
EXTRACT_BE_U_8(const void * p)211*8b26181fSAndroid Build Coastguard Worker EXTRACT_BE_U_8(const void *p)
212*8b26181fSAndroid Build Coastguard Worker {
213*8b26181fSAndroid Build Coastguard Worker 	return ((uint64_t)(((uint64_t)ntohl(((const unaligned_uint32_t *)(p) + 0)->val)) << 32 |
214*8b26181fSAndroid Build Coastguard Worker 		((uint64_t)ntohl(((const unaligned_uint32_t *)(p) + 1)->val)) << 0));
215*8b26181fSAndroid Build Coastguard Worker }
216*8b26181fSAndroid Build Coastguard Worker 
217*8b26181fSAndroid Build Coastguard Worker UNALIGNED_OK static inline int64_t
EXTRACT_BE_S_8(const void * p)218*8b26181fSAndroid Build Coastguard Worker EXTRACT_BE_S_8(const void *p)
219*8b26181fSAndroid Build Coastguard Worker {
220*8b26181fSAndroid Build Coastguard Worker 	return ((int64_t)(((uint64_t)ntohl(((const unaligned_uint32_t *)(p) + 0)->val)) << 32 |
221*8b26181fSAndroid Build Coastguard Worker 		((uint64_t)ntohl(((const unaligned_uint32_t *)(p) + 1)->val)) << 0));
222*8b26181fSAndroid Build Coastguard Worker }
223*8b26181fSAndroid Build Coastguard Worker #else
224*8b26181fSAndroid Build Coastguard Worker /*
225*8b26181fSAndroid Build Coastguard Worker  * This architecture doesn't natively support unaligned loads, and either
226*8b26181fSAndroid Build Coastguard Worker  * this isn't a GCC-compatible compiler, we don't have __attribute__,
227*8b26181fSAndroid Build Coastguard Worker  * or we do but we don't know of any better way with this instruction
228*8b26181fSAndroid Build Coastguard Worker  * set to do unaligned loads, so do unaligned loads of big-endian
229*8b26181fSAndroid Build Coastguard Worker  * quantities the hard way - fetch the bytes one at a time and
230*8b26181fSAndroid Build Coastguard Worker  * assemble them.
231*8b26181fSAndroid Build Coastguard Worker  *
232*8b26181fSAndroid Build Coastguard Worker  * XXX - ARM is a special case.  ARMv1 through ARMv5 didn't suppory
233*8b26181fSAndroid Build Coastguard Worker  * unaligned loads; ARMv6 and later support it *but* have a bit in
234*8b26181fSAndroid Build Coastguard Worker  * the system control register that the OS can set and that causes
235*8b26181fSAndroid Build Coastguard Worker  * unaligned loads to fault rather than succeeding.
236*8b26181fSAndroid Build Coastguard Worker  *
237*8b26181fSAndroid Build Coastguard Worker  * At least some OSes may set that flag, so we do *not* treat ARM
238*8b26181fSAndroid Build Coastguard Worker  * as supporting unaligned loads.  If your OS supports them on ARM,
239*8b26181fSAndroid Build Coastguard Worker  * and you want to use them, please update the tests in the #if above
240*8b26181fSAndroid Build Coastguard Worker  * to check for ARM *and* for your OS.
241*8b26181fSAndroid Build Coastguard Worker  */
242*8b26181fSAndroid Build Coastguard Worker #define EXTRACT_BE_U_2(p) \
243*8b26181fSAndroid Build Coastguard Worker 	((uint16_t)(((uint16_t)(*((const uint8_t *)(p) + 0)) << 8) | \
244*8b26181fSAndroid Build Coastguard Worker 	            ((uint16_t)(*((const uint8_t *)(p) + 1)) << 0)))
245*8b26181fSAndroid Build Coastguard Worker #define EXTRACT_BE_S_2(p) \
246*8b26181fSAndroid Build Coastguard Worker 	((int16_t)(((uint16_t)(*((const uint8_t *)(p) + 0)) << 8) | \
247*8b26181fSAndroid Build Coastguard Worker 	           ((uint16_t)(*((const uint8_t *)(p) + 1)) << 0)))
248*8b26181fSAndroid Build Coastguard Worker #define EXTRACT_BE_U_4(p) \
249*8b26181fSAndroid Build Coastguard Worker 	((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 0)) << 24) | \
250*8b26181fSAndroid Build Coastguard Worker 	            ((uint32_t)(*((const uint8_t *)(p) + 1)) << 16) | \
251*8b26181fSAndroid Build Coastguard Worker 	            ((uint32_t)(*((const uint8_t *)(p) + 2)) << 8) | \
252*8b26181fSAndroid Build Coastguard Worker 	            ((uint32_t)(*((const uint8_t *)(p) + 3)) << 0)))
253*8b26181fSAndroid Build Coastguard Worker #define EXTRACT_BE_S_4(p) \
254*8b26181fSAndroid Build Coastguard Worker 	((int32_t)(((uint32_t)(*((const uint8_t *)(p) + 0)) << 24) | \
255*8b26181fSAndroid Build Coastguard Worker 	           ((uint32_t)(*((const uint8_t *)(p) + 1)) << 16) | \
256*8b26181fSAndroid Build Coastguard Worker 	           ((uint32_t)(*((const uint8_t *)(p) + 2)) << 8) | \
257*8b26181fSAndroid Build Coastguard Worker 	           ((uint32_t)(*((const uint8_t *)(p) + 3)) << 0)))
258*8b26181fSAndroid Build Coastguard Worker #define EXTRACT_BE_U_8(p) \
259*8b26181fSAndroid Build Coastguard Worker 	((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 56) | \
260*8b26181fSAndroid Build Coastguard Worker 	            ((uint64_t)(*((const uint8_t *)(p) + 1)) << 48) | \
261*8b26181fSAndroid Build Coastguard Worker 	            ((uint64_t)(*((const uint8_t *)(p) + 2)) << 40) | \
262*8b26181fSAndroid Build Coastguard Worker 	            ((uint64_t)(*((const uint8_t *)(p) + 3)) << 32) | \
263*8b26181fSAndroid Build Coastguard Worker 	            ((uint64_t)(*((const uint8_t *)(p) + 4)) << 24) | \
264*8b26181fSAndroid Build Coastguard Worker 	            ((uint64_t)(*((const uint8_t *)(p) + 5)) << 16) | \
265*8b26181fSAndroid Build Coastguard Worker 	            ((uint64_t)(*((const uint8_t *)(p) + 6)) << 8) | \
266*8b26181fSAndroid Build Coastguard Worker 	            ((uint64_t)(*((const uint8_t *)(p) + 7)) << 0)))
267*8b26181fSAndroid Build Coastguard Worker #define EXTRACT_BE_S_8(p) \
268*8b26181fSAndroid Build Coastguard Worker 	((int64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 56) | \
269*8b26181fSAndroid Build Coastguard Worker 	           ((uint64_t)(*((const uint8_t *)(p) + 1)) << 48) | \
270*8b26181fSAndroid Build Coastguard Worker 	           ((uint64_t)(*((const uint8_t *)(p) + 2)) << 40) | \
271*8b26181fSAndroid Build Coastguard Worker 	           ((uint64_t)(*((const uint8_t *)(p) + 3)) << 32) | \
272*8b26181fSAndroid Build Coastguard Worker 	           ((uint64_t)(*((const uint8_t *)(p) + 4)) << 24) | \
273*8b26181fSAndroid Build Coastguard Worker 	           ((uint64_t)(*((const uint8_t *)(p) + 5)) << 16) | \
274*8b26181fSAndroid Build Coastguard Worker 	           ((uint64_t)(*((const uint8_t *)(p) + 6)) << 8) | \
275*8b26181fSAndroid Build Coastguard Worker 	           ((uint64_t)(*((const uint8_t *)(p) + 7)) << 0)))
276*8b26181fSAndroid Build Coastguard Worker 
277*8b26181fSAndroid Build Coastguard Worker /*
278*8b26181fSAndroid Build Coastguard Worker  * Extract an IPv4 address, which is in network byte order, and not
279*8b26181fSAndroid Build Coastguard Worker  * necessarily aligned, and provide the result in host byte order.
280*8b26181fSAndroid Build Coastguard Worker  */
281*8b26181fSAndroid Build Coastguard Worker #define EXTRACT_IPV4_TO_HOST_ORDER(p) \
282*8b26181fSAndroid Build Coastguard Worker 	((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 0)) << 24) | \
283*8b26181fSAndroid Build Coastguard Worker 	            ((uint32_t)(*((const uint8_t *)(p) + 1)) << 16) | \
284*8b26181fSAndroid Build Coastguard Worker 	            ((uint32_t)(*((const uint8_t *)(p) + 2)) << 8) | \
285*8b26181fSAndroid Build Coastguard Worker 	            ((uint32_t)(*((const uint8_t *)(p) + 3)) << 0)))
286*8b26181fSAndroid Build Coastguard Worker #endif /* unaligned access checks */
287*8b26181fSAndroid Build Coastguard Worker 
288*8b26181fSAndroid Build Coastguard Worker /*
289*8b26181fSAndroid Build Coastguard Worker  * Non-power-of-2 sizes.
290*8b26181fSAndroid Build Coastguard Worker  */
291*8b26181fSAndroid Build Coastguard Worker #define EXTRACT_BE_U_3(p) \
292*8b26181fSAndroid Build Coastguard Worker 	((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 0)) << 16) | \
293*8b26181fSAndroid Build Coastguard Worker 	            ((uint32_t)(*((const uint8_t *)(p) + 1)) << 8) | \
294*8b26181fSAndroid Build Coastguard Worker 	            ((uint32_t)(*((const uint8_t *)(p) + 2)) << 0)))
295*8b26181fSAndroid Build Coastguard Worker 
296*8b26181fSAndroid Build Coastguard Worker #define EXTRACT_BE_S_3(p) \
297*8b26181fSAndroid Build Coastguard Worker 	(((*((const uint8_t *)(p) + 0)) & 0x80) ? \
298*8b26181fSAndroid Build Coastguard Worker 	  ((int32_t)(((uint32_t)(*((const uint8_t *)(p) + 0)) << 16) | \
299*8b26181fSAndroid Build Coastguard Worker 	             ((uint32_t)(*((const uint8_t *)(p) + 1)) << 8) | \
300*8b26181fSAndroid Build Coastguard Worker 	             ((uint32_t)(*((const uint8_t *)(p) + 2)) << 0))) : \
301*8b26181fSAndroid Build Coastguard Worker 	  ((int32_t)(0xFF000000U | \
302*8b26181fSAndroid Build Coastguard Worker 	             ((uint32_t)(*((const uint8_t *)(p) + 0)) << 16) | \
303*8b26181fSAndroid Build Coastguard Worker 	             ((uint32_t)(*((const uint8_t *)(p) + 1)) << 8) | \
304*8b26181fSAndroid Build Coastguard Worker 	             ((uint32_t)(*((const uint8_t *)(p) + 2)) << 0))))
305*8b26181fSAndroid Build Coastguard Worker 
306*8b26181fSAndroid Build Coastguard Worker #define EXTRACT_BE_U_5(p) \
307*8b26181fSAndroid Build Coastguard Worker 	((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 32) | \
308*8b26181fSAndroid Build Coastguard Worker 	            ((uint64_t)(*((const uint8_t *)(p) + 1)) << 24) | \
309*8b26181fSAndroid Build Coastguard Worker 	            ((uint64_t)(*((const uint8_t *)(p) + 2)) << 16) | \
310*8b26181fSAndroid Build Coastguard Worker 	            ((uint64_t)(*((const uint8_t *)(p) + 3)) << 8) | \
311*8b26181fSAndroid Build Coastguard Worker 	            ((uint64_t)(*((const uint8_t *)(p) + 4)) << 0)))
312*8b26181fSAndroid Build Coastguard Worker 
313*8b26181fSAndroid Build Coastguard Worker #define EXTRACT_BE_S_5(p) \
314*8b26181fSAndroid Build Coastguard Worker 	(((*((const uint8_t *)(p) + 0)) & 0x80) ? \
315*8b26181fSAndroid Build Coastguard Worker 	  ((int64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 32) | \
316*8b26181fSAndroid Build Coastguard Worker 	             ((uint64_t)(*((const uint8_t *)(p) + 1)) << 24) | \
317*8b26181fSAndroid Build Coastguard Worker 	             ((uint64_t)(*((const uint8_t *)(p) + 2)) << 16) | \
318*8b26181fSAndroid Build Coastguard Worker 	             ((uint64_t)(*((const uint8_t *)(p) + 3)) << 8) | \
319*8b26181fSAndroid Build Coastguard Worker 	             ((uint64_t)(*((const uint8_t *)(p) + 4)) << 0))) : \
320*8b26181fSAndroid Build Coastguard Worker 	  ((int64_t)(INT64_T_CONSTANT(0xFFFFFF0000000000U) | \
321*8b26181fSAndroid Build Coastguard Worker 	             ((uint64_t)(*((const uint8_t *)(p) + 0)) << 32) | \
322*8b26181fSAndroid Build Coastguard Worker 	             ((uint64_t)(*((const uint8_t *)(p) + 1)) << 24) | \
323*8b26181fSAndroid Build Coastguard Worker 	             ((uint64_t)(*((const uint8_t *)(p) + 2)) << 16) | \
324*8b26181fSAndroid Build Coastguard Worker 	             ((uint64_t)(*((const uint8_t *)(p) + 3)) << 8) | \
325*8b26181fSAndroid Build Coastguard Worker 	             ((uint64_t)(*((const uint8_t *)(p) + 4)) << 0))))
326*8b26181fSAndroid Build Coastguard Worker 
327*8b26181fSAndroid Build Coastguard Worker #define EXTRACT_BE_U_6(p) \
328*8b26181fSAndroid Build Coastguard Worker 	((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 40) | \
329*8b26181fSAndroid Build Coastguard Worker 	            ((uint64_t)(*((const uint8_t *)(p) + 1)) << 32) | \
330*8b26181fSAndroid Build Coastguard Worker 	            ((uint64_t)(*((const uint8_t *)(p) + 2)) << 24) | \
331*8b26181fSAndroid Build Coastguard Worker 	            ((uint64_t)(*((const uint8_t *)(p) + 3)) << 16) | \
332*8b26181fSAndroid Build Coastguard Worker 	            ((uint64_t)(*((const uint8_t *)(p) + 4)) << 8) | \
333*8b26181fSAndroid Build Coastguard Worker 	            ((uint64_t)(*((const uint8_t *)(p) + 5)) << 0)))
334*8b26181fSAndroid Build Coastguard Worker 
335*8b26181fSAndroid Build Coastguard Worker #define EXTRACT_BE_S_6(p) \
336*8b26181fSAndroid Build Coastguard Worker 	(((*((const uint8_t *)(p) + 0)) & 0x80) ? \
337*8b26181fSAndroid Build Coastguard Worker 	   ((int64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 40) | \
338*8b26181fSAndroid Build Coastguard Worker 	              ((uint64_t)(*((const uint8_t *)(p) + 1)) << 32) | \
339*8b26181fSAndroid Build Coastguard Worker 	              ((uint64_t)(*((const uint8_t *)(p) + 2)) << 24) | \
340*8b26181fSAndroid Build Coastguard Worker 	              ((uint64_t)(*((const uint8_t *)(p) + 3)) << 16) | \
341*8b26181fSAndroid Build Coastguard Worker 	              ((uint64_t)(*((const uint8_t *)(p) + 4)) << 8) | \
342*8b26181fSAndroid Build Coastguard Worker 	              ((uint64_t)(*((const uint8_t *)(p) + 5)) << 0))) : \
343*8b26181fSAndroid Build Coastguard Worker 	  ((int64_t)(INT64_T_CONSTANT(0xFFFFFFFF00000000U) | \
344*8b26181fSAndroid Build Coastguard Worker 	              ((uint64_t)(*((const uint8_t *)(p) + 0)) << 40) | \
345*8b26181fSAndroid Build Coastguard Worker 	              ((uint64_t)(*((const uint8_t *)(p) + 1)) << 32) | \
346*8b26181fSAndroid Build Coastguard Worker 	              ((uint64_t)(*((const uint8_t *)(p) + 2)) << 24) | \
347*8b26181fSAndroid Build Coastguard Worker 	              ((uint64_t)(*((const uint8_t *)(p) + 3)) << 16) | \
348*8b26181fSAndroid Build Coastguard Worker 	              ((uint64_t)(*((const uint8_t *)(p) + 4)) << 8) | \
349*8b26181fSAndroid Build Coastguard Worker 	              ((uint64_t)(*((const uint8_t *)(p) + 5)) << 0))))
350*8b26181fSAndroid Build Coastguard Worker 
351*8b26181fSAndroid Build Coastguard Worker #define EXTRACT_BE_U_7(p) \
352*8b26181fSAndroid Build Coastguard Worker 	((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 48) | \
353*8b26181fSAndroid Build Coastguard Worker 	            ((uint64_t)(*((const uint8_t *)(p) + 1)) << 40) | \
354*8b26181fSAndroid Build Coastguard Worker 	            ((uint64_t)(*((const uint8_t *)(p) + 2)) << 32) | \
355*8b26181fSAndroid Build Coastguard Worker 	            ((uint64_t)(*((const uint8_t *)(p) + 3)) << 24) | \
356*8b26181fSAndroid Build Coastguard Worker 	            ((uint64_t)(*((const uint8_t *)(p) + 4)) << 16) | \
357*8b26181fSAndroid Build Coastguard Worker 	            ((uint64_t)(*((const uint8_t *)(p) + 5)) << 8) | \
358*8b26181fSAndroid Build Coastguard Worker 	            ((uint64_t)(*((const uint8_t *)(p) + 6)) << 0)))
359*8b26181fSAndroid Build Coastguard Worker 
360*8b26181fSAndroid Build Coastguard Worker #define EXTRACT_BE_S_7(p) \
361*8b26181fSAndroid Build Coastguard Worker 	(((*((const uint8_t *)(p) + 0)) & 0x80) ? \
362*8b26181fSAndroid Build Coastguard Worker 	  ((int64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 48) | \
363*8b26181fSAndroid Build Coastguard Worker 	             ((uint64_t)(*((const uint8_t *)(p) + 1)) << 40) | \
364*8b26181fSAndroid Build Coastguard Worker 	             ((uint64_t)(*((const uint8_t *)(p) + 2)) << 32) | \
365*8b26181fSAndroid Build Coastguard Worker 	             ((uint64_t)(*((const uint8_t *)(p) + 3)) << 24) | \
366*8b26181fSAndroid Build Coastguard Worker 	             ((uint64_t)(*((const uint8_t *)(p) + 4)) << 16) | \
367*8b26181fSAndroid Build Coastguard Worker 	             ((uint64_t)(*((const uint8_t *)(p) + 5)) << 8) | \
368*8b26181fSAndroid Build Coastguard Worker 	             ((uint64_t)(*((const uint8_t *)(p) + 6)) << 0))) : \
369*8b26181fSAndroid Build Coastguard Worker 	    ((int64_t)(INT64_T_CONSTANT(0xFFFFFFFFFF000000U) | \
370*8b26181fSAndroid Build Coastguard Worker 	             ((uint64_t)(*((const uint8_t *)(p) + 0)) << 48) | \
371*8b26181fSAndroid Build Coastguard Worker 	             ((uint64_t)(*((const uint8_t *)(p) + 1)) << 40) | \
372*8b26181fSAndroid Build Coastguard Worker 	             ((uint64_t)(*((const uint8_t *)(p) + 2)) << 32) | \
373*8b26181fSAndroid Build Coastguard Worker 	             ((uint64_t)(*((const uint8_t *)(p) + 3)) << 24) | \
374*8b26181fSAndroid Build Coastguard Worker 	             ((uint64_t)(*((const uint8_t *)(p) + 4)) << 16) | \
375*8b26181fSAndroid Build Coastguard Worker 	             ((uint64_t)(*((const uint8_t *)(p) + 5)) << 8) | \
376*8b26181fSAndroid Build Coastguard Worker 	             ((uint64_t)(*((const uint8_t *)(p) + 6)) << 0))))
377*8b26181fSAndroid Build Coastguard Worker 
378*8b26181fSAndroid Build Coastguard Worker /*
379*8b26181fSAndroid Build Coastguard Worker  * Macros to extract possibly-unaligned little-endian integral values.
380*8b26181fSAndroid Build Coastguard Worker  * XXX - do loads on little-endian machines that support unaligned loads?
381*8b26181fSAndroid Build Coastguard Worker  */
382*8b26181fSAndroid Build Coastguard Worker #define EXTRACT_LE_U_2(p) \
383*8b26181fSAndroid Build Coastguard Worker 	((uint16_t)(((uint16_t)(*((const uint8_t *)(p) + 1)) << 8) | \
384*8b26181fSAndroid Build Coastguard Worker 	            ((uint16_t)(*((const uint8_t *)(p) + 0)) << 0)))
385*8b26181fSAndroid Build Coastguard Worker #define EXTRACT_LE_S_2(p) \
386*8b26181fSAndroid Build Coastguard Worker 	((int16_t)(((uint16_t)(*((const uint8_t *)(p) + 1)) << 8) | \
387*8b26181fSAndroid Build Coastguard Worker 	           ((uint16_t)(*((const uint8_t *)(p) + 0)) << 0)))
388*8b26181fSAndroid Build Coastguard Worker #define EXTRACT_LE_U_4(p) \
389*8b26181fSAndroid Build Coastguard Worker 	((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 3)) << 24) | \
390*8b26181fSAndroid Build Coastguard Worker 	            ((uint32_t)(*((const uint8_t *)(p) + 2)) << 16) | \
391*8b26181fSAndroid Build Coastguard Worker 	            ((uint32_t)(*((const uint8_t *)(p) + 1)) << 8) | \
392*8b26181fSAndroid Build Coastguard Worker 	            ((uint32_t)(*((const uint8_t *)(p) + 0)) << 0)))
393*8b26181fSAndroid Build Coastguard Worker #define EXTRACT_LE_S_4(p) \
394*8b26181fSAndroid Build Coastguard Worker 	((int32_t)(((uint32_t)(*((const uint8_t *)(p) + 3)) << 24) | \
395*8b26181fSAndroid Build Coastguard Worker 	           ((uint32_t)(*((const uint8_t *)(p) + 2)) << 16) | \
396*8b26181fSAndroid Build Coastguard Worker 	           ((uint32_t)(*((const uint8_t *)(p) + 1)) << 8) | \
397*8b26181fSAndroid Build Coastguard Worker 	           ((uint32_t)(*((const uint8_t *)(p) + 0)) << 0)))
398*8b26181fSAndroid Build Coastguard Worker #define EXTRACT_LE_U_3(p) \
399*8b26181fSAndroid Build Coastguard Worker 	((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 2)) << 16) | \
400*8b26181fSAndroid Build Coastguard Worker 	            ((uint32_t)(*((const uint8_t *)(p) + 1)) << 8) | \
401*8b26181fSAndroid Build Coastguard Worker 	            ((uint32_t)(*((const uint8_t *)(p) + 0)) << 0)))
402*8b26181fSAndroid Build Coastguard Worker #define EXTRACT_LE_S_3(p) \
403*8b26181fSAndroid Build Coastguard Worker 	((int32_t)(((uint32_t)(*((const uint8_t *)(p) + 2)) << 16) | \
404*8b26181fSAndroid Build Coastguard Worker 	           ((uint32_t)(*((const uint8_t *)(p) + 1)) << 8) | \
405*8b26181fSAndroid Build Coastguard Worker 	           ((uint32_t)(*((const uint8_t *)(p) + 0)) << 0)))
406*8b26181fSAndroid Build Coastguard Worker #define EXTRACT_LE_U_8(p) \
407*8b26181fSAndroid Build Coastguard Worker 	((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 7)) << 56) | \
408*8b26181fSAndroid Build Coastguard Worker 	            ((uint64_t)(*((const uint8_t *)(p) + 6)) << 48) | \
409*8b26181fSAndroid Build Coastguard Worker 	            ((uint64_t)(*((const uint8_t *)(p) + 5)) << 40) | \
410*8b26181fSAndroid Build Coastguard Worker 	            ((uint64_t)(*((const uint8_t *)(p) + 4)) << 32) | \
411*8b26181fSAndroid Build Coastguard Worker 	            ((uint64_t)(*((const uint8_t *)(p) + 3)) << 24) | \
412*8b26181fSAndroid Build Coastguard Worker 	            ((uint64_t)(*((const uint8_t *)(p) + 2)) << 16) | \
413*8b26181fSAndroid Build Coastguard Worker 	            ((uint64_t)(*((const uint8_t *)(p) + 1)) << 8) | \
414*8b26181fSAndroid Build Coastguard Worker 	            ((uint64_t)(*((const uint8_t *)(p) + 0)) << 0)))
415*8b26181fSAndroid Build Coastguard Worker #define EXTRACT_LE_S_8(p) \
416*8b26181fSAndroid Build Coastguard Worker 	((int64_t)(((uint64_t)(*((const uint8_t *)(p) + 7)) << 56) | \
417*8b26181fSAndroid Build Coastguard Worker 	           ((uint64_t)(*((const uint8_t *)(p) + 6)) << 48) | \
418*8b26181fSAndroid Build Coastguard Worker 	           ((uint64_t)(*((const uint8_t *)(p) + 5)) << 40) | \
419*8b26181fSAndroid Build Coastguard Worker 	           ((uint64_t)(*((const uint8_t *)(p) + 4)) << 32) | \
420*8b26181fSAndroid Build Coastguard Worker 	           ((uint64_t)(*((const uint8_t *)(p) + 3)) << 24) | \
421*8b26181fSAndroid Build Coastguard Worker 	           ((uint64_t)(*((const uint8_t *)(p) + 2)) << 16) | \
422*8b26181fSAndroid Build Coastguard Worker 	           ((uint64_t)(*((const uint8_t *)(p) + 1)) << 8) | \
423*8b26181fSAndroid Build Coastguard Worker 	           ((uint64_t)(*((const uint8_t *)(p) + 0)) << 0)))
424