xref: /freebsd/contrib/tcpdump/extract.h (revision 340b342723415525f3dc424bed8592e9ff715817)
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  *
21a5779b6eSRui Paulo  * @(#) $Header: /tcpdump/master/tcpdump/extract.h,v 1.25 2006-01-30 16:20:07 hannes Exp $ (LBL)
224edb46e9SPaul Traina  */
234edb46e9SPaul Traina 
241de50e9fSSam Leffler /*
251de50e9fSSam Leffler  * Macros to extract possibly-unaligned big-endian integral values.
261de50e9fSSam Leffler  */
274edb46e9SPaul Traina #ifdef LBL_ALIGN
285b0fe478SBruce M Simpson /*
295b0fe478SBruce M Simpson  * The processor doesn't natively handle unaligned loads.
305b0fe478SBruce M Simpson  */
315b0fe478SBruce M Simpson #ifdef HAVE___ATTRIBUTE__
325b0fe478SBruce M Simpson /*
335b0fe478SBruce M Simpson  * We have __attribute__; we assume that means we have __attribute__((packed)).
345b0fe478SBruce M Simpson  * Declare packed structures containing a u_int16_t and a u_int32_t,
355b0fe478SBruce M Simpson  * cast the pointer to point to one of those, and fetch through it;
365b0fe478SBruce M Simpson  * the GCC manual doesn't appear to explicitly say that
375b0fe478SBruce M Simpson  * __attribute__((packed)) causes the compiler to generate unaligned-safe
385b0fe478SBruce M Simpson  * code, but it apppears to do so.
395b0fe478SBruce M Simpson  *
405b0fe478SBruce M Simpson  * We do this in case the compiler can generate, for this instruction set,
415b0fe478SBruce M Simpson  * better code to do an unaligned load and pass stuff to "ntohs()" or
425b0fe478SBruce M Simpson  * "ntohl()" than the code to fetch the bytes one at a time and
435b0fe478SBruce M Simpson  * assemble them.  (That might not be the case on a little-endian platform,
445b0fe478SBruce M Simpson  * where "ntohs()" and "ntohl()" might not be done inline.)
455b0fe478SBruce M Simpson  */
465b0fe478SBruce M Simpson typedef struct {
475b0fe478SBruce M Simpson 	u_int16_t	val;
485b0fe478SBruce M Simpson } __attribute__((packed)) unaligned_u_int16_t;
495b0fe478SBruce M Simpson 
505b0fe478SBruce M Simpson typedef struct {
515b0fe478SBruce M Simpson 	u_int32_t	val;
525b0fe478SBruce M Simpson } __attribute__((packed)) unaligned_u_int32_t;
535b0fe478SBruce M Simpson 
54*340b3427SPedro F. Giffuni static inline u_int16_t
55*340b3427SPedro F. Giffuni EXTRACT_16BITS(const void *p)
56*340b3427SPedro F. Giffuni {
57*340b3427SPedro F. Giffuni 	return ((u_int16_t)ntohs(((const unaligned_u_int16_t *)(p))->val));
58*340b3427SPedro F. Giffuni }
59*340b3427SPedro F. Giffuni 
60*340b3427SPedro F. Giffuni static inline u_int32_t
61*340b3427SPedro F. Giffuni EXTRACT_32BITS(const void *p)
62*340b3427SPedro F. Giffuni {
63*340b3427SPedro F. Giffuni 	return ((u_int32_t)ntohl(((const unaligned_u_int32_t *)(p))->val));
64*340b3427SPedro F. Giffuni }
65*340b3427SPedro F. Giffuni 
66*340b3427SPedro F. Giffuni static inline u_int64_t
67*340b3427SPedro F. Giffuni EXTRACT_64BITS(const void *p)
68*340b3427SPedro F. Giffuni {
69*340b3427SPedro F. Giffuni 	return ((u_int64_t)(((u_int64_t)ntohl(((const unaligned_u_int32_t *)(p) + 0)->val)) << 32 | \
70*340b3427SPedro F. Giffuni 		((u_int64_t)ntohl(((const unaligned_u_int32_t *)(p) + 1)->val)) << 0));
71*340b3427SPedro F. Giffuni 
72*340b3427SPedro F. Giffuni }
731de50e9fSSam Leffler 
745b0fe478SBruce M Simpson #else /* HAVE___ATTRIBUTE__ */
755b0fe478SBruce M Simpson /*
765b0fe478SBruce M Simpson  * We don't have __attribute__, so do unaligned loads of big-endian
775b0fe478SBruce M Simpson  * quantities the hard way - fetch the bytes one at a time and
785b0fe478SBruce M Simpson  * assemble them.
795b0fe478SBruce M Simpson  */
805b0fe478SBruce M Simpson #define EXTRACT_16BITS(p) \
815b0fe478SBruce M Simpson 	((u_int16_t)((u_int16_t)*((const u_int8_t *)(p) + 0) << 8 | \
825b0fe478SBruce M Simpson 		     (u_int16_t)*((const u_int8_t *)(p) + 1)))
835b0fe478SBruce M Simpson #define EXTRACT_32BITS(p) \
845b0fe478SBruce M Simpson 	((u_int32_t)((u_int32_t)*((const u_int8_t *)(p) + 0) << 24 | \
85a90e161bSBill Fenner 		     (u_int32_t)*((const u_int8_t *)(p) + 1) << 16 | \
86a90e161bSBill Fenner 		     (u_int32_t)*((const u_int8_t *)(p) + 2) << 8 | \
875b0fe478SBruce M Simpson 		     (u_int32_t)*((const u_int8_t *)(p) + 3)))
881de50e9fSSam Leffler #define EXTRACT_64BITS(p) \
891de50e9fSSam Leffler 	((u_int64_t)((u_int64_t)*((const u_int8_t *)(p) + 0) << 56 | \
901de50e9fSSam Leffler 		     (u_int64_t)*((const u_int8_t *)(p) + 1) << 48 | \
911de50e9fSSam Leffler 		     (u_int64_t)*((const u_int8_t *)(p) + 2) << 40 | \
921de50e9fSSam Leffler 		     (u_int64_t)*((const u_int8_t *)(p) + 3) << 32 | \
931de50e9fSSam Leffler 	             (u_int64_t)*((const u_int8_t *)(p) + 4) << 24 | \
941de50e9fSSam Leffler 		     (u_int64_t)*((const u_int8_t *)(p) + 5) << 16 | \
951de50e9fSSam Leffler 		     (u_int64_t)*((const u_int8_t *)(p) + 6) << 8 | \
961de50e9fSSam Leffler 		     (u_int64_t)*((const u_int8_t *)(p) + 7)))
975b0fe478SBruce M Simpson #endif /* HAVE___ATTRIBUTE__ */
985b0fe478SBruce M Simpson #else /* LBL_ALIGN */
995b0fe478SBruce M Simpson /*
1005b0fe478SBruce M Simpson  * The processor natively handles unaligned loads, so we can just
1015b0fe478SBruce M Simpson  * cast the pointer and fetch through it.
1025b0fe478SBruce M Simpson  */
103*340b3427SPedro F. Giffuni static inline u_int16_t
104*340b3427SPedro F. Giffuni EXTRACT_16BITS(const void *p)
105*340b3427SPedro F. Giffuni {
106*340b3427SPedro F. Giffuni 	return ((u_int16_t)ntohs(*(const u_int16_t *)(p)));
107*340b3427SPedro F. Giffuni }
108*340b3427SPedro F. Giffuni 
109*340b3427SPedro F. Giffuni static inline u_int32_t
110*340b3427SPedro F. Giffuni EXTRACT_32BITS(const void *p)
111*340b3427SPedro F. Giffuni {
112*340b3427SPedro F. Giffuni 	return ((u_int32_t)ntohl(*(const u_int32_t *)(p)));
113*340b3427SPedro F. Giffuni }
114*340b3427SPedro F. Giffuni 
115*340b3427SPedro F. Giffuni static inline u_int64_t
116*340b3427SPedro F. Giffuni EXTRACT_64BITS(const void *p)
117*340b3427SPedro F. Giffuni {
118*340b3427SPedro F. Giffuni 	return ((u_int64_t)(((u_int64_t)ntohl(*((const u_int32_t *)(p) + 0))) << 32 | \
119*340b3427SPedro F. Giffuni 		((u_int64_t)ntohl(*((const u_int32_t *)(p) + 1))) << 0));
120*340b3427SPedro F. Giffuni 
121*340b3427SPedro F. Giffuni }
122*340b3427SPedro F. Giffuni 
1235b0fe478SBruce M Simpson #endif /* LBL_ALIGN */
1244edb46e9SPaul Traina 
1254edb46e9SPaul Traina #define EXTRACT_24BITS(p) \
1265b0fe478SBruce M Simpson 	((u_int32_t)((u_int32_t)*((const u_int8_t *)(p) + 0) << 16 | \
127a90e161bSBill Fenner 		     (u_int32_t)*((const u_int8_t *)(p) + 1) << 8 | \
1285b0fe478SBruce M Simpson 		     (u_int32_t)*((const u_int8_t *)(p) + 2)))
1294edb46e9SPaul Traina 
1301de50e9fSSam Leffler /*
1311de50e9fSSam Leffler  * Macros to extract possibly-unaligned little-endian integral values.
1321de50e9fSSam Leffler  * XXX - do loads on little-endian machines that support unaligned loads?
1331de50e9fSSam Leffler  */
1344edb46e9SPaul Traina #define EXTRACT_LE_8BITS(p) (*(p))
1354edb46e9SPaul Traina #define EXTRACT_LE_16BITS(p) \
1365b0fe478SBruce M Simpson 	((u_int16_t)((u_int16_t)*((const u_int8_t *)(p) + 1) << 8 | \
1375b0fe478SBruce M Simpson 		     (u_int16_t)*((const u_int8_t *)(p) + 0)))
1384edb46e9SPaul Traina #define EXTRACT_LE_32BITS(p) \
1395b0fe478SBruce M Simpson 	((u_int32_t)((u_int32_t)*((const u_int8_t *)(p) + 3) << 24 | \
140a90e161bSBill Fenner 		     (u_int32_t)*((const u_int8_t *)(p) + 2) << 16 | \
141a90e161bSBill Fenner 		     (u_int32_t)*((const u_int8_t *)(p) + 1) << 8 | \
1425b0fe478SBruce M Simpson 		     (u_int32_t)*((const u_int8_t *)(p) + 0)))
143a5779b6eSRui Paulo #define EXTRACT_LE_24BITS(p) \
144a5779b6eSRui Paulo 	((u_int32_t)((u_int32_t)*((const u_int8_t *)(p) + 2) << 16 | \
145a5779b6eSRui Paulo 		     (u_int32_t)*((const u_int8_t *)(p) + 1) << 8 | \
146a5779b6eSRui Paulo 		     (u_int32_t)*((const u_int8_t *)(p) + 0)))
1471de50e9fSSam Leffler #define EXTRACT_LE_64BITS(p) \
1481de50e9fSSam Leffler 	((u_int64_t)((u_int64_t)*((const u_int8_t *)(p) + 7) << 56 | \
1491de50e9fSSam Leffler 		     (u_int64_t)*((const u_int8_t *)(p) + 6) << 48 | \
1501de50e9fSSam Leffler 		     (u_int64_t)*((const u_int8_t *)(p) + 5) << 40 | \
1511de50e9fSSam Leffler 		     (u_int64_t)*((const u_int8_t *)(p) + 4) << 32 | \
1521de50e9fSSam Leffler 	             (u_int64_t)*((const u_int8_t *)(p) + 3) << 24 | \
1531de50e9fSSam Leffler 		     (u_int64_t)*((const u_int8_t *)(p) + 2) << 16 | \
1541de50e9fSSam Leffler 		     (u_int64_t)*((const u_int8_t *)(p) + 1) << 8 | \
1551de50e9fSSam Leffler 		     (u_int64_t)*((const u_int8_t *)(p) + 0)))
156