]>
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.
23 * Inline functions or macros to extract possibly-unaligned big-endian
26 #include "funcattrs.h"
29 * If we have versions of GCC or Clang that support an __attribute__
30 * to say "if we're building with unsigned behavior sanitization,
31 * don't complain about undefined behavior in this function", we
32 * label these functions with that attribute - we *know* it's undefined
33 * in the C standard, but we *also* know it does what we want with
34 * the ISA we're targeting and the compiler we're using.
36 * For GCC 4.9.0 and later, we use __attribute__((no_sanitize_undefined));
37 * pre-5.0 GCC doesn't have __has_attribute, and I'm not sure whether
38 * GCC or Clang first had __attribute__((no_sanitize(XXX)).
40 * For Clang, we check for __attribute__((no_sanitize(XXX)) with
41 * __has_attribute, as there are versions of Clang that support
42 * __attribute__((no_sanitize("undefined")) but don't support
43 * __attribute__((no_sanitize_undefined)).
45 * We define this here, rather than in funcattrs.h, because we
46 * only want it used here, we don't want it to be broadly used.
47 * (Any printer will get this defined, but this should at least
48 * make it harder for people to find.)
50 #if defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 409)
51 #define UNALIGNED_OK __attribute__((no_sanitize_undefined))
52 #elif __has_attribute(no_sanitize)
53 #define UNALIGNED_OK __attribute__((no_sanitize("undefined")))
60 * The processor doesn't natively handle unaligned loads.
62 #if defined(__GNUC__) && defined(HAVE___ATTRIBUTE__) && \
63 (defined(__alpha) || defined(__alpha__) || \
64 defined(__mips) || defined(__mips__))
67 * This is a GCC-compatible compiler and we have __attribute__, which
68 * we assume that mean we have __attribute__((packed)), and this is
69 * MIPS or Alpha, which has instructions that can help when doing
72 * Declare packed structures containing a uint16_t and a uint32_t,
73 * cast the pointer to point to one of those, and fetch through it;
74 * the GCC manual doesn't appear to explicitly say that
75 * __attribute__((packed)) causes the compiler to generate unaligned-safe
76 * code, but it apppears to do so.
78 * We do this in case the compiler can generate code using those
79 * instructions to do an unaligned load and pass stuff to "ntohs()" or
80 * "ntohl()", which might be better than than the code to fetch the
81 * bytes one at a time and assemble them. (That might not be the
82 * case on a little-endian platform, such as DEC's MIPS machines and
83 * Alpha machines, where "ntohs()" and "ntohl()" might not be done
86 * We do this only for specific architectures because, for example,
87 * at least some versions of GCC, when compiling for 64-bit SPARC,
88 * generate code that assumes alignment if we do this.
90 * XXX - add other architectures and compilers as possible and
93 * HP's C compiler, indicated by __HP_cc being defined, supports
94 * "#pragma unaligned N" in version A.05.50 and later, where "N"
95 * specifies a number of bytes at which the typedef on the next
96 * line is aligned, e.g.
99 * typedef uint16_t unaligned_uint16_t;
101 * to define unaligned_uint16_t as a 16-bit unaligned data type.
102 * This could be presumably used, in sufficiently recent versions of
103 * the compiler, with macros similar to those below. This would be
104 * useful only if that compiler could generate better code for PA-RISC
105 * or Itanium than would be generated by a bunch of shifts-and-ORs.
107 * DEC C, indicated by __DECC being defined, has, at least on Alpha,
108 * an __unaligned qualifier that can be applied to pointers to get the
109 * compiler to generate code that does unaligned loads and stores when
110 * dereferencing the pointer in question.
112 * XXX - what if the native C compiler doesn't support
113 * __attribute__((packed))? How can we get it to generate unaligned
114 * accesses for *specific* items?
118 } __attribute__((packed
)) unaligned_uint16_t
;
122 } __attribute__((packed
)) unaligned_uint32_t
;
124 UNALIGNED_OK
static inline uint16_t
125 EXTRACT_16BITS(const void *p
)
127 return ((uint16_t)ntohs(((const unaligned_uint16_t
*)(p
))->val
));
130 UNALIGNED_OK
static inline uint32_t
131 EXTRACT_32BITS(const void *p
)
133 return ((uint32_t)ntohl(((const unaligned_uint32_t
*)(p
))->val
));
136 UNALIGNED_OK
static inline uint64_t
137 EXTRACT_64BITS(const void *p
)
139 return ((uint64_t)(((uint64_t)ntohl(((const unaligned_uint32_t
*)(p
) + 0)->val
)) << 32 |
140 ((uint64_t)ntohl(((const unaligned_uint32_t
*)(p
) + 1)->val
)) << 0));
143 #else /* have to do it a byte at a time */
145 * This isn't a GCC-compatible compiler, we don't have __attribute__,
146 * or we do but we don't know of any better way with this instruction
147 * set to do unaligned loads, so do unaligned loads of big-endian
148 * quantities the hard way - fetch the bytes one at a time and
151 #define EXTRACT_16BITS(p) \
152 ((uint16_t)(((uint16_t)(*((const uint8_t *)(p) + 0)) << 8) | \
153 ((uint16_t)(*((const uint8_t *)(p) + 1)) << 0)))
154 #define EXTRACT_32BITS(p) \
155 ((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 0)) << 24) | \
156 ((uint32_t)(*((const uint8_t *)(p) + 1)) << 16) | \
157 ((uint32_t)(*((const uint8_t *)(p) + 2)) << 8) | \
158 ((uint32_t)(*((const uint8_t *)(p) + 3)) << 0)))
159 #define EXTRACT_64BITS(p) \
160 ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 56) | \
161 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 48) | \
162 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 40) | \
163 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 32) | \
164 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 24) | \
165 ((uint64_t)(*((const uint8_t *)(p) + 5)) << 16) | \
166 ((uint64_t)(*((const uint8_t *)(p) + 6)) << 8) | \
167 ((uint64_t)(*((const uint8_t *)(p) + 7)) << 0)))
168 #endif /* must special-case unaligned accesses */
169 #else /* LBL_ALIGN */
171 * The processor natively handles unaligned loads, so we can just
172 * cast the pointer and fetch through it.
174 static inline uint16_t UNALIGNED_OK
175 EXTRACT_16BITS(const void *p
)
177 return ((uint16_t)ntohs(*(const uint16_t *)(p
)));
180 static inline uint32_t UNALIGNED_OK
181 EXTRACT_32BITS(const void *p
)
183 return ((uint32_t)ntohl(*(const uint32_t *)(p
)));
186 static inline uint64_t UNALIGNED_OK
187 EXTRACT_64BITS(const void *p
)
189 return ((uint64_t)(((uint64_t)ntohl(*((const uint32_t *)(p
) + 0))) << 32 |
190 ((uint64_t)ntohl(*((const uint32_t *)(p
) + 1))) << 0));
194 #endif /* LBL_ALIGN */
196 #define EXTRACT_24BITS(p) \
197 ((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 0)) << 16) | \
198 ((uint32_t)(*((const uint8_t *)(p) + 1)) << 8) | \
199 ((uint32_t)(*((const uint8_t *)(p) + 2)) << 0)))
201 #define EXTRACT_40BITS(p) \
202 ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 32) | \
203 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 24) | \
204 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 16) | \
205 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 8) | \
206 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 0)))
208 #define EXTRACT_48BITS(p) \
209 ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 40) | \
210 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 32) | \
211 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 24) | \
212 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 16) | \
213 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 8) | \
214 ((uint64_t)(*((const uint8_t *)(p) + 5)) << 0)))
216 #define EXTRACT_56BITS(p) \
217 ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 48) | \
218 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 40) | \
219 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 32) | \
220 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 24) | \
221 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 16) | \
222 ((uint64_t)(*((const uint8_t *)(p) + 5)) << 8) | \
223 ((uint64_t)(*((const uint8_t *)(p) + 6)) << 0)))
226 * Macros to extract possibly-unaligned little-endian integral values.
227 * XXX - do loads on little-endian machines that support unaligned loads?
229 #define EXTRACT_LE_8BITS(p) (*(p))
230 #define EXTRACT_LE_16BITS(p) \
231 ((uint16_t)(((uint16_t)(*((const uint8_t *)(p) + 1)) << 8) | \
232 ((uint16_t)(*((const uint8_t *)(p) + 0)) << 0)))
233 #define EXTRACT_LE_32BITS(p) \
234 ((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 3)) << 24) | \
235 ((uint32_t)(*((const uint8_t *)(p) + 2)) << 16) | \
236 ((uint32_t)(*((const uint8_t *)(p) + 1)) << 8) | \
237 ((uint32_t)(*((const uint8_t *)(p) + 0)) << 0)))
238 #define EXTRACT_LE_24BITS(p) \
239 ((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 2)) << 16) | \
240 ((uint32_t)(*((const uint8_t *)(p) + 1)) << 8) | \
241 ((uint32_t)(*((const uint8_t *)(p) + 0)) << 0)))
242 #define EXTRACT_LE_64BITS(p) \
243 ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 7)) << 56) | \
244 ((uint64_t)(*((const uint8_t *)(p) + 6)) << 48) | \
245 ((uint64_t)(*((const uint8_t *)(p) + 5)) << 40) | \
246 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 32) | \
247 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 24) | \
248 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 16) | \
249 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 8) | \
250 ((uint64_t)(*((const uint8_t *)(p) + 0)) << 0)))
253 * Macros to check the presence of the values in question.
255 #define ND_TTEST_8BITS(p) ND_TTEST2(*(p), 1)
256 #define ND_TCHECK_8BITS(p) ND_TCHECK2(*(p), 1)
258 #define ND_TTEST_16BITS(p) ND_TTEST2(*(p), 2)
259 #define ND_TCHECK_16BITS(p) ND_TCHECK2(*(p), 2)
261 #define ND_TTEST_24BITS(p) ND_TTEST2(*(p), 3)
262 #define ND_TCHECK_24BITS(p) ND_TCHECK2(*(p), 3)
264 #define ND_TTEST_32BITS(p) ND_TTEST2(*(p), 4)
265 #define ND_TCHECK_32BITS(p) ND_TCHECK2(*(p), 4)
267 #define ND_TTEST_40BITS(p) ND_TTEST2(*(p), 5)
268 #define ND_TCHECK_40BITS(p) ND_TCHECK2(*(p), 5)
270 #define ND_TTEST_48BITS(p) ND_TTEST2(*(p), 6)
271 #define ND_TCHECK_48BITS(p) ND_TCHECK2(*(p), 6)
273 #define ND_TTEST_56BITS(p) ND_TTEST2(*(p), 7)
274 #define ND_TCHECK_56BITS(p) ND_TCHECK2(*(p), 7)
276 #define ND_TTEST_64BITS(p) ND_TTEST2(*(p), 8)
277 #define ND_TCHECK_64BITS(p) ND_TCHECK2(*(p), 8)