]>
The Tcpdump Group git mirrors - tcpdump/blob - extract.h
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))
27 #define EXTRACT_INT8(p) ((int8_t)(*(p)))
30 * Inline functions or macros to extract possibly-unaligned big-endian
33 #include "funcattrs.h"
36 * If we have versions of GCC or Clang that support an __attribute__
37 * to say "if we're building with unsigned behavior sanitization,
38 * don't complain about undefined behavior in this function", we
39 * label these functions with that attribute - we *know* it's undefined
40 * in the C standard, but we *also* know it does what we want with
41 * the ISA we're targeting and the compiler we're using.
43 * For GCC 4.9.0 and later, we use __attribute__((no_sanitize_undefined));
44 * pre-5.0 GCC doesn't have __has_attribute, and I'm not sure whether
45 * GCC or Clang first had __attribute__((no_sanitize(XXX)).
47 * For Clang, we check for __attribute__((no_sanitize(XXX)) with
48 * __has_attribute, as there are versions of Clang that support
49 * __attribute__((no_sanitize("undefined")) but don't support
50 * __attribute__((no_sanitize_undefined)).
52 * We define this here, rather than in funcattrs.h, because we
53 * only want it used here, we don't want it to be broadly used.
54 * (Any printer will get this defined, but this should at least
55 * make it harder for people to find.)
57 #if defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 409)
58 #define UNALIGNED_OK __attribute__((no_sanitize_undefined))
59 #elif __has_attribute(no_sanitize)
60 #define UNALIGNED_OK __attribute__((no_sanitize("undefined")))
65 #if (defined(__i386__) || defined(_M_IX86) || defined(__X86__) || defined(__x86_64__) || defined(_M_X64)) || \
66 (defined(__arm__) || defined(_M_ARM) || defined(__aarch64__)) || \
67 (defined(__m68k__) && (!defined(__mc68000__) && !defined(__mc68010__))) || \
68 (defined(__ppc__) || defined(__ppc64__) || defined(_M_PPC) || defined(_ARCH_PPC) || defined(_ARCH_PPC64)) || \
69 (defined(__s390__) || defined(__s390x__) || defined(__zarch__))
71 * The processor natively handles unaligned loads, so we can just
72 * cast the pointer and fetch through it.
74 * XXX - are those all the x86 tests we need?
75 * XXX - do we need to worry about ARMv1 through ARMv5, which didn't
76 * support unaligned loads, and, if so, do we need to worry about all
77 * of them, or just some of them, e.g. ARMv5?
78 * XXX - are those the only 68k tests we need not to generated
79 * unaligned accesses if the target is the 68000 or 68010?
80 * XXX - are there any tests we don't need, because some definitions are for
81 * compilers that also predefine the GCC symbols?
82 * XXX - do we need to test for both 32-bit and 64-bit versions of those
83 * architectures in all cases?
85 static inline uint16_t UNALIGNED_OK
86 EXTRACT_BE_16BITS(const void *p
)
88 return ((uint16_t)ntohs(*(const uint16_t *)(p
)));
91 static inline int16_t UNALIGNED_OK
92 EXTRACT_BE_INT16(const void *p
)
94 return ((int16_t)ntohs(*(const int16_t *)(p
)));
97 static inline uint32_t UNALIGNED_OK
98 EXTRACT_BE_32BITS(const void *p
)
100 return ((uint32_t)ntohl(*(const uint32_t *)(p
)));
103 static inline int32_t UNALIGNED_OK
104 EXTRACT_BE_INT32(const void *p
)
106 return ((int32_t)ntohl(*(const int32_t *)(p
)));
109 static inline uint64_t UNALIGNED_OK
110 EXTRACT_BE_64BITS(const void *p
)
112 return ((uint64_t)(((uint64_t)ntohl(*((const uint32_t *)(p
) + 0))) << 32 |
113 ((uint64_t)ntohl(*((const uint32_t *)(p
) + 1))) << 0));
117 static inline int64_t UNALIGNED_OK
118 EXTRACT_BE_INT64(const void *p
)
120 return ((int64_t)(((int64_t)ntohl(*((const uint32_t *)(p
) + 0))) << 32 |
121 ((uint64_t)ntohl(*((const uint32_t *)(p
) + 1))) << 0));
124 #elif defined(__GNUC__) && defined(HAVE___ATTRIBUTE__) && \
125 (defined(__alpha) || defined(__alpha__) || \
126 defined(__mips) || defined(__mips__))
128 * This is MIPS or Alpha, which don't natively handle unaligned loads,
129 * but which have instructions that can help when doing unaligned
130 * loads, and this is a GCC-compatible compiler and we have __attribute__,
131 * which we assume that mean we have __attribute__((packed)), which
132 * we can use to convince the compiler to generate those instructions.
134 * Declare packed structures containing a uint16_t and a uint32_t,
135 * cast the pointer to point to one of those, and fetch through it;
136 * the GCC manual doesn't appear to explicitly say that
137 * __attribute__((packed)) causes the compiler to generate unaligned-safe
138 * code, but it apppears to do so.
140 * We do this in case the compiler can generate code using those
141 * instructions to do an unaligned load and pass stuff to "ntohs()" or
142 * "ntohl()", which might be better than than the code to fetch the
143 * bytes one at a time and assemble them. (That might not be the
144 * case on a little-endian platform, such as DEC's MIPS machines and
145 * Alpha machines, where "ntohs()" and "ntohl()" might not be done
148 * We do this only for specific architectures because, for example,
149 * at least some versions of GCC, when compiling for 64-bit SPARC,
150 * generate code that assumes alignment if we do this.
152 * XXX - add other architectures and compilers as possible and
155 * HP's C compiler, indicated by __HP_cc being defined, supports
156 * "#pragma unaligned N" in version A.05.50 and later, where "N"
157 * specifies a number of bytes at which the typedef on the next
158 * line is aligned, e.g.
161 * typedef uint16_t unaligned_uint16_t;
163 * to define unaligned_uint16_t as a 16-bit unaligned data type.
164 * This could be presumably used, in sufficiently recent versions of
165 * the compiler, with macros similar to those below. This would be
166 * useful only if that compiler could generate better code for PA-RISC
167 * or Itanium than would be generated by a bunch of shifts-and-ORs.
169 * DEC C, indicated by __DECC being defined, has, at least on Alpha,
170 * an __unaligned qualifier that can be applied to pointers to get the
171 * compiler to generate code that does unaligned loads and stores when
172 * dereferencing the pointer in question.
174 * XXX - what if the native C compiler doesn't support
175 * __attribute__((packed))? How can we get it to generate unaligned
176 * accesses for *specific* items?
180 } __attribute__((packed
)) unaligned_uint16_t
;
184 } __attribute__((packed
)) unaligned_int16_t
;
188 } __attribute__((packed
)) unaligned_uint32_t
;
192 } __attribute__((packed
)) unaligned_int32_t
;
194 UNALIGNED_OK
static inline uint16_t
195 EXTRACT_BE_16BITS(const void *p
)
197 return ((uint16_t)ntohs(((const unaligned_uint16_t
*)(p
))->val
));
200 UNALIGNED_OK
static inline int16_t
201 EXTRACT_BE_INT16(const void *p
)
203 return ((int16_t)ntohs(((const unaligned_int16_t
*)(p
))->val
));
206 UNALIGNED_OK
static inline uint32_t
207 EXTRACT_BE_32BITS(const void *p
)
209 return ((uint32_t)ntohl(((const unaligned_uint32_t
*)(p
))->val
));
212 UNALIGNED_OK
static inline int32_t
213 EXTRACT_BE_INT32(const void *p
)
215 return ((int32_t)ntohl(((const unaligned_int32_t
*)(p
))->val
));
218 UNALIGNED_OK
static inline uint64_t
219 EXTRACT_BE_64BITS(const void *p
)
221 return ((uint64_t)(((uint64_t)ntohl(((const unaligned_uint32_t
*)(p
) + 0)->val
)) << 32 |
222 ((uint64_t)ntohl(((const unaligned_uint32_t
*)(p
) + 1)->val
)) << 0));
225 UNALIGNED_OK
static inline int64_t
226 EXTRACT_BE_INT64(const void *p
)
228 return ((int64_t)(((uint64_t)ntohl(((const unaligned_uint32_t
*)(p
) + 0)->val
)) << 32 |
229 ((uint64_t)ntohl(((const unaligned_uint32_t
*)(p
) + 1)->val
)) << 0));
233 * This architecture doesn't natively support unaligned loads, and either
234 * this isn't a GCC-compatible compiler, we don't have __attribute__,
235 * or we do but we don't know of any better way with this instruction
236 * set to do unaligned loads, so do unaligned loads of big-endian
237 * quantities the hard way - fetch the bytes one at a time and
240 #define EXTRACT_BE_16BITS(p) \
241 ((uint16_t)(((uint16_t)(*((const uint8_t *)(p) + 0)) << 8) | \
242 ((uint16_t)(*((const uint8_t *)(p) + 1)) << 0)))
243 #define EXTRACT_BE_INT16(p) \
244 ((int16_t)(((uint16_t)(*((const uint8_t *)(p) + 0)) << 8) | \
245 ((uint16_t)(*((const uint8_t *)(p) + 1)) << 0)))
246 #define EXTRACT_BE_32BITS(p) \
247 ((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 0)) << 24) | \
248 ((uint32_t)(*((const uint8_t *)(p) + 1)) << 16) | \
249 ((uint32_t)(*((const uint8_t *)(p) + 2)) << 8) | \
250 ((uint32_t)(*((const uint8_t *)(p) + 3)) << 0)))
251 #define EXTRACT_BE_INT32(p) \
252 ((int32_t)(((uint32_t)(*((const uint8_t *)(p) + 0)) << 24) | \
253 ((uint32_t)(*((const uint8_t *)(p) + 1)) << 16) | \
254 ((uint32_t)(*((const uint8_t *)(p) + 2)) << 8) | \
255 ((uint32_t)(*((const uint8_t *)(p) + 3)) << 0)))
256 #define EXTRACT_BE_64BITS(p) \
257 ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 56) | \
258 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 48) | \
259 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 40) | \
260 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 32) | \
261 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 24) | \
262 ((uint64_t)(*((const uint8_t *)(p) + 5)) << 16) | \
263 ((uint64_t)(*((const uint8_t *)(p) + 6)) << 8) | \
264 ((uint64_t)(*((const uint8_t *)(p) + 7)) << 0)))
265 #define EXTRACT_BE_INT64(p) \
266 ((int64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 56) | \
267 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 48) | \
268 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 40) | \
269 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 32) | \
270 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 24) | \
271 ((uint64_t)(*((const uint8_t *)(p) + 5)) << 16) | \
272 ((uint64_t)(*((const uint8_t *)(p) + 6)) << 8) | \
273 ((uint64_t)(*((const uint8_t *)(p) + 7)) << 0)))
274 #endif /* unaligned access checks */
276 #define EXTRACT_BE_24BITS(p) \
277 ((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 0)) << 16) | \
278 ((uint32_t)(*((const uint8_t *)(p) + 1)) << 8) | \
279 ((uint32_t)(*((const uint8_t *)(p) + 2)) << 0)))
281 #define EXTRACT_BE_INT24(p) \
282 (((*((const uint8_t *)(p) + 0)) & 0x80) ? \
283 ((int32_t)(((uint32_t)(*((const uint8_t *)(p) + 0)) << 16) | \
284 ((uint32_t)(*((const uint8_t *)(p) + 1)) << 8) | \
285 ((uint32_t)(*((const uint8_t *)(p) + 2)) << 0))) : \
286 ((int32_t)(0xFF000000U | \
287 ((uint32_t)(*((const uint8_t *)(p) + 0)) << 16) | \
288 ((uint32_t)(*((const uint8_t *)(p) + 1)) << 8) | \
289 ((uint32_t)(*((const uint8_t *)(p) + 2)) << 0))))
291 #define EXTRACT_BE_40BITS(p) \
292 ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 32) | \
293 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 24) | \
294 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 16) | \
295 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 8) | \
296 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 0)))
298 #define EXTRACT_BE_INT40(p) \
299 (((*((const uint8_t *)(p) + 0)) & 0x80) ? \
300 ((int64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 32) | \
301 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 24) | \
302 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 16) | \
303 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 8) | \
304 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 0))) : \
305 ((int64_t)(INT64_T_CONSTANT(0xFFFFFF0000000000U) | \
306 ((uint64_t)(*((const uint8_t *)(p) + 0)) << 32) | \
307 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 24) | \
308 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 16) | \
309 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 8) | \
310 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 0))))
312 #define EXTRACT_BE_48BITS(p) \
313 ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 40) | \
314 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 32) | \
315 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 24) | \
316 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 16) | \
317 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 8) | \
318 ((uint64_t)(*((const uint8_t *)(p) + 5)) << 0)))
320 #define EXTRACT_BE_INT48(p) \
321 (((*((const uint8_t *)(p) + 0)) & 0x80) ? \
322 ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 40) | \
323 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 32) | \
324 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 24) | \
325 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 16) | \
326 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 8) | \
327 ((uint64_t)(*((const uint8_t *)(p) + 5)) << 0))) : \
328 ((int64_t)(INT64_T_CONSTANT(0xFFFFFFFF00000000U) | \
329 ((uint64_t)(*((const uint8_t *)(p) + 0)) << 40) | \
330 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 32) | \
331 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 24) | \
332 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 16) | \
333 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 8) | \
334 ((uint64_t)(*((const uint8_t *)(p) + 5)) << 0))))
336 #define EXTRACT_BE_56BITS(p) \
337 ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 48) | \
338 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 40) | \
339 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 32) | \
340 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 24) | \
341 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 16) | \
342 ((uint64_t)(*((const uint8_t *)(p) + 5)) << 8) | \
343 ((uint64_t)(*((const uint8_t *)(p) + 6)) << 0)))
345 #define EXTRACT_BE_INT56(p) \
346 (((*((const uint8_t *)(p) + 0)) & 0x80) ? \
347 ((int64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 48) | \
348 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 40) | \
349 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 32) | \
350 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 24) | \
351 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 16) | \
352 ((uint64_t)(*((const uint8_t *)(p) + 5)) << 8) | \
353 ((uint64_t)(*((const uint8_t *)(p) + 6)) << 0))) : \
354 ((int64_t)(INT64_T_CONSTANT(0xFFFFFFFFFF000000U) | \
355 ((uint64_t)(*((const uint8_t *)(p) + 0)) << 48) | \
356 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 40) | \
357 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 32) | \
358 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 24) | \
359 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 16) | \
360 ((uint64_t)(*((const uint8_t *)(p) + 5)) << 8) | \
361 ((uint64_t)(*((const uint8_t *)(p) + 6)) << 0))))
364 * Macros to extract possibly-unaligned little-endian integral values.
365 * XXX - do loads on little-endian machines that support unaligned loads?
367 #define EXTRACT_LE_16BITS(p) \
368 ((uint16_t)(((uint16_t)(*((const uint8_t *)(p) + 1)) << 8) | \
369 ((uint16_t)(*((const uint8_t *)(p) + 0)) << 0)))
370 #define EXTRACT_LE_32BITS(p) \
371 ((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 3)) << 24) | \
372 ((uint32_t)(*((const uint8_t *)(p) + 2)) << 16) | \
373 ((uint32_t)(*((const uint8_t *)(p) + 1)) << 8) | \
374 ((uint32_t)(*((const uint8_t *)(p) + 0)) << 0)))
375 #define EXTRACT_LE_24BITS(p) \
376 ((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 2)) << 16) | \
377 ((uint32_t)(*((const uint8_t *)(p) + 1)) << 8) | \
378 ((uint32_t)(*((const uint8_t *)(p) + 0)) << 0)))
379 #define EXTRACT_LE_64BITS(p) \
380 ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 7)) << 56) | \
381 ((uint64_t)(*((const uint8_t *)(p) + 6)) << 48) | \
382 ((uint64_t)(*((const uint8_t *)(p) + 5)) << 40) | \
383 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 32) | \
384 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 24) | \
385 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 16) | \
386 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 8) | \
387 ((uint64_t)(*((const uint8_t *)(p) + 0)) << 0)))
390 * Macros to check the presence of the values in question.
392 #define ND_TTEST_1(p) ND_TTEST2(*(p), 1)
393 #define ND_TCHECK_1(p) ND_TCHECK2(*(p), 1)
395 #define ND_TTEST_2(p) ND_TTEST2(*(p), 2)
396 #define ND_TCHECK_2(p) ND_TCHECK2(*(p), 2)
398 #define ND_TTEST_3(p) ND_TTEST2(*(p), 3)
399 #define ND_TCHECK_3(p) ND_TCHECK2(*(p), 3)
401 #define ND_TTEST_4(p) ND_TTEST2(*(p), 4)
402 #define ND_TCHECK_4(p) ND_TCHECK2(*(p), 4)
404 #define ND_TTEST_5(p) ND_TTEST2(*(p), 5)
405 #define ND_TCHECK_5(p) ND_TCHECK2(*(p), 5)
407 #define ND_TTEST_6(p) ND_TTEST2(*(p), 6)
408 #define ND_TCHECK_6(p) ND_TCHECK2(*(p), 6)
410 #define ND_TTEST_7(p) ND_TTEST2(*(p), 7)
411 #define ND_TCHECK_7(p) ND_TCHECK2(*(p), 7)
413 #define ND_TTEST_8(p) ND_TTEST2(*(p), 8)
414 #define ND_TCHECK_8(p) ND_TCHECK2(*(p), 8)
416 #define ND_TTEST_16(p) ND_TTEST2(*(p), 16)
417 #define ND_TCHECK_16(p) ND_TCHECK2(*(p), 16)