]> The Tcpdump Group git mirrors - tcpdump/blob - extract.h
Merge remote-tracking branch 'upstream/master'
[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 /*
23 * For 8-bit values; provided for the sake of completeness. Byte order
24 * isn't relevant, and alignment isn't an issue.
25 */
26 #define EXTRACT_8BITS(p) (*(p))
27 #define EXTRACT_LE_8BITS(p) (*(p))
28
29 /*
30 * Inline functions or macros to extract possibly-unaligned big-endian
31 * integral values.
32 */
33 #include "funcattrs.h"
34
35 /*
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.
42 *
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)).
46 *
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)).
51 *
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.)
56 */
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
64
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__))
70 /*
71 * The processor natively handles unaligned loads, so we can just
72 * cast the pointer and fetch through it.
73 *
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?
84 */
85 static inline uint16_t UNALIGNED_OK
86 EXTRACT_16BITS(const void *p)
87 {
88 return ((uint16_t)ntohs(*(const uint16_t *)(p)));
89 }
90
91 static inline uint32_t UNALIGNED_OK
92 EXTRACT_32BITS(const void *p)
93 {
94 return ((uint32_t)ntohl(*(const uint32_t *)(p)));
95 }
96
97 static inline uint64_t UNALIGNED_OK
98 EXTRACT_64BITS(const void *p)
99 {
100 return ((uint64_t)(((uint64_t)ntohl(*((const uint32_t *)(p) + 0))) << 32 |
101 ((uint64_t)ntohl(*((const uint32_t *)(p) + 1))) << 0));
102
103 }
104 #elif defined(__GNUC__) && defined(HAVE___ATTRIBUTE__) && \
105 (defined(__alpha) || defined(__alpha__) || \
106 defined(__mips) || defined(__mips__))
107 /*
108 * This is MIPS or Alpha, which don't natively handle unaligned loads,
109 * but which have instructions that can help when doing unaligned
110 * loads, and this is a GCC-compatible compiler and we have __attribute__,
111 * which we assume that mean we have __attribute__((packed)), which
112 * we can use to convince the compiler to generate those instructions.
113 *
114 * Declare packed structures containing a uint16_t and a uint32_t,
115 * cast the pointer to point to one of those, and fetch through it;
116 * the GCC manual doesn't appear to explicitly say that
117 * __attribute__((packed)) causes the compiler to generate unaligned-safe
118 * code, but it apppears to do so.
119 *
120 * We do this in case the compiler can generate code using those
121 * instructions to do an unaligned load and pass stuff to "ntohs()" or
122 * "ntohl()", which might be better than than the code to fetch the
123 * bytes one at a time and assemble them. (That might not be the
124 * case on a little-endian platform, such as DEC's MIPS machines and
125 * Alpha machines, where "ntohs()" and "ntohl()" might not be done
126 * inline.)
127 *
128 * We do this only for specific architectures because, for example,
129 * at least some versions of GCC, when compiling for 64-bit SPARC,
130 * generate code that assumes alignment if we do this.
131 *
132 * XXX - add other architectures and compilers as possible and
133 * appropriate.
134 *
135 * HP's C compiler, indicated by __HP_cc being defined, supports
136 * "#pragma unaligned N" in version A.05.50 and later, where "N"
137 * specifies a number of bytes at which the typedef on the next
138 * line is aligned, e.g.
139 *
140 * #pragma unalign 1
141 * typedef uint16_t unaligned_uint16_t;
142 *
143 * to define unaligned_uint16_t as a 16-bit unaligned data type.
144 * This could be presumably used, in sufficiently recent versions of
145 * the compiler, with macros similar to those below. This would be
146 * useful only if that compiler could generate better code for PA-RISC
147 * or Itanium than would be generated by a bunch of shifts-and-ORs.
148 *
149 * DEC C, indicated by __DECC being defined, has, at least on Alpha,
150 * an __unaligned qualifier that can be applied to pointers to get the
151 * compiler to generate code that does unaligned loads and stores when
152 * dereferencing the pointer in question.
153 *
154 * XXX - what if the native C compiler doesn't support
155 * __attribute__((packed))? How can we get it to generate unaligned
156 * accesses for *specific* items?
157 */
158 typedef struct {
159 uint16_t val;
160 } __attribute__((packed)) unaligned_uint16_t;
161
162 typedef struct {
163 uint32_t val;
164 } __attribute__((packed)) unaligned_uint32_t;
165
166 UNALIGNED_OK static inline uint16_t
167 EXTRACT_16BITS(const void *p)
168 {
169 return ((uint16_t)ntohs(((const unaligned_uint16_t *)(p))->val));
170 }
171
172 UNALIGNED_OK static inline uint32_t
173 EXTRACT_32BITS(const void *p)
174 {
175 return ((uint32_t)ntohl(((const unaligned_uint32_t *)(p))->val));
176 }
177
178 UNALIGNED_OK static inline uint64_t
179 EXTRACT_64BITS(const void *p)
180 {
181 return ((uint64_t)(((uint64_t)ntohl(((const unaligned_uint32_t *)(p) + 0)->val)) << 32 |
182 ((uint64_t)ntohl(((const unaligned_uint32_t *)(p) + 1)->val)) << 0));
183 }
184 #else
185 /*
186 * This architecture doesn't natively support unaligned loads, and either
187 * this isn't a GCC-compatible compiler, we don't have __attribute__,
188 * or we do but we don't know of any better way with this instruction
189 * set to do unaligned loads, so do unaligned loads of big-endian
190 * quantities the hard way - fetch the bytes one at a time and
191 * assemble them.
192 */
193 #define EXTRACT_16BITS(p) \
194 ((uint16_t)(((uint16_t)(*((const uint8_t *)(p) + 0)) << 8) | \
195 ((uint16_t)(*((const uint8_t *)(p) + 1)) << 0)))
196 #define EXTRACT_32BITS(p) \
197 ((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 0)) << 24) | \
198 ((uint32_t)(*((const uint8_t *)(p) + 1)) << 16) | \
199 ((uint32_t)(*((const uint8_t *)(p) + 2)) << 8) | \
200 ((uint32_t)(*((const uint8_t *)(p) + 3)) << 0)))
201 #define EXTRACT_64BITS(p) \
202 ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 56) | \
203 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 48) | \
204 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 40) | \
205 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 32) | \
206 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 24) | \
207 ((uint64_t)(*((const uint8_t *)(p) + 5)) << 16) | \
208 ((uint64_t)(*((const uint8_t *)(p) + 6)) << 8) | \
209 ((uint64_t)(*((const uint8_t *)(p) + 7)) << 0)))
210 #endif /* unaligned access checks */
211
212 #define EXTRACT_24BITS(p) \
213 ((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 0)) << 16) | \
214 ((uint32_t)(*((const uint8_t *)(p) + 1)) << 8) | \
215 ((uint32_t)(*((const uint8_t *)(p) + 2)) << 0)))
216
217 #define EXTRACT_40BITS(p) \
218 ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 32) | \
219 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 24) | \
220 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 16) | \
221 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 8) | \
222 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 0)))
223
224 #define EXTRACT_48BITS(p) \
225 ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 40) | \
226 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 32) | \
227 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 24) | \
228 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 16) | \
229 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 8) | \
230 ((uint64_t)(*((const uint8_t *)(p) + 5)) << 0)))
231
232 #define EXTRACT_56BITS(p) \
233 ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 48) | \
234 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 40) | \
235 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 32) | \
236 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 24) | \
237 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 16) | \
238 ((uint64_t)(*((const uint8_t *)(p) + 5)) << 8) | \
239 ((uint64_t)(*((const uint8_t *)(p) + 6)) << 0)))
240
241 /*
242 * Macros to extract possibly-unaligned little-endian integral values.
243 * XXX - do loads on little-endian machines that support unaligned loads?
244 */
245 #define EXTRACT_LE_16BITS(p) \
246 ((uint16_t)(((uint16_t)(*((const uint8_t *)(p) + 1)) << 8) | \
247 ((uint16_t)(*((const uint8_t *)(p) + 0)) << 0)))
248 #define EXTRACT_LE_32BITS(p) \
249 ((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 3)) << 24) | \
250 ((uint32_t)(*((const uint8_t *)(p) + 2)) << 16) | \
251 ((uint32_t)(*((const uint8_t *)(p) + 1)) << 8) | \
252 ((uint32_t)(*((const uint8_t *)(p) + 0)) << 0)))
253 #define EXTRACT_LE_24BITS(p) \
254 ((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 2)) << 16) | \
255 ((uint32_t)(*((const uint8_t *)(p) + 1)) << 8) | \
256 ((uint32_t)(*((const uint8_t *)(p) + 0)) << 0)))
257 #define EXTRACT_LE_64BITS(p) \
258 ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 7)) << 56) | \
259 ((uint64_t)(*((const uint8_t *)(p) + 6)) << 48) | \
260 ((uint64_t)(*((const uint8_t *)(p) + 5)) << 40) | \
261 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 32) | \
262 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 24) | \
263 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 16) | \
264 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 8) | \
265 ((uint64_t)(*((const uint8_t *)(p) + 0)) << 0)))
266
267 /*
268 * Macros to check the presence of the values in question.
269 */
270 #define ND_TTEST_8BITS(p) ND_TTEST2(*(p), 1)
271 #define ND_TCHECK_8BITS(p) ND_TCHECK2(*(p), 1)
272
273 #define ND_TTEST_16BITS(p) ND_TTEST2(*(p), 2)
274 #define ND_TCHECK_16BITS(p) ND_TCHECK2(*(p), 2)
275
276 #define ND_TTEST_24BITS(p) ND_TTEST2(*(p), 3)
277 #define ND_TCHECK_24BITS(p) ND_TCHECK2(*(p), 3)
278
279 #define ND_TTEST_32BITS(p) ND_TTEST2(*(p), 4)
280 #define ND_TCHECK_32BITS(p) ND_TCHECK2(*(p), 4)
281
282 #define ND_TTEST_40BITS(p) ND_TTEST2(*(p), 5)
283 #define ND_TCHECK_40BITS(p) ND_TCHECK2(*(p), 5)
284
285 #define ND_TTEST_48BITS(p) ND_TTEST2(*(p), 6)
286 #define ND_TCHECK_48BITS(p) ND_TCHECK2(*(p), 6)
287
288 #define ND_TTEST_56BITS(p) ND_TTEST2(*(p), 7)
289 #define ND_TCHECK_56BITS(p) ND_TCHECK2(*(p), 7)
290
291 #define ND_TTEST_64BITS(p) ND_TTEST2(*(p), 8)
292 #define ND_TCHECK_64BITS(p) ND_TCHECK2(*(p), 8)
293
294 #define ND_TTEST_128BITS(p) ND_TTEST2(*(p), 16)
295 #define ND_TCHECK_128BITS(p) ND_TCHECK2(*(p), 16)