lib: remove unused libfruutils
[unleashed.git] / contrib / tcpdump / extract.h
blob5969c22574b5a83cd9718c13ba7861da8d11e38d
1 /*
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
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.
23 * For 8-bit values; provided for the sake of completeness. Byte order
24 * isn't relevant, and alignment isn't an issue.
26 #define EXTRACT_8BITS(p) (*(p))
27 #define EXTRACT_LE_8BITS(p) (*(p))
30 * Inline functions or macros to extract possibly-unaligned big-endian
31 * integral values.
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")))
61 #else
62 #define UNALIGNED_OK
63 #endif
65 #ifdef LBL_ALIGN
67 * The processor doesn't natively handle unaligned loads.
69 #if defined(__GNUC__) && defined(HAVE___ATTRIBUTE__) && \
70 (defined(__alpha) || defined(__alpha__) || \
71 defined(__mips) || defined(__mips__))
74 * This is a GCC-compatible compiler and we have __attribute__, which
75 * we assume that mean we have __attribute__((packed)), and this is
76 * MIPS or Alpha, which has instructions that can help when doing
77 * unaligned loads.
79 * Declare packed structures containing a uint16_t and a uint32_t,
80 * cast the pointer to point to one of those, and fetch through it;
81 * the GCC manual doesn't appear to explicitly say that
82 * __attribute__((packed)) causes the compiler to generate unaligned-safe
83 * code, but it apppears to do so.
85 * We do this in case the compiler can generate code using those
86 * instructions to do an unaligned load and pass stuff to "ntohs()" or
87 * "ntohl()", which might be better than than the code to fetch the
88 * bytes one at a time and assemble them. (That might not be the
89 * case on a little-endian platform, such as DEC's MIPS machines and
90 * Alpha machines, where "ntohs()" and "ntohl()" might not be done
91 * inline.)
93 * We do this only for specific architectures because, for example,
94 * at least some versions of GCC, when compiling for 64-bit SPARC,
95 * generate code that assumes alignment if we do this.
97 * XXX - add other architectures and compilers as possible and
98 * appropriate.
100 * HP's C compiler, indicated by __HP_cc being defined, supports
101 * "#pragma unaligned N" in version A.05.50 and later, where "N"
102 * specifies a number of bytes at which the typedef on the next
103 * line is aligned, e.g.
105 * #pragma unalign 1
106 * typedef uint16_t unaligned_uint16_t;
108 * to define unaligned_uint16_t as a 16-bit unaligned data type.
109 * This could be presumably used, in sufficiently recent versions of
110 * the compiler, with macros similar to those below. This would be
111 * useful only if that compiler could generate better code for PA-RISC
112 * or Itanium than would be generated by a bunch of shifts-and-ORs.
114 * DEC C, indicated by __DECC being defined, has, at least on Alpha,
115 * an __unaligned qualifier that can be applied to pointers to get the
116 * compiler to generate code that does unaligned loads and stores when
117 * dereferencing the pointer in question.
119 * XXX - what if the native C compiler doesn't support
120 * __attribute__((packed))? How can we get it to generate unaligned
121 * accesses for *specific* items?
123 typedef struct {
124 uint16_t val;
125 } __attribute__((packed)) unaligned_uint16_t;
127 typedef struct {
128 uint32_t val;
129 } __attribute__((packed)) unaligned_uint32_t;
131 UNALIGNED_OK static inline uint16_t
132 EXTRACT_16BITS(const void *p)
134 return ((uint16_t)ntohs(((const unaligned_uint16_t *)(p))->val));
137 UNALIGNED_OK static inline uint32_t
138 EXTRACT_32BITS(const void *p)
140 return ((uint32_t)ntohl(((const unaligned_uint32_t *)(p))->val));
143 UNALIGNED_OK static inline uint64_t
144 EXTRACT_64BITS(const void *p)
146 return ((uint64_t)(((uint64_t)ntohl(((const unaligned_uint32_t *)(p) + 0)->val)) << 32 |
147 ((uint64_t)ntohl(((const unaligned_uint32_t *)(p) + 1)->val)) << 0));
150 #else /* have to do it a byte at a time */
152 * This isn't a GCC-compatible compiler, we don't have __attribute__,
153 * or we do but we don't know of any better way with this instruction
154 * set to do unaligned loads, so do unaligned loads of big-endian
155 * quantities the hard way - fetch the bytes one at a time and
156 * assemble them.
158 #define EXTRACT_16BITS(p) \
159 ((uint16_t)(((uint16_t)(*((const uint8_t *)(p) + 0)) << 8) | \
160 ((uint16_t)(*((const uint8_t *)(p) + 1)) << 0)))
161 #define EXTRACT_32BITS(p) \
162 ((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 0)) << 24) | \
163 ((uint32_t)(*((const uint8_t *)(p) + 1)) << 16) | \
164 ((uint32_t)(*((const uint8_t *)(p) + 2)) << 8) | \
165 ((uint32_t)(*((const uint8_t *)(p) + 3)) << 0)))
166 #define EXTRACT_64BITS(p) \
167 ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 56) | \
168 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 48) | \
169 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 40) | \
170 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 32) | \
171 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 24) | \
172 ((uint64_t)(*((const uint8_t *)(p) + 5)) << 16) | \
173 ((uint64_t)(*((const uint8_t *)(p) + 6)) << 8) | \
174 ((uint64_t)(*((const uint8_t *)(p) + 7)) << 0)))
175 #endif /* must special-case unaligned accesses */
176 #else /* LBL_ALIGN */
178 * The processor natively handles unaligned loads, so we can just
179 * cast the pointer and fetch through it.
181 static inline uint16_t UNALIGNED_OK
182 EXTRACT_16BITS(const void *p)
184 return ((uint16_t)ntohs(*(const uint16_t *)(p)));
187 static inline uint32_t UNALIGNED_OK
188 EXTRACT_32BITS(const void *p)
190 return ((uint32_t)ntohl(*(const uint32_t *)(p)));
193 static inline uint64_t UNALIGNED_OK
194 EXTRACT_64BITS(const void *p)
196 return ((uint64_t)(((uint64_t)ntohl(*((const uint32_t *)(p) + 0))) << 32 |
197 ((uint64_t)ntohl(*((const uint32_t *)(p) + 1))) << 0));
201 #endif /* LBL_ALIGN */
203 #define EXTRACT_24BITS(p) \
204 ((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 0)) << 16) | \
205 ((uint32_t)(*((const uint8_t *)(p) + 1)) << 8) | \
206 ((uint32_t)(*((const uint8_t *)(p) + 2)) << 0)))
208 #define EXTRACT_40BITS(p) \
209 ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 32) | \
210 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 24) | \
211 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 16) | \
212 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 8) | \
213 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 0)))
215 #define EXTRACT_48BITS(p) \
216 ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 40) | \
217 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 32) | \
218 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 24) | \
219 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 16) | \
220 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 8) | \
221 ((uint64_t)(*((const uint8_t *)(p) + 5)) << 0)))
223 #define EXTRACT_56BITS(p) \
224 ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 48) | \
225 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 40) | \
226 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 32) | \
227 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 24) | \
228 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 16) | \
229 ((uint64_t)(*((const uint8_t *)(p) + 5)) << 8) | \
230 ((uint64_t)(*((const uint8_t *)(p) + 6)) << 0)))
233 * Macros to extract possibly-unaligned little-endian integral values.
234 * XXX - do loads on little-endian machines that support unaligned loads?
236 #define EXTRACT_LE_16BITS(p) \
237 ((uint16_t)(((uint16_t)(*((const uint8_t *)(p) + 1)) << 8) | \
238 ((uint16_t)(*((const uint8_t *)(p) + 0)) << 0)))
239 #define EXTRACT_LE_32BITS(p) \
240 ((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 3)) << 24) | \
241 ((uint32_t)(*((const uint8_t *)(p) + 2)) << 16) | \
242 ((uint32_t)(*((const uint8_t *)(p) + 1)) << 8) | \
243 ((uint32_t)(*((const uint8_t *)(p) + 0)) << 0)))
244 #define EXTRACT_LE_24BITS(p) \
245 ((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 2)) << 16) | \
246 ((uint32_t)(*((const uint8_t *)(p) + 1)) << 8) | \
247 ((uint32_t)(*((const uint8_t *)(p) + 0)) << 0)))
248 #define EXTRACT_LE_64BITS(p) \
249 ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 7)) << 56) | \
250 ((uint64_t)(*((const uint8_t *)(p) + 6)) << 48) | \
251 ((uint64_t)(*((const uint8_t *)(p) + 5)) << 40) | \
252 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 32) | \
253 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 24) | \
254 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 16) | \
255 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 8) | \
256 ((uint64_t)(*((const uint8_t *)(p) + 0)) << 0)))
259 * Macros to check the presence of the values in question.
261 #define ND_TTEST_8BITS(p) ND_TTEST2(*(p), 1)
262 #define ND_TCHECK_8BITS(p) ND_TCHECK2(*(p), 1)
264 #define ND_TTEST_16BITS(p) ND_TTEST2(*(p), 2)
265 #define ND_TCHECK_16BITS(p) ND_TCHECK2(*(p), 2)
267 #define ND_TTEST_24BITS(p) ND_TTEST2(*(p), 3)
268 #define ND_TCHECK_24BITS(p) ND_TCHECK2(*(p), 3)
270 #define ND_TTEST_32BITS(p) ND_TTEST2(*(p), 4)
271 #define ND_TCHECK_32BITS(p) ND_TCHECK2(*(p), 4)
273 #define ND_TTEST_40BITS(p) ND_TTEST2(*(p), 5)
274 #define ND_TCHECK_40BITS(p) ND_TCHECK2(*(p), 5)
276 #define ND_TTEST_48BITS(p) ND_TTEST2(*(p), 6)
277 #define ND_TCHECK_48BITS(p) ND_TCHECK2(*(p), 6)
279 #define ND_TTEST_56BITS(p) ND_TTEST2(*(p), 7)
280 #define ND_TCHECK_56BITS(p) ND_TCHECK2(*(p), 7)
282 #define ND_TTEST_64BITS(p) ND_TTEST2(*(p), 8)
283 #define ND_TCHECK_64BITS(p) ND_TCHECK2(*(p), 8)
285 #define ND_TTEST_128BITS(p) ND_TTEST2(*(p), 16)
286 #define ND_TCHECK_128BITS(p) ND_TCHECK2(*(p), 16)