]>
The Tcpdump Group git mirrors - tcpdump/blob - extract.h
5a773a4b29df087668f6777c2a3779349c074433
2 * Copyright (c) 1992, 1993, 1994, 1995, 1996
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
23 * For 8-bit values; needed to fetch a one-byte value. Byte order
24 * isn't relevant, and alignment isn't an issue.
26 #define EXTRACT_8BITS(p) (*(p))
29 * Inline functions or macros to extract possibly-unaligned big-endian
32 #include "funcattrs.h"
35 * If we have versions of GCC or Clang that support an __attribute__
36 * to say "if we're building with unsigned behavior sanitization,
37 * don't complain about undefined behavior in this function", we
38 * label these functions with that attribute - we *know* it's undefined
39 * in the C standard, but we *also* know it does what we want with
40 * the ISA we're targeting and the compiler we're using.
42 * For GCC 4.9.0 and later, we use __attribute__((no_sanitize_undefined));
43 * pre-5.0 GCC doesn't have __has_attribute, and I'm not sure whether
44 * GCC or Clang first had __attribute__((no_sanitize(XXX)).
46 * For Clang, we check for __attribute__((no_sanitize(XXX)) with
47 * __has_attribute, as there are versions of Clang that support
48 * __attribute__((no_sanitize("undefined")) but don't support
49 * __attribute__((no_sanitize_undefined)).
51 * We define this here, rather than in funcattrs.h, because we
52 * only want it used here, we don't want it to be broadly used.
53 * (Any printer will get this defined, but this should at least
54 * make it harder for people to find.)
56 #if defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 409)
57 #define UNALIGNED_OK __attribute__((no_sanitize_undefined))
58 #elif __has_attribute(no_sanitize)
59 #define UNALIGNED_OK __attribute__((no_sanitize("undefined")))
64 #if (defined(__i386__) || defined(_M_IX86) || defined(__X86__) || defined(__x86_64__) || defined(_M_X64)) || \
65 (defined(__arm__) || defined(_M_ARM) || defined(__aarch64__)) || \
66 (defined(__m68k__) && (!defined(__mc68000__) && !defined(__mc68010__))) || \
67 (defined(__ppc__) || defined(__ppc64__) || defined(_M_PPC) || defined(_ARCH_PPC) || defined(_ARCH_PPC64)) || \
68 (defined(__s390__) || defined(__s390x__) || defined(__zarch__))
70 * The processor natively handles unaligned loads, so we can just
71 * cast the pointer and fetch through it.
73 * XXX - are those all the x86 tests we need?
74 * XXX - do we need to worry about ARMv1 through ARMv5, which didn't
75 * support unaligned loads, and, if so, do we need to worry about all
76 * of them, or just some of them, e.g. ARMv5?
77 * XXX - are those the only 68k tests we need not to generated
78 * unaligned accesses if the target is the 68000 or 68010?
79 * XXX - are there any tests we don't need, because some definitions are for
80 * compilers that also predefine the GCC symbols?
81 * XXX - do we need to test for both 32-bit and 64-bit versions of those
82 * architectures in all cases?
84 static inline uint16_t UNALIGNED_OK
85 EXTRACT_BE_16BITS(const void *p
)
87 return ((uint16_t)ntohs(*(const uint16_t *)(p
)));
90 static inline uint32_t UNALIGNED_OK
91 EXTRACT_BE_32BITS(const void *p
)
93 return ((uint32_t)ntohl(*(const uint32_t *)(p
)));
96 static inline uint64_t UNALIGNED_OK
97 EXTRACT_BE_64BITS(const void *p
)
99 return ((uint64_t)(((uint64_t)ntohl(*((const uint32_t *)(p
) + 0))) << 32 |
100 ((uint64_t)ntohl(*((const uint32_t *)(p
) + 1))) << 0));
103 #elif defined(__GNUC__) && defined(HAVE___ATTRIBUTE__) && \
104 (defined(__alpha) || defined(__alpha__) || \
105 defined(__mips) || defined(__mips__))
107 * This is MIPS or Alpha, which don't natively handle unaligned loads,
108 * but which have instructions that can help when doing unaligned
109 * loads, and this is a GCC-compatible compiler and we have __attribute__,
110 * which we assume that mean we have __attribute__((packed)), which
111 * we can use to convince the compiler to generate those instructions.
113 * Declare packed structures containing a uint16_t and a uint32_t,
114 * cast the pointer to point to one of those, and fetch through it;
115 * the GCC manual doesn't appear to explicitly say that
116 * __attribute__((packed)) causes the compiler to generate unaligned-safe
117 * code, but it apppears to do so.
119 * We do this in case the compiler can generate code using those
120 * instructions to do an unaligned load and pass stuff to "ntohs()" or
121 * "ntohl()", which might be better than than the code to fetch the
122 * bytes one at a time and assemble them. (That might not be the
123 * case on a little-endian platform, such as DEC's MIPS machines and
124 * Alpha machines, where "ntohs()" and "ntohl()" might not be done
127 * We do this only for specific architectures because, for example,
128 * at least some versions of GCC, when compiling for 64-bit SPARC,
129 * generate code that assumes alignment if we do this.
131 * XXX - add other architectures and compilers as possible and
134 * HP's C compiler, indicated by __HP_cc being defined, supports
135 * "#pragma unaligned N" in version A.05.50 and later, where "N"
136 * specifies a number of bytes at which the typedef on the next
137 * line is aligned, e.g.
140 * typedef uint16_t unaligned_uint16_t;
142 * to define unaligned_uint16_t as a 16-bit unaligned data type.
143 * This could be presumably used, in sufficiently recent versions of
144 * the compiler, with macros similar to those below. This would be
145 * useful only if that compiler could generate better code for PA-RISC
146 * or Itanium than would be generated by a bunch of shifts-and-ORs.
148 * DEC C, indicated by __DECC being defined, has, at least on Alpha,
149 * an __unaligned qualifier that can be applied to pointers to get the
150 * compiler to generate code that does unaligned loads and stores when
151 * dereferencing the pointer in question.
153 * XXX - what if the native C compiler doesn't support
154 * __attribute__((packed))? How can we get it to generate unaligned
155 * accesses for *specific* items?
159 } __attribute__((packed
)) unaligned_uint16_t
;
163 } __attribute__((packed
)) unaligned_uint32_t
;
165 UNALIGNED_OK
static inline uint16_t
166 EXTRACT_BE_16BITS(const void *p
)
168 return ((uint16_t)ntohs(((const unaligned_uint16_t
*)(p
))->val
));
171 UNALIGNED_OK
static inline uint32_t
172 EXTRACT_BE_32BITS(const void *p
)
174 return ((uint32_t)ntohl(((const unaligned_uint32_t
*)(p
))->val
));
177 UNALIGNED_OK
static inline uint64_t
178 EXTRACT_BE_64BITS(const void *p
)
180 return ((uint64_t)(((uint64_t)ntohl(((const unaligned_uint32_t
*)(p
) + 0)->val
)) << 32 |
181 ((uint64_t)ntohl(((const unaligned_uint32_t
*)(p
) + 1)->val
)) << 0));
185 * This architecture doesn't natively support unaligned loads, and either
186 * this isn't a GCC-compatible compiler, we don't have __attribute__,
187 * or we do but we don't know of any better way with this instruction
188 * set to do unaligned loads, so do unaligned loads of big-endian
189 * quantities the hard way - fetch the bytes one at a time and
192 #define EXTRACT_BE_16BITS(p) \
193 ((uint16_t)(((uint16_t)(*((const uint8_t *)(p) + 0)) << 8) | \
194 ((uint16_t)(*((const uint8_t *)(p) + 1)) << 0)))
195 #define EXTRACT_BE_32BITS(p) \
196 ((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 0)) << 24) | \
197 ((uint32_t)(*((const uint8_t *)(p) + 1)) << 16) | \
198 ((uint32_t)(*((const uint8_t *)(p) + 2)) << 8) | \
199 ((uint32_t)(*((const uint8_t *)(p) + 3)) << 0)))
200 #define EXTRACT_BE_64BITS(p) \
201 ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 56) | \
202 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 48) | \
203 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 40) | \
204 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 32) | \
205 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 24) | \
206 ((uint64_t)(*((const uint8_t *)(p) + 5)) << 16) | \
207 ((uint64_t)(*((const uint8_t *)(p) + 6)) << 8) | \
208 ((uint64_t)(*((const uint8_t *)(p) + 7)) << 0)))
209 #endif /* unaligned access checks */
211 #define EXTRACT_BE_24BITS(p) \
212 ((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 0)) << 16) | \
213 ((uint32_t)(*((const uint8_t *)(p) + 1)) << 8) | \
214 ((uint32_t)(*((const uint8_t *)(p) + 2)) << 0)))
216 #define EXTRACT_BE_40BITS(p) \
217 ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 32) | \
218 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 24) | \
219 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 16) | \
220 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 8) | \
221 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 0)))
223 #define EXTRACT_BE_48BITS(p) \
224 ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 40) | \
225 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 32) | \
226 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 24) | \
227 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 16) | \
228 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 8) | \
229 ((uint64_t)(*((const uint8_t *)(p) + 5)) << 0)))
231 #define EXTRACT_BE_56BITS(p) \
232 ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 48) | \
233 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 40) | \
234 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 32) | \
235 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 24) | \
236 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 16) | \
237 ((uint64_t)(*((const uint8_t *)(p) + 5)) << 8) | \
238 ((uint64_t)(*((const uint8_t *)(p) + 6)) << 0)))
241 * Macros to extract possibly-unaligned little-endian integral values.
242 * XXX - do loads on little-endian machines that support unaligned loads?
244 #define EXTRACT_LE_16BITS(p) \
245 ((uint16_t)(((uint16_t)(*((const uint8_t *)(p) + 1)) << 8) | \
246 ((uint16_t)(*((const uint8_t *)(p) + 0)) << 0)))
247 #define EXTRACT_LE_32BITS(p) \
248 ((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 3)) << 24) | \
249 ((uint32_t)(*((const uint8_t *)(p) + 2)) << 16) | \
250 ((uint32_t)(*((const uint8_t *)(p) + 1)) << 8) | \
251 ((uint32_t)(*((const uint8_t *)(p) + 0)) << 0)))
252 #define EXTRACT_LE_24BITS(p) \
253 ((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 2)) << 16) | \
254 ((uint32_t)(*((const uint8_t *)(p) + 1)) << 8) | \
255 ((uint32_t)(*((const uint8_t *)(p) + 0)) << 0)))
256 #define EXTRACT_LE_64BITS(p) \
257 ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 7)) << 56) | \
258 ((uint64_t)(*((const uint8_t *)(p) + 6)) << 48) | \
259 ((uint64_t)(*((const uint8_t *)(p) + 5)) << 40) | \
260 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 32) | \
261 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 24) | \
262 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 16) | \
263 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 8) | \
264 ((uint64_t)(*((const uint8_t *)(p) + 0)) << 0)))
267 * Macros to check the presence of the values in question.
269 #define ND_TTEST_8BITS(p) ND_TTEST2(*(p), 1)
270 #define ND_TCHECK_8BITS(p) ND_TCHECK2(*(p), 1)
272 #define ND_TTEST_16BITS(p) ND_TTEST2(*(p), 2)
273 #define ND_TCHECK_16BITS(p) ND_TCHECK2(*(p), 2)
275 #define ND_TTEST_24BITS(p) ND_TTEST2(*(p), 3)
276 #define ND_TCHECK_24BITS(p) ND_TCHECK2(*(p), 3)
278 #define ND_TTEST_32BITS(p) ND_TTEST2(*(p), 4)
279 #define ND_TCHECK_32BITS(p) ND_TCHECK2(*(p), 4)
281 #define ND_TTEST_40BITS(p) ND_TTEST2(*(p), 5)
282 #define ND_TCHECK_40BITS(p) ND_TCHECK2(*(p), 5)
284 #define ND_TTEST_48BITS(p) ND_TTEST2(*(p), 6)
285 #define ND_TCHECK_48BITS(p) ND_TCHECK2(*(p), 6)
287 #define ND_TTEST_56BITS(p) ND_TTEST2(*(p), 7)
288 #define ND_TCHECK_56BITS(p) ND_TCHECK2(*(p), 7)
290 #define ND_TTEST_64BITS(p) ND_TTEST2(*(p), 8)
291 #define ND_TCHECK_64BITS(p) ND_TCHECK2(*(p), 8)
293 #define ND_TTEST_128BITS(p) ND_TTEST2(*(p), 16)
294 #define ND_TCHECK_128BITS(p) ND_TCHECK2(*(p), 16)