xref: /freebsd/contrib/tcpdump/extract.h (revision 1de50e9f417616cf647a842762944a2301cb1415)
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  *
211de50e9fSSam Leffler  * @(#) $Header: /tcpdump/master/tcpdump/extract.h,v 1.24 2005/01/15 02:06:50 guy 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 
544edb46e9SPaul Traina #define EXTRACT_16BITS(p) \
555b0fe478SBruce M Simpson 	((u_int16_t)ntohs(((const unaligned_u_int16_t *)(p))->val))
564edb46e9SPaul Traina #define EXTRACT_32BITS(p) \
575b0fe478SBruce M Simpson 	((u_int32_t)ntohl(((const unaligned_u_int32_t *)(p))->val))
581de50e9fSSam Leffler #define EXTRACT_64BITS(p) \
591de50e9fSSam Leffler 	((u_int64_t)(((u_int64_t)ntohl(((const unaligned_u_int32_t *)(p) + 0)->val)) << 32 | \
601de50e9fSSam Leffler 		     ((u_int64_t)ntohl(((const unaligned_u_int32_t *)(p) + 1)->val)) << 0))
611de50e9fSSam Leffler 
625b0fe478SBruce M Simpson #else /* HAVE___ATTRIBUTE__ */
635b0fe478SBruce M Simpson /*
645b0fe478SBruce M Simpson  * We don't have __attribute__, so do unaligned loads of big-endian
655b0fe478SBruce M Simpson  * quantities the hard way - fetch the bytes one at a time and
665b0fe478SBruce M Simpson  * assemble them.
675b0fe478SBruce M Simpson  */
685b0fe478SBruce M Simpson #define EXTRACT_16BITS(p) \
695b0fe478SBruce M Simpson 	((u_int16_t)((u_int16_t)*((const u_int8_t *)(p) + 0) << 8 | \
705b0fe478SBruce M Simpson 		     (u_int16_t)*((const u_int8_t *)(p) + 1)))
715b0fe478SBruce M Simpson #define EXTRACT_32BITS(p) \
725b0fe478SBruce M Simpson 	((u_int32_t)((u_int32_t)*((const u_int8_t *)(p) + 0) << 24 | \
73a90e161bSBill Fenner 		     (u_int32_t)*((const u_int8_t *)(p) + 1) << 16 | \
74a90e161bSBill Fenner 		     (u_int32_t)*((const u_int8_t *)(p) + 2) << 8 | \
755b0fe478SBruce M Simpson 		     (u_int32_t)*((const u_int8_t *)(p) + 3)))
761de50e9fSSam Leffler #define EXTRACT_64BITS(p) \
771de50e9fSSam Leffler 	((u_int64_t)((u_int64_t)*((const u_int8_t *)(p) + 0) << 56 | \
781de50e9fSSam Leffler 		     (u_int64_t)*((const u_int8_t *)(p) + 1) << 48 | \
791de50e9fSSam Leffler 		     (u_int64_t)*((const u_int8_t *)(p) + 2) << 40 | \
801de50e9fSSam Leffler 		     (u_int64_t)*((const u_int8_t *)(p) + 3) << 32 | \
811de50e9fSSam Leffler 	             (u_int64_t)*((const u_int8_t *)(p) + 4) << 24 | \
821de50e9fSSam Leffler 		     (u_int64_t)*((const u_int8_t *)(p) + 5) << 16 | \
831de50e9fSSam Leffler 		     (u_int64_t)*((const u_int8_t *)(p) + 6) << 8 | \
841de50e9fSSam Leffler 		     (u_int64_t)*((const u_int8_t *)(p) + 7)))
855b0fe478SBruce M Simpson #endif /* HAVE___ATTRIBUTE__ */
865b0fe478SBruce M Simpson #else /* LBL_ALIGN */
875b0fe478SBruce M Simpson /*
885b0fe478SBruce M Simpson  * The processor natively handles unaligned loads, so we can just
895b0fe478SBruce M Simpson  * cast the pointer and fetch through it.
905b0fe478SBruce M Simpson  */
914edb46e9SPaul Traina #define EXTRACT_16BITS(p) \
92a90e161bSBill Fenner 	((u_int16_t)ntohs(*(const u_int16_t *)(p)))
934edb46e9SPaul Traina #define EXTRACT_32BITS(p) \
94a90e161bSBill Fenner 	((u_int32_t)ntohl(*(const u_int32_t *)(p)))
951de50e9fSSam Leffler #define EXTRACT_64BITS(p) \
961de50e9fSSam Leffler 	((u_int64_t)(((u_int64_t)ntohl(*((const u_int32_t *)(p) + 0))) << 32 | \
971de50e9fSSam Leffler 		     ((u_int64_t)ntohl(*((const u_int32_t *)(p) + 1))) << 0))
985b0fe478SBruce M Simpson #endif /* LBL_ALIGN */
994edb46e9SPaul Traina 
1004edb46e9SPaul Traina #define EXTRACT_24BITS(p) \
1015b0fe478SBruce M Simpson 	((u_int32_t)((u_int32_t)*((const u_int8_t *)(p) + 0) << 16 | \
102a90e161bSBill Fenner 		     (u_int32_t)*((const u_int8_t *)(p) + 1) << 8 | \
1035b0fe478SBruce M Simpson 		     (u_int32_t)*((const u_int8_t *)(p) + 2)))
1044edb46e9SPaul Traina 
1051de50e9fSSam Leffler /*
1061de50e9fSSam Leffler  * Macros to extract possibly-unaligned little-endian integral values.
1071de50e9fSSam Leffler  * XXX - do loads on little-endian machines that support unaligned loads?
1081de50e9fSSam Leffler  */
1094edb46e9SPaul Traina #define EXTRACT_LE_8BITS(p) (*(p))
1104edb46e9SPaul Traina #define EXTRACT_LE_16BITS(p) \
1115b0fe478SBruce M Simpson 	((u_int16_t)((u_int16_t)*((const u_int8_t *)(p) + 1) << 8 | \
1125b0fe478SBruce M Simpson 		     (u_int16_t)*((const u_int8_t *)(p) + 0)))
1134edb46e9SPaul Traina #define EXTRACT_LE_32BITS(p) \
1145b0fe478SBruce M Simpson 	((u_int32_t)((u_int32_t)*((const u_int8_t *)(p) + 3) << 24 | \
115a90e161bSBill Fenner 		     (u_int32_t)*((const u_int8_t *)(p) + 2) << 16 | \
116a90e161bSBill Fenner 		     (u_int32_t)*((const u_int8_t *)(p) + 1) << 8 | \
1175b0fe478SBruce M Simpson 		     (u_int32_t)*((const u_int8_t *)(p) + 0)))
1181de50e9fSSam Leffler #define EXTRACT_LE_64BITS(p) \
1191de50e9fSSam Leffler 	((u_int64_t)((u_int64_t)*((const u_int8_t *)(p) + 7) << 56 | \
1201de50e9fSSam Leffler 		     (u_int64_t)*((const u_int8_t *)(p) + 6) << 48 | \
1211de50e9fSSam Leffler 		     (u_int64_t)*((const u_int8_t *)(p) + 5) << 40 | \
1221de50e9fSSam Leffler 		     (u_int64_t)*((const u_int8_t *)(p) + 4) << 32 | \
1231de50e9fSSam Leffler 	             (u_int64_t)*((const u_int8_t *)(p) + 3) << 24 | \
1241de50e9fSSam Leffler 		     (u_int64_t)*((const u_int8_t *)(p) + 2) << 16 | \
1251de50e9fSSam Leffler 		     (u_int64_t)*((const u_int8_t *)(p) + 1) << 8 | \
1261de50e9fSSam Leffler 		     (u_int64_t)*((const u_int8_t *)(p) + 0)))
127