xref: /openbsd-src/usr.sbin/tcpdump/extract.h (revision 8500990981f885cbe5e6a4958549cacc238b5ae6)
1 /*	$OpenBSD: extract.h,v 1.7 2002/09/03 12:21:12 ho Exp $	*/
2 
3 /*
4  * Copyright (c) 1992, 1993, 1994, 1995, 1996
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that: (1) source code distributions
9  * retain the above copyright notice and this paragraph in its entirety, (2)
10  * distributions including binary code include the above copyright notice and
11  * this paragraph in its entirety in the documentation or other materials
12  * provided with the distribution, and (3) all advertising materials mentioning
13  * features or use of this software display the following acknowledgement:
14  * ``This product includes software developed by the University of California,
15  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
16  * the University nor the names of its contributors may be used to endorse
17  * or promote products derived from this software without specific prior
18  * written permission.
19  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
20  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
21  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22  *
23  * @(#) $TCPDUMP: /tcpdump/master/tcpdump/extract.h,v 1.17 2001/09/17 21:57:52 fenner Exp $ (LBL)
24  */
25 
26 /* Network to host order macros */
27 
28 #ifdef LBL_ALIGN
29 #define EXTRACT_16BITS(p) \
30 	((u_int16_t)*((const u_int8_t *)(p) + 0) << 8 | \
31 	(u_int16_t)*((const u_int8_t *)(p) + 1))
32 #define EXTRACT_32BITS(p) \
33 	((u_int32_t)*((const u_int8_t *)(p) + 0) << 24 | \
34 	(u_int32_t)*((const u_int8_t *)(p) + 1) << 16 | \
35 	(u_int32_t)*((const u_int8_t *)(p) + 2) << 8 | \
36 	(u_int32_t)*((const u_int8_t *)(p) + 3))
37 #else
38 #define EXTRACT_16BITS(p) \
39 	((u_int16_t)ntohs(*(const u_int16_t *)(p)))
40 #define EXTRACT_32BITS(p) \
41 	((u_int32_t)ntohl(*(const u_int32_t *)(p)))
42 #endif
43 
44 #define EXTRACT_24BITS(p) \
45 	((u_int32_t)*((const u_int8_t *)(p) + 0) << 16 | \
46 	(u_int32_t)*((const u_int8_t *)(p) + 1) << 8 | \
47 	(u_int32_t)*((const u_int8_t *)(p) + 2))
48 
49 /* Little endian protocol host order macros */
50 
51 #define EXTRACT_LE_8BITS(p) (*(p))
52 #define EXTRACT_LE_16BITS(p) \
53 	((u_int16_t)*((const u_int8_t *)(p) + 1) << 8 | \
54 	(u_int16_t)*((const u_int8_t *)(p) + 0))
55 #define EXTRACT_LE_32BITS(p) \
56 	((u_int32_t)*((const u_int8_t *)(p) + 3) << 24 | \
57 	(u_int32_t)*((const u_int8_t *)(p) + 2) << 16 | \
58 	(u_int32_t)*((const u_int8_t *)(p) + 1) << 8 | \
59 	(u_int32_t)*((const u_int8_t *)(p) + 0))
60