]> The Tcpdump Group git mirrors - tcpdump/blob - extract.h
Ethernet: Rename a printer
[tcpdump] / extract.h
1 /*
2 * Copyright (c) 1992, 1993, 1994, 1995, 1996
3 * The Regents of the University of California. All rights reserved.
4 *
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
16 * written permission.
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.
20 */
21
22 #include <string.h>
23
24 /*
25 * For 8-bit values; needed to fetch a one-byte value. Byte order
26 * isn't relevant, and alignment isn't an issue.
27 */
28 #define EXTRACT_U_1(p) ((uint8_t)(*(p)))
29 #define EXTRACT_S_1(p) ((int8_t)(*(p)))
30
31 /*
32 * Inline functions or macros to extract possibly-unaligned big-endian
33 * integral values.
34 */
35 #include "funcattrs.h"
36 #include "netdissect.h"
37
38 /*
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.
45 *
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)).
49 *
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)).
54 *
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.)
59 */
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")))
64 #else
65 #define UNALIGNED_OK
66 #endif
67
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__))
72 /*
73 * The processor natively handles unaligned loads, so we can just
74 * cast the pointer and fetch through it.
75 *
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?
83 */
84 UNALIGNED_OK static inline uint16_t
85 EXTRACT_BE_U_2(const void *p)
86 {
87 return ((uint16_t)ntohs(*(const uint16_t *)(p)));
88 }
89
90 UNALIGNED_OK static inline int16_t
91 EXTRACT_BE_S_2(const void *p)
92 {
93 return ((int16_t)ntohs(*(const int16_t *)(p)));
94 }
95
96 UNALIGNED_OK static inline uint32_t
97 EXTRACT_BE_U_4(const void *p)
98 {
99 return ((uint32_t)ntohl(*(const uint32_t *)(p)));
100 }
101
102 UNALIGNED_OK static inline int32_t
103 EXTRACT_BE_S_4(const void *p)
104 {
105 return ((int32_t)ntohl(*(const int32_t *)(p)));
106 }
107
108 UNALIGNED_OK static inline uint64_t
109 EXTRACT_BE_U_8(const void *p)
110 {
111 return ((uint64_t)(((uint64_t)ntohl(*((const uint32_t *)(p) + 0))) << 32 |
112 ((uint64_t)ntohl(*((const uint32_t *)(p) + 1))) << 0));
113
114 }
115
116 UNALIGNED_OK static inline int64_t
117 EXTRACT_BE_S_8(const void *p)
118 {
119 return ((int64_t)(((int64_t)ntohl(*((const uint32_t *)(p) + 0))) << 32 |
120 ((uint64_t)ntohl(*((const uint32_t *)(p) + 1))) << 0));
121
122 }
123
124 /*
125 * Extract an IPv4 address, which is in network byte order, and not
126 * necessarily aligned, and provide the result in host byte order.
127 */
128 UNALIGNED_OK static inline uint32_t
129 EXTRACT_IPV4_TO_HOST_ORDER(const void *p)
130 {
131 return ((uint32_t)ntohl(*(const uint32_t *)(p)));
132 }
133 #elif ND_IS_AT_LEAST_GNUC_VERSION(2,0) && \
134 (defined(__alpha) || defined(__alpha__) || \
135 defined(__mips) || defined(__mips__))
136 /*
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.
143 *
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.
149 *
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
156 * inline.)
157 *
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.
161 *
162 * XXX - add other architectures and compilers as possible and
163 * appropriate.
164 *
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.
169 *
170 * #pragma unalign 1
171 * typedef uint16_t unaligned_uint16_t;
172 *
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.
178 *
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.
183 *
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?
187 */
188 typedef struct {
189 uint16_t val;
190 } __attribute__((packed)) unaligned_uint16_t;
191
192 typedef struct {
193 int16_t val;
194 } __attribute__((packed)) unaligned_int16_t;
195
196 typedef struct {
197 uint32_t val;
198 } __attribute__((packed)) unaligned_uint32_t;
199
200 typedef struct {
201 int32_t val;
202 } __attribute__((packed)) unaligned_int32_t;
203
204 UNALIGNED_OK static inline uint16_t
205 EXTRACT_BE_U_2(const void *p)
206 {
207 return ((uint16_t)ntohs(((const unaligned_uint16_t *)(p))->val));
208 }
209
210 UNALIGNED_OK static inline int16_t
211 EXTRACT_BE_S_2(const void *p)
212 {
213 return ((int16_t)ntohs(((const unaligned_int16_t *)(p))->val));
214 }
215
216 UNALIGNED_OK static inline uint32_t
217 EXTRACT_BE_U_4(const void *p)
218 {
219 return ((uint32_t)ntohl(((const unaligned_uint32_t *)(p))->val));
220 }
221
222 UNALIGNED_OK static inline int32_t
223 EXTRACT_BE_S_4(const void *p)
224 {
225 return ((int32_t)ntohl(((const unaligned_int32_t *)(p))->val));
226 }
227
228 UNALIGNED_OK static inline uint64_t
229 EXTRACT_BE_U_8(const void *p)
230 {
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));
233 }
234
235 UNALIGNED_OK static inline int64_t
236 EXTRACT_BE_S_8(const void *p)
237 {
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));
240 }
241
242 /*
243 * Extract an IPv4 address, which is in network byte order, and not
244 * necessarily aligned, and provide the result in host byte order.
245 */
246 UNALIGNED_OK static inline uint32_t
247 EXTRACT_IPV4_TO_HOST_ORDER(const void *p)
248 {
249 return ((uint32_t)ntohl(((const unaligned_uint32_t *)(p))->val));
250 }
251 #else
252 /*
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
258 * assemble them.
259 *
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.
264 *
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.
269 */
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)))
304
305 /*
306 * Extract an IPv4 address, which is in network byte order, and not
307 * necessarily aligned, and provide the result in host byte order.
308 */
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 */
315
316 /*
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
325 * headers.)
326 *
327 * XXX - on platforms that can do unaligned accesses, just cast and
328 * dereference the pointer.
329 */
330 static inline uint16_t
331 EXTRACT_HE_U_2(const void *p)
332 {
333 uint16_t val;
334
335 UNALIGNED_MEMCPY(&val, p, sizeof(uint16_t));
336 return val;
337 }
338
339 static inline int16_t
340 EXTRACT_HE_S_2(const void *p)
341 {
342 int16_t val;
343
344 UNALIGNED_MEMCPY(&val, p, sizeof(int16_t));
345 return val;
346 }
347
348 static inline uint32_t
349 EXTRACT_HE_U_4(const void *p)
350 {
351 uint32_t val;
352
353 UNALIGNED_MEMCPY(&val, p, sizeof(uint32_t));
354 return val;
355 }
356
357 static inline int32_t
358 EXTRACT_HE_S_4(const void *p)
359 {
360 int32_t val;
361
362 UNALIGNED_MEMCPY(&val, p, sizeof(int32_t));
363 return val;
364 }
365
366 /*
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.
370 *
371 * This works the same way regardless of the host's byte order.
372 */
373 static inline uint32_t
374 EXTRACT_IPV4_TO_NETWORK_ORDER(const void *p)
375 {
376 uint32_t addr;
377
378 UNALIGNED_MEMCPY(&addr, p, sizeof(uint32_t));
379 return addr;
380 }
381
382 /*
383 * Non-power-of-2 sizes.
384 */
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)))
389
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))))
399
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)))
406
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))))
420
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)))
428
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))))
444
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)))
453
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))))
471
472 /*
473 * Macros to extract possibly-unaligned little-endian integral values.
474 * XXX - do loads on little-endian machines that support unaligned loads?
475 */
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)))
510
511 /*
512 * Non-power-of-2 sizes.
513 */
514
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)))
544
545 /*
546 * Macros to check the presence of the values in question.
547 */
548 #define ND_TTEST_1(p) ND_TTEST_LEN((p), 1)
549 #define ND_TCHECK_1(p) ND_TCHECK_LEN((p), 1)
550
551 #define ND_TTEST_2(p) ND_TTEST_LEN((p), 2)
552 #define ND_TCHECK_2(p) ND_TCHECK_LEN((p), 2)
553
554 #define ND_TTEST_3(p) ND_TTEST_LEN((p), 3)
555 #define ND_TCHECK_3(p) ND_TCHECK_LEN((p), 3)
556
557 #define ND_TTEST_4(p) ND_TTEST_LEN((p), 4)
558 #define ND_TCHECK_4(p) ND_TCHECK_LEN((p), 4)
559
560 #define ND_TTEST_5(p) ND_TTEST_LEN((p), 5)
561 #define ND_TCHECK_5(p) ND_TCHECK_LEN((p), 5)
562
563 #define ND_TTEST_6(p) ND_TTEST_LEN((p), 6)
564 #define ND_TCHECK_6(p) ND_TCHECK_LEN((p), 6)
565
566 #define ND_TTEST_7(p) ND_TTEST_LEN((p), 7)
567 #define ND_TCHECK_7(p) ND_TCHECK_LEN((p), 7)
568
569 #define ND_TTEST_8(p) ND_TTEST_LEN((p), 8)
570 #define ND_TCHECK_8(p) ND_TCHECK_LEN((p), 8)
571
572 #define ND_TTEST_16(p) ND_TTEST_LEN((p), 16)
573 #define ND_TCHECK_16(p) ND_TCHECK_LEN((p), 16)
574
575 /* get_u_1 and get_s_1 */
576
577 static inline uint8_t
578 get_u_1(netdissect_options *ndo, const u_char *p)
579 {
580 if (!ND_TTEST_1(p))
581 longjmp(ndo->ndo_truncated, 1);
582 return EXTRACT_U_1(p);
583 }
584
585 static inline int8_t
586 get_s_1(netdissect_options *ndo, const u_char *p)
587 {
588 if (!ND_TTEST_1(p))
589 longjmp(ndo->ndo_truncated, 1);
590 return EXTRACT_S_1(p);
591 }
592
593 /* get_be_u_N */
594
595 static inline uint16_t
596 get_be_u_2(netdissect_options *ndo, const u_char *p)
597 {
598 if (!ND_TTEST_2(p))
599 longjmp(ndo->ndo_truncated, 1);
600 return EXTRACT_BE_U_2(p);
601 }
602
603 static inline uint32_t
604 get_be_u_3(netdissect_options *ndo, const u_char *p)
605 {
606 if (!ND_TTEST_3(p))
607 longjmp(ndo->ndo_truncated, 1);
608 return EXTRACT_BE_U_3(p);
609 }
610
611 static inline uint32_t
612 get_be_u_4(netdissect_options *ndo, const u_char *p)
613 {
614 if (!ND_TTEST_4(p))
615 longjmp(ndo->ndo_truncated, 1);
616 return EXTRACT_BE_U_4(p);
617 }
618
619 static inline uint64_t
620 get_be_u_5(netdissect_options *ndo, const u_char *p)
621 {
622 if (!ND_TTEST_5(p))
623 longjmp(ndo->ndo_truncated, 1);
624 return EXTRACT_BE_U_5(p);
625 }
626
627 static inline uint64_t
628 get_be_u_6(netdissect_options *ndo, const u_char *p)
629 {
630 if (!ND_TTEST_6(p))
631 longjmp(ndo->ndo_truncated, 1);
632 return EXTRACT_BE_U_6(p);
633 }
634
635 static inline uint64_t
636 get_be_u_7(netdissect_options *ndo, const u_char *p)
637 {
638 if (!ND_TTEST_7(p))
639 longjmp(ndo->ndo_truncated, 1);
640 return EXTRACT_BE_U_7(p);
641 }
642
643 static inline uint64_t
644 get_be_u_8(netdissect_options *ndo, const u_char *p)
645 {
646 if (!ND_TTEST_8(p))
647 longjmp(ndo->ndo_truncated, 1);
648 return EXTRACT_BE_U_8(p);
649 }
650
651 /* get_be_s_N */
652
653 static inline int16_t
654 get_be_s_2(netdissect_options *ndo, const u_char *p)
655 {
656 if (!ND_TTEST_2(p))
657 longjmp(ndo->ndo_truncated, 1);
658 return EXTRACT_BE_S_2(p);
659 }
660
661 static inline int32_t
662 get_be_s_3(netdissect_options *ndo, const u_char *p)
663 {
664 if (!ND_TTEST_3(p))
665 longjmp(ndo->ndo_truncated, 1);
666 return EXTRACT_BE_S_3(p);
667 }
668
669 static inline int32_t
670 get_be_s_4(netdissect_options *ndo, const u_char *p)
671 {
672 if (!ND_TTEST_4(p))
673 longjmp(ndo->ndo_truncated, 1);
674 return EXTRACT_BE_S_4(p);
675 }
676
677 static inline int64_t
678 get_be_s_5(netdissect_options *ndo, const u_char *p)
679 {
680 if (!ND_TTEST_5(p))
681 longjmp(ndo->ndo_truncated, 1);
682 return EXTRACT_BE_S_5(p);
683 }
684
685 static inline int64_t
686 get_be_s_6(netdissect_options *ndo, const u_char *p)
687 {
688 if (!ND_TTEST_6(p))
689 longjmp(ndo->ndo_truncated, 1);
690 return EXTRACT_BE_S_6(p);
691 }
692
693 static inline int64_t
694 get_be_s_7(netdissect_options *ndo, const u_char *p)
695 {
696 if (!ND_TTEST_7(p))
697 longjmp(ndo->ndo_truncated, 1);
698 return EXTRACT_BE_S_7(p);
699 }
700
701 static inline int64_t
702 get_be_s_8(netdissect_options *ndo, const u_char *p)
703 {
704 if (!ND_TTEST_8(p))
705 longjmp(ndo->ndo_truncated, 1);
706 return EXTRACT_BE_S_8(p);
707 }
708
709 /* get_he_u_N */
710
711 static inline uint16_t
712 get_he_u_2(netdissect_options *ndo, const u_char *p)
713 {
714 if (!ND_TTEST_2(p))
715 longjmp(ndo->ndo_truncated, 1);
716 return EXTRACT_HE_U_2(p);
717 }
718
719 static inline uint32_t
720 get_he_u_4(netdissect_options *ndo, const u_char *p)
721 {
722 if (!ND_TTEST_4(p))
723 longjmp(ndo->ndo_truncated, 1);
724 return EXTRACT_HE_U_4(p);
725 }
726
727 /* get_he_s_N */
728
729 static inline int16_t
730 get_he_s_2(netdissect_options *ndo, const u_char *p)
731 {
732 if (!ND_TTEST_2(p))
733 longjmp(ndo->ndo_truncated, 1);
734 return EXTRACT_HE_S_2(p);
735 }
736
737 static inline int32_t
738 get_he_s_4(netdissect_options *ndo, const u_char *p)
739 {
740 if (!ND_TTEST_4(p))
741 longjmp(ndo->ndo_truncated, 1);
742 return EXTRACT_HE_S_4(p);
743 }
744
745 /* get_le_u_N */
746
747 static inline uint16_t
748 get_le_u_2(netdissect_options *ndo, const u_char *p)
749 {
750 if (!ND_TTEST_2(p))
751 longjmp(ndo->ndo_truncated, 1);
752 return EXTRACT_LE_U_2(p);
753 }
754
755 static inline uint32_t
756 get_le_u_3(netdissect_options *ndo, const u_char *p)
757 {
758 if (!ND_TTEST_3(p))
759 longjmp(ndo->ndo_truncated, 1);
760 return EXTRACT_LE_U_3(p);
761 }
762
763 static inline uint32_t
764 get_le_u_4(netdissect_options *ndo, const u_char *p)
765 {
766 if (!ND_TTEST_4(p))
767 longjmp(ndo->ndo_truncated, 1);
768 return EXTRACT_LE_U_4(p);
769 }
770
771 static inline uint64_t
772 get_le_u_5(netdissect_options *ndo, const u_char *p)
773 {
774 if (!ND_TTEST_5(p))
775 longjmp(ndo->ndo_truncated, 1);
776 return EXTRACT_LE_U_5(p);
777 }
778
779 static inline uint64_t
780 get_le_u_6(netdissect_options *ndo, const u_char *p)
781 {
782 if (!ND_TTEST_6(p))
783 longjmp(ndo->ndo_truncated, 1);
784 return EXTRACT_LE_U_6(p);
785 }
786
787 static inline uint64_t
788 get_le_u_7(netdissect_options *ndo, const u_char *p)
789 {
790 if (!ND_TTEST_7(p))
791 longjmp(ndo->ndo_truncated, 1);
792 return EXTRACT_LE_U_7(p);
793 }
794
795 static inline uint64_t
796 get_le_u_8(netdissect_options *ndo, const u_char *p)
797 {
798 if (!ND_TTEST_8(p))
799 longjmp(ndo->ndo_truncated, 1);
800 return EXTRACT_LE_U_8(p);
801 }
802
803 /* get_le_s_N */
804
805 static inline int16_t
806 get_le_s_2(netdissect_options *ndo, const u_char *p)
807 {
808 if (!ND_TTEST_2(p))
809 longjmp(ndo->ndo_truncated, 1);
810 return EXTRACT_LE_S_2(p);
811 }
812
813 static inline int32_t
814 get_le_s_3(netdissect_options *ndo, const u_char *p)
815 {
816 if (!ND_TTEST_3(p))
817 longjmp(ndo->ndo_truncated, 1);
818 return EXTRACT_LE_S_3(p);
819 }
820
821 static inline int32_t
822 get_le_s_4(netdissect_options *ndo, const u_char *p)
823 {
824 if (!ND_TTEST_4(p))
825 longjmp(ndo->ndo_truncated, 1);
826 return EXTRACT_LE_S_4(p);
827 }
828
829 static inline int64_t
830 get_le_s_8(netdissect_options *ndo, const u_char *p)
831 {
832 if (!ND_TTEST_8(p))
833 longjmp(ndo->ndo_truncated, 1);
834 return EXTRACT_LE_S_8(p);
835 }
836
837 /* get_ipv4_to_{host|network]_order */
838
839 static inline uint32_t
840 get_ipv4_to_host_order(netdissect_options *ndo, const u_char *p)
841 {
842 if (!ND_TTEST_4(p))
843 longjmp(ndo->ndo_truncated, 1);
844 return EXTRACT_IPV4_TO_HOST_ORDER(p);
845 }
846
847 static inline uint32_t
848 get_ipv4_to_network_order(netdissect_options *ndo, const u_char *p)
849 {
850 if (!ND_TTEST_4(p))
851 longjmp(ndo->ndo_truncated, 1);
852 return EXTRACT_IPV4_TO_NETWORK_ORDER(p);
853 }
854
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))
857
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))
865
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))
873
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))
876
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))
879
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))
887
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))
892
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))