]>
The Tcpdump Group git mirrors - libpcap/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 #include <arpa/inet.h>
26 #include <pcap/pcap-inttypes.h>
27 #include <pcap/compiler-tests.h>
28 #include "portability.h"
31 * If we have versions of GCC or Clang that support an __attribute__
32 * to say "if we're building with unsigned behavior sanitization,
33 * don't complain about undefined behavior in this function", we
34 * label these functions with that attribute - we *know* it's undefined
35 * in the C standard, but we *also* know it does what we want with
36 * the ISA we're targeting and the compiler we're using.
38 * For GCC 4.9.0 and later, we use __attribute__((no_sanitize_undefined));
39 * pre-5.0 GCC doesn't have __has_attribute, and I'm not sure whether
40 * GCC or Clang first had __attribute__((no_sanitize(XXX)).
42 * For Clang, we check for __attribute__((no_sanitize(XXX)) with
43 * __has_attribute, as there are versions of Clang that support
44 * __attribute__((no_sanitize("undefined")) but don't support
45 * __attribute__((no_sanitize_undefined)).
47 * We define this here, rather than in funcattrs.h, because we
48 * only want it used here, we don't want it to be broadly used.
49 * (Any printer will get this defined, but this should at least
50 * make it harder for people to find.)
52 #if defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 409)
53 #define UNALIGNED_OK __attribute__((no_sanitize_undefined))
54 #elif __has_attribute(no_sanitize)
55 #define UNALIGNED_OK __attribute__((no_sanitize("undefined")))
60 #if (defined(__i386__) || defined(_M_IX86) || defined(__X86__) || defined(__x86_64__) || defined(_M_X64)) || \
61 (defined(__arm__) || defined(_M_ARM) || defined(__aarch64__)) || \
62 (defined(__m68k__) && (!defined(__mc68000__) && !defined(__mc68010__))) || \
63 (defined(__ppc__) || defined(__ppc64__) || defined(_M_PPC) || defined(_ARCH_PPC) || defined(_ARCH_PPC64)) || \
64 (defined(__s390__) || defined(__s390x__) || defined(__zarch__))
66 * The processor natively handles unaligned loads, so we can just
67 * cast the pointer and fetch through it.
69 * XXX - are those all the x86 tests we need?
70 * XXX - do we need to worry about ARMv1 through ARMv5, which didn't
71 * support unaligned loads, and, if so, do we need to worry about all
72 * of them, or just some of them, e.g. ARMv5?
73 * XXX - are those the only 68k tests we need not to generated
74 * unaligned accesses if the target is the 68000 or 68010?
75 * XXX - are there any tests we don't need, because some definitions are for
76 * compilers that also predefine the GCC symbols?
77 * XXX - do we need to test for both 32-bit and 64-bit versions of those
78 * architectures in all cases?
80 UNALIGNED_OK
static inline uint16_t
81 EXTRACT_BE_U_2(const void *p
)
83 return ((uint16_t)ntohs(*(const uint16_t *)(p
)));
86 UNALIGNED_OK
static inline int16_t
87 EXTRACT_BE_S_2(const void *p
)
89 return ((int16_t)ntohs(*(const int16_t *)(p
)));
92 UNALIGNED_OK
static inline uint32_t
93 EXTRACT_BE_U_4(const void *p
)
95 return ((uint32_t)ntohl(*(const uint32_t *)(p
)));
98 UNALIGNED_OK
static inline int32_t
99 EXTRACT_BE_S_4(const void *p
)
101 return ((int32_t)ntohl(*(const int32_t *)(p
)));
104 UNALIGNED_OK
static inline uint64_t
105 EXTRACT_BE_U_8(const void *p
)
107 return ((uint64_t)(((uint64_t)ntohl(*((const uint32_t *)(p
) + 0))) << 32 |
108 ((uint64_t)ntohl(*((const uint32_t *)(p
) + 1))) << 0));
112 UNALIGNED_OK
static inline int64_t
113 EXTRACT_BE_S_8(const void *p
)
115 return ((int64_t)(((int64_t)ntohl(*((const uint32_t *)(p
) + 0))) << 32 |
116 ((uint64_t)ntohl(*((const uint32_t *)(p
) + 1))) << 0));
119 #elif PCAP_IS_AT_LEAST_GNUC_VERSION(2,0) && \
120 (defined(__alpha) || defined(__alpha__) || \
121 defined(__mips) || defined(__mips__))
123 * This is MIPS or Alpha, which don't natively handle unaligned loads,
124 * but which have instructions that can help when doing unaligned
125 * loads, and this is GCC 2.0 or later or a compiler that claims to
126 * be GCC 2.0 or later, which we assume that mean we have
127 * __attribute__((packed)), which we can use to convince the compiler
128 * to generate those instructions.
130 * Declare packed structures containing a uint16_t and a uint32_t,
131 * cast the pointer to point to one of those, and fetch through it;
132 * the GCC manual doesn't appear to explicitly say that
133 * __attribute__((packed)) causes the compiler to generate unaligned-safe
134 * code, but it apppears to do so.
136 * We do this in case the compiler can generate code using those
137 * instructions to do an unaligned load and pass stuff to "ntohs()" or
138 * "ntohl()", which might be better than than the code to fetch the
139 * bytes one at a time and assemble them. (That might not be the
140 * case on a little-endian platform, such as DEC's MIPS machines and
141 * Alpha machines, where "ntohs()" and "ntohl()" might not be done
144 * We do this only for specific architectures because, for example,
145 * at least some versions of GCC, when compiling for 64-bit SPARC,
146 * generate code that assumes alignment if we do this.
148 * XXX - add other architectures and compilers as possible and
151 * HP's C compiler, indicated by __HP_cc being defined, supports
152 * "#pragma unaligned N" in version A.05.50 and later, where "N"
153 * specifies a number of bytes at which the typedef on the next
154 * line is aligned, e.g.
157 * typedef uint16_t unaligned_uint16_t;
159 * to define unaligned_uint16_t as a 16-bit unaligned data type.
160 * This could be presumably used, in sufficiently recent versions of
161 * the compiler, with macros similar to those below. This would be
162 * useful only if that compiler could generate better code for PA-RISC
163 * or Itanium than would be generated by a bunch of shifts-and-ORs.
165 * DEC C, indicated by __DECC being defined, has, at least on Alpha,
166 * an __unaligned qualifier that can be applied to pointers to get the
167 * compiler to generate code that does unaligned loads and stores when
168 * dereferencing the pointer in question.
170 * XXX - what if the native C compiler doesn't support
171 * __attribute__((packed))? How can we get it to generate unaligned
172 * accesses for *specific* items?
176 } __attribute__((packed
)) unaligned_uint16_t
;
180 } __attribute__((packed
)) unaligned_int16_t
;
184 } __attribute__((packed
)) unaligned_uint32_t
;
188 } __attribute__((packed
)) unaligned_int32_t
;
190 UNALIGNED_OK
static inline uint16_t
191 EXTRACT_BE_U_2(const void *p
)
193 return ((uint16_t)ntohs(((const unaligned_uint16_t
*)(p
))->val
));
196 UNALIGNED_OK
static inline int16_t
197 EXTRACT_BE_S_2(const void *p
)
199 return ((int16_t)ntohs(((const unaligned_int16_t
*)(p
))->val
));
202 UNALIGNED_OK
static inline uint32_t
203 EXTRACT_BE_U_4(const void *p
)
205 return ((uint32_t)ntohl(((const unaligned_uint32_t
*)(p
))->val
));
208 UNALIGNED_OK
static inline int32_t
209 EXTRACT_BE_S_4(const void *p
)
211 return ((int32_t)ntohl(((const unaligned_int32_t
*)(p
))->val
));
214 UNALIGNED_OK
static inline uint64_t
215 EXTRACT_BE_U_8(const void *p
)
217 return ((uint64_t)(((uint64_t)ntohl(((const unaligned_uint32_t
*)(p
) + 0)->val
)) << 32 |
218 ((uint64_t)ntohl(((const unaligned_uint32_t
*)(p
) + 1)->val
)) << 0));
221 UNALIGNED_OK
static inline int64_t
222 EXTRACT_BE_S_8(const void *p
)
224 return ((int64_t)(((uint64_t)ntohl(((const unaligned_uint32_t
*)(p
) + 0)->val
)) << 32 |
225 ((uint64_t)ntohl(((const unaligned_uint32_t
*)(p
) + 1)->val
)) << 0));
229 * This architecture doesn't natively support unaligned loads, and either
230 * this isn't a GCC-compatible compiler, we don't have __attribute__,
231 * or we do but we don't know of any better way with this instruction
232 * set to do unaligned loads, so do unaligned loads of big-endian
233 * quantities the hard way - fetch the bytes one at a time and
236 #define EXTRACT_BE_U_2(p) \
237 ((uint16_t)(((uint16_t)(*((const uint8_t *)(p) + 0)) << 8) | \
238 ((uint16_t)(*((const uint8_t *)(p) + 1)) << 0)))
239 #define EXTRACT_BE_S_2(p) \
240 ((int16_t)(((uint16_t)(*((const uint8_t *)(p) + 0)) << 8) | \
241 ((uint16_t)(*((const uint8_t *)(p) + 1)) << 0)))
242 #define EXTRACT_BE_U_4(p) \
243 ((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 0)) << 24) | \
244 ((uint32_t)(*((const uint8_t *)(p) + 1)) << 16) | \
245 ((uint32_t)(*((const uint8_t *)(p) + 2)) << 8) | \
246 ((uint32_t)(*((const uint8_t *)(p) + 3)) << 0)))
247 #define EXTRACT_BE_S_4(p) \
248 ((int32_t)(((uint32_t)(*((const uint8_t *)(p) + 0)) << 24) | \
249 ((uint32_t)(*((const uint8_t *)(p) + 1)) << 16) | \
250 ((uint32_t)(*((const uint8_t *)(p) + 2)) << 8) | \
251 ((uint32_t)(*((const uint8_t *)(p) + 3)) << 0)))
252 #define EXTRACT_BE_U_8(p) \
253 ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 56) | \
254 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 48) | \
255 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 40) | \
256 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 32) | \
257 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 24) | \
258 ((uint64_t)(*((const uint8_t *)(p) + 5)) << 16) | \
259 ((uint64_t)(*((const uint8_t *)(p) + 6)) << 8) | \
260 ((uint64_t)(*((const uint8_t *)(p) + 7)) << 0)))
261 #define EXTRACT_BE_S_8(p) \
262 ((int64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 56) | \
263 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 48) | \
264 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 40) | \
265 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 32) | \
266 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 24) | \
267 ((uint64_t)(*((const uint8_t *)(p) + 5)) << 16) | \
268 ((uint64_t)(*((const uint8_t *)(p) + 6)) << 8) | \
269 ((uint64_t)(*((const uint8_t *)(p) + 7)) << 0)))
272 * Extract an IPv4 address, which is in network byte order, and not
273 * necessarily aligned, and provide the result in host byte order.
275 #define EXTRACT_IPV4_TO_HOST_ORDER(p) \
276 ((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 0)) << 24) | \
277 ((uint32_t)(*((const uint8_t *)(p) + 1)) << 16) | \
278 ((uint32_t)(*((const uint8_t *)(p) + 2)) << 8) | \
279 ((uint32_t)(*((const uint8_t *)(p) + 3)) << 0)))
280 #endif /* unaligned access checks */
283 * Non-power-of-2 sizes.
285 #define EXTRACT_BE_U_3(p) \
286 ((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 0)) << 16) | \
287 ((uint32_t)(*((const uint8_t *)(p) + 1)) << 8) | \
288 ((uint32_t)(*((const uint8_t *)(p) + 2)) << 0)))
290 #define EXTRACT_BE_S_3(p) \
291 (((*((const uint8_t *)(p) + 0)) & 0x80) ? \
292 ((int32_t)(((uint32_t)(*((const uint8_t *)(p) + 0)) << 16) | \
293 ((uint32_t)(*((const uint8_t *)(p) + 1)) << 8) | \
294 ((uint32_t)(*((const uint8_t *)(p) + 2)) << 0))) : \
295 ((int32_t)(0xFF000000U | \
296 ((uint32_t)(*((const uint8_t *)(p) + 0)) << 16) | \
297 ((uint32_t)(*((const uint8_t *)(p) + 1)) << 8) | \
298 ((uint32_t)(*((const uint8_t *)(p) + 2)) << 0))))
300 #define EXTRACT_BE_U_5(p) \
301 ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 32) | \
302 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 24) | \
303 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 16) | \
304 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 8) | \
305 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 0)))
307 #define EXTRACT_BE_S_5(p) \
308 (((*((const uint8_t *)(p) + 0)) & 0x80) ? \
309 ((int64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 32) | \
310 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 24) | \
311 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 16) | \
312 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 8) | \
313 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 0))) : \
314 ((int64_t)(INT64_T_CONSTANT(0xFFFFFF0000000000U) | \
315 ((uint64_t)(*((const uint8_t *)(p) + 0)) << 32) | \
316 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 24) | \
317 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 16) | \
318 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 8) | \
319 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 0))))
321 #define EXTRACT_BE_U_6(p) \
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)))
329 #define EXTRACT_BE_S_6(p) \
330 (((*((const uint8_t *)(p) + 0)) & 0x80) ? \
331 ((int64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 40) | \
332 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 32) | \
333 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 24) | \
334 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 16) | \
335 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 8) | \
336 ((uint64_t)(*((const uint8_t *)(p) + 5)) << 0))) : \
337 ((int64_t)(INT64_T_CONSTANT(0xFFFFFFFF00000000U) | \
338 ((uint64_t)(*((const uint8_t *)(p) + 0)) << 40) | \
339 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 32) | \
340 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 24) | \
341 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 16) | \
342 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 8) | \
343 ((uint64_t)(*((const uint8_t *)(p) + 5)) << 0))))
345 #define EXTRACT_BE_U_7(p) \
346 ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 48) | \
347 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 40) | \
348 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 32) | \
349 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 24) | \
350 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 16) | \
351 ((uint64_t)(*((const uint8_t *)(p) + 5)) << 8) | \
352 ((uint64_t)(*((const uint8_t *)(p) + 6)) << 0)))
354 #define EXTRACT_BE_S_7(p) \
355 (((*((const uint8_t *)(p) + 0)) & 0x80) ? \
356 ((int64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 48) | \
357 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 40) | \
358 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 32) | \
359 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 24) | \
360 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 16) | \
361 ((uint64_t)(*((const uint8_t *)(p) + 5)) << 8) | \
362 ((uint64_t)(*((const uint8_t *)(p) + 6)) << 0))) : \
363 ((int64_t)(INT64_T_CONSTANT(0xFFFFFFFFFF000000U) | \
364 ((uint64_t)(*((const uint8_t *)(p) + 0)) << 48) | \
365 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 40) | \
366 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 32) | \
367 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 24) | \
368 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 16) | \
369 ((uint64_t)(*((const uint8_t *)(p) + 5)) << 8) | \
370 ((uint64_t)(*((const uint8_t *)(p) + 6)) << 0))))
373 * Macros to extract possibly-unaligned little-endian integral values.
374 * XXX - do loads on little-endian machines that support unaligned loads?
376 #define EXTRACT_LE_U_2(p) \
377 ((uint16_t)(((uint16_t)(*((const uint8_t *)(p) + 1)) << 8) | \
378 ((uint16_t)(*((const uint8_t *)(p) + 0)) << 0)))
379 #define EXTRACT_LE_S_2(p) \
380 ((int16_t)(((uint16_t)(*((const uint8_t *)(p) + 1)) << 8) | \
381 ((uint16_t)(*((const uint8_t *)(p) + 0)) << 0)))
382 #define EXTRACT_LE_U_4(p) \
383 ((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 3)) << 24) | \
384 ((uint32_t)(*((const uint8_t *)(p) + 2)) << 16) | \
385 ((uint32_t)(*((const uint8_t *)(p) + 1)) << 8) | \
386 ((uint32_t)(*((const uint8_t *)(p) + 0)) << 0)))
387 #define EXTRACT_LE_S_4(p) \
388 ((int32_t)(((uint32_t)(*((const uint8_t *)(p) + 3)) << 24) | \
389 ((uint32_t)(*((const uint8_t *)(p) + 2)) << 16) | \
390 ((uint32_t)(*((const uint8_t *)(p) + 1)) << 8) | \
391 ((uint32_t)(*((const uint8_t *)(p) + 0)) << 0)))
392 #define EXTRACT_LE_U_3(p) \
393 ((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 2)) << 16) | \
394 ((uint32_t)(*((const uint8_t *)(p) + 1)) << 8) | \
395 ((uint32_t)(*((const uint8_t *)(p) + 0)) << 0)))
396 #define EXTRACT_LE_S_3(p) \
397 ((int32_t)(((uint32_t)(*((const uint8_t *)(p) + 2)) << 16) | \
398 ((uint32_t)(*((const uint8_t *)(p) + 1)) << 8) | \
399 ((uint32_t)(*((const uint8_t *)(p) + 0)) << 0)))
400 #define EXTRACT_LE_U_8(p) \
401 ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 7)) << 56) | \
402 ((uint64_t)(*((const uint8_t *)(p) + 6)) << 48) | \
403 ((uint64_t)(*((const uint8_t *)(p) + 5)) << 40) | \
404 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 32) | \
405 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 24) | \
406 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 16) | \
407 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 8) | \
408 ((uint64_t)(*((const uint8_t *)(p) + 0)) << 0)))
409 #define EXTRACT_LE_S_8(p) \
410 ((int64_t)(((uint64_t)(*((const uint8_t *)(p) + 7)) << 56) | \
411 ((uint64_t)(*((const uint8_t *)(p) + 6)) << 48) | \
412 ((uint64_t)(*((const uint8_t *)(p) + 5)) << 40) | \
413 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 32) | \
414 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 24) | \
415 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 16) | \
416 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 8) | \
417 ((uint64_t)(*((const uint8_t *)(p) + 0)) << 0)))