]>
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.
25 * For 8-bit values; needed to fetch a one-byte value. Byte order
26 * isn't relevant, and alignment isn't an issue.
28 #define EXTRACT_U_1(p) ((uint8_t)(*(p)))
29 #define EXTRACT_S_1(p) ((int8_t)(*(p)))
32 * Inline functions or macros to extract possibly-unaligned big-endian
35 #include "funcattrs.h"
36 #include "netdissect.h"
39 * If we have versions of GCC or Clang that support an __attribute__
40 * to say "if we're building with unsigned behavior sanitization,
41 * don't complain about undefined behavior in this function", we
42 * label these functions with that attribute - we *know* it's undefined
43 * in the C standard, but we *also* know it does what we want with
44 * the ISA we're targeting and the compiler we're using.
46 * For GCC 4.9.0 and later, we use __attribute__((no_sanitize_undefined));
47 * pre-5.0 GCC doesn't have __has_attribute, and I'm not sure whether
48 * GCC or Clang first had __attribute__((no_sanitize(XXX)).
50 * For Clang, we check for __attribute__((no_sanitize(XXX)) with
51 * __has_attribute, as there are versions of Clang that support
52 * __attribute__((no_sanitize("undefined")) but don't support
53 * __attribute__((no_sanitize_undefined)).
55 * We define this here, rather than in funcattrs.h, because we
56 * only want it used here, we don't want it to be broadly used.
57 * (Any printer will get this defined, but this should at least
58 * make it harder for people to find.)
60 #if defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 409)
61 #define UNALIGNED_OK __attribute__((no_sanitize_undefined))
62 #elif __has_attribute(no_sanitize)
63 #define UNALIGNED_OK __attribute__((no_sanitize("undefined")))
68 #if (defined(__i386__) || defined(_M_IX86) || defined(__X86__) || defined(__x86_64__) || defined(_M_X64)) || \
69 (defined(__m68k__) && (!defined(__mc68000__) && !defined(__mc68010__))) || \
70 (defined(__ppc__) || defined(__ppc64__) || defined(_M_PPC) || defined(_ARCH_PPC) || defined(_ARCH_PPC64)) || \
71 (defined(__s390__) || defined(__s390x__) || defined(__zarch__))
73 * The processor natively handles unaligned loads, so we can just
74 * cast the pointer and fetch through it.
76 * XXX - are those all the x86 tests we need?
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 UNALIGNED_OK
static inline uint16_t
85 EXTRACT_BE_U_2(const void *p
)
87 return ((uint16_t)ntohs(*(const uint16_t *)(p
)));
90 UNALIGNED_OK
static inline int16_t
91 EXTRACT_BE_S_2(const void *p
)
93 return ((int16_t)ntohs(*(const int16_t *)(p
)));
96 UNALIGNED_OK
static inline uint32_t
97 EXTRACT_BE_U_4(const void *p
)
99 return ((uint32_t)ntohl(*(const uint32_t *)(p
)));
102 UNALIGNED_OK
static inline int32_t
103 EXTRACT_BE_S_4(const void *p
)
105 return ((int32_t)ntohl(*(const int32_t *)(p
)));
108 UNALIGNED_OK
static inline uint64_t
109 EXTRACT_BE_U_8(const void *p
)
111 return ((uint64_t)(((uint64_t)ntohl(*((const uint32_t *)(p
) + 0))) << 32 |
112 ((uint64_t)ntohl(*((const uint32_t *)(p
) + 1))) << 0));
116 UNALIGNED_OK
static inline int64_t
117 EXTRACT_BE_S_8(const void *p
)
119 return ((int64_t)(((int64_t)ntohl(*((const uint32_t *)(p
) + 0))) << 32 |
120 ((uint64_t)ntohl(*((const uint32_t *)(p
) + 1))) << 0));
125 * Extract an IPv4 address, which is in network byte order, and not
126 * necessarily aligned, and provide the result in host byte order.
128 UNALIGNED_OK
static inline uint32_t
129 EXTRACT_IPV4_TO_HOST_ORDER(const void *p
)
131 return ((uint32_t)ntohl(*(const uint32_t *)(p
)));
133 #elif ND_IS_AT_LEAST_GNUC_VERSION(2,0) && \
134 (defined(__alpha) || defined(__alpha__) || \
135 defined(__mips) || defined(__mips__))
137 * This is MIPS or Alpha, which don't natively handle unaligned loads,
138 * but which have instructions that can help when doing unaligned
139 * loads, and this is GCC 2.0 or later or a compiler that claims to
140 * be GCC 2.0 or later, which we assume that mean we have
141 * __attribute__((packed)), which we can use to convince the compiler
142 * to generate those instructions.
144 * Declare packed structures containing a uint16_t and a uint32_t,
145 * cast the pointer to point to one of those, and fetch through it;
146 * the GCC manual doesn't appear to explicitly say that
147 * __attribute__((packed)) causes the compiler to generate unaligned-safe
148 * code, but it apppears to do so.
150 * We do this in case the compiler can generate code using those
151 * instructions to do an unaligned load and pass stuff to "ntohs()" or
152 * "ntohl()", which might be better than than the code to fetch the
153 * bytes one at a time and assemble them. (That might not be the
154 * case on a little-endian platform, such as DEC's MIPS machines and
155 * Alpha machines, where "ntohs()" and "ntohl()" might not be done
158 * We do this only for specific architectures because, for example,
159 * at least some versions of GCC, when compiling for 64-bit SPARC,
160 * generate code that assumes alignment if we do this.
162 * XXX - add other architectures and compilers as possible and
165 * HP's C compiler, indicated by __HP_cc being defined, supports
166 * "#pragma unaligned N" in version A.05.50 and later, where "N"
167 * specifies a number of bytes at which the typedef on the next
168 * line is aligned, e.g.
171 * typedef uint16_t unaligned_uint16_t;
173 * to define unaligned_uint16_t as a 16-bit unaligned data type.
174 * This could be presumably used, in sufficiently recent versions of
175 * the compiler, with macros similar to those below. This would be
176 * useful only if that compiler could generate better code for PA-RISC
177 * or Itanium than would be generated by a bunch of shifts-and-ORs.
179 * DEC C, indicated by __DECC being defined, has, at least on Alpha,
180 * an __unaligned qualifier that can be applied to pointers to get the
181 * compiler to generate code that does unaligned loads and stores when
182 * dereferencing the pointer in question.
184 * XXX - what if the native C compiler doesn't support
185 * __attribute__((packed))? How can we get it to generate unaligned
186 * accesses for *specific* items?
190 } __attribute__((packed
)) unaligned_uint16_t
;
194 } __attribute__((packed
)) unaligned_int16_t
;
198 } __attribute__((packed
)) unaligned_uint32_t
;
202 } __attribute__((packed
)) unaligned_int32_t
;
204 UNALIGNED_OK
static inline uint16_t
205 EXTRACT_BE_U_2(const void *p
)
207 return ((uint16_t)ntohs(((const unaligned_uint16_t
*)(p
))->val
));
210 UNALIGNED_OK
static inline int16_t
211 EXTRACT_BE_S_2(const void *p
)
213 return ((int16_t)ntohs(((const unaligned_int16_t
*)(p
))->val
));
216 UNALIGNED_OK
static inline uint32_t
217 EXTRACT_BE_U_4(const void *p
)
219 return ((uint32_t)ntohl(((const unaligned_uint32_t
*)(p
))->val
));
222 UNALIGNED_OK
static inline int32_t
223 EXTRACT_BE_S_4(const void *p
)
225 return ((int32_t)ntohl(((const unaligned_int32_t
*)(p
))->val
));
228 UNALIGNED_OK
static inline uint64_t
229 EXTRACT_BE_U_8(const void *p
)
231 return ((uint64_t)(((uint64_t)ntohl(((const unaligned_uint32_t
*)(p
) + 0)->val
)) << 32 |
232 ((uint64_t)ntohl(((const unaligned_uint32_t
*)(p
) + 1)->val
)) << 0));
235 UNALIGNED_OK
static inline int64_t
236 EXTRACT_BE_S_8(const void *p
)
238 return ((int64_t)(((uint64_t)ntohl(((const unaligned_uint32_t
*)(p
) + 0)->val
)) << 32 |
239 ((uint64_t)ntohl(((const unaligned_uint32_t
*)(p
) + 1)->val
)) << 0));
243 * Extract an IPv4 address, which is in network byte order, and not
244 * necessarily aligned, and provide the result in host byte order.
246 UNALIGNED_OK
static inline uint32_t
247 EXTRACT_IPV4_TO_HOST_ORDER(const void *p
)
249 return ((uint32_t)ntohl(((const unaligned_uint32_t
*)(p
))->val
));
253 * This architecture doesn't natively support unaligned loads, and either
254 * this isn't a GCC-compatible compiler, we don't have __attribute__,
255 * or we do but we don't know of any better way with this instruction
256 * set to do unaligned loads, so do unaligned loads of big-endian
257 * quantities the hard way - fetch the bytes one at a time and
260 * XXX - ARM is a special case. ARMv1 through ARMv5 didn't suppory
261 * unaligned loads; ARMv6 and later support it *but* have a bit in
262 * the system control register that the OS can set and that causes
263 * unaligned loads to fault rather than succeeding.
265 * At least some OSes may set that flag, so we do *not* treat ARM
266 * as supporting unaligned loads. If your OS supports them on ARM,
267 * and you want to use them, please update the tests in the #if above
268 * to check for ARM *and* for your OS.
270 #define EXTRACT_BE_U_2(p) \
271 ((uint16_t)(((uint16_t)(*((const uint8_t *)(p) + 0)) << 8) | \
272 ((uint16_t)(*((const uint8_t *)(p) + 1)) << 0)))
273 #define EXTRACT_BE_S_2(p) \
274 ((int16_t)(((uint16_t)(*((const uint8_t *)(p) + 0)) << 8) | \
275 ((uint16_t)(*((const uint8_t *)(p) + 1)) << 0)))
276 #define EXTRACT_BE_U_4(p) \
277 ((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 0)) << 24) | \
278 ((uint32_t)(*((const uint8_t *)(p) + 1)) << 16) | \
279 ((uint32_t)(*((const uint8_t *)(p) + 2)) << 8) | \
280 ((uint32_t)(*((const uint8_t *)(p) + 3)) << 0)))
281 #define EXTRACT_BE_S_4(p) \
282 ((int32_t)(((uint32_t)(*((const uint8_t *)(p) + 0)) << 24) | \
283 ((uint32_t)(*((const uint8_t *)(p) + 1)) << 16) | \
284 ((uint32_t)(*((const uint8_t *)(p) + 2)) << 8) | \
285 ((uint32_t)(*((const uint8_t *)(p) + 3)) << 0)))
286 #define EXTRACT_BE_U_8(p) \
287 ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 56) | \
288 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 48) | \
289 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 40) | \
290 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 32) | \
291 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 24) | \
292 ((uint64_t)(*((const uint8_t *)(p) + 5)) << 16) | \
293 ((uint64_t)(*((const uint8_t *)(p) + 6)) << 8) | \
294 ((uint64_t)(*((const uint8_t *)(p) + 7)) << 0)))
295 #define EXTRACT_BE_S_8(p) \
296 ((int64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 56) | \
297 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 48) | \
298 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 40) | \
299 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 32) | \
300 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 24) | \
301 ((uint64_t)(*((const uint8_t *)(p) + 5)) << 16) | \
302 ((uint64_t)(*((const uint8_t *)(p) + 6)) << 8) | \
303 ((uint64_t)(*((const uint8_t *)(p) + 7)) << 0)))
306 * Extract an IPv4 address, which is in network byte order, and not
307 * necessarily aligned, and provide the result in host byte order.
309 #define EXTRACT_IPV4_TO_HOST_ORDER(p) \
310 ((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 0)) << 24) | \
311 ((uint32_t)(*((const uint8_t *)(p) + 1)) << 16) | \
312 ((uint32_t)(*((const uint8_t *)(p) + 2)) << 8) | \
313 ((uint32_t)(*((const uint8_t *)(p) + 3)) << 0)))
314 #endif /* unaligned access checks */
317 * Extract numerical values in *host* byte order. (Some metadata
318 * headers are in the byte order of the host that wrote the file,
319 * and libpcap translate them to the byte order of the host
320 * reading the file. This means that if a program on that host
321 * reads with libpcap and writes to a new file, the new file will
322 * be written in the byte order of the host writing the file. Thus,
323 * the magic number in pcap files and byte-order magic in pcapng
324 * files can be used to determine the byte order in those metadata
327 * XXX - on platforms that can do unaligned accesses, just cast and
328 * dereference the pointer.
330 static inline uint16_t
331 EXTRACT_HE_U_2(const void *p
)
335 UNALIGNED_MEMCPY(&val
, p
, sizeof(uint16_t));
339 static inline int16_t
340 EXTRACT_HE_S_2(const void *p
)
344 UNALIGNED_MEMCPY(&val
, p
, sizeof(int16_t));
348 static inline uint32_t
349 EXTRACT_HE_U_4(const void *p
)
353 UNALIGNED_MEMCPY(&val
, p
, sizeof(uint32_t));
357 static inline int32_t
358 EXTRACT_HE_S_4(const void *p
)
362 UNALIGNED_MEMCPY(&val
, p
, sizeof(int32_t));
367 * Extract an IPv4 address, which is in network byte order, and which
368 * is not necessarily aligned on a 4-byte boundary, and provide the
369 * result in network byte order.
371 * This works the same way regardless of the host's byte order.
373 static inline uint32_t
374 EXTRACT_IPV4_TO_NETWORK_ORDER(const void *p
)
378 UNALIGNED_MEMCPY(&addr
, p
, sizeof(uint32_t));
383 * Non-power-of-2 sizes.
385 #define EXTRACT_BE_U_3(p) \
386 ((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 0)) << 16) | \
387 ((uint32_t)(*((const uint8_t *)(p) + 1)) << 8) | \
388 ((uint32_t)(*((const uint8_t *)(p) + 2)) << 0)))
390 #define EXTRACT_BE_S_3(p) \
391 (((*((const uint8_t *)(p) + 0)) & 0x80) ? \
392 ((int32_t)(((uint32_t)(*((const uint8_t *)(p) + 0)) << 16) | \
393 ((uint32_t)(*((const uint8_t *)(p) + 1)) << 8) | \
394 ((uint32_t)(*((const uint8_t *)(p) + 2)) << 0))) : \
395 ((int32_t)(0xFF000000U | \
396 ((uint32_t)(*((const uint8_t *)(p) + 0)) << 16) | \
397 ((uint32_t)(*((const uint8_t *)(p) + 1)) << 8) | \
398 ((uint32_t)(*((const uint8_t *)(p) + 2)) << 0))))
400 #define EXTRACT_BE_U_5(p) \
401 ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 32) | \
402 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 24) | \
403 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 16) | \
404 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 8) | \
405 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 0)))
407 #define EXTRACT_BE_S_5(p) \
408 (((*((const uint8_t *)(p) + 0)) & 0x80) ? \
409 ((int64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 32) | \
410 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 24) | \
411 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 16) | \
412 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 8) | \
413 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 0))) : \
414 ((int64_t)(INT64_T_CONSTANT(0xFFFFFF0000000000U) | \
415 ((uint64_t)(*((const uint8_t *)(p) + 0)) << 32) | \
416 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 24) | \
417 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 16) | \
418 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 8) | \
419 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 0))))
421 #define EXTRACT_BE_U_6(p) \
422 ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 40) | \
423 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 32) | \
424 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 24) | \
425 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 16) | \
426 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 8) | \
427 ((uint64_t)(*((const uint8_t *)(p) + 5)) << 0)))
429 #define EXTRACT_BE_S_6(p) \
430 (((*((const uint8_t *)(p) + 0)) & 0x80) ? \
431 ((int64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 40) | \
432 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 32) | \
433 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 24) | \
434 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 16) | \
435 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 8) | \
436 ((uint64_t)(*((const uint8_t *)(p) + 5)) << 0))) : \
437 ((int64_t)(INT64_T_CONSTANT(0xFFFFFFFF00000000U) | \
438 ((uint64_t)(*((const uint8_t *)(p) + 0)) << 40) | \
439 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 32) | \
440 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 24) | \
441 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 16) | \
442 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 8) | \
443 ((uint64_t)(*((const uint8_t *)(p) + 5)) << 0))))
445 #define EXTRACT_BE_U_7(p) \
446 ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 48) | \
447 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 40) | \
448 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 32) | \
449 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 24) | \
450 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 16) | \
451 ((uint64_t)(*((const uint8_t *)(p) + 5)) << 8) | \
452 ((uint64_t)(*((const uint8_t *)(p) + 6)) << 0)))
454 #define EXTRACT_BE_S_7(p) \
455 (((*((const uint8_t *)(p) + 0)) & 0x80) ? \
456 ((int64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 48) | \
457 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 40) | \
458 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 32) | \
459 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 24) | \
460 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 16) | \
461 ((uint64_t)(*((const uint8_t *)(p) + 5)) << 8) | \
462 ((uint64_t)(*((const uint8_t *)(p) + 6)) << 0))) : \
463 ((int64_t)(INT64_T_CONSTANT(0xFFFFFFFFFF000000U) | \
464 ((uint64_t)(*((const uint8_t *)(p) + 0)) << 48) | \
465 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 40) | \
466 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 32) | \
467 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 24) | \
468 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 16) | \
469 ((uint64_t)(*((const uint8_t *)(p) + 5)) << 8) | \
470 ((uint64_t)(*((const uint8_t *)(p) + 6)) << 0))))
473 * Macros to extract possibly-unaligned little-endian integral values.
474 * XXX - do loads on little-endian machines that support unaligned loads?
476 #define EXTRACT_LE_U_2(p) \
477 ((uint16_t)(((uint16_t)(*((const uint8_t *)(p) + 1)) << 8) | \
478 ((uint16_t)(*((const uint8_t *)(p) + 0)) << 0)))
479 #define EXTRACT_LE_S_2(p) \
480 ((int16_t)(((uint16_t)(*((const uint8_t *)(p) + 1)) << 8) | \
481 ((uint16_t)(*((const uint8_t *)(p) + 0)) << 0)))
482 #define EXTRACT_LE_U_4(p) \
483 ((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 3)) << 24) | \
484 ((uint32_t)(*((const uint8_t *)(p) + 2)) << 16) | \
485 ((uint32_t)(*((const uint8_t *)(p) + 1)) << 8) | \
486 ((uint32_t)(*((const uint8_t *)(p) + 0)) << 0)))
487 #define EXTRACT_LE_S_4(p) \
488 ((int32_t)(((uint32_t)(*((const uint8_t *)(p) + 3)) << 24) | \
489 ((uint32_t)(*((const uint8_t *)(p) + 2)) << 16) | \
490 ((uint32_t)(*((const uint8_t *)(p) + 1)) << 8) | \
491 ((uint32_t)(*((const uint8_t *)(p) + 0)) << 0)))
492 #define EXTRACT_LE_U_8(p) \
493 ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 7)) << 56) | \
494 ((uint64_t)(*((const uint8_t *)(p) + 6)) << 48) | \
495 ((uint64_t)(*((const uint8_t *)(p) + 5)) << 40) | \
496 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 32) | \
497 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 24) | \
498 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 16) | \
499 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 8) | \
500 ((uint64_t)(*((const uint8_t *)(p) + 0)) << 0)))
501 #define EXTRACT_LE_S_8(p) \
502 ((int64_t)(((uint64_t)(*((const uint8_t *)(p) + 7)) << 56) | \
503 ((uint64_t)(*((const uint8_t *)(p) + 6)) << 48) | \
504 ((uint64_t)(*((const uint8_t *)(p) + 5)) << 40) | \
505 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 32) | \
506 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 24) | \
507 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 16) | \
508 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 8) | \
509 ((uint64_t)(*((const uint8_t *)(p) + 0)) << 0)))
512 * Non-power-of-2 sizes.
515 #define EXTRACT_LE_U_3(p) \
516 ((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 2)) << 16) | \
517 ((uint32_t)(*((const uint8_t *)(p) + 1)) << 8) | \
518 ((uint32_t)(*((const uint8_t *)(p) + 0)) << 0)))
519 #define EXTRACT_LE_S_3(p) \
520 ((int32_t)(((uint32_t)(*((const uint8_t *)(p) + 2)) << 16) | \
521 ((uint32_t)(*((const uint8_t *)(p) + 1)) << 8) | \
522 ((uint32_t)(*((const uint8_t *)(p) + 0)) << 0)))
523 #define EXTRACT_LE_U_5(p) \
524 ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 4)) << 32) | \
525 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 24) | \
526 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 16) | \
527 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 8) | \
528 ((uint64_t)(*((const uint8_t *)(p) + 0)) << 0)))
529 #define EXTRACT_LE_U_6(p) \
530 ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 5)) << 40) | \
531 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 32) | \
532 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 24) | \
533 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 16) | \
534 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 8) | \
535 ((uint64_t)(*((const uint8_t *)(p) + 0)) << 0)))
536 #define EXTRACT_LE_U_7(p) \
537 ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 6)) << 48) | \
538 ((uint64_t)(*((const uint8_t *)(p) + 5)) << 40) | \
539 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 32) | \
540 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 24) | \
541 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 16) | \
542 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 8) | \
543 ((uint64_t)(*((const uint8_t *)(p) + 0)) << 0)))
546 * Macros to check the presence of the values in question.
548 #define ND_TTEST_1(p) ND_TTEST_LEN((p), 1)
549 #define ND_TCHECK_1(p) ND_TCHECK_LEN((p), 1)
551 #define ND_TTEST_2(p) ND_TTEST_LEN((p), 2)
552 #define ND_TCHECK_2(p) ND_TCHECK_LEN((p), 2)
554 #define ND_TTEST_3(p) ND_TTEST_LEN((p), 3)
555 #define ND_TCHECK_3(p) ND_TCHECK_LEN((p), 3)
557 #define ND_TTEST_4(p) ND_TTEST_LEN((p), 4)
558 #define ND_TCHECK_4(p) ND_TCHECK_LEN((p), 4)
560 #define ND_TTEST_5(p) ND_TTEST_LEN((p), 5)
561 #define ND_TCHECK_5(p) ND_TCHECK_LEN((p), 5)
563 #define ND_TTEST_6(p) ND_TTEST_LEN((p), 6)
564 #define ND_TCHECK_6(p) ND_TCHECK_LEN((p), 6)
566 #define ND_TTEST_7(p) ND_TTEST_LEN((p), 7)
567 #define ND_TCHECK_7(p) ND_TCHECK_LEN((p), 7)
569 #define ND_TTEST_8(p) ND_TTEST_LEN((p), 8)
570 #define ND_TCHECK_8(p) ND_TCHECK_LEN((p), 8)
572 #define ND_TTEST_16(p) ND_TTEST_LEN((p), 16)
573 #define ND_TCHECK_16(p) ND_TCHECK_LEN((p), 16)
575 /* get_u_1 and get_s_1 */
577 static inline uint8_t
578 get_u_1(netdissect_options
*ndo
, const u_char
*p
)
581 longjmp(ndo
->ndo_truncated
, 1);
582 return EXTRACT_U_1(p
);
586 get_s_1(netdissect_options
*ndo
, const u_char
*p
)
589 longjmp(ndo
->ndo_truncated
, 1);
590 return EXTRACT_S_1(p
);
595 static inline uint16_t
596 get_be_u_2(netdissect_options
*ndo
, const u_char
*p
)
599 longjmp(ndo
->ndo_truncated
, 1);
600 return EXTRACT_BE_U_2(p
);
603 static inline uint32_t
604 get_be_u_3(netdissect_options
*ndo
, const u_char
*p
)
607 longjmp(ndo
->ndo_truncated
, 1);
608 return EXTRACT_BE_U_3(p
);
611 static inline uint32_t
612 get_be_u_4(netdissect_options
*ndo
, const u_char
*p
)
615 longjmp(ndo
->ndo_truncated
, 1);
616 return EXTRACT_BE_U_4(p
);
619 static inline uint64_t
620 get_be_u_5(netdissect_options
*ndo
, const u_char
*p
)
623 longjmp(ndo
->ndo_truncated
, 1);
624 return EXTRACT_BE_U_5(p
);
627 static inline uint64_t
628 get_be_u_6(netdissect_options
*ndo
, const u_char
*p
)
631 longjmp(ndo
->ndo_truncated
, 1);
632 return EXTRACT_BE_U_6(p
);
635 static inline uint64_t
636 get_be_u_7(netdissect_options
*ndo
, const u_char
*p
)
639 longjmp(ndo
->ndo_truncated
, 1);
640 return EXTRACT_BE_U_7(p
);
643 static inline uint64_t
644 get_be_u_8(netdissect_options
*ndo
, const u_char
*p
)
647 longjmp(ndo
->ndo_truncated
, 1);
648 return EXTRACT_BE_U_8(p
);
653 static inline int16_t
654 get_be_s_2(netdissect_options
*ndo
, const u_char
*p
)
657 longjmp(ndo
->ndo_truncated
, 1);
658 return EXTRACT_BE_S_2(p
);
661 static inline int32_t
662 get_be_s_3(netdissect_options
*ndo
, const u_char
*p
)
665 longjmp(ndo
->ndo_truncated
, 1);
666 return EXTRACT_BE_S_3(p
);
669 static inline int32_t
670 get_be_s_4(netdissect_options
*ndo
, const u_char
*p
)
673 longjmp(ndo
->ndo_truncated
, 1);
674 return EXTRACT_BE_S_4(p
);
677 static inline int64_t
678 get_be_s_5(netdissect_options
*ndo
, const u_char
*p
)
681 longjmp(ndo
->ndo_truncated
, 1);
682 return EXTRACT_BE_S_5(p
);
685 static inline int64_t
686 get_be_s_6(netdissect_options
*ndo
, const u_char
*p
)
689 longjmp(ndo
->ndo_truncated
, 1);
690 return EXTRACT_BE_S_6(p
);
693 static inline int64_t
694 get_be_s_7(netdissect_options
*ndo
, const u_char
*p
)
697 longjmp(ndo
->ndo_truncated
, 1);
698 return EXTRACT_BE_S_7(p
);
701 static inline int64_t
702 get_be_s_8(netdissect_options
*ndo
, const u_char
*p
)
705 longjmp(ndo
->ndo_truncated
, 1);
706 return EXTRACT_BE_S_8(p
);
711 static inline uint16_t
712 get_he_u_2(netdissect_options
*ndo
, const u_char
*p
)
715 longjmp(ndo
->ndo_truncated
, 1);
716 return EXTRACT_HE_U_2(p
);
719 static inline uint32_t
720 get_he_u_4(netdissect_options
*ndo
, const u_char
*p
)
723 longjmp(ndo
->ndo_truncated
, 1);
724 return EXTRACT_HE_U_4(p
);
729 static inline int16_t
730 get_he_s_2(netdissect_options
*ndo
, const u_char
*p
)
733 longjmp(ndo
->ndo_truncated
, 1);
734 return EXTRACT_HE_S_2(p
);
737 static inline int32_t
738 get_he_s_4(netdissect_options
*ndo
, const u_char
*p
)
741 longjmp(ndo
->ndo_truncated
, 1);
742 return EXTRACT_HE_S_4(p
);
747 static inline uint16_t
748 get_le_u_2(netdissect_options
*ndo
, const u_char
*p
)
751 longjmp(ndo
->ndo_truncated
, 1);
752 return EXTRACT_LE_U_2(p
);
755 static inline uint32_t
756 get_le_u_3(netdissect_options
*ndo
, const u_char
*p
)
759 longjmp(ndo
->ndo_truncated
, 1);
760 return EXTRACT_LE_U_3(p
);
763 static inline uint32_t
764 get_le_u_4(netdissect_options
*ndo
, const u_char
*p
)
767 longjmp(ndo
->ndo_truncated
, 1);
768 return EXTRACT_LE_U_4(p
);
771 static inline uint64_t
772 get_le_u_5(netdissect_options
*ndo
, const u_char
*p
)
775 longjmp(ndo
->ndo_truncated
, 1);
776 return EXTRACT_LE_U_5(p
);
779 static inline uint64_t
780 get_le_u_6(netdissect_options
*ndo
, const u_char
*p
)
783 longjmp(ndo
->ndo_truncated
, 1);
784 return EXTRACT_LE_U_6(p
);
787 static inline uint64_t
788 get_le_u_7(netdissect_options
*ndo
, const u_char
*p
)
791 longjmp(ndo
->ndo_truncated
, 1);
792 return EXTRACT_LE_U_7(p
);
795 static inline uint64_t
796 get_le_u_8(netdissect_options
*ndo
, const u_char
*p
)
799 longjmp(ndo
->ndo_truncated
, 1);
800 return EXTRACT_LE_U_8(p
);
805 static inline int16_t
806 get_le_s_2(netdissect_options
*ndo
, const u_char
*p
)
809 longjmp(ndo
->ndo_truncated
, 1);
810 return EXTRACT_LE_S_2(p
);
813 static inline int32_t
814 get_le_s_3(netdissect_options
*ndo
, const u_char
*p
)
817 longjmp(ndo
->ndo_truncated
, 1);
818 return EXTRACT_LE_S_3(p
);
821 static inline int32_t
822 get_le_s_4(netdissect_options
*ndo
, const u_char
*p
)
825 longjmp(ndo
->ndo_truncated
, 1);
826 return EXTRACT_LE_S_4(p
);
829 static inline int64_t
830 get_le_s_8(netdissect_options
*ndo
, const u_char
*p
)
833 longjmp(ndo
->ndo_truncated
, 1);
834 return EXTRACT_LE_S_8(p
);
837 /* get_ipv4_to_{host|network]_order */
839 static inline uint32_t
840 get_ipv4_to_host_order(netdissect_options
*ndo
, const u_char
*p
)
843 longjmp(ndo
->ndo_truncated
, 1);
844 return EXTRACT_IPV4_TO_HOST_ORDER(p
);
847 static inline uint32_t
848 get_ipv4_to_network_order(netdissect_options
*ndo
, const u_char
*p
)
851 longjmp(ndo
->ndo_truncated
, 1);
852 return EXTRACT_IPV4_TO_NETWORK_ORDER(p
);
855 #define GET_U_1(p) get_u_1(ndo, (const u_char *)(p))
856 #define GET_S_1(p) get_s_1(ndo, (const u_char *)(p))
858 #define GET_BE_U_2(p) get_be_u_2(ndo, (const u_char *)(p))
859 #define GET_BE_U_3(p) get_be_u_3(ndo, (const u_char *)(p))
860 #define GET_BE_U_4(p) get_be_u_4(ndo, (const u_char *)(p))
861 #define GET_BE_U_5(p) get_be_u_5(ndo, (const u_char *)(p))
862 #define GET_BE_U_6(p) get_be_u_6(ndo, (const u_char *)(p))
863 #define GET_BE_U_7(p) get_be_u_7(ndo, (const u_char *)(p))
864 #define GET_BE_U_8(p) get_be_u_8(ndo, (const u_char *)(p))
866 #define GET_BE_S_2(p) get_be_s_2(ndo, (const u_char *)(p))
867 #define GET_BE_S_3(p) get_be_s_3(ndo, (const u_char *)(p))
868 #define GET_BE_S_4(p) get_be_s_4(ndo, (const u_char *)(p))
869 #define GET_BE_S_5(p) get_be_s_5(ndo, (const u_char *)(p))
870 #define GET_BE_S_6(p) get_be_s_6(ndo, (const u_char *)(p))
871 #define GET_BE_S_7(p) get_be_s_7(ndo, (const u_char *)(p))
872 #define GET_BE_S_8(p) get_be_s_8(ndo, (const u_char *)(p))
874 #define GET_HE_U_2(p) get_he_u_2(ndo, (const u_char *)(p))
875 #define GET_HE_U_4(p) get_he_u_4(ndo, (const u_char *)(p))
877 #define GET_HE_S_2(p) get_he_s_2(ndo, (const u_char *)(p))
878 #define GET_HE_S_4(p) get_he_s_4(ndo, (const u_char *)(p))
880 #define GET_LE_U_2(p) get_le_u_2(ndo, (const u_char *)(p))
881 #define GET_LE_U_3(p) get_le_u_3(ndo, (const u_char *)(p))
882 #define GET_LE_U_4(p) get_le_u_4(ndo, (const u_char *)(p))
883 #define GET_LE_U_5(p) get_le_u_5(ndo, (const u_char *)(p))
884 #define GET_LE_U_6(p) get_le_u_6(ndo, (const u_char *)(p))
885 #define GET_LE_U_7(p) get_le_u_7(ndo, (const u_char *)(p))
886 #define GET_LE_U_8(p) get_le_u_8(ndo, (const u_char *)(p))
888 #define GET_LE_S_2(p) get_le_s_2(ndo, (const u_char *)(p))
889 #define GET_LE_S_3(p) get_le_s_3(ndo, (const u_char *)(p))
890 #define GET_LE_S_4(p) get_le_s_4(ndo, (const u_char *)(p))
891 #define GET_LE_S_8(p) get_le_s_8(ndo, (const u_char *)(p))
893 #define GET_IPV4_TO_HOST_ORDER(p) get_ipv4_to_host_order(ndo, (const u_char *)(p))
894 #define GET_IPV4_TO_NETWORK_ORDER(p) get_ipv4_to_network_order(ndo, (const u_char *)(p))