xref: /freebsd/contrib/tcpdump/extract.h (revision 3340d77368116708ab5b5b95acf6c9c710528300)
14edb46e9SPaul Traina /*
24edb46e9SPaul Traina  * Copyright (c) 1992, 1993, 1994, 1995, 1996
34edb46e9SPaul Traina  *	The Regents of the University of California.  All rights reserved.
44edb46e9SPaul Traina  *
54edb46e9SPaul Traina  * Redistribution and use in source and binary forms, with or without
64edb46e9SPaul Traina  * modification, are permitted provided that: (1) source code distributions
74edb46e9SPaul Traina  * retain the above copyright notice and this paragraph in its entirety, (2)
84edb46e9SPaul Traina  * distributions including binary code include the above copyright notice and
94edb46e9SPaul Traina  * this paragraph in its entirety in the documentation or other materials
104edb46e9SPaul Traina  * provided with the distribution, and (3) all advertising materials mentioning
114edb46e9SPaul Traina  * features or use of this software display the following acknowledgement:
124edb46e9SPaul Traina  * ``This product includes software developed by the University of California,
134edb46e9SPaul Traina  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
144edb46e9SPaul Traina  * the University nor the names of its contributors may be used to endorse
154edb46e9SPaul Traina  * or promote products derived from this software without specific prior
164edb46e9SPaul Traina  * written permission.
174edb46e9SPaul Traina  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
184edb46e9SPaul Traina  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
194edb46e9SPaul Traina  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
204edb46e9SPaul Traina  */
214edb46e9SPaul Traina 
221de50e9fSSam Leffler /*
231de50e9fSSam Leffler  * Macros to extract possibly-unaligned big-endian integral values.
241de50e9fSSam Leffler  */
254edb46e9SPaul Traina #ifdef LBL_ALIGN
265b0fe478SBruce M Simpson /*
275b0fe478SBruce M Simpson  * The processor doesn't natively handle unaligned loads.
285b0fe478SBruce M Simpson  */
293c602fabSXin LI #if defined(__GNUC__) && defined(HAVE___ATTRIBUTE__) && \
303c602fabSXin LI     (defined(__alpha) || defined(__alpha__) || \
313c602fabSXin LI      defined(__mips) || defined(__mips__))
323c602fabSXin LI 
335b0fe478SBruce M Simpson /*
343c602fabSXin LI  * This is a GCC-compatible compiler and we have __attribute__, which
353c602fabSXin LI  * we assume that mean we have __attribute__((packed)), and this is
363c602fabSXin LI  * MIPS or Alpha, which has instructions that can help when doing
373c602fabSXin LI  * unaligned loads.
383c602fabSXin LI  *
393c602fabSXin LI  * Declare packed structures containing a uint16_t and a uint32_t,
405b0fe478SBruce M Simpson  * cast the pointer to point to one of those, and fetch through it;
415b0fe478SBruce M Simpson  * the GCC manual doesn't appear to explicitly say that
425b0fe478SBruce M Simpson  * __attribute__((packed)) causes the compiler to generate unaligned-safe
435b0fe478SBruce M Simpson  * code, but it apppears to do so.
445b0fe478SBruce M Simpson  *
453c602fabSXin LI  * We do this in case the compiler can generate code using those
463c602fabSXin LI  * instructions to do an unaligned load and pass stuff to "ntohs()" or
473c602fabSXin LI  * "ntohl()", which might be better than than the code to fetch the
483c602fabSXin LI  * bytes one at a time and assemble them.  (That might not be the
493c602fabSXin LI  * case on a little-endian platform, such as DEC's MIPS machines and
503c602fabSXin LI  * Alpha machines, where "ntohs()" and "ntohl()" might not be done
513c602fabSXin LI  * inline.)
523c602fabSXin LI  *
533c602fabSXin LI  * We do this only for specific architectures because, for example,
543c602fabSXin LI  * at least some versions of GCC, when compiling for 64-bit SPARC,
553c602fabSXin LI  * generate code that assumes alignment if we do this.
563c602fabSXin LI  *
573c602fabSXin LI  * XXX - add other architectures and compilers as possible and
583c602fabSXin LI  * appropriate.
593c602fabSXin LI  *
603c602fabSXin LI  * HP's C compiler, indicated by __HP_cc being defined, supports
613c602fabSXin LI  * "#pragma unaligned N" in version A.05.50 and later, where "N"
623c602fabSXin LI  * specifies a number of bytes at which the typedef on the next
633c602fabSXin LI  * line is aligned, e.g.
643c602fabSXin LI  *
653c602fabSXin LI  *	#pragma unalign 1
663c602fabSXin LI  *	typedef uint16_t unaligned_uint16_t;
673c602fabSXin LI  *
683c602fabSXin LI  * to define unaligned_uint16_t as a 16-bit unaligned data type.
693c602fabSXin LI  * This could be presumably used, in sufficiently recent versions of
703c602fabSXin LI  * the compiler, with macros similar to those below.  This would be
713c602fabSXin LI  * useful only if that compiler could generate better code for PA-RISC
723c602fabSXin LI  * or Itanium than would be generated by a bunch of shifts-and-ORs.
733c602fabSXin LI  *
743c602fabSXin LI  * DEC C, indicated by __DECC being defined, has, at least on Alpha,
753c602fabSXin LI  * an __unaligned qualifier that can be applied to pointers to get the
763c602fabSXin LI  * compiler to generate code that does unaligned loads and stores when
773c602fabSXin LI  * dereferencing the pointer in question.
783c602fabSXin LI  *
793c602fabSXin LI  * XXX - what if the native C compiler doesn't support
803c602fabSXin LI  * __attribute__((packed))?  How can we get it to generate unaligned
813c602fabSXin LI  * accesses for *specific* items?
825b0fe478SBruce M Simpson  */
835b0fe478SBruce M Simpson typedef struct {
843c602fabSXin LI 	uint16_t	val;
853c602fabSXin LI } __attribute__((packed)) unaligned_uint16_t;
865b0fe478SBruce M Simpson 
875b0fe478SBruce M Simpson typedef struct {
883c602fabSXin LI 	uint32_t	val;
893c602fabSXin LI } __attribute__((packed)) unaligned_uint32_t;
905b0fe478SBruce M Simpson 
913c602fabSXin LI static inline uint16_t
92340b3427SPedro F. Giffuni EXTRACT_16BITS(const void *p)
93340b3427SPedro F. Giffuni {
943c602fabSXin LI 	return ((uint16_t)ntohs(((const unaligned_uint16_t *)(p))->val));
95340b3427SPedro F. Giffuni }
96340b3427SPedro F. Giffuni 
973c602fabSXin LI static inline uint32_t
98340b3427SPedro F. Giffuni EXTRACT_32BITS(const void *p)
99340b3427SPedro F. Giffuni {
1003c602fabSXin LI 	return ((uint32_t)ntohl(((const unaligned_uint32_t *)(p))->val));
101340b3427SPedro F. Giffuni }
102340b3427SPedro F. Giffuni 
1033c602fabSXin LI static inline uint64_t
104340b3427SPedro F. Giffuni EXTRACT_64BITS(const void *p)
105340b3427SPedro F. Giffuni {
106*3340d773SGleb Smirnoff 	return ((uint64_t)(((uint64_t)ntohl(((const unaligned_uint32_t *)(p) + 0)->val)) << 32 |
1073c602fabSXin LI 		((uint64_t)ntohl(((const unaligned_uint32_t *)(p) + 1)->val)) << 0));
108340b3427SPedro F. Giffuni }
1091de50e9fSSam Leffler 
1103c602fabSXin LI #else /* have to do it a byte at a time */
1115b0fe478SBruce M Simpson /*
1123c602fabSXin LI  * This isn't a GCC-compatible compiler, we don't have __attribute__,
1133c602fabSXin LI  * or we do but we don't know of any better way with this instruction
1143c602fabSXin LI  * set to do unaligned loads, so do unaligned loads of big-endian
1155b0fe478SBruce M Simpson  * quantities the hard way - fetch the bytes one at a time and
1165b0fe478SBruce M Simpson  * assemble them.
1175b0fe478SBruce M Simpson  */
1185b0fe478SBruce M Simpson #define EXTRACT_16BITS(p) \
1198bdc5a62SPatrick Kelsey 	((uint16_t)(((uint16_t)(*((const uint8_t *)(p) + 0)) << 8) | \
1208bdc5a62SPatrick Kelsey 	            ((uint16_t)(*((const uint8_t *)(p) + 1)) << 0)))
1215b0fe478SBruce M Simpson #define EXTRACT_32BITS(p) \
1228bdc5a62SPatrick Kelsey 	((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 0)) << 24) | \
1238bdc5a62SPatrick Kelsey 	            ((uint32_t)(*((const uint8_t *)(p) + 1)) << 16) | \
1248bdc5a62SPatrick Kelsey 	            ((uint32_t)(*((const uint8_t *)(p) + 2)) << 8) | \
1258bdc5a62SPatrick Kelsey 	            ((uint32_t)(*((const uint8_t *)(p) + 3)) << 0)))
1261de50e9fSSam Leffler #define EXTRACT_64BITS(p) \
1278bdc5a62SPatrick Kelsey 	((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 56) | \
1288bdc5a62SPatrick Kelsey 	            ((uint64_t)(*((const uint8_t *)(p) + 1)) << 48) | \
1298bdc5a62SPatrick Kelsey 	            ((uint64_t)(*((const uint8_t *)(p) + 2)) << 40) | \
1308bdc5a62SPatrick Kelsey 	            ((uint64_t)(*((const uint8_t *)(p) + 3)) << 32) | \
1318bdc5a62SPatrick Kelsey 	            ((uint64_t)(*((const uint8_t *)(p) + 4)) << 24) | \
1328bdc5a62SPatrick Kelsey 	            ((uint64_t)(*((const uint8_t *)(p) + 5)) << 16) | \
1338bdc5a62SPatrick Kelsey 	            ((uint64_t)(*((const uint8_t *)(p) + 6)) << 8) | \
1348bdc5a62SPatrick Kelsey 	            ((uint64_t)(*((const uint8_t *)(p) + 7)) << 0)))
1353c602fabSXin LI #endif /* must special-case unaligned accesses */
1365b0fe478SBruce M Simpson #else /* LBL_ALIGN */
1375b0fe478SBruce M Simpson /*
1385b0fe478SBruce M Simpson  * The processor natively handles unaligned loads, so we can just
1395b0fe478SBruce M Simpson  * cast the pointer and fetch through it.
1405b0fe478SBruce M Simpson  */
1413c602fabSXin LI static inline uint16_t
142340b3427SPedro F. Giffuni EXTRACT_16BITS(const void *p)
143340b3427SPedro F. Giffuni {
1443c602fabSXin LI 	return ((uint16_t)ntohs(*(const uint16_t *)(p)));
145340b3427SPedro F. Giffuni }
146340b3427SPedro F. Giffuni 
1473c602fabSXin LI static inline uint32_t
148340b3427SPedro F. Giffuni EXTRACT_32BITS(const void *p)
149340b3427SPedro F. Giffuni {
1503c602fabSXin LI 	return ((uint32_t)ntohl(*(const uint32_t *)(p)));
151340b3427SPedro F. Giffuni }
152340b3427SPedro F. Giffuni 
1533c602fabSXin LI static inline uint64_t
154340b3427SPedro F. Giffuni EXTRACT_64BITS(const void *p)
155340b3427SPedro F. Giffuni {
156*3340d773SGleb Smirnoff 	return ((uint64_t)(((uint64_t)ntohl(*((const uint32_t *)(p) + 0))) << 32 |
1573c602fabSXin LI 		((uint64_t)ntohl(*((const uint32_t *)(p) + 1))) << 0));
158340b3427SPedro F. Giffuni 
159340b3427SPedro F. Giffuni }
160340b3427SPedro F. Giffuni 
1615b0fe478SBruce M Simpson #endif /* LBL_ALIGN */
1624edb46e9SPaul Traina 
1634edb46e9SPaul Traina #define EXTRACT_24BITS(p) \
1648bdc5a62SPatrick Kelsey 	((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 0)) << 16) | \
1658bdc5a62SPatrick Kelsey 	            ((uint32_t)(*((const uint8_t *)(p) + 1)) << 8) | \
1668bdc5a62SPatrick Kelsey 	            ((uint32_t)(*((const uint8_t *)(p) + 2)) << 0)))
1673c602fabSXin LI 
1683c602fabSXin LI #define EXTRACT_40BITS(p) \
1698bdc5a62SPatrick Kelsey 	((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 32) | \
1708bdc5a62SPatrick Kelsey 	            ((uint64_t)(*((const uint8_t *)(p) + 1)) << 24) | \
1718bdc5a62SPatrick Kelsey 	            ((uint64_t)(*((const uint8_t *)(p) + 2)) << 16) | \
1728bdc5a62SPatrick Kelsey 	            ((uint64_t)(*((const uint8_t *)(p) + 3)) << 8) | \
1738bdc5a62SPatrick Kelsey 	            ((uint64_t)(*((const uint8_t *)(p) + 4)) << 0)))
1743c602fabSXin LI 
1753c602fabSXin LI #define EXTRACT_48BITS(p) \
1768bdc5a62SPatrick Kelsey 	((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 40) | \
1778bdc5a62SPatrick Kelsey 	            ((uint64_t)(*((const uint8_t *)(p) + 1)) << 32) | \
1788bdc5a62SPatrick Kelsey 	            ((uint64_t)(*((const uint8_t *)(p) + 2)) << 24) | \
1798bdc5a62SPatrick Kelsey 	            ((uint64_t)(*((const uint8_t *)(p) + 3)) << 16) | \
1808bdc5a62SPatrick Kelsey 	            ((uint64_t)(*((const uint8_t *)(p) + 4)) << 8) | \
1818bdc5a62SPatrick Kelsey 	            ((uint64_t)(*((const uint8_t *)(p) + 5)) << 0)))
1823c602fabSXin LI 
1833c602fabSXin LI #define EXTRACT_56BITS(p) \
1848bdc5a62SPatrick Kelsey 	((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 48) | \
1858bdc5a62SPatrick Kelsey 	            ((uint64_t)(*((const uint8_t *)(p) + 1)) << 40) | \
1868bdc5a62SPatrick Kelsey 	            ((uint64_t)(*((const uint8_t *)(p) + 2)) << 32) | \
1878bdc5a62SPatrick Kelsey 	            ((uint64_t)(*((const uint8_t *)(p) + 3)) << 24) | \
1888bdc5a62SPatrick Kelsey 	            ((uint64_t)(*((const uint8_t *)(p) + 4)) << 16) | \
1898bdc5a62SPatrick Kelsey 	            ((uint64_t)(*((const uint8_t *)(p) + 5)) << 8) | \
1908bdc5a62SPatrick Kelsey 	            ((uint64_t)(*((const uint8_t *)(p) + 6)) << 0)))
1914edb46e9SPaul Traina 
1921de50e9fSSam Leffler /*
1931de50e9fSSam Leffler  * Macros to extract possibly-unaligned little-endian integral values.
1941de50e9fSSam Leffler  * XXX - do loads on little-endian machines that support unaligned loads?
1951de50e9fSSam Leffler  */
1964edb46e9SPaul Traina #define EXTRACT_LE_8BITS(p) (*(p))
1974edb46e9SPaul Traina #define EXTRACT_LE_16BITS(p) \
1988bdc5a62SPatrick Kelsey 	((uint16_t)(((uint16_t)(*((const uint8_t *)(p) + 1)) << 8) | \
1998bdc5a62SPatrick Kelsey 	            ((uint16_t)(*((const uint8_t *)(p) + 0)) << 0)))
2004edb46e9SPaul Traina #define EXTRACT_LE_32BITS(p) \
2018bdc5a62SPatrick Kelsey 	((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 3)) << 24) | \
2028bdc5a62SPatrick Kelsey 	            ((uint32_t)(*((const uint8_t *)(p) + 2)) << 16) | \
2038bdc5a62SPatrick Kelsey 	            ((uint32_t)(*((const uint8_t *)(p) + 1)) << 8) | \
2048bdc5a62SPatrick Kelsey 	            ((uint32_t)(*((const uint8_t *)(p) + 0)) << 0)))
205a5779b6eSRui Paulo #define EXTRACT_LE_24BITS(p) \
2068bdc5a62SPatrick Kelsey 	((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 2)) << 16) | \
2078bdc5a62SPatrick Kelsey 	            ((uint32_t)(*((const uint8_t *)(p) + 1)) << 8) | \
2088bdc5a62SPatrick Kelsey 	            ((uint32_t)(*((const uint8_t *)(p) + 0)) << 0)))
2091de50e9fSSam Leffler #define EXTRACT_LE_64BITS(p) \
2108bdc5a62SPatrick Kelsey 	((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 7)) << 56) | \
2118bdc5a62SPatrick Kelsey 	            ((uint64_t)(*((const uint8_t *)(p) + 6)) << 48) | \
2128bdc5a62SPatrick Kelsey 	            ((uint64_t)(*((const uint8_t *)(p) + 5)) << 40) | \
2138bdc5a62SPatrick Kelsey 	            ((uint64_t)(*((const uint8_t *)(p) + 4)) << 32) | \
2148bdc5a62SPatrick Kelsey 	            ((uint64_t)(*((const uint8_t *)(p) + 3)) << 24) | \
2158bdc5a62SPatrick Kelsey 	            ((uint64_t)(*((const uint8_t *)(p) + 2)) << 16) | \
2168bdc5a62SPatrick Kelsey 	            ((uint64_t)(*((const uint8_t *)(p) + 1)) << 8) | \
2178bdc5a62SPatrick Kelsey 	            ((uint64_t)(*((const uint8_t *)(p) + 0)) << 0)))
218*3340d773SGleb Smirnoff 
219*3340d773SGleb Smirnoff /*
220*3340d773SGleb Smirnoff  * Macros to check the presence of the values in question.
221*3340d773SGleb Smirnoff  */
222*3340d773SGleb Smirnoff #define ND_TTEST_8BITS(p) ND_TTEST2(*(p), 1)
223*3340d773SGleb Smirnoff #define ND_TCHECK_8BITS(p) ND_TCHECK2(*(p), 1)
224*3340d773SGleb Smirnoff 
225*3340d773SGleb Smirnoff #define ND_TTEST_16BITS(p) ND_TTEST2(*(p), 2)
226*3340d773SGleb Smirnoff #define ND_TCHECK_16BITS(p) ND_TCHECK2(*(p), 2)
227*3340d773SGleb Smirnoff 
228*3340d773SGleb Smirnoff #define ND_TTEST_24BITS(p) ND_TTEST2(*(p), 3)
229*3340d773SGleb Smirnoff #define ND_TCHECK_24BITS(p) ND_TCHECK2(*(p), 3)
230*3340d773SGleb Smirnoff 
231*3340d773SGleb Smirnoff #define ND_TTEST_32BITS(p) ND_TTEST2(*(p), 4)
232*3340d773SGleb Smirnoff #define ND_TCHECK_32BITS(p) ND_TCHECK2(*(p), 4)
233*3340d773SGleb Smirnoff 
234*3340d773SGleb Smirnoff #define ND_TTEST_40BITS(p) ND_TTEST2(*(p), 5)
235*3340d773SGleb Smirnoff #define ND_TCHECK_40BITS(p) ND_TCHECK2(*(p), 5)
236*3340d773SGleb Smirnoff 
237*3340d773SGleb Smirnoff #define ND_TTEST_48BITS(p) ND_TTEST2(*(p), 6)
238*3340d773SGleb Smirnoff #define ND_TCHECK_48BITS(p) ND_TCHECK2(*(p), 6)
239*3340d773SGleb Smirnoff 
240*3340d773SGleb Smirnoff #define ND_TTEST_56BITS(p) ND_TTEST2(*(p), 7)
241*3340d773SGleb Smirnoff #define ND_TCHECK_56BITS(p) ND_TCHECK2(*(p), 7)
242*3340d773SGleb Smirnoff 
243*3340d773SGleb Smirnoff #define ND_TTEST_64BITS(p) ND_TTEST2(*(p), 8)
244*3340d773SGleb Smirnoff #define ND_TCHECK_64BITS(p) ND_TCHECK2(*(p), 8)
245