xref: /openbsd-src/usr.sbin/tcpdump/extract.h (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: extract.h,v 1.6 2000/10/03 14:31:54 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  * @(#) $Header: /home/cvs/src/usr.sbin/tcpdump/extract.h,v 1.6 2000/10/03 14:31:54 ho Exp $ (LBL)
24  */
25 
26 /* Network to host order macros */
27 
28 #ifdef LBL_ALIGN
29 #define EXTRACT_16BITS(p) \
30 	((u_short)*((u_char *)(p) + 0) << 8 | \
31 	(u_short)*((u_char *)(p) + 1))
32 #define EXTRACT_32BITS(p) \
33 	((u_int32_t)*((u_char *)(p) + 0) << 24 | \
34 	(u_int32_t)*((u_char *)(p) + 1) << 16 | \
35 	(u_int32_t)*((u_char *)(p) + 2) << 8 | \
36 	(u_int32_t)*((u_char *)(p) + 3))
37 #else
38 #define EXTRACT_16BITS(p) \
39 	((u_short)ntohs(*(u_short *)(p)))
40 #define EXTRACT_32BITS(p) \
41 	((u_int32_t)ntohl(*(u_int32_t *)(p)))
42 #endif
43 
44 #define EXTRACT_24BITS(p) \
45 	((u_int32_t)*((u_char *)(p) + 0) << 16 | \
46 	(u_int32_t)*((u_char *)(p) + 1) << 8 | \
47 	(u_int32_t)*((u_char *)(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_short)*((u_char *)(p) + 1) << 8 | \
54 	(u_short)*((u_char *)(p) + 0))
55 #define EXTRACT_LE_32BITS(p) \
56 	((u_int32_t)*((u_char *)(p) + 3) << 24 | \
57 	(u_int32_t)*((u_char *)(p) + 2) << 16 | \
58 	(u_int32_t)*((u_char *)(p) + 1) << 8 | \
59 	(u_int32_t)*((u_char *)(p) + 0))
60